WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.32k stars 4.12k forks source link

Should templates registered for nonexistent/inactive plugins appear in the UI? #64754

Open justintadlock opened 2 weeks ago

justintadlock commented 2 weeks ago

61577 introduced a plugin template registration API. The intention was to specifically let plugin authors register templates specifically for their plugin by registering the template in the form of plugin_uri//template_name.

This works well. As you can see, if I register a template for Gutenberg with gutenberg//template_name, it correctly appears in the UI:

add_action( 'init', function() {
    wp_register_block_template( 'gutenberg//awesome', [
        'title'       => __( 'An Awesome Template', 'pluginslug' ),
        'description' => __( 'The template description', 'pluginslug' ),
        'content'     => '<!-- wp:template-part {"slug":"header","tagName":"header"} /--><!-- wp:group {"tagName":"main","layout":{"inherit":true}} --><main class="wp-block-group"><!-- wp:paragraph --><p>This is a plugin-registered template.</p><!-- /wp:paragraph --></main><!-- /wp:group -->'
    ] );
} );

Screenshot:

image

However, if I change the plugin_uri bit to anything (not a valid, active plugin), such as super-duper//awesome, it still appears in the UI but with the super-duper "title":

add_action( 'init', function() {
    wp_register_block_template( 'super-duper//awesome', [
        'title'       => __( 'An Awesome Template', 'pluginslug' ),
        'description' => __( 'The template description', 'pluginslug' ),
        'content'     => '<!-- wp:template-part {"slug":"header","tagName":"header"} /--><!-- wp:group {"tagName":"main","layout":{"inherit":true}} --><main class="wp-block-group"><!-- wp:paragraph --><p>This is a plugin-registered template.</p><!-- /wp:paragraph --></main><!-- /wp:group -->'
    ] );
} );

Screenshot:

image

Note that there's no way to define a title that can be internationalized for this section in the UI, so I think this is likely bad practice in general.

Discussion / Needs Decision

Should this be restricted so that it doesn't show templates registered for inactive plugin URIs? Or should this continue working as a sort of fallback? Other options?


I think there are larger discussions on expanding what can be registered in future iterations of the feature (custom groups/categories, theme-registered templates, etc.). But I want to limit this ticket to just what the behavior should be for this first version of the API so that there is no confusion around what should happen.

Aljullu commented 2 weeks ago

Thanks for opening this issue, @justintadlock!

I didn't restrict the namespace to match the current plugin uri to follow suit with register_block_type(), which also allows using any namespace. However, one difference between both functions is that the namespace used by wp_register_block_template() ends up being shown in the UI as the author/origin of the template, while the namespace used in register_block_type() is never shown in the UI, AFAIK.

I don't have a strong opinion towards restricting the namespace to match the plugin uri or not. However, in order to be cautious, I lean towards being more restrictive, at least in the first version, and only accept the current plugin uri as the namespace. This would allow us to open the namespace to any value in the future. However, if the first version allows any namespace, we will never be able to revert the decision.

What do you think?

cc @ntsekouras @youknowriad

ndiego commented 2 weeks ago

I lean towards being more restrictive, at least in the first version, and only accept the current plugin uri as the namespace.

I think this is reasonable for the first iteration of this API. I can imagine people wanting to change this though in the future, but you would also need to be able to set a title.

youknowriad commented 1 week ago

the namespace used in register_block_type() is never shown in the UI, AFAIK.

Just want to correct this. The "namespace" of register_block_type can be shown in the UI if you also register it as a "block collection" (it's very similar in principal to the template source)

ndiego commented 1 week ago

The "namespace" of register_block_type can be shown in the UI if you also register it as a "block collection" (it's very similar in principal to the template source)

This is new to me, or maybe I am misunderstanding something. How do you register a block collection?

youknowriad commented 1 week ago

@ndiego I think the API is documented here https://developer.wordpress.org/block-editor/reference-guides/block-api/block-registration/#block-collections (wp.blocks.registerBlockCollection)... It's a very old API :)

ndiego commented 1 week ago

I think the API is documented here https://developer.wordpress.org/block-editor/reference-guides/block-api/block-registration/#block-collections (wp.blocks.registerBlockCollection)... It's a very old API :)

Ah, very cool. Thanks!