Skip to content

Commit

Permalink
Add: Public functions for interacting with global styles & settings.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jorgefilipecosta committed Nov 8, 2021
1 parent 8dfb59d commit d8fe393
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 36 deletions.
1 change: 1 addition & 0 deletions wp-admin/load-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
37 changes: 27 additions & 10 deletions wp-includes/block-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
128 changes: 128 additions & 0 deletions wp-includes/global-styles-and-settings.php
Original file line number Diff line number Diff line change
@@ -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;
}
26 changes: 1 addition & 25 deletions wp-includes/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion wp-includes/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions wp-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit d8fe393

Please sign in to comment.