From d8fe393934ed41949c2061db00e0136f13d913d7 Mon Sep 17 00:00:00 2001 From: jorgefilipecosta Date: Mon, 8 Nov 2021 21:45:58 +0000 Subject: [PATCH] Add: Public functions for interacting with global styles & settings. This commit ports the public functions to interact with the global styles & settings. See #54336. Props oandregal. Built from https://develop.svn.wordpress.org/trunk@52054 git-svn-id: http://core.svn.wordpress.org/trunk@51646 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/load-styles.php | 1 + wp-includes/block-editor.php | 37 ++++-- wp-includes/global-styles-and-settings.php | 128 +++++++++++++++++++++ wp-includes/script-loader.php | 26 +---- wp-includes/version.php | 2 +- wp-settings.php | 1 + 6 files changed, 159 insertions(+), 36 deletions(-) create mode 100644 wp-includes/global-styles-and-settings.php diff --git a/wp-admin/load-styles.php b/wp-admin/load-styles.php index 18594fde6897..84a7b729d5da 100644 --- a/wp-admin/load-styles.php +++ b/wp-admin/load-styles.php @@ -18,6 +18,7 @@ require ABSPATH . 'wp-admin/includes/noop.php'; require ABSPATH . WPINC . '/theme.php'; require ABSPATH . WPINC . '/class-wp-theme-json-resolver.php'; +require ABSPATH . WPINC . '/global-styles-and-settings.php'; require ABSPATH . WPINC . '/script-loader.php'; require ABSPATH . WPINC . '/version.php'; diff --git a/wp-includes/block-editor.php b/wp-includes/block-editor.php index 96339d5e626c..9edaa4962465 100644 --- a/wp-includes/block-editor.php +++ b/wp-includes/block-editor.php @@ -303,21 +303,38 @@ function get_block_editor_settings( array $custom_settings, $block_editor_contex $custom_settings ); - $theme_json = WP_Theme_JSON_Resolver::get_merged_data(); + $presets = array( + array( + 'css' => 'variables', + '__unstableType' => 'presets', + '__experimentalNoWrapper' => true, + ), + array( + 'css' => 'presets', + '__unstableType' => 'presets', + ), + ); + foreach ( $presets as $preset_style ) { + $actual_css = wp_get_global_stylesheet( array( $preset_style['css'] ) ); + if ( '' !== $actual_css ) { + $preset_style['css'] = $actual_css; + $editor_settings['styles'][] = $preset_style; + } + } if ( WP_Theme_JSON_Resolver::theme_has_support() ) { - $editor_settings['styles'][] = array( - 'css' => $theme_json->get_stylesheet( array( 'styles', 'presets' ) ), - '__unstableType' => 'globalStyles', - ); - $editor_settings['styles'][] = array( - 'css' => $theme_json->get_stylesheet( array( 'variables' ) ), - '__experimentalNoWrapper' => true, - '__unstableType' => 'globalStyles', + $block_classes = array( + 'css' => 'styles', + '__unstableType' => 'theme', ); + $actual_css = wp_get_global_stylesheet( array( $block_classes['css'] ) ); + if ( '' !== $actual_css ) { + $block_classes['css'] = $actual_css; + $editor_settings['styles'][] = $block_classes; + } } - $editor_settings['__experimentalFeatures'] = $theme_json->get_settings(); + $editor_settings['__experimentalFeatures'] = wp_get_global_settings(); // These settings may need to be updated based on data coming from theme.json sources. if ( isset( $editor_settings['__experimentalFeatures']['color']['palette'] ) ) { $colors_by_origin = $editor_settings['__experimentalFeatures']['color']['palette']; diff --git a/wp-includes/global-styles-and-settings.php b/wp-includes/global-styles-and-settings.php new file mode 100644 index 000000000000..ad4e87a45c01 --- /dev/null +++ b/wp-includes/global-styles-and-settings.php @@ -0,0 +1,128 @@ +get_settings(); + + return _wp_array_get( $settings, $path, $settings ); +} + +/** + * Function to get the styles resulting of merging core, theme, and user data. + * + * @since 5.9.0 + * + * @param array $path Path to the specific style to retrieve. Optional. + * If empty, will return all styles. + * @param string $block_name Which block to retrieve the styles from. Optional. + * If empty, it'll return the styles for the global context. + * @param string $origin Which origin to take data from. Optional. + * It can be 'all' (core, theme, and user) or 'base' (core and theme). + * If empty or unknown, 'all' is used. + * + * @return array The styles to retrieve. + */ +function wp_get_global_styles( $path = array(), $block_name = '', $origin = 'all' ) { + if ( '' !== $block_name ) { + $path = array_merge( array( 'blocks', $block_name ), $path ); + } + + if ( 'base' === $origin ) { + $origin = 'theme'; + } else { + $origin = 'user'; + } + + $styles = WP_Theme_JSON_Resolver::get_merged_data( $origin )->get_raw_data()['styles']; + + return _wp_array_get( $styles, $path, $styles ); +} + +/** + * Returns the stylesheet resulting of merging core, theme, and user data. + * + * @since 5.9.0 + * + * @param array $types Types of styles to load. Optional. + * It accepts 'variables', 'styles', 'presets' as values. + * If empty, it'll load all for themes with theme.json support + * and only [ 'variables', 'presets' ] for themes without theme.json support. + * + * @return string Stylesheet. + */ +function wp_get_global_stylesheet( $types = array() ) { + // Return cached value if it can be used and exists. + // It's cached by theme to make sure that theme switching clears the cache. + $can_use_cached = ( + ( empty( $types ) ) && + ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) && + ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ) && + ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) && + ! is_admin() + ); + $transient_name = 'global_styles_' . get_stylesheet(); + if ( $can_use_cached ) { + $cached = get_transient( $transient_name ); + if ( $cached ) { + return $cached; + } + } + + $supports_theme_json = WP_Theme_JSON_Resolver::theme_has_support(); + $supports_link_color = get_theme_support( 'experimental-link-color' ); + if ( empty( $types ) && ! $supports_theme_json ) { + $types = array( 'variables', 'presets' ); + } elseif ( empty( $types ) ) { + $types = array( 'variables', 'styles', 'presets' ); + } + + $origins = array( 'core', 'theme', 'user' ); + if ( ! $supports_theme_json && ! $supports_link_color ) { + // In this case we only enqueue the core presets (CSS Custom Properties + the classes). + $origins = array( 'core' ); + } elseif ( ! $supports_theme_json && $supports_link_color ) { + // For the legacy link color feature to work, the CSS Custom Properties + // should be in scope (either the core or the theme ones). + $origins = array( 'core', 'theme' ); + } + + $tree = WP_Theme_JSON_Resolver::get_merged_data(); + $stylesheet = $tree->get_stylesheet( $types, $origins ); + + if ( $can_use_cached ) { + // Cache for a minute. + // This cache doesn't need to be any longer, we only want to avoid spikes on high-traffic sites. + set_transient( $transient_name, $stylesheet, MINUTE_IN_SECONDS ); + } + + return $stylesheet; +} diff --git a/wp-includes/script-loader.php b/wp-includes/script-loader.php index 5ab358facbd7..f33eb113bbd7 100644 --- a/wp-includes/script-loader.php +++ b/wp-includes/script-loader.php @@ -2303,31 +2303,7 @@ function wp_enqueue_global_styles() { return; } - $can_use_cache = ( - ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) && - ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ) && - ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) && - ! is_admin() - ); - - $stylesheet = null; - $transient_name = 'global_styles_' . get_stylesheet(); - - if ( $can_use_cache ) { - $cache = get_transient( $transient_name ); - if ( $cache ) { - $stylesheet = $cache; - } - } - - if ( null === $stylesheet ) { - $theme_json = WP_Theme_JSON_Resolver::get_merged_data(); - $stylesheet = $theme_json->get_stylesheet(); - - if ( $can_use_cache ) { - set_transient( $transient_name, $stylesheet, MINUTE_IN_SECONDS ); - } - } + $stylesheet = wp_get_global_stylesheet(); if ( empty( $stylesheet ) ) { return; diff --git a/wp-includes/version.php b/wp-includes/version.php index 19e0044505f0..21d340d3917a 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '5.9-alpha-52053'; +$wp_version = '5.9-alpha-52054'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. diff --git a/wp-settings.php b/wp-settings.php index d3ce78c31dff..9f027ee645f0 100644 --- a/wp-settings.php +++ b/wp-settings.php @@ -173,6 +173,7 @@ require ABSPATH . WPINC . '/class-wp-theme-json-schema.php'; require ABSPATH . WPINC . '/class-wp-theme-json.php'; require ABSPATH . WPINC . '/class-wp-theme-json-resolver.php'; +require ABSPATH . WPINC . '/global-styles-and-settings.php'; require ABSPATH . WPINC . '/class-wp-block-template.php'; require ABSPATH . WPINC . '/block-template-utils.php'; require ABSPATH . WPINC . '/block-template.php';