Skip to content

Conversation

@joemcgill
Copy link
Member

This moves the location of the _doing_it_wrong() call added to WP_Theme::is_block_theme() in [59968] to wp_is_block_theme() in order to inform developers that calling this function too early can result in broken parent/child theme relationships.

Trac ticket: https://core.trac.wordpress.org/ticket/63086


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

This moves the location of the `_doing_it_wrong()` call added to `WP_Theme::is_block_theme()` in [59968] to `wp_is_block_theme()` in order to inform developers that calling this function too early can result in broken parent/child theme relationships.

Ticket: https://core.trac.wordpress.org/ticket/63086.
@github-actions
Copy link

github-actions bot commented Mar 17, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props joemcgill, peterwilsoncc, afragen.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@afragen
Copy link
Member

afragen commented Mar 17, 2025

Doesn’t is_block_theme() return false if directories aren’t set up? If so, is there a need to exit early in the warning?

@joemcgill
Copy link
Member Author

joemcgill commented Mar 17, 2025

Good question, @afragen! Returning early ensures that wp_get_theme() is avoided to avoid the bug from Trac-63062.

(Edit: also, FWIW, I checked the Core Rollback plugin, and that plugin is calling wp_is_block_theme() as part of the settings that are triggered on admin_init, so no notice should be thrown.)

@github-actions
Copy link

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@afragen
Copy link
Member

afragen commented Mar 17, 2025

@joemcgill I didn't think there was an issue in Core Rollback, but it is an example of use in a plugin.

I've added your changes in 2 of my test sites running 6.8-beta2. I think there might still be an issue somewhere in The Events Calendar plugin.

@joemcgill
Copy link
Member Author

I've added your changes in 2 of my test sites running 6.8-beta2. I think there might still be an issue somewhere in The Events Calendar plugin.

Thanks for testing, @afragen! You're right, this will get triggered on TEC, because they are calling wp_is_block_theme() on the plugins_loaded hook in order to determine whether the current theme supports full site editing. While calling wp_is_block_theme() this early won't cause the bug from https://core.trac.wordpress.org/ticket/63062 to occur, it will result in incorrect behavior, because it will return false even if the active theme is a block theme, since the theme has not yet been set up.

@afragen
Copy link
Member

afragen commented Mar 17, 2025

The changes have been running on 2 of my test servers and only reported errors are from The Events Calendar and Events Calendar Pro.

It’s looking much better.

*/
function wp_is_block_theme() {
if ( ! did_action( 'setup_theme' ) ) {
_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before themes are set up.' ), '6.8.0' );
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before themes are set up.' ), '6.8.0' );
_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before theme directories are set up.' ), '6.8.0' );

Or

This function should not be called before register_theme_directory() is called.

Co-authored-by: Joe McGill <801097+joemcgill@users.noreply.github.com>
@peterwilsoncc
Copy link
Contributor

peterwilsoncc commented Mar 18, 2025

I've merged in Joe's suggestion of switching to test for the definition of theme directories. No error is thrown in the customizer during my testing.

Using a modified version of @SirLouen's testing plugin while running @fabiankaegy's MVP block child theme attached to Core-63062, I've been able to confirm child theme are working as expected. Without the early return prior to the the directories being set up, the child theme continues to result in a fatal error when the function is called on muplugins_loaded.

Screenshot 2025-03-18 at 2 31 14 pm

Coincidently, I've got some client work to do this-afternoon on a site running WooCommerce, so I will check for notices in that once I have switched tasks.

@github-actions
Copy link

A commit was made that fixes the Trac ticket referenced in the description of this pull request.

SVN changeset: 60037
GitHub commit: 79fd25f

This PR will be closed, but please confirm the accuracy of this and reopen if there is more work to be done.

@github-actions github-actions bot closed this Mar 18, 2025
@SirLouen
Copy link
Member

SirLouen commented Mar 18, 2025

Replying to @peterwilsoncc

No error is thrown in the customizer during my testing.

I got this error yesterday in the morning and this is why I started researching further in the idea proposed by dinhtungdu.
Then I built the plugin in the evening, tested everything according to his suggested ideas, and thereafter, tested again this patch, and got the error once more.

But I was testing against the if ( ! did_action( 'setup_theme' ) ) { version

Not the current if ( empty( $GLOBALS['wp_theme_directories'] ) ) { version, which is entirely different. But my plugin cannot catch this.

PS: I've noted that the convention to check for the global $wp_theme_directories is using this format:

	global $wp_theme_directories;
	if ( empty( $wp_theme_directories ) ) {
		_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before themes are set up.' ), '6.8.0' );
		return false;
	}

Instead of $GLOBALS['wp_theme_directories'] that I observe it's only used in the testing files, despite both forms are right

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants