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

Creating a wp_navigation in REST throws undefined property error in WordPress 6.5. #59867

Closed
Hug0-Drelon opened this issue Mar 14, 2024 · 1 comment · Fixed by #59875
Closed
Assignees
Labels
[Block] Navigation Affects the Navigation Block [Type] Bug An existing feature does not function as intended

Comments

@Hug0-Drelon
Copy link
Contributor

Hug0-Drelon commented Mar 14, 2024

Description

Creating a wp_navigation in REST throws Undefined property: stdClass::$ID error.

Step-by-step reproduction instructions

Create a POST REST request on wp/v2/navigation with at least title or content parameter set.
See debug.log for an error such as:

Undefined property: stdClass::$ID

/var/www/html/wp-includes/blocks/navigation.php:1479
/var/www/html/wp-includes/class-wp-hook.php:326
/var/www/html/wp-includes/plugin.php:205
/var/www/html/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php:1399
/var/www/html/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php:650
/var/www/html/wp-includes/rest-api/class-wp-rest-server.php:1230
/var/www/html/wp-includes/rest-api/class-wp-rest-server.php:1063
/wordpress-phpunit/includes/spy-rest-server.php:71

Screenshots, screen recording, code snippet

Here is a PHPUnit test that shows the bug:

<?php

class Gutenberg_REST_Create_Navigation_Block_Test extends WP_UnitTestCase {
	public function test_block_core_navigation_rest_creation() {
		wp_set_current_user( 1 );

		$post_type_object = get_post_type_object( 'wp_navigation' );
		$request = new WP_REST_Request( 'POST', '/wp/v2/' . $post_type_object->rest_base );
		$request->set_param( 'title', 'Title ' . $post_type_object->label );
		$request->set_param( 'content', $post_type_object->label );
		$request->set_param( '_locale', 'user' );

		$response = rest_get_server()->dispatch( $request ); // Triggers the error.

                 // Assertions aren't reached.
		$this->assertNotEmpty( $response->get_status() );
		$this->assertSame( 201, $response->get_status() );
	}
}

For what I understand, the bug was introduced in this PR.
During creation, there is no $post->ID set yet, so doing get_post( $post->iD ) causes the error.

Environment info

  • WordPress nightly (from 6.5 RC2 I think)

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@Hug0-Drelon Hug0-Drelon added the [Type] Bug An existing feature does not function as intended label Mar 14, 2024
@Mamaduka Mamaduka added the [Block] Navigation Affects the Navigation Block label Mar 14, 2024
@Mamaduka
Copy link
Member

cc @ockham

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Navigation Affects the Navigation Block [Type] Bug An existing feature does not function as intended
Projects
Status: Done
4 participants