From 615a27832ac501490421539e9c3bc3bb11e21e39 Mon Sep 17 00:00:00 2001 From: Jorge Date: Mon, 30 Jan 2023 19:08:15 +0000 Subject: [PATCH 1/3] Add: Template types to the patterns API. --- src/wp-includes/block-patterns.php | 3 ++- ...lass-wp-rest-block-patterns-controller.php | 7 +++++++ .../wpRestBlockPatternsController.php | 20 +++++++++++-------- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/wp-includes/block-patterns.php b/src/wp-includes/block-patterns.php index da35e019b4641..863059b607ab6 100644 --- a/src/wp-includes/block-patterns.php +++ b/src/wp-includes/block-patterns.php @@ -318,6 +318,7 @@ function _register_theme_block_patterns() { 'blockTypes' => 'Block Types', 'postTypes' => 'Post Types', 'inserter' => 'Inserter', + 'templateTypes' => 'Template Types', ); /* @@ -388,7 +389,7 @@ function _register_theme_block_patterns() { } // For properties of type array, parse data as comma-separated. - foreach ( array( 'categories', 'keywords', 'blockTypes', 'postTypes' ) as $property ) { + foreach ( array( 'categories', 'keywords', 'blockTypes', 'postTypes', 'templateTypes' ) as $property ) { if ( ! empty( $pattern_data[ $property ] ) ) { $pattern_data[ $property ] = array_filter( preg_split( diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php index adc4a0aade8cf..721bef19afae5 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php @@ -173,6 +173,7 @@ public function prepare_item_for_response( $item, $request ) { 'keywords' => 'keywords', 'content' => 'content', 'inserter' => 'inserter', + 'templateTypes' => 'template_types', ); $data = array(); foreach ( $keys as $item_key => $rest_key ) { @@ -248,6 +249,12 @@ public function get_item_schema() { 'readonly' => true, 'context' => array( 'view', 'edit', 'embed' ), ), + 'template_types' => array( + 'description' => __( 'An array of template types where the pattern fits.', 'gutenberg' ), + 'type' => 'array', + 'readonly' => true, + 'context' => array( 'view', 'edit', 'embed' ), + ), 'content' => array( 'description' => __( 'The pattern content.' ), 'type' => 'string', diff --git a/tests/phpunit/tests/rest-api/wpRestBlockPatternsController.php b/tests/phpunit/tests/rest-api/wpRestBlockPatternsController.php index 3229f2f38dd2e..e2d814becc38e 100644 --- a/tests/phpunit/tests/rest-api/wpRestBlockPatternsController.php +++ b/tests/phpunit/tests/rest-api/wpRestBlockPatternsController.php @@ -81,15 +81,17 @@ public static function wpSetUpBeforeClass( $factory ) { 'categories' => array( 'test' ), 'viewportWidth' => 1440, 'content' => '

One

', + 'templateTypes' => array( 'page' ), ) ); $test_registry->register( 'test/two', array( - 'title' => 'Pattern Two', - 'categories' => array( 'test' ), - 'content' => '

Two

', + 'title' => 'Pattern Two', + 'categories' => array( 'test' ), + 'content' => '

Two

', + 'templateTypes' => array( 'single' ), ) ); @@ -128,7 +130,7 @@ public function test_get_items() { wp_set_current_user( self::$admin_id ); $request = new WP_REST_Request( 'GET', static::REQUEST_ROUTE ); - $request['_fields'] = 'name,content'; + $request['_fields'] = 'name,content,template_types'; $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); @@ -136,16 +138,18 @@ public function test_get_items() { $this->assertGreaterThanOrEqual( 2, count( $data ), 'WP_REST_Block_Patterns_Controller::get_items() should return at least 2 items' ); $this->assertSame( array( - 'name' => 'test/one', - 'content' => '

One

', + 'name' => 'test/one', + 'content' => '

One

', + 'template_types' => array( 'page' ), ), $data[0], 'WP_REST_Block_Patterns_Controller::get_items() should return test/one' ); $this->assertSame( array( - 'name' => 'test/two', - 'content' => '

Two

', + 'name' => 'test/two', + 'content' => '

Two

', + 'template_types' => array( 'single' ), ), $data[1], 'WP_REST_Block_Patterns_Controller::get_items() should return test/two' From f12d38ebe9a76c33f664ba07b67c9117e7f191e4 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Tue, 31 Jan 2023 15:10:39 +0000 Subject: [PATCH 2/3] Update src/wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php Co-authored-by: Mukesh Panchal --- .../endpoints/class-wp-rest-block-patterns-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php index 721bef19afae5..4d50bfae047b4 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php @@ -250,7 +250,7 @@ public function get_item_schema() { 'context' => array( 'view', 'edit', 'embed' ), ), 'template_types' => array( - 'description' => __( 'An array of template types where the pattern fits.', 'gutenberg' ), + 'description' => __( 'An array of template types where the pattern fits.' ), 'type' => 'array', 'readonly' => true, 'context' => array( 'view', 'edit', 'embed' ), From d8e6b95a6d036e74cdc0c909e09a25a153ede417 Mon Sep 17 00:00:00 2001 From: Jorge Date: Tue, 31 Jan 2023 16:17:23 +0000 Subject: [PATCH 3/3] feedback --- src/wp-includes/block-patterns.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wp-includes/block-patterns.php b/src/wp-includes/block-patterns.php index 863059b607ab6..05bb6817ebaf2 100644 --- a/src/wp-includes/block-patterns.php +++ b/src/wp-includes/block-patterns.php @@ -302,6 +302,7 @@ function _register_remote_theme_patterns() { * - Keywords (comma-separated values) * - Block Types (comma-separated values) * - Post Types (comma-separated values) + * - Template Types (comma-separated values) * - Inserter (yes/no) * * @since 6.0.0