Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: Template types to the patterns API. #3946

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
Copy link
Member

@mukeshpanchal27 mukeshpanchal27 Jan 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

);

/*
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' ),
jorgefilipecosta marked this conversation as resolved.
Show resolved Hide resolved
'type' => 'array',
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
),
'content' => array(
'description' => __( 'The pattern content.' ),
'type' => 'string',
Expand Down
20 changes: 12 additions & 8 deletions tests/phpunit/tests/rest-api/wpRestBlockPatternsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,17 @@ 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' ),
)
);

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