WordPress Version: 6.2
/**
* 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.
*
* @since 6.0.0
* @access private
*
* @param string $block_content Rendered block content.
* @param array $block Block object.
* @return string Filtered block content.
*/
function wp_restore_image_outer_container($block_content, $block)
{
$image_with_align = "\n/# 1) everything up to the class attribute contents\n(\n\t^\\s*\n\t<figure\\b\n\t[^>]*\n\t\\bclass=\n\t[\"']\n)\n# 2) the class attribute contents\n(\n\t[^\"']*\n\t\\bwp-block-image\\b\n\t[^\"']*\n\t\\b(?:alignleft|alignright|aligncenter)\\b\n\t[^\"']*\n)\n# 3) everything after the class attribute contents\n(\n\t[\"']\n\t[^>]*\n\t>\n\t.*\n\t<\\/figure>\n)/iUx";
if (wp_theme_has_theme_json() || 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>';
}