From c283bf2bb64325cf8645fa0c417f68890a1c1316 Mon Sep 17 00:00:00 2001 From: Timothy Jacobs Date: Wed, 7 Jul 2021 19:34:33 +0000 Subject: [PATCH] REST API: Ensure a sidebar's widgets property is a list. When a widget is removed from a sidebar, if it was removed from the middle of the list, the widgets property would become an object with numeric keys. The sidebars controller now forces the widgets property to be a list. Props walbo. Fixes #53612. git-svn-id: https://develop.svn.wordpress.org/trunk@51377 602fd350-edb4-49c9-b593-d223f7449a82 --- .../class-wp-rest-sidebars-controller.php | 2 +- .../rest-api/rest-sidebars-controller.php | 54 ++++++++++++++++++- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-sidebars-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-sidebars-controller.php index 357e35814a..50c84f1dd9 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-sidebars-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-sidebars-controller.php @@ -304,7 +304,7 @@ class WP_REST_Sidebars_Controller extends WP_REST_Controller { } ); - $sidebar['widgets'] = $widgets; + $sidebar['widgets'] = array_values( $widgets ); } $schema = $this->get_item_schema(); diff --git a/tests/phpunit/tests/rest-api/rest-sidebars-controller.php b/tests/phpunit/tests/rest-api/rest-sidebars-controller.php index 72a80aca4a..a257281041 100644 --- a/tests/phpunit/tests/rest-api/rest-sidebars-controller.php +++ b/tests/phpunit/tests/rest-api/rest-sidebars-controller.php @@ -75,7 +75,7 @@ class WP_Test_REST_Sidebars_Controller extends WP_Test_REST_Controller_Testcase } private function setup_widget( $option_name, $number, $settings ) { - update_option( + $this->setup_widgets( $option_name, array( $number => $settings, @@ -83,6 +83,10 @@ class WP_Test_REST_Sidebars_Controller extends WP_Test_REST_Controller_Testcase ); } + private function setup_widgets( $option_name, $settings ) { + update_option( $option_name, $settings ); + } + private function setup_sidebar( $id, $attrs = array(), $widgets = array() ) { global $wp_registered_sidebars; update_option( @@ -490,6 +494,54 @@ class WP_Test_REST_Sidebars_Controller extends WP_Test_REST_Controller_Testcase $this->assertNotContains( 'text-1', rest_do_request( '/wp/v2/sidebars/sidebar-1' )->get_data()['widgets'] ); } + /** + * @ticket 53612 + */ + public function test_batch_remove_widgets_from_existing_sidebar() { + wp_widgets_init(); + + $this->setup_widgets( + 'widget_text', + array( + 2 => array( 'text' => 'Text widget' ), + 3 => array( 'text' => 'Text widget' ), + 4 => array( 'text' => 'Text widget' ), + 5 => array( 'text' => 'Text widget' ), + 6 => array( 'text' => 'Text widget' ), + ) + ); + + $this->setup_sidebar( + 'sidebar-1', + array( + 'name' => 'Test sidebar', + ), + array( 'text-2', 'text-3', 'text-4', 'text-5', 'text-6' ) + ); + + $request = new WP_REST_Request( 'POST', '/batch/v1' ); + $request->set_body_params( + array( + 'requests' => array( + array( + 'method' => 'DELETE', + 'path' => '/wp/v2/widgets/text-2?force=1', + ), + array( + 'method' => 'DELETE', + 'path' => '/wp/v2/widgets/text-3?force=1', + ), + ), + ) + ); + rest_get_server()->dispatch( $request ); + + $this->assertSame( + array( 'text-4', 'text-5', 'text-6' ), + rest_do_request( '/wp/v2/sidebars/sidebar-1' )->get_data()['widgets'] + ); + } + /** * @ticket 41683 */