| @@ -8,10 +8,12 @@ | ||
| 8 | 8 | /** |
| 9 | 9 | * Returns the stylesheet resulting of merging core, theme, and user data. |
| 10 | 10 | * |
| 11 | 11 | * @param array $types Types of styles to load. Optional. |
| 12 | - * See {@see 'WP_Theme_JSON::get_stylesheet'} for all valid types. | |
| 13 | - * If empty, will load: 'variables', 'presets', 'styles'. | |
| 12 | + * It accepts as values: 'variables', 'presets', 'styles', 'base-layout-styles. | |
| 13 | + * If empty, it'll load the following: | |
| 14 | + * - for themes without theme.json: 'variables', 'presets', 'base-layout-styles'. | |
| 15 | + * - for themes with theme.json: 'variables', 'presets', 'styles'. | |
| 14 | 16 | * |
| 15 | 17 | * @return string Stylesheet. |
| 16 | 18 | */ |
| 17 | 19 | function gutenberg_get_global_stylesheet( $types = array() ) { |
| @@ -25,24 +27,17 @@ | ||
| 25 | 27 | return $cached; |
| 26 | 28 | } |
| 27 | 29 | } |
| 28 | 30 | $tree = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data(); |
| 29 | - $tree = WP_Theme_JSON_Resolver_Gutenberg::resolve_theme_file_uris( $tree ); | |
| 30 | 31 | |
| 31 | - if ( empty( $types ) ) { | |
| 32 | + $supports_theme_json = wp_theme_has_theme_json(); | |
| 33 | + if ( empty( $types ) && ! $supports_theme_json ) { | |
| 34 | + $types = array( 'variables', 'presets', 'base-layout-styles' ); | |
| 35 | + } elseif ( empty( $types ) ) { | |
| 32 | 36 | $types = array( 'variables', 'presets', 'styles' ); |
| 33 | 37 | } |
| 34 | 38 | |
| 35 | 39 | /* |
| 36 | - * Enable base layout styles only mode for classic themes without theme.json. | |
| 37 | - * This skips alignment styles that target .wp-site-blocks which is only used by block themes. | |
| 38 | - */ | |
| 39 | - $options = array(); | |
| 40 | - if ( ! wp_is_block_theme() && ! wp_theme_has_theme_json() ) { | |
| 41 | - $options['base_layout_styles'] = true; | |
| 42 | - } | |
| 43 | - | |
| 44 | - /* | |
| 45 | 40 | * If variables are part of the stylesheet, |
| 46 | 41 | * we add them. |
| 47 | 42 | * |
| 48 | 43 | * This is so themes without a theme.json still work as before 5.9: |
| @@ -57,9 +52,9 @@ | ||
| 57 | 52 | * at a later phase (render cycle) so we only render the ones in use. |
| 58 | 53 | * @see wp_add_global_styles_for_blocks |
| 59 | 54 | */ |
| 60 | 55 | $origins = array( 'default', 'theme', 'custom' ); |
| 61 | - $styles_variables = $tree->get_stylesheet( array( 'variables' ), $origins, $options ); | |
| 56 | + $styles_variables = $tree->get_stylesheet( array( 'variables' ), $origins ); | |
| 62 | 57 | $types = array_diff( $types, array( 'variables' ) ); |
| 63 | 58 | } |
| 64 | 59 | |
| 65 | 60 | /* |
| @@ -75,10 +70,13 @@ | ||
| 75 | 70 | * This is because styles for blocks origin are added |
| 76 | 71 | * at a later phase (render cycle) so we only render the ones in use. |
| 77 | 72 | * @see wp_add_global_styles_for_blocks |
| 78 | 73 | */ |
| 79 | - $origins = array( 'default', 'theme', 'custom' ); | |
| 80 | - $styles_rest = $tree->get_stylesheet( $types, $origins, $options ); | |
| 74 | + $origins = array( 'default', 'theme', 'custom' ); | |
| 75 | + if ( ! $supports_theme_json ) { | |
| 76 | + $origins = array( 'default' ); | |
| 77 | + } | |
| 78 | + $styles_rest = $tree->get_stylesheet( $types, $origins ); | |
| 81 | 79 | } |
| 82 | 80 | $stylesheet = $styles_variables . $styles_rest; |
| 83 | 81 | if ( $can_use_cached ) { |
| 84 | 82 | wp_cache_set( $cache_key, $stylesheet, $cache_group ); |
| @@ -113,9 +111,12 @@ | ||
| 113 | 111 | } |
| 114 | 112 | |
| 115 | 113 | // This is the default value when no origin is provided or when it is 'all'. |
| 116 | 114 | $origin = 'custom'; |
| 117 | - if ( isset( $context['origin'] ) && 'base' === $context['origin'] ) { | |
| 115 | + if ( | |
| 116 | + ! wp_theme_has_theme_json() || | |
| 117 | + ( isset( $context['origin'] ) && 'base' === $context['origin'] ) | |
| 118 | + ) { | |
| 118 | 119 | $origin = 'theme'; |
| 119 | 120 | } |
| 120 | 121 | |
| 121 | 122 | $cache_group = 'theme_json'; |
| @@ -132,14 +133,11 @@ | ||
| 132 | 133 | |
| 133 | 134 | /** |
| 134 | 135 | * Gets the global styles custom css from theme.json. |
| 135 | 136 | * |
| 136 | - * @deprecated Gutenberg 18.6.0 Use {@see 'gutenberg_get_global_stylesheet'} instead for top-level custom CSS, or {@see 'WP_Theme_JSON_Gutenberg::get_styles_for_block'} for block-level custom CSS. | |
| 137 | - * | |
| 138 | 137 | * @return string |
| 139 | 138 | */ |
| 140 | 139 | function gutenberg_get_global_styles_custom_css() { |
| 141 | - _deprecated_function( __FUNCTION__, 'Gutenberg 18.6.0', 'gutenberg_get_global_stylesheet' ); | |
| 142 | 140 | // Ignore cache when `WP_DEBUG` is enabled, so it doesn't interfere with the theme developers workflow. |
| 143 | 141 | $can_use_cached = ! WP_DEBUG; |
| 144 | 142 | $cache_key = 'gutenberg_get_global_custom_css'; |
| 145 | 143 | $cache_group = 'theme_json'; |
| @@ -149,41 +147,14 @@ | ||
| 149 | 147 | return $cached; |
| 150 | 148 | } |
| 151 | 149 | } |
| 152 | 150 | |
| 153 | - $tree = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data(); | |
| 154 | - $stylesheet = $tree->get_custom_css(); | |
| 155 | - | |
| 156 | - if ( $can_use_cached ) { | |
| 157 | - wp_cache_set( $cache_key, $stylesheet, $cache_group ); | |
| 151 | + if ( ! wp_theme_has_theme_json() ) { | |
| 152 | + return ''; | |
| 158 | 153 | } |
| 159 | 154 | |
| 160 | - return $stylesheet; | |
| 161 | -} | |
| 162 | - | |
| 163 | -/** | |
| 164 | - * Gets the global styles base custom CSS from theme.json. | |
| 165 | - * | |
| 166 | - * @since 6.6.0 | |
| 167 | - * | |
| 168 | - * @return string The global base custom CSS. | |
| 169 | - */ | |
| 170 | -function gutenberg_get_global_styles_base_custom_css() { | |
| 171 | - _deprecated_function( __FUNCTION__, 'Gutenberg 18.6.0', 'gutenberg_get_global_stylesheet' ); | |
| 172 | - | |
| 173 | - $can_use_cached = ! WP_DEBUG; | |
| 174 | - | |
| 175 | - $cache_key = 'gutenberg_get_global_styles_base_custom_css'; | |
| 176 | - $cache_group = 'theme_json'; | |
| 177 | - if ( $can_use_cached ) { | |
| 178 | - $cached = wp_cache_get( $cache_key, $cache_group ); | |
| 179 | - if ( $cached ) { | |
| 180 | - return $cached; | |
| 181 | - } | |
| 182 | - } | |
| 183 | - | |
| 184 | 155 | $tree = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data(); |
| 185 | - $stylesheet = $tree->get_base_custom_css(); | |
| 156 | + $stylesheet = $tree->get_custom_css(); | |
| 186 | 157 | |
| 187 | 158 | if ( $can_use_cached ) { |
| 188 | 159 | wp_cache_set( $cache_key, $stylesheet, $cache_group ); |
| 189 | 160 | } |
| @@ -191,103 +162,18 @@ | ||
| 191 | 162 | return $stylesheet; |
| 192 | 163 | } |
| 193 | 164 | |
| 194 | 165 | /** |
| 195 | - * Adds the global styles per-block custom CSS from theme.json | |
| 196 | - * to the inline style for each block. | |
| 197 | - * | |
| 198 | - * @since 6.6.0 | |
| 199 | - * | |
| 200 | - * @global WP_Styles $wp_styles | |
| 201 | - */ | |
| 202 | -function gutenberg_add_global_styles_block_custom_css() { | |
| 203 | - _deprecated_function( __FUNCTION__, 'Gutenberg 18.6.0', 'gutenberg_add_global_styles_for_blocks' ); | |
| 204 | - global $wp_styles; | |
| 205 | - | |
| 206 | - if ( ! wp_should_load_separate_core_block_assets() ) { | |
| 207 | - return; | |
| 208 | - } | |
| 209 | - | |
| 210 | - $tree = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data(); | |
| 211 | - $block_nodes = $tree->get_block_custom_css_nodes(); | |
| 212 | - | |
| 213 | - foreach ( $block_nodes as $metadata ) { | |
| 214 | - $block_css = $tree->get_block_custom_css( $metadata['css'], $metadata['selector'] ); | |
| 215 | - | |
| 216 | - $stylesheet_handle = 'global-styles'; | |
| 217 | - | |
| 218 | - /* | |
| 219 | - * When `wp_should_load_separate_core_block_assets()` is true, follow a similar | |
| 220 | - * logic to the one in `gutenberg_add_global_styles_for_blocks` to add the custom | |
| 221 | - * css only when the block is rendered. | |
| 222 | - */ | |
| 223 | - if ( isset( $metadata['name'] ) ) { | |
| 224 | - if ( str_starts_with( $metadata['name'], 'core/' ) ) { | |
| 225 | - $block_name = str_replace( 'core/', '', $metadata['name'] ); | |
| 226 | - $block_handle = 'wp-block-' . $block_name; | |
| 227 | - if ( in_array( $block_handle, $wp_styles->queue, true ) ) { | |
| 228 | - wp_add_inline_style( $stylesheet_handle, $block_css ); | |
| 229 | - } | |
| 230 | - } else { | |
| 231 | - wp_add_inline_style( $stylesheet_handle, $block_css ); | |
| 232 | - } | |
| 233 | - } | |
| 234 | - } | |
| 235 | -} | |
| 236 | - | |
| 237 | - | |
| 238 | -/** | |
| 239 | 166 | * Adds global style rules to the inline style for each block. |
| 240 | 167 | * |
| 241 | - * @global WP_Styles $wp_styles | |
| 242 | - * | |
| 243 | 168 | * @return void |
| 244 | 169 | */ |
| 245 | 170 | function gutenberg_add_global_styles_for_blocks() { |
| 246 | - global $wp_styles; | |
| 247 | 171 | $tree = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data(); |
| 248 | - $tree = WP_Theme_JSON_Resolver_Gutenberg::resolve_theme_file_uris( $tree ); | |
| 249 | 172 | $block_nodes = $tree->get_styles_block_nodes(); |
| 250 | - | |
| 251 | - $can_use_cached = ! wp_is_development_mode( 'theme' ); | |
| 252 | - $update_cache = false; | |
| 253 | - | |
| 254 | - if ( $can_use_cached ) { | |
| 255 | - // Hash the merged WP_Theme_JSON data to bust cache on settings or styles change. | |
| 256 | - $cache_hash = md5( wp_json_encode( $tree->get_raw_data() ) ); | |
| 257 | - $cache_key = 'wp_styles_for_blocks'; | |
| 258 | - $cached = get_transient( $cache_key ); | |
| 259 | - | |
| 260 | - // Reset the cached data if there is no value or if the hash has changed. | |
| 261 | - if ( ! is_array( $cached ) || $cached['hash'] !== $cache_hash ) { | |
| 262 | - $cached = array( | |
| 263 | - 'hash' => $cache_hash, | |
| 264 | - 'blocks' => array(), | |
| 265 | - ); | |
| 266 | - | |
| 267 | - // Update the cache if the hash has changed. | |
| 268 | - $update_cache = true; | |
| 269 | - } | |
| 270 | - } | |
| 271 | - | |
| 272 | 173 | foreach ( $block_nodes as $metadata ) { |
| 273 | - if ( $can_use_cached ) { | |
| 274 | - // Generate a unique cache key based on the full metadata to ensure pseudo-selectors and other variations get unique keys. | |
| 275 | - $cache_node_key = md5( wp_json_encode( $metadata ) ); | |
| 174 | + $block_css = $tree->get_styles_for_block( $metadata ); | |
| 276 | 175 | |
| 277 | - if ( isset( $cached['blocks'][ $cache_node_key ] ) ) { | |
| 278 | - $block_css = $cached['blocks'][ $cache_node_key ]; | |
| 279 | - } else { | |
| 280 | - $block_css = $tree->get_styles_for_block( $metadata ); | |
| 281 | - $cached['blocks'][ $cache_node_key ] = $block_css; | |
| 282 | - | |
| 283 | - // Update the cache if the cache contents have changed. | |
| 284 | - $update_cache = true; | |
| 285 | - } | |
| 286 | - } else { | |
| 287 | - $block_css = $tree->get_styles_for_block( $metadata ); | |
| 288 | - } | |
| 289 | - | |
| 290 | 176 | if ( ! wp_should_load_separate_core_block_assets() ) { |
| 291 | 177 | wp_add_inline_style( 'global-styles', $block_css ); |
| 292 | 178 | continue; |
| 293 | 179 | } |
| @@ -292,50 +178,43 @@ | ||
| 292 | 178 | continue; |
| 293 | 179 | } |
| 294 | 180 | |
| 295 | 181 | $stylesheet_handle = 'global-styles'; |
| 296 | - | |
| 297 | - /* | |
| 298 | - * When `wp_should_load_separate_core_block_assets()` is true, block styles are | |
| 299 | - * enqueued for each block on the page in class WP_Block's render function. | |
| 300 | - * This means there will be a handle in the styles queue for each of those blocks. | |
| 301 | - * Block-specific global styles should be attached to the global-styles handle, but | |
| 302 | - * only for blocks on the page, thus we check if the block's handle is in the queue | |
| 303 | - * before adding the inline style. | |
| 304 | - * This conditional loading only applies to core blocks. | |
| 305 | - */ | |
| 306 | 182 | if ( isset( $metadata['name'] ) ) { |
| 183 | + /* | |
| 184 | + * These block styles are added on block_render. | |
| 185 | + * This hooks inline CSS to them so that they are loaded conditionally | |
| 186 | + * based on whether or not the block is used on the page. | |
| 187 | + */ | |
| 307 | 188 | if ( str_starts_with( $metadata['name'], 'core/' ) ) { |
| 308 | - $block_name = str_replace( 'core/', '', $metadata['name'] ); | |
| 309 | - $block_handle = 'wp-block-' . $block_name; | |
| 310 | - if ( in_array( $block_handle, $wp_styles->queue, true ) ) { | |
| 311 | - wp_add_inline_style( $stylesheet_handle, $block_css ); | |
| 312 | - } | |
| 313 | - } else { | |
| 314 | - wp_add_inline_style( $stylesheet_handle, $block_css ); | |
| 189 | + $block_name = str_replace( 'core/', '', $metadata['name'] ); | |
| 190 | + $stylesheet_handle = 'wp-block-' . $block_name; | |
| 315 | 191 | } |
| 192 | + wp_add_inline_style( $stylesheet_handle, $block_css ); | |
| 316 | 193 | } |
| 317 | 194 | |
| 318 | 195 | // The likes of block element styles from theme.json do not have $metadata['name'] set. |
| 319 | 196 | if ( ! isset( $metadata['name'] ) && ! empty( $metadata['path'] ) ) { |
| 320 | - $block_name = wp_get_block_name_from_theme_json_path( $metadata['path'] ); | |
| 321 | - if ( $block_name ) { | |
| 322 | - if ( str_starts_with( $block_name, 'core/' ) ) { | |
| 323 | - $block_name = str_replace( 'core/', '', $block_name ); | |
| 324 | - $block_handle = 'wp-block-' . $block_name; | |
| 325 | - if ( in_array( $block_handle, $wp_styles->queue, true ) ) { | |
| 326 | - wp_add_inline_style( $stylesheet_handle, $block_css ); | |
| 197 | + $result = array_values( | |
| 198 | + array_filter( | |
| 199 | + $metadata['path'], | |
| 200 | + static function ( $item ) { | |
| 201 | + if ( strpos( $item, 'core/' ) !== false ) { | |
| 202 | + return true; | |
| 203 | + } | |
| 204 | + return false; | |
| 327 | 205 | } |
| 328 | - } else { | |
| 329 | - wp_add_inline_style( $stylesheet_handle, $block_css ); | |
| 206 | + ) | |
| 207 | + ); | |
| 208 | + if ( isset( $result[0] ) ) { | |
| 209 | + if ( str_starts_with( $result[0], 'core/' ) ) { | |
| 210 | + $block_name = str_replace( 'core/', '', $result[0] ); | |
| 211 | + $stylesheet_handle = 'wp-block-' . $block_name; | |
| 330 | 212 | } |
| 213 | + wp_add_inline_style( $stylesheet_handle, $block_css ); | |
| 331 | 214 | } |
| 332 | 215 | } |
| 333 | 216 | } |
| 334 | - | |
| 335 | - if ( $update_cache ) { | |
| 336 | - set_transient( $cache_key, $cached ); | |
| 337 | - } | |
| 338 | 217 | } |
| 339 | 218 | |
| 340 | 219 | /** |
| 341 | 220 | * Private function to clean the caches used by gutenberg_get_global_settings method. |
| @@ -347,9 +226,8 @@ | ||
| 347 | 226 | wp_cache_delete( 'gutenberg_get_global_stylesheet', 'theme_json' ); |
| 348 | 227 | wp_cache_delete( 'gutenberg_get_global_settings_custom', 'theme_json' ); |
| 349 | 228 | wp_cache_delete( 'gutenberg_get_global_settings_theme', 'theme_json' ); |
| 350 | 229 | wp_cache_delete( 'gutenberg_get_global_custom_css', 'theme_json' ); |
| 351 | - wp_cache_delete( 'gutenberg_get_global_styles_base_custom_css', 'theme_json' ); | |
| 352 | 230 | WP_Theme_JSON_Resolver_Gutenberg::clean_cached_data(); |
| 353 | 231 | } |
| 354 | 232 | add_action( 'start_previewing_theme', '_gutenberg_clean_theme_json_caches' ); |
| 355 | 233 | add_action( 'switch_theme', '_gutenberg_clean_theme_json_caches' ); |