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 Dec 1, 2022
1 parent d80dd87 commit 137a626
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 16 deletions.
1 change: 1 addition & 0 deletions docs/reference-guides/block-api/block-patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ The properties available for block patterns are:
- `viewportWidth` (optional): An integer specifying the intended width of the pattern to allow for a scaled preview of the pattern in the inserter.
- `blockTypes` (optional): An array of block types that the pattern is intended to be used with. Each value needs to be the declared block's `name`.
- `postTypes` (optional): An array of post types that the pattern is restricted to be used with. The pattern will only be available when editing one of the post types passed on the array, for all the other post types the pattern is not available at all.
- `templateTypes` (optional): An array of template types where the pattern makes sense e.g: '404' if the pattern is for a 404 page, single-post if the pattern is for showing a single post.
- `inserter` (optional): By default, all patterns will appear in the inserter. To hide a pattern so that it can only be inserted programmatically, set the `inserter` to `false`.

The following code sample registers a block pattern named 'my-plugin/my-awesome-pattern':
Expand Down
9 changes: 0 additions & 9 deletions lib/compat/wordpress-6.1/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,6 @@ function gutenberg_update_post_types_rest_response( $response, $post_type ) {
}
add_filter( 'rest_prepare_post_type', 'gutenberg_update_post_types_rest_response', 10, 2 );

/**
* Registers the block patterns REST API routes.
*/
function gutenberg_register_gutenberg_rest_block_patterns() {
$block_patterns = new Gutenberg_REST_Block_Patterns_Controller();
$block_patterns->register_routes();
}
add_action( 'rest_api_init', 'gutenberg_register_gutenberg_rest_block_patterns', 100 );

/**
* Exposes the site logo URL through the WordPress REST API.
*
Expand Down
3 changes: 2 additions & 1 deletion lib/compat/wordpress-6.2/block-patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ function gutenberg_register_theme_block_patterns() {
'blockTypes' => 'Block Types',
'postTypes' => 'Post Types',
'inserter' => 'Inserter',
'templateTypes' => 'Template Types',
);

/*
Expand Down Expand Up @@ -228,7 +229,7 @@ function gutenberg_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 @@ -129,6 +129,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 @@ -204,6 +205,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.', 'gutenberg' ),
'type' => 'string',
Expand Down
9 changes: 9 additions & 0 deletions lib/compat/wordpress-6.2/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,12 @@ function gutenberg_pattern_directory_collection_params_6_2( $query_params ) {
return $query_params;
}
add_filter( 'rest_pattern_directory_collection_params', 'gutenberg_pattern_directory_collection_params_6_2' );

/**
* Registers the block patterns REST API routes.
*/
function gutenberg_register_gutenberg_rest_block_patterns() {
$block_patterns = new Gutenberg_REST_Block_Patterns_Controller();
$block_patterns->register_routes();
}
add_action( 'rest_api_init', 'gutenberg_register_gutenberg_rest_block_patterns', 100 );
2 changes: 1 addition & 1 deletion lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ function gutenberg_is_experiment_enabled( $name ) {
require_once __DIR__ . '/compat/wordpress-6.0/rest-api.php';

// WordPress 6.1 compat.
require_once __DIR__ . '/compat/wordpress-6.1/class-gutenberg-rest-block-patterns-controller.php';
require_once __DIR__ . '/compat/wordpress-6.1/class-gutenberg-rest-templates-controller.php';
require_once __DIR__ . '/compat/wordpress-6.1/rest-api.php';

// WordPress 6.2 compat.
require_once __DIR__ . '/compat/wordpress-6.2/class-gutenberg-rest-block-patterns-controller.php';
require_once __DIR__ . '/compat/wordpress-6.2/class-gutenberg-rest-block-pattern-categories-controller.php';
require_once __DIR__ . '/compat/wordpress-6.2/class-gutenberg-rest-pattern-directory-controller-6-2.php';
require_once __DIR__ . '/compat/wordpress-6.2/rest-api.php';
Expand Down
12 changes: 7 additions & 5 deletions phpunit/class-gutenberg-rest-block-patterns-controller-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,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 All @@ -75,10 +77,10 @@ public function test_get_items() {
wp_set_current_user( self::$admin_id );

$expected_names = array( 'test/one', 'test/two' );
$expected_fields = array( 'name', 'content' );
$expected_fields = array( 'name', 'content', 'templateTypes' );

$request = new WP_REST_Request( 'GET', '/wp/v2/block-patterns/patterns' );
$request['_fields'] = 'name,content';
$request['_fields'] = 'name,content,templateTypes';
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();

Expand Down

0 comments on commit 137a626

Please sign in to comment.