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

Try CSS custom properties for duotone #42870

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
155 changes: 128 additions & 27 deletions lib/block-supports/duotone.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,12 +403,14 @@ function gutenberg_get_duotone_filter_svg( $preset ) {
* @param WP_Block_Type $block_type Block Type.
*/
function gutenberg_register_duotone_support( $block_type ) {
$has_duotone_support = false;
$should_add_attributes = false;
if ( property_exists( $block_type, 'supports' ) ) {
$has_duotone_support = _wp_array_get( $block_type->supports, array( 'color', '__experimentalDuotone' ), false );
$has_deprecated_experimental_duotone_support = _wp_array_get( $block_type->supports, array( 'color', '__experimentalDuotone' ), false );
$has_duotone_support = _wp_array_get( $block_type->supports, array( 'filter', 'duotone' ), false );
$should_add_attributes = $has_duotone_support || $has_deprecated_experimental_duotone_support;
}

if ( $has_duotone_support ) {
if ( $should_add_attributes ) {
if ( ! $block_type->attributes ) {
$block_type->attributes = array();
}
Expand All @@ -421,30 +423,7 @@ function gutenberg_register_duotone_support( $block_type ) {
}
}

/**
* Render out the duotone stylesheet and SVG.
*
* @param string $block_content Rendered block content.
* @param array $block Block object.
* @return string Filtered block content.
*/
function gutenberg_render_duotone_support( $block_content, $block ) {
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );

$duotone_support = false;
if ( $block_type && property_exists( $block_type, 'supports' ) ) {
$duotone_support = _wp_array_get( $block_type->supports, array( 'color', '__experimentalDuotone' ), false );
}

$has_duotone_attribute = isset( $block['attrs']['style']['color']['duotone'] );

if (
! $duotone_support ||
! $has_duotone_attribute
) {
return $block_content;
}

function gutenberg_render_deprecated_experimental_duotone_support( $block_content, $block, $duotone_support ) {
$colors = $block['attrs']['style']['color']['duotone'];
$filter_key = is_array( $colors ) ? implode( '-', $colors ) : $colors;
$filter_preset = array(
Expand Down Expand Up @@ -508,11 +487,133 @@ static function () use ( $filter_svg, $selector ) {
);
}

function gutenberg_apply_duotone_support( $block_type, $block_attributes ) {
$attributes = array();
$duotone_support = _wp_array_get( $block_type->supports, array( 'filter', 'duotone' ), false );
$colors = _wp_array_get( $block_attributes, array( 'style', 'color', 'duotone' ), null );
if ( $duotone_support && $colors ) {
$filter_key = is_array( $colors ) ? implode( '-', $colors ) : $colors;
$filter_preset = array(
'slug' => wp_unique_id( sanitize_key( $filter_key . '-' ) ),
'colors' => $colors,
);
$filter_property = gutenberg_get_duotone_filter_property( $filter_preset );
$filter_id = gutenberg_get_duotone_filter_id( $filter_preset );
$attributes['style'] = sprintf( '--wp--style--filter: %s;', $filter_property );
$attributes['class'] = $filter_id; // Required for Safari block re-render.

if ( is_array( $colors ) ) {
add_action(
'wp_footer',
static function () use ( $filter_preset, $filter_id ) {
$filter_svg = gutenberg_get_duotone_filter_svg( $filter_preset );
echo $filter_svg;

/*
* Safari renders elements incorrectly on first paint when the
* SVG filter comes after the content that it is filtering, so
* we force a repaint with a WebKit hack which solves the issue.
*/
global $is_safari;
if ( $is_safari ) {
/*
* Simply accessing el.offsetHeight flushes layout and style
* changes in WebKit without having to wait for setTimeout.
*/
printf(
'<script>( function() { var el = document.getElementsByClassName( %s )[0]; var display = el.style.display; el.style.display = "none"; el.offsetHeight; el.style.display = display; } )();</script>',
wp_json_encode( $filter_id )
);
}
}
);
}
}

return $attributes;
}

// TODO: This may be able to be used for applying block supports to static blocks automatically.
// TODO: Use HTML Walker instead of regular expressions.
// TODO: Function inline docs mentioning class-wp-block-supports.php get_block_wrapper_attributes and apply_block_supports.
function gutenberg_render_block_wrapper_attributes( $block_content, $new_attributes = array() ) {
// This is hardcoded on purpose.
// We only support a fixed list of attributes.
$attributes_to_render = array( 'style', 'class' );
foreach ( $attributes_to_render as $attribute_name ) {
if ( empty( $new_attributes[ $attribute_name ] ) ) {
continue;
}

$attribute_pattern = '/'. preg_quote( $attribute_name, '/' ) . '="([^"]*)"/';
$wrapper_pattern = '/<[^>]+?' . substr( $attribute_pattern, 1, -1 ) . '[^>]*>/';
preg_match(
$wrapper_pattern,
$block_content,
$matches
);

if ( isset( $matches[1] ) ) {
// Replace the attribute.
$value = $new_attributes[ $attribute_name ] . ' ' . $matches[1];
$block_content = preg_replace(
$attribute_pattern,
sprintf( '%s="%s"', $attribute_name, esc_attr( $value ) ),
$block_content,
1
);
} else {
// No matching attribute was found or there was an error, so add a new attribute.
$value = $new_attributes[ $attribute_name ];
$block_content = preg_replace(
'/(\s*\/?>)/',
sprintf( ' %s="%s"${0}', $attribute_name, esc_attr( $value ) ),
$block_content,
1
);
}
}

return $block_content;
}

/**
* Render out the duotone stylesheet and SVG.
*
* @param string $block_content Rendered block content.
* @param array $block Block object.
* @return string Filtered block content.
*/
function gutenberg_render_duotone_support( $block_content, $block ) {
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
if ( ! $block_type || ! property_exists( $block_type, 'supports' )) {
return $block_content;
}

$duotone_support = _wp_array_get( $block_type->supports, array( 'filter', 'duotone' ), false );
if ( $duotone_support && isset( $block['attrs']['style']['color']['duotone'] ) ) {
$new_attributes = gutenberg_apply_duotone_support( $block_type, $block['attrs'] );
return gutenberg_render_block_wrapper_attributes( $block_content, $new_attributes );
}

$deprecated_experimental_duotone_support = _wp_array_get( $block_type->supports, array( 'color', '__experimentalDuotone' ), false );
if ( $deprecated_experimental_duotone_support && isset( $block['attrs']['style']['color']['duotone'] ) ) {
return gutenberg_render_deprecated_experimental_duotone_support( $block_content, $block, $deprecated_experimental_duotone_support );
}

return $block_content;
}

// Register the block support.
WP_Block_Supports::get_instance()->register(
'duotone',
array(
'register_attribute' => 'gutenberg_register_duotone_support',
/*
* If static blocks were supported, we could do this instead of
* the render_block filter for the new filter.duotone support.
*/
// 'apply' => 'gutenberg_apply_duotone_support',
)
);

Expand Down
Loading