Skip to content

Commit

Permalink
Configure new duotone for cover block
Browse files Browse the repository at this point in the history
Configure new duotone for image block

Refactor withDuotoneStyles

Add filter.duotone check to duotone hooks

Add deprecated notice to __experimentalDuotone styles

Apply duotone via --wp--style--filter in duotone.js

Refactor gutenberg_render_duotone_support

Add filter.duotone check in duotone.php

Fix undefined and unset duotone editor styles

Fix duotone rendering in php

Support wrapper attributes in core/image block

Apply duotone via --wp--style--filter in duotone.php

Fix duotone selector in block editor

Regex micro optimizations

Refactor render_block_core_cover invert image check

Support wrapper attributes in core/cover block

Add check for stabalized duotone support in render_block hook

Revert image block to use __experimentalDuotone

Add back __experimentalDuotone for cover block

Fix duotone for static blocks

Add TODO comment for gutenberg_render_block_wrapper_attributes

Simplify preg_match results
  • Loading branch information
Alex Lende authored and getdave committed Feb 21, 2023
1 parent b935741 commit fb91635
Show file tree
Hide file tree
Showing 8 changed files with 378 additions and 107 deletions.
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

0 comments on commit fb91635

Please sign in to comment.