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