WordPress / gutenberg

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

Block templates with specific postTypes that have been edited by the user can be selected for any other post type #43557

Closed Aljullu closed 2 years ago

Aljullu commented 2 years ago

Description

I originally opened this issue in Trac, but moving it here for better visibility.

When a block template provided by a theme has been edited by the user, the postTypes argument is ignored, so the template is available for the wrong post types.

Step-by-step reproduction instructions

  1. With a block theme (I used Twenty Twenty Two), create two templates and add them to theme.json:
    {
    "version": 2,
    "customTemplates": [
        ....
        {
            "name": "custom-single-post-template",
            "title": "Custom Single Post template",
            "postTypes": [
                "post"
            ]
        },
        {
            "name": "custom-single-post-template-not-modified",
            "title": "Custom Single Post template (not modified)",
            "postTypes": [
                "post"
            ]
        }
    ],
    ...
    }
  2. Go to Appearance > Editor > Templates and make a modification to Custom Single Post template.
  3. Now, create a new page.
  4. Notice in the Template panel in the sidebar, Custom Single Post template is available while Custom Single Post template (not modified) isn't.

Expected behavior

If the user modifies a template in the Site Editor, that shouldn't affect which post types that template is available for.

Additional info

I think the issue is caused by _build_block_template_result_from_post() not setting a post_types attribute to the returned templates. _build_block_template_result_from_post() is used by get_block_template().

We identified that issue when working on WooCommerce templates. See woocommerce/woocommerce#42630.

Screenshots, screen recording, code snippet

No response

Environment info

No response

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

Aljullu commented 2 years ago

PR created in wordpress-develop: https://github.com/WordPress/wordpress-develop/pull/3131

Mamaduka commented 2 years ago

Thanks for the core PR, @Aljullu.

I had a WIP branch for the plugin, which is slightly different from your patch. So I will try to create PR tomorrow morning.