-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Duotone: Limit SVG filter output to used filters #49103
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
Merged
Merged
Changes from all commits
Commits
Show all changes
55 commits
Select commit
Hold shift + click to select a range
7aa4c4a
Adds WP_Duotone class to set-up preset and theme-defined blocks using…
jeryj 23b631b
Fix incorrect filter name
jeryj 2f63ab2
Check if duotone filter is applied on a block in theme.json
jeryj 70581a1
Rename duotone class static variables and functions
jeryj a024ddb
Set WP_Duotone:: array and only output those via wp_footer
jeryj 0256548
Only output used duotone presets
jeryj 2c283cc
Cleanup, removal, and only output used SVG filters and CSS
jeryj 460a413
Support var:preset|duotone|default-filter syntax for block name styles
jeryj 2d0e235
Refactor to handle both duotone pipe and css var style syntax for duo…
jeryj 5f47de7
Renamed output_presets to output, as it also includes custom dutone o…
jeryj c39504b
Fix theme json vs block styles duotone order of application
jeryj 1f13855
Renamed filter to be more accurate, but should refactor out
jeryj 0c67759
Simplify getting global styles presets
jeryj e41b1e6
Undo theme json class duotone refactoring and remove filter
jeryj 45426d2
Remove unused code
jeryj 3d224bf
Rename save_ to set_ class method names
jeryj 476fe04
Fix duotone test to use real global preset value
jeryj c23a4a4
Tests for WP_Duotone::gutenberg_get_slug_from_attr()
jeryj 47d86d4
Rework gutenberg_get_slug_from_attr to use regex for more accurate ma…
jeryj a7fab17
Tests for WP_Duotone::is_preset
jeryj af3a236
Rework is_preset to also check if the preset exists
jeryj 005baa1
Remove array_key_exists checks for presets within the block filter
jeryj c7160de
Return early if no block content
jeryj f5499b4
Make linter happy
jeryj 99faa18
Remove unnecessary methods from theme json class
jeryj f599e58
Renamed get_css_var to get_css_custom_property name and added get_css…
jeryj 31da413
Renamed get_css_declaration to get_css_custom_property_declaration
jeryj a656518
Rename WP_Duotone to WP_Duotone_Gutenberg to match gutenberg coding s…
jeryj 2df2c5c
Move safari rerender hack into WP_Duotone_Gutenberg
jeryj 826cdc9
Move footer action to output SVG into WP_Duotone_Gutenberg.
jeryj 737b9cb
Move wp_enqueue_scripts action into WP_Duotone_Gutenberg class.
jeryj b18c1a4
Moved gutenberg_render_duotone_support into WP_Duotone_Gutenberg class.
jeryj 3a885b3
Move scrip-loader modifications to 6.3 release
jeryj 6f5db3f
Set WP_Duotone_Gutenberg static variables private
jeryj 19739f3
Linting and comments
jeryj 0b6538a
Switched to protected static from private static
jeryj f35b70c
Fix comment misspellings
jeryj 4d0ff95
Minor cleanup and comments
jeryj 28f5f84
Change from protected to private variables
jeryj d587738
Use self:: instead of WP_Duotone_Gutenberg:: within class
jeryj bad40ba
Move actions from class into duotone.php
jeryj f284d92
Set internal methods to private in duotone class
jeryj 719551d
Rename duotone weird-chars test to invalid-slug-chards
jeryj c98c327
Add gutenberg_render_duotone_support as deprecated
jeryj d3c83fc
Rename duotone test file to class-wp-duotone-test
jeryj c49b056
Update tests to allow access to private static methods
jeryj 7b938b9
Rename test suite to WP_Duotone_Gutenberg_Test
jeryj d254364
Set wp theme json duotone value_func to null
jeryj 46ad8b7
Change deprecated magic constant to correct name of __FUNCTION__
jeryj d598e9a
Correct duotone stylehseet to dutuotone CSS styles comment
jeryj 431496c
Update comment about how duotone styles are included in the stylesheet
jeryj 8cfa88a
Lint php
jeryj dd80152
Set WP_Duotone_Gutenberg CSS_VAR_PREFIX to private
jeryj 1457d58
Remove private specifier to CSS_VAR_PREFIX
88179f6
Added tests teardown to prevent css rules from leaking to other phpun…
jeryj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -426,128 +426,12 @@ function gutenberg_register_duotone_support( $block_type ) { | |
| * | ||
| * @param string $block_content Rendered block content. | ||
| * @param array $block Block object. | ||
| * @deprecated 6.3.0 Use WP_Duotone_Gutenberg::render_duotone_support() instead. | ||
| * @return string Filtered block content. | ||
| */ | ||
| function gutenberg_render_duotone_support( $block_content, $block ) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For those reviewing, this function had modifications and was moved to the |
||
| $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; | ||
| } | ||
|
|
||
| // 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'. | ||
| // 3. A CSS string - e.g. 'unset' to remove globally applied duotone. | ||
| $duotone_attr = $block['attrs']['style']['color']['duotone']; | ||
|
|
||
| $is_preset = is_string( $duotone_attr ) && strpos( $duotone_attr, 'var:preset|duotone|' ) === 0; | ||
| $is_css = is_string( $duotone_attr ) && strpos( $duotone_attr, 'var:preset|duotone|' ) === false; | ||
| $is_custom = is_array( $duotone_attr ); | ||
|
|
||
| // Generate the pieces needed for rendering a duotone to the page. | ||
| if ( $is_preset ) { | ||
| // Extract the slug from the preset variable string. | ||
| $slug = str_replace( 'var:preset|duotone|', '', $duotone_attr ); | ||
|
|
||
| // Utilize existing preset CSS custom property. | ||
| $filter_property = "var(--wp--preset--duotone--$slug)"; | ||
| } elseif ( $is_css ) { | ||
| // Build a unique slug for the filter based on the CSS value. | ||
| $slug = wp_unique_id( sanitize_key( $duotone_attr . '-' ) ); | ||
|
|
||
| // Pass through the CSS value. | ||
| $filter_property = $duotone_attr; | ||
| } elseif ( $is_custom ) { | ||
| // Build a unique slug for the filter based on the array of colors. | ||
| $slug = wp_unique_id( sanitize_key( implode( '-', $duotone_attr ) . '-' ) ); | ||
|
|
||
| // This has the same shape as a preset, so it can be used in place of a | ||
| // preset when getting the filter property and SVG filter. | ||
| $filter_data = array( | ||
| 'slug' => $slug, | ||
| 'colors' => $duotone_attr, | ||
| ); | ||
|
|
||
| // Build a customized CSS filter property for unique slug. | ||
| $filter_property = gutenberg_get_duotone_filter_property( $filter_data ); | ||
|
|
||
| // SVG will be output on the page later. | ||
| $filter_svg = gutenberg_get_duotone_filter_svg( $filter_data ); | ||
| } | ||
|
|
||
| // - Applied as a class attribute to the block wrapper. | ||
| // - Used as a selector to apply the filter to the block. | ||
| $filter_id = gutenberg_get_duotone_filter_id( array( 'slug' => $slug ) ); | ||
|
|
||
| // Build the CSS selectors to which the filter will be applied. | ||
| $selector = WP_Theme_JSON_Gutenberg::scope_selector( '.' . $filter_id, $duotone_support ); | ||
|
|
||
| // Calling gutenberg_style_engine_get_stylesheet_from_css_rules ensures that | ||
| // the styles are rendered in an inline for block supports because we're | ||
| // using the `context` option to instruct it so. | ||
| gutenberg_style_engine_get_stylesheet_from_css_rules( | ||
| array( | ||
| array( | ||
| 'selector' => $selector, | ||
| 'declarations' => array( | ||
| // !important is needed because these styles | ||
| // render before global styles, | ||
| // and they should be overriding the duotone | ||
| // filters set by global styles. | ||
| 'filter' => $filter_property . ' !important', | ||
| ), | ||
| ), | ||
| ), | ||
| array( | ||
| 'context' => 'block-supports', | ||
| ) | ||
| ); | ||
|
|
||
| // If we needed to generate an SVG, output it on the page. | ||
| if ( isset( $filter_svg ) ) { | ||
| add_action( | ||
| 'wp_footer', | ||
| static function () use ( $filter_svg, $selector ) { | ||
| 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.querySelector( %s ); var display = el.style.display; el.style.display = "none"; el.offsetHeight; el.style.display = display; } )();</script>', | ||
| wp_json_encode( $selector ) | ||
| ); | ||
| } | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| // Like the layout hook, this assumes the hook only applies to blocks with a single wrapper. | ||
| return preg_replace( | ||
| '/' . preg_quote( 'class="', '/' ) . '/', | ||
| 'class="' . $filter_id . ' ', | ||
| $block_content, | ||
| 1 | ||
| ); | ||
| _deprecated_function( __FUNCTION__, '6.3.0', 'WP_Duotone_Gutenberg::render_duotone_support' ); | ||
| return WP_Duotone_Gutenberg::render_duotone_support( $block_content, $block ); | ||
| } | ||
|
|
||
| // Register the block support. | ||
|
|
@@ -558,6 +442,10 @@ static function () use ( $filter_svg, $selector ) { | |
| ) | ||
| ); | ||
|
|
||
| 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_filter( 'render_block', 'gutenberg_render_duotone_support', 10, 2 ); | ||
| add_filter( 'render_block', array( 'WP_Duotone_Gutenberg', 'render_duotone_support' ), 10, 2 ); | ||
| add_action( 'wp_enqueue_scripts', array( 'WP_Duotone_Gutenberg', 'output_global_styles' ), 11 ); | ||
| add_action( 'wp_footer', array( 'WP_Duotone_Gutenberg', 'output_footer_assets' ), 10 ); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.