diff --git a/blocks/duotone-filter/duotone.php b/blocks/duotone-filter/duotone.php index 68390c77..f04f8331 100644 --- a/blocks/duotone-filter/duotone.php +++ b/blocks/duotone-filter/duotone.php @@ -1,35 +1,22 @@ $r / 0xff, - 'g' => $g / 0xff, - 'b' => $b / 0xff, - ); - } -} +include_once 'utils.php'; // phpcs:disable $duotone_id = $block['attrs']['duotoneId']; +$duotone_selector = '.wp-block-image.' . $duotone_id . ' img'; $duotone_dark = hex2rgb( $block['attrs']['duotoneDark'] ); $duotone_light = hex2rgb( $block['attrs']['duotoneLight'] ); // phpcs:enable ?> + + - values=".2989 .5870 .1140 0 0 - .2989 .5870 .1140 0 0 - .2989 .5870 .1140 0 0 + values=".299 .587 .114 0 0 + .299 .587 .114 0 0 + .299 .587 .114 0 0 0 0 0 1 0" /> diff --git a/blocks/duotone-filter/index.php b/blocks/duotone-filter/index.php index fe5dcd7c..718dd00f 100644 --- a/blocks/duotone-filter/index.php +++ b/blocks/duotone-filter/index.php @@ -8,16 +8,16 @@ ] ); function is_supported_block( $block ) { - $supported_blocks = [ 'core/image', 'core/cover' ]; + $supported_blocks = [ 'core/image' ]; return in_array( $block['blockName'], $supported_blocks, true ); } add_filter( 'render_block', function ( $block_content, $block ) { if ( ! is_supported_block( $block ) || - ! array_key_exists( 'duotoneId', $block['attrs'] ) || - ! array_key_exists( 'duotoneDark', $block['attrs'] ) || - ! array_key_exists( 'duotoneLight', $block['attrs'] ) + ! isset( $block['attrs']['duotoneId'] ) || + ! isset( $block['attrs']['duotoneDark'] ) || + ! isset( $block['attrs']['duotoneLight'] ) ) { return $block_content; } @@ -26,6 +26,6 @@ function is_supported_block( $block ) { include 'duotone.php'; $duotone = ob_get_clean(); - return $duotone . $block_content; + return $block_content . $duotone; }, 10, 2 ); } ); diff --git a/blocks/duotone-filter/src/duotone.js b/blocks/duotone-filter/src/duotone.js index 7688acfb..16214346 100644 --- a/blocks/duotone-filter/src/duotone.js +++ b/blocks/duotone-filter/src/duotone.js @@ -3,76 +3,66 @@ */ import { SVG } from '@wordpress/components'; -const hex2rgb = ( color = '' ) => { - let r = '0'; - let g = '0'; - let b = '0'; - - if ( color.length === 4 ) { - r = '0x' + color[ 1 ] + color[ 1 ]; - g = '0x' + color[ 2 ] + color[ 2 ]; - b = '0x' + color[ 3 ] + color[ 3 ]; - } else if ( color.length === 7 ) { - r = '0x' + color[ 1 ] + color[ 2 ]; - g = '0x' + color[ 3 ] + color[ 4 ]; - b = '0x' + color[ 5 ] + color[ 6 ]; - } else { - return {}; - } - - return { - r: r / 0xff, - g: g / 0xff, - b: b / 0xff, - }; -}; +/** + * Internal dependencies + */ +import { hex2rgb } from './utils'; -function Duotone( { id: duotoneId, darkColor, lightColor } ) { +function Duotone( { id: duotoneId, darkColor, lightColor, selector } ) { const duotoneDark = hex2rgb( darkColor ); const duotoneLight = hex2rgb( lightColor ); + const stylesheet = ` +${ selector } { + filter: url( #${ duotoneId } ); +} +`; + return ( - - - - - - - - + + + + - - - - + + + + + + + + +