Skip to content
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

Add filter for duotone to account for gutenberg_restore_image_outer_container in classic themes #59764

Merged
merged 6 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/block-supports/duotone.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
}
if ( class_exists( 'WP_Duotone' ) ) {
remove_filter( 'render_block', array( 'WP_Duotone', 'render_duotone_support' ) );
remove_filter( 'render_block_core/image', array( 'WP_Duotone', 'restore_image_outer_container' ) );
}
add_filter( 'render_block', array( 'WP_Duotone_Gutenberg', 'render_duotone_support' ), 10, 2 );
add_filter( 'render_block_core/image', array( 'WP_Duotone_Gutenberg', 'restore_image_outer_container' ), 10, 1 );

// Enqueue styles.
// Block styles (core-block-supports-inline-css) before the style engine (gutenberg_enqueue_stored_styles).
Expand Down
39 changes: 39 additions & 0 deletions lib/class-wp-duotone-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,45 @@ public static function render_duotone_support( $block_content, $block ) {
return $tags->get_updated_html();
}

/**
* Fixes the issue with our generated class name not being added to the block's outer container
* in classic themes due to gutenberg_restore_image_outer_container from layout block supports.
*
* @since 6.5.0
*
* @param string $block_content Rendered block content.
* @return string Filtered block content.
*/
public static function restore_image_outer_container( $block_content ) {
if ( wp_theme_has_theme_json() ) {
return $block_content;
}

$tags = new WP_HTML_Tag_Processor( $block_content );
$wrapper_query = array(
'tag_name' => 'div',
'class_name' => 'wp-block-image',
);
if ( ! $tags->next_tag( $wrapper_query ) ) {
return $block_content;
}

$tags->set_bookmark( 'wrapper-div' );
$tags->next_tag();

$inner_classnames = explode( ' ', $tags->get_attribute( 'class' ) );
foreach ( $inner_classnames as $classname ) {
if ( 0 === strpos( $classname, 'wp-duotone' ) ) {
$tags->remove_class( $classname );
$tags->seek( 'wrapper-div' );
$tags->add_class( $classname );
break;
}
}

return $tags->get_updated_html();
}

/**
* Appends the used block duotone filter declarations to the inline block supports CSS.
*
Expand Down
Loading