wp_restore_image_outer_container() – For themes without theme.json file, make sure to restore the outer div for the aligned image block to avoid breaking styles relying on that div.

You appear to be a bot. Output may be restricted

Description

For themes without theme.json file, make sure to restore the outer div for the aligned image block to avoid breaking styles relying on that div.

Usage

$string = wp_restore_image_outer_container( $block_content, $block );

Parameters

$block_content
( string ) required – Rendered block content.
$block
( array ) required – Block object.

Returns

string Filtered block content.

Source

File name: wordpress/wp-includes/block-supports/layout.php
Lines:

1 to 46 of 46
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_JSON_Resolver::theme_has_support() ||
    0 === preg_match( $image_with_align, $block_content, $matches )
  ) {
    return $block_content;
  }

  $wrapper_classnames = array( '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'] ) );
  }
  $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>';
}
 

 View on GitHub View on Trac