|
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 */ |
|
24 public function __construct() { |
|
25 $widget_ops = array( |
|
26 'classname' => 'widget_categories', |
|
27 'description' => __( 'A list or dropdown of categories.' ), |
|
28 'customize_selective_refresh' => true, |
|
29 ); |
|
30 parent::__construct( 'categories', __( 'Categories' ), $widget_ops ); |
|
31 } |
|
32 |
|
33 /** |
|
34 * Outputs the content for the current Categories widget instance. |
|
35 * |
|
36 * @since 2.8.0 |
|
37 * |
|
38 * @staticvar bool $first_dropdown |
|
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 $title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Categories' ); |
|
48 |
|
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 |
|
52 $c = ! empty( $instance['count'] ) ? '1' : '0'; |
|
53 $h = ! empty( $instance['hierarchical'] ) ? '1' : '0'; |
|
54 $d = ! empty( $instance['dropdown'] ) ? '1' : '0'; |
|
55 |
|
56 echo $args['before_widget']; |
|
57 |
|
58 if ( $title ) { |
|
59 echo $args['before_title'] . $title . $args['after_title']; |
|
60 } |
|
61 |
|
62 $cat_args = array( |
|
63 'orderby' => 'name', |
|
64 'show_count' => $c, |
|
65 'hierarchical' => $h, |
|
66 ); |
|
67 |
|
68 if ( $d ) { |
|
69 echo sprintf( '<form action="%s" method="get">', esc_url( home_url() ) ); |
|
70 $dropdown_id = ( $first_dropdown ) ? 'cat' : "{$this->id_base}-dropdown-{$this->number}"; |
|
71 $first_dropdown = false; |
|
72 |
|
73 echo '<label class="screen-reader-text" for="' . esc_attr( $dropdown_id ) . '">' . $title . '</label>'; |
|
74 |
|
75 $cat_args['show_option_none'] = __( 'Select Category' ); |
|
76 $cat_args['id'] = $dropdown_id; |
|
77 |
|
78 /** |
|
79 * Filters the arguments for the Categories widget drop-down. |
|
80 * |
|
81 * @since 2.8.0 |
|
82 * @since 4.9.0 Added the `$instance` parameter. |
|
83 * |
|
84 * @see wp_dropdown_categories() |
|
85 * |
|
86 * @param array $cat_args An array of Categories widget drop-down arguments. |
|
87 * @param array $instance Array of settings for the current widget. |
|
88 */ |
|
89 wp_dropdown_categories( apply_filters( 'widget_categories_dropdown_args', $cat_args, $instance ) ); |
|
90 |
|
91 echo '</form>'; |
|
92 ?> |
|
93 |
|
94 <script type='text/javascript'> |
|
95 /* <![CDATA[ */ |
|
96 (function() { |
|
97 var dropdown = document.getElementById( "<?php echo esc_js( $dropdown_id ); ?>" ); |
|
98 function onCatChange() { |
|
99 if ( dropdown.options[ dropdown.selectedIndex ].value > 0 ) { |
|
100 dropdown.parentNode.submit(); |
|
101 } |
|
102 } |
|
103 dropdown.onchange = onCatChange; |
|
104 })(); |
|
105 /* ]]> */ |
|
106 </script> |
|
107 |
|
108 <?php |
|
109 } else { |
|
110 ?> |
|
111 <ul> |
|
112 <?php |
|
113 $cat_args['title_li'] = ''; |
|
114 |
|
115 /** |
|
116 * Filters the arguments for the Categories widget. |
|
117 * |
|
118 * @since 2.8.0 |
|
119 * @since 4.9.0 Added the `$instance` parameter. |
|
120 * |
|
121 * @param array $cat_args An array of Categories widget options. |
|
122 * @param array $instance Array of settings for the current widget. |
|
123 */ |
|
124 wp_list_categories( apply_filters( 'widget_categories_args', $cat_args, $instance ) ); |
|
125 ?> |
|
126 </ul> |
|
127 <?php |
|
128 } |
|
129 |
|
130 echo $args['after_widget']; |
|
131 } |
|
132 |
|
133 /** |
|
134 * Handles updating settings for the current Categories widget instance. |
|
135 * |
|
136 * @since 2.8.0 |
|
137 * |
|
138 * @param array $new_instance New settings for this instance as input by the user via |
|
139 * WP_Widget::form(). |
|
140 * @param array $old_instance Old settings for this instance. |
|
141 * @return array Updated settings to save. |
|
142 */ |
|
143 public function update( $new_instance, $old_instance ) { |
|
144 $instance = $old_instance; |
|
145 $instance['title'] = sanitize_text_field( $new_instance['title'] ); |
|
146 $instance['count'] = !empty($new_instance['count']) ? 1 : 0; |
|
147 $instance['hierarchical'] = !empty($new_instance['hierarchical']) ? 1 : 0; |
|
148 $instance['dropdown'] = !empty($new_instance['dropdown']) ? 1 : 0; |
|
149 |
|
150 return $instance; |
|
151 } |
|
152 |
|
153 /** |
|
154 * Outputs the settings form for the Categories widget. |
|
155 * |
|
156 * @since 2.8.0 |
|
157 * |
|
158 * @param array $instance Current settings. |
|
159 */ |
|
160 public function form( $instance ) { |
|
161 //Defaults |
|
162 $instance = wp_parse_args( (array) $instance, array( 'title' => '') ); |
|
163 $title = sanitize_text_field( $instance['title'] ); |
|
164 $count = isset($instance['count']) ? (bool) $instance['count'] :false; |
|
165 $hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false; |
|
166 $dropdown = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false; |
|
167 ?> |
|
168 <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:' ); ?></label> |
|
169 <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> |
|
170 |
|
171 <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 ); ?> /> |
|
172 <label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e( 'Display as dropdown' ); ?></label><br /> |
|
173 |
|
174 <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>"<?php checked( $count ); ?> /> |
|
175 <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e( 'Show post counts' ); ?></label><br /> |
|
176 |
|
177 <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('hierarchical'); ?>" name="<?php echo $this->get_field_name('hierarchical'); ?>"<?php checked( $hierarchical ); ?> /> |
|
178 <label for="<?php echo $this->get_field_id('hierarchical'); ?>"><?php _e( 'Show hierarchy' ); ?></label></p> |
|
179 <?php |
|
180 } |
|
181 |
|
182 } |