Changeset 55234
- Timestamp:
- 02/06/2023 07:38:08 PM (2 years ago)
- Location:
- trunk
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/block-patterns.php
r55173 r55234 160 160 161 161 /** 162 * Normalize the pattern properties to camelCase. 163 * 164 * The API's format is snake_case, `register_block_pattern()` expects camelCase. 165 * 166 * @since 6.2.0 167 * @access private 168 * 169 * @param array $pattern Pattern as returned from the Pattern Directory API. 170 * @return array Normalized pattern. 171 */ 172 function wp_normalize_remote_block_pattern( $pattern ) { 173 if ( isset( $pattern['block_types'] ) ) { 174 $pattern['blockTypes'] = $pattern['block_types']; 175 unset( $pattern['block_types'] ); 176 } 177 178 if ( isset( $pattern['viewport_width'] ) ) { 179 $pattern['viewportWidth'] = $pattern['viewport_width']; 180 unset( $pattern['viewport_width'] ); 181 } 182 183 return (array) $pattern; 184 } 185 186 /** 162 187 * Register Core's official patterns from wordpress.org/patterns. 163 188 * 164 189 * @since 5.8.0 165 190 * @since 5.9.0 The $current_screen argument was removed. 191 * @since 6.2.0 Normalize the pattern from the API (snake_case) to the 192 * format expected by `register_block_pattern` (camelCase). 166 193 * 167 194 * @param WP_Screen $deprecated Unused. Formerly the screen that the current request was triggered from. … … 197 224 $patterns = $response->get_data(); 198 225 199 foreach ( $patterns as $settings ) { 200 $pattern_name = 'core/' . sanitize_title( $settings['title'] ); 201 register_block_pattern( $pattern_name, (array) $settings ); 226 foreach ( $patterns as $pattern ) { 227 $normalized_pattern = wp_normalize_remote_block_pattern( $pattern ); 228 $pattern_name = 'core/' . sanitize_title( $normalized_pattern['title'] ); 229 register_block_pattern( $pattern_name, $normalized_pattern ); 202 230 } 203 231 } … … 208 236 * 209 237 * @since 5.9.0 238 * @since 6.2.0 Normalized the pattern from the API (snake_case) to the 239 * format expected by `register_block_pattern()` (camelCase). 210 240 */ 211 241 function _load_remote_featured_patterns() { … … 227 257 } 228 258 $patterns = $response->get_data(); 229 259 $registry = WP_Block_Patterns_Registry::get_instance(); 230 260 foreach ( $patterns as $pattern ) { 231 $ pattern_name = sanitize_title( $pattern['title']);232 $ registry = WP_Block_Patterns_Registry::get_instance();261 $normalized_pattern = wp_normalize_remote_block_pattern( $pattern ); 262 $pattern_name = sanitize_title( $normalized_pattern['title'] ); 233 263 // Some patterns might be already registered as core patterns with the `core` prefix. 234 264 $is_registered = $registry->is_registered( $pattern_name ) || $registry->is_registered( "core/$pattern_name" ); 235 265 if ( ! $is_registered ) { 236 register_block_pattern( $pattern_name, (array) $pattern );266 register_block_pattern( $pattern_name, $normalized_pattern ); 237 267 } 238 268 } … … 244 274 * 245 275 * @since 6.0.0 276 * @since 6.2.0 Normalized the pattern from the API (snake_case) to the 277 * format expected by `register_block_pattern()` (camelCase). 246 278 * @access private 247 279 */ … … 270 302 $patterns_registry = WP_Block_Patterns_Registry::get_instance(); 271 303 foreach ( $patterns as $pattern ) { 272 $pattern_name = sanitize_title( $pattern['title'] ); 304 $normalized_pattern = wp_normalize_remote_block_pattern( $pattern ); 305 $pattern_name = sanitize_title( $normalized_pattern['title'] ); 273 306 // Some patterns might be already registered as core patterns with the `core` prefix. 274 307 $is_registered = $patterns_registry->is_registered( $pattern_name ) || $patterns_registry->is_registered( "core/$pattern_name" ); 275 308 if ( ! $is_registered ) { 276 register_block_pattern( $pattern_name, (array) $pattern );309 register_block_pattern( $pattern_name, $normalized_pattern ); 277 310 } 278 311 } -
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php
r55132 r55234 104 104 'slug' => true, 105 105 ); 106 $query_args = array_intersect_key( $request->get_params(), $valid_query_args );106 $query_args = array_intersect_key( $request->get_params(), $valid_query_args ); 107 107 108 108 $query_args['locale'] = get_user_locale(); … … 203 203 'description' => sanitize_text_field( $raw_pattern->meta->wpop_description ), 204 204 'viewport_width' => absint( $raw_pattern->meta->wpop_viewport_width ), 205 'block_types' => array_map( 'sanitize_text_field', $raw_pattern->meta->wpop_block_types ), 205 206 ); 206 207 … … 225 226 * 226 227 * @since 5.8.0 228 * @since 6.2.0 Added `'block_types'` to schema. 227 229 * 228 230 * @return array Item schema data. … … 286 288 'type' => 'integer', 287 289 'context' => array( 'view', 'edit', 'embed' ), 290 ), 291 292 'block_types' => array( 293 'description' => __( 'The block types which can use this pattern.' ), 294 'type' => 'array', 295 'uniqueItems' => true, 296 'items' => array( 'type' => 'string' ), 297 'context' => array( 'view', 'embed' ), 288 298 ), 289 299 ), -
trunk/tests/phpunit/data/blocks/pattern-directory/browse-all.json
r53665 r55234 11 11 "wpop_description": "A heading preceded by a chapter number, and followed by a paragraph.", 12 12 "wpop_keywords": "blog post", 13 "wpop_viewport_width": 1000 13 "wpop_viewport_width": 1000, 14 "wpop_block_types": [ "core/heading" ] 14 15 }, 15 16 "category_slugs": [ "text" ], … … 28 29 "wpop_description": "A large hero section with an example background image and a heading in the center.", 29 30 "wpop_keywords": "header, hero", 30 "wpop_viewport_width": 1000 31 "wpop_viewport_width": 1000, 32 "wpop_block_types": [] 31 33 }, 32 34 "category_slugs": [ "header" ], … … 45 47 "wpop_description": "A large hero section with a bright gradient background, a big heading and a filled button.", 46 48 "wpop_keywords": "call to action, hero section", 47 "wpop_viewport_width": 1000 49 "wpop_viewport_width": 1000, 50 "wpop_block_types": [] 48 51 }, 49 52 "category_slugs": [ "header" ], -
trunk/tests/phpunit/data/blocks/pattern-directory/browse-category-2.json
r53665 r55234 11 11 "wpop_description": "Three filled buttons with rounded corners, side by side.", 12 12 "wpop_keywords": "", 13 "wpop_viewport_width": 600 13 "wpop_viewport_width": 600, 14 "wpop_block_types": [] 14 15 }, 15 16 "category_slugs": [ "buttons" ], … … 28 29 "wpop_description": "Two buttons, one filled and one outlined, side by side.", 29 30 "wpop_keywords": "", 30 "wpop_viewport_width": 500 31 "wpop_viewport_width": 500, 32 "wpop_block_types": [] 31 33 }, 32 34 "category_slugs": [ "buttons" ], -
trunk/tests/phpunit/data/blocks/pattern-directory/browse-keyword-11.json
r53665 r55234 11 11 "wpop_description": "A heading preceded by a chapter number, and followed by a paragraph.", 12 12 "wpop_keywords": "", 13 "wpop_viewport_width": 1000 13 "wpop_viewport_width": 1000, 14 "wpop_block_types": [] 14 15 }, 15 16 "category_slugs": [ "text" ], … … 28 29 "wpop_description": "A large hero section with an example background image and a heading in the center.", 29 30 "wpop_keywords": "", 30 "wpop_viewport_width": 1000 31 "wpop_viewport_width": 1000, 32 "wpop_block_types": [] 31 33 }, 32 34 "category_slugs": [ "header" ], … … 45 47 "wpop_description": "A large hero section with a bright gradient background, a big heading and a filled button.", 46 48 "wpop_keywords": "", 47 "wpop_viewport_width": 1000 49 "wpop_viewport_width": 1000, 50 "wpop_block_types": [] 48 51 }, 49 52 "category_slugs": [ "header" ], -
trunk/tests/phpunit/data/blocks/pattern-directory/search-button.json
r53665 r55234 11 11 "wpop_description": "A large hero section with a bright gradient background, a big heading and a filled button.", 12 12 "wpop_keywords": "", 13 "wpop_viewport_width": 1000 13 "wpop_viewport_width": 1000, 14 "wpop_block_types": [] 14 15 }, 15 16 "category_slugs": [ "header" ], … … 28 29 "wpop_description": "Three small columns of text, each with an outlined button with rounded corners at the bottom.", 29 30 "wpop_keywords": "", 30 "wpop_viewport_width": 1000 31 "wpop_viewport_width": 1000, 32 "wpop_block_types": [] 31 33 }, 32 34 "category_slugs": [ "columns" ], … … 45 47 "wpop_description": "Three filled buttons with rounded corners, side by side.", 46 48 "wpop_keywords": "", 47 "wpop_viewport_width": 600 49 "wpop_viewport_width": 600, 50 "wpop_block_types": [] 48 51 }, 49 52 "category_slugs": [ "buttons" ], … … 62 65 "wpop_description": "Two buttons, one filled and one outlined, side by side.", 63 66 "wpop_keywords": "", 64 "wpop_viewport_width": 500 67 "wpop_viewport_width": 500, 68 "wpop_block_types": [] 65 69 }, 66 70 "category_slugs": [ "buttons" ],
Note: See TracChangeset
for help on using the changeset viewer.