From ffafbefc36740e7fabecd2a8b0520070060079d5 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Wed, 30 Jun 2021 10:41:58 +0000 Subject: [PATCH] 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 --- .../class-wp-rest-widgets-controller.php | 24 +++++++++++++++---- wp-includes/version.php | 2 +- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-widgets-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-widgets-controller.php index 5fb9b8f6e1..9b97da4199 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-widgets-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-widgets-controller.php @@ -289,13 +289,14 @@ class WP_REST_Widgets_Controller extends WP_REST_Controller { * * @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. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. */ public function delete_item( $request ) { - global $wp_registered_widget_updates; + global $wp_registered_widget_updates, $wp_widget_factory; retrieve_widgets(); @@ -344,6 +345,17 @@ class WP_REST_Widgets_Controller extends WP_REST_Controller { $_POST = $original_post; $_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, '' ); $response->set_data( @@ -520,9 +532,11 @@ class WP_REST_Widgets_Controller extends WP_REST_Controller { $widget_object->_set( $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 - // want in the REST API, though, as we support batch requests. + /* + * 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; } diff --git a/wp-includes/version.php b/wp-includes/version.php index c0683c1d25..2c9f79a0bf 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @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.