Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazily load Duotone settings only when needed. #4831

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/wp-includes/block-supports/duotone.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@
)
);

// 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 );

// Add classnames to blocks using duotone support.
add_filter( 'render_block', array( 'WP_Duotone', 'render_duotone_support' ), 10, 2 );

Expand Down
71 changes: 55 additions & 16 deletions src/wp-includes/class-wp-duotone.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class WP_Duotone {
*
* @var array
*/
private static $global_styles_block_names = array();
private static $global_styles_block_names;

/**
* An array of duotone filter data from global, theme, and custom presets.
Expand All @@ -78,7 +78,7 @@ class WP_Duotone {
*
* @var array
*/
private static $global_styles_presets = array();
private static $global_styles_presets;

/**
* All of the duotone filter data from presets for CSS custom properties on
Expand Down Expand Up @@ -575,7 +575,7 @@ private static function is_preset( $duotone_attr ) {
$slug = self::get_slug_from_attribute( $duotone_attr );
$filter_id = self::get_filter_id( $slug );

return array_key_exists( $filter_id, self::$global_styles_presets );
return array_key_exists( $filter_id, self::_get_global_styles_presets() );
}

/**
Expand Down Expand Up @@ -897,7 +897,8 @@ private static function enqueue_custom_filter( $filter_id, $duotone_selector, $f
* @param string $filter_value The filter CSS value. e.g. 'url(#wp-duotone-blue-orange)' or 'unset'.
*/
private static function enqueue_global_styles_preset( $filter_id, $duotone_selector, $filter_value ) {
if ( ! array_key_exists( $filter_id, self::$global_styles_presets ) ) {
$global_styles_presets = self::_get_global_styles_presets();
if ( ! array_key_exists( $filter_id, $global_styles_presets ) ) {
$error_message = sprintf(
/* translators: %s: duotone filter ID */
__( 'The duotone id "%s" is not registered in theme.json settings' ),
Expand All @@ -906,8 +907,8 @@ private static function enqueue_global_styles_preset( $filter_id, $duotone_selec
_doing_it_wrong( __METHOD__, $error_message, '6.3.0' );
return;
}
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 ] );
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 ] );
}

/**
Expand Down Expand Up @@ -1003,6 +1004,7 @@ public static function set_global_styles_presets() {
$tree = wp_get_global_settings();
$presets_by_origin = _wp_array_get( $tree, array( 'color', 'duotone' ), array() );

self::$global_styles_presets = array();
foreach ( $presets_by_origin as $presets ) {
foreach ( $presets as $preset ) {
$filter_id = self::get_filter_id( _wp_to_kebab_case( $preset['slug'] ) );
Expand All @@ -1012,6 +1014,21 @@ public static function set_global_styles_presets() {
}
}

/**
* Get global styles presets.
*
* @since 6.3.0
*
* @return array
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
*/
private static function _get_global_styles_presets() {
if ( ! isset( self::$global_styles_presets ) ) {
self::set_global_styles_presets();
}

return self::$global_styles_presets;
}

/**
* Scrape all block names from global styles and store in self::$global_styles_block_names.
*
Expand All @@ -1026,6 +1043,8 @@ public static function set_global_style_block_names() {
$block_nodes = $tree->get_styles_block_nodes();
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
$theme_json = $tree->get_raw_data();

self::$global_styles_block_names = array();

foreach ( $block_nodes as $block_node ) {
// This block definition doesn't include any duotone settings. Skip it.
if ( empty( $block_node['duotone'] ) ) {
Expand All @@ -1048,6 +1067,20 @@ public static function set_global_style_block_names() {
}
}

/**
* Get global style block names.
*
* @since 6.3.0
*
* @return array
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
*/
private static function _get_global_style_block_names() {
if ( ! isset( self::$global_styles_block_names ) ) {
self::set_global_style_block_names();
}
return self::$global_styles_block_names;
}

/**
* Render out the duotone CSS styles and SVG.
*
Expand All @@ -1061,17 +1094,22 @@ public static function set_global_style_block_names() {
* @return string Filtered block content.
*/
public static function render_duotone_support( $block_content, $block ) {
if ( empty( $block_content ) ) {
return $block_content;
}
$duotone_selector = self::get_selector( $block['blockName'] );

if ( ! $duotone_selector ) {
return $block_content;
}

$global_styles_block_names = self::_get_global_style_block_names();

// The block should have a duotone attribute or have duotone defined in its theme.json to be processed.
$has_duotone_attribute = isset( $block['attrs']['style']['color']['duotone'] );
$has_global_styles_duotone = array_key_exists( $block['blockName'], self::$global_styles_block_names );
$has_global_styles_duotone = array_key_exists( $block['blockName'], $global_styles_block_names );

if (
empty( $block_content ) ||
! $duotone_selector ||
( ! $has_duotone_attribute && ! $has_global_styles_duotone )
) {
if ( ! $has_duotone_attribute && ! $has_global_styles_duotone ) {
return $block_content;
}

Expand Down Expand Up @@ -1119,7 +1157,7 @@ public static function render_duotone_support( $block_content, $block ) {
self::enqueue_custom_filter( $filter_id, $duotone_selector, $filter_value, $filter_data );
}
} elseif ( $has_global_styles_duotone ) {
$slug = self::$global_styles_block_names[ $block['blockName'] ]; // e.g. 'blue-orange'.
$slug = $global_styles_block_names[ $block['blockName'] ]; // e.g. 'blue-orange'.
$filter_id = self::get_filter_id( $slug ); // e.g. 'wp-duotone-filter-blue-orange'.
$filter_value = self::get_css_var( $slug ); // e.g. 'var(--wp--preset--duotone--blue-orange)'.

Expand Down Expand Up @@ -1198,14 +1236,15 @@ public static function output_footer_assets() {
* @return array The editor settings with duotone SVGs and CSS custom properties.
*/
public static function add_editor_settings( $settings ) {
if ( ! empty( self::$global_styles_presets ) ) {
$global_styles_presets = self::_get_global_styles_presets();
if ( ! empty( $global_styles_presets ) ) {
if ( ! isset( $settings['styles'] ) ) {
$settings['styles'] = array();
}

$settings['styles'][] = array(
// For the editor we can add all of the presets by default.
'assets' => self::get_svg_definitions( self::$global_styles_presets ),
'assets' => self::get_svg_definitions( $global_styles_presets ),
// The 'svgs' type is new in 6.3 and requires the corresponding JS changes in the EditorStyles component to work.
'__unstableType' => 'svgs',
// These styles not generated by global styles, so this must be false or they will be stripped out in wp_get_block_editor_settings.
Expand All @@ -1214,7 +1253,7 @@ public static function add_editor_settings( $settings ) {

$settings['styles'][] = array(
// For the editor we can add all of the presets by default.
'css' => self::get_global_styles_presets( self::$global_styles_presets ),
'css' => self::get_global_styles_presets( $global_styles_presets ),
// This must be set and must be something other than 'theme' or they will be stripped out in the post editor <Editor> component.
'__unstableType' => 'presets',
// 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.
Expand Down