Skip to content
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

Move theme.json support check to class #28788

Merged
merged 2 commits into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/class-wp-theme-json-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ class WP_Theme_JSON_Resolver {
*/
private static $theme = null;

/**
* Whether or not the theme supports theme.json.
*
* @var boolean
*/
private static $theme_has_support = null;
Copy link
Member

Choose a reason for hiding this comment

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

Just out of curiosity, is there a difference if we skip the = null part?
Usually I'd just write it like private static $theme_has_support;

Copy link
Member Author

@oandregal oandregal Feb 5, 2021

Choose a reason for hiding this comment

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

Nah, I don't think it'd matter in this particular case.

I understand it's a good practice to always initialize them and be explicit so you don't have to think about how the engine would do it and avoid weird cases:

  • there's an uninitialized variable, you think the engine is going to initialize it to null
  • with that expectation, in certain places of the code you use the isset check
  • however, the engine decides to initialize it to an empty string because of some type checking it has done
  • result: weird bug!

I don't know how much of this holds true for modern engines in any language VS being a no longer necessary convention people still do.


/**
* Container for data coming from the user.
*
Expand Down Expand Up @@ -478,4 +485,17 @@ public static function get_user_custom_post_type_id() {
return self::$user_custom_post_type_id;
}

/**
* Whether the current theme has a theme.json file.
*
* @return boolean
*/
public static function theme_has_support() {
if ( ! isset( self::$theme_has_support ) ) {
self::$theme_has_support = is_readable( locate_template( 'experimental-theme.json' ) );
}

return self::$theme_has_support;
}

}
27 changes: 6 additions & 21 deletions lib/global-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,6 @@
* @package gutenberg
*/

/**
* Whether the current theme has a theme.json file.
*
* @return boolean
*/
function gutenberg_experimental_global_styles_has_theme_json_support() {
static $is_readable;

if ( ! isset( $is_readable ) ) {
$is_readable = is_readable( locate_template( 'experimental-theme.json' ) );
}

return $is_readable;
}

/**
* Returns the theme presets registered via add_theme_support, if any.
*
Expand Down Expand Up @@ -155,7 +140,7 @@ function gutenberg_experimental_global_styles_get_stylesheet( $tree, $type = 'al

$stylesheet = $tree->get_stylesheet( $type );

if ( ( 'all' === $type || 'block_styles' === $type ) && gutenberg_experimental_global_styles_has_theme_json_support() ) {
if ( ( 'all' === $type || 'block_styles' === $type ) && WP_Theme_JSON_Resolver::theme_has_support() ) {
// To support all themes, we added in the block-library stylesheet
// a style rule such as .has-link-color a { color: var(--wp--style--color--link, #00e); }
// so that existing link colors themes used didn't break.
Expand All @@ -178,7 +163,7 @@ function gutenberg_experimental_global_styles_get_stylesheet( $tree, $type = 'al
* and enqueues the resulting stylesheet.
*/
function gutenberg_experimental_global_styles_enqueue_assets() {
if ( ! gutenberg_experimental_global_styles_has_theme_json_support() ) {
if ( ! WP_Theme_JSON_Resolver::theme_has_support() ) {
return;
}

Expand Down Expand Up @@ -219,7 +204,7 @@ function gutenberg_experimental_global_styles_settings( $settings ) {
$resolver = new WP_Theme_JSON_Resolver();
$origin = 'theme';
if (
gutenberg_experimental_global_styles_has_theme_json_support() &&
WP_Theme_JSON_Resolver::theme_has_support() &&
gutenberg_is_fse_theme()
) {
// Only lookup for the user data if we need it.
Expand All @@ -245,15 +230,15 @@ function gutenberg_experimental_global_styles_settings( $settings ) {
! empty( $screen ) &&
function_exists( 'gutenberg_is_edit_site_page' ) &&
gutenberg_is_edit_site_page( $screen->id ) &&
gutenberg_experimental_global_styles_has_theme_json_support() &&
WP_Theme_JSON_Resolver::theme_has_support() &&
gutenberg_is_fse_theme()
) {
$user_cpt_id = WP_Theme_JSON_Resolver::get_user_custom_post_type_id();
$base_styles = $resolver->get_origin( $theme_support_data, 'theme' )->get_raw_data();

$settings['__experimentalGlobalStylesUserEntityId'] = $user_cpt_id;
$settings['__experimentalGlobalStylesBaseStyles'] = $base_styles;
} elseif ( gutenberg_experimental_global_styles_has_theme_json_support() ) {
} elseif ( WP_Theme_JSON_Resolver::theme_has_support() ) {
// STEP 3 - ADD STYLES IF THEME HAS SUPPORT
//
// If we are in a block editor context, but not in edit-site,
Expand All @@ -278,7 +263,7 @@ function_exists( 'gutenberg_is_edit_site_page' ) &&
* @return array|undefined
*/
function gutenberg_experimental_global_styles_register_user_cpt() {
if ( ! gutenberg_experimental_global_styles_has_theme_json_support() ) {
if ( ! WP_Theme_JSON_Resolver::theme_has_support() ) {
return;
}

Expand Down