Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Widgets: Don't delete a widget if it is moved to a different area #32608

Merged
merged 2 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions lib/class-wp-rest-widgets-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,14 @@ public function delete_item_permissions_check( $request ) { // phpcs:ignore Vari
*
* @since 5.6.0
*
* @global array $wp_registered_widget_updates The registered widget update functions.
*
* @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;

$widget_id = $request['id'];
$sidebar_id = wp_find_widgets_sidebar( $widget_id );

Expand All @@ -299,17 +303,46 @@ public function delete_item( $request ) {
$request['context'] = 'edit';

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, '' );
$prepared->set_data(

$response->set_data(
array(
'deleted' => true,
'previous' => $prepared->get_data(),
'previous' => $response->get_data(),
)
);
} else {
wp_assign_widget_to_sidebar( $widget_id, 'wp_inactive_widgets' );
$prepared = $this->prepare_item_for_response(

$response = $this->prepare_item_for_response(
array(
'sidebar_id' => 'wp_inactive_widgets',
'widget_id' => $widget_id,
Expand All @@ -318,7 +351,7 @@ public function delete_item( $request ) {
);
}

return $prepared;
return $response;
}

/**
Expand Down
21 changes: 14 additions & 7 deletions packages/edit-widgets/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,20 @@ export function* saveWidgetArea( widgetAreaId ) {
return true;
} );

// Get all widgets that have been deleted
const deletedWidgets = areaWidgets.filter(
( { id } ) =>
! widgetsBlocks.some(
( widgetBlock ) => getWidgetIdFromBlock( widgetBlock ) === id
)
);
// Determine which widgets have been deleted. We can tell if a widget is
// deleted and not just moved to a different area by looking to see if
// getWidgetAreaForWidgetId() finds something.
const deletedWidgets = [];
for ( const widget of areaWidgets ) {
const widgetsNewArea = yield select(
editWidgetsStoreName,
'getWidgetAreaForWidgetId',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is surprising to me that getWidgetAreaForWidgetId actually uses getEditedEntityRecord under the hood so it's returning the latest edited state rather than the stored one. Is it intentional? Should we make it more explicit?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm yes probably it shouldn't. What do you mean by make it more explicit? Happy to accept a ```suggestion 😛

Copy link
Member

@kevin940726 kevin940726 Jun 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I mean to rename getWidgetAreaForWidgetId to something like getEditedWidgetAreaForWidgetId. That's the easiest fix 😆

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, we can't simply rename because it's a public function. We'd have to deprecate getWidgetAreaForWidgetId and add getEditedWidgetAreaForWidgetId. I'm not sure if it's worth it? What do you think?

I kind of want to change how all of this works after 5.8... I don't think we should have select( 'core/edit-widgets' ).getWidgetAreaForWidgetId() at all because we already have e.g. select( 'core' ).getWidgetArea() which can do the job if we add blocks to the widget area entity type.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! Whatever you think is the best 👍

widget.id
);
if ( ! widgetsNewArea ) {
deletedWidgets.push( widget );
}
}

const batchMeta = [];
const batchTasks = [];
Expand Down