class-wp-widget-calendar.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. *
  22. * @static
  23. * @var int
  24. */
  25. private static $instance = 0;
  26. /**
  27. * Sets up a new Calendar widget instance.
  28. *
  29. * @since 2.8.0
  30. */
  31. public function __construct() {
  32. $widget_ops = array(
  33. 'classname' => 'widget_calendar',
  34. 'description' => __( 'A calendar of your site&#8217;s Posts.' ),
  35. 'customize_selective_refresh' => true,
  36. );
  37. parent::__construct( 'calendar', __( 'Calendar' ), $widget_ops );
  38. }
  39. /**
  40. * Outputs the content for the current Calendar widget instance.
  41. *
  42. * @since 2.8.0
  43. *
  44. * @param array $args Display arguments including 'before_title', 'after_title',
  45. * 'before_widget', and 'after_widget'.
  46. * @param array $instance The settings for the particular instance of the widget.
  47. */
  48. public function widget( $args, $instance ) {
  49. $title = ! empty( $instance['title'] ) ? $instance['title'] : '';
  50. /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
  51. $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
  52. echo $args['before_widget'];
  53. if ( $title ) {
  54. echo $args['before_title'] . $title . $args['after_title'];
  55. }
  56. if ( 0 === self::$instance ) {
  57. echo '<div id="calendar_wrap" class="calendar_wrap">';
  58. } else {
  59. echo '<div class="calendar_wrap">';
  60. }
  61. get_calendar();
  62. echo '</div>';
  63. echo $args['after_widget'];
  64. self::$instance++;
  65. }
  66. /**
  67. * Handles updating settings for the current Calendar widget instance.
  68. *
  69. * @since 2.8.0
  70. *
  71. * @param array $new_instance New settings for this instance as input by the user via
  72. * WP_Widget::form().
  73. * @param array $old_instance Old settings for this instance.
  74. * @return array Updated settings to save.
  75. */
  76. public function update( $new_instance, $old_instance ) {
  77. $instance = $old_instance;
  78. $instance['title'] = sanitize_text_field( $new_instance['title'] );
  79. return $instance;
  80. }
  81. /**
  82. * Outputs the settings form for the Calendar widget.
  83. *
  84. * @since 2.8.0
  85. *
  86. * @param array $instance Current settings.
  87. */
  88. public function form( $instance ) {
  89. $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
  90. $title = sanitize_text_field( $instance['title'] );
  91. ?>
  92. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
  93. <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($title); ?>" /></p>
  94. <?php
  95. }
  96. }