-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Add wp_theme_has_theme_json
as a public API to know whether a theme has a theme.json
#45168
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
ddd3839
Add wp_theme_has_theme_json
oandregal 1d13726
Use the public API in all places
oandregal e694592
Add wp_theme_clean_theme_json_cached_data
oandregal f4288b7
Port and add unit tests
oandregal 204df73
Add note to backport
oandregal 9c15bdb
Update lib/compat/wordpress-6.2/get-global-styles-and-settings.php
oandregal c17920f
Load filters
oandregal 3300cb6
Better naming
oandregal 3079045
Fix lint issues
oandregal 1bb3680
Add -test.php suffix for it to be picked up
oandregal be07c58
Fix lint issues
oandregal 331a2ef
Better coverage for all use cases
oandregal 0c181ee
Simplify
oandregal 174793f
Update test for resolver
oandregal 2b570aa
Update lib/compat/wordpress-6.2/get-global-styles-and-settings.php
oandregal 92db7ba
Properly set up theme directory
oandregal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
/** | ||
* Sets up the default filters and actions for most | ||
* of the WordPress hooks. | ||
* | ||
* If you need to remove a default hook, this file will | ||
* give you the priority to use for removing the hook. | ||
* | ||
* Not all of the default hooks are found in this file. | ||
* For instance, administration-related hooks are located in | ||
* wp-admin/includes/admin-filters.php. | ||
* | ||
* If a hook should only be called from a specific context | ||
* (admin area, multisite environment…), please move it | ||
* to a more appropriate file instead. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Note for backport: we should also remove the existing filters: | ||
* | ||
* > add_action( 'switch_theme', array( 'WP_Theme_JSON_Resolver', 'clean_cached_data' ) ); | ||
* > add_action( 'start_previewing_theme', array( 'WP_Theme_JSON_Resolver', 'clean_cached_data' ) ); | ||
*/ | ||
add_action( 'switch_theme', 'wp_theme_clean_theme_json_cached_data' ); | ||
add_action( 'start_previewing_theme', 'wp_theme_clean_theme_json_cached_data' ); |
47 changes: 47 additions & 0 deletions
47
lib/compat/wordpress-6.2/get-global-styles-and-settings.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
/** | ||
* API to interact with global settings & styles. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
if ( ! function_exists( 'wp_theme_has_theme_json' ) ) { | ||
/** | ||
* Whether a theme or its parent have a theme.json file. | ||
* | ||
* @param boolean $clear_cache Whether the cache should be cleared and theme support recomputed. Default is false. | ||
* | ||
* @return boolean | ||
*/ | ||
function wp_theme_has_theme_json( $clear_cache = false ) { | ||
static $theme_has_support = null; | ||
|
||
if ( true === $clear_cache ) { | ||
$theme_has_support = null; | ||
} | ||
|
||
if ( null !== $theme_has_support ) { | ||
return $theme_has_support; | ||
} | ||
|
||
// Has the own theme a theme.json? | ||
$theme_has_support = is_readable( get_stylesheet_directory() . '/theme.json' ); | ||
|
||
// Look up the parent if the child does not have a theme.json. | ||
if ( ! $theme_has_support ) { | ||
$theme_has_support = is_readable( get_template_directory() . '/theme.json' ); | ||
} | ||
|
||
return $theme_has_support; | ||
} | ||
} | ||
|
||
if ( ! function_exists( 'wp_theme_clean_theme_json_cached_data' ) ) { | ||
/** | ||
* Clean theme.json related cached data. | ||
*/ | ||
function wp_theme_clean_theme_json_cached_data() { | ||
wp_theme_has_theme_json( true ); | ||
WP_Theme_JSON_Resolver_Gutenberg::clean_cached_data(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
phpunit/data/themedir1/block-theme-child-no-theme-json/style.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
Theme Name: Block Theme Child with no theme.json | ||
Theme URI: https://wordpress.org/ | ||
Description: For testing purposes only. | ||
Template: block-theme | ||
Version: 1.0.0 | ||
Text Domain: block-theme-child-no-theme-json | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
Theme Name: Default Child Theme with no theme.json | ||
Theme URI: https://wordpress.org/ | ||
Description: For testing purposes only. | ||
Template: default | ||
Version: 1.0.0 | ||
Text Domain: default-child-no-theme-json | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Took the stylesheet from the |
||
Theme Name: WordPress Default | ||
Theme URI: http://wordpress.org/ | ||
Description: The default WordPress theme based on the famous <a href="http://binarybonsai.com/kubrick/">Kubrick</a>. | ||
Version: 1.6 | ||
Author: Michael Heilemann | ||
Author URI: http://binarybonsai.com/ | ||
|
||
This is just a stub to test the loading of the above metadata. | ||
*/ | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
/** | ||
* Tests theme.json related public APIs. | ||
* | ||
* @package Gutenberg | ||
*/ | ||
|
||
class WP_Theme_Json_Test extends WP_UnitTestCase { | ||
|
||
public function set_up() { | ||
parent::set_up(); | ||
$this->theme_root = realpath( __DIR__ . '/data/themedir1' ); | ||
|
||
$this->orig_theme_dir = $GLOBALS['wp_theme_directories']; | ||
|
||
// /themes is necessary as theme.php functions assume /themes is the root if there is only one root. | ||
$GLOBALS['wp_theme_directories'] = array( WP_CONTENT_DIR . '/themes', $this->theme_root ); | ||
|
||
add_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) ); | ||
add_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) ); | ||
add_filter( 'template_root', array( $this, 'filter_set_theme_root' ) ); | ||
$this->queries = array(); | ||
// Clear caches. | ||
wp_clean_themes_cache(); | ||
unset( $GLOBALS['wp_themes'] ); | ||
} | ||
|
||
public function tear_down() { | ||
$GLOBALS['wp_theme_directories'] = $this->orig_theme_dir; | ||
wp_clean_themes_cache(); | ||
unset( $GLOBALS['wp_themes'] ); | ||
parent::tear_down(); | ||
} | ||
|
||
public function filter_set_theme_root() { | ||
return $this->theme_root; | ||
} | ||
|
||
/** | ||
* Test that it reports correctly themes that have a theme.json. | ||
* | ||
* @group theme_json | ||
* | ||
* @covers wp_theme_has_theme_json | ||
*/ | ||
public function test_theme_has_theme_json() { | ||
switch_theme( 'block-theme' ); | ||
$this->assertTrue( wp_theme_has_theme_json() ); | ||
} | ||
|
||
/** | ||
* Test that it reports correctly themes that do not have a theme.json. | ||
* | ||
* @group theme_json | ||
* | ||
* @covers wp_theme_has_theme_json | ||
*/ | ||
public function test_theme_has_no_theme_json() { | ||
switch_theme( 'default' ); | ||
$this->assertFalse( wp_theme_has_theme_json() ); | ||
} | ||
|
||
/** | ||
* Test it reports correctly child themes that have a theme.json. | ||
* | ||
* @group theme_json | ||
* | ||
* @covers wp_theme_has_theme_json | ||
*/ | ||
public function test_child_theme_has_theme_json() { | ||
switch_theme( 'block-theme-child' ); | ||
$this->assertTrue( wp_theme_has_theme_json() ); | ||
} | ||
|
||
/** | ||
* Test that it reports correctly child themes that do not have a theme.json | ||
* and the parent does. | ||
* | ||
* @group theme_json | ||
* | ||
* @covers wp_theme_has_theme_json | ||
*/ | ||
public function test_child_theme_has_not_theme_json_but_parent_has() { | ||
oandregal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
switch_theme( 'block-theme-child-no-theme-json' ); | ||
$this->assertTrue( wp_theme_has_theme_json() ); | ||
} | ||
|
||
/** | ||
* Test that it reports correctly child themes that do not have a theme.json | ||
* and the parent does not either. | ||
* | ||
* @group theme_json | ||
* | ||
* @covers wp_theme_has_theme_json | ||
*/ | ||
public function test_neither_child_or_parent_themes_have_theme_json() { | ||
switch_theme( 'default-child-no-theme-json' ); | ||
$this->assertFalse( wp_theme_has_theme_json() ); | ||
} | ||
|
||
/** | ||
* Test that switching themes recalculates theme support. | ||
* | ||
* @group theme_json | ||
* | ||
* @covers wp_theme_has_theme_json | ||
*/ | ||
public function test_switching_themes_recalculates_support() { | ||
// The "default" theme doesn't have theme.json support. | ||
switch_theme( 'default' ); | ||
$default = wp_theme_has_theme_json(); | ||
|
||
// Switch to a theme that does have support. | ||
switch_theme( 'block-theme' ); | ||
$block_theme = wp_theme_has_theme_json(); | ||
|
||
$this->assertFalse( $default ); | ||
$this->assertTrue( $block_theme ); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has been moved to
phpunit/wp-theme-json-test.php