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

Trac 56975 Stub functions in noop and restore caching #3872

Closed
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
16 changes: 16 additions & 0 deletions src/wp-admin/includes/noop.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,19 @@ function get_file( $path ) {

return @file_get_contents( $path );
}

/**
* @ignore
* @since 6.2.0
*/
function wp_cache_get() {
return false;
}
Comment on lines +112 to +114
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
function wp_cache_get() {
return false;
}
function wp_cache_get( $key = '', $group = '', $force = false, &$found = null ) {
$found = false;
return false;
}

This is to account for the case in which $found is used to account for false as a possible cached value.

$cache_found = false;
if ( null === $this->posts ) {
$cached_results = wp_cache_get( $cache_key, 'posts', false, $cache_found );

I don't know how much it's used in core but it would be good to avoid an error if it is.


/**
* @ignore
* @since 6.2.0
*/
function wp_cache_set() {
return false;
}
44 changes: 43 additions & 1 deletion src/wp-includes/global-styles-and-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,42 @@ function ( $item ) {
* @return bool Returns true if theme or its parent has a theme.json file, false otherwise.
*/
function wp_theme_has_theme_json() {
/*
* By using the 'theme_json' group, this data is marked to be non-persistent across requests.
* @see `wp_cache_add_non_persistent_groups()`.
*
* The rationale for this is to make sure derived data from theme.json
* is always fresh from the potential modifications done via hooks
* that can use dynamic data (modify the stylesheet depending on some option,
* settings depending on user permissions, etc.).
* For some of the existing hooks to modify theme.json behavior:
* @see https://make.wordpress.org/core/2022/10/10/filters-for-theme-json-data/
*
* A different alternative considered was to invalidate the cache upon certain
* events such as options add/update/delete, user meta, etc.
* It was judged not enough, hence this approach.
* @see https://github.com/WordPress/gutenberg/pull/45372
*/
$cache_group = 'theme_json';
$cache_key = 'wp_theme_has_theme_json';
$theme_has_support = wp_cache_get( $cache_key, $cache_group );

/*
* $theme_has_support is stored as an int in the cache.
*
* The reason not to store it as a boolean is to avoid working
* with the $found parameter which apparently had some issues in some implementations
* @see https://developer.wordpress.org/reference/functions/wp_cache_get/
*
* Ignore cache when `WP_DEBUG` is enabled, so it doesn't interfere with the theme
* developer's workflow.
*
* @todo Replace `WP_DEBUG` once an "in development mode" check is available in Core.
*/
if ( ! WP_DEBUG && is_int( $theme_has_support ) ) {
return (bool) $theme_has_support;
}

// Does the theme have its own theme.json?
$theme_has_support = is_readable( get_stylesheet_directory() . '/theme.json' );

Expand All @@ -272,7 +308,11 @@ function wp_theme_has_theme_json() {
$theme_has_support = is_readable( get_template_directory() . '/theme.json' );
}

return $theme_has_support;
$theme_has_support = $theme_has_support ? 1 : 0;

wp_cache_set( $cache_key, $theme_has_support, $cache_group );

return (bool) $theme_has_support;
}

/**
Expand All @@ -281,5 +321,7 @@ function wp_theme_has_theme_json() {
* @since 6.2.0
*/
function wp_clean_theme_json_cache() {
wp_cache_delete( 'wp_theme_has_theme_json', 'theme_json' );

WP_Theme_JSON_Resolver::clean_cached_data();
}
2 changes: 1 addition & 1 deletion src/wp-includes/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ function wp_start_object_cache() {
)
);

wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
wp_cache_add_non_persistent_groups( array( 'counts', 'plugins', 'theme_json' ) );
}

$first_init = false;
Expand Down
4 changes: 2 additions & 2 deletions src/wp-includes/ms-blogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ function switch_to_blog( $new_blog_id, $deprecated = null ) {
);
}

wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
wp_cache_add_non_persistent_groups( array( 'counts', 'plugins', 'theme_json' ) );
}
}

Expand Down Expand Up @@ -666,7 +666,7 @@ function restore_current_blog() {
);
}

wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
wp_cache_add_non_persistent_groups( array( 'counts', 'plugins', 'theme_json' ) );
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/includes/abstract-testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ public static function flush_cache() {
)
);

wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
wp_cache_add_non_persistent_groups( array( 'counts', 'plugins', 'theme_json' ) );
}

/**
Expand Down