Make WordPress Core

Changeset 55234


Ignore:
Timestamp:
02/06/2023 07:38:08 PM (17 months 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.

Location:
trunk
Files:
6 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    }
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php

    r55132 r55234  
    104104            'slug'     => true,
    105105        );
    106         $query_args = array_intersect_key( $request->get_params(), $valid_query_args );
     106        $query_args       = array_intersect_key( $request->get_params(), $valid_query_args );
    107107
    108108        $query_args['locale']             = get_user_locale();
     
    203203            'description'    => sanitize_text_field( $raw_pattern->meta->wpop_description ),
    204204            'viewport_width' => absint( $raw_pattern->meta->wpop_viewport_width ),
     205            'block_types'    => array_map( 'sanitize_text_field', $raw_pattern->meta->wpop_block_types ),
    205206        );
    206207
     
    225226     *
    226227     * @since 5.8.0
     228     * @since 6.2.0 Added `'block_types'` to schema.
    227229     *
    228230     * @return array Item schema data.
     
    286288                    'type'        => 'integer',
    287289                    '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' ),
    288298                ),
    289299            ),
  • trunk/tests/phpunit/data/blocks/pattern-directory/browse-all.json

    r53665 r55234  
    1111            "wpop_description": "A heading preceded by a chapter number, and followed by a paragraph.",
    1212            "wpop_keywords": "blog post",
    13             "wpop_viewport_width": 1000
     13            "wpop_viewport_width": 1000,
     14            "wpop_block_types": [ "core/heading" ]
    1415        },
    1516        "category_slugs": [ "text" ],
     
    2829            "wpop_description": "A large hero section with an example background image and a heading in the center.",
    2930            "wpop_keywords": "header, hero",
    30             "wpop_viewport_width": 1000
     31            "wpop_viewport_width": 1000,
     32            "wpop_block_types": []
    3133        },
    3234        "category_slugs": [ "header" ],
     
    4547            "wpop_description": "A large hero section with a bright gradient background, a big heading and a filled button.",
    4648            "wpop_keywords": "call to action, hero section",
    47             "wpop_viewport_width": 1000
     49            "wpop_viewport_width": 1000,
     50            "wpop_block_types": []
    4851        },
    4952        "category_slugs": [ "header" ],
  • trunk/tests/phpunit/data/blocks/pattern-directory/browse-category-2.json

    r53665 r55234  
    1111            "wpop_description": "Three filled buttons with rounded corners, side by side.",
    1212            "wpop_keywords": "",
    13             "wpop_viewport_width": 600
     13            "wpop_viewport_width": 600,
     14            "wpop_block_types": []
    1415        },
    1516        "category_slugs": [ "buttons" ],
     
    2829            "wpop_description": "Two buttons, one filled and one outlined, side by side.",
    2930            "wpop_keywords": "",
    30             "wpop_viewport_width": 500
     31            "wpop_viewport_width": 500,
     32            "wpop_block_types": []
    3133        },
    3234        "category_slugs": [ "buttons" ],
  • trunk/tests/phpunit/data/blocks/pattern-directory/browse-keyword-11.json

    r53665 r55234  
    1111            "wpop_description": "A heading preceded by a chapter number, and followed by a paragraph.",
    1212            "wpop_keywords": "",
    13             "wpop_viewport_width": 1000
     13            "wpop_viewport_width": 1000,
     14            "wpop_block_types": []
    1415        },
    1516        "category_slugs": [ "text" ],
     
    2829            "wpop_description": "A large hero section with an example background image and a heading in the center.",
    2930            "wpop_keywords": "",
    30             "wpop_viewport_width": 1000
     31            "wpop_viewport_width": 1000,
     32            "wpop_block_types": []
    3133        },
    3234        "category_slugs": [ "header" ],
     
    4547            "wpop_description": "A large hero section with a bright gradient background, a big heading and a filled button.",
    4648            "wpop_keywords": "",
    47             "wpop_viewport_width": 1000
     49            "wpop_viewport_width": 1000,
     50            "wpop_block_types": []
    4851        },
    4952        "category_slugs": [ "header" ],
  • trunk/tests/phpunit/data/blocks/pattern-directory/search-button.json

    r53665 r55234  
    1111            "wpop_description": "A large hero section with a bright gradient background, a big heading and a filled button.",
    1212            "wpop_keywords": "",
    13             "wpop_viewport_width": 1000
     13            "wpop_viewport_width": 1000,
     14            "wpop_block_types": []
    1415        },
    1516        "category_slugs": [ "header" ],
     
    2829            "wpop_description": "Three small columns of text, each with an outlined button with rounded corners at the bottom.",
    2930            "wpop_keywords": "",
    30             "wpop_viewport_width": 1000
     31            "wpop_viewport_width": 1000,
     32            "wpop_block_types": []
    3133        },
    3234        "category_slugs": [ "columns" ],
     
    4547            "wpop_description": "Three filled buttons with rounded corners, side by side.",
    4648            "wpop_keywords": "",
    47             "wpop_viewport_width": 600
     49            "wpop_viewport_width": 600,
     50            "wpop_block_types": []
    4851        },
    4952        "category_slugs": [ "buttons" ],
     
    6265            "wpop_description": "Two buttons, one filled and one outlined, side by side.",
    6366            "wpop_keywords": "",
    64             "wpop_viewport_width": 500
     67            "wpop_viewport_width": 500,
     68            "wpop_block_types": []
    6569        },
    6670        "category_slugs": [ "buttons" ],
Note: See TracChangeset for help on using the changeset viewer.