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

First pass at using the new sidebars and widget endpoints. #26086

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
10 changes: 8 additions & 2 deletions lib/class-wp-rest-batch-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function register_routes() {
array(
'callback' => array( $this, 'serve_batch_request' ),
'permission_callback' => '__return_true',
'methods' => array( 'POST', 'PUT', 'PATCH', 'DELETE' ),
'methods' => 'POST',
'args' => array(
'validation' => array(
'type' => 'string',
Expand All @@ -39,6 +39,12 @@ public function register_routes() {
'items' => array(
'type' => 'object',
'properties' => array(
'method' => array(
'type' => 'string',
'enum' => array( 'POST', 'PUT', 'PATCH', 'DELETE' ),
'default' => 'POST',
'required' => true,
),
'path' => array(
'type' => 'string',
'required' => true,
Expand Down Expand Up @@ -86,7 +92,7 @@ public function serve_batch_request( WP_REST_Request $batch_request ) {
continue;
}

$single_request = new WP_REST_Request( $batch_request->get_method(), $parsed_url['path'] );
$single_request = new WP_REST_Request( $args['method'], $parsed_url['path'] );

if ( ! empty( $parsed_url['query'] ) ) {
$query_args = null; // Satisfy linter.
Expand Down
5 changes: 5 additions & 0 deletions lib/class-wp-rest-sidebars-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,13 @@ public function update_item( $request ) {

foreach ( $sidebars as $sidebar_id => $widgets ) {
foreach ( $widgets as $i => $widget_id ) {
// This automatically removes the passed widget ids from any other sidebars in use.
if ( $sidebar_id !== $request['id'] && in_array( $widget_id, $request['widgets'], true ) ) {
unset( $sidebars[ $sidebar_id ][ $i ] );
}

// This automatically removes omitted widget ids to the inactive sidebar.
if ( $sidebar_id === $request['id'] && ! in_array( $widget_id, $request['widgets'], true ) ) {
$sidebars['wp_inactive_widgets'][] = $widget_id;
}
}
Expand Down
31 changes: 15 additions & 16 deletions lib/class-wp-rest-widgets-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ public function create_item( $request ) {
$request['context'] = 'edit';

$response = $this->prepare_item_for_response( compact( 'sidebar_id', 'widget_id' ), $request );

if ( is_wp_error( $response ) ) {
return $response;
}

$response->set_status( 201 );

return $response;
Expand Down Expand Up @@ -387,20 +392,14 @@ protected function save_widget( $request ) {
// Just because we saved new widget doesn't mean it was added to $wp_registered_widgets.
// Let's make sure it's there so that it's included in the response.
if ( ! isset( $wp_registered_widgets[ $input_widget['id'] ] ) || 1 === $number ) {
$first_widget_id = substr( $input_widget['id'], 0, strrpos( $input_widget['id'], '-' ) ) . '-1';

if ( isset( $wp_registered_widgets[ $first_widget_id ] ) ) {
$wp_registered_widgets[ $input_widget['id'] ] = $wp_registered_widgets[ $first_widget_id ];

$widget_class = get_class( $update_control['callback'][0] );
$new_object = new $widget_class(
$input_widget['id_base'],
$input_widget['name'],
$input_widget['settings']
);
$new_object->_register();
$wp_registered_widgets[ $input_widget['id'] ]['callback'][0] = $new_object;
}
$widget_class = get_class( $update_control['callback'][0] );
$new_object = new $widget_class(
$input_widget['id_base'],
$input_widget['name'],
$input_widget['settings']
);
$new_object->_set( $number );
$new_object->_register();
}
} else {
$registered_widget_id = null;
Expand Down Expand Up @@ -550,8 +549,8 @@ public function prepare_item_for_response( $item, $request ) {
) {
$control = $wp_registered_widget_controls[ $widget_id ];
$arguments = array();
if ( ! empty( $widget['number'] ) ) {
$arguments[0] = array( 'number' => $widget['number'] );
if ( ! empty( $prepared['number'] ) ) {
$arguments[0] = array( 'number' => $prepared['number'] );
}
ob_start();
call_user_func_array( $control['callback'], $arguments );
Expand Down
3 changes: 2 additions & 1 deletion lib/widgets-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ function gutenberg_widgets_init( $hook ) {

$preload_paths = array(
array( '/wp/v2/media', 'OPTIONS' ),
'/__experimental/sidebars?context=edit&per_page=-1',
'/wp/v2/sidebars?context=edit&per_page=-1',
'/wp/v2/widgets?context=edit&per_page=-1',
);
$preload_data = array_reduce(
$preload_paths,
Expand Down
5 changes: 3 additions & 2 deletions packages/batch-processing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
"!((src|build|build-module)/(components|utils)/**)"
],
"dependencies": {
"@wordpress/data": "file:../data",
"uuid": "^8.3.1"
"@wordpress/data": "file:../data",
"lodash": "^4.17.20",
"uuid": "^8.3.1"
},
"publishConfig": {
"access": "public"
Expand Down
10 changes: 9 additions & 1 deletion packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,19 @@ export const defaultEntities = [
{
name: 'sidebar',
kind: 'root',
baseURL: '/__experimental/sidebars',
baseURL: '/wp/v2/sidebars',
plural: 'sidebars',
transientEdits: { blocks: true },
label: __( 'Widget areas' ),
},
{
name: 'widget',
kind: 'root',
baseURL: '/wp/v2/widgets',
plural: 'widgets',
transientEdits: { blocks: true },
label: __( 'Widgets' ),
},
{
label: __( 'User' ),
name: 'user',
Expand Down
4 changes: 3 additions & 1 deletion packages/edit-widgets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"dependencies": {
"@babel/runtime": "^7.11.2",
"@wordpress/api-fetch": "file:../api-fetch",
"@wordpress/batch-processing": "file:../batch-processing",
"@wordpress/block-editor": "file:../block-editor",
"@wordpress/block-library": "file:../block-library",
"@wordpress/blocks": "file:../blocks",
Expand All @@ -53,7 +54,8 @@
"classnames": "^2.2.5",
"lodash": "^4.17.19",
"reakit": "^1.1.0",
"rememo": "^3.0.0"
"rememo": "^3.0.0",
"uuid": "^8.3.1"
},
"publishConfig": {
"access": "public"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export default function WidgetAreasBlockEditorProvider( {
true
),
widgetAreas: select( 'core/edit-widgets' ).getWidgetAreas(),
widgets: select( 'core/edit-widgets' ).getWidgets(),
reusableBlocks: select( 'core' ).getEntityRecords(
'postType',
'wp_block'
Expand Down
Loading