Skip to content

Commit

Permalink
Add wp_theme_has_theme_json as a public API to know whether a theme…
Browse files Browse the repository at this point in the history
… has a `theme.json` (#45168)
  • Loading branch information
oandregal authored Oct 28, 2022
1 parent 05e92d0 commit 9ff5888
Show file tree
Hide file tree
Showing 18 changed files with 237 additions and 25 deletions.
4 changes: 2 additions & 2 deletions lib/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ function gutenberg_restore_group_inner_container( $block_content, $block ) {
preg_quote( $tag_name, '/' )
);
if (
WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ||
wp_theme_has_theme_json() ||
1 === preg_match( $group_with_inner_container_regex, $block_content ) ||
( isset( $block['attrs']['layout']['type'] ) && 'flex' === $block['attrs']['layout']['type'] )
) {
Expand Down Expand Up @@ -542,7 +542,7 @@ function gutenberg_restore_image_outer_container( $block_content, $block ) {
)/iUx";

if (
WP_Theme_JSON_Resolver::theme_has_support() ||
wp_theme_has_theme_json() ||
0 === preg_match( $image_with_align, $block_content, $matches )
) {
return $block_content;
Expand Down
2 changes: 1 addition & 1 deletion lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ function gutenberg_register_packages_styles( $styles ) {
);

// Only load the default layout and margin styles for themes without theme.json file.
if ( ! WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) {
if ( ! wp_theme_has_theme_json() ) {
$wp_edit_blocks_dependencies[] = 'wp-editor-classic-layout-styles';
}

Expand Down
2 changes: 1 addition & 1 deletion lib/compat/wordpress-6.0/block-patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function _register_remote_theme_patterns() {
return;
}

if ( ! WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) {
if ( ! wp_theme_has_theme_json() ) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static function get_theme_data( $deprecated = array(), $options = array()
* and merge the static::$theme upon that.
*/
$theme_support_data = WP_Theme_JSON_Gutenberg::get_from_editor_settings( get_default_block_editor_settings() );
if ( ! static::theme_has_support() ) {
if ( ! wp_theme_has_theme_json() ) {
if ( ! isset( $theme_support_data['settings']['color'] ) ) {
$theme_support_data['settings']['color'] = array();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function gutenberg_get_global_styles_svg_filters() {
}
}

$supports_theme_json = WP_Theme_JSON_Resolver_Gutenberg::theme_has_support();
$supports_theme_json = wp_theme_has_theme_json();

$origins = array( 'default', 'theme', 'custom' );
if ( ! $supports_theme_json ) {
Expand Down
2 changes: 1 addition & 1 deletion lib/compat/wordpress-6.1/block-editor-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function gutenberg_get_block_editor_settings( $settings ) {
}
}

if ( WP_Theme_JSON_Resolver::theme_has_support() ) {
if ( wp_theme_has_theme_json() ) {
$block_classes = array(
'css' => 'styles',
'__unstableType' => 'theme',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function gutenberg_get_global_stylesheet( $types = array() ) {
}
}
$tree = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data();
$supports_theme_json = WP_Theme_JSON_Resolver_Gutenberg::theme_has_support();
$supports_theme_json = wp_theme_has_theme_json();
if ( empty( $types ) && ! $supports_theme_json ) {
$types = array( 'variables', 'presets', 'base-layout-styles' );
} elseif ( empty( $types ) ) {
Expand Down
2 changes: 1 addition & 1 deletion lib/compat/wordpress-6.1/template-parts-screen.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ static function( $classes ) {
'styles' => get_block_editor_theme_styles(),
'defaultTemplateTypes' => $indexed_template_types,
'defaultTemplatePartAreas' => get_allowed_block_template_part_areas(),
'supportsLayout' => WP_Theme_JSON_Resolver::theme_has_support(),
'supportsLayout' => wp_theme_has_theme_json(),
'supportsTemplatePartsMode' => ! wp_is_block_theme() && current_theme_supports( 'block-template-parts' ),
'__unstableHomeTemplate' => gutenberg_resolve_home_template(),
);
Expand Down
27 changes: 27 additions & 0 deletions lib/compat/wordpress-6.2/default-filters.php
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 lib/compat/wordpress-6.2/get-global-styles-and-settings.php
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();
}
}
2 changes: 1 addition & 1 deletion lib/experimental/block-editor-settings-mobile.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function gutenberg_get_block_editor_settings_mobile( $settings ) {
isset( $_GET['context'] ) &&
'mobile' === $_GET['context']
) {
if ( WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) {
if ( wp_theme_has_theme_json() ) {
$settings['__experimentalStyles'] = gutenberg_get_global_styles();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static function get_theme_data( $deprecated = array(), $settings = array(
* and merge the static::$theme upon that.
*/
$theme_support_data = WP_Theme_JSON_Gutenberg::get_from_editor_settings( get_default_block_editor_settings() );
if ( ! static::theme_has_support() ) {
if ( ! wp_theme_has_theme_json() ) {
if ( ! isset( $theme_support_data['settings']['color'] ) ) {
$theme_support_data['settings']['color'] = array();
}
Expand Down
2 changes: 2 additions & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/compat/wordpress-6.1/block-editor-settings.php';
require __DIR__ . '/compat/wordpress-6.1/persisted-preferences.php';
require __DIR__ . '/compat/wordpress-6.1/get-global-styles-and-settings.php';
require __DIR__ . '/compat/wordpress-6.2/get-global-styles-and-settings.php';
require __DIR__ . '/compat/wordpress-6.2/default-filters.php';
require __DIR__ . '/compat/wordpress-6.1/class-wp-theme-json-data-gutenberg.php';
require __DIR__ . '/compat/wordpress-6.1/class-wp-theme-json-6-1.php';
require __DIR__ . '/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php';
Expand Down
15 changes: 1 addition & 14 deletions phpunit/class-wp-theme-json-resolver-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,19 +125,6 @@ public function test_translations_are_applied() {
);
}

public function test_switching_themes_recalculates_data() {
// The "default" theme doesn't have theme.json support.
switch_theme( 'default' );
$default = WP_Theme_JSON_Resolver_Gutenberg::theme_has_support();

// Switch to a theme that does have support.
switch_theme( 'block-theme' );
$has_theme_json_support = WP_Theme_JSON_Resolver_Gutenberg::theme_has_support();

$this->assertFalse( $default );
$this->assertTrue( $has_theme_json_support );
}

public function test_add_theme_supports_are_loaded_for_themes_without_theme_json() {
switch_theme( 'default' );
$color_palette = array(
Expand Down Expand Up @@ -165,7 +152,7 @@ public function test_add_theme_supports_are_loaded_for_themes_without_theme_json
remove_theme_support( 'custom-line-height' );
remove_theme_support( 'editor-color-palette' );

$this->assertFalse( WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() );
$this->assertFalse( wp_theme_has_theme_json() );
$this->assertTrue( $settings['typography']['lineHeight'] );
$this->assertSame( $color_palette, $settings['color']['palette']['theme'] );
}
Expand Down
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
*/
8 changes: 8 additions & 0 deletions phpunit/data/themedir1/default-child-no-theme-json/style.css
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
*/
13 changes: 13 additions & 0 deletions phpunit/data/themedir1/default/style.css
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.
*/



120 changes: 120 additions & 0 deletions phpunit/wp-theme-json-test.php
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 );
}
}

0 comments on commit 9ff5888

Please sign in to comment.