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