wip
This commit is contained in:
@@ -858,8 +858,9 @@ function wp_render_layout_support_flag( $block_content, $block ) {
|
||||
}
|
||||
|
||||
// Add combined layout and block classname for global styles to hook onto.
|
||||
$block_name = explode( '/', $block['blockName'] );
|
||||
$class_names[] = 'wp-block-' . end( $block_name ) . '-' . $layout_classname;
|
||||
$split_block_name = explode( '/', $block['blockName'] );
|
||||
$full_block_name = 'core' === $split_block_name[0] ? end( $split_block_name ) : implode( '-', $split_block_name );
|
||||
$class_names[] = 'wp-block-' . $full_block_name . '-' . $layout_classname;
|
||||
|
||||
// Add classes to the outermost HTML tag if necessary.
|
||||
if ( ! empty( $outer_class_names ) ) {
|
||||
@@ -1073,50 +1074,53 @@ add_filter( 'render_block_core/group', 'wp_restore_group_inner_container', 10, 2
|
||||
* @return string Filtered block content.
|
||||
*/
|
||||
function wp_restore_image_outer_container( $block_content, $block ) {
|
||||
$image_with_align = "
|
||||
/# 1) everything up to the class attribute contents
|
||||
(
|
||||
^\s*
|
||||
<figure\b
|
||||
[^>]*
|
||||
\bclass=
|
||||
[\"']
|
||||
)
|
||||
# 2) the class attribute contents
|
||||
(
|
||||
[^\"']*
|
||||
\bwp-block-image\b
|
||||
[^\"']*
|
||||
\b(?:alignleft|alignright|aligncenter)\b
|
||||
[^\"']*
|
||||
)
|
||||
# 3) everything after the class attribute contents
|
||||
(
|
||||
[\"']
|
||||
[^>]*
|
||||
>
|
||||
.*
|
||||
<\/figure>
|
||||
)/iUx";
|
||||
if ( wp_theme_has_theme_json() ) {
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
$figure_processor = new WP_HTML_Tag_Processor( $block_content );
|
||||
if (
|
||||
wp_theme_has_theme_json() ||
|
||||
0 === preg_match( $image_with_align, $block_content, $matches )
|
||||
! $figure_processor->next_tag( 'FIGURE' ) ||
|
||||
! $figure_processor->has_class( 'wp-block-image' ) ||
|
||||
! (
|
||||
$figure_processor->has_class( 'alignleft' ) ||
|
||||
$figure_processor->has_class( 'aligncenter' ) ||
|
||||
$figure_processor->has_class( 'alignright' )
|
||||
)
|
||||
) {
|
||||
return $block_content;
|
||||
}
|
||||
|
||||
$wrapper_classnames = array( 'wp-block-image' );
|
||||
/*
|
||||
* The next section of code wraps the existing figure in a new DIV element.
|
||||
* While doing it, it needs to transfer the layout and the additional CSS
|
||||
* class names from the original figure upward to the wrapper.
|
||||
*
|
||||
* Example:
|
||||
*
|
||||
* // From this…
|
||||
* <!-- wp:image {"className":"hires"} -->
|
||||
* <figure class="wp-block-image wide hires">…
|
||||
*
|
||||
* // To this…
|
||||
* <div class="wp-block-image hires"><figure class="wide">…
|
||||
*/
|
||||
$wrapper_processor = new WP_HTML_Tag_Processor( '<div>' );
|
||||
$wrapper_processor->next_token();
|
||||
$wrapper_processor->set_attribute(
|
||||
'class',
|
||||
is_string( $block['attrs']['className'] ?? null )
|
||||
? "wp-block-image {$block['attrs']['className']}"
|
||||
: 'wp-block-image'
|
||||
);
|
||||
|
||||
// If the block has a classNames attribute these classnames need to be removed from the content and added back
|
||||
// to the new wrapper div also.
|
||||
if ( ! empty( $block['attrs']['className'] ) ) {
|
||||
$wrapper_classnames = array_merge( $wrapper_classnames, explode( ' ', $block['attrs']['className'] ) );
|
||||
// And remove them from the existing content; it has been transferred upward.
|
||||
$figure_processor->remove_class( 'wp-block-image' );
|
||||
foreach ( $wrapper_processor->class_list() as $class_name ) {
|
||||
$figure_processor->remove_class( $class_name );
|
||||
}
|
||||
$content_classnames = explode( ' ', $matches[2] );
|
||||
$filtered_content_classnames = array_diff( $content_classnames, $wrapper_classnames );
|
||||
|
||||
return '<div class="' . implode( ' ', $wrapper_classnames ) . '">' . $matches[1] . implode( ' ', $filtered_content_classnames ) . $matches[3] . '</div>';
|
||||
return "{$wrapper_processor->get_updated_html()}{$figure_processor->get_updated_html()}</div>";
|
||||
}
|
||||
|
||||
add_filter( 'render_block_core/image', 'wp_restore_image_outer_container', 10, 2 );
|
||||
|
||||
Reference in New Issue
Block a user