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
This commit is contained in:
Timothy Jacobs 2021-07-07 19:34:33 +00:00
parent 8c413885bd
commit c283bf2bb6
2 changed files with 54 additions and 2 deletions

View File

@ -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();

View File

@ -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
*/