wp/wp-includes/blocks/media-text.php
changeset 22 8c2e4d02f4ef
parent 21 48c4eec2b7e6
equal deleted inserted replaced
21:48c4eec2b7e6 22:8c2e4d02f4ef
    27 	$current_featured_image = get_the_post_thumbnail_url();
    27 	$current_featured_image = get_the_post_thumbnail_url();
    28 	if ( ! $current_featured_image ) {
    28 	if ( ! $current_featured_image ) {
    29 		return $content;
    29 		return $content;
    30 	}
    30 	}
    31 
    31 
       
    32 	$has_media_on_right = isset( $attributes['mediaPosition'] ) && 'right' === $attributes['mediaPosition'];
       
    33 	$image_fill         = isset( $attributes['imageFill'] ) && $attributes['imageFill'];
       
    34 	$focal_point        = isset( $attributes['focalPoint'] ) ? round( $attributes['focalPoint']['x'] * 100 ) . '% ' . round( $attributes['focalPoint']['y'] * 100 ) . '%' : '50% 50%';
       
    35 	$unique_id          = 'wp-block-media-text__media-' . wp_unique_id();
       
    36 
       
    37 	$block_tag_processor = new WP_HTML_Tag_Processor( $content );
       
    38 	$block_query         = array(
       
    39 		'tag_name'   => 'div',
       
    40 		'class_name' => 'wp-block-media-text',
       
    41 	);
       
    42 
       
    43 	while ( $block_tag_processor->next_tag( $block_query ) ) {
       
    44 		if ( $image_fill ) {
       
    45 			// The markup below does not work with the deprecated `is-image-fill` class.
       
    46 			$block_tag_processor->remove_class( 'is-image-fill' );
       
    47 			$block_tag_processor->add_class( 'is-image-fill-element' );
       
    48 		}
       
    49 	}
       
    50 
       
    51 	$content = $block_tag_processor->get_updated_html();
       
    52 
    32 	$media_tag_processor   = new WP_HTML_Tag_Processor( $content );
    53 	$media_tag_processor   = new WP_HTML_Tag_Processor( $content );
    33 	$wrapping_figure_query = array(
    54 	$wrapping_figure_query = array(
    34 		'tag_name'   => 'figure',
    55 		'tag_name'   => 'figure',
    35 		'class_name' => 'wp-block-media-text__media',
    56 		'class_name' => 'wp-block-media-text__media',
    36 	);
    57 	);
    37 	$has_media_on_right    = isset( $attributes['mediaPosition'] ) && 'right' === $attributes['mediaPosition'];
       
    38 	$image_fill            = isset( $attributes['imageFill'] ) && $attributes['imageFill'];
       
    39 	$focal_point           = isset( $attributes['focalPoint'] ) ? round( $attributes['focalPoint']['x'] * 100 ) . '% ' . round( $attributes['focalPoint']['y'] * 100 ) . '%' : '50% 50%';
       
    40 	$unique_id             = 'wp-block-media-text__media-' . wp_unique_id();
       
    41 
    58 
    42 	if ( $has_media_on_right ) {
    59 	if ( $has_media_on_right ) {
    43 		// Loop through all the figure tags and set a bookmark on the last figure tag.
    60 		// Loop through all the figure tags and set a bookmark on the last figure tag.
    44 		while ( $media_tag_processor->next_tag( $wrapping_figure_query ) ) {
    61 		while ( $media_tag_processor->next_tag( $wrapping_figure_query ) ) {
    45 			$media_tag_processor->set_bookmark( 'last_figure' );
    62 			$media_tag_processor->set_bookmark( 'last_figure' );
    46 		}
    63 		}
    47 		if ( $media_tag_processor->has_bookmark( 'last_figure' ) ) {
    64 		if ( $media_tag_processor->has_bookmark( 'last_figure' ) ) {
    48 			$media_tag_processor->seek( 'last_figure' );
    65 			$media_tag_processor->seek( 'last_figure' );
    49 			if ( $image_fill ) {
    66 			// Insert a unique ID to identify the figure tag.
    50 				$media_tag_processor->set_attribute( 'style', 'background-image:url(' . esc_url( $current_featured_image ) . ');background-position:' . $focal_point . ';' );
    67 			$media_tag_processor->set_attribute( 'id', $unique_id );
    51 			} else {
       
    52 				// Insert a unique ID to identify the figure tag.
       
    53 				$media_tag_processor->set_attribute( 'id', $unique_id );
       
    54 			}
       
    55 		}
    68 		}
    56 	} else {
    69 	} else {
    57 		if ( $media_tag_processor->next_tag( $wrapping_figure_query ) ) {
    70 		if ( $media_tag_processor->next_tag( $wrapping_figure_query ) ) {
    58 			if ( $image_fill ) {
    71 			// Insert a unique ID to identify the figure tag.
    59 				$media_tag_processor->set_attribute( 'style', 'background-image:url(' . esc_url( $current_featured_image ) . ');background-position:' . $focal_point . ';' );
    72 			$media_tag_processor->set_attribute( 'id', $unique_id );
    60 			} else {
       
    61 				// Insert a unique ID to identify the figure tag.
       
    62 				$media_tag_processor->set_attribute( 'id', $unique_id );
       
    63 			}
       
    64 		}
    73 		}
    65 	}
    74 	}
    66 
    75 
    67 	$content = $media_tag_processor->get_updated_html();
    76 	$content = $media_tag_processor->get_updated_html();
    68 
    77 
    69 	// If the image is not set to fill, add the image tag inside the figure tag,
    78 	// Add the image tag inside the figure tag, and update the image attributes
    70 	// and update the image attributes in order to display the featured image.
    79 	// in order to display the featured image.
    71 	if ( ! $image_fill ) {
    80 	$media_size_slug = isset( $attributes['mediaSizeSlug'] ) ? $attributes['mediaSizeSlug'] : 'full';
    72 		$media_size_slug = isset( $attributes['mediaSizeSlug'] ) ? $attributes['mediaSizeSlug'] : 'full';
    81 	$image_tag       = '<img class="wp-block-media-text__featured_image">';
    73 		$image_tag       = '<img class="wp-block-media-text__featured_image">';
    82 	$content         = preg_replace(
    74 		$content         = preg_replace(
    83 		'/(<figure\s+id="' . preg_quote( $unique_id, '/' ) . '"\s+class="wp-block-media-text__media"\s*>)/',
    75 			'/(<figure\s+id="' . preg_quote( $unique_id, '/' ) . '"\s+class="wp-block-media-text__media"\s*>)/',
    84 		'$1' . $image_tag,
    76 			'$1' . $image_tag,
    85 		$content
    77 			$content
    86 	);
    78 		);
       
    79 
    87 
    80 		$image_tag_processor = new WP_HTML_Tag_Processor( $content );
    88 	$image_tag_processor = new WP_HTML_Tag_Processor( $content );
       
    89 	if ( $image_tag_processor->next_tag(
       
    90 		array(
       
    91 			'tag_name' => 'figure',
       
    92 			'id'       => $unique_id,
       
    93 		)
       
    94 	) ) {
       
    95 		// The ID is only used to ensure that the correct figure tag is selected,
       
    96 		// and can now be removed.
       
    97 		$image_tag_processor->remove_attribute( 'id' );
    81 		if ( $image_tag_processor->next_tag(
    98 		if ( $image_tag_processor->next_tag(
    82 			array(
    99 			array(
    83 				'tag_name' => 'figure',
   100 				'tag_name'   => 'img',
    84 				'id'       => $unique_id,
   101 				'class_name' => 'wp-block-media-text__featured_image',
    85 			)
   102 			)
    86 		) ) {
   103 		) ) {
    87 			// The ID is only used to ensure that the correct figure tag is selected,
   104 			$image_tag_processor->set_attribute( 'src', esc_url( $current_featured_image ) );
    88 			// and can now be removed.
   105 			$image_tag_processor->set_attribute( 'class', 'wp-image-' . get_post_thumbnail_id() . ' size-' . $media_size_slug );
    89 			$image_tag_processor->remove_attribute( 'id' );
   106 			$image_tag_processor->set_attribute( 'alt', trim( strip_tags( get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true ) ) ) );
    90 			if ( $image_tag_processor->next_tag(
   107 			if ( $image_fill ) {
    91 				array(
   108 				$image_tag_processor->set_attribute( 'style', 'object-position:' . $focal_point . ';' );
    92 					'tag_name'   => 'img',
   109 			}
    93 					'class_name' => 'wp-block-media-text__featured_image',
       
    94 				)
       
    95 			) ) {
       
    96 				$image_tag_processor->set_attribute( 'src', esc_url( $current_featured_image ) );
       
    97 				$image_tag_processor->set_attribute( 'class', 'wp-image-' . get_post_thumbnail_id() . ' size-' . $media_size_slug );
       
    98 				$image_tag_processor->set_attribute( 'alt', trim( strip_tags( get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true ) ) ) );
       
    99 
   110 
   100 				$content = $image_tag_processor->get_updated_html();
   111 			$content = $image_tag_processor->get_updated_html();
   101 			}
       
   102 		}
   112 		}
   103 	}
   113 	}
   104 
   114 
   105 	return $content;
   115 	return $content;
   106 }
   116 }