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

Navigation Editor: Fix saving locations using the "Manage Locations" popup #34714

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
6e60a4f
Create SaveButton component.
anton-vlasenko Sep 6, 2021
0f00012
Fix code style.
anton-vlasenko Sep 6, 2021
199f66a
Remove SaveButton component.
anton-vlasenko Sep 6, 2021
248b25d
Implement a function that sends batch requests to update menu locations.
anton-vlasenko Sep 7, 2021
6b61772
1. Fix request path for __experimental/menus route.
anton-vlasenko Sep 8, 2021
a7477ad
Fix code style.
anton-vlasenko Sep 8, 2021
580ae4f
Allow batch requests for `__experimental/menus` endpoints because we …
anton-vlasenko Sep 8, 2021
0bd18e4
1. Display an error or a success message when updating menus' locations.
anton-vlasenko Sep 8, 2021
d8b4baf
Add async/await to be sure we return get response instead of a Promise.
anton-vlasenko Sep 9, 2021
bbe16c2
Move "save" button to the right.
anton-vlasenko Sep 9, 2021
e580a3f
In this context "Update" is better than "Save".
anton-vlasenko Sep 9, 2021
1a8d4a9
Increase unit test code coverage by adding a unit test for WP_REST_Me…
anton-vlasenko Sep 9, 2021
311539b
There was a type in the method's name.
anton-vlasenko Sep 9, 2021
3cff794
Fix code style.
anton-vlasenko Sep 9, 2021
c6b901d
1. Export createBatch function so that it can be used externally.
anton-vlasenko Sep 10, 2021
3d81931
1. Refactor Manage Locations and use createBatch function to send bat…
anton-vlasenko Sep 10, 2021
6757073
Update lib/class-wp-rest-menus-controller.php
anton-vlasenko Sep 10, 2021
1b64516
Fix parent class name.
anton-vlasenko Sep 10, 2021
1f7bd51
Fix code style.
anton-vlasenko Sep 10, 2021
358f609
Add default domain for translation because lint-php check fails.
anton-vlasenko Sep 10, 2021
f86bc8d
Fix the unit test.
anton-vlasenko Sep 10, 2021
d150a76
Move the test to the REST_Nav_Menus_Controller_Test.
anton-vlasenko Sep 10, 2021
39fd38f
1. We need to place this public method before protected and private m…
anton-vlasenko Sep 10, 2021
03f170b
Revert "1. We need to place this public method before protected and p…
anton-vlasenko Sep 10, 2021
1b14218
Revert "Move the test to the REST_Nav_Menus_Controller_Test."
anton-vlasenko Sep 10, 2021
12268f8
Fix the test.
anton-vlasenko Sep 10, 2021
f7d1783
Fix the error inside the WP_REST_Menus_Controller::register_routes
anton-vlasenko Sep 10, 2021
d31cff2
Revert "1. Export createBatch function so that it can be used externa…
anton-vlasenko Sep 10, 2021
215622b
Refactor locations update logic and revert to apiFetch.
anton-vlasenko Sep 10, 2021
3254ad4
Fix the import statement.
anton-vlasenko Sep 10, 2021
43d9e70
Move the test to the REST_Nav_Menus_Controller_Test.
anton-vlasenko Sep 10, 2021
8f5b433
Fix code style.
anton-vlasenko Sep 10, 2021
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
72 changes: 72 additions & 0 deletions lib/class-wp-rest-menus-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* @see WP_REST_Controller
*/
class WP_REST_Menus_Controller extends WP_REST_Terms_Controller {

/**
* Constructor.
*
Expand All @@ -22,6 +23,77 @@ public function __construct( $taxonomy ) {
$this->namespace = '__experimental';
}

/**
* Overrides the route registration to support "allow_batch".
*
* @since 11.5.0
*
* @see register_rest_route()
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
),
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array( $this, 'create_item' ),
'permission_callback' => array( $this, 'create_item_permissions_check' ),
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);

register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[\d]+)',
array(
'args' => array(
'id' => array(
'description' => __( 'Unique identifier for the term.', 'default' ),
'type' => 'integer',
),
),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item' ),
'permission_callback' => array( $this, 'get_item_permissions_check' ),
'args' => array(
'context' => $this->get_context_param( array( 'default' => 'view' ) ),
),
),
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'update_item' ),
'permission_callback' => array( $this, 'update_item_permissions_check' ),
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
),
array(
'methods' => WP_REST_Server::DELETABLE,
'callback' => array( $this, 'delete_item' ),
'permission_callback' => array( $this, 'delete_item_permissions_check' ),
'args' => array(
'force' => array(
'type' => 'boolean',
'default' => false,
'description' => __( 'Required to be true, as terms do not support trashing.', 'default' ),
),
),
),
'allow_batch' => array( 'v1' => true ),
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}


/**
* Checks if a request has access to read terms in the specified taxonomy.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import {
SelectControl,
} from '@wordpress/components';
import { decodeEntities } from '@wordpress/html-entities';
import apiFetch from '@wordpress/api-fetch';
import { useDispatch } from '@wordpress/data';
import { store as noticesStore } from '@wordpress/notices';

/**
* Internal dependencies
Expand All @@ -31,6 +34,62 @@ export default function ManageLocations( {
const [ isModalOpen, setIsModalOpen ] = useState( false );
const openModal = () => setIsModalOpen( true );
const closeModal = () => setIsModalOpen( false );
const { createSuccessNotice, createErrorNotice } = useDispatch(
noticesStore
);

const validateBatchResponse = ( batchResponse ) => {
if ( batchResponse.failed ) {
return false;
}

const errorResponses = batchResponse.responses.filter( ( response ) => {
return 200 > response.status || 300 <= response.status;
} );

return 1 > errorResponses.length;
};

const handleUpdateMenuLocations = async () => {
const method = 'POST';
const batchRequests = menus.map( ( { id } ) => {
const locations = menuLocations
.filter( ( menuLocation ) => menuLocation.menu === id )
.map( ( menuLocation ) => menuLocation.name );

return {
path: `/__experimental/menus/${ id }`,
body: {
locations,
},
method,
};
} );

const batchResponse = await apiFetch( {
path: 'batch/v1',
data: {
validation: 'require-all-validate',
requests: batchRequests,
},
method,
} );

const isSuccess = validateBatchResponse( batchResponse );

if ( isSuccess ) {
createSuccessNotice( __( 'Menu locations have been updated.' ), {
type: 'snackbar',
} );
closeModal();
return;
}

createErrorNotice(
__( 'An error occurred while trying to update menu locations.' ),
{ type: 'snackbar' }
);
};

if ( ! menuLocations || ! menus?.length ) {
return <Spinner />;
Expand Down Expand Up @@ -156,6 +215,13 @@ export default function ManageLocations( {
{ themeLocationCountTextModal }
</div>
{ menuLocationCard }
<Button
className="edit-navigation-manage-locations__save-button"
variant="primary"
onClick={ handleUpdateMenuLocations }
>
{ __( 'Update' ) }
</Button>
</Modal>
) }
</PanelBody>
Expand Down
4 changes: 4 additions & 0 deletions packages/edit-navigation/src/components/sidebar/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,7 @@
.edit-navigation-manage-locations__theme-location-text-modal {
margin-bottom: $grid-unit-30;
}

.edit-navigation-manage-locations__save-button {
float: right;
}
10 changes: 10 additions & 0 deletions phpunit/class-rest-nav-menus-controller-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,16 @@ public function test_get_item_wrong_permission() {
$this->assertErrorResponse( 'rest_cannot_view', $response, 403 );
}

public function test_it_allows_batch_requests_when_updating_menus() {
$rest_server = rest_get_server();
// This call is needed to initialize route_options.
$rest_server->get_routes();
$route_options = $rest_server->get_route_options( '/__experimental/menus/(?P<id>[\d]+)' );

$this->assertArrayHasKey( 'allow_batch', $route_options );
$this->assertSame( array( 'v1' => true ), $route_options['allow_batch'] );
}

/**
* @param WP_REST_Response $response Response Class.
*/
Expand Down