Skip to content

Commit 9dececd

Browse files
committed
Widgets: Use booleans in Categories widget arguments.
The Categories widget passed `show_count` and `hierarchical` to `wp_list_categories()` and `wp_dropdown_categories()` as the numeric strings `'0'` and `'1'`, while both functions document these arguments as `bool|int`. They are now passed as booleans, matching what the Categories block already passes. Core only checks these values for truthiness, so output is unchanged; a callback on the `widget_categories_args` or `widget_categories_dropdown_args` filter that compares them strictly to `'1'` or `'0'` is the only code affected, which is noted in the filters' `@since` tags. The arguments passed to those filters, along with the widget's display arguments and instance settings, are now documented as hashes so that static analysis can type them. The `count`, `hierarchical` and `dropdown` settings are documented as the `0` or `1` that `update()` stores, while `widget()` keeps reading them loosely for instances saved before WordPress 2.8, which stored booleans. A missing title in the submitted settings no longer triggers an undefined array key warning in `update()`, which can happen when REST API form data omits it. Developed in #8071. Follow-up to r10809, r41685, r64331. Props westonruter, marian1. See #65817, #65860. Fixes #62762. git-svn-id: https://develop.svn.wordpress.org/trunk@64413 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 5b69902 commit 9dececd

2 files changed

Lines changed: 133 additions & 20 deletions

File tree

‎src/wp-includes/widgets/class-wp-widget-categories.php‎

Lines changed: 99 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,22 @@ public function __construct() {
3838
* @since 4.2.0 Creates a unique HTML ID for the `<select>` element
3939
* if more than one instance is displayed on the page.
4040
*
41-
* @param array $args Display arguments including 'before_title', 'after_title',
42-
* 'before_widget', and 'after_widget'.
43-
* @param array $instance Settings for the current Categories widget instance.
41+
* @param array $args {
42+
* Display arguments.
43+
*
44+
* @type string $before_widget HTML markup to prepend to each widget in the sidebar.
45+
* @type string $after_widget HTML markup to append to each widget in the sidebar.
46+
* @type string $before_title HTML markup to prepend to the widget title when displayed.
47+
* @type string $after_title HTML markup to append to the widget title when displayed.
48+
* }
49+
* @param array $instance {
50+
* Settings for the current Categories widget instance.
51+
*
52+
* @type string $title Widget title.
53+
* @type 0|1 $count Whether to show post counts.
54+
* @type 0|1 $hierarchical Whether to show the category hierarchy.
55+
* @type 0|1 $dropdown Whether to display the categories as a drop-down.
56+
* }
4457
*/
4558
public function widget( $args, $instance ) {
4659
static $first_dropdown = true;
@@ -51,9 +64,15 @@ public function widget( $args, $instance ) {
5164
/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
5265
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
5366

54-
$count = ! empty( $instance['count'] ) ? '1' : '0';
55-
$hierarchical = ! empty( $instance['hierarchical'] ) ? '1' : '0';
56-
$dropdown = ! empty( $instance['dropdown'] ) ? '1' : '0';
67+
/*
68+
* Although update() stores these flags as 0 or 1, they are checked loosely for back-compat:
69+
* the keys are absent for an instance that has not been saved yet, instances saved before
70+
* WordPress 2.8 stored booleans, and the_widget() passes along whatever values its caller
71+
* supplies.
72+
*/
73+
$count = ! empty( $instance['count'] );
74+
$hierarchical = ! empty( $instance['hierarchical'] );
75+
$dropdown = ! empty( $instance['dropdown'] );
5776

5877
echo $args['before_widget'];
5978

@@ -82,11 +101,29 @@ public function widget( $args, $instance ) {
82101
*
83102
* @since 2.8.0
84103
* @since 4.9.0 Added the `$instance` parameter.
104+
* @since 7.2.0 The `show_count` and `hierarchical` arguments in `$cat_args` are now booleans instead of the numeric strings '0' and '1'.
85105
*
86106
* @see wp_dropdown_categories()
87107
*
88-
* @param array $cat_args An array of Categories widget drop-down arguments.
89-
* @param array $instance Array of settings for the current widget.
108+
* @param array $cat_args {
109+
* An array of Categories widget drop-down arguments. See {@see wp_dropdown_categories()} for
110+
* information on additional accepted arguments.
111+
*
112+
* @type string $orderby Which column to use for ordering categories. See {@see get_terms()}
113+
* for a list of accepted values. Default 'name'.
114+
* @type bool $show_count Whether to include post counts.
115+
* @type bool $hierarchical Whether to traverse the taxonomy hierarchy.
116+
* @type string $show_option_none Text to display for showing no categories. Set to 'Select Category'.
117+
* @type string $id Value for the 'id' attribute of the select element.
118+
* }
119+
* @param array $instance {
120+
* Array of settings for the current widget.
121+
*
122+
* @type string $title Widget title.
123+
* @type 0|1 $count Whether to show post counts.
124+
* @type 0|1 $hierarchical Whether to show the category hierarchy.
125+
* @type 0|1 $dropdown Whether to display the categories as a drop-down.
126+
* }
90127
*/
91128
wp_dropdown_categories( apply_filters( 'widget_categories_dropdown_args', $cat_args, $instance ) );
92129

@@ -149,9 +186,26 @@ function onClick() {
149186
*
150187
* @since 2.8.0
151188
* @since 4.9.0 Added the `$instance` parameter.
189+
* @since 7.2.0 The `show_count` and `hierarchical` arguments in `$cat_args` are now booleans instead of the numeric strings '0' and '1'.
190+
*
191+
* @param array $cat_args {
192+
* An array of Categories widget options. See {@see wp_list_categories()} for information on
193+
* additional accepted arguments.
152194
*
153-
* @param array $cat_args An array of Categories widget options.
154-
* @param array $instance Array of settings for the current widget.
195+
* @type string $orderby Which column to use for ordering categories. See {@see get_terms()}
196+
* for a list of accepted values. Default 'name'.
197+
* @type bool $show_count Whether to include post counts.
198+
* @type bool $hierarchical Whether to traverse the taxonomy hierarchy.
199+
* @type string $title_li Text to use for the list title `<li>` element. Default empty string.
200+
* }
201+
* @param array $instance {
202+
* Array of settings for the current widget.
203+
*
204+
* @type string $title Widget title.
205+
* @type 0|1 $count Whether to show post counts.
206+
* @type 0|1 $hierarchical Whether to show the category hierarchy.
207+
* @type 0|1 $dropdown Whether to display the categories as a drop-down.
208+
* }
155209
*/
156210
wp_list_categories( apply_filters( 'widget_categories_args', $cat_args, $instance ) );
157211
?>
@@ -171,14 +225,36 @@ function onClick() {
171225
*
172226
* @since 2.8.0
173227
*
174-
* @param array $new_instance New settings for this instance as input by the user via
175-
* WP_Widget::form().
176-
* @param array $old_instance Old settings for this instance.
177-
* @return array Updated settings to save.
228+
* @param array $new_instance {
229+
* New settings for this instance as input by the user via {@see WP_Widget::form()}. A checkbox
230+
* submits 'on' when checked and is omitted when unchecked. A saved instance resubmitted
231+
* through the REST API passes 0 or 1 instead.
232+
*
233+
* @type string $title Widget title.
234+
* @type 'on'|0|1 $count Whether to show post counts.
235+
* @type 'on'|0|1 $hierarchical Whether to show the category hierarchy.
236+
* @type 'on'|0|1 $dropdown Whether to display the categories as a drop-down.
237+
* }
238+
* @param array $old_instance {
239+
* Old settings for this instance.
240+
*
241+
* @type string $title Widget title.
242+
* @type 0|1 $count Whether to show post counts.
243+
* @type 0|1 $hierarchical Whether to show the category hierarchy.
244+
* @type 0|1 $dropdown Whether to display the categories as a drop-down.
245+
* }
246+
* @return array {
247+
* Updated settings to save.
248+
*
249+
* @type string $title Widget title.
250+
* @type 0|1 $count Whether to show post counts.
251+
* @type 0|1 $hierarchical Whether to show the category hierarchy.
252+
* @type 0|1 $dropdown Whether to display the categories as a drop-down.
253+
* }
178254
*/
179255
public function update( $new_instance, $old_instance ) {
180256
$instance = $old_instance;
181-
$instance['title'] = sanitize_text_field( $new_instance['title'] );
257+
$instance['title'] = sanitize_text_field( $new_instance['title'] ?? '' );
182258
$instance['count'] = ! empty( $new_instance['count'] ) ? 1 : 0;
183259
$instance['hierarchical'] = ! empty( $new_instance['hierarchical'] ) ? 1 : 0;
184260
$instance['dropdown'] = ! empty( $new_instance['dropdown'] ) ? 1 : 0;
@@ -191,7 +267,14 @@ public function update( $new_instance, $old_instance ) {
191267
*
192268
* @since 2.8.0
193269
*
194-
* @param array $instance Current settings.
270+
* @param array $instance {
271+
* Current settings.
272+
*
273+
* @type string $title Widget title.
274+
* @type 0|1 $count Whether to show post counts.
275+
* @type 0|1 $hierarchical Whether to show the category hierarchy.
276+
* @type 0|1 $dropdown Whether to display the categories as a drop-down.
277+
* }
195278
*/
196279
public function form( $instance ) {
197280
// Defaults.

‎tests/phpunit/tests/widgets/wpWidgetCategories.php‎

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,12 +284,14 @@ public function test_widget_applies_widget_categories_args_filter( $instance, $c
284284

285285
list( $cat_args, $filter_instance ) = $filter_args[0];
286286

287+
$this->assertIsArray( $cat_args );
288+
287289
$this->assertSame( $instance, $filter_instance );
288290
$this->assertSameSets( array( 'orderby', 'show_count', 'hierarchical', 'title_li' ), array_keys( $cat_args ) );
289291
$this->assertSame( 'name', $cat_args['orderby'] );
290292
$this->assertSame( '', $cat_args['title_li'] );
291-
$this->assertSame( $count, (bool) $cat_args['show_count'], 'show_count should match the count setting.' );
292-
$this->assertSame( $hierarchical, (bool) $cat_args['hierarchical'], 'hierarchical should match the hierarchical setting.' );
293+
$this->assertSame( $count, $cat_args['show_count'], 'show_count should match the count setting.' );
294+
$this->assertSame( $hierarchical, $cat_args['hierarchical'], 'hierarchical should match the hierarchical setting.' );
293295
}
294296

295297
/**
@@ -318,6 +320,23 @@ public static function data_widget_options() {
318320
true,
319321
true,
320322
),
323+
// Instances saved before WordPress 2.8 stored booleans.
324+
'booleans enabled' => array(
325+
array(
326+
'count' => true,
327+
'hierarchical' => true,
328+
),
329+
true,
330+
true,
331+
),
332+
'booleans disabled' => array(
333+
array(
334+
'count' => false,
335+
'hierarchical' => false,
336+
),
337+
false,
338+
false,
339+
),
321340
);
322341
}
323342

@@ -422,11 +441,13 @@ public function test_widget_applies_widget_categories_dropdown_args_filter() {
422441

423442
list( $cat_args, $filter_instance ) = $filter_args[0];
424443

444+
$this->assertIsArray( $cat_args );
445+
425446
$this->assertSame( $instance, $filter_instance );
426447
$this->assertSameSets( array( 'orderby', 'show_count', 'hierarchical', 'show_option_none', 'id' ), array_keys( $cat_args ) );
427448
$this->assertSame( 'name', $cat_args['orderby'] );
428-
$this->assertTrue( (bool) $cat_args['show_count'] );
429-
$this->assertTrue( (bool) $cat_args['hierarchical'] );
449+
$this->assertTrue( $cat_args['show_count'] );
450+
$this->assertTrue( $cat_args['hierarchical'] );
430451
$this->assertSame( 'Select Category', $cat_args['show_option_none'] );
431452
$this->assertSame( 'categories-dropdown-3', $cat_args['id'] );
432453
}
@@ -500,6 +521,15 @@ public static function data_update() {
500521
'dropdown' => 0,
501522
),
502523
),
524+
'missing title' => array(
525+
array( 'count' => 'on' ),
526+
array(
527+
'title' => '',
528+
'count' => 1,
529+
'hierarchical' => 0,
530+
'dropdown' => 0,
531+
),
532+
),
503533
);
504534
}
505535

0 commit comments

Comments
 (0)