wp/wp-includes/blocks/cover.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
--- a/wp/wp-includes/blocks/cover.php	Thu Sep 29 08:06:27 2022 +0200
+++ b/wp/wp-includes/blocks/cover.php	Fri Sep 05 18:40:08 2025 +0200
@@ -8,8 +8,10 @@
 /**
  * Renders the `core/cover` block on server.
  *
- * @param array $attributes The block attributes.
- * @param array $content    The block rendered content.
+ * @since 6.0.0
+ *
+ * @param array  $attributes The block attributes.
+ * @param string $content    The block rendered content.
  *
  * @return string Returns the cover block markup, if useFeaturedImage is true.
  */
@@ -25,7 +27,7 @@
 		);
 
 		if ( isset( $attributes['focalPoint'] ) ) {
-			$object_position              = round( $attributes['focalPoint']['x'] * 100 ) . '%' . ' ' . round( $attributes['focalPoint']['y'] * 100 ) . '%';
+			$object_position              = round( $attributes['focalPoint']['x'] * 100 ) . '% ' . round( $attributes['focalPoint']['y'] * 100 ) . '%';
 			$attr['data-object-position'] = $object_position;
 			$attr['style']                = 'object-position: ' . $object_position;
 		}
@@ -34,7 +36,7 @@
 
 		/*
 		 * Inserts the featured image between the (1st) cover 'background' `span` and 'inner_container' `div`,
-		 * and removes eventual withespace characters between the two (typically introduced at template level)
+		 * and removes eventual whitespace characters between the two (typically introduced at template level)
 		 */
 		$inner_container_start = '/<div\b[^>]+wp-block-cover__inner-container[\s|"][^>]*>/U';
 		if ( 1 === preg_match( $inner_container_start, $content, $matches, PREG_OFFSET_CAPTURE ) ) {
@@ -46,12 +48,19 @@
 			update_post_thumbnail_cache();
 		}
 		$current_featured_image = get_the_post_thumbnail_url();
-		$content                = preg_replace(
-			'/class=\".*?\"/',
-			'${0} style="background-image:url(' . esc_url( $current_featured_image ) . ')"',
-			$content,
-			1
-		);
+		if ( ! $current_featured_image ) {
+			return $content;
+		}
+
+		$processor = new WP_HTML_Tag_Processor( $content );
+		$processor->next_tag();
+
+		$styles         = $processor->get_attribute( 'style' );
+		$merged_styles  = ! empty( $styles ) ? $styles . ';' : '';
+		$merged_styles .= 'background-image:url(' . esc_url( $current_featured_image ) . ');';
+
+		$processor->set_attribute( 'style', $merged_styles );
+		$content = $processor->get_updated_html();
 	}
 
 	return $content;
@@ -59,6 +68,8 @@
 
 /**
  * Registers the `core/cover` block renderer on server.
+ *
+ * @since 6.0.0
  */
 function register_block_core_cover() {
 	register_block_type_from_metadata(