REST API: Fix delete widget endpoint

Makes the `DELETE /wp/v2/widgets/:id?force=1` endpoint actually delete the
widget from the `"widget-$id_base"` option and not just remove it from
`'sidebars_widgets'`.

Fixes #53313.
Props TimothyBlynJacobs.

Built from https://develop.svn.wordpress.org/trunk@51059


git-svn-id: http://core.svn.wordpress.org/trunk@50668 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
noisysocks 2021-06-02 01:22:57 +00:00
parent 38cea1e659
commit 2fd3f23408
2 changed files with 39 additions and 6 deletions

View File

@ -283,10 +283,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.
*
* @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;
$widget_id = $request['id']; $widget_id = $request['id'];
$sidebar_id = wp_find_widgets_sidebar( $widget_id ); $sidebar_id = wp_find_widgets_sidebar( $widget_id );
@ -301,17 +305,46 @@ class WP_REST_Widgets_Controller extends WP_REST_Controller {
$request['context'] = 'edit'; $request['context'] = 'edit';
if ( $request['force'] ) { if ( $request['force'] ) {
$prepared = $this->prepare_item_for_response( compact( 'widget_id', 'sidebar_id' ), $request ); $response = $this->prepare_item_for_response( compact( 'widget_id', 'sidebar_id' ), $request );
$parsed_id = wp_parse_widget_id( $widget_id );
$id_base = $parsed_id['id_base'];
$original_post = $_POST;
$original_request = $_REQUEST;
$_POST = array(
'sidebar' => $sidebar_id,
"widget-$id_base" => array(),
'the-widget-id' => $widget_id,
'delete_widget' => '1',
);
$_REQUEST = $_POST;
$callback = $wp_registered_widget_updates[ $id_base ]['callback'];
$params = $wp_registered_widget_updates[ $id_base ]['params'];
if ( is_callable( $callback ) ) {
ob_start();
call_user_func_array( $callback, $params );
ob_end_clean();
}
$_POST = $original_post;
$_REQUEST = $original_request;
wp_assign_widget_to_sidebar( $widget_id, '' ); wp_assign_widget_to_sidebar( $widget_id, '' );
$prepared->set_data(
$response->set_data(
array( array(
'deleted' => true, 'deleted' => true,
'previous' => $prepared->get_data(), 'previous' => $response->get_data(),
) )
); );
} else { } else {
wp_assign_widget_to_sidebar( $widget_id, 'wp_inactive_widgets' ); wp_assign_widget_to_sidebar( $widget_id, 'wp_inactive_widgets' );
$prepared = $this->prepare_item_for_response(
$response = $this->prepare_item_for_response(
array( array(
'sidebar_id' => 'wp_inactive_widgets', 'sidebar_id' => 'wp_inactive_widgets',
'widget_id' => $widget_id, 'widget_id' => $widget_id,
@ -320,7 +353,7 @@ class WP_REST_Widgets_Controller extends WP_REST_Controller {
); );
} }
return $prepared; return $response;
} }
/** /**

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.8-alpha-51058'; $wp_version = '5.8-alpha-51059';
/** /**
* 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.