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
This commit is contained in:
jorgefilipecosta 2021-11-08 21:45:58 +00:00
parent 8dfb59d4de
commit d8fe393934
6 changed files with 161 additions and 38 deletions

View File

@ -18,6 +18,7 @@ define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );
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';

View File

@ -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();
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' ) ),
$presets = array(
array(
'css' => 'variables',
'__unstableType' => 'presets',
'__experimentalNoWrapper' => true,
'__unstableType' => 'globalStyles',
);
),
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;
}
}
$editor_settings['__experimentalFeatures'] = $theme_json->get_settings();
if ( WP_Theme_JSON_Resolver::theme_has_support() ) {
$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'] = 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'];

View File

@ -0,0 +1,128 @@
<?php
/**
* APIs to interact with global settings & styles.
*
* @package WordPress
*/
/**
* Function to get the settings resulting of merging core, theme, and user data.
*
* @since 5.9.0
*
* @param array $path Path to the specific setting to retrieve. Optional.
* If empty, will return all settings.
* @param string $block_name Which block to retrieve the settings from. Optional
* If empty, it'll return the settings 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 settings to retrieve.
*/
function wp_get_global_settings( $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';
}
$settings = WP_Theme_JSON_Resolver::get_merged_data( $origin )->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;
}

View File

@ -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;

View File

@ -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.

View File

@ -173,6 +173,7 @@ require ABSPATH . WPINC . '/class-wp-theme.php';
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';