<!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>[56226] trunk: Editor: Lazily load Duotone settings only when needed.</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/56226">56226</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/56226","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>spacedmonkey</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-07-13 11:32:19 +0000 (Thu, 13 Jul 2023)</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'>Editor: Lazily load Duotone settings only when needed.
Introduced in <a href="https://core.trac.wordpress.org/changeset/56101">[56101]</a> the `WP_Duotone` class, hooks into the `wp_loaded` action to load duotone style data from global styles. Hooking in early in the bootstrap process caused a number of problems. This hook, triggered an error on installing, as this lookup for global styles, would result in a global post trying to be created, even before the table existed. Additionally, this implementation caused a severe performance regression, as duotone styling data was loaded unnecessarily for requests that did not require such data, such as REST API calls or actions within the wp-admin interface.
In this change, refactor the `WP_Duotone` to lazily load the global styles and theme.json data, only when a block that supports duotone is encountered. The method `render_duotone_support` was change to take a third parameter to reuse the existing `WP_Block_Type` object passed to the filter, to save it being looked up again. The code has also got improved type checking and the use of the util function `block_has_support`. Furthermore, the code's readability has been improved, along with enhancements to the documentation blocks.
Props Chouby, spacedmonkey, SergeyBiryukov, swissspidy, costdev, joemcgill, flixos90, mukesh27, nazmul111, ajlende, isabel_brison.
Fixes <a href="https://core.trac.wordpress.org/ticket/58673">#58673</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesblocksupportsduotonephp">trunk/src/wp-includes/block-supports/duotone.php</a></li>
<li><a href="#trunksrcwpincludesclasswpduotonephp">trunk/src/wp-includes/class-wp-duotone.php</a></li>
<li><a href="#trunksrcwpincludesdeprecatedphp">trunk/src/wp-includes/deprecated.php</a></li>
<li><a href="#trunktestsphpunittestsblocksupportsduotonephp">trunk/tests/phpunit/tests/block-supports/duotone.php</a></li>
<li><a href="#trunktestsphpunittestsblockssupportedStylesphp">trunk/tests/phpunit/tests/blocks/supportedStyles.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesblocksupportsduotonephp"></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-supports/duotone.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/block-supports/duotone.php 2023-07-13 04:14:27 UTC (rev 56225)
+++ trunk/src/wp-includes/block-supports/duotone.php 2023-07-13 11:32:19 UTC (rev 56226)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -40,12 +40,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> )
</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">-// Set up metadata prior to rendering any blocks.
-add_action( 'wp_loaded', array( 'WP_Duotone', 'set_global_styles_presets' ), 10 );
-add_action( 'wp_loaded', array( 'WP_Duotone', 'set_global_style_block_names' ), 10 );
-
</del><span class="cx" style="display: block; padding: 0 10px"> // Add classnames to blocks using duotone support.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-add_filter( 'render_block', array( 'WP_Duotone', 'render_duotone_support' ), 10, 2 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+add_filter( 'render_block', array( 'WP_Duotone', 'render_duotone_support' ), 10, 3 );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Enqueue styles.
</span><span class="cx" style="display: block; padding: 0 10px"> // Block styles (core-block-supports-inline-css) before the style engine (wp_enqueue_stored_styles).
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpduotonephp"></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/class-wp-duotone.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-duotone.php 2023-07-13 04:14:27 UTC (rev 56225)
+++ trunk/src/wp-includes/class-wp-duotone.php 2023-07-13 11:32:19 UTC (rev 56226)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -54,7 +54,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @var array
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- private static $global_styles_block_names = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ private static $global_styles_block_names;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * An array of duotone filter data from global, theme, and custom presets.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -78,7 +78,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @var array
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- private static $global_styles_presets = array();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ private static $global_styles_presets;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * All of the duotone filter data from presets for CSS custom properties on
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -575,7 +575,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $slug = self::get_slug_from_attribute( $duotone_attr );
</span><span class="cx" style="display: block; padding: 0 10px"> $filter_id = self::get_filter_id( $slug );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return array_key_exists( $filter_id, self::$global_styles_presets );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return array_key_exists( $filter_id, self::get_all_global_styles_presets() );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -897,7 +897,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $filter_value The filter CSS value. e.g. 'url(#wp-duotone-blue-orange)' or 'unset'.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> private static function enqueue_global_styles_preset( $filter_id, $duotone_selector, $filter_value ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! array_key_exists( $filter_id, self::$global_styles_presets ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $global_styles_presets = self::get_all_global_styles_presets();
+ if ( ! array_key_exists( $filter_id, $global_styles_presets ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $error_message = sprintf(
</span><span class="cx" style="display: block; padding: 0 10px"> /* translators: %s: duotone filter ID */
</span><span class="cx" style="display: block; padding: 0 10px"> __( 'The duotone id "%s" is not registered in theme.json settings' ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -906,8 +907,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> _doing_it_wrong( __METHOD__, $error_message, '6.3.0' );
</span><span class="cx" style="display: block; padding: 0 10px"> return;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- self::$used_global_styles_presets[ $filter_id ] = self::$global_styles_presets[ $filter_id ];
- self::enqueue_custom_filter( $filter_id, $duotone_selector, $filter_value, self::$global_styles_presets[ $filter_id ] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ self::$used_global_styles_presets[ $filter_id ] = $global_styles_presets[ $filter_id ];
+ self::enqueue_custom_filter( $filter_id, $duotone_selector, $filter_value, $global_styles_presets[ $filter_id ] );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -920,16 +921,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param WP_Block_Type $block_type Block Type.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public static function register_duotone_support( $block_type ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $has_duotone_support = false;
- if ( property_exists( $block_type, 'supports' ) ) {
- /*
- * Previous `color.__experimentalDuotone` support flag is migrated
- * to `filter.duotone` via `block_type_metadata_settings` filter.
- */
- $has_duotone_support = _wp_array_get( $block_type->supports, array( 'filter', 'duotone' ), null );
- }
-
- if ( $has_duotone_support ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /*
+ * Previous `color.__experimentalDuotone` support flag is migrated
+ * to `filter.duotone` via `block_type_metadata_settings` filter.
+ */
+ if ( block_has_support( $block_type, array( 'filter', 'duotone' ), null ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! $block_type->attributes ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $block_type->attributes = array();
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -948,45 +944,43 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * This handles selectors defined in `color.__experimentalDuotone` support
</span><span class="cx" style="display: block; padding: 0 10px"> * if `filter.duotone` support is not defined.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param string $block_name The block name.
- *
</del><span class="cx" style="display: block; padding: 0 10px"> * @internal
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- *
</del><span class="cx" style="display: block; padding: 0 10px"> * @since 6.3.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return string The CSS selector or null if there is no support.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param WP_Block_Type $block_type Block type to check for support.
+ * @return string|null The CSS selector or null if there is no support.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- private static function get_selector( $block_name ) {
- $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_name );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ private static function get_selector( $block_type ) {
+ if ( ! ( $block_type instanceof WP_Block_Type ) ) {
+ return null;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( $block_type && property_exists( $block_type, 'supports' ) ) {
- /*
- * Backwards compatibility with `supports.color.__experimentalDuotone`
- * is provided via the `block_type_metadata_settings` filter. If
- * `supports.filter.duotone` has not been set and the experimental
- * property has been, the experimental property value is copied into
- * `supports.filter.duotone`.
- */
- $duotone_support = _wp_array_get( $block_type->supports, array( 'filter', 'duotone' ), false );
- if ( ! $duotone_support ) {
- return null;
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /*
+ * Backward compatibility with `supports.color.__experimentalDuotone`
+ * is provided via the `block_type_metadata_settings` filter. If
+ * `supports.filter.duotone` has not been set and the experimental
+ * property has been, the experimental property value is copied into
+ * `supports.filter.duotone`.
+ */
+ $duotone_support = block_has_support( $block_type, array( 'filter', 'duotone' ) );
+ if ( ! $duotone_support ) {
+ return null;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- /*
- * If the experimental duotone support was set, that value is to be
- * treated as a selector and requires scoping.
- */
- $experimental_duotone = _wp_array_get( $block_type->supports, array( 'color', '__experimentalDuotone' ), false );
- if ( $experimental_duotone ) {
- $root_selector = wp_get_block_css_selector( $block_type );
- return is_string( $experimental_duotone )
- ? WP_Theme_JSON::scope_selector( $root_selector, $experimental_duotone )
- : $root_selector;
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /*
+ * If the experimental duotone support was set, that value is to be
+ * treated as a selector and requires scoping.
+ */
+ $experimental_duotone = _wp_array_get( $block_type->supports, array( 'color', '__experimentalDuotone' ), false );
+ if ( $experimental_duotone ) {
+ $root_selector = wp_get_block_css_selector( $block_type );
+ return is_string( $experimental_duotone )
+ ? WP_Theme_JSON::scope_selector( $root_selector, $experimental_duotone )
+ : $root_selector;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Regular filter.duotone support uses filter.duotone selectors with fallbacks.
- return wp_get_block_css_selector( $block_type, array( 'filter', 'duotone' ), true );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Regular filter.duotone support uses filter.duotone selectors with fallbacks.
+ return wp_get_block_css_selector( $block_type, array( 'filter', 'duotone' ), true );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -997,12 +991,18 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * use duotone preset filters.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 6.3.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
+ * @return array An array of global styles presets, keyed on the filter ID.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public static function set_global_styles_presets() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ private static function get_all_global_styles_presets() {
+ if ( isset( self::$global_styles_presets ) ) {
+ return self::$global_styles_presets;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> // Get the per block settings from the theme.json.
</span><span class="cx" style="display: block; padding: 0 10px"> $tree = wp_get_global_settings();
</span><span class="cx" style="display: block; padding: 0 10px"> $presets_by_origin = _wp_array_get( $tree, array( 'color', 'duotone' ), array() );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ self::$global_styles_presets = array();
</ins><span class="cx" style="display: block; padding: 0 10px"> foreach ( $presets_by_origin as $presets ) {
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $presets as $preset ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $filter_id = self::get_filter_id( _wp_to_kebab_case( $preset['slug'] ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1010,6 +1010,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> self::$global_styles_presets[ $filter_id ] = $preset;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ return self::$global_styles_presets;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1019,13 +1021,20 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * duotone filters defined in the theme.json global styles.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 6.3.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ *
+ * @return string[] An array of global style block slugs, keyed on the block name.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public static function set_global_style_block_names() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ private static function get_all_global_style_block_names() {
+ if ( isset( self::$global_styles_block_names ) ) {
+ return self::$global_styles_block_names;
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> // Get the per block settings from the theme.json.
</span><span class="cx" style="display: block; padding: 0 10px"> $tree = WP_Theme_JSON_Resolver::get_merged_data();
</span><span class="cx" style="display: block; padding: 0 10px"> $block_nodes = $tree->get_styles_block_nodes();
</span><span class="cx" style="display: block; padding: 0 10px"> $theme_json = $tree->get_raw_data();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ self::$global_styles_block_names = array();
+
</ins><span class="cx" style="display: block; padding: 0 10px"> foreach ( $block_nodes as $block_node ) {
</span><span class="cx" style="display: block; padding: 0 10px"> // This block definition doesn't include any duotone settings. Skip it.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( empty( $block_node['duotone'] ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1046,6 +1055,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> self::$global_styles_block_names[ $block_node['name'] ] = $slug;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return self::$global_styles_block_names;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1056,22 +1066,28 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 6.3.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param string $block_content Rendered block content.
- * @param array $block Block object.
- * @return string Filtered block content.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param string $block_content Rendered block content.
+ * @param array $block Block object.
+ * @param WP_Block $wp_block The block instance.
+ * @return string Filtered block content.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public static function render_duotone_support( $block_content, $block ) {
- $duotone_selector = self::get_selector( $block['blockName'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public static function render_duotone_support( $block_content, $block, $wp_block ) {
+ if ( empty( $block_content ) || ! $block['blockName'] ) {
+ return $block_content;
+ }
+ $duotone_selector = self::get_selector( $wp_block->block_type );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! $duotone_selector ) {
+ return $block_content;
+ }
+
+ $global_styles_block_names = self::get_all_global_style_block_names();
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // The block should have a duotone attribute or have duotone defined in its theme.json to be processed.
</span><span class="cx" style="display: block; padding: 0 10px"> $has_duotone_attribute = isset( $block['attrs']['style']['color']['duotone'] );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $has_global_styles_duotone = array_key_exists( $block['blockName'], self::$global_styles_block_names );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $has_global_styles_duotone = array_key_exists( $block['blockName'], $global_styles_block_names );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if (
- empty( $block_content ) ||
- ! $duotone_selector ||
- ( ! $has_duotone_attribute && ! $has_global_styles_duotone )
- ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! $has_duotone_attribute && ! $has_global_styles_duotone ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return $block_content;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1119,7 +1135,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> self::enqueue_custom_filter( $filter_id, $duotone_selector, $filter_value, $filter_data );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> } elseif ( $has_global_styles_duotone ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $slug = self::$global_styles_block_names[ $block['blockName'] ]; // e.g. 'blue-orange'.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $slug = $global_styles_block_names[ $block['blockName'] ]; // e.g. 'blue-orange'.
</ins><span class="cx" style="display: block; padding: 0 10px"> $filter_id = self::get_filter_id( $slug ); // e.g. 'wp-duotone-filter-blue-orange'.
</span><span class="cx" style="display: block; padding: 0 10px"> $filter_value = self::get_css_var( $slug ); // e.g. 'var(--wp--preset--duotone--blue-orange)'.
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1206,7 +1222,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return array The editor settings with duotone SVGs and CSS custom properties.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public static function add_editor_settings( $settings ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! empty( self::$global_styles_presets ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $global_styles_presets = self::get_all_global_styles_presets();
+ if ( ! empty( $global_styles_presets ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( ! isset( $settings['styles'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $settings['styles'] = array();
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1213,7 +1230,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $settings['styles'][] = array(
</span><span class="cx" style="display: block; padding: 0 10px"> // For the editor we can add all of the presets by default.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'assets' => self::get_svg_definitions( self::$global_styles_presets ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'assets' => self::get_svg_definitions( $global_styles_presets ),
</ins><span class="cx" style="display: block; padding: 0 10px"> // The 'svgs' type is new in 6.3 and requires the corresponding JS changes in the EditorStyles component to work.
</span><span class="cx" style="display: block; padding: 0 10px"> '__unstableType' => 'svgs',
</span><span class="cx" style="display: block; padding: 0 10px"> // These styles not generated by global styles, so this must be false or they will be stripped out in wp_get_block_editor_settings.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1222,7 +1239,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $settings['styles'][] = array(
</span><span class="cx" style="display: block; padding: 0 10px"> // For the editor we can add all of the presets by default.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'css' => self::get_global_styles_presets( self::$global_styles_presets ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ 'css' => self::get_global_styles_presets( $global_styles_presets ),
</ins><span class="cx" style="display: block; padding: 0 10px"> // This must be set and must be something other than 'theme' or they will be stripped out in the post editor <Editor> component.
</span><span class="cx" style="display: block; padding: 0 10px"> '__unstableType' => 'presets',
</span><span class="cx" style="display: block; padding: 0 10px"> // These styles are no longer generated by global styles, so this must be false or they will be stripped out in wp_get_block_editor_settings.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1242,7 +1259,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $settings Current block type settings.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array $metadata Block metadata as read in via block.json.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- *
</del><span class="cx" style="display: block; padding: 0 10px"> * @return array Filtered block type settings.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public static function migrate_experimental_duotone_support_flag( $settings, $metadata ) {
</span></span></pre></div>
<a id="trunksrcwpincludesdeprecatedphp"></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/deprecated.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/deprecated.php 2023-07-13 04:14:27 UTC (rev 56225)
+++ trunk/src/wp-includes/deprecated.php 2023-07-13 11:32:19 UTC (rev 56226)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5215,7 +5215,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_render_duotone_support( $block_content, $block ) {
</span><span class="cx" style="display: block; padding: 0 10px"> _deprecated_function( __FUNCTION__, '6.3.0', 'WP_Duotone::render_duotone_support()' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return WP_Duotone::render_duotone_support( $block_content, $block );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $wp_block = new WP_Block( $block );
+ return WP_Duotone::render_duotone_support( $block_content, $block, $wp_block );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="trunktestsphpunittestsblocksupportsduotonephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/block-supports/duotone.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/block-supports/duotone.php 2023-07-13 04:14:27 UTC (rev 56225)
+++ trunk/tests/phpunit/tests/block-supports/duotone.php 2023-07-13 11:32:19 UTC (rev 56226)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -27,9 +27,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'blockName' => 'core/image',
</span><span class="cx" style="display: block; padding: 0 10px"> 'attrs' => array( 'style' => array( 'color' => array( 'duotone' => 'var:preset|duotone|blue-orange' ) ) ),
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $wp_block = new WP_Block( $block );
</ins><span class="cx" style="display: block; padding: 0 10px"> $block_content = '<figure class="wp-block-image size-full"><img src="/my-image.jpg" /></figure>';
</span><span class="cx" style="display: block; padding: 0 10px"> $expected = '<figure class="wp-block-image size-full wp-duotone-blue-orange"><img src="/my-image.jpg" /></figure>';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertSame( $expected, WP_Duotone::render_duotone_support( $block_content, $block ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertSame( $expected, WP_Duotone::render_duotone_support( $block_content, $block, $wp_block ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -44,9 +45,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'blockName' => 'core/image',
</span><span class="cx" style="display: block; padding: 0 10px"> 'attrs' => array( 'style' => array( 'color' => array( 'duotone' => 'unset' ) ) ),
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $wp_block = new WP_Block( $block );
</ins><span class="cx" style="display: block; padding: 0 10px"> $block_content = '<figure class="wp-block-image size-full"><img src="/my-image.jpg" /></figure>';
</span><span class="cx" style="display: block; padding: 0 10px"> $expected = '/<figure class="wp-block-image size-full wp-duotone-unset-\d+"><img src="\\/my-image.jpg" \\/><\\/figure>/';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertMatchesRegularExpression( $expected, WP_Duotone::render_duotone_support( $block_content, $block ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertMatchesRegularExpression( $expected, WP_Duotone::render_duotone_support( $block_content, $block, $wp_block ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -59,9 +61,10 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'blockName' => 'core/image',
</span><span class="cx" style="display: block; padding: 0 10px"> 'attrs' => array( 'style' => array( 'color' => array( 'duotone' => array( '#FFFFFF', '#000000' ) ) ) ),
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $wp_block = new WP_Block( $block );
</ins><span class="cx" style="display: block; padding: 0 10px"> $block_content = '<figure class="wp-block-image size-full"><img src="/my-image.jpg" /></figure>';
</span><span class="cx" style="display: block; padding: 0 10px"> $expected = '/<figure class="wp-block-image size-full wp-duotone-ffffff-000000-\d+"><img src="\\/my-image.jpg" \\/><\\/figure>/';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertMatchesRegularExpression( $expected, WP_Duotone::render_duotone_support( $block_content, $block ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertMatchesRegularExpression( $expected, WP_Duotone::render_duotone_support( $block_content, $block, $wp_block ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="trunktestsphpunittestsblockssupportedStylesphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/blocks/supportedStyles.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/blocks/supportedStyles.php 2023-07-13 04:14:27 UTC (rev 56225)
+++ trunk/tests/phpunit/tests/blocks/supportedStyles.php 2023-07-13 11:32:19 UTC (rev 56226)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -695,7 +695,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->register_block_type( 'core/example', $block_type_settings );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $block = array(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $block = array(
</ins><span class="cx" style="display: block; padding: 0 10px"> 'blockName' => 'core/example',
</span><span class="cx" style="display: block; padding: 0 10px"> 'attrs' => array(),
</span><span class="cx" style="display: block; padding: 0 10px"> 'innerBlock' => array(),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -702,6 +702,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'innerContent' => array(),
</span><span class="cx" style="display: block; padding: 0 10px"> 'innerHTML' => array(),
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $wp_block = new WP_Block( $block );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Custom error handler's see Warnings even if they are suppressed by the @ symbol.
</span><span class="cx" style="display: block; padding: 0 10px"> $errors = array();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -714,7 +715,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // HTML5 elements like <time> are not supported by the DOMDocument parser used by the block supports feature.
</span><span class="cx" style="display: block; padding: 0 10px"> // This specific example is emitted by the "Display post date" setting in the latest-posts block.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- apply_filters( 'render_block', '<div><time datetime="2020-06-18T04:01:43+10:00" class="wp-block-latest-posts__post-date">June 18, 2020</time></div>', $block );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ apply_filters( 'render_block', '<div><time datetime="2020-06-18T04:01:43+10:00" class="wp-block-latest-posts__post-date">June 18, 2020</time></div>', $block, $wp_block );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> restore_error_handler();
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre>
</div>
</div>
</body>
</html>