Skip to content
Open
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: 9 additions & 1 deletion src/wp-includes/class-wp-entity-fields-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

These checks are good, we should have them. Thanks.

return array();
}
$this->initialize();
return array_values( $this->fields[ $kind ][ $name ] ?? array() );
}
Expand Down Expand Up @@ -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.
Expand All @@ -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 ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ public function register_routes() {
'kind' => array(
'description' => __( 'Entity kind.' ),
'type' => 'string',
'minLength' => 1,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is good, thanks for bringing them in for entity fields.

'required' => true,
),
'name' => array(
'description' => __( 'Entity name.' ),
'type' => 'string',
'minLength' => 1,
'required' => true,
),
),
Expand Down
33 changes: 33 additions & 0 deletions tests/phpunit/tests/entity-fields/wpEntityFieldsRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, array{0: mixed, 1: mixed}> 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.
Expand Down
30 changes: 0 additions & 30 deletions tests/phpunit/tests/rest-api/rest-entity-actions-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/qunit/fixtures/wp-api-generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
Loading