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

Update ReaderThemeSupportFeatures::theme_has_theme_json() with latest changes #7583

Merged
merged 3 commits into from
Jul 11, 2023
Merged
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
44 changes: 18 additions & 26 deletions src/ReaderThemeSupportFeatures.php
Original file line number Diff line number Diff line change
Expand Up @@ -579,37 +579,29 @@ public function get_relative_luminance_from_hex( $hex ) {
* @return bool False if `wp_get_global_settings()` not exists or theme.json not found, true otherwise.
*/
private function theme_has_theme_json() {
// @TODO: Uncomment this once `wp_theme_has_theme_json()` caching is fixed.
// if ( function_exists( 'wp_theme_has_theme_json' ) ) {
// return wp_theme_has_theme_json();
// }
if ( function_exists( 'wp_theme_has_theme_json' ) ) {
return wp_theme_has_theme_json();
}

static $theme_has_support = null;
static $prev_stylesheet_directory = null;
static $prev_template_directory = null;
static $theme_has_support = [];

$stylesheet_directory = get_stylesheet_directory();
$template_directory = get_template_directory();

// Make sure that the cached $theme_has_support value is reset when the theme changes.
if ( null !== $theme_has_support && (
$stylesheet_directory !== $prev_stylesheet_directory ||
$template_directory !== $prev_template_directory
) ) {
$theme_has_support = null;
}
$prev_stylesheet_directory = $stylesheet_directory;
$prev_template_directory = $template_directory;
$stylesheet = get_stylesheet();

if (
null !== $theme_has_support &&
// Ignore static cache when the development mode is set to 'theme', to avoid interfering with
// the theme developer's workflow.
( function_exists( 'wp_get_development_mode' ) && wp_get_development_mode() !== 'theme' )
isset( $theme_has_support[ $stylesheet ] ) &&

/*
* Ignore static cache when `WP_DEBUG` is enabled. Why? To avoid interfering with
* the theme developer's workflow. Note that core uses a newer wp_get_development_mode() check.
*/
! ( defined( 'WP_DEBUG' ) && WP_DEBUG )
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@westonruter wp_get_development_mode() is recently introduced. So should we keep WP_DEBUG instead? Or we can add this function to this file and then use it.

Copy link
Member

Choose a reason for hiding this comment

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

Good point. Yes, this should keep using WP_DEBUG. For sites with wp_get_development_mode(), they'll also have wp_theme_has_theme_json().

) {
return $theme_has_support;
return $theme_has_support[ $stylesheet ];
}

$stylesheet_directory = get_stylesheet_directory();
$template_directory = get_template_directory();

// This is the same as get_theme_file_path(), which isn't available in load-styles.php context.
if ( $stylesheet_directory !== $template_directory && file_exists( $stylesheet_directory . '/theme.json' ) ) {
$path = $stylesheet_directory . '/theme.json';
Expand All @@ -620,9 +612,9 @@ private function theme_has_theme_json() {
/** This filter is documented in wp-includes/link-template.php */
$path = apply_filters( 'theme_file_path', $path, 'theme.json' );

$theme_has_support = file_exists( $path );
$theme_has_support[ $stylesheet ] = file_exists( $path );

return $theme_has_support;
return $theme_has_support[ $stylesheet ];
}

/**
Expand Down