Skip to content

Commit

Permalink
Add: Template types to the patterns API.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Jan 30, 2023
1 parent b74cfa8 commit ca8fcae
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/wp-includes/block-patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ function _register_theme_block_patterns() {
'blockTypes' => 'Block Types',
'postTypes' => 'Post Types',
'inserter' => 'Inserter',
'templateTypes' => 'Template Types',
);

/*
Expand Down Expand Up @@ -388,7 +389,7 @@ function _register_theme_block_patterns() {
}

// For properties of type array, parse data as comma-separated.
foreach ( array( 'categories', 'keywords', 'blockTypes', 'postTypes' ) as $property ) {
foreach ( array( 'categories', 'keywords', 'blockTypes', 'postTypes', 'templateTypes' ) as $property ) {
if ( ! empty( $pattern_data[ $property ] ) ) {
$pattern_data[ $property ] = array_filter(
preg_split(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ public function prepare_item_for_response( $item, $request ) {
'keywords' => 'keywords',
'content' => 'content',
'inserter' => 'inserter',
'templateTypes' => 'template_types',
);
$data = array();
foreach ( $keys as $item_key => $rest_key ) {
Expand Down Expand Up @@ -248,6 +249,12 @@ public function get_item_schema() {
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
),
'template_types' => array(
'description' => __( 'An array of template types where the pattern fits.', 'gutenberg' ),
'type' => 'array',
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
),
'content' => array(
'description' => __( 'The pattern content.' ),
'type' => 'string',
Expand Down
26 changes: 15 additions & 11 deletions tests/phpunit/tests/rest-api/wpRestBlockPatternsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,26 @@ public static function wpSetUpBeforeClass( $factory ) {
'categories' => array( 'test' ),
'viewportWidth' => 1440,
'content' => '<!-- wp:heading {"level":1} --><h1>One</h1><!-- /wp:heading -->',
'templateTypes' => array( 'page' ),
)
);

$test_registry->register(
'test/two',
array(
'title' => 'Pattern Two',
'categories' => array( 'test' ),
'content' => '<!-- wp:paragraph --><p>Two</p><!-- /wp:paragraph -->',
'title' => 'Pattern Two',
'categories' => array( 'test' ),
'content' => '<!-- wp:paragraph --><p>Two</p><!-- /wp:paragraph -->',
'templateTypes' => array( 'single' ),
)
);

$test_registry->register(
'test/three',
array(
'title' => 'Pattern Three',
'categories' => array( 'test', 'buttons', 'query' ),
'content' => '<!-- wp:paragraph --><p>Three</p><!-- /wp:paragraph -->',
'title' => 'Pattern Three',
'categories' => array( 'test', 'buttons', 'query' ),
'content' => '<!-- wp:paragraph --><p>Three</p><!-- /wp:paragraph -->',
)
);
}
Expand Down Expand Up @@ -128,24 +130,26 @@ public function test_get_items() {
wp_set_current_user( self::$admin_id );

$request = new WP_REST_Request( 'GET', static::REQUEST_ROUTE );
$request['_fields'] = 'name,content';
$request['_fields'] = 'name,content,template_types';
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();

$this->assertIsArray( $data, 'WP_REST_Block_Patterns_Controller::get_items() should return an array' );
$this->assertGreaterThanOrEqual( 2, count( $data ), 'WP_REST_Block_Patterns_Controller::get_items() should return at least 2 items' );
$this->assertSame(
array(
'name' => 'test/one',
'content' => '<!-- wp:heading {"level":1} --><h1>One</h1><!-- /wp:heading -->',
'name' => 'test/one',
'content' => '<!-- wp:heading {"level":1} --><h1>One</h1><!-- /wp:heading -->',
'template_types' => array( 'page' ),
),
$data[0],
'WP_REST_Block_Patterns_Controller::get_items() should return test/one'
);
$this->assertSame(
array(
'name' => 'test/two',
'content' => '<!-- wp:paragraph --><p>Two</p><!-- /wp:paragraph -->',
'name' => 'test/two',
'content' => '<!-- wp:paragraph --><p>Two</p><!-- /wp:paragraph -->',
'template_types' => array( 'single' ),
),
$data[1],
'WP_REST_Block_Patterns_Controller::get_items() should return test/two'
Expand Down

0 comments on commit ca8fcae

Please sign in to comment.