From 49d01acf05c00283954cbdf18070e9ca66763853 Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Mon, 26 Jun 2023 13:17:09 +1000 Subject: [PATCH 1/6] Changes from https://github.com/WordPress/wordpress-develop/pull/4655 --- src/wp-includes/block-patterns.php | 14 ++++++++++---- .../class-wp-rest-block-patterns-controller.php | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/block-patterns.php b/src/wp-includes/block-patterns.php index e93c40619107b..b573c6f4c8db5 100644 --- a/src/wp-includes/block-patterns.php +++ b/src/wp-includes/block-patterns.php @@ -12,6 +12,7 @@ * Registers the core block patterns and categories. * * @since 5.5.0 + * @since 6.3.0 Added source to core block patterns. * @access private */ function _register_core_block_patterns_and_categories() { @@ -29,10 +30,9 @@ function _register_core_block_patterns_and_categories() { ); foreach ( $core_block_patterns as $core_block_pattern ) { - register_block_pattern( - 'core/' . $core_block_pattern, - require __DIR__ . '/block-patterns/' . $core_block_pattern . '.php' - ); + $pattern = require ABSPATH . WPINC . '/block-patterns/' . $core_block_pattern . '.php'; + $pattern['source'] = 'core'; // Added in 6.3.0. + register_block_pattern( 'core/' . $core_block_pattern, $pattern ); } } @@ -190,6 +190,7 @@ function wp_normalize_remote_block_pattern( $pattern ) { * @since 5.9.0 The $current_screen argument was removed. * @since 6.2.0 Normalize the pattern from the API (snake_case) to the * format expected by `register_block_pattern` (camelCase). + * @since 6.3.0 Add 'pattern-directory/core' to the pattern's source. * * @param WP_Screen $deprecated Unused. Formerly the screen that the current request was triggered from. */ @@ -224,6 +225,7 @@ function _load_remote_block_patterns( $deprecated = null ) { $patterns = $response->get_data(); foreach ( $patterns as $pattern ) { + $pattern['source'] = 'pattern-directory/core'; // Added in 6.3.0. $normalized_pattern = wp_normalize_remote_block_pattern( $pattern ); $pattern_name = 'core/' . sanitize_title( $normalized_pattern['title'] ); register_block_pattern( $pattern_name, $normalized_pattern ); @@ -237,6 +239,7 @@ function _load_remote_block_patterns( $deprecated = null ) { * @since 5.9.0 * @since 6.2.0 Normalized the pattern from the API (snake_case) to the * format expected by `register_block_pattern()` (camelCase). + * @since 6.3.0 Add 'pattern-directory/featured' to the pattern's 'source'. */ function _load_remote_featured_patterns() { $supports_core_patterns = get_theme_support( 'core-block-patterns' ); @@ -258,6 +261,7 @@ function _load_remote_featured_patterns() { $patterns = $response->get_data(); $registry = WP_Block_Patterns_Registry::get_instance(); foreach ( $patterns as $pattern ) { + $pattern['source'] = 'pattern-directory/featured'; // Added in 6.3.0. $normalized_pattern = wp_normalize_remote_block_pattern( $pattern ); $pattern_name = sanitize_title( $normalized_pattern['title'] ); // Some patterns might be already registered as core patterns with the `core` prefix. @@ -275,6 +279,7 @@ function _load_remote_featured_patterns() { * @since 6.0.0 * @since 6.2.0 Normalized the pattern from the API (snake_case) to the * format expected by `register_block_pattern()` (camelCase). + * @since 6.3.0 Add 'pattern-directory/theme' to the pattern's 'source'. * @access private */ function _register_remote_theme_patterns() { @@ -301,6 +306,7 @@ function _register_remote_theme_patterns() { $patterns = $response->get_data(); $patterns_registry = WP_Block_Patterns_Registry::get_instance(); foreach ( $patterns as $pattern ) { + $pattern['source'] = 'pattern-directory/theme'; // Added in 6.3.0. $normalized_pattern = wp_normalize_remote_block_pattern( $pattern ); $pattern_name = sanitize_title( $normalized_pattern['title'] ); // Some patterns might be already registered as core patterns with the `core` prefix. 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 445f8c82f6e6f..3acaefdf2cab8 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 @@ -155,6 +155,7 @@ protected function migrate_pattern_categories( $pattern ) { * Prepare a raw block pattern before it gets output in a REST API response. * * @since 6.0.0 + * @since 6.3.0 Added `source` property. * * @param array $item Raw pattern as registered, before any changes. * @param WP_REST_Request $request Request object. @@ -174,6 +175,7 @@ public function prepare_item_for_response( $item, $request ) { 'blockTypes' => 'block_types', 'postTypes' => 'post_types', 'templateTypes' => 'template_types', + 'source' => 'source', ); $data = array(); foreach ( $keys as $item_key => $rest_key ) { @@ -192,6 +194,7 @@ public function prepare_item_for_response( $item, $request ) { * Retrieves the block pattern schema, conforming to JSON Schema. * * @since 6.0.0 + * @since 6.3.0 Added `source` property. * * @return array Item schema data. */ @@ -267,6 +270,20 @@ public function get_item_schema() { 'readonly' => true, 'context' => array( 'view', 'edit', 'embed' ), ), + 'source' => array( + 'description' => __( 'Where the pattern comes from e.g. core' ), + 'type' => 'string', + 'readonly' => true, + 'context' => array( 'view', 'edit', 'embed' ), + 'enum' => array( + 'core', + 'plugin', + 'theme', + 'pattern-directory/core', + 'pattern-directory/theme', + 'pattern-directory/featured', + ), + ), ), ); From 9a6553dc2515621572960bad0d77eaffd01902f7 Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Mon, 26 Jun 2023 14:03:24 +1000 Subject: [PATCH 2/6] Tweak comment Co-authored-by: Mukesh Panchal --- src/wp-includes/block-patterns.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/block-patterns.php b/src/wp-includes/block-patterns.php index b573c6f4c8db5..82e3c79b92e69 100644 --- a/src/wp-includes/block-patterns.php +++ b/src/wp-includes/block-patterns.php @@ -190,7 +190,7 @@ function wp_normalize_remote_block_pattern( $pattern ) { * @since 5.9.0 The $current_screen argument was removed. * @since 6.2.0 Normalize the pattern from the API (snake_case) to the * format expected by `register_block_pattern` (camelCase). - * @since 6.3.0 Add 'pattern-directory/core' to the pattern's source. + * @since 6.3.0 Add 'pattern-directory/core' to the pattern's 'source'. * * @param WP_Screen $deprecated Unused. Formerly the screen that the current request was triggered from. */ From a65767a6699dccb37278ab996f4dd0503a65fe61 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Mon, 26 Jun 2023 13:08:26 +0800 Subject: [PATCH 3/6] Revert change to path --- src/wp-includes/block-patterns.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/block-patterns.php b/src/wp-includes/block-patterns.php index 82e3c79b92e69..35819971c931f 100644 --- a/src/wp-includes/block-patterns.php +++ b/src/wp-includes/block-patterns.php @@ -30,7 +30,7 @@ function _register_core_block_patterns_and_categories() { ); foreach ( $core_block_patterns as $core_block_pattern ) { - $pattern = require ABSPATH . WPINC . '/block-patterns/' . $core_block_pattern . '.php'; + $pattern = require __DIR__ . '/block-patterns/' . $core_block_pattern . '.php'; $pattern['source'] = 'core'; // Added in 6.3.0. register_block_pattern( 'core/' . $core_block_pattern, $pattern ); } From 679ba66f5f27a683bcd65346f47fd82e6cc72b9c Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Mon, 26 Jun 2023 13:09:38 +0800 Subject: [PATCH 4/6] Remove added in 6.3.0 comments --- src/wp-includes/block-patterns.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/block-patterns.php b/src/wp-includes/block-patterns.php index 35819971c931f..a2d90a261e43b 100644 --- a/src/wp-includes/block-patterns.php +++ b/src/wp-includes/block-patterns.php @@ -31,7 +31,7 @@ function _register_core_block_patterns_and_categories() { foreach ( $core_block_patterns as $core_block_pattern ) { $pattern = require __DIR__ . '/block-patterns/' . $core_block_pattern . '.php'; - $pattern['source'] = 'core'; // Added in 6.3.0. + $pattern['source'] = 'core'; register_block_pattern( 'core/' . $core_block_pattern, $pattern ); } } @@ -225,7 +225,7 @@ function _load_remote_block_patterns( $deprecated = null ) { $patterns = $response->get_data(); foreach ( $patterns as $pattern ) { - $pattern['source'] = 'pattern-directory/core'; // Added in 6.3.0. + $pattern['source'] = 'pattern-directory/core'; $normalized_pattern = wp_normalize_remote_block_pattern( $pattern ); $pattern_name = 'core/' . sanitize_title( $normalized_pattern['title'] ); register_block_pattern( $pattern_name, $normalized_pattern ); @@ -261,7 +261,7 @@ function _load_remote_featured_patterns() { $patterns = $response->get_data(); $registry = WP_Block_Patterns_Registry::get_instance(); foreach ( $patterns as $pattern ) { - $pattern['source'] = 'pattern-directory/featured'; // Added in 6.3.0. + $pattern['source'] = 'pattern-directory/featured'; $normalized_pattern = wp_normalize_remote_block_pattern( $pattern ); $pattern_name = sanitize_title( $normalized_pattern['title'] ); // Some patterns might be already registered as core patterns with the `core` prefix. @@ -306,7 +306,7 @@ function _register_remote_theme_patterns() { $patterns = $response->get_data(); $patterns_registry = WP_Block_Patterns_Registry::get_instance(); foreach ( $patterns as $pattern ) { - $pattern['source'] = 'pattern-directory/theme'; // Added in 6.3.0. + $pattern['source'] = 'pattern-directory/theme'; $normalized_pattern = wp_normalize_remote_block_pattern( $pattern ); $pattern_name = sanitize_title( $normalized_pattern['title'] ); // Some patterns might be already registered as core patterns with the `core` prefix. From 69af6b2c063cbf317a8ec1f133971dac9870fc68 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Mon, 26 Jun 2023 13:17:06 +0800 Subject: [PATCH 5/6] Try updating tests --- .../phpunit/tests/rest-api/wpRestBlockPatternsController.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/phpunit/tests/rest-api/wpRestBlockPatternsController.php b/tests/phpunit/tests/rest-api/wpRestBlockPatternsController.php index 319eb20bab65c..967cec9c31fb6 100644 --- a/tests/phpunit/tests/rest-api/wpRestBlockPatternsController.php +++ b/tests/phpunit/tests/rest-api/wpRestBlockPatternsController.php @@ -76,6 +76,7 @@ public static function wpSetUpBeforeClass( $factory ) { 'viewportWidth' => 1440, 'categories' => array( 'test' ), 'templateTypes' => array( 'page' ), + 'source' => 'theme', ) ); @@ -86,6 +87,7 @@ public static function wpSetUpBeforeClass( $factory ) { 'content' => '

Two

', 'categories' => array( 'test' ), 'templateTypes' => array( 'single' ), + 'source' => 'core', ) ); @@ -95,6 +97,7 @@ public static function wpSetUpBeforeClass( $factory ) { 'title' => 'Pattern Three', 'content' => '

Three

', 'categories' => array( 'test', 'buttons', 'query' ), + 'source' => 'pattern-directory/featured', ) ); } @@ -135,6 +138,7 @@ public function test_get_items() { 'name' => 'test/one', 'content' => '

One

', 'template_types' => array( 'page' ), + 'source' => 'theme', ), $data[0], 'WP_REST_Block_Patterns_Controller::get_items() should return test/one' @@ -144,6 +148,7 @@ public function test_get_items() { 'name' => 'test/two', 'content' => '

Two

', 'template_types' => array( 'single' ), + 'source' => 'core', ), $data[1], 'WP_REST_Block_Patterns_Controller::get_items() should return test/two' From 567d5f83f652b62c386cc086bcdf5fa7b8c97da1 Mon Sep 17 00:00:00 2001 From: Aaron Robertshaw <60436221+aaronrobertshaw@users.noreply.github.com> Date: Mon, 26 Jun 2023 15:49:14 +1000 Subject: [PATCH 6/6] Request source field in get_items test --- tests/phpunit/tests/rest-api/wpRestBlockPatternsController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/rest-api/wpRestBlockPatternsController.php b/tests/phpunit/tests/rest-api/wpRestBlockPatternsController.php index 967cec9c31fb6..5482e32cbede2 100644 --- a/tests/phpunit/tests/rest-api/wpRestBlockPatternsController.php +++ b/tests/phpunit/tests/rest-api/wpRestBlockPatternsController.php @@ -127,7 +127,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,template_types'; + $request['_fields'] = 'name,content,source,template_types'; $response = rest_get_server()->dispatch( $request ); $data = $response->get_data();