Make WordPress Core

Changeset 56063


Ignore:
Timestamp:
06/27/2023 11:11:02 AM (15 months ago)
Author:
isabel_brison
Message:

Editor: allow filtering block patterns by source.

Extends the REST API endpoint for block patterns to provide a source attribute that can be used to filter patterns.

Props aaronrobertshaw, mukesh27, talldanwp, ramonopoly.
Fixes 58622.

Location:
trunk
Files:
3 edited

Legend:

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

    r55983 r56063  
    1313 *
    1414 * @since 5.5.0
     15 * @since 6.3.0 Added source to core block patterns.
    1516 * @access private
    1617 */
     
    3031
    3132        foreach ( $core_block_patterns as $core_block_pattern ) {
    32             register_block_pattern(
    33                 'core/' . $core_block_pattern,
    34                 require __DIR__ . '/block-patterns/' . $core_block_pattern . '.php'
    35             );
     33            $pattern           = require __DIR__ . '/block-patterns/' . $core_block_pattern . '.php';
     34            $pattern['source'] = 'core';
     35            register_block_pattern( 'core/' . $core_block_pattern, $pattern );
    3636        }
    3737    }
     
    191191 * @since 6.2.0 Normalize the pattern from the API (snake_case) to the
    192192 *              format expected by `register_block_pattern` (camelCase).
     193 * @since 6.3.0 Add 'pattern-directory/core' to the pattern's 'source'.
    193194 *
    194195 * @param WP_Screen $deprecated Unused. Formerly the screen that the current request was triggered from.
     
    225226
    226227        foreach ( $patterns as $pattern ) {
     228            $pattern['source']  = 'pattern-directory/core';
    227229            $normalized_pattern = wp_normalize_remote_block_pattern( $pattern );
    228230            $pattern_name       = 'core/' . sanitize_title( $normalized_pattern['title'] );
     
    238240 * @since 6.2.0 Normalized the pattern from the API (snake_case) to the
    239241 *              format expected by `register_block_pattern()` (camelCase).
     242 * @since 6.3.0 Add 'pattern-directory/featured' to the pattern's 'source'.
    240243 */
    241244function _load_remote_featured_patterns() {
     
    259262    $registry = WP_Block_Patterns_Registry::get_instance();
    260263    foreach ( $patterns as $pattern ) {
     264        $pattern['source']  = 'pattern-directory/featured';
    261265        $normalized_pattern = wp_normalize_remote_block_pattern( $pattern );
    262266        $pattern_name       = sanitize_title( $normalized_pattern['title'] );
     
    276280 * @since 6.2.0 Normalized the pattern from the API (snake_case) to the
    277281 *              format expected by `register_block_pattern()` (camelCase).
     282 * @since 6.3.0 Add 'pattern-directory/theme' to the pattern's 'source'.
    278283 * @access private
    279284 */
     
    302307    $patterns_registry = WP_Block_Patterns_Registry::get_instance();
    303308    foreach ( $patterns as $pattern ) {
     309        $pattern['source']  = 'pattern-directory/theme';
    304310        $normalized_pattern = wp_normalize_remote_block_pattern( $pattern );
    305311        $pattern_name       = sanitize_title( $normalized_pattern['title'] );
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php

    r55173 r56063  
    156156     *
    157157     * @since 6.0.0
     158     * @since 6.3.0 Added `source` property.
    158159     *
    159160     * @param array           $item    Raw pattern as registered, before any changes.
     
    175176            'postTypes'     => 'post_types',
    176177            'templateTypes' => 'template_types',
     178            'source'        => 'source',
    177179        );
    178180        $data   = array();
     
    193195     *
    194196     * @since 6.0.0
     197     * @since 6.3.0 Added `source` property.
    195198     *
    196199     * @return array Item schema data.
     
    268271                    'context'     => array( 'view', 'edit', 'embed' ),
    269272                ),
     273                'source'         => array(
     274                    'description' => __( 'Where the pattern comes from e.g. core' ),
     275                    'type'        => 'string',
     276                    'readonly'    => true,
     277                    'context'     => array( 'view', 'edit', 'embed' ),
     278                    'enum'        => array(
     279                        'core',
     280                        'plugin',
     281                        'theme',
     282                        'pattern-directory/core',
     283                        'pattern-directory/theme',
     284                        'pattern-directory/featured',
     285                    ),
     286                ),
    270287            ),
    271288        );
  • trunk/tests/phpunit/tests/rest-api/wpRestBlockPatternsController.php

    r55457 r56063  
    7777                'categories'    => array( 'test' ),
    7878                'templateTypes' => array( 'page' ),
     79                'source'        => 'theme',
    7980            )
    8081        );
     
    8788                'categories'    => array( 'test' ),
    8889                'templateTypes' => array( 'single' ),
     90                'source'        => 'core',
    8991            )
    9092        );
     
    9698                'content'    => '<!-- wp:paragraph --><p>Three</p><!-- /wp:paragraph -->',
    9799                'categories' => array( 'test', 'buttons', 'query' ),
     100                'source'     => 'pattern-directory/featured',
    98101            )
    99102        );
     
    125128
    126129        $request            = new WP_REST_Request( 'GET', static::REQUEST_ROUTE );
    127         $request['_fields'] = 'name,content,template_types';
     130        $request['_fields'] = 'name,content,source,template_types';
    128131        $response           = rest_get_server()->dispatch( $request );
    129132        $data               = $response->get_data();
     
    136139                'content'        => '<!-- wp:heading {"level":1} --><h1>One</h1><!-- /wp:heading -->',
    137140                'template_types' => array( 'page' ),
     141                'source'         => 'theme',
    138142            ),
    139143            $data[0],
     
    145149                'content'        => '<!-- wp:paragraph --><p>Two</p><!-- /wp:paragraph -->',
    146150                'template_types' => array( 'single' ),
     151                'source'         => 'core',
    147152            ),
    148153            $data[1],
Note: See TracChangeset for help on using the changeset viewer.