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

Fix Nav block invalid permissions warning by avoiding using trashed wp_navigation posts. #42940

Closed
wants to merge 4 commits into from
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* REST API: Gutenberg_REST_Navigation_Controller class
*
* @package Gutenberg
* @subpackage REST_API
*/

class Gutenberg_REST_Navigation_Controller extends WP_REST_Posts_Controller {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note we will need to introduce this class anyway (see #42809)


/**
* Overide WP_REST_Posts_Controller function to discard
* certain post statuses and treat as missing.
*
* @param int $id the ID of the Navigation post.
* @return WP_Post|null
*/
protected function get_post( $id ) {

$result = parent::get_post( $id );

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

$post = $result;

if ( 'publish' === $post->post_status || 'draft' === $post->post_status ) {
return $post;
} else {
return new WP_Error(
'rest_post_invalid_id',
__( 'Invalid post ID.' ),
array( 'status' => 404 )
);
}

}
}
14 changes: 14 additions & 0 deletions lib/compat/wordpress-6.1/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,17 @@ function gutenberg_register_gutenberg_rest_block_patterns() {
$block_patterns->register_routes();
}
add_action( 'rest_api_init', 'gutenberg_register_gutenberg_rest_block_patterns', 100 );



function gutenberg_update_navigation_rest_controller( $args, $post_type ) {
if ( in_array( $post_type, array( 'wp_navigation' ), true ) ) {
// Original set in
// https://github.com/WordPress/wordpress-develop/blob/6cbed78c94b9d8c6a9b4c8b472b88ee0cd56528c/src/wp-includes/post.php#L528.
$args['rest_controller_class'] = 'Gutenberg_REST_Navigation_Controller';
}
return $args;
}
add_filter( 'register_post_type_args', 'gutenberg_update_navigation_rest_controller', 10, 2 );


1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function gutenberg_is_experiment_enabled( $name ) {
// WordPress 6.1 compat.
require_once __DIR__ . '/compat/wordpress-6.1/class-gutenberg-rest-block-patterns-controller.php';
require_once __DIR__ . '/compat/wordpress-6.1/class-gutenberg-rest-templates-controller.php';
require_once __DIR__ . '/compat/wordpress-6.1/class-gutenberg-rest-navigation-controller.php';
require_once __DIR__ . '/compat/wordpress-6.1/rest-api.php';

// Experimental.
Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/navigation/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ function Navigation( {
hasResolvedCanUserCreateNavigationMenu,
} = useNavigationMenu( ref );

const navMenuResolvedButMissing =
hasResolvedNavigationMenus && isNavigationMenuMissing;

// Attempt to retrieve and prioritize any existing navigation menu unless
// a specific ref is allocated or the user is explicitly creating a new menu. The aim is
// for the block to "just work" from a user perspective using existing data.
Expand Down Expand Up @@ -386,6 +389,7 @@ function Navigation( {
if ( isSelected || isInnerBlockSelected ) {
if (
ref &&
! navMenuResolvedButMissing &&
hasResolvedCanUserUpdateNavigationMenu &&
! canUserUpdateNavigationMenu
) {
Expand Down