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 plugin template registration API #7125

Closed
Closed
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
fb5b81b
Add plugin template registration API
Aljullu Aug 1, 2024
356f341
Rename 'templates' to 'block templates' in several APIs
Aljullu Aug 1, 2024
1cf666a
Simplify get_block_file_template() logic
Aljullu Aug 1, 2024
aa4ede2
Change default author text from theme slug to the plugin slug
Aljullu Aug 9, 2024
5b8af2f
Remove unnecessary usage of empty
Aljullu Aug 9, 2024
fe33355
DRY when validating registry input
Aljullu Aug 9, 2024
57bfe8e
Fix typo in error message
Aljullu Aug 9, 2024
01d62d7
Fix typo in variable name
Aljullu Aug 9, 2024
48351b4
Fix wrong register method signature
Aljullu Aug 9, 2024
b9962d7
Make plugin-registered templates overriden by themes to fall back to …
Aljullu Aug 19, 2024
cf747f8
Remove 'gutenberg' domain from translation strings
Aljullu Aug 28, 2024
b1f9ec2
Make variable as optional and clarify it's the plugin slug
Aljullu Aug 28, 2024
8188a0f
Add since tags to new functions
Aljullu Aug 28, 2024
b3e73e0
Clarify registering functions refer to block templates
Aljullu Aug 28, 2024
d843c49
Update src/wp-includes/block-template-utils.php
Aljullu Aug 28, 2024
b6b0311
Update src/wp-includes/rest-api/endpoints/class-wp-rest-templates-con…
Aljullu Sep 2, 2024
211ca86
Update src/wp-includes/class-wp-block-templates-registry.php
Aljullu Sep 2, 2024
f6a3dfa
Add docblocks to tests
Aljullu Sep 3, 2024
fa9e6ad
Move WP_Block_Templates_Registry_Test under block-templates
Aljullu Sep 3, 2024
e96f28d
Add property to WP_Template object
Aljullu Sep 9, 2024
32bb1e2
Fix wrong return type in register function
Aljullu Sep 9, 2024
56222e4
Update src/wp-includes/block-template-utils.php
Aljullu Sep 9, 2024
ae88141
Update src/wp-includes/class-wp-block-templates-registry.php
Aljullu Sep 9, 2024
06443ea
Update src/wp-includes/class-wp-block-templates-registry.php
Aljullu Sep 9, 2024
feaf88d
Remove duplicate args param
Aljullu Sep 9, 2024
0252d6f
Fix wrong return type in unregister function
Aljullu Sep 9, 2024
e9d8580
Remove unnecessary reassignment of ->origin
Aljullu Sep 9, 2024
5c8c19b
Merge branch 'trunk' into add/plugin-template-registration-api
peterwilsoncc Sep 10, 2024
cb65273
Rename test class.
peterwilsoncc Sep 10, 2024
6a6394d
Fix wrong object to get the slug from
Aljullu Sep 10, 2024
7d42b33
Update tests/phpunit/tests/block-templates/WpBlockTemplatesRegistry.php
Aljullu Sep 10, 2024
18dcff4
Fix wrong docblock comments
Aljullu Sep 10, 2024
09d39d2
Fix another wrong docblock comment
Aljullu Sep 10, 2024
e8b2b2c
A few test nitpicks.
peterwilsoncc Sep 13, 2024
bd7fc60
Merge branch 'trunk' into add/plugin-template-registration-api
peterwilsoncc Sep 13, 2024
9a18a25
Doc standards fixes.
peterwilsoncc Sep 14, 2024
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
60 changes: 49 additions & 11 deletions src/wp-includes/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,15 @@ function _build_block_template_result_from_file( $template_file, $template_type
$template->is_custom = true;
$template->modified = null;

if ( 'wp_template' === $template_type ) {
$registered_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $template_file['slug'] );
if ( $registered_template ) {
$template->plugin = $registered_template->plugin;
Aljullu marked this conversation as resolved.
Show resolved Hide resolved
$template->title = empty( $template->title ) || $template->title === $template->slug ? $registered_template->title : $template->title;
$template->description = empty( $template->description ) ? $registered_template->description : $template->description;
}
}

if ( 'wp_template' === $template_type && isset( $default_template_types[ $template_file['slug'] ] ) ) {
$template->description = $default_template_types[ $template_file['slug'] ]['description'];
$template->title = $default_template_types[ $template_file['slug'] ]['title'];
Expand Down Expand Up @@ -1014,6 +1023,19 @@ function _build_block_template_result_from_post( $post ) {
}
}

if ( 'wp_template' === $post->post_type ) {
$registered_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $post->slug );
Aljullu marked this conversation as resolved.
Show resolved Hide resolved
if ( $registered_template ) {
$template->plugin = $registered_template->plugin;
$template->origin =
'theme' !== $template->origin && 'theme' !== $template->source ?
'plugin' :
$template->origin;
$template->title = empty( $template->title ) || $template->title === $template->slug ? $registered_template->title : $template->title;
$template->description = empty( $template->description ) ? $registered_template->description : $template->description;
}
}

$hooked_blocks = get_hooked_blocks();
if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
$before_block_visitor = make_before_block_visitor( $hooked_blocks, $template, 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata' );
Expand Down Expand Up @@ -1157,6 +1179,23 @@ function get_block_templates( $query = array(), $template_type = 'wp_template' )
foreach ( $template_files as $template_file ) {
$query_result[] = _build_block_template_result_from_file( $template_file, $template_type );
}

if ( 'wp_template' === $template_type ) {
// Add templates registered in the template registry. Filtering out the ones which have a theme file.
$registered_templates = WP_Block_Templates_Registry::get_instance()->get_by_query( $query );
$matching_registered_templates = array_filter(
$registered_templates,
function ( $registered_template ) use ( $template_files ) {
foreach ( $template_files as $template_file ) {
if ( $template_file['slug'] === $registered_template->slug ) {
return false;
}
}
return true;
}
);
$query_result = array_merge( $query_result, $matching_registered_templates );
}
}

/**
Expand Down Expand Up @@ -1287,18 +1326,17 @@ function get_block_file_template( $id, $template_type = 'wp_template' ) {
}
list( $theme, $slug ) = $parts;

if ( get_stylesheet() !== $theme ) {
/** This filter is documented in wp-includes/block-template-utils.php */
return apply_filters( 'get_block_file_template', null, $id, $template_type );
}
if ( get_stylesheet() === $theme ) {
$template_file = _get_block_template_file( $template_type, $slug );
if ( null !== $template_file ) {
$block_template = _build_block_template_result_from_file( $template_file, $template_type );

$template_file = _get_block_template_file( $template_type, $slug );
if ( null === $template_file ) {
/** This filter is documented in wp-includes/block-template-utils.php */
return apply_filters( 'get_block_file_template', null, $id, $template_type );
/** This filter is documented in wp-includes/block-template-utils.php */
return apply_filters( 'get_block_file_template', $block_template, $id, $template_type );
}
}

$block_template = _build_block_template_result_from_file( $template_file, $template_type );
$block_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $slug );

/**
* Filters the block template object after it has been (potentially) fetched from the theme file.
Expand Down Expand Up @@ -1665,12 +1703,12 @@ function inject_ignored_hooked_blocks_metadata_attributes( $changes, $deprecated
);
}

$content = get_comment_delimited_block_content(
$content = get_comment_delimited_block_content(
'core/template-part',
$attributes,
$changes->post_content
);
$content = apply_block_hooks_to_content( $content, $template, 'set_ignored_hooked_blocks_metadata' );
$content = apply_block_hooks_to_content( $content, $template, 'set_ignored_hooked_blocks_metadata' );
$changes->post_content = remove_serialized_parent_block( $content );

$wrapper_block_markup = extract_serialized_parent_block( $content );
Expand Down
34 changes: 34 additions & 0 deletions src/wp-includes/block-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,37 @@ function _resolve_template_for_new_post( $wp_query ) {
$wp_query->set( 'post_status', 'auto-draft' );
}
}

/**
* Register a block template.
*
* @param string $template_name Template name in the form of `plugin_uri//template_name`.
* @param array|string $args {
* @type string $title Optional. Title of the template as it will be shown in the Site Editor
* and other UI elements.
* @type string $description Optional. Description of the template as it will be shown in the Site
* Editor.
* @type string $content Optional. Default content of the template that will be used when the
* template is rendered or edited in the editor.
* @type string[] $post_types Optional. Array of post types to which the template should be available.
* @type string $plugin Optional. Slug of the plugin that registers the template.
* }
* @return WP_Block_Template|WP_Error The registered template object on success, WP_Error object on failure.
*
* @since 6.7.0
*/
function wp_register_block_template( $template_name, $args = array() ) {
return WP_Block_Templates_Registry::get_instance()->register( $template_name, $args );
}

/**
* Unregister a block template.
*
* @param string $template_name Template name in the form of `plugin_uri//template_name`.
* @return WP_Block_Template|WP_Error True on success, WP_Error on failure or if the template doesn't exist.
*
* @since 6.7.0
*/
function wp_unregister_block_template( $template_name ) {
return WP_Block_Templates_Registry::get_instance()->unregister( $template_name );
}
8 changes: 8 additions & 0 deletions src/wp-includes/class-wp-block-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ class WP_Block_Template {
*/
public $author;

/**
* Plugin.
*
* @since 6.7.0
* @var string|null
*/
public $plugin;

/**
* Post types.
*
Expand Down
Loading
Loading