-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Update wp_theme_has_theme_json to use WP_Object_Cache
#45543
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
Changes from 1 commit
0739837
6003a43
f58e23b
20b2d8c
e5b1b41
a5e6bdf
57d2df9
5473642
f197534
0aa8ce3
ee310d7
ffee4e1
961528f
af3ecb9
5268077
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,9 +15,10 @@ | |
| * @return boolean | ||
| */ | ||
| function wp_theme_has_theme_json() { | ||
| $cache_group = 'theme_json'; | ||
oandregal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $cache_key = 'wp_theme_has_theme_json'; | ||
oandregal marked this conversation as resolved.
Show resolved
Hide resolved
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wanted to note here that this deliberately uses the function name as the cache key for a reason: this is expected to be unique in the PHP namespace. Additionally, unlike other functions, this is prefixed by |
||
| $cache_found = false; | ||
oandregal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| $theme_has_support = wp_cache_get( $cache_key, '', false, $cache_found ); | ||
| $theme_has_support = wp_cache_get( $cache_key, $cache_group, false, $cache_found ); | ||
| if ( $cache_found ) { | ||
| return $theme_has_support; | ||
| } | ||
|
|
@@ -30,7 +31,7 @@ function wp_theme_has_theme_json() { | |
| $theme_has_support = is_readable( get_template_directory() . '/theme.json' ); | ||
| } | ||
|
|
||
| wp_cache_set( $cache_key, $theme_has_support ); | ||
| wp_cache_set( $cache_key, $theme_has_support, $cache_group ); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit-pick, but we could simplify the code here to // Check whether the theme or its parent theme have a theme.json.
$theme_has_support = is_readable( get_stylesheet_directory() . '/theme.json' ) || is_readable( get_template_directory() . '/theme.json' );
wp_cache_set( $cache_key, (int) $theme_has_support, $cache_group );This code will do the same as what you have here. If the first check is
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One thing I like from the current implementation is that it is really obvious that it stores an integer instead of a boolean. That's a bit more obscured in the concise proposal, and I worry that means it'll become fragile (aka, someone from the future could remove the integer casting inadvertently). |
||
|
|
||
| return $theme_has_support; | ||
| } | ||
|
|
@@ -41,7 +42,7 @@ function wp_theme_has_theme_json() { | |
| * Clean theme.json related cached data. | ||
| */ | ||
| function wp_theme_clean_theme_json_cached_data() { | ||
|
||
| wp_cache_delete( 'wp_theme_has_theme_json' ); | ||
| wp_cache_delete( 'wp_theme_has_theme_json', 'theme_json' ); | ||
| WP_Theme_JSON_Resolver_Gutenberg::clean_cached_data(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function should accept a theme as a param. So we can check if none active themes have theme json. Example.
https://github.com/WordPress/wordpress-develop/blob/551c3d6619b7e88742911ca2dd3aed77a71cb0dc/src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php#L368-L374
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That could be useful, although I'm reluctant to do it in this PR. All the current use cases we have don't need it, so it adds complexity (manage invalidation of stylesheet per theme) without real needs. When/If we need that, we should implement it. It's a good follow-up PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with both of you here, but for now I agree more with @oandregal that this can be added in a follow up PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here and here are examples where we need to know if another theme that is not the current theme has theme supports json.
We can add this later, but this is something that is required.