-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
wp_theme_has_theme_json
as a public API to know whether a theme…
… has a `theme.json` (#45168)
- Loading branch information
Showing
18 changed files
with
237 additions
and
25 deletions.
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 @@ | ||
/* | ||
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() { | ||
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 ); | ||
} | ||
} |