class-wp-widget-calendar.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * Widget API: WP_Widget_Calendar class
  4. *
  5. * @package WordPress
  6. * @subpackage Widgets
  7. * @since 4.4.0
  8. */
  9. /**
  10. * Core class used to implement the Calendar widget.
  11. *
  12. * @since 2.8.0
  13. *
  14. * @see WP_Widget
  15. */
  16. class WP_Widget_Calendar extends WP_Widget {
  17. /**
  18. * Ensure that the ID attribute only appears in the markup once
  19. *
  20. * @since 4.4.0
  21. * @var int
  22. */
  23. private static $instance = 0;
  24. /**
  25. * Sets up a new Calendar widget instance.
  26. *
  27. * @since 2.8.0
  28. */
  29. public function __construct() {
  30. $widget_ops = array(
  31. 'classname' => 'widget_calendar',
  32. 'description' => __( 'A calendar of your site’s posts.' ),
  33. 'customize_selective_refresh' => true,
  34. );
  35. parent::__construct( 'calendar', __( 'Calendar' ), $widget_ops );
  36. }
  37. /**
  38. * Outputs the content for the current Calendar widget instance.
  39. *
  40. * @since 2.8.0
  41. *
  42. * @param array $args Display arguments including 'before_title', 'after_title',
  43. * 'before_widget', and 'after_widget'.
  44. * @param array $instance The settings for the particular instance of the widget.
  45. */
  46. public function widget( $args, $instance ) {
  47. $title = ! empty( $instance['title'] ) ? $instance['title'] : '';
  48. /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
  49. $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
  50. echo $args['before_widget'];
  51. if ( $title ) {
  52. echo $args['before_title'] . $title . $args['after_title'];
  53. }
  54. if ( 0 === self::$instance ) {
  55. echo '<div id="calendar_wrap" class="calendar_wrap">';
  56. } else {
  57. echo '<div class="calendar_wrap">';
  58. }
  59. get_calendar();
  60. echo '</div>';
  61. echo $args['after_widget'];
  62. self::$instance++;
  63. }
  64. /**
  65. * Handles updating settings for the current Calendar widget instance.
  66. *
  67. * @since 2.8.0
  68. *
  69. * @param array $new_instance New settings for this instance as input by the user via
  70. * WP_Widget::form().
  71. * @param array $old_instance Old settings for this instance.
  72. * @return array Updated settings to save.
  73. */
  74. public function update( $new_instance, $old_instance ) {
  75. $instance = $old_instance;
  76. $instance['title'] = sanitize_text_field( $new_instance['title'] );
  77. return $instance;
  78. }
  79. /**
  80. * Outputs the settings form for the Calendar widget.
  81. *
  82. * @since 2.8.0
  83. *
  84. * @param array $instance Current settings.
  85. */
  86. public function form( $instance ) {
  87. $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
  88. ?>
  89. <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
  90. <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" /></p>
  91. <?php
  92. }
  93. }