REST API: Allow multiple widgets to be deleted in a single batch request.

This resets the `WP_Widget::$updated` flag when deleting a widget, to avoid blocking all future updates in a request.

Props noisysocks, andraganescu.
Fixes #53557.
Built from https://develop.svn.wordpress.org/trunk@51277


git-svn-id: http://core.svn.wordpress.org/trunk@50886 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2021-06-30 10:41:58 +00:00
parent 4c598d36b1
commit ffafbefc36
2 changed files with 20 additions and 6 deletions

View File

@ -289,13 +289,14 @@ class WP_REST_Widgets_Controller extends WP_REST_Controller {
* *
* @since 5.8.0 * @since 5.8.0
* *
* @global array $wp_registered_widget_updates The registered widget update functions. * @global array $wp_registered_widget_updates The registered widget update functions.
* @global WP_Widget_Factory $wp_widget_factory
* *
* @param WP_REST_Request $request Full details about the request. * @param WP_REST_Request $request Full details about the request.
* @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
*/ */
public function delete_item( $request ) { public function delete_item( $request ) {
global $wp_registered_widget_updates; global $wp_registered_widget_updates, $wp_widget_factory;
retrieve_widgets(); retrieve_widgets();
@ -344,6 +345,17 @@ class WP_REST_Widgets_Controller extends WP_REST_Controller {
$_POST = $original_post; $_POST = $original_post;
$_REQUEST = $original_request; $_REQUEST = $original_request;
$widget_object = $wp_widget_factory->get_widget_object( $id_base );
if ( $widget_object ) {
/*
* WP_Widget sets `updated = true` after an update to prevent more than one widget
* from being saved per request. This isn't what we want in the REST API, though,
* as we support batch requests.
*/
$widget_object->updated = false;
}
wp_assign_widget_to_sidebar( $widget_id, '' ); wp_assign_widget_to_sidebar( $widget_id, '' );
$response->set_data( $response->set_data(
@ -520,9 +532,11 @@ class WP_REST_Widgets_Controller extends WP_REST_Controller {
$widget_object->_set( $number ); $widget_object->_set( $number );
$widget_object->_register_one( $number ); $widget_object->_register_one( $number );
// WP_Widget sets updated = true after an update to prevent more /*
// than one widget from being saved per request. This isn't what we * WP_Widget sets `updated = true` after an update to prevent more than one widget
// want in the REST API, though, as we support batch requests. * from being saved per request. This isn't what we want in the REST API, though,
* as we support batch requests.
*/
$widget_object->updated = false; $widget_object->updated = false;
} }

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.9-alpha-51276'; $wp_version = '5.9-alpha-51277';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.