Make WordPress Core


Ignore:
Timestamp:
02/06/2023 07:38:08 PM (4 years ago)
Author:
flixos90
Message:

Editor: Support the block_types and viewport_width props for remote patterns fetched from Pattern Directory.

Props ntsekouras, ironprogrammer, hellofromtonya, flixos90.
Fixes #57611.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-patterns.php

    r55173 r55234  
    160160
    161161/**
     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 */
     172function 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/**
    162187 * Register Core's official patterns from wordpress.org/patterns.
    163188 *
    164189 * @since 5.8.0
    165190 * @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).
    166193 *
    167194 * @param WP_Screen $deprecated Unused. Formerly the screen that the current request was triggered from.
    … …  
    197224                $patterns = $response->get_data();
    198225
    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 );
    202230                }
    203231        }
    … …  
    208236 *
    209237 * @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).
    210240 */
    211241function _load_remote_featured_patterns() {
    … …  
    227257        }
    228258        $patterns = $response->get_data();
    229 
     259        $registry = WP_Block_Patterns_Registry::get_instance();
    230260        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'] );
    233263                // Some patterns might be already registered as core patterns with the `core` prefix.
    234264                $is_registered = $registry->is_registered( $pattern_name ) || $registry->is_registered( "core/$pattern_name" );
    235265                if ( ! $is_registered ) {
    236                         register_block_pattern( $pattern_name, (array) $pattern );
     266                        register_block_pattern( $pattern_name, $normalized_pattern );
    237267                }
    238268        }
    … …  
    244274 *
    245275 * @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).
    246278 * @access private
    247279 */
    … …  
    270302        $patterns_registry = WP_Block_Patterns_Registry::get_instance();
    271303        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'] );
    273306                // Some patterns might be already registered as core patterns with the `core` prefix.
    274307                $is_registered = $patterns_registry->is_registered( $pattern_name ) || $patterns_registry->is_registered( "core/$pattern_name" );
    275308                if ( ! $is_registered ) {
    276                         register_block_pattern( $pattern_name, (array) $pattern );
     309                        register_block_pattern( $pattern_name, $normalized_pattern );
    277310                }
    278311        }
Note: See TracChangeset for help on using the changeset viewer.