Themes: Improve error notice when wp_is_block_theme() is called too early.

This is a follow-up to [59968] which improves error handling for when `wp_is_block_theme()` is called too early. Initially, `_doing_it_wrong()` was called in `WP_Theme::is_block_theme()` was called prior to themes being set up, in order to avoid the bug reported in #63062, where doing so would cause parent themes to not be resolved. After further investigation, the issue is only caused by calling `wp_is_block_theme()` before the root theme directory has been registered (e.g., when called on the `muplugins_loaded` hook).

Moving This error message to `wp_is_block_theme()` when `$GLOBALS['wp_theme_directories']` is empty avoids unnecessary error messages.

Props wildworks, sukhendu2002, SirLouen, dinhtungdu, narenin, joemcgill, peterwilsoncc, afragen.
Fixes #63086. See #63062.


git-svn-id: https://develop.svn.wordpress.org/trunk@60037 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Joe McGill 2025-03-18 12:28:17 +00:00
parent 056221298c
commit 79fd25fa56
2 changed files with 5 additions and 5 deletions

View File

@ -1576,11 +1576,6 @@ final class WP_Theme implements ArrayAccess {
* @return bool
*/
public function is_block_theme() {
if ( ! did_action( 'setup_theme' ) ) {
_doing_it_wrong( __METHOD__, __( 'This method should not be called before themes are set up.' ), '6.8.0' );
return false;
}
if ( isset( $this->block_theme ) ) {
return $this->block_theme;
}

View File

@ -4352,6 +4352,11 @@ function create_initial_theme_features() {
* @return bool Whether the active theme is a block-based theme or not.
*/
function wp_is_block_theme() {
if ( empty( $GLOBALS['wp_theme_directories'] ) ) {
_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before the theme directory is registered.' ), '6.8.0' );
return false;
}
return wp_get_theme()->is_block_theme();
}