|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * APIs to interact with global settings & styles. |
| 4 | + * |
| 5 | + * @package WordPress |
| 6 | + */ |
| 7 | + |
| 8 | +/** |
| 9 | + * Function to get the settings resulting of merging core, theme, and user data. |
| 10 | + * |
| 11 | + * @since 5.9.0 |
| 12 | + * |
| 13 | + * @param array $path Path to the specific setting to retrieve. Optional. |
| 14 | + * If empty, will return all settings. |
| 15 | + * @param string $block_name Which block to retrieve the settings from. Optional |
| 16 | + * If empty, it'll return the settings for the global context. |
| 17 | + * @param string $origin Which origin to take data from. Optional. |
| 18 | + * It can be 'all' (core, theme, and user) or 'base' (core and theme). |
| 19 | + * If empty or unknown, 'all' is used. |
| 20 | + * |
| 21 | + * @return array The settings to retrieve. |
| 22 | + */ |
| 23 | +function wp_get_global_settings( $path = array(), $block_name = '', $origin = 'all' ) { |
| 24 | + if ( '' !== $block_name ) { |
| 25 | + $path = array_merge( array( 'blocks', $block_name ), $path ); |
| 26 | + } |
| 27 | + |
| 28 | + if ( 'base' === $origin ) { |
| 29 | + $origin = 'theme'; |
| 30 | + } else { |
| 31 | + $origin = 'user'; |
| 32 | + } |
| 33 | + |
| 34 | + $settings = WP_Theme_JSON_Resolver::get_merged_data( $origin )->get_settings(); |
| 35 | + |
| 36 | + return _wp_array_get( $settings, $path, $settings ); |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Function to get the styles resulting of merging core, theme, and user data. |
| 41 | + * |
| 42 | + * @since 5.9.0 |
| 43 | + * |
| 44 | + * @param array $path Path to the specific style to retrieve. Optional. |
| 45 | + * If empty, will return all styles. |
| 46 | + * @param string $block_name Which block to retrieve the styles from. Optional. |
| 47 | + * If empty, it'll return the styles for the global context. |
| 48 | + * @param string $origin Which origin to take data from. Optional. |
| 49 | + * It can be 'all' (core, theme, and user) or 'base' (core and theme). |
| 50 | + * If empty or unknown, 'all' is used. |
| 51 | + * |
| 52 | + * @return array The styles to retrieve. |
| 53 | + */ |
| 54 | +function wp_get_global_styles( $path = array(), $block_name = '', $origin = 'all' ) { |
| 55 | + if ( '' !== $block_name ) { |
| 56 | + $path = array_merge( array( 'blocks', $block_name ), $path ); |
| 57 | + } |
| 58 | + |
| 59 | + if ( 'base' === $origin ) { |
| 60 | + $origin = 'theme'; |
| 61 | + } else { |
| 62 | + $origin = 'user'; |
| 63 | + } |
| 64 | + |
| 65 | + $styles = WP_Theme_JSON_Resolver::get_merged_data( $origin )->get_raw_data()['styles']; |
| 66 | + |
| 67 | + return _wp_array_get( $styles, $path, $styles ); |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Returns the stylesheet resulting of merging core, theme, and user data. |
| 72 | + * |
| 73 | + * @since 5.9.0 |
| 74 | + * |
| 75 | + * @param array $types Types of styles to load. Optional. |
| 76 | + * It accepts 'variables', 'styles', 'presets' as values. |
| 77 | + * If empty, it'll load all for themes with theme.json support |
| 78 | + * and only [ 'variables', 'presets' ] for themes without theme.json support. |
| 79 | + * |
| 80 | + * @return string Stylesheet. |
| 81 | + */ |
| 82 | +function wp_get_global_stylesheet( $types = array() ) { |
| 83 | + // Return cached value if it can be used and exists. |
| 84 | + // It's cached by theme to make sure that theme switching clears the cache. |
| 85 | + $can_use_cached = ( |
| 86 | + ( empty( $types ) ) && |
| 87 | + ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) && |
| 88 | + ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ) && |
| 89 | + ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) && |
| 90 | + ! is_admin() |
| 91 | + ); |
| 92 | + $transient_name = 'global_styles_' . get_stylesheet(); |
| 93 | + if ( $can_use_cached ) { |
| 94 | + $cached = get_transient( $transient_name ); |
| 95 | + if ( $cached ) { |
| 96 | + return $cached; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + $supports_theme_json = WP_Theme_JSON_Resolver::theme_has_support(); |
| 101 | + $supports_link_color = get_theme_support( 'experimental-link-color' ); |
| 102 | + if ( empty( $types ) && ! $supports_theme_json ) { |
| 103 | + $types = array( 'variables', 'presets' ); |
| 104 | + } elseif ( empty( $types ) ) { |
| 105 | + $types = array( 'variables', 'styles', 'presets' ); |
| 106 | + } |
| 107 | + |
| 108 | + $origins = array( 'core', 'theme', 'user' ); |
| 109 | + if ( ! $supports_theme_json && ! $supports_link_color ) { |
| 110 | + // In this case we only enqueue the core presets (CSS Custom Properties + the classes). |
| 111 | + $origins = array( 'core' ); |
| 112 | + } elseif ( ! $supports_theme_json && $supports_link_color ) { |
| 113 | + // For the legacy link color feature to work, the CSS Custom Properties |
| 114 | + // should be in scope (either the core or the theme ones). |
| 115 | + $origins = array( 'core', 'theme' ); |
| 116 | + } |
| 117 | + |
| 118 | + $tree = WP_Theme_JSON_Resolver::get_merged_data(); |
| 119 | + $stylesheet = $tree->get_stylesheet( $types, $origins ); |
| 120 | + |
| 121 | + if ( $can_use_cached ) { |
| 122 | + // Cache for a minute. |
| 123 | + // This cache doesn't need to be any longer, we only want to avoid spikes on high-traffic sites. |
| 124 | + set_transient( $transient_name, $stylesheet, MINUTE_IN_SECONDS ); |
| 125 | + } |
| 126 | + |
| 127 | + return $stylesheet; |
| 128 | +} |
0 commit comments