Make WordPress Core

Changeset 55168


Ignore:
Timestamp:
01/31/2023 04:47:58 PM (2 years ago)
Author:
jorgefilipecosta
Message:

Add: Template types to the patterns API.

Backports https://github.com/WordPress/gutenberg/pull/45814 into the core.
This commit adds a new templateType property to the patterns registration API.
This property allows a pattern to specify which template it makes sense on, e.g.: 404, single-post, single-product, category.

Props youknowriad, ntsekouras,
spacedmonkey.

Location:
trunk
Files:
3 edited

Legend:

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

    r55098 r55168  
    303303 *   - Block Types      (comma-separated values)
    304304 *   - Post Types       (comma-separated values)
     305 *   - Template Types   (comma-separated values)
    305306 *   - Inserter         (yes/no)
    306307 *
     
    319320        'postTypes'     => 'Post Types',
    320321        'inserter'      => 'Inserter',
     322        'templateTypes' => 'Template Types',
    321323    );
    322324
     
    389391
    390392                    // For properties of type array, parse data as comma-separated.
    391                     foreach ( array( 'categories', 'keywords', 'blockTypes', 'postTypes' ) as $property ) {
     393                    foreach ( array( 'categories', 'keywords', 'blockTypes', 'postTypes', 'templateTypes' ) as $property ) {
    392394                        if ( ! empty( $pattern_data[ $property ] ) ) {
    393395                            $pattern_data[ $property ] = array_filter(
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php

    r55125 r55168  
    174174            'content'       => 'content',
    175175            'inserter'      => 'inserter',
     176            'templateTypes' => 'template_types',
    176177        );
    177178        $data   = array();
     
    249250                    'context'     => array( 'view', 'edit', 'embed' ),
    250251                ),
     252                'template_types' => array(
     253                    'description' => __( 'An array of template types where the pattern fits.' ),
     254                    'type'        => 'array',
     255                    'readonly'    => true,
     256                    'context'     => array( 'view', 'edit', 'embed' ),
     257                ),
    251258                'content'        => array(
    252259                    'description' => __( 'The pattern content.' ),
  • trunk/tests/phpunit/tests/rest-api/wpRestBlockPatternsController.php

    r55125 r55168  
    8282                'viewportWidth' => 1440,
    8383                'content'       => '<!-- wp:heading {"level":1} --><h1>One</h1><!-- /wp:heading -->',
     84                'templateTypes' => array( 'page' ),
    8485            )
    8586        );
     
    8889            'test/two',
    8990            array(
    90                 'title'      => 'Pattern Two',
    91                 'categories' => array( 'test' ),
    92                 'content'    => '<!-- wp:paragraph --><p>Two</p><!-- /wp:paragraph -->',
     91                'title'         => 'Pattern Two',
     92                'categories'    => array( 'test' ),
     93                'content'       => '<!-- wp:paragraph --><p>Two</p><!-- /wp:paragraph -->',
     94                'templateTypes' => array( 'single' ),
    9395            )
    9496        );
     
    129131
    130132        $request            = new WP_REST_Request( 'GET', static::REQUEST_ROUTE );
    131         $request['_fields'] = 'name,content';
     133        $request['_fields'] = 'name,content,template_types';
    132134        $response           = rest_get_server()->dispatch( $request );
    133135        $data               = $response->get_data();
     
    137139        $this->assertSame(
    138140            array(
    139                 'name'    => 'test/one',
    140                 'content' => '<!-- wp:heading {"level":1} --><h1>One</h1><!-- /wp:heading -->',
     141                'name'           => 'test/one',
     142                'content'        => '<!-- wp:heading {"level":1} --><h1>One</h1><!-- /wp:heading -->',
     143                'template_types' => array( 'page' ),
    141144            ),
    142145            $data[0],
     
    145148        $this->assertSame(
    146149            array(
    147                 'name'    => 'test/two',
    148                 'content' => '<!-- wp:paragraph --><p>Two</p><!-- /wp:paragraph -->',
     150                'name'           => 'test/two',
     151                'content'        => '<!-- wp:paragraph --><p>Two</p><!-- /wp:paragraph -->',
     152                'template_types' => array( 'single' ),
    149153            ),
    150154            $data[1],
Note: See TracChangeset for help on using the changeset viewer.