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

Pattern: fix regression error in post type templates #55858

Merged

Conversation

renatho
Copy link
Contributor

@renatho renatho commented Nov 3, 2023

What?

Fixes #55846

When loading the editor, the getCurrentTheme() is returning undefined, which breaks the editor when the post type contains a pattern. So this PR fixes it.

I'd appreciate it if it could be merged before WP 6.4 is released, otherwise, we'll have users facing this issue on their sites.

It seems it started to happen after this PR: #53423. cc @pbking, in case you have the opportunity to check it.

Why?

We have some post types in Sensei LMS which contains a template with patterns. It happens at least for the Course, Lesson and Email post types. This is one related issue: Automattic/sensei#7262 in Sensei repository.

Notice that it can happen with any post type that has a template with a pattern.

How?

I just added a optional chain operator to make sure it won't break when the current theme is not ready for use in the editor yet.

I didn't investigate deeper if it could have other related issues, but this small fix fixes the mentioned use case specifically.

Testing Instructions

  1. Create a new post type with a template by using the following snippet:
    add_action( 'init', function() {
        register_post_type(
    	    'my-test',
    	    [
    		    'public'       => true,
    		    'show_in_rest' => true,
    		    'template'     => [
    			    [
    				    'core/pattern',
    				    [ 'slug' => 'core/simple-header-with-dark-background' ],
    			    ],
    		    ]
    	    ]
        );
    } );
  2. Create a new post of this new post type.
  3. See that no errors happen in the editor or in the browser console.

Screenshots or screencast

The error before the fix:

Screenshot 2023-11-01 at 11 48 33

@Rajinsharwar
Copy link
Contributor

Patch tested: This is solving the issue with the "Uncaught TypeError: Cannot read properties of undefined (reading 'stylesheet')". Tested with both a parent theme, as well as with a child theme. ✅

@mikachan mikachan added [Type] Bug An existing feature does not function as intended [Package] Block library /packages/block-library [Feature] Patterns A collection of blocks that can be synced (previously reusable blocks) or unsynced Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta labels Nov 4, 2023
Copy link
Member

@mikachan mikachan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this! 🙇 Great catch!

I can confirm this PR fixes the issue. Without this PR and on 6.4, I see the following console errors with a post in a custom post type:

image

With this PR, the console errors disappear. I can also confirm that the optional chain operator is used in other places in the codebase, including after getCurrentTheme(), so I'm confident this is a good fix. I think this is good to bring in and I've also labeled this for inclusion in 6.4.

@mikachan mikachan merged commit 74f5af4 into WordPress:trunk Nov 5, 2023
53 of 54 checks passed
@github-actions github-actions bot added this to the Gutenberg 17.1 milestone Nov 5, 2023
@mikachan
Copy link
Member

mikachan commented Nov 5, 2023

I just cherry-picked this PR to the 6.4-gb-sync-11.5 branch to get it included in the next release: 0ac4d37

@mikachan mikachan removed the Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Nov 5, 2023
@renatho renatho deleted the fix/post-type-template-with-patterns branch November 5, 2023 15:09
@renatho
Copy link
Contributor Author

renatho commented Nov 5, 2023

Awesome! Thank you @mikachan and @Rajinsharwar !!

pento pushed a commit to WordPress/wordpress-develop that referenced this pull request Nov 5, 2023
This update includes the following fixes reported after RC3:

* Regression: [WordPress/gutenberg#55858 Fixes patterns not working anymore as a post template for custom post types].

Scenario: When creating a new post for any custom post type registered with its "template" argument set to a pattern.

For this scenario, the pattern template no longer renders in the post editor or the frontend with 6.4, whereas it did render properly in 6.3.2.

This package update resolves the console error raised:
{{{
Uncaught TypeError: select(...).getCurrentTheme() is undefined 
}}}
which restores the rendering of the pattern template.

* Regression: [WordPress/gutenberg#55859  Fixes positioning and styles for the new lightbox's trigger] introduced in 6.4.

Follow up to [57048], [57034], [56987], [56961], [56849], [56818], [56816].

Props renathoc, rajinsharwar, richtabor, joen, mikachan, hellofromTonya.
Fixes #59411.

git-svn-id: https://develop.svn.wordpress.org/trunk@57063 602fd350-edb4-49c9-b593-d223f7449a82
github-actions bot pushed a commit to platformsh/wordpress-performance that referenced this pull request Nov 5, 2023
This update includes the following fixes reported after RC3:

* Regression: [WordPress/gutenberg#55858 Fixes patterns not working anymore as a post template for custom post types].

Scenario: When creating a new post for any custom post type registered with its "template" argument set to a pattern.

For this scenario, the pattern template no longer renders in the post editor or the frontend with 6.4, whereas it did render properly in 6.3.2.

This package update resolves the console error raised:
{{{
Uncaught TypeError: select(...).getCurrentTheme() is undefined 
}}}
which restores the rendering of the pattern template.

* Regression: [WordPress/gutenberg#55859  Fixes positioning and styles for the new lightbox's trigger] introduced in 6.4.

Follow up to [57048], [57034], [56987], [56961], [56849], [56818], [56816].

Props renathoc, rajinsharwar, richtabor, joen, mikachan, hellofromTonya.
Fixes #59411.
Built from https://develop.svn.wordpress.org/trunk@57063


git-svn-id: https://core.svn.wordpress.org/trunk@56574 1a063a9b-81f0-0310-95a4-ce76da25c4cd
markjaquith pushed a commit to markjaquith/WordPress that referenced this pull request Nov 5, 2023
This update includes the following fixes reported after RC3:

* Regression: [WordPress/gutenberg#55858 Fixes patterns not working anymore as a post template for custom post types].

Scenario: When creating a new post for any custom post type registered with its "template" argument set to a pattern.

For this scenario, the pattern template no longer renders in the post editor or the frontend with 6.4, whereas it did render properly in 6.3.2.

This package update resolves the console error raised:
{{{
Uncaught TypeError: select(...).getCurrentTheme() is undefined 
}}}
which restores the rendering of the pattern template.

* Regression: [WordPress/gutenberg#55859  Fixes positioning and styles for the new lightbox's trigger] introduced in 6.4.

Follow up to [57048], [57034], [56987], [56961], [56849], [56818], [56816].

Props renathoc, rajinsharwar, richtabor, joen, mikachan, hellofromTonya.
Fixes #59411.
Built from https://develop.svn.wordpress.org/trunk@57063


git-svn-id: http://core.svn.wordpress.org/trunk@56574 1a063a9b-81f0-0310-95a4-ce76da25c4cd
pento pushed a commit to WordPress/wordpress-develop that referenced this pull request Nov 5, 2023
This update includes the following fixes reported after RC3:

* Regression: [WordPress/gutenberg#55858 Fixes patterns not working anymore as a post template for custom post types].

Scenario: When creating a new post for any custom post type registered with its "template" argument set to a pattern.

For this scenario, the pattern template no longer renders in the post editor or the frontend with 6.4, whereas it did render properly in 6.3.2.

This package update resolves the console error raised:
{{{
Uncaught TypeError: select(...).getCurrentTheme() is undefined 
}}}
which restores the rendering of the pattern template.

* Regression: [WordPress/gutenberg#55859  Fixes positioning and styles for the new lightbox's trigger] introduced in 6.4.

Follow up to [57048], [57034], [56987], [56961], [56849], [56818], [56816].

Reviewed by jorbin.
Merges [57063] to the 6.4 branch.

Props renathoc, rajinsharwar, richtabor, joen, mikachan, hellofromTonya.
Fixes #59411.

git-svn-id: https://develop.svn.wordpress.org/branches/6.4@57064 602fd350-edb4-49c9-b593-d223f7449a82
markjaquith pushed a commit to markjaquith/WordPress that referenced this pull request Nov 5, 2023
This update includes the following fixes reported after RC3:

* Regression: [WordPress/gutenberg#55858 Fixes patterns not working anymore as a post template for custom post types].

Scenario: When creating a new post for any custom post type registered with its "template" argument set to a pattern.

For this scenario, the pattern template no longer renders in the post editor or the frontend with 6.4, whereas it did render properly in 6.3.2.

This package update resolves the console error raised:
{{{
Uncaught TypeError: select(...).getCurrentTheme() is undefined 
}}}
which restores the rendering of the pattern template.

* Regression: [WordPress/gutenberg#55859  Fixes positioning and styles for the new lightbox's trigger] introduced in 6.4.

Follow up to [57048], [57034], [56987], [56961], [56849], [56818], [56816].

Reviewed by jorbin.
Merges [57063] to the 6.4 branch.

Props renathoc, rajinsharwar, richtabor, joen, mikachan, hellofromTonya.
Fixes #59411.
Built from https://develop.svn.wordpress.org/branches/6.4@57064


git-svn-id: http://core.svn.wordpress.org/branches/6.4@56575 1a063a9b-81f0-0310-95a4-ce76da25c4cd
@oandregal oandregal changed the title Fix regression error with pattern in post type templates Pattern: fix regression error in post type templates Nov 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Patterns A collection of blocks that can be synced (previously reusable blocks) or unsynced [Package] Block library /packages/block-library [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Patterns not working anymore as a post template
3 participants