Skip to content

Commit

Permalink
Polish duotone rendering code (#49706)
Browse files Browse the repository at this point in the history
* Rename gutenberg_get_slug_from_attr for clarity

* Use double quotes for string concatination

* Correct WP_Duotone_Gutenberg class comment

* Match example filter slugs used throughout the file

* Update comments to clarify methods and variables

* Fix @SInCE declarations on public methods

* Reorder methods for better readablity

* Add existance check for core versions of actions/filters
  • Loading branch information
Alex Lende authored Apr 20, 2023
1 parent 73e6403 commit 41220dd
Show file tree
Hide file tree
Showing 3 changed files with 228 additions and 197 deletions.
29 changes: 26 additions & 3 deletions lib/block-supports/duotone.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 );

/*
Expand Down
Loading

1 comment on commit 41220dd

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 41220dd.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4756835759
📝 Reported issues:

Please sign in to comment.