Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -84,6 +96,7 @@ public function get_items_permissions_check( $request ) {
* 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.
Expand All @@ -101,12 +114,43 @@ public function get_items( $request ) {
$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.
*
Expand Down
54 changes: 54 additions & 0 deletions tests/phpunit/tests/rest-api/wpRestBlockPatternsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ public static function wpSetUpBeforeClass( $factory ) {
'content' => '<!-- wp:paragraph --><p>Two</p><!-- /wp:paragraph -->',
)
);

$test_registry->register(
'test/three',
array(
'title' => 'Pattern Three',
'categories' => array( 'test', 'buttons', 'query' ),
'content' => '<!-- wp:paragraph --><p>Three</p><!-- /wp:paragraph -->',
)
);
}

public static function wpTearDownAfterClass() {
Expand Down Expand Up @@ -171,6 +180,51 @@ public function test_get_items_forbidden() {
$this->assertSame( 403, $response->get_status() );
}

/**
* Tests the proper migration of old core pattern categories to new ones.
*
* @since 6.2.0
*
* @ticket 57532
*
* @covers WP_REST_Block_Patterns_Controller::get_items
*/
public function test_get_items_migrate_pattern_categories() {
wp_set_current_user( self::$admin_id );

$request = new WP_REST_Request( 'GET', static::REQUEST_ROUTE );
$request['_fields'] = 'name,categories';
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();

$this->assertIsArray( $data, 'WP_REST_Block_Patterns_Controller::get_items() should return an array' );
$this->assertGreaterThanOrEqual( 3, count( $data ), 'WP_REST_Block_Patterns_Controller::get_items() should return at least 3 items' );
$this->assertSame(
array(
'name' => 'test/one',
'categories' => array( 'test' ),
),
$data[0],
'WP_REST_Block_Patterns_Controller::get_items() should return test/one'
);
$this->assertSame(
array(
'name' => 'test/two',
'categories' => array( 'test' ),
),
$data[1],
'WP_REST_Block_Patterns_Controller::get_items() should return test/two'
);
$this->assertSame(
array(
'name' => 'test/three',
'categories' => array( 'test', 'call-to-action', 'posts' ),
),
$data[2],
'WP_REST_Block_Patterns_Controller::get_items() should return test/three'
);
}

/**
* @doesNotPerformAssertions
*/
Expand Down