-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Deprecate remaining global duotone functions #49702
Merged
scruffian
merged 6 commits into
trunk
from
fix/deprecate-remaining-global-duotone-functions
Apr 18, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
972d4a4
Refactor duotone to use the colord port
43d0ca7
Add deprecation TODO
e25dec1
Deprecate gutenberg_get_duotone_filter_id and gutenberg_get_duotone_f…
16a63c1
Refactor gutenberg_register_duotone_support into the class
d7f1d31
Move deprecated functions to the bottom of the file
6adec60
Move gutenberg_render_duotone_support @deprecated annotation up
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 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
This file contains 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 |
---|---|---|
|
@@ -99,6 +99,11 @@ class WP_Duotone_Gutenberg { | |
*/ | ||
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. | ||
|
@@ -501,6 +506,16 @@ private static function get_css_custom_property_name( $slug ) { | |
return self::CSS_VAR_PREFIX . $slug; | ||
} | ||
|
||
/** | ||
* Get the ID of the duotone filter. | ||
* | ||
* @param string $slug The slug of the duotone preset. | ||
* @return string The ID of the duotone filter. | ||
*/ | ||
private static function get_filter_id( $slug ) { | ||
return self::FILTER_ID_PREFIX . $slug; | ||
} | ||
|
||
/** | ||
* Gets the SVG for the duotone filter definition. | ||
* | ||
|
@@ -593,7 +608,7 @@ private static function get_css_var( $slug ) { | |
* @return string The CSS declaration. | ||
*/ | ||
private static function get_css_custom_property_declaration( $filter_data ) { | ||
$declaration_value = gutenberg_get_duotone_filter_property( $filter_data ); | ||
$declaration_value = self::get_filter_css_property_value_from_preset( $filter_data ); | ||
$duotone_preset_css_property_name = self::get_css_custom_property_name( $filter_data['slug'] ); | ||
return $duotone_preset_css_property_name . ': ' . $declaration_value . ';'; | ||
} | ||
|
@@ -715,6 +730,32 @@ private static function get_selector( $block_name ) { | |
} | ||
} | ||
|
||
/** | ||
* Registers the style and colors block attributes for block types that support it. | ||
* | ||
* @param WP_Block_Type $block_type Block Type. | ||
*/ | ||
public static function register_duotone_support( $block_type ) { | ||
$has_duotone_support = false; | ||
if ( property_exists( $block_type, 'supports' ) ) { | ||
// Previous `color.__experimentalDuotone` support flag is migrated | ||
// to `filter.duotone` via `block_type_metadata_settings` filter. | ||
$has_duotone_support = _wp_array_get( $block_type->supports, array( 'filter', 'duotone' ), null ); | ||
} | ||
|
||
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 CSS styles and SVG. | ||
* | ||
|
@@ -775,7 +816,7 @@ public static function render_duotone_support( $block_content, $block ) { | |
'colors' => $duotone_attr, | ||
); | ||
// Build a customized CSS filter property for unique slug. | ||
$declaration_value = gutenberg_get_duotone_filter_property( $filter_data ); | ||
$declaration_value = self::get_filter_css_property_value_from_preset( $filter_data ); | ||
|
||
self::$output[ $slug ] = $filter_data; | ||
} | ||
|
@@ -790,7 +831,7 @@ public static function render_duotone_support( $block_content, $block ) { | |
|
||
// - 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 ) ); | ||
$filter_id = self::get_filter_id_from_preset( array( 'slug' => $slug ) ); | ||
|
||
// Build the CSS selectors to which the filter will be applied. | ||
$selectors = explode( ',', $duotone_selector ); | ||
|
@@ -860,15 +901,44 @@ public static function migrate_experimental_duotone_support_flag( $settings, $me | |
return $settings; | ||
} | ||
|
||
/** | ||
* Returns the prefixed id for the duotone filter for use as a CSS id. | ||
* | ||
* @param array $preset Duotone preset value as seen in theme.json. | ||
* @return string Duotone filter CSS id. | ||
*/ | ||
public static function get_filter_id_from_preset( $preset ) { | ||
$filter_id = ''; | ||
if ( isset( $preset['slug'] ) ) { | ||
$filter_id = self::get_filter_id( $preset['slug'] ); | ||
} | ||
return $filter_id; | ||
} | ||
|
||
/** | ||
* Gets the SVG for the duotone filter definition from a preset. | ||
* | ||
* @param array $preset The duotone preset. | ||
* @return string The SVG for the filter definition. | ||
*/ | ||
public static function get_filter_svg_from_preset( $preset ) { | ||
// TODO: This function will be refactored out in a follow-up PR where it will be deprecated. | ||
$filter_id = gutenberg_get_duotone_filter_id( $preset ); | ||
$filter_id = self::get_filter_id_from_preset( $preset ); | ||
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. Should we have a deprecated call in here? 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. |
||
return self::get_filter_svg( $filter_id, $preset['colors'] ); | ||
} | ||
|
||
/** | ||
* Gets the CSS filter property value from a preset. | ||
* | ||
* @param array $preset The duotone preset. | ||
* @return string The CSS filter property value. | ||
*/ | ||
public static function get_filter_css_property_value_from_preset( $preset ) { | ||
if ( isset( $preset['colors'] ) && is_string( $preset['colors'] ) ) { | ||
return $preset['colors']; | ||
} | ||
|
||
$filter_id = self::get_filter_id_from_preset( $preset ); | ||
|
||
return 'url(#' . $filter_id . ')'; | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block looks like it was just moved from below. Is there a reason for that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's so you don't have to scroll through 400 lines of deprecated code to see the first line of code that actually does something.