| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * APIs to interact with global settings & styles. 4 * 5 * @package WordPress 6 */ 7 8 /** 9 * Gets the settings resulting of merging core, theme, and user data. 10 * 11 * @since 5.9.0 12 * @since 7.2.0 Removed the per-request cache; WP_Theme_JSON_Resolver::get_merged_data() caches the merged data. 13 * 14 * @param array $path Path to the specific setting to retrieve. Optional. 15 * If empty, will return all settings. 16 * @param array $context { 17 * Metadata to know where to retrieve the $path from. Optional. 18 * 19 * @type string $block_name Which block to retrieve the settings from. 20 * If empty, it'll return the settings for the global context. 21 * @type string $origin Which origin to take data from. 22 * Valid values are 'all' (core, theme, and user) or 'base' (core and theme). 23 * If empty or unknown, 'all' is used. 24 * } 25 * @return mixed The settings array or individual setting value to retrieve. 26 */ 27 function wp_get_global_settings( $path = array(), $context = array() ) { 28 if ( ! empty( $context['block_name'] ) ) { 29 $new_path = array( 'blocks', $context['block_name'] ); 30 foreach ( $path as $subpath ) { 31 $new_path[] = $subpath; 32 } 33 $path = $new_path; 34 } 35 36 // This is the default value when no origin is provided or when it is 'all'. 37 $origin = 'custom'; 38 if ( isset( $context['origin'] ) && 'base' === $context['origin'] ) { 39 $origin = 'theme'; 40 } 41 42 $settings = WP_Theme_JSON_Resolver::get_merged_data( $origin )->get_settings(); 43 44 return _wp_array_get( $settings, $path, $settings ); 45 } 46 47 /** 48 * Returns CSS media queries for responsive viewport style states. 49 * 50 * Breakpoint values are read from the viewport settings, sanitized, and 51 * normalized before the media query strings are generated. By default, the 52 * returned keys are `@mobile` and `@tablet`. When `$options['include_desktop']` 53 * is truthy, `@desktop` is included. 54 * 55 * @since 7.2.0 56 * 57 * @param mixed $viewport_settings Viewport settings from theme.json. 58 * @param array $options { 59 * Optional. Options for generating media queries. 60 * 61 * @type bool $include_desktop Whether to include the desktop media query. Default false. 62 * } 63 * @return array Responsive media queries. 64 */ 65 function wp_get_viewport_media_queries( $viewport_settings = null, $options = array() ) { 66 return WP_Theme_JSON::get_viewport_media_queries( $viewport_settings, $options ); 67 } 68 69 /** 70 * Gets the styles resulting of merging core, theme, and user data. 71 * 72 * @since 5.9.0 73 * @since 6.3.0 the internal link format "var:preset|color|secondary" is resolved 74 * to "var(--wp--preset--font-size--small)" so consumers don't have to. 75 * @since 6.3.0 `transforms` is now usable in the `context` parameter. In case [`transforms`]['resolve_variables'] 76 * is defined, variables are resolved to their value in the styles. 77 * 78 * @param array $path Path to the specific style to retrieve. Optional. 79 * If empty, will return all styles. 80 * @param array $context { 81 * Metadata to know where to retrieve the $path from. Optional. 82 * 83 * @type string $block_name Which block to retrieve the styles from. 84 * If empty, it'll return the styles for the global context. 85 * @type string $origin Which origin to take data from. 86 * Valid values are 'all' (core, theme, and user) or 'base' (core and theme). 87 * If empty or unknown, 'all' is used. 88 * @type array $transforms Which transformation(s) to apply. 89 * Valid value is array( 'resolve-variables' ). 90 * If defined, variables are resolved to their value in the styles. 91 * } 92 * @return mixed The styles array or individual style value to retrieve. 93 */ 94 function wp_get_global_styles( $path = array(), $context = array() ) { 95 if ( ! empty( $context['block_name'] ) ) { 96 $path = array_merge( array( 'blocks', $context['block_name'] ), $path ); 97 } 98 99 $origin = 'custom'; 100 if ( isset( $context['origin'] ) && 'base' === $context['origin'] ) { 101 $origin = 'theme'; 102 } 103 104 $resolve_variables = isset( $context['transforms'] ) 105 && is_array( $context['transforms'] ) 106 && in_array( 'resolve-variables', $context['transforms'], true ); 107 108 $merged_data = WP_Theme_JSON_Resolver::get_merged_data( $origin ); 109 if ( $resolve_variables ) { 110 $merged_data = WP_Theme_JSON::resolve_variables( $merged_data ); 111 } 112 $styles = $merged_data->get_raw_data()['styles']; 113 return _wp_array_get( $styles, $path, $styles ); 114 } 115 116 117 /** 118 * Returns the stylesheet resulting of merging core, theme, and user data. 119 * 120 * @since 5.9.0 121 * @since 6.1.0 Added 'base-layout-styles' support. 122 * @since 6.6.0 Resolves relative paths in theme.json styles to theme absolute paths. 123 * @since 7.0.0 Deprecated 'base-layout-styles' type; classic themes now receive full styles 124 * with layout-specific alignment rules skipped via `base_layout_styles` option. 125 * 126 * @param array $types Optional. Types of styles to load. 127 * See {@see 'WP_Theme_JSON::get_stylesheet'} for all valid types. 128 * If empty, will load: 'variables', 'presets', 'styles'. 129 * @return string Stylesheet. 130 */ 131 function wp_get_global_stylesheet( $types = array() ) { 132 /* 133 * Ignore cache when the development mode is set to 'theme', so it doesn't interfere with the theme 134 * developer's workflow. 135 */ 136 $can_use_cached = empty( $types ) && ! wp_is_development_mode( 'theme' ); 137 138 /* 139 * By using the 'theme_json' group, this data is marked to be non-persistent across requests. 140 * @see `wp_cache_add_non_persistent_groups()`. 141 * 142 * The rationale for this is to make sure derived data from theme.json 143 * is always fresh from the potential modifications done via hooks 144 * that can use dynamic data (modify the stylesheet depending on some option, 145 * settings depending on user permissions, etc.). 146 * See some of the existing hooks to modify theme.json behavior: 147 * @link https://make.wordpress.org/core/2022/10/10/filters-for-theme-json-data/ 148 * 149 * A different alternative considered was to invalidate the cache upon certain 150 * events such as options add/update/delete, user meta, etc. 151 * It was judged not enough, hence this approach. 152 * @link https://github.com/WordPress/gutenberg/pull/45372 153 */ 154 $cache_group = 'theme_json'; 155 $cache_key = 'wp_get_global_stylesheet'; 156 if ( $can_use_cached ) { 157 $cached = wp_cache_get( $cache_key, $cache_group ); 158 if ( $cached ) { 159 return $cached; 160 } 161 } 162 163 $tree = WP_Theme_JSON_Resolver::resolve_theme_file_uris( WP_Theme_JSON_Resolver::get_merged_data() ); 164 165 if ( empty( $types ) ) { 166 $types = array( 'variables', 'styles', 'presets' ); 167 } 168 169 /* 170 * Enable base layout styles only mode for classic themes without theme.json. 171 * This skips alignment styles that target .wp-site-blocks which is only used by block themes. 172 */ 173 $options = array(); 174 if ( ! wp_is_block_theme() && ! wp_theme_has_theme_json() ) { 175 $options['base_layout_styles'] = true; 176 } 177 178 /* 179 * If variables are part of the stylesheet, then add them. 180 * This is so themes without a theme.json still work as before 5.9: 181 * they can override the default presets. 182 * See https://core.trac.wordpress.org/ticket/54782 183 */ 184 $styles_variables = ''; 185 if ( in_array( 'variables', $types, true ) ) { 186 /* 187 * Only use the default, theme, and custom origins. Why? 188 * Because styles for `blocks` origin are added at a later phase 189 * (i.e. in the render cycle). Here, only the ones in use are rendered. 190 * @see wp_add_global_styles_for_blocks 191 */ 192 $origins = array( 'default', 'theme', 'custom' ); 193 $styles_variables = $tree->get_stylesheet( array( 'variables' ), $origins, $options ); 194 $types = array_diff( $types, array( 'variables' ) ); 195 } 196 197 /* 198 * For the remaining types (presets, styles), we do consider origins: 199 * 200 * - themes without theme.json: only the classes for the presets defined by core 201 * - themes with theme.json: the presets and styles classes, both from core and the theme 202 */ 203 $styles_rest = ''; 204 if ( ! empty( $types ) ) { 205 /* 206 * Only use the default, theme, and custom origins. Why? 207 * Because styles for `blocks` origin are added at a later phase 208 * (i.e. in the render cycle). Here, only the ones in use are rendered. 209 * @see wp_add_global_styles_for_blocks 210 */ 211 $origins = array( 'default', 'theme', 'custom' ); 212 $styles_rest = $tree->get_stylesheet( $types, $origins, $options ); 213 } 214 215 $stylesheet = $styles_variables . $styles_rest; 216 if ( $can_use_cached ) { 217 wp_cache_set( $cache_key, $stylesheet, $cache_group ); 218 } 219 220 return $stylesheet; 221 } 222 223 /** 224 * Adds global style rules to the inline style for each block. 225 * 226 * @since 6.1.0 227 * @since 6.7.0 Resolve relative paths in block styles. 228 * 229 * @global WP_Styles $wp_styles 230 */ 231 function wp_add_global_styles_for_blocks() { 232 global $wp_styles; 233 234 $tree = WP_Theme_JSON_Resolver::get_merged_data(); 235 $tree = WP_Theme_JSON_Resolver::resolve_theme_file_uris( $tree ); 236 $block_nodes = $tree->get_styles_block_nodes(); 237 238 $can_use_cached = ! wp_is_development_mode( 'theme' ); 239 $update_cache = false; 240 241 if ( $can_use_cached ) { 242 // Hash the merged WP_Theme_JSON data to bust cache on settings or styles change. 243 $cache_hash = md5( wp_json_encode( $tree->get_raw_data() ) ); 244 $cache_key = 'wp_styles_for_blocks'; 245 $cached = get_transient( $cache_key ); 246 247 // Reset the cached data if there is no value or if the hash has changed. 248 if ( ! is_array( $cached ) || $cached['hash'] !== $cache_hash ) { 249 $cached = array( 250 'hash' => $cache_hash, 251 'blocks' => array(), 252 ); 253 254 // Update the cache if the hash has changed. 255 $update_cache = true; 256 } 257 } 258 259 foreach ( $block_nodes as $metadata ) { 260 261 if ( $can_use_cached ) { 262 // Generate a unique cache key based on the full metadata to ensure pseudo-selectors and other variations get unique keys. 263 $cache_node_key = md5( wp_json_encode( $metadata ) ); 264 265 if ( isset( $cached['blocks'][ $cache_node_key ] ) ) { 266 $block_css = $cached['blocks'][ $cache_node_key ]; 267 } else { 268 $block_css = $tree->get_styles_for_block( $metadata ); 269 $cached['blocks'][ $cache_node_key ] = $block_css; 270 271 // Update the cache if the cache contents have changed. 272 $update_cache = true; 273 } 274 } else { 275 $block_css = $tree->get_styles_for_block( $metadata ); 276 } 277 278 if ( ! wp_should_load_block_assets_on_demand() ) { 279 wp_add_inline_style( 'global-styles', $block_css ); 280 continue; 281 } 282 283 $stylesheet_handle = 'global-styles'; 284 285 /* 286 * When `wp_should_load_block_assets_on_demand()` is true, block styles are 287 * enqueued for each block on the page in class WP_Block's render function. 288 * This means there will be a handle in the styles queue for each of those blocks. 289 * Block-specific global styles should be attached to the global-styles handle, but 290 * only for blocks on the page, thus we check if the block's handle is in the queue 291 * before adding the inline style. 292 * This conditional loading only applies to core blocks. 293 * TODO: Explore how this could be expanded to third-party blocks as well. 294 */ 295 if ( isset( $metadata['name'] ) ) { 296 if ( str_starts_with( $metadata['name'], 'core/' ) ) { 297 $block_name = str_replace( 'core/', '', $metadata['name'] ); 298 $block_handle = 'wp-block-' . $block_name; 299 if ( in_array( $block_handle, $wp_styles->queue, true ) ) { 300 wp_add_inline_style( $stylesheet_handle, $block_css ); 301 } 302 } else { 303 wp_add_inline_style( $stylesheet_handle, $block_css ); 304 } 305 } 306 307 // The likes of block element styles from theme.json do not have $metadata['name'] set. 308 if ( ! isset( $metadata['name'] ) && ! empty( $metadata['path'] ) ) { 309 $block_name = wp_get_block_name_from_theme_json_path( $metadata['path'] ); 310 if ( $block_name ) { 311 if ( str_starts_with( $block_name, 'core/' ) ) { 312 $block_name = str_replace( 'core/', '', $block_name ); 313 $block_handle = 'wp-block-' . $block_name; 314 if ( in_array( $block_handle, $wp_styles->queue, true ) ) { 315 wp_add_inline_style( $stylesheet_handle, $block_css ); 316 } 317 } else { 318 wp_add_inline_style( $stylesheet_handle, $block_css ); 319 } 320 } 321 } 322 } 323 324 if ( $update_cache ) { 325 set_transient( $cache_key, $cached ); 326 } 327 } 328 329 /** 330 * Gets the block name from a given theme.json path. 331 * 332 * @since 6.3.0 333 * @access private 334 * 335 * @param array $path An array of keys describing the path to a property in theme.json. 336 * @return string Identified block name, or empty string if none found. 337 */ 338 function wp_get_block_name_from_theme_json_path( $path ) { 339 // Block name is expected to be the third item after 'styles' and 'blocks'. 340 if ( 341 count( $path ) >= 3 342 && 'styles' === $path[0] 343 && 'blocks' === $path[1] 344 && str_contains( $path[2], '/' ) 345 ) { 346 return $path[2]; 347 } 348 349 /* 350 * As fallback and for backward compatibility, allow any core block to be 351 * at any position. 352 */ 353 $result = array_values( 354 array_filter( 355 $path, 356 static function ( $item ) { 357 if ( str_contains( $item, 'core/' ) ) { 358 return true; 359 } 360 return false; 361 } 362 ) 363 ); 364 return $result[0] ?? ''; 365 } 366 367 /** 368 * Checks whether a theme or its parent has a theme.json file. 369 * 370 * @since 6.2.0 371 * 372 * @return bool Returns true if theme or its parent has a theme.json file, false otherwise. 373 */ 374 function wp_theme_has_theme_json() { 375 static $theme_has_support = array(); 376 377 $stylesheet = get_stylesheet(); 378 379 if ( 380 isset( $theme_has_support[ $stylesheet ] ) && 381 /* 382 * Ignore static cache when the development mode is set to 'theme', to avoid interfering with 383 * the theme developer's workflow. 384 */ 385 ! wp_is_development_mode( 'theme' ) 386 ) { 387 return $theme_has_support[ $stylesheet ]; 388 } 389 390 $path = get_theme_file_path( 'theme.json' ); 391 392 $theme_has_support[ $stylesheet ] = file_exists( $path ); 393 394 return $theme_has_support[ $stylesheet ]; 395 } 396 397 /** 398 * Cleans the caches under the theme_json group. 399 * 400 * @since 6.2.0 401 */ 402 function wp_clean_theme_json_cache() { 403 wp_cache_delete( 'wp_get_global_stylesheet', 'theme_json' ); 404 wp_cache_delete( 'wp_get_global_styles_svg_filters', 'theme_json' ); 405 wp_cache_delete( 'wp_get_global_styles_custom_css', 'theme_json' ); 406 wp_cache_delete( 'wp_get_theme_data_template_parts', 'theme_json' ); 407 WP_Theme_JSON_Resolver::clean_cached_data(); 408 } 409 410 /** 411 * Returns the current theme's wanted patterns (slugs) to be 412 * registered from Pattern Directory. 413 * 414 * @since 6.3.0 415 * 416 * @return string[] 417 */ 418 function wp_get_theme_directory_pattern_slugs() { 419 return WP_Theme_JSON_Resolver::get_theme_data( array(), array( 'with_supports' => false ) )->get_patterns(); 420 } 421 422 /** 423 * Returns the metadata for the custom templates defined by the theme via theme.json. 424 * 425 * @since 6.4.0 426 * 427 * @return array Associative array of `$template_name => $template_data` pairs, 428 * with `$template_data` having "title" and "postTypes" fields. 429 */ 430 function wp_get_theme_data_custom_templates() { 431 return WP_Theme_JSON_Resolver::get_theme_data( array(), array( 'with_supports' => false ) )->get_custom_templates(); 432 } 433 434 /** 435 * Returns the metadata for the template parts defined by the theme. 436 * 437 * @since 6.4.0 438 * 439 * @return array Associative array of `$part_name => $part_data` pairs, 440 * with `$part_data` having "title" and "area" fields. 441 */ 442 function wp_get_theme_data_template_parts() { 443 $cache_group = 'theme_json'; 444 $cache_key = 'wp_get_theme_data_template_parts'; 445 $can_use_cached = ! wp_is_development_mode( 'theme' ); 446 447 if ( $can_use_cached ) { 448 $metadata = wp_cache_get( $cache_key, $cache_group ); 449 if ( false !== $metadata ) { 450 return $metadata; 451 } 452 } 453 454 $metadata = WP_Theme_JSON_Resolver::get_theme_data( array(), array( 'with_supports' => false ) )->get_template_parts(); 455 if ( $can_use_cached ) { 456 wp_cache_set( $cache_key, $metadata, $cache_group ); 457 } 458 459 return $metadata; 460 } 461 462 /** 463 * Determines the CSS selector for the block type and property provided, 464 * returning it if available. 465 * 466 * @since 6.3.0 467 * 468 * @param WP_Block_Type $block_type The block's type. 469 * @param string|array $target The desired selector's target, `root` or array path. 470 * @param bool $fallback Whether to fall back to broader selector. 471 * @return string|null CSS selector or `null` if no selector available. 472 */ 473 function wp_get_block_css_selector( $block_type, $target = 'root', $fallback = false ) { 474 if ( empty( $target ) ) { 475 return null; 476 } 477 478 $has_selectors = ! empty( $block_type->selectors ); 479 480 // Root Selector. 481 482 // Calculated before returning as it can be used as fallback for 483 // feature selectors later on. 484 $root_selector = null; 485 486 if ( $has_selectors && isset( $block_type->selectors['root'] ) ) { 487 // Use the selectors API if available. 488 $root_selector = $block_type->selectors['root']; 489 } elseif ( isset( $block_type->supports['__experimentalSelector'] ) && is_string( $block_type->supports['__experimentalSelector'] ) ) { 490 // Use the old experimental selector supports property if set. 491 $root_selector = $block_type->supports['__experimentalSelector']; 492 } else { 493 // If no root selector found, generate default block class selector. 494 $block_name = str_replace( '/', '-', str_replace( 'core/', '', $block_type->name ) ); 495 $root_selector = ".wp-block-{$block_name}"; 496 } 497 498 // Return selector if it's the root target we are looking for. 499 if ( 'root' === $target ) { 500 return $root_selector; 501 } 502 503 // If target is not `root` we have a feature or subfeature as the target. 504 // If the target is a string convert to an array. 505 if ( is_string( $target ) ) { 506 $target = explode( '.', $target ); 507 } 508 509 // Feature Selectors ( May fallback to root selector ). 510 if ( 1 === count( $target ) ) { 511 $fallback_selector = $fallback ? $root_selector : null; 512 513 // Prefer the selectors API if available. 514 if ( $has_selectors ) { 515 // Look for selector under `feature.root`. 516 $path = array( current( $target ), 'root' ); 517 $feature_selector = _wp_array_get( $block_type->selectors, $path, null ); 518 519 if ( $feature_selector ) { 520 return $feature_selector; 521 } 522 523 // Check if feature selector is set via shorthand. 524 $feature_selector = _wp_array_get( $block_type->selectors, $target, null ); 525 526 return is_string( $feature_selector ) ? $feature_selector : $fallback_selector; 527 } 528 529 // Try getting old experimental supports selector value. 530 $path = array( current( $target ), '__experimentalSelector' ); 531 $feature_selector = _wp_array_get( $block_type->supports, $path, null ); 532 533 // Nothing to work with, provide fallback or null. 534 if ( null === $feature_selector ) { 535 return $fallback_selector; 536 } 537 538 // Scope the feature selector by the block's root selector. 539 return WP_Theme_JSON::scope_selector( $root_selector, $feature_selector ); 540 } 541 542 // Subfeature selector 543 // This may fallback either to parent feature or root selector. 544 $subfeature_selector = null; 545 546 // Use selectors API if available. 547 if ( $has_selectors ) { 548 $subfeature_selector = _wp_array_get( $block_type->selectors, $target, null ); 549 } 550 551 // Only return if we have a subfeature selector. 552 if ( $subfeature_selector ) { 553 return $subfeature_selector; 554 } 555 556 // To this point we don't have a subfeature selector. If a fallback 557 // has been requested, remove subfeature from target path and return 558 // results of a call for the parent feature's selector. 559 if ( $fallback ) { 560 return wp_get_block_css_selector( $block_type, $target[0], $fallback ); 561 } 562 563 return null; 564 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Fri Oct 30 08:20:34 2026 | Cross-referenced by PHPXref |