From 45ef4c0623b666ea427d19c1dad1124e23e97269 Mon Sep 17 00:00:00 2001 From: hellofromTonya Date: Tue, 24 Jan 2023 14:36:12 +0000 Subject: [PATCH] Editor: Migrate old to the new pattern categories. Adds a new non-public `WP_REST_Block_Patterns_Controller::migrate_pattern_categories()` method to automatically migrate existing content's pattern categories to the new ones introduced in [55098]. Old to New `'buttons'` to `'call-to-action'` `'columns'` to `'text'` `'query'` to `'posts'` Reference: * Part of [https://github.com/WordPress/gutenberg/pull/46144 Gutenberg PR 46144] Follow-up to [55098], [53152]. Props ntsekouras, annezazu, jameskoster, joen, hellofromTonya, mcsf, paaljoachim, ryelle. Fixes #57532. Built from https://develop.svn.wordpress.org/trunk@55125 git-svn-id: http://core.svn.wordpress.org/trunk@54658 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- ...lass-wp-rest-block-patterns-controller.php | 46 ++++++++++++++++++- wp-includes/version.php | 2 +- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php index cb0fe57fed..adc4a0aade 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php @@ -24,6 +24,18 @@ class WP_REST_Block_Patterns_Controller extends WP_REST_Controller { */ private $remote_patterns_loaded; + /** + * An array that maps old categories names to new ones. + * + * @since 6.2.0 + * @var array + */ + protected static $categories_migration = array( + 'buttons' => 'call-to-action', + 'columns' => 'text', + 'query' => 'posts', + ); + /** * Constructs the controller. * @@ -84,6 +96,7 @@ class WP_REST_Block_Patterns_Controller extends WP_REST_Controller { * Retrieves all block patterns. * * @since 6.0.0 + * @since 6.2.0 Added migration for old core pattern categories to the new ones. * * @param WP_REST_Request $request Full details about the request. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. @@ -101,12 +114,43 @@ class WP_REST_Block_Patterns_Controller extends WP_REST_Controller { $response = array(); $patterns = WP_Block_Patterns_Registry::get_instance()->get_all_registered(); foreach ( $patterns as $pattern ) { - $prepared_pattern = $this->prepare_item_for_response( $pattern, $request ); + $migrated_pattern = $this->migrate_pattern_categories( $pattern ); + $prepared_pattern = $this->prepare_item_for_response( $migrated_pattern, $request ); $response[] = $this->prepare_response_for_collection( $prepared_pattern ); } return rest_ensure_response( $response ); } + /** + * Migrates old core pattern categories to the new categories. + * + * Core pattern categories are revamped. Migration is needed to ensure + * backwards compatibility. + * + * @since 6.2.0 + * + * @param array $pattern Raw pattern as registered, before applying any changes. + * @return array Migrated pattern. + */ + protected function migrate_pattern_categories( $pattern ) { + // No categories to migrate. + if ( + ! isset( $pattern['categories'] ) || + ! is_array( $pattern['categories'] ) + ) { + return $pattern; + } + + foreach ( $pattern['categories'] as $index => $category ) { + // If the category exists as a key, then it needs migration. + if ( isset( static::$categories_migration[ $category ] ) ) { + $pattern['categories'][ $index ] = static::$categories_migration[ $category ]; + } + } + + return $pattern; + } + /** * Prepare a raw block pattern before it gets output in a REST API response. * diff --git a/wp-includes/version.php b/wp-includes/version.php index 67c9a7b03d..740fdaaf25 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.2-alpha-55124'; +$wp_version = '6.2-alpha-55125'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.