-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
get-global-styles-and-settings.php
87 lines (78 loc) · 2.92 KB
/
get-global-styles-and-settings.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?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.
*
* The result would be cached via the WP_Object_Cache.
* It can be cleared by calling wp_theme_has_theme_json_clean_cache().
*
* @return boolean
*/
function wp_theme_has_theme_json() {
$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
* https://developer.wordpress.org/reference/functions/wp_cache_get/
*
* Ignore cache when `WP_DEBUG` is enabled, so it doesn't interfere with the theme developers workflow.
*/
if ( ! WP_DEBUG && is_int( $theme_has_support ) ) {
return (bool) $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' );
}
$theme_has_support = $theme_has_support ? 1 : 0;
wp_cache_set( $cache_key, $theme_has_support, $cache_group );
return (bool) $theme_has_support;
}
}
if ( ! function_exists( 'wp_theme_has_theme_json_clean_cache' ) ) {
/**
* Function to clean the cache used by wp_theme_has_theme_json method.
*
* Not to backport to core. Delete it instead.
*/
function wp_theme_has_theme_json_clean_cache() {
_deprecated_function( __METHOD__, '14.7' );
}
}
/**
* Tell the cache mechanisms not to persist theme.json data across requests.
* The data stored under this cache group:
*
* - wp_theme_has_theme_json
* - gutenberg_get_global_settings
* - gutenberg_get_global_stylesheet
*
* There is some hooks consumers can use to modify parts
* of the theme.json logic.
* See https://make.wordpress.org/core/2022/10/10/filters-for-theme-json-data/
*
* The rationale to make this cache group non persistent 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,
* or settings depending on user permissions, etc.).
*
* 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
*/
function _gutenberg_add_non_persistent_theme_json_cache_group() {
wp_cache_add_non_persistent_groups( 'theme_json' );
}
add_action( 'plugins_loaded', '_gutenberg_add_non_persistent_theme_json_cache_group' );