Changeset 60968
- Timestamp:
- 10/17/2025 11:52:41 PM (5 months ago)
- Location:
- trunk
- Files:
-
- 2 edited
-
src/wp-includes/block-supports/layout.php (modified) (1 diff)
-
tests/phpunit/tests/block-supports/layout.php (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/block-supports/layout.php
r60651 r60968 1075 1075 */ 1076 1076 function wp_restore_image_outer_container( $block_content, $block ) { 1077 $image_with_align = " 1078 /# 1) everything up to the class attribute contents 1079 ( 1080 ^\s* 1081 <figure\b 1082 [^>]* 1083 \bclass= 1084 [\"'] 1085 ) 1086 # 2) the class attribute contents 1087 ( 1088 [^\"']* 1089 \bwp-block-image\b 1090 [^\"']* 1091 \b(?:alignleft|alignright|aligncenter)\b 1092 [^\"']* 1093 ) 1094 # 3) everything after the class attribute contents 1095 ( 1096 [\"'] 1097 [^>]* 1098 > 1099 .* 1100 <\/figure> 1101 )/iUx"; 1102 1077 if ( wp_theme_has_theme_json() ) { 1078 return $block_content; 1079 } 1080 1081 $figure_processor = new WP_HTML_Tag_Processor( $block_content ); 1103 1082 if ( 1104 wp_theme_has_theme_json() || 1105 0 === preg_match( $image_with_align, $block_content, $matches ) 1083 ! $figure_processor->next_tag( 'FIGURE' ) || 1084 ! $figure_processor->has_class( 'wp-block-image' ) || 1085 ! ( 1086 $figure_processor->has_class( 'alignleft' ) || 1087 $figure_processor->has_class( 'aligncenter' ) || 1088 $figure_processor->has_class( 'alignright' ) 1089 ) 1106 1090 ) { 1107 1091 return $block_content; 1108 1092 } 1109 1093 1110 $wrapper_classnames = array( 'wp-block-image' ); 1111 1112 // If the block has a classNames attribute these classnames need to be removed from the content and added back 1113 // to the new wrapper div also. 1114 if ( ! empty( $block['attrs']['className'] ) ) { 1115 $wrapper_classnames = array_merge( $wrapper_classnames, explode( ' ', $block['attrs']['className'] ) ); 1116 } 1117 $content_classnames = explode( ' ', $matches[2] ); 1118 $filtered_content_classnames = array_diff( $content_classnames, $wrapper_classnames ); 1119 1120 return '<div class="' . implode( ' ', $wrapper_classnames ) . '">' . $matches[1] . implode( ' ', $filtered_content_classnames ) . $matches[3] . '</div>'; 1094 /* 1095 * The next section of code wraps the existing figure in a new DIV element. 1096 * While doing it, it needs to transfer the layout and the additional CSS 1097 * class names from the original figure upward to the wrapper. 1098 * 1099 * Example: 1100 * 1101 * // From this… 1102 * <!-- wp:image {"className":"hires"} --> 1103 * <figure class="wp-block-image wide hires">… 1104 * 1105 * // To this… 1106 * <div class="wp-block-image hires"><figure class="wide">… 1107 */ 1108 $wrapper_processor = new WP_HTML_Tag_Processor( '<div>' ); 1109 $wrapper_processor->next_token(); 1110 $wrapper_processor->set_attribute( 1111 'class', 1112 is_string( $block['attrs']['className'] ?? null ) 1113 ? "wp-block-image {$block['attrs']['className']}" 1114 : 'wp-block-image' 1115 ); 1116 1117 // And remove them from the existing content; it has been transferred upward. 1118 $figure_processor->remove_class( 'wp-block-image' ); 1119 foreach ( $wrapper_processor->class_list() as $class_name ) { 1120 $figure_processor->remove_class( $class_name ); 1121 } 1122 1123 return "{$wrapper_processor->get_updated_html()}{$figure_processor->get_updated_html()}</div>"; 1121 1124 } 1122 1125 -
trunk/tests/phpunit/tests/block-supports/layout.php
r60727 r60968 75 75 $expected = '<figure class="wp-block-image size-full"><img src="/my-image.jpg"/></figure>'; 76 76 77 $this->assert Same( $expected, wp_restore_image_outer_container( $block_content, $block ) );77 $this->assertEqualHTML( $expected, wp_restore_image_outer_container( $block_content, $block ) ); 78 78 } 79 79 … … 91 91 $expected = '<div class="wp-block-image"><figure class="alignright size-full"><img src="/my-image.jpg"/></figure></div>'; 92 92 93 $this->assert Same( $expected, wp_restore_image_outer_container( $block_content, $block ) );93 $this->assertEqualHTML( $expected, wp_restore_image_outer_container( $block_content, $block ) ); 94 94 } 95 95 … … 112 112 ); 113 113 114 $this->assert Same( $expected, wp_restore_image_outer_container( $block_image_html, $block ) );114 $this->assertEqualHTML( $expected, wp_restore_image_outer_container( $block_image_html, $block ) ); 115 115 } 116 116 … … 166 166 $expected = '<figure class="wp-block-image alignright size-full is-style-round my-custom-classname"><img src="/my-image.jpg"/></figure>'; 167 167 168 $this->assert Same( $expected, wp_restore_image_outer_container( $block_content, $block ) );168 $this->assertEqualHTML( $expected, wp_restore_image_outer_container( $block_content, $block ) ); 169 169 } 170 170
Note: See TracChangeset
for help on using the changeset viewer.