| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
- <?php
- /**
- * Duotone block support flag.
- *
- * Parts of this source were derived and modified from TinyColor,
- * released under the MIT license.
- *
- * https://github.com/bgrins/TinyColor
- *
- * Copyright (c), Brian Grinstead, http://briangrinstead.com
- *
- * Permission is hereby granted, free of charge, to any person obtaining
- * a copy of this software and associated documentation files (the
- * "Software"), to deal in the Software without restriction, including
- * without limitation the rights to use, copy, modify, merge, publish,
- * distribute, sublicense, and/or sell copies of the Software, and to
- * permit persons to whom the Software is furnished to do so, subject to
- * the following conditions:
- *
- * The above copyright notice and this permission notice shall be
- * included in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- *
- * @package WordPress
- * @since 5.8.0
- */
- /**
- * Takes input from [0, n] and returns it as [0, 1].
- *
- * Direct port of TinyColor's function, lightly simplified to maintain
- * consistency with TinyColor.
- *
- * @see https://github.com/bgrins/TinyColor
- *
- * @since 5.8.0
- * @access private
- *
- * @param mixed $n Number of unknown type.
- * @param int $max Upper value of the range to bound to.
- *
- * @return float Value in the range [0, 1].
- */
- function wp_tinycolor_bound01( $n, $max ) {
- if ( 'string' === gettype( $n ) && false !== strpos( $n, '.' ) && 1 === (float) $n ) {
- $n = '100%';
- }
- $n = min( $max, max( 0, (float) $n ) );
- // Automatically convert percentage into number.
- if ( 'string' === gettype( $n ) && false !== strpos( $n, '%' ) ) {
- $n = (int) ( $n * $max ) / 100;
- }
- // Handle floating point rounding errors.
- if ( ( abs( $n - $max ) < 0.000001 ) ) {
- return 1.0;
- }
- // Convert into [0, 1] range if it isn't already.
- return ( $n % $max ) / (float) $max;
- }
- /**
- * Round and convert values of an RGB object.
- *
- * Direct port of TinyColor's function, lightly simplified to maintain
- * consistency with TinyColor.
- *
- * @see https://github.com/bgrins/TinyColor
- *
- * @since 5.8.0
- * @access private
- *
- * @param array $rgb_color RGB object.
- *
- * @return array Rounded and converted RGB object.
- */
- function wp_tinycolor_rgb_to_rgb( $rgb_color ) {
- return array(
- 'r' => wp_tinycolor_bound01( $rgb_color['r'], 255 ) * 255,
- 'g' => wp_tinycolor_bound01( $rgb_color['g'], 255 ) * 255,
- 'b' => wp_tinycolor_bound01( $rgb_color['b'], 255 ) * 255,
- );
- }
- /**
- * Helper function for hsl to rgb conversion.
- *
- * Direct port of TinyColor's function, lightly simplified to maintain
- * consistency with TinyColor.
- *
- * @see https://github.com/bgrins/TinyColor
- *
- * @since 5.8.0
- * @access private
- *
- * @param float $p first component.
- * @param float $q second component.
- * @param float $t third component.
- *
- * @return float R, G, or B component.
- */
- function wp_tinycolor_hue_to_rgb( $p, $q, $t ) {
- if ( $t < 0 ) {
- $t += 1;
- }
- if ( $t > 1 ) {
- $t -= 1;
- }
- if ( $t < 1 / 6 ) {
- return $p + ( $q - $p ) * 6 * $t;
- }
- if ( $t < 1 / 2 ) {
- return $q;
- }
- if ( $t < 2 / 3 ) {
- return $p + ( $q - $p ) * ( 2 / 3 - $t ) * 6;
- }
- return $p;
- }
- /**
- * Convert an HSL object to an RGB object with converted and rounded values.
- *
- * Direct port of TinyColor's function, lightly simplified to maintain
- * consistency with TinyColor.
- *
- * @see https://github.com/bgrins/TinyColor
- *
- * @since 5.8.0
- * @access private
- *
- * @param array $hsl_color HSL object.
- *
- * @return array Rounded and converted RGB object.
- */
- function wp_tinycolor_hsl_to_rgb( $hsl_color ) {
- $h = wp_tinycolor_bound01( $hsl_color['h'], 360 );
- $s = wp_tinycolor_bound01( $hsl_color['s'], 100 );
- $l = wp_tinycolor_bound01( $hsl_color['l'], 100 );
- if ( 0 === $s ) {
- // Achromatic.
- $r = $l;
- $g = $l;
- $b = $l;
- } else {
- $q = $l < 0.5 ? $l * ( 1 + $s ) : $l + $s - $l * $s;
- $p = 2 * $l - $q;
- $r = wp_tinycolor_hue_to_rgb( $p, $q, $h + 1 / 3 );
- $g = wp_tinycolor_hue_to_rgb( $p, $q, $h );
- $b = wp_tinycolor_hue_to_rgb( $p, $q, $h - 1 / 3 );
- }
- return array(
- 'r' => $r * 255,
- 'g' => $g * 255,
- 'b' => $b * 255,
- );
- }
- /**
- * Parses hex, hsl, and rgb CSS strings using the same regex as TinyColor v1.4.2
- * used in the JavaScript. Only colors output from react-color are implemented
- * and the alpha value is ignored as it is not used in duotone.
- *
- * Direct port of TinyColor's function, lightly simplified to maintain
- * consistency with TinyColor.
- *
- * @see https://github.com/bgrins/TinyColor
- * @see https://github.com/casesandberg/react-color/
- *
- * @since 5.8.0
- * @access private
- *
- * @param string $color_str CSS color string.
- *
- * @return array RGB object.
- */
- function wp_tinycolor_string_to_rgb( $color_str ) {
- $color_str = strtolower( trim( $color_str ) );
- $css_integer = '[-\\+]?\\d+%?';
- $css_number = '[-\\+]?\\d*\\.\\d+%?';
- $css_unit = '(?:' . $css_number . ')|(?:' . $css_integer . ')';
- $permissive_match3 = '[\\s|\\(]+(' . $css_unit . ')[,|\\s]+(' . $css_unit . ')[,|\\s]+(' . $css_unit . ')\\s*\\)?';
- $permissive_match4 = '[\\s|\\(]+(' . $css_unit . ')[,|\\s]+(' . $css_unit . ')[,|\\s]+(' . $css_unit . ')[,|\\s]+(' . $css_unit . ')\\s*\\)?';
- $rgb_regexp = '/^rgb' . $permissive_match3 . '$/';
- if ( preg_match( $rgb_regexp, $color_str, $match ) ) {
- return wp_tinycolor_rgb_to_rgb(
- array(
- 'r' => $match[1],
- 'g' => $match[2],
- 'b' => $match[3],
- )
- );
- }
- $rgba_regexp = '/^rgba' . $permissive_match4 . '$/';
- if ( preg_match( $rgba_regexp, $color_str, $match ) ) {
- return wp_tinycolor_rgb_to_rgb(
- array(
- 'r' => $match[1],
- 'g' => $match[2],
- 'b' => $match[3],
- )
- );
- }
- $hsl_regexp = '/^hsl' . $permissive_match3 . '$/';
- if ( preg_match( $hsl_regexp, $color_str, $match ) ) {
- return wp_tinycolor_hsl_to_rgb(
- array(
- 'h' => $match[1],
- 's' => $match[2],
- 'l' => $match[3],
- )
- );
- }
- $hsla_regexp = '/^hsla' . $permissive_match4 . '$/';
- if ( preg_match( $hsla_regexp, $color_str, $match ) ) {
- return wp_tinycolor_hsl_to_rgb(
- array(
- 'h' => $match[1],
- 's' => $match[2],
- 'l' => $match[3],
- )
- );
- }
- $hex8_regexp = '/^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/';
- if ( preg_match( $hex8_regexp, $color_str, $match ) ) {
- return wp_tinycolor_rgb_to_rgb(
- array(
- 'r' => base_convert( $match[1], 16, 10 ),
- 'g' => base_convert( $match[2], 16, 10 ),
- 'b' => base_convert( $match[3], 16, 10 ),
- )
- );
- }
- $hex6_regexp = '/^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/';
- if ( preg_match( $hex6_regexp, $color_str, $match ) ) {
- return wp_tinycolor_rgb_to_rgb(
- array(
- 'r' => base_convert( $match[1], 16, 10 ),
- 'g' => base_convert( $match[2], 16, 10 ),
- 'b' => base_convert( $match[3], 16, 10 ),
- )
- );
- }
- $hex4_regexp = '/^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/';
- if ( preg_match( $hex4_regexp, $color_str, $match ) ) {
- return wp_tinycolor_rgb_to_rgb(
- array(
- 'r' => base_convert( $match[1] . $match[1], 16, 10 ),
- 'g' => base_convert( $match[2] . $match[2], 16, 10 ),
- 'b' => base_convert( $match[3] . $match[3], 16, 10 ),
- )
- );
- }
- $hex3_regexp = '/^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/';
- if ( preg_match( $hex3_regexp, $color_str, $match ) ) {
- return wp_tinycolor_rgb_to_rgb(
- array(
- 'r' => base_convert( $match[1] . $match[1], 16, 10 ),
- 'g' => base_convert( $match[2] . $match[2], 16, 10 ),
- 'b' => base_convert( $match[3] . $match[3], 16, 10 ),
- )
- );
- }
- }
- /**
- * Registers the style and colors block attributes for block types that support it.
- *
- * @since 5.8.0
- * @access private
- *
- * @param WP_Block_Type $block_type Block Type.
- */
- function wp_register_duotone_support( $block_type ) {
- $has_duotone_support = false;
- if ( property_exists( $block_type, 'supports' ) ) {
- $has_duotone_support = _wp_array_get( $block_type->supports, array( 'color', '__experimentalDuotone' ), false );
- }
- if ( $has_duotone_support ) {
- if ( ! $block_type->attributes ) {
- $block_type->attributes = array();
- }
- if ( ! array_key_exists( 'style', $block_type->attributes ) ) {
- $block_type->attributes['style'] = array(
- 'type' => 'object',
- );
- }
- }
- }
- /**
- * Render out the duotone stylesheet and SVG.
- *
- * @since 5.8.0
- * @access private
- *
- * @param string $block_content Rendered block content.
- * @param array $block Block object.
- *
- * @return string Filtered block content.
- */
- function wp_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;
- }
- $duotone_colors = $block['attrs']['style']['color']['duotone'];
- $duotone_values = array(
- 'r' => array(),
- 'g' => array(),
- 'b' => array(),
- );
- foreach ( $duotone_colors as $color_str ) {
- $color = wp_tinycolor_string_to_rgb( $color_str );
- $duotone_values['r'][] = $color['r'] / 255;
- $duotone_values['g'][] = $color['g'] / 255;
- $duotone_values['b'][] = $color['b'] / 255;
- }
- $duotone_id = 'wp-duotone-filter-' . uniqid();
- $selectors = explode( ',', $duotone_support );
- $selectors_scoped = array_map(
- function ( $selector ) use ( $duotone_id ) {
- return '.' . $duotone_id . ' ' . trim( $selector );
- },
- $selectors
- );
- $selectors_group = implode( ', ', $selectors_scoped );
- ob_start();
- ?>
- <style>
- <?php echo $selectors_group; ?> {
- filter: url( <?php echo esc_url( '#' . $duotone_id ); ?> );
- }
- </style>
- <svg
- xmlns:xlink="http://www.w3.org/1999/xlink"
- viewBox="0 0 0 0"
- width="0"
- height="0"
- focusable="false"
- role="none"
- style="visibility: hidden; position: absolute; left: -9999px; overflow: hidden;"
- >
- <defs>
- <filter id="<?php echo esc_attr( $duotone_id ); ?>">
- <feColorMatrix
- type="matrix"
- <?php // phpcs:disable Generic.WhiteSpace.DisallowSpaceIndent ?>
- values=".299 .587 .114 0 0
- .299 .587 .114 0 0
- .299 .587 .114 0 0
- 0 0 0 1 0"
- <?php // phpcs:enable Generic.WhiteSpace.DisallowSpaceIndent ?>
- />
- <feComponentTransfer color-interpolation-filters="sRGB" >
- <feFuncR type="table" tableValues="<?php echo esc_attr( implode( ' ', $duotone_values['r'] ) ); ?>" />
- <feFuncG type="table" tableValues="<?php echo esc_attr( implode( ' ', $duotone_values['g'] ) ); ?>" />
- <feFuncB type="table" tableValues="<?php echo esc_attr( implode( ' ', $duotone_values['b'] ) ); ?>" />
- </feComponentTransfer>
- </filter>
- </defs>
- </svg>
- <?php
- $duotone = ob_get_clean();
- // Like the layout hook, this assumes the hook only applies to blocks with a single wrapper.
- $content = preg_replace(
- '/' . preg_quote( 'class="', '/' ) . '/',
- 'class="' . $duotone_id . ' ',
- $block_content,
- 1
- );
- return $content . $duotone;
- }
- // Register the block support.
- WP_Block_Supports::get_instance()->register(
- 'duotone',
- array(
- 'register_attribute' => 'wp_register_duotone_support',
- )
- );
- add_filter( 'render_block', 'wp_render_duotone_support', 10, 2 );
|