From 199f9f3be0c1d9be16306ea545d8ba6da417a7ad Mon Sep 17 00:00:00 2001 From: Alex Lende Date: Mon, 10 Apr 2023 21:05:11 -0500 Subject: [PATCH 1/8] Rename gutenberg_get_slug_from_attr for clarity --- lib/class-wp-duotone-gutenberg.php | 9 ++++----- phpunit/class-wp-duotone-test.php | 8 ++++---- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/class-wp-duotone-gutenberg.php b/lib/class-wp-duotone-gutenberg.php index 970840b683d270..7f08ffb6c8cc07 100644 --- a/lib/class-wp-duotone-gutenberg.php +++ b/lib/class-wp-duotone-gutenberg.php @@ -495,7 +495,7 @@ public static function set_global_style_block_names() { continue; } // If it has a duotone filter preset, save the block name and the preset slug. - $slug = self::gutenberg_get_slug_from_attr( $duotone_attr ); + $slug = self::get_slug_from_attribute( $duotone_attr ); if ( $slug && $slug !== $duotone_attr ) { self::$global_styles_block_names[ $block_node['name'] ] = $slug; @@ -511,7 +511,7 @@ public static function set_global_style_block_names() { * @param string $duotone_attr The duotone attribute from a block. * @return string The slug of the duotone preset or an empty string if no slug is found. */ - private static function gutenberg_get_slug_from_attr( $duotone_attr ) { + private static function get_slug_from_attribute( $duotone_attr ) { // Uses Branch Reset Groups `(?|…)` to return one capture group. preg_match( '/(?|var:preset\|duotone\|(\S+)|var\(--wp--preset--duotone--(\S+)\))/', $duotone_attr, $matches ); @@ -525,7 +525,7 @@ private static function gutenberg_get_slug_from_attr( $duotone_attr ) { * @return bool True if the duotone preset present and valid. */ private static function is_preset( $duotone_attr ) { - $slug = self::gutenberg_get_slug_from_attr( $duotone_attr ); + $slug = self::get_slug_from_attribute( $duotone_attr ); $filter_id = self::get_filter_id( $slug ); return array_key_exists( $filter_id, self::$global_styles_presets ); @@ -910,8 +910,7 @@ public static function render_duotone_support( $block_content, $block ) { $is_custom = is_array( $duotone_attr ); if ( $is_preset ) { - - $slug = self::gutenberg_get_slug_from_attr( $duotone_attr ); // e.g. 'green-blue'. + $slug = self::get_slug_from_attribute( $duotone_attr ); // e.g. 'green-blue'. $filter_id = self::get_filter_id( $slug ); // e.g. 'wp-duotone-filter-green-blue'. $filter_value = self::get_css_var( $slug ); // e.g. 'var(--wp--preset--duotone--green-blue)'. diff --git a/phpunit/class-wp-duotone-test.php b/phpunit/class-wp-duotone-test.php index e9a6f52077fa28..327e88f9728618 100644 --- a/phpunit/class-wp-duotone-test.php +++ b/phpunit/class-wp-duotone-test.php @@ -45,7 +45,7 @@ public function test_gutenberg_render_duotone_support_custom() { $this->assertMatchesRegularExpression( $expected, WP_Duotone_Gutenberg::render_duotone_support( $block_content, $block ) ); } - public function data_gutenberg_get_slug_from_attr() { + public function data_get_slug_from_attribute() { return array( 'pipe-slug' => array( 'var:preset|duotone|blue-orange', 'blue-orange' ), 'css-var' => array( 'var(--wp--preset--duotone--blue-orange)', 'blue-orange' ), @@ -60,11 +60,11 @@ public function data_gutenberg_get_slug_from_attr() { } /** - * @dataProvider data_gutenberg_get_slug_from_attr + * @dataProvider data_get_slug_from_attribute */ - public function test_gutenberg_get_slug_from_attr( $data_attr, $expected ) { + public function test_get_slug_from_attribute( $data_attr, $expected ) { - $reflection = new ReflectionMethod( 'WP_Duotone_Gutenberg', 'gutenberg_get_slug_from_attr' ); + $reflection = new ReflectionMethod( 'WP_Duotone_Gutenberg', 'get_slug_from_attribute' ); $reflection->setAccessible( true ); $this->assertSame( $expected, $reflection->invoke( null, $data_attr ) ); From 3b6ddfe1e2f43f12a58a015f6df0cf107998ff71 Mon Sep 17 00:00:00 2001 From: Alex Lende Date: Mon, 10 Apr 2023 21:05:54 -0500 Subject: [PATCH 2/8] Use double quotes for string concatination --- lib/class-wp-duotone-gutenberg.php | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/lib/class-wp-duotone-gutenberg.php b/lib/class-wp-duotone-gutenberg.php index 7f08ffb6c8cc07..ef93b3fcda0b67 100644 --- a/lib/class-wp-duotone-gutenberg.php +++ b/lib/class-wp-duotone-gutenberg.php @@ -129,16 +129,6 @@ class WP_Duotone_Gutenberg { */ private static $block_css_declarations = array(); - /** - * Prefix used for generating and referencing duotone CSS custom properties. - */ - const CSS_VAR_PREFIX = '--wp--preset--duotone--'; - - /** - * Prefix used for generating and referencing duotone filter IDs. - */ - const FILTER_ID_PREFIX = 'wp-duotone-'; - /** * Direct port of colord's clamp function. Using min/max instead of * nested ternaries. @@ -538,7 +528,7 @@ private static function is_preset( $duotone_attr ) { * @return string The CSS variable name. */ private static function get_css_custom_property_name( $slug ) { - return self::CSS_VAR_PREFIX . $slug; + return "--wp--preset--duotone--$slug"; } /** @@ -548,7 +538,7 @@ private static function get_css_custom_property_name( $slug ) { * @return string The ID of the duotone filter. */ private static function get_filter_id( $slug ) { - return self::FILTER_ID_PREFIX . $slug; + return "wp-duotone-$slug"; } /** @@ -558,7 +548,7 @@ private static function get_filter_id( $slug ) { * @return string The URL for the duotone filter. */ private static function get_filter_url( $filter_id ) { - return 'url(#' . $filter_id . ')'; + return "url(#$filter_id)"; } /** @@ -642,7 +632,8 @@ private static function get_filter_svg( $filter_id, $colors ) { * @return string The CSS variable. */ private static function get_css_var( $slug ) { - return 'var(' . self::get_css_custom_property_name( $slug ) . ')'; + $name = self::get_css_custom_property_name( $slug ); + return "var($name)"; } /** @@ -752,7 +743,7 @@ private static function get_global_styles_presets( $sources ) { $colors = $filter_data['colors']; $css_property_name = self::get_css_custom_property_name( $slug ); $declaration_value = is_string( $colors ) ? $colors : self::get_filter_url( $filter_id ); - $css .= $css_property_name . ':' . $declaration_value . ';'; + $css .= "$css_property_name:$declaration_value;"; } $css .= '}'; return $css; From 6a01fe105c304150a28da4d78ab8a2cc2eb7ad17 Mon Sep 17 00:00:00 2001 From: Alex Lende Date: Mon, 10 Apr 2023 21:06:45 -0500 Subject: [PATCH 3/8] Correct WP_Duotone_Gutenberg class comment --- lib/class-wp-duotone-gutenberg.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/class-wp-duotone-gutenberg.php b/lib/class-wp-duotone-gutenberg.php index ef93b3fcda0b67..f8546e342a141d 100644 --- a/lib/class-wp-duotone-gutenberg.php +++ b/lib/class-wp-duotone-gutenberg.php @@ -33,7 +33,7 @@ */ /** - * Manages which duotone filters need to be output on the page. + * Manages duotone block supports and global styles. * * @access public */ From 20ceda0aa33f413a3466599260a091dbab458cca Mon Sep 17 00:00:00 2001 From: Alex Lende Date: Mon, 10 Apr 2023 21:07:33 -0500 Subject: [PATCH 4/8] Match example filter slugs used throughout the file --- lib/class-wp-duotone-gutenberg.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/class-wp-duotone-gutenberg.php b/lib/class-wp-duotone-gutenberg.php index f8546e342a141d..f3d8b6832f4a67 100644 --- a/lib/class-wp-duotone-gutenberg.php +++ b/lib/class-wp-duotone-gutenberg.php @@ -477,7 +477,7 @@ public static function set_global_style_block_names() { continue; } - // Value looks like this: 'var(--wp--preset--duotone--blue-orange)' or 'var:preset|duotone|default-filter'. + // Value looks like this: 'var(--wp--preset--duotone--blue-orange)' or 'var:preset|duotone|blue-orange'. $duotone_attr_path = array_merge( $block_node['path'], array( 'filter', 'duotone' ) ); $duotone_attr = _wp_array_get( $theme_json, $duotone_attr_path, array() ); @@ -495,7 +495,7 @@ public static function set_global_style_block_names() { /** * Take the inline CSS duotone variable from a block and return the slug. Handles styles slugs like: - * var:preset|duotone|default-filter + * var:preset|duotone|blue-orange * var(--wp--preset--duotone--blue-orange) * * @param string $duotone_attr The duotone attribute from a block. @@ -892,7 +892,7 @@ public static function render_duotone_support( $block_content, $block ) { // Possible values for duotone attribute: // 1. Array of colors - e.g. array('#000000', '#ffffff'). - // 2. Variable for an existing Duotone preset - e.g. 'var:preset|duotone|green-blue' or 'var(--wp--preset--duotone--green-blue)'' + // 2. Variable for an existing Duotone preset - e.g. 'var:preset|duotone|blue-orange' or 'var(--wp--preset--duotone--blue-orange)'' // 3. A CSS string - e.g. 'unset' to remove globally applied duotone. $duotone_attr = $block['attrs']['style']['color']['duotone']; @@ -901,9 +901,9 @@ public static function render_duotone_support( $block_content, $block ) { $is_custom = is_array( $duotone_attr ); if ( $is_preset ) { - $slug = self::get_slug_from_attribute( $duotone_attr ); // e.g. 'green-blue'. - $filter_id = self::get_filter_id( $slug ); // e.g. 'wp-duotone-filter-green-blue'. - $filter_value = self::get_css_var( $slug ); // e.g. 'var(--wp--preset--duotone--green-blue)'. + $slug = self::get_slug_from_attribute( $duotone_attr ); // e.g. 'blue-orange'. + $filter_id = self::get_filter_id( $slug ); // e.g. 'wp-duotone-filter-blue-orange'. + $filter_value = self::get_css_var( $slug ); // e.g. 'var(--wp--preset--duotone--blue-orange)'. // CSS custom property, SVG filter, and block CSS. self::enqueue_global_styles_preset( $filter_id, $duotone_selector, $filter_value ); @@ -928,9 +928,9 @@ public static function render_duotone_support( $block_content, $block ) { self::enqueue_custom_filter( $filter_id, $duotone_selector, $filter_value, $filter_data ); } } elseif ( $has_global_styles_duotone ) { - $slug = self::$global_styles_block_names[ $block['blockName'] ]; // e.g. 'green-blue'. - $filter_id = self::get_filter_id( $slug ); // e.g. 'wp-duotone-filter-green-blue'. - $filter_value = self::get_css_var( $slug ); // e.g. 'var(--wp--preset--duotone--green-blue)'. + $slug = self::$global_styles_block_names[ $block['blockName'] ]; // e.g. 'blue-orange'. + $filter_id = self::get_filter_id( $slug ); // e.g. 'wp-duotone-filter-blue-orange'. + $filter_value = self::get_css_var( $slug ); // e.g. 'var(--wp--preset--duotone--blue-orange)'. // CSS custom property, SVG filter, and block CSS. self::enqueue_global_styles_preset( $filter_id, $duotone_selector, $filter_value ); From 97969cdb8901aac13d2d9152ea4d1345879e57ea Mon Sep 17 00:00:00 2001 From: Alex Lende Date: Mon, 10 Apr 2023 21:08:48 -0500 Subject: [PATCH 5/8] Update comments to clarify methods and variables --- lib/class-wp-duotone-gutenberg.php | 37 ++++++++++++++++-------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/lib/class-wp-duotone-gutenberg.php b/lib/class-wp-duotone-gutenberg.php index f3d8b6832f4a67..1e99e8d5bad46f 100644 --- a/lib/class-wp-duotone-gutenberg.php +++ b/lib/class-wp-duotone-gutenberg.php @@ -39,18 +39,20 @@ */ class WP_Duotone_Gutenberg { /** - * An array of Duotone presets from global, theme, and custom styles. + * An array of duotone filter data from global, theme, and custom presets. * * Example: - * [ - * 'blue-orange' => - * [ - * 'slug' => 'blue-orange', - * 'colors' => [ '#0000ff', '#ffcc00' ], - * ] + * [ + * 'wp-duotone-blue-orange' => [ + * 'slug' => 'blue-orange', + * 'colors' => [ '#0000ff', '#ffcc00' ], + * ], + * 'wp-duotone-red-yellow' => [ + * 'slug' => 'red-yellow', + * 'colors' => [ '#cc0000', '#ffff33' ], * ], * … - * ] + * ] * * @since 6.3.0 * @var array @@ -58,8 +60,8 @@ class WP_Duotone_Gutenberg { private static $global_styles_presets = array(); /** - * An array of block names from global, theme, and custom styles that have duotone presets. We'll use this to quickly - * check if a block being rendered needs to have duotone applied, and which duotone preset to use. + * Block names from global, theme, and custom styles that use duotone presets and the slug of + * the preset they are using. * * Example: * [ @@ -73,21 +75,21 @@ class WP_Duotone_Gutenberg { private static $global_styles_block_names = array(); /** - * An array of Duotone SVG and CSS output needed for the frontend duotone rendering based on what is - * being output on the page. Organized by an id of the preset/color group and the information needed - * to generate the SVG and CSS at render. + * All of the duotone filter data for SVGs on the page. Includes both + * presets and custom filters. * * Example: * [ * 'wp-duotone-blue-orange' => [ - * 'slug' => 'blue-orange', + * 'slug' => 'blue-orange', * 'colors' => [ '#0000ff', '#ffcc00' ], * ], * 'wp-duotone-000000-ffffff-2' => [ - * 'slug' => '000000-ffffff-2', + * 'slug' => '000000-ffffff-2', * 'colors' => [ '#000000', '#ffffff' ], * ], - * ] + * … + * ] * * @since 6.3.0 * @var array @@ -687,7 +689,8 @@ public static function add_editor_settings( $settings ) { } /** - * Appends the used global style duotone filter CSS Vars to the inline global styles CSS + * Appends the used global style duotone filter presets (CSS custom + * properties) to the inline global styles CSS. */ public static function output_global_styles() { if ( ! empty( self::$used_global_styles_presets ) ) { From f837e4d57825b846f248cd3f5f4004c2f96b8fb9 Mon Sep 17 00:00:00 2001 From: Alex Lende Date: Mon, 10 Apr 2023 21:09:28 -0500 Subject: [PATCH 6/8] Fix @since declarations on public methods --- lib/class-wp-duotone-gutenberg.php | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/class-wp-duotone-gutenberg.php b/lib/class-wp-duotone-gutenberg.php index 1e99e8d5bad46f..39bb33e92ddd66 100644 --- a/lib/class-wp-duotone-gutenberg.php +++ b/lib/class-wp-duotone-gutenberg.php @@ -54,7 +54,6 @@ class WP_Duotone_Gutenberg { * … * ] * - * @since 6.3.0 * @var array */ private static $global_styles_presets = array(); @@ -69,7 +68,6 @@ class WP_Duotone_Gutenberg { * … * ] * - * @since 6.3.0 * @var array */ private static $global_styles_block_names = array(); @@ -91,7 +89,6 @@ class WP_Duotone_Gutenberg { * … * ] * - * @since 6.3.0 * @var array */ private static $used_svg_filter_data = array(); @@ -449,6 +446,8 @@ private static function colord_parse( $input ) { /** * Get all possible duotone presets from global and theme styles and store as slug => [ colors array ] * We only want to process this one time. On block render we'll access and output only the needed presets for that page. + * + * @since 6.3.0 */ public static function set_global_styles_presets() { // Get the per block settings from the theme.json. @@ -466,6 +465,8 @@ public static function set_global_styles_presets() { /** * Scrape all block names from global styles and store in self::$global_styles_block_names + * + * @since 6.3.0 */ public static function set_global_style_block_names() { // Get the per block settings from the theme.json. @@ -640,6 +641,8 @@ private static function get_css_var( $slug ) { /** * Outputs all necessary SVG for duotone filters, CSS for classic themes. + * + * @since 6.3.0 */ public static function output_footer_assets() { if ( ! empty( self::$used_svg_filter_data ) ) { @@ -657,6 +660,8 @@ public static function output_footer_assets() { * they can be pulled in by the EditorStyles component in JS and rendered in * the post editor. * + * @since 6.3.0 + * * @param array $settings The block editor settings from the `block_editor_settings_all` filter. * @return array The editor settings with duotone SVGs and CSS custom properties. */ @@ -691,6 +696,8 @@ public static function add_editor_settings( $settings ) { /** * Appends the used global style duotone filter presets (CSS custom * properties) to the inline global styles CSS. + * + * @since 6.3.0 */ public static function output_global_styles() { if ( ! empty( self::$used_global_styles_presets ) ) { @@ -700,6 +707,8 @@ public static function output_global_styles() { /** * Appends the used block duotone filter declarations to the inline block supports CSS. + * + * @since 6.3.0 */ public static function output_block_styles() { if ( ! empty( self::$block_css_declarations ) ) { @@ -845,6 +854,8 @@ private static function enqueue_global_styles_preset( $filter_id, $duotone_selec /** * Registers the style and colors block attributes for block types that support it. * + * @since 6.3.0 + * * @param WP_Block_Type $block_type Block Type. */ public static function register_duotone_support( $block_type ) { @@ -871,6 +882,8 @@ public static function register_duotone_support( $block_type ) { /** * Render out the duotone CSS styles and SVG. * + * @since 6.3.0 + * * @param string $block_content Rendered block content. * @param array $block Block object. * @return string Filtered block content. @@ -952,6 +965,8 @@ public static function render_duotone_support( $block_content, $block ) { * Migrate the old experimental duotone support flag to its stabilized location * under `supports.filter.duotone` and sets. * + * @since 6.3.0 + * * @param array $settings Current block type settings. * @param array $metadata Block metadata as read in via block.json. * From 91e26e854e291f9791471ea93ad22eca8e70ffb1 Mon Sep 17 00:00:00 2001 From: Alex Lende Date: Mon, 10 Apr 2023 21:10:11 -0500 Subject: [PATCH 7/8] Reorder methods for better readablity --- lib/class-wp-duotone-gutenberg.php | 336 ++++++++++++++--------------- 1 file changed, 168 insertions(+), 168 deletions(-) diff --git a/lib/class-wp-duotone-gutenberg.php b/lib/class-wp-duotone-gutenberg.php index 39bb33e92ddd66..b461e4ba0a64fc 100644 --- a/lib/class-wp-duotone-gutenberg.php +++ b/lib/class-wp-duotone-gutenberg.php @@ -38,6 +38,20 @@ * @access public */ class WP_Duotone_Gutenberg { + /** + * Block names from global, theme, and custom styles that use duotone presets and the slug of + * the preset they are using. + * + * Example: + * [ + * 'core/featured-image' => 'blue-orange', + * … + * ] + * + * @var array + */ + private static $global_styles_block_names = array(); + /** * An array of duotone filter data from global, theme, and custom presets. * @@ -59,18 +73,21 @@ class WP_Duotone_Gutenberg { private static $global_styles_presets = array(); /** - * Block names from global, theme, and custom styles that use duotone presets and the slug of - * the preset they are using. + * All of the duotone filter data from presets for CSS custom properties on + * the page. * * Example: * [ - * 'core/featured-image' => 'blue-orange', - * … + * 'wp-duotone-blue-orange' => [ + * 'slug' => 'blue-orange', + * 'colors' => [ '#0000ff', '#ffcc00' ], + * ], + * … * ] * * @var array */ - private static $global_styles_block_names = array(); + private static $used_global_styles_presets = array(); /** * All of the duotone filter data for SVGs on the page. Includes both @@ -93,23 +110,6 @@ class WP_Duotone_Gutenberg { */ private static $used_svg_filter_data = array(); - /** - * All of the duotone filter data from presets for CSS custom properties on - * the page. - * - * Example: - * [ - * 'wp-duotone-blue-orange' => [ - * 'slug' => 'blue-orange', - * 'colors' => [ '#0000ff', '#ffcc00' ], - * ], - * … - * ] - * - * @var array - */ - private static $used_global_styles_presets = array(); - /** * All of the block CSS declarations for styles on the page. * @@ -443,59 +443,6 @@ private static function colord_parse( $input ) { return $result; } - /** - * Get all possible duotone presets from global and theme styles and store as slug => [ colors array ] - * We only want to process this one time. On block render we'll access and output only the needed presets for that page. - * - * @since 6.3.0 - */ - public static function set_global_styles_presets() { - // Get the per block settings from the theme.json. - $tree = gutenberg_get_global_settings(); - $presets_by_origin = _wp_array_get( $tree, array( 'color', 'duotone' ), array() ); - - foreach ( $presets_by_origin as $presets ) { - foreach ( $presets as $preset ) { - $filter_id = self::get_filter_id( _wp_to_kebab_case( $preset['slug'] ) ); - - self::$global_styles_presets[ $filter_id ] = $preset; - } - } - } - - /** - * Scrape all block names from global styles and store in self::$global_styles_block_names - * - * @since 6.3.0 - */ - public static function set_global_style_block_names() { - // Get the per block settings from the theme.json. - $tree = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data(); - $block_nodes = $tree->get_styles_block_nodes(); - $theme_json = $tree->get_raw_data(); - - foreach ( $block_nodes as $block_node ) { - // This block definition doesn't include any duotone settings. Skip it. - if ( empty( $block_node['duotone'] ) ) { - continue; - } - - // Value looks like this: 'var(--wp--preset--duotone--blue-orange)' or 'var:preset|duotone|blue-orange'. - $duotone_attr_path = array_merge( $block_node['path'], array( 'filter', 'duotone' ) ); - $duotone_attr = _wp_array_get( $theme_json, $duotone_attr_path, array() ); - - if ( empty( $duotone_attr ) ) { - continue; - } - // If it has a duotone filter preset, save the block name and the preset slug. - $slug = self::get_slug_from_attribute( $duotone_attr ); - - if ( $slug && $slug !== $duotone_attr ) { - self::$global_styles_block_names[ $block_node['name'] ] = $slug; - } - } - } - /** * Take the inline CSS duotone variable from a block and return the slug. Handles styles slugs like: * var:preset|duotone|blue-orange @@ -544,6 +491,17 @@ private static function get_filter_id( $slug ) { return "wp-duotone-$slug"; } + /** + * Get the CSS variable for a duotone preset. + * + * @param string $slug The slug of the duotone preset. + * @return string The CSS variable. + */ + private static function get_css_var( $slug ) { + $name = self::get_css_custom_property_name( $slug ); + return "var($name)"; + } + /** * Get the URL for a duotone filter. * @@ -628,99 +586,6 @@ private static function get_filter_svg( $filter_id, $colors ) { return $svg; } - /** - * Get the CSS variable for a duotone preset. - * - * @param string $slug The slug of the duotone preset. - * @return string The CSS variable. - */ - private static function get_css_var( $slug ) { - $name = self::get_css_custom_property_name( $slug ); - return "var($name)"; - } - - /** - * Outputs all necessary SVG for duotone filters, CSS for classic themes. - * - * @since 6.3.0 - */ - public static function output_footer_assets() { - if ( ! empty( self::$used_svg_filter_data ) ) { - echo self::get_svg_definitions( self::$used_svg_filter_data ); - } - - // This is for classic themes - in block themes, the CSS is added in the head via wp_add_inline_style in the wp_enqueue_scripts action. - if ( ! wp_is_block_theme() && ! empty( self::$used_global_styles_presets ) ) { - wp_add_inline_style( 'core-block-supports', self::get_global_styles_presets( self::$used_global_styles_presets ) ); - } - } - - /** - * Adds the duotone SVGs and CSS custom properties to the editor settings so - * they can be pulled in by the EditorStyles component in JS and rendered in - * the post editor. - * - * @since 6.3.0 - * - * @param array $settings The block editor settings from the `block_editor_settings_all` filter. - * @return array The editor settings with duotone SVGs and CSS custom properties. - */ - public static function add_editor_settings( $settings ) { - if ( ! empty( self::$global_styles_presets ) ) { - if ( ! isset( $settings['styles'] ) ) { - $settings['styles'] = array(); - } - - $settings['styles'][] = array( - // For the editor we can add all of the presets by default. - 'assets' => self::get_svg_definitions( self::$global_styles_presets ), - // The 'svgs' type is new in 6.3 and requires the corresponding JS changes in the EditorStyles component to work. - '__unstableType' => 'svgs', - // These styles not generated by global styles, so this must be false or they will be stripped out in gutenberg_get_block_editor_settings. - 'isGlobalStyles' => false, - ); - - $settings['styles'][] = array( - // For the editor we can add all of the presets by default. - 'css' => self::get_global_styles_presets( self::$global_styles_presets ), - // This must be set and must be something other than 'theme' or they will be stripped out in the post editor component. - '__unstableType' => 'presets', - // These styles are no longer generated by global styles, so this must be false or they will be stripped out in gutenberg_get_block_editor_settings. - 'isGlobalStyles' => false, - ); - } - - return $settings; - } - - /** - * Appends the used global style duotone filter presets (CSS custom - * properties) to the inline global styles CSS. - * - * @since 6.3.0 - */ - public static function output_global_styles() { - if ( ! empty( self::$used_global_styles_presets ) ) { - wp_add_inline_style( 'global-styles', self::get_global_styles_presets( self::$used_global_styles_presets ) ); - } - } - - /** - * Appends the used block duotone filter declarations to the inline block supports CSS. - * - * @since 6.3.0 - */ - public static function output_block_styles() { - if ( ! empty( self::$block_css_declarations ) ) { - gutenberg_style_engine_get_stylesheet_from_css_rules( - self::$block_css_declarations, - array( - 'context' => 'block-supports', - ) - ); - } - } - /** * Get the SVGs for the duotone filters. * @@ -879,6 +744,59 @@ public static function register_duotone_support( $block_type ) { } } + /** + * Get all possible duotone presets from global and theme styles and store as slug => [ colors array ] + * We only want to process this one time. On block render we'll access and output only the needed presets for that page. + * + * @since 6.3.0 + */ + public static function set_global_styles_presets() { + // Get the per block settings from the theme.json. + $tree = gutenberg_get_global_settings(); + $presets_by_origin = _wp_array_get( $tree, array( 'color', 'duotone' ), array() ); + + foreach ( $presets_by_origin as $presets ) { + foreach ( $presets as $preset ) { + $filter_id = self::get_filter_id( _wp_to_kebab_case( $preset['slug'] ) ); + + self::$global_styles_presets[ $filter_id ] = $preset; + } + } + } + + /** + * Scrape all block names from global styles and store in self::$global_styles_block_names + * + * @since 6.3.0 + */ + public static function set_global_style_block_names() { + // Get the per block settings from the theme.json. + $tree = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data(); + $block_nodes = $tree->get_styles_block_nodes(); + $theme_json = $tree->get_raw_data(); + + foreach ( $block_nodes as $block_node ) { + // This block definition doesn't include any duotone settings. Skip it. + if ( empty( $block_node['duotone'] ) ) { + continue; + } + + // Value looks like this: 'var(--wp--preset--duotone--blue-orange)' or 'var:preset|duotone|blue-orange'. + $duotone_attr_path = array_merge( $block_node['path'], array( 'filter', 'duotone' ) ); + $duotone_attr = _wp_array_get( $theme_json, $duotone_attr_path, array() ); + + if ( empty( $duotone_attr ) ) { + continue; + } + // If it has a duotone filter preset, save the block name and the preset slug. + $slug = self::get_slug_from_attribute( $duotone_attr ); + + if ( $slug && $slug !== $duotone_attr ) { + self::$global_styles_block_names[ $block_node['name'] ] = $slug; + } + } + } + /** * Render out the duotone CSS styles and SVG. * @@ -961,6 +879,88 @@ public static function render_duotone_support( $block_content, $block ) { return $tags->get_updated_html(); } + /** + * Appends the used block duotone filter declarations to the inline block supports CSS. + * + * @since 6.3.0 + */ + public static function output_block_styles() { + if ( ! empty( self::$block_css_declarations ) ) { + gutenberg_style_engine_get_stylesheet_from_css_rules( + self::$block_css_declarations, + array( + 'context' => 'block-supports', + ) + ); + } + } + + /** + * Appends the used global style duotone filter presets (CSS custom + * properties) to the inline global styles CSS. + * + * @since 6.3.0 + */ + public static function output_global_styles() { + if ( ! empty( self::$used_global_styles_presets ) ) { + wp_add_inline_style( 'global-styles', self::get_global_styles_presets( self::$used_global_styles_presets ) ); + } + } + + /** + * Outputs all necessary SVG for duotone filters, CSS for classic themes. + * + * @since 6.3.0 + */ + public static function output_footer_assets() { + if ( ! empty( self::$used_svg_filter_data ) ) { + echo self::get_svg_definitions( self::$used_svg_filter_data ); + } + + // This is for classic themes - in block themes, the CSS is added in the head via wp_add_inline_style in the wp_enqueue_scripts action. + if ( ! wp_is_block_theme() && ! empty( self::$used_global_styles_presets ) ) { + wp_add_inline_style( 'core-block-supports', self::get_global_styles_presets( self::$used_global_styles_presets ) ); + } + } + + /** + * Adds the duotone SVGs and CSS custom properties to the editor settings so + * they can be pulled in by the EditorStyles component in JS and rendered in + * the post editor. + * + * @since 6.3.0 + * + * @param array $settings The block editor settings from the `block_editor_settings_all` filter. + * @return array The editor settings with duotone SVGs and CSS custom properties. + */ + public static function add_editor_settings( $settings ) { + if ( ! empty( self::$global_styles_presets ) ) { + if ( ! isset( $settings['styles'] ) ) { + $settings['styles'] = array(); + } + + $settings['styles'][] = array( + // For the editor we can add all of the presets by default. + 'assets' => self::get_svg_definitions( self::$global_styles_presets ), + // The 'svgs' type is new in 6.3 and requires the corresponding JS changes in the EditorStyles component to work. + '__unstableType' => 'svgs', + // These styles not generated by global styles, so this must be false or they will be stripped out in gutenberg_get_block_editor_settings. + 'isGlobalStyles' => false, + ); + + $settings['styles'][] = array( + // For the editor we can add all of the presets by default. + 'css' => self::get_global_styles_presets( self::$global_styles_presets ), + // This must be set and must be something other than 'theme' or they will be stripped out in the post editor component. + '__unstableType' => 'presets', + // These styles are no longer generated by global styles, so this must be false or they will be stripped out in gutenberg_get_block_editor_settings. + 'isGlobalStyles' => false, + ); + } + + return $settings; + } + /** * Migrate the old experimental duotone support flag to its stabilized location * under `supports.filter.duotone` and sets. From 498a2b08c906e8d8f2b15d88cc011c71043d609c Mon Sep 17 00:00:00 2001 From: Alex Lende Date: Wed, 19 Apr 2023 15:37:25 -0500 Subject: [PATCH 8/8] Add existance check for core versions of actions/filters --- lib/block-supports/duotone.php | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/block-supports/duotone.php b/lib/block-supports/duotone.php index 153a96cf128a89..f6e2b479f350fc 100644 --- a/lib/block-supports/duotone.php +++ b/lib/block-supports/duotone.php @@ -5,7 +5,7 @@ * @package gutenberg */ -// Register duotone block supports. +// Register the block support. (overrides core one). WP_Block_Supports::get_instance()->register( 'duotone', array( @@ -14,26 +14,49 @@ ); // Set up metadata prior to rendering any blocks. +if ( class_exists( 'WP_Duotone' ) ) { + remove_action( 'wp_loaded', array( 'WP_Duotone', 'set_global_styles_presets' ) ); + remove_action( 'wp_loaded', array( 'WP_Duotone', 'set_global_style_block_names' ) ); +} add_action( 'wp_loaded', array( 'WP_Duotone_Gutenberg', 'set_global_styles_presets' ), 10 ); add_action( 'wp_loaded', array( 'WP_Duotone_Gutenberg', 'set_global_style_block_names' ), 10 ); -// Remove WordPress core filter to avoid rendering duplicate support elements. -remove_filter( 'render_block', 'wp_render_duotone_support', 10, 2 ); +// Add classnames to blocks using duotone support. +if ( function_exists( 'wp_render_duotone_support' ) ) { + // Deprecated render function. + remove_filter( 'render_block', 'wp_render_duotone_support' ); +} +if ( class_exists( 'WP_Duotone' ) ) { + remove_filter( 'render_block', array( 'WP_Duotone', 'render_duotone_support' ) ); +} add_filter( 'render_block', array( 'WP_Duotone_Gutenberg', 'render_duotone_support' ), 10, 2 ); // Enqueue styles. // Block styles (core-block-supports-inline-css) before the style engine (gutenberg_enqueue_stored_styles). // Global styles (global-styles-inline-css) after the other global styles (gutenberg_enqueue_global_styles). +if ( class_exists( 'WP_Duotone' ) ) { + remove_action( 'wp_enqueue_scripts', array( 'WP_Duotone', 'output_block_styles' ) ); + remove_action( 'wp_enqueue_scripts', array( 'WP_Duotone', 'output_global_styles' ) ); +} add_action( 'wp_enqueue_scripts', array( 'WP_Duotone_Gutenberg', 'output_block_styles' ), 9 ); add_action( 'wp_enqueue_scripts', array( 'WP_Duotone_Gutenberg', 'output_global_styles' ), 11 ); // Add SVG filters to the footer. Also, for classic themes, output block styles (core-block-supports-inline-css). +if ( class_exists( 'WP_Duotone' ) ) { + remove_action( 'wp_footer', array( 'WP_Duotone', 'output_footer_assets' ) ); +} add_action( 'wp_footer', array( 'WP_Duotone_Gutenberg', 'output_footer_assets' ), 10 ); // Add styles and SVGs for use in the editor via the EditorStyles component. +if ( class_exists( 'WP_Duotone' ) ) { + remove_filter( 'block_editor_settings_all', array( 'WP_Duotone', 'add_editor_settings' ) ); +} add_filter( 'block_editor_settings_all', array( 'WP_Duotone_Gutenberg', 'add_editor_settings' ), 10 ); // Migrate the old experimental duotone support flag. +if ( class_exists( 'WP_Duotone' ) ) { + remove_filter( 'block_type_metadata_settings', array( 'WP_Duotone', 'migrate_experimental_duotone_support_flag' ) ); +} add_filter( 'block_type_metadata_settings', array( 'WP_Duotone_Gutenberg', 'migrate_experimental_duotone_support_flag' ), 10, 2 ); /*