<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[52054] trunk/src: Add: Public functions for interacting with global styles & settings.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/52054">52054</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/52054","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>jorgefilipecosta</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2021-11-08 21:44:12 +0000 (Mon, 08 Nov 2021)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Add: Public functions for interacting with global styles & settings.
This commit ports the public functions to interact with the global styles & settings.
See <a href="https://core.trac.wordpress.org/ticket/54336">#54336</a>.
Props oandregal.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminloadstylesphp">trunk/src/wp-admin/load-styles.php</a></li>
<li><a href="#trunksrcwpincludesblockeditorphp">trunk/src/wp-includes/block-editor.php</a></li>
<li><a href="#trunksrcwpincludesscriptloaderphp">trunk/src/wp-includes/script-loader.php</a></li>
<li><a href="#trunksrcwpsettingsphp">trunk/src/wp-settings.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesglobalstylesandsettingsphp">trunk/src/wp-includes/global-styles-and-settings.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminloadstylesphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/load-styles.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/load-styles.php 2021-11-08 21:41:43 UTC (rev 52053)
+++ trunk/src/wp-admin/load-styles.php 2021-11-08 21:44:12 UTC (rev 52054)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -18,6 +18,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . 'wp-admin/includes/noop.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/theme.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/class-wp-theme-json-resolver.php';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+require ABSPATH . WPINC . '/global-styles-and-settings.php';
</ins><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/script-loader.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/version.php';
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunksrcwpincludesblockeditorphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/block-editor.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/block-editor.php 2021-11-08 21:41:43 UTC (rev 52053)
+++ trunk/src/wp-includes/block-editor.php 2021-11-08 21:44:12 UTC (rev 52054)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -303,21 +303,38 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $custom_settings
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $theme_json = WP_Theme_JSON_Resolver::get_merged_data();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $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;
+ }
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( WP_Theme_JSON_Resolver::theme_has_support() ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $editor_settings['styles'][] = array(
- 'css' => $theme_json->get_stylesheet( array( 'styles', 'presets' ) ),
- '__unstableType' => 'globalStyles',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $block_classes = array(
+ 'css' => 'styles',
+ '__unstableType' => 'theme',
</ins><span class="cx" style="display: block; padding: 0 10px"> );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $editor_settings['styles'][] = array(
- 'css' => $theme_json->get_stylesheet( array( 'variables' ) ),
- '__experimentalNoWrapper' => true,
- '__unstableType' => 'globalStyles',
- );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $actual_css = wp_get_global_stylesheet( array( $block_classes['css'] ) );
+ if ( '' !== $actual_css ) {
+ $block_classes['css'] = $actual_css;
+ $editor_settings['styles'][] = $block_classes;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $editor_settings['__experimentalFeatures'] = $theme_json->get_settings();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $editor_settings['__experimentalFeatures'] = wp_get_global_settings();
</ins><span class="cx" style="display: block; padding: 0 10px"> // These settings may need to be updated based on data coming from theme.json sources.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( isset( $editor_settings['__experimentalFeatures']['color']['palette'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $colors_by_origin = $editor_settings['__experimentalFeatures']['color']['palette'];
</span></span></pre></div>
<a id="trunksrcwpincludesglobalstylesandsettingsphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/src/wp-includes/global-styles-and-settings.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/global-styles-and-settings.php (rev 0)
+++ trunk/src/wp-includes/global-styles-and-settings.php 2021-11-08 21:44:12 UTC (rev 52054)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,128 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?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;
+}
</ins></span></pre></div>
<a id="trunksrcwpincludesscriptloaderphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/script-loader.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/script-loader.php 2021-11-08 21:41:43 UTC (rev 52053)
+++ trunk/src/wp-includes/script-loader.php 2021-11-08 21:44:12 UTC (rev 52054)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2303,32 +2303,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $can_use_cache = (
- ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) &&
- ( ! defined( 'SCRIPT_DEBUG' ) || ! SCRIPT_DEBUG ) &&
- ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) &&
- ! is_admin()
- );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $stylesheet = wp_get_global_stylesheet();
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $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 );
- }
- }
-
</del><span class="cx" style="display: block; padding: 0 10px"> if ( empty( $stylesheet ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> return;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunksrcwpsettingsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-settings.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-settings.php 2021-11-08 21:41:43 UTC (rev 52053)
+++ trunk/src/wp-settings.php 2021-11-08 21:44:12 UTC (rev 52054)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -173,6 +173,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/class-wp-theme-json-schema.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/class-wp-theme-json.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/class-wp-theme-json-resolver.php';
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+require ABSPATH . WPINC . '/global-styles-and-settings.php';
</ins><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/class-wp-block-template.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/block-template-utils.php';
</span><span class="cx" style="display: block; padding: 0 10px"> require ABSPATH . WPINC . '/block-template.php';
</span></span></pre>
</div>
</div>
</body>
</html>