Make WordPress Core

Changeset 55125


Ignore:
Timestamp:
01/24/2023 02:34:10 PM (20 months 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.

Location:
trunk
Files:
2 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
  • trunk/tests/phpunit/tests/rest-api/wpRestBlockPatternsController.php

    r54088 r55125  
    9393            )
    9494        );
     95
     96        $test_registry->register(
     97            'test/three',
     98            array(
     99                'title'      => 'Pattern Three',
     100                'categories' => array( 'test', 'buttons', 'query' ),
     101                'content'    => '<!-- wp:paragraph --><p>Three</p><!-- /wp:paragraph -->',
     102            )
     103        );
    95104    }
    96105
     
    173182
    174183    /**
     184     * Tests the proper migration of old core pattern categories to new ones.
     185     *
     186     * @since 6.2.0
     187     *
     188     * @ticket 57532
     189     *
     190     * @covers WP_REST_Block_Patterns_Controller::get_items
     191     */
     192    public function test_get_items_migrate_pattern_categories() {
     193        wp_set_current_user( self::$admin_id );
     194
     195        $request            = new WP_REST_Request( 'GET', static::REQUEST_ROUTE );
     196        $request['_fields'] = 'name,categories';
     197        $response           = rest_get_server()->dispatch( $request );
     198        $data               = $response->get_data();
     199
     200        $this->assertIsArray( $data, 'WP_REST_Block_Patterns_Controller::get_items() should return an array' );
     201        $this->assertGreaterThanOrEqual( 3, count( $data ), 'WP_REST_Block_Patterns_Controller::get_items() should return at least 3 items' );
     202        $this->assertSame(
     203            array(
     204                'name'       => 'test/one',
     205                'categories' => array( 'test' ),
     206            ),
     207            $data[0],
     208            'WP_REST_Block_Patterns_Controller::get_items() should return test/one'
     209        );
     210        $this->assertSame(
     211            array(
     212                'name'       => 'test/two',
     213                'categories' => array( 'test' ),
     214            ),
     215            $data[1],
     216            'WP_REST_Block_Patterns_Controller::get_items() should return test/two'
     217        );
     218        $this->assertSame(
     219            array(
     220                'name'       => 'test/three',
     221                'categories' => array( 'test', 'call-to-action', 'posts' ),
     222            ),
     223            $data[2],
     224            'WP_REST_Block_Patterns_Controller::get_items() should return test/three'
     225        );
     226    }
     227
     228    /**
    175229     * @doesNotPerformAssertions
    176230     */
Note: See TracChangeset for help on using the changeset viewer.