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