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

Duotone: Only output Duotone filters that are used #48291

Closed
wants to merge 12 commits into from
19 changes: 17 additions & 2 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,11 @@ public static function get_element_class_name( $element ) {
*/
const LATEST_SCHEMA = 2;

/**
* An array of Duotone presets.
*/
static $duotone_presets;

/**
* Constructor.
*
Expand Down Expand Up @@ -2266,6 +2271,11 @@ private static function get_block_nodes( $theme_json, $selectors = array() ) {
return $nodes;
}

private function gutenberg_get_preset_slug_from_declaration( $declaration ) {
preg_match('/var\(--wp--preset--duotone--(.*)\)/', $declaration['value'], $matches );
return $matches[1];
}

/**
* Gets the CSS rules for a particular block from theme.json.
*
Expand Down Expand Up @@ -2425,8 +2435,10 @@ function( $pseudo_selector ) use ( $selector ) {
$declarations_duotone = array();
foreach ( $declarations as $index => $declaration ) {
if ( 'filter' === $declaration['name'] ) {
unset( $declarations[ $index ] );
$filter_preset_slug = $this->gutenberg_get_preset_slug_from_declaration( $declarations[ $index ] );
self::$duotone_presets[] = $filter_preset_slug;
$declarations_duotone[] = $declaration;
unset( $declarations[ $index ] );
}
}

Expand Down Expand Up @@ -2700,7 +2712,9 @@ public function get_svg_filters( $origins ) {
continue;
}
foreach ( $duotone_presets[ $origin ] as $duotone_preset ) {
$filters .= wp_get_duotone_filter_svg( $duotone_preset );
if ( in_array( $duotone_preset['slug'], self::$duotone_presets ) ) {
$filters .= wp_get_duotone_filter_svg( $duotone_preset );
}
}
}
}
Expand Down Expand Up @@ -3449,4 +3463,5 @@ public function set_spacing_sizes() {

_wp_array_set( $this->theme_json, array( 'settings', 'spacing', 'spacingSizes', 'default' ), $spacing_sizes );
}

}
42 changes: 42 additions & 0 deletions lib/compat/wordpress-6.2/get-global-styles-and-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,45 @@ function _gutenberg_add_non_persistent_theme_json_cache_group() {
wp_cache_add_non_persistent_groups( 'theme_json' );
}
add_action( 'plugins_loaded', '_gutenberg_add_non_persistent_theme_json_cache_group' );


/**
* Returns a string containing the SVGs to be referenced as filters (duotone).
*
* @since 5.9.1
*
* @return string
*/
function gutenberg_get_global_styles_svg_filters() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a copy of gutenberg_get_global_styles_svg_filters with the reference to the WP_Theme_JSON_Resolver class updated to WP_Theme_JSON_Resolver_Gutenberg.

/*
* Ignore cache when `WP_DEBUG` is enabled, so it doesn't interfere with the theme
* developer's workflow.
*
* @todo Replace `WP_DEBUG` once an "in development mode" check is available in Core.
*/
$can_use_cached = ! WP_DEBUG;
$cache_group = 'theme_json';
$cache_key = 'wp_get_global_styles_svg_filters';
if ( $can_use_cached ) {
$cached = wp_cache_get( $cache_key, $cache_group );
if ( $cached ) {
return $cached;
}
}

$supports_theme_json = wp_theme_has_theme_json();

$origins = array( 'default', 'theme', 'custom' );
if ( ! $supports_theme_json ) {
$origins = array( 'default' );
}

$tree = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data();
$svgs = $tree->get_svg_filters( $origins );

if ( $can_use_cached ) {
wp_cache_set( $cache_key, $svgs, $cache_group );
}

return $svgs;
}
35 changes: 35 additions & 0 deletions lib/compat/wordpress-6.2/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,38 @@ function gutenberg_enqueue_global_styles_custom_css() {
}
}
add_action( 'wp_enqueue_scripts', 'gutenberg_enqueue_global_styles_custom_css' );



/**
* Renders the SVG filters supplied by theme.json.
*
* Note that this doesn't render the per-block user-defined
* filters which are handled by wp_render_duotone_support,
* but it should be rendered before the filtered content
* in the body to satisfy Safari's rendering quirks.
*
* @since 5.9.1
*/
function gutenberg_global_styles_render_svg_filters() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a copy of wp_global_styles_render_svg_filters

/*
* When calling via the in_admin_header action, we only want to render the
* SVGs on block editor pages.
*/
if (
is_admin() &&
! get_current_screen()->is_block_editor()
) {
return;
}

$filters = gutenberg_get_global_styles_svg_filters();
if ( ! empty( $filters ) ) {
echo $filters;
}
}

remove_action( 'wp_body_open', 'wp_global_styles_render_svg_filters' );
remove_action( 'in_admin_header', 'wp_global_styles_render_svg_filters' );
add_action( 'wp_body_open', 'gutenberg_global_styles_render_svg_filters' );
add_action( 'in_admin_header', 'gutenberg_global_styles_render_svg_filters' );