WordPress 4.5
[autoinstalls/wordpress.git] / wp-includes / widgets / class-wp-widget-archives.php
1 <?php
2 /**
3  * Widget API: WP_Widget_Archives class
4  *
5  * @package WordPress
6  * @subpackage Widgets
7  * @since 4.4.0
8  */
9
10 /**
11  * Core class used to implement the Archives widget.
12  *
13  * @since 2.8.0
14  *
15  * @see WP_Widget
16  */
17 class WP_Widget_Archives extends WP_Widget {
18
19         /**
20          * Sets up a new Archives widget instance.
21          *
22          * @since 2.8.0
23          * @access public
24          */
25         public function __construct() {
26                 $widget_ops = array(
27                         'classname' => 'widget_archive',
28                         'description' => __( 'A monthly archive of your site&#8217;s Posts.' ),
29                         'customize_selective_refresh' => true,
30                 );
31                 parent::__construct('archives', __('Archives'), $widget_ops);
32         }
33
34         /**
35          * Outputs the content for the current Archives widget instance.
36          *
37          * @since 2.8.0
38          * @access public
39          *
40          * @param array $args     Display arguments including 'before_title', 'after_title',
41          *                        'before_widget', and 'after_widget'.
42          * @param array $instance Settings for the current Archives widget instance.
43          */
44         public function widget( $args, $instance ) {
45                 $c = ! empty( $instance['count'] ) ? '1' : '0';
46                 $d = ! empty( $instance['dropdown'] ) ? '1' : '0';
47
48                 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
49                 $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Archives' ) : $instance['title'], $instance, $this->id_base );
50
51                 echo $args['before_widget'];
52                 if ( $title ) {
53                         echo $args['before_title'] . $title . $args['after_title'];
54                 }
55
56                 if ( $d ) {
57                         $dropdown_id = "{$this->id_base}-dropdown-{$this->number}";
58                         ?>
59                 <label class="screen-reader-text" for="<?php echo esc_attr( $dropdown_id ); ?>"><?php echo $title; ?></label>
60                 <select id="<?php echo esc_attr( $dropdown_id ); ?>" name="archive-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'>
61                         <?php
62                         /**
63                          * Filter the arguments for the Archives widget drop-down.
64                          *
65                          * @since 2.8.0
66                          *
67                          * @see wp_get_archives()
68                          *
69                          * @param array $args An array of Archives widget drop-down arguments.
70                          */
71                         $dropdown_args = apply_filters( 'widget_archives_dropdown_args', array(
72                                 'type'            => 'monthly',
73                                 'format'          => 'option',
74                                 'show_post_count' => $c
75                         ) );
76
77                         switch ( $dropdown_args['type'] ) {
78                                 case 'yearly':
79                                         $label = __( 'Select Year' );
80                                         break;
81                                 case 'monthly':
82                                         $label = __( 'Select Month' );
83                                         break;
84                                 case 'daily':
85                                         $label = __( 'Select Day' );
86                                         break;
87                                 case 'weekly':
88                                         $label = __( 'Select Week' );
89                                         break;
90                                 default:
91                                         $label = __( 'Select Post' );
92                                         break;
93                         }
94                         ?>
95
96                         <option value=""><?php echo esc_attr( $label ); ?></option>
97                         <?php wp_get_archives( $dropdown_args ); ?>
98
99                 </select>
100                 <?php } else { ?>
101                 <ul>
102                 <?php
103                 /**
104                  * Filter the arguments for the Archives widget.
105                  *
106                  * @since 2.8.0
107                  *
108                  * @see wp_get_archives()
109                  *
110                  * @param array $args An array of Archives option arguments.
111                  */
112                 wp_get_archives( apply_filters( 'widget_archives_args', array(
113                         'type'            => 'monthly',
114                         'show_post_count' => $c
115                 ) ) );
116                 ?>
117                 </ul>
118                 <?php
119                 }
120
121                 echo $args['after_widget'];
122         }
123
124         /**
125          * Handles updating settings for the current Archives widget instance.
126          *
127          * @since 2.8.0
128          * @access public
129          *
130          * @param array $new_instance New settings for this instance as input by the user via
131          *                            WP_Widget_Archives::form().
132          * @param array $old_instance Old settings for this instance.
133          * @return array Updated settings to save.
134          */
135         public function update( $new_instance, $old_instance ) {
136                 $instance = $old_instance;
137                 $new_instance = wp_parse_args( (array) $new_instance, array( 'title' => '', 'count' => 0, 'dropdown' => '') );
138                 $instance['title'] = sanitize_text_field( $new_instance['title'] );
139                 $instance['count'] = $new_instance['count'] ? 1 : 0;
140                 $instance['dropdown'] = $new_instance['dropdown'] ? 1 : 0;
141
142                 return $instance;
143         }
144
145         /**
146          * Outputs the settings form for the Archives widget.
147          *
148          * @since 2.8.0
149          * @access public
150          *
151          * @param array $instance Current settings.
152          */
153         public function form( $instance ) {
154                 $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'count' => 0, 'dropdown' => '') );
155                 $title = sanitize_text_field( $instance['title'] );
156                 ?>
157                 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> <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>
158                 <p>
159                         <input class="checkbox" type="checkbox"<?php checked( $instance['dropdown'] ); ?> id="<?php echo $this->get_field_id('dropdown'); ?>" name="<?php echo $this->get_field_name('dropdown'); ?>" /> <label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e('Display as dropdown'); ?></label>
160                         <br/>
161                         <input class="checkbox" type="checkbox"<?php checked( $instance['count'] ); ?> id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" /> <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Show post counts'); ?></label>
162                 </p>
163                 <?php
164         }
165 }