-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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. #45814
Add: Template types to the patterns API. #45814
Conversation
Open in CodeSandbox Web Editor | VS Code | VS Code Insiders |
11e24ec
to
c529b8e
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a unit test
035614b
to
9db5c24
Compare
137a626
to
c36c29b
Compare
Hi @spacedmonkey, thank you for the review 👍 The unit tests were updated to take into account template types. |
'type' => 'array', | ||
'readonly' => true, | ||
'context' => array( 'view', 'edit', 'embed' ), | ||
), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does it mean to have both block types and template types for instance? Is this even possible? I feel like we need some kind of documentation about all these properties.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are two distinct things. blocktTypes specifies blocks where the pattern makes sense, e.g: group, templateTypes specifies templates where the pattern makes sense. I think it is ok to have both and may make sense, e.g.: a 404 pattern that is a group block wrapping some 404 content may have group block type and 404 template, it should work well I don't see a reason for it not to work.
My plan is to add the same documentation the other properties have e.g: here docs/reference-guides/block-api/block-patterns.md, and a follow-up core PR similar to this one WordPress/wordpress-develop#2858.
c36c29b
to
536c39b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, this looks good to me
5cded5d
to
463e399
Compare
463e399
to
77b0ae2
Compare
Backports WordPress/gutenberg#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
Backports WordPress/gutenberg#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. Built from https://develop.svn.wordpress.org/trunk@55168 git-svn-id: http://core.svn.wordpress.org/trunk@54701 1a063a9b-81f0-0310-95a4-ce76da25c4cd
Backports WordPress/gutenberg#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. Built from https://develop.svn.wordpress.org/trunk@55168 git-svn-id: https://core.svn.wordpress.org/trunk@54701 1a063a9b-81f0-0310-95a4-ce76da25c4cd
I'm adding some additional testing info here, as this enhancement has been highlighted in the WordPress 6.2 Call for Testing post (also see r55168). Testing InstructionsSteps to Test
Supplemental ArtifactsFigure 1: TT1 Test Code Snippetregister_block_pattern(
'query/template-type-test',
array(
'title' => __( 'Template type test', 'twentytwentyone' ),
'templateTypes' => array( '404' ),
'content' => '<!-- wp:paragraph {"align":"center","fontSize":"x-large"} -->
<p class="has-text-align-center has-x-large-font-size">404</p>
<!-- /wp:paragraph -->',
)
); Figure 2: API response output in dev tools console |
Dev note: IntroductionThe Patterns API is a powerful feature in WordPress that allows developers to create pre-designed blocks of content that can be easily inserted into posts, pages, custom post types, and templates. With the introduction of WordPress 6.2, the Patterns API has been expanded to include a template types property. The property allows pattern developers to specify which templates their patterns fit in. For example, a pattern can declare its purpose is to be used on 404 templates or as author templates, etc. Any template in the WordPress hierarchy is supported. Right now template_types property has no impact on the behavior and UX of WordPress. But using the API provides WordPress core information on the relationship between a pattern and a template. This information could be used for multiple purposes. The first usage in mind is to show the user some patterns that make sense on a template when the user starts creating a template. Users can start from a pattern instead of "blank" or the fallback template. Technical detailsThe Patterns API's register_block_pattern() function was updated to include a new template_types parameter. This parameter is an array of strings containing the names of the templates the template is intended for, e.g., 404, author, etc. The parameter template_types is optional, and using it is not mandatory to register a pattern. The REST API was also extended to return the template types of a pattern in case there is one defined. Testing
Figure 1: TT1 Test Code Snippetregister_block_pattern(
'query/template-type-test',
array(
'title' => __( 'Template type test', 'twentytwentyone' ),
'templateTypes' => array( '404' ),
'content' => '<!-- wp:paragraph {"align":"center","fontSize":"x-large"} -->
<p class="has-text-align-center has-x-large-font-size">404</p>
<!-- /wp:paragraph -->',
)
); Figure 2: API response output in dev tools consoleProps to @ironprogrammer for writing these detailed instructions. |
Backports WordPress/gutenberg#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. Built from https://develop.svn.wordpress.org/trunk@55168 git-svn-id: http://core.svn.wordpress.org/trunk@54701 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This PR 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.
This is required by #42325 in order to when creating a template show all the relevant patterns that exist for it.
It may also allow users on the pattern directory to explore patterns by the template where they are supposed to be used.
Testing
I pasted the following sample pattern on lib/compat/wordpress-6.2/block-patterns.php.
I opened the post editor and the developer tools and pasted
wp.apiFetch( { path: '/wp/v2/block-patterns/patterns' } ).then( console.log );
on the browser console.I verified that the API response for included query/template-type-test include 404 in the templateTypes.