]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/widgets/class-wp-widget-categories.php
Wordpress 4.6
[autoinstalls/wordpress.git] / wp-includes / widgets / class-wp-widget-categories.php
1 <?php
2 /**
3  * Widget API: WP_Widget_Categories class
4  *
5  * @package WordPress
6  * @subpackage Widgets
7  * @since 4.4.0
8  */
9
10 /**
11  * Core class used to implement a Categories widget.
12  *
13  * @since 2.8.0
14  *
15  * @see WP_Widget
16  */
17 class WP_Widget_Categories extends WP_Widget {
18
19         /**
20          * Sets up a new Categories widget instance.
21          *
22          * @since 2.8.0
23          * @access public
24          */
25         public function __construct() {
26                 $widget_ops = array(
27                         'classname' => 'widget_categories',
28                         'description' => __( 'A list or dropdown of categories.' ),
29                         'customize_selective_refresh' => true,
30                 );
31                 parent::__construct( 'categories', __( 'Categories' ), $widget_ops );
32         }
33
34         /**
35          * Outputs the content for the current Categories 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 Categories widget instance.
43          */
44         public function widget( $args, $instance ) {
45                 static $first_dropdown = true;
46
47                 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
48                 $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Categories' ) : $instance['title'], $instance, $this->id_base );
49
50                 $c = ! empty( $instance['count'] ) ? '1' : '0';
51                 $h = ! empty( $instance['hierarchical'] ) ? '1' : '0';
52                 $d = ! empty( $instance['dropdown'] ) ? '1' : '0';
53
54                 echo $args['before_widget'];
55                 if ( $title ) {
56                         echo $args['before_title'] . $title . $args['after_title'];
57                 }
58
59                 $cat_args = array(
60                         'orderby'      => 'name',
61                         'show_count'   => $c,
62                         'hierarchical' => $h
63                 );
64
65                 if ( $d ) {
66                         $dropdown_id = ( $first_dropdown ) ? 'cat' : "{$this->id_base}-dropdown-{$this->number}";
67                         $first_dropdown = false;
68
69                         echo '<label class="screen-reader-text" for="' . esc_attr( $dropdown_id ) . '">' . $title . '</label>';
70
71                         $cat_args['show_option_none'] = __( 'Select Category' );
72                         $cat_args['id'] = $dropdown_id;
73
74                         /**
75                          * Filters the arguments for the Categories widget drop-down.
76                          *
77                          * @since 2.8.0
78                          *
79                          * @see wp_dropdown_categories()
80                          *
81                          * @param array $cat_args An array of Categories widget drop-down arguments.
82                          */
83                         wp_dropdown_categories( apply_filters( 'widget_categories_dropdown_args', $cat_args ) );
84                         ?>
85
86 <script type='text/javascript'>
87 /* <![CDATA[ */
88 (function() {
89         var dropdown = document.getElementById( "<?php echo esc_js( $dropdown_id ); ?>" );
90         function onCatChange() {
91                 if ( dropdown.options[ dropdown.selectedIndex ].value > 0 ) {
92                         location.href = "<?php echo home_url(); ?>/?cat=" + dropdown.options[ dropdown.selectedIndex ].value;
93                 }
94         }
95         dropdown.onchange = onCatChange;
96 })();
97 /* ]]> */
98 </script>
99
100 <?php
101                 } else {
102 ?>
103                 <ul>
104 <?php
105                 $cat_args['title_li'] = '';
106
107                 /**
108                  * Filters the arguments for the Categories widget.
109                  *
110                  * @since 2.8.0
111                  *
112                  * @param array $cat_args An array of Categories widget options.
113                  */
114                 wp_list_categories( apply_filters( 'widget_categories_args', $cat_args ) );
115 ?>
116                 </ul>
117 <?php
118                 }
119
120                 echo $args['after_widget'];
121         }
122
123         /**
124          * Handles updating settings for the current Categories widget instance.
125          *
126          * @since 2.8.0
127          * @access public
128          *
129          * @param array $new_instance New settings for this instance as input by the user via
130          *                            WP_Widget::form().
131          * @param array $old_instance Old settings for this instance.
132          * @return array Updated settings to save.
133          */
134         public function update( $new_instance, $old_instance ) {
135                 $instance = $old_instance;
136                 $instance['title'] = sanitize_text_field( $new_instance['title'] );
137                 $instance['count'] = !empty($new_instance['count']) ? 1 : 0;
138                 $instance['hierarchical'] = !empty($new_instance['hierarchical']) ? 1 : 0;
139                 $instance['dropdown'] = !empty($new_instance['dropdown']) ? 1 : 0;
140
141                 return $instance;
142         }
143
144         /**
145          * Outputs the settings form for the Categories widget.
146          *
147          * @since 2.8.0
148          * @access public
149          *
150          * @param array $instance Current settings.
151          */
152         public function form( $instance ) {
153                 //Defaults
154                 $instance = wp_parse_args( (array) $instance, array( 'title' => '') );
155                 $title = sanitize_text_field( $instance['title'] );
156                 $count = isset($instance['count']) ? (bool) $instance['count'] :false;
157                 $hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
158                 $dropdown = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false;
159                 ?>
160                 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:' ); ?></label>
161                 <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>
162
163                 <p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('dropdown'); ?>" name="<?php echo $this->get_field_name('dropdown'); ?>"<?php checked( $dropdown ); ?> />
164                 <label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e( 'Display as dropdown' ); ?></label><br />
165
166                 <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>"<?php checked( $count ); ?> />
167                 <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e( 'Show post counts' ); ?></label><br />
168
169                 <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('hierarchical'); ?>" name="<?php echo $this->get_field_name('hierarchical'); ?>"<?php checked( $hierarchical ); ?> />
170                 <label for="<?php echo $this->get_field_id('hierarchical'); ?>"><?php _e( 'Show hierarchy' ); ?></label></p>
171                 <?php
172         }
173
174 }