Changeset 55125
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php
r54356 r55125 24 24 */ 25 25 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 ); 26 38 27 39 /** … … 85 97 * 86 98 * @since 6.0.0 99 * @since 6.2.0 Added migration for old core pattern categories to the new ones. 87 100 * 88 101 * @param WP_REST_Request $request Full details about the request. … … 102 115 $patterns = WP_Block_Patterns_Registry::get_instance()->get_all_registered(); 103 116 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 ); 105 119 $response[] = $this->prepare_response_for_collection( $prepared_pattern ); 106 120 } 107 121 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; 108 152 } 109 153 -
trunk/tests/phpunit/tests/rest-api/wpRestBlockPatternsController.php
r54088 r55125 93 93 ) 94 94 ); 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 ); 95 104 } 96 105 … … 173 182 174 183 /** 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 /** 175 229 * @doesNotPerformAssertions 176 230 */
Note: See TracChangeset
for help on using the changeset viewer.