@@ -79,3 +79,113 @@ function _wp_theme_has_theme_json_clean_cache_upon_upgrading_active_theme( $upgr
79
79
}
80
80
}
81
81
}
82
+
83
+ /**
84
+ * Returns the stylesheet resulting of merging core, theme, and user data.
85
+ *
86
+ * @param array $types Types of styles to load. Optional.
87
+ * It accepts 'variables', 'styles', 'presets' as values.
88
+ * If empty, it'll load all for themes with theme.json support
89
+ * and only [ 'variables', 'presets' ] for themes without theme.json support.
90
+ *
91
+ * @return string Stylesheet.
92
+ */
93
+ function gutenberg_get_global_stylesheet ( $ types = array () ) {
94
+ // Ignore cache when `WP_DEBUG` is enabled, so it doesn't interfere with the theme developers workflow.
95
+ $ can_use_cached = empty ( $ types ) && ( ! defined ( 'WP_DEBUG ' ) || ! WP_DEBUG );
96
+ $ cache_key = 'gutenberg_get_global_stylesheet ' ;
97
+ $ cache_group = 'theme_json ' ;
98
+ if ( $ can_use_cached ) {
99
+ $ cached = wp_cache_get ( $ cache_key , $ cache_group );
100
+ if ( $ cached ) {
101
+ return $ cached ;
102
+ }
103
+ }
104
+ $ tree = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data ();
105
+ $ supports_theme_json = wp_theme_has_theme_json ();
106
+ if ( empty ( $ types ) && ! $ supports_theme_json ) {
107
+ $ types = array ( 'variables ' , 'presets ' , 'base-layout-styles ' );
108
+ } elseif ( empty ( $ types ) ) {
109
+ $ types = array ( 'variables ' , 'styles ' , 'presets ' );
110
+ }
111
+
112
+ /*
113
+ * If variables are part of the stylesheet,
114
+ * we add them.
115
+ *
116
+ * This is so themes without a theme.json still work as before 5.9:
117
+ * they can override the default presets.
118
+ * See https://core.trac.wordpress.org/ticket/54782
119
+ */
120
+ $ styles_variables = '' ;
121
+ if ( in_array ( 'variables ' , $ types , true ) ) {
122
+ /*
123
+ * We only use the default, theme, and custom origins.
124
+ * This is because styles for blocks origin are added
125
+ * at a later phase (render cycle) so we only render the ones in use.
126
+ * @see wp_add_global_styles_for_blocks
127
+ */
128
+ $ origins = array ( 'default ' , 'theme ' , 'custom ' );
129
+ $ styles_variables = $ tree ->get_stylesheet ( array ( 'variables ' ), $ origins );
130
+ $ types = array_diff ( $ types , array ( 'variables ' ) );
131
+ }
132
+
133
+ /*
134
+ * For the remaining types (presets, styles), we do consider origins:
135
+ *
136
+ * - themes without theme.json: only the classes for the presets defined by core
137
+ * - themes with theme.json: the presets and styles classes, both from core and the theme
138
+ */
139
+ $ styles_rest = '' ;
140
+ if ( ! empty ( $ types ) ) {
141
+ /*
142
+ * We only use the default, theme, and custom origins.
143
+ * This is because styles for blocks origin are added
144
+ * at a later phase (render cycle) so we only render the ones in use.
145
+ * @see wp_add_global_styles_for_blocks
146
+ */
147
+ $ origins = array ( 'default ' , 'theme ' , 'custom ' );
148
+ if ( ! $ supports_theme_json ) {
149
+ $ origins = array ( 'default ' );
150
+ }
151
+ $ styles_rest = $ tree ->get_stylesheet ( $ types , $ origins );
152
+ }
153
+ $ stylesheet = $ styles_variables . $ styles_rest ;
154
+ if ( $ can_use_cached ) {
155
+ wp_cache_set ( $ cache_key , $ stylesheet , $ cache_group );
156
+ }
157
+ return $ stylesheet ;
158
+ }
159
+
160
+ /**
161
+ * Clean the cache used by the `gutenberg_get_global_stylesheet` function.
162
+ */
163
+ function gutenberg_get_global_stylesheet_clean_cache () {
164
+ wp_cache_delete ( 'gutenberg_get_global_stylesheet ' , 'theme_json ' );
165
+ }
166
+
167
+ /**
168
+ * Private function to clean the cache used by the `gutenberg_get_global_stylesheet` function after an upgrade.
169
+ *
170
+ * It is hooked into the `upgrader_process_complete` action.
171
+ *
172
+ * @see default-filters.php
173
+ *
174
+ * @param WP_Upgrader $upgrader WP_Upgrader instance.
175
+ * @param array $options Array of bulk item update data.
176
+ */
177
+ function _gutenberg_get_global_stylesheet_clean_cache_upon_upgrading ( $ upgrader , $ options ) {
178
+ if ( 'update ' !== $ options ['action ' ] ) {
179
+ return ;
180
+ }
181
+
182
+ if (
183
+ 'core ' === $ options ['type ' ] ||
184
+ 'plugin ' === $ options ['type ' ] ||
185
+ // Clean cache only if the active theme was updated.
186
+ ( 'theme ' === $ options ['type ' ] && ( isset ( $ options ['themes ' ][ get_stylesheet () ] ) || isset ( $ options ['themes ' ][ get_template () ] ) ) )
187
+ ) {
188
+ gutenberg_get_global_stylesheet_clean_cache ();
189
+ }
190
+ }
191
+
0 commit comments