Skip to content

Commit 3a0e7f2

Browse files
committed
Add new global styles functions and update usage
1 parent 79c2858 commit 3a0e7f2

5 files changed

Lines changed: 158 additions & 35 deletions

File tree

src/wp-admin/load-styles.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
require ABSPATH . 'wp-admin/includes/noop.php';
1919
require ABSPATH . WPINC . '/theme.php';
2020
require ABSPATH . WPINC . '/class-wp-theme-json-resolver.php';
21+
require ABSPATH . WPINC . '/global-styles-and-settings.php';
2122
require ABSPATH . WPINC . '/script-loader.php';
2223
require ABSPATH . WPINC . '/version.php';
2324

src/wp-includes/block-editor.php

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -291,21 +291,38 @@ function get_block_editor_settings( array $custom_settings, $block_editor_contex
291291
$custom_settings
292292
);
293293

294-
$theme_json = WP_Theme_JSON_Resolver::get_merged_data();
294+
$presets = array(
295+
array(
296+
'css' => 'variables',
297+
'__unstableType' => 'presets',
298+
'__experimentalNoWrapper' => true,
299+
),
300+
array(
301+
'css' => 'presets',
302+
'__unstableType' => 'presets',
303+
),
304+
);
305+
foreach ( $presets as $preset_style ) {
306+
$actual_css = wp_get_global_stylesheet( array( $preset_style['css'] ) );
307+
if ( '' !== $actual_css ) {
308+
$preset_style['css'] = $actual_css;
309+
$editor_settings['styles'][] = $preset_style;
310+
}
311+
}
295312

296313
if ( WP_Theme_JSON_Resolver::theme_has_support() ) {
297-
$editor_settings['styles'][] = array(
298-
'css' => $theme_json->get_stylesheet( array( 'styles', 'presets' ) ),
299-
'__unstableType' => 'globalStyles',
300-
);
301-
$editor_settings['styles'][] = array(
302-
'css' => $theme_json->get_stylesheet( array( 'variables' ) ),
303-
'__experimentalNoWrapper' => true,
304-
'__unstableType' => 'globalStyles',
314+
$block_classes = array(
315+
'css' => 'styles',
316+
'__unstableType' => 'theme',
305317
);
318+
$actual_css = wp_get_global_stylesheet( array( $block_classes['css'] ) );
319+
if ( '' !== $actual_css ) {
320+
$block_classes['css'] = $actual_css;
321+
$editor_settings['styles'][] = $block_classes;
322+
}
306323
}
307324

308-
$editor_settings['__experimentalFeatures'] = $theme_json->get_settings();
325+
$editor_settings['__experimentalFeatures'] = wp_get_global_settings();
309326
// These settings may need to be updated based on data coming from theme.json sources.
310327
if ( isset( $editor_settings['__experimentalFeatures']['color']['palette'] ) ) {
311328
$colors_by_origin = $editor_settings['__experimentalFeatures']['color']['palette'];
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
}

src/wp-includes/script-loader.php

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2303,31 +2303,7 @@ function wp_enqueue_global_styles() {
23032303
return;
23042304
}
23052305

2306-
$can_use_cache = (
2307-
( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) &&
2308-
( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ) &&
2309-
( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) &&
2310-
! is_admin()
2311-
);
2312-
2313-
$stylesheet = null;
2314-
$transient_name = 'global_styles_' . get_stylesheet();
2315-
2316-
if ( $can_use_cache ) {
2317-
$cache = get_transient( $transient_name );
2318-
if ( $cache ) {
2319-
$stylesheet = $cache;
2320-
}
2321-
}
2322-
2323-
if ( null === $stylesheet ) {
2324-
$theme_json = WP_Theme_JSON_Resolver::get_merged_data();
2325-
$stylesheet = $theme_json->get_stylesheet();
2326-
2327-
if ( $can_use_cache ) {
2328-
set_transient( $transient_name, $stylesheet, MINUTE_IN_SECONDS );
2329-
}
2330-
}
2306+
$stylesheet = wp_get_global_stylesheet();
23312307

23322308
if ( empty( $stylesheet ) ) {
23332309
return;

src/wp-settings.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@
173173
require ABSPATH . WPINC . '/class-wp-theme-json-schema.php';
174174
require ABSPATH . WPINC . '/class-wp-theme-json.php';
175175
require ABSPATH . WPINC . '/class-wp-theme-json-resolver.php';
176+
require ABSPATH . WPINC . '/global-styles-and-settings.php';
176177
require ABSPATH . WPINC . '/class-wp-block-template.php';
177178
require ABSPATH . WPINC . '/block-template-utils.php';
178179
require ABSPATH . WPINC . '/block-template.php';

0 commit comments

Comments
 (0)