Make WordPress Core


Ignore:
Timestamp:
01/24/2023 02:34:10 PM (2 years ago)
Author:
hellofromTonya
Message:

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:

Follow-up to [55098], [53152].

Props ntsekouras, annezazu, jameskoster, joen, hellofromTonya, mcsf, paaljoachim, ryelle.
Fixes #57532.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php

    r54356 r55125  
    2424     */
    2525    private $remote_patterns_loaded;
     26
     27    /**
     28     * An array that maps old categories names to new ones.
     29     *
     30     * @since 6.2.0
     31     * @var array
     32     */
     33    protected static $categories_migration = array(
     34        'buttons' => 'call-to-action',
     35        'columns' => 'text',
     36        'query'   => 'posts',
     37    );
    2638
    2739    /**
     
    8597     *
    8698     * @since 6.0.0
     99     * @since 6.2.0 Added migration for old core pattern categories to the new ones.
    87100     *
    88101     * @param WP_REST_Request $request Full details about the request.
     
    102115        $patterns = WP_Block_Patterns_Registry::get_instance()->get_all_registered();
    103116        foreach ( $patterns as $pattern ) {
    104             $prepared_pattern = $this->prepare_item_for_response( $pattern, $request );
     117            $migrated_pattern = $this->migrate_pattern_categories( $pattern );
     118            $prepared_pattern = $this->prepare_item_for_response( $migrated_pattern, $request );
    105119            $response[]       = $this->prepare_response_for_collection( $prepared_pattern );
    106120        }
    107121        return rest_ensure_response( $response );
     122    }
     123
     124    /**
     125     * Migrates old core pattern categories to the new categories.
     126     *
     127     * Core pattern categories are revamped. Migration is needed to ensure
     128     * backwards compatibility.
     129     *
     130     * @since 6.2.0
     131     *
     132     * @param array $pattern Raw pattern as registered, before applying any changes.
     133     * @return array Migrated pattern.
     134     */
     135    protected function migrate_pattern_categories( $pattern ) {
     136        // No categories to migrate.
     137        if (
     138            ! isset( $pattern['categories'] ) ||
     139            ! is_array( $pattern['categories'] )
     140        ) {
     141            return $pattern;
     142        }
     143
     144        foreach ( $pattern['categories'] as $index => $category ) {
     145            // If the category exists as a key, then it needs migration.
     146            if ( isset( static::$categories_migration[ $category ] ) ) {
     147                $pattern['categories'][ $index ] = static::$categories_migration[ $category ];
     148            }
     149        }
     150
     151        return $pattern;
    108152    }
    109153
Note: See TracChangeset for help on using the changeset viewer.