diff --git a/src/wp-includes/class-wp-entity-fields-registry.php b/src/wp-includes/class-wp-entity-fields-registry.php index 864828ca1c243..1cc9b629e086e 100644 --- a/src/wp-includes/class-wp-entity-fields-registry.php +++ b/src/wp-includes/class-wp-entity-fields-registry.php @@ -445,8 +445,12 @@ public function get_collection_fields( $directory ) { * @param string $kind The entity kind (e.g. `postType`). * @param string $name The entity name (e.g. `page`). * @return array[] The list of field definitions, in registration order. + * Empty when the entity is invalid. */ public function get_registered( $kind, $name ) { + if ( ! $this->validate_entity( __METHOD__, $kind, $name ) ) { + return array(); + } $this->initialize(); return array_values( $this->fields[ $kind ][ $name ] ?? array() ); } @@ -478,7 +482,8 @@ static function ( $entities ) { * @param string $kind The entity kind (e.g. `postType`). * @param string $name The entity name (e.g. `page`). * @return array[] { - * The script modules, in registration order. + * The script modules, in registration order. Empty when the entity + * is invalid. * * @type array ...$0 { * @type string $id The id of the script module. @@ -488,6 +493,9 @@ static function ( $entities ) { * } */ public function get_registered_field_modules( $kind, $name ) { + if ( ! $this->validate_entity( __METHOD__, $kind, $name ) ) { + return array(); + } $this->initialize(); $modules = array(); foreach ( $this->field_modules[ $kind ][ $name ] ?? array() as $module => $field_ids ) { diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-actions-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-actions-controller.php index bf9ebcf895a16..121421fc00576 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-actions-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-actions-controller.php @@ -39,10 +39,6 @@ public function __construct() { * @since 7.2.0 */ public function register_routes() { - if ( $this->is_route_of_entity_items() ) { - return; - } - register_rest_route( $this->namespace, '/' . $this->rest_base, @@ -71,44 +67,6 @@ public function register_routes() { ); } - /** - * Checks whether a post type or taxonomy has its items on the route of - * the controller, reporting it with _doing_it_wrong(). - * - * The route is then left to the post type or taxonomy: registering it - * as well would not serve the actions, as the route of the items comes - * first, and would replace the schema of that route. - * - * @since 7.2.0 - * - * @return bool Whether a post type or taxonomy has its items on the route. - */ - protected function is_route_of_entity_items() { - $objects = array_merge( - array_values( get_post_types( array( 'show_in_rest' => true ), 'objects' ) ), - array_values( get_taxonomies( array( 'show_in_rest' => true ), 'objects' ) ) - ); - foreach ( $objects as $object ) { - $namespace = $object->rest_namespace ? $object->rest_namespace : 'wp/v2'; - $base = $object->rest_base ? $object->rest_base : $object->name; - if ( $this->namespace === $namespace && $this->rest_base === $base ) { - _doing_it_wrong( - __METHOD__, - sprintf( - /* translators: 1: REST route, e.g. /wp/v2/entity-actions. 2: Post type or taxonomy name. */ - __( 'The %1$s route serves the items of the "%2$s" post type or taxonomy, so the actions of entities are not available.' ), - '/' . $this->namespace . '/' . $this->rest_base, - $object->name - ), - '7.2.0' - ); - return true; - } - } - - return false; - } - /** * Checks if a given request has access to read the actions of an entity. * diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-fields-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-fields-controller.php index c9738f2a7bb6e..cbc16bee7c01b 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-fields-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-entity-fields-controller.php @@ -51,11 +51,13 @@ public function register_routes() { 'kind' => array( 'description' => __( 'Entity kind.' ), 'type' => 'string', + 'minLength' => 1, 'required' => true, ), 'name' => array( 'description' => __( 'Entity name.' ), 'type' => 'string', + 'minLength' => 1, 'required' => true, ), ), diff --git a/tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php b/tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php index 6a035503d1f1f..f12d6731bc365 100644 --- a/tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php +++ b/tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php @@ -548,6 +548,39 @@ static function () use ( &$results ) { $this->assertContains( 'author', array_column( wp_get_registered_entity_fields( 'postType', 'page' ), 'id' ), 'The default field is kept.' ); } + /** + * Reading with an entity kind or name that is not a non-empty string + * returns empty lists with a notice rather than a fatal error. + * + * @ticket 66229 + * + * @dataProvider data_invalid_entities + * + * @param mixed $kind The entity kind. + * @param mixed $name The entity name. + */ + public function test_reading_with_an_invalid_entity_returns_empty_lists( $kind, $name ) { + $this->setExpectedIncorrectUsage( 'WP_Entity_Fields_Registry::get_registered' ); + $this->setExpectedIncorrectUsage( 'WP_Entity_Fields_Registry::get_registered_field_modules' ); + + $this->assertSame( array(), wp_get_registered_entity_fields( $kind, $name ), 'No fields should be returned.' ); + $this->assertSame( array(), wp_get_registered_entity_field_modules( $kind, $name ), 'No script modules should be returned.' ); + } + + /** + * Data provider. + * + * @return array The entity kind and name. + */ + public static function data_invalid_entities() { + return array( + 'an array kind' => array( array(), 'page' ), + 'an array name' => array( 'postType', array() ), + 'an empty kind' => array( '', 'page' ), + 'an integer name' => array( 'postType', 1 ), + ); + } + /** * An id that is not a non-empty string is reported and skipped: the * other ids of the call are unregistered. diff --git a/tests/phpunit/tests/rest-api/rest-entity-actions-controller.php b/tests/phpunit/tests/rest-api/rest-entity-actions-controller.php index c67107147493a..15f58d7a47cc7 100644 --- a/tests/phpunit/tests/rest-api/rest-entity-actions-controller.php +++ b/tests/phpunit/tests/rest-api/rest-entity-actions-controller.php @@ -144,36 +144,6 @@ public function test_register_routes() { $this->assertInstanceOf( 'WP_REST_Entity_Actions_Controller', $routes[ self::ROUTE ][0]['callback'][0] ); } - /** - * A taxonomy whose items are on the route keeps it: the route of the - * actions is not registered, and that is reported. - * - * @covers ::register_routes - * @covers ::is_route_of_entity_items - * - * @ticket 66343 - */ - public function test_the_route_of_a_taxonomy_with_the_same_base_is_kept() { - global $wp_rest_server; - $this->setExpectedIncorrectUsage( 'WP_REST_Entity_Actions_Controller::is_route_of_entity_items' ); - register_taxonomy( 'entity-actions', 'post', array( 'show_in_rest' => true ) ); - - try { - $wp_rest_server = null; - $handlers = rest_get_server()->get_routes()[ self::ROUTE ]; - $response = rest_get_server()->dispatch( new WP_REST_Request( 'GET', self::ROUTE ) ); - } finally { - unregister_taxonomy( 'entity-actions' ); - $wp_rest_server = null; - } - - $this->assertCount( 2, $handlers, 'Only the route of the terms is registered.' ); - $this->assertInstanceOf( WP_REST_Terms_Controller::class, $handlers[0]['callback'][0] ); - $this->assertArrayHasKey( 'POST', $handlers[1]['methods'], 'Terms can still be created.' ); - $this->assertSame( 200, $response->get_status(), 'The terms are listed.' ); - $this->assertSame( array(), $response->get_data() ); - } - /** * Editors (with `edit_posts`) can read the actions. * diff --git a/tests/phpunit/tests/rest-api/rest-entity-fields-controller.php b/tests/phpunit/tests/rest-api/rest-entity-fields-controller.php index f5ac37779f8b3..5e3d5561ce435 100644 --- a/tests/phpunit/tests/rest-api/rest-entity-fields-controller.php +++ b/tests/phpunit/tests/rest-api/rest-entity-fields-controller.php @@ -405,6 +405,12 @@ public function test_get_items_requires_kind_and_name() { $missing_kind = $this->dispatch_request( null, 'page' ); $this->assertErrorResponse( 'rest_missing_callback_param', $missing_kind, 400 ); + + $empty_kind = $this->dispatch_request( '', 'page' ); + $this->assertErrorResponse( 'rest_invalid_param', $empty_kind, 400 ); + + $empty_name = $this->dispatch_request( 'postType', '' ); + $this->assertErrorResponse( 'rest_invalid_param', $empty_name, 400 ); } /** diff --git a/tests/qunit/fixtures/wp-api-generated.js b/tests/qunit/fixtures/wp-api-generated.js index 807eda9d2cf92..bb83cd91b8728 100644 --- a/tests/qunit/fixtures/wp-api-generated.js +++ b/tests/qunit/fixtures/wp-api-generated.js @@ -13134,11 +13134,13 @@ mockedApiResponse.Schema = { "kind": { "description": "Entity kind.", "type": "string", + "minLength": 1, "required": true }, "name": { "description": "Entity name.", "type": "string", + "minLength": 1, "required": true } }