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.

git-svn-id: https://develop.svn.wordpress.org/trunk@55168 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jorge Costa
2023-01-31 16:47:58 +00:00
parent aaf0a3366c
commit 620760e1c1
3 changed files with 22 additions and 9 deletions

View File

@@ -302,6 +302,7 @@ function _register_remote_theme_patterns() {
* - Keywords (comma-separated values)
* - Block Types (comma-separated values)
* - Post Types (comma-separated values)
* - Template Types (comma-separated values)
* - Inserter (yes/no)
*
* @since 6.0.0
@@ -318,6 +319,7 @@ function _register_theme_block_patterns() {
'blockTypes' => 'Block Types',
'postTypes' => 'Post Types',
'inserter' => 'Inserter',
'templateTypes' => 'Template Types',
);
/*
@@ -388,7 +390,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(

View File

@@ -173,6 +173,7 @@ class WP_REST_Block_Patterns_Controller extends WP_REST_Controller {
'keywords' => 'keywords',
'content' => 'content',
'inserter' => 'inserter',
'templateTypes' => 'template_types',
);
$data = array();
foreach ( $keys as $item_key => $rest_key ) {
@@ -248,6 +249,12 @@ class WP_REST_Block_Patterns_Controller extends WP_REST_Controller {
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
),
'template_types' => array(
'description' => __( 'An array of template types where the pattern fits.' ),
'type' => 'array',
'readonly' => true,
'context' => array( 'view', 'edit', 'embed' ),
),
'content' => array(
'description' => __( 'The pattern content.' ),
'type' => 'string',

View File

@@ -81,15 +81,17 @@ class Tests_REST_WpRestBlockPatternsController extends WP_Test_REST_Controller_T
'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' ),
)
);
@@ -128,7 +130,7 @@ class Tests_REST_WpRestBlockPatternsController extends WP_Test_REST_Controller_T
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();
@@ -136,16 +138,18 @@ class Tests_REST_WpRestBlockPatternsController extends WP_Test_REST_Controller_T
$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'