Editor: Allow block pattern categories to have descriptions.

Updates the corresponding REST API endpoint and unit test.

Fixes #57478.


git-svn-id: https://develop.svn.wordpress.org/trunk@55097 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Riad Benguella
2023-01-19 12:11:57 +00:00
parent 134c05d698
commit 48342a279d
2 changed files with 26 additions and 8 deletions

View File

@@ -102,10 +102,10 @@ class WP_REST_Block_Pattern_Categories_Controller extends WP_REST_Controller {
*/
public function prepare_item_for_response( $item, $request ) {
$fields = $this->get_fields_for_response( $request );
$keys = array( 'name', 'label' );
$keys = array( 'name', 'label', 'description' );
$data = array();
foreach ( $keys as $key ) {
if ( rest_is_field_included( $key, $fields ) ) {
if ( isset( $item[ $key ] ) && rest_is_field_included( $key, $fields ) ) {
$data[ $key ] = $item[ $key ];
}
}
@@ -130,18 +130,24 @@ class WP_REST_Block_Pattern_Categories_Controller extends WP_REST_Controller {
'title' => 'block-pattern-category',
'type' => 'object',
'properties' => array(
'name' => array(
'name' => array(
'description' => __( 'The category name.' ),
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
),
'label' => array(
'label' => array(
'description' => __( 'The category label, in human readable format.' ),
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
),
'description' => array(
'description' => __( 'The category description, in human readable format.' ),
'type' => 'string',
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
),
),
);

View File

@@ -74,8 +74,20 @@ class Tests_REST_WpRestBlockPatternCategoriesController extends WP_Test_REST_Con
self::$registry_instance_property->setValue( $test_registry );
// Register some categories in the test registry.
$test_registry->register( 'test', array( 'label' => 'Test' ) );
$test_registry->register( 'query', array( 'label' => 'Query' ) );
$test_registry->register(
'test',
array(
'label' => 'Test',
'description' => 'Test description',
)
);
$test_registry->register(
'query',
array(
'label' => 'Query',
'description' => 'Query',
)
);
}
public static function wpTearDownAfterClass() {
@@ -103,10 +115,10 @@ class Tests_REST_WpRestBlockPatternCategoriesController extends WP_Test_REST_Con
wp_set_current_user( self::$admin_id );
$expected_names = array( 'test', 'query' );
$expected_fields = array( 'name', 'label' );
$expected_fields = array( 'name', 'label', 'description' );
$request = new WP_REST_Request( 'GET', static::REQUEST_ROUTE );
$request['_fields'] = 'name,label';
$request['_fields'] = 'name,label,description';
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();