Media: revert the multi-mime feature.

This feature isn't quite ready to land.

Reverts r53786, r53848, r53847, r53845, r53751.

Props flixos90, azaozz, dd32.
See #55443.



git-svn-id: https://develop.svn.wordpress.org/trunk@54085 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Adam Silverstein
2022-09-06 21:11:41 +00:00
parent 9c45c90679
commit 87047ccf3a
11 changed files with 133 additions and 1838 deletions
-116
View File
@@ -1852,11 +1852,6 @@ function wp_filter_content_tags( $content, $context = null ) {
$filtered_image = wp_img_tag_add_decoding_attr( $filtered_image, $context );
}
// Use alternate mime types when specified and available.
if ( $attachment_id > 0 && _wp_in_front_end_context() ) {
$filtered_image = wp_image_use_alternate_mime_types( $filtered_image, $context, $attachment_id );
}
/**
* Filters an img tag within the content for a given context.
*
@@ -1903,117 +1898,6 @@ function wp_filter_content_tags( $content, $context = null ) {
return $content;
}
/**
* Use alternate mime type images in the front end content output when available.
*
* @since 6.1.0
*
* @param string $image The HTML `img` tag where the attribute should be added.
* @param string $context Additional context to pass to the filters.
* @param int $attachment_id The attachment ID.
* @return string Converted `img` tag with `loading` attribute added.
*/
function wp_image_use_alternate_mime_types( $image, $context, $attachment_id ) {
$metadata = wp_get_attachment_metadata( $attachment_id );
if ( empty( $metadata['file'] ) ) {
return $image;
}
// Only alter images with a `sources` attribute
if ( empty( $metadata['sources'] ) ) {
return $image;
};
$target_mimes = array( 'image/webp', 'image/jpeg' );
/**
* Filter the content image mime type output selection and order.
*
* When outputting images in the content, the first mime type available will be used.
*
* @since 6.1.0
*
* @param array $target_mimes The image output mime type and order. Default is array( 'image/webp', 'image/jpeg' ).
* @param int $attachment_id The attachment ID.
* @param string $context Additional context to pass to the filters.
* @return array The filtered output mime type and order. Return an empty array to skip mime type substitution.
*/
$target_mimes = apply_filters( 'wp_content_image_mimes', $target_mimes, $attachment_id, $context );
if ( false === $target_mimes ) {
return $image;
}
// Find the appropriate size for the provided URL in the first available mime type.
foreach ( $target_mimes as $target_mime ) {
// Handle full size image replacement.
if ( ! empty( $metadata['sources'][ $target_mime ]['file'] ) ) {
$src_filename = wp_basename( $metadata['file'] );
// This is the same MIME type as the original, so the entire $target_mime can be skipped.
// Since it is already the preferred MIME type, the entire loop can be cancelled.
if ( $metadata['sources'][ $target_mime ]['file'] === $src_filename ) {
break;
}
$image = str_replace( $src_filename, $metadata['sources'][ $target_mime ]['file'], $image );
// The full size was replaced, so unset this entirely here so that in the next iteration it is no longer
// considered, simply for a small performance optimization.
unset( $metadata['sources'] );
}
// Go through each image size and replace with the first available mime type version.
foreach ( $metadata['sizes'] as $name => $size_data ) {
// Check if size has an original file.
if ( empty( $size_data['file'] ) ) {
continue;
}
// Check if size has a source in the desired mime type.
if ( empty( $size_data['sources'][ $target_mime ]['file'] ) ) {
continue;
}
$src_filename = wp_basename( $size_data['file'] );
// This is the same MIME type as the original, so the entire $target_mime can be skipped.
// Since it is already the preferred MIME type, the entire loop can be cancelled.
if ( $size_data['sources'][ $target_mime ]['file'] === $src_filename ) {
break 2;
}
// Found a match, replace with the new filename.
$image = str_replace( $src_filename, $size_data['sources'][ $target_mime ]['file'], $image );
// This size was replaced, so unset this entirely here so that in the next iteration it is no longer
// considered, simply for a small performance optimization.
unset( $metadata['sizes'][ $name ] );
}
}
return $image;
}
/**
* Check if execution is currently in the front end content context, outside of <head>.
*
* @since 6.1.0
* @access private
*
* @return bool True if in the front end content context, false otherwise.
*/
function _wp_in_front_end_context() {
global $wp_query;
// Check if this request is generally outside (or before) any frontend context.
if ( ! isset( $wp_query ) || defined( 'XMLRPC_REQUEST' ) || defined( 'REST_REQUEST' ) || is_feed() ) {
return false;
}
// Check if we're anywhere before the 'wp_head' action has completed.
return did_action( 'template_redirect' ) && ! doing_action( 'wp_head' );
}
/**
* Adds `loading` attribute to an `img` HTML tag.
*