From ba1096f2ba80924c06551b53c43c1850ab9dc432 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Tue, 17 Jan 2023 11:36:23 +0100 Subject: [PATCH 1/3] Add description support for block pattern categories --- ...wp-rest-block-pattern-categories-controller.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-pattern-categories-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-pattern-categories-controller.php index 761c0d677741a..bd6b76ac2c270 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-pattern-categories-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-pattern-categories-controller.php @@ -102,10 +102,10 @@ public function get_items( $request ) { */ 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 @@ public function get_item_schema() { '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' ), + ), ), ); From 339e7e71ee0f6a470947e2c92af19656b793e7da Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Tue, 17 Jan 2023 11:51:21 +0100 Subject: [PATCH 2/3] Update the unit test to cover descriptions --- .../rest-api/wpRestBlockPatternCategoriesController.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/tests/rest-api/wpRestBlockPatternCategoriesController.php b/tests/phpunit/tests/rest-api/wpRestBlockPatternCategoriesController.php index bd9dd9a0dddea..357aa43b60073 100644 --- a/tests/phpunit/tests/rest-api/wpRestBlockPatternCategoriesController.php +++ b/tests/phpunit/tests/rest-api/wpRestBlockPatternCategoriesController.php @@ -74,8 +74,8 @@ public static function wpSetupBeforeClass( $factory ) { 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 +103,10 @@ public function test_get_items() { 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(); From 2c7dc5df91f5cac26801506d1e339ac2bde7d9ae Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Tue, 17 Jan 2023 12:13:15 +0100 Subject: [PATCH 3/3] Fix phpcs --- .../wpRestBlockPatternCategoriesController.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/rest-api/wpRestBlockPatternCategoriesController.php b/tests/phpunit/tests/rest-api/wpRestBlockPatternCategoriesController.php index 357aa43b60073..256eabdb82f34 100644 --- a/tests/phpunit/tests/rest-api/wpRestBlockPatternCategoriesController.php +++ b/tests/phpunit/tests/rest-api/wpRestBlockPatternCategoriesController.php @@ -74,8 +74,20 @@ public static function wpSetupBeforeClass( $factory ) { self::$registry_instance_property->setValue( $test_registry ); // Register some categories in the test registry. - $test_registry->register( 'test', array( 'label' => 'Test', 'description' => 'Test description' ) ); - $test_registry->register( 'query', array( 'label' => 'Query', 'description' => 'Query' ) ); + $test_registry->register( + 'test', + array( + 'label' => 'Test', + 'description' => 'Test description', + ) + ); + $test_registry->register( + 'query', + array( + 'label' => 'Query', + 'description' => 'Query', + ) + ); } public static function wpTearDownAfterClass() {