plugin updates
This commit is contained in:
@@ -1137,6 +1137,16 @@ function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = f
|
||||
}
|
||||
}
|
||||
|
||||
// Adds 'auto' to the sizes attribute if applicable.
|
||||
if (
|
||||
isset( $attr['loading'] ) &&
|
||||
'lazy' === $attr['loading'] &&
|
||||
isset( $attr['sizes'] ) &&
|
||||
! wp_sizes_attribute_includes_valid_auto( $attr['sizes'] )
|
||||
) {
|
||||
$attr['sizes'] = 'auto, ' . $attr['sizes'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the list of attachment image attributes.
|
||||
*
|
||||
@@ -1917,6 +1927,9 @@ function wp_filter_content_tags( $content, $context = null ) {
|
||||
// Add loading optimization attributes if applicable.
|
||||
$filtered_image = wp_img_tag_add_loading_optimization_attrs( $filtered_image, $context );
|
||||
|
||||
// Adds 'auto' to the sizes attribute if applicable.
|
||||
$filtered_image = wp_img_tag_add_auto_sizes( $filtered_image );
|
||||
|
||||
/**
|
||||
* Filters an img tag within the content for a given context.
|
||||
*
|
||||
@@ -1963,6 +1976,59 @@ function wp_filter_content_tags( $content, $context = null ) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds 'auto' to the sizes attribute to the image, if the image is lazy loaded and does not already include it.
|
||||
*
|
||||
* @since 6.7.0
|
||||
*
|
||||
* @param string $image The image tag markup being filtered.
|
||||
* @return string The filtered image tag markup.
|
||||
*/
|
||||
function wp_img_tag_add_auto_sizes( string $image ): string {
|
||||
$processor = new WP_HTML_Tag_Processor( $image );
|
||||
|
||||
// Bail if there is no IMG tag.
|
||||
if ( ! $processor->next_tag( array( 'tag_name' => 'IMG' ) ) ) {
|
||||
return $image;
|
||||
}
|
||||
|
||||
// Bail early if the image is not lazy-loaded.
|
||||
$value = $processor->get_attribute( 'loading' );
|
||||
if ( ! is_string( $value ) || 'lazy' !== strtolower( trim( $value, " \t\f\r\n" ) ) ) {
|
||||
return $image;
|
||||
}
|
||||
|
||||
$sizes = $processor->get_attribute( 'sizes' );
|
||||
|
||||
// Bail early if the image is not responsive.
|
||||
if ( ! is_string( $sizes ) ) {
|
||||
return $image;
|
||||
}
|
||||
|
||||
// Don't add 'auto' to the sizes attribute if it already exists.
|
||||
if ( wp_sizes_attribute_includes_valid_auto( $sizes ) ) {
|
||||
return $image;
|
||||
}
|
||||
|
||||
$processor->set_attribute( 'sizes', "auto, $sizes" );
|
||||
return $processor->get_updated_html();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the given 'sizes' attribute includes the 'auto' keyword as the first item in the list.
|
||||
*
|
||||
* Per the HTML spec, if present it must be the first entry.
|
||||
*
|
||||
* @since 6.7.0
|
||||
*
|
||||
* @param string $sizes_attr The 'sizes' attribute value.
|
||||
* @return bool True if the 'auto' keyword is present, false otherwise.
|
||||
*/
|
||||
function wp_sizes_attribute_includes_valid_auto( string $sizes_attr ): bool {
|
||||
list( $first_size ) = explode( ',', $sizes_attr, 2 );
|
||||
return 'auto' === strtolower( trim( $first_size, " \t\f\r\n" ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds optimization attributes to an `img` HTML tag.
|
||||
*
|
||||
@@ -1973,6 +2039,7 @@ function wp_filter_content_tags( $content, $context = null ) {
|
||||
* @return string Converted `img` tag with optimization attributes added.
|
||||
*/
|
||||
function wp_img_tag_add_loading_optimization_attrs( $image, $context ) {
|
||||
$src = preg_match( '/ src=["\']?([^"\']*)/i', $image, $matche_src ) ? $matche_src[1] : null;
|
||||
$width = preg_match( '/ width=["\']([0-9]+)["\']/', $image, $match_width ) ? (int) $match_width[1] : null;
|
||||
$height = preg_match( '/ height=["\']([0-9]+)["\']/', $image, $match_height ) ? (int) $match_height[1] : null;
|
||||
$loading_val = preg_match( '/ loading=["\']([A-Za-z]+)["\']/', $image, $match_loading ) ? $match_loading[1] : null;
|
||||
@@ -1987,6 +2054,7 @@ function wp_img_tag_add_loading_optimization_attrs( $image, $context ) {
|
||||
$optimization_attrs = wp_get_loading_optimization_attributes(
|
||||
'img',
|
||||
array(
|
||||
'src' => $src,
|
||||
'width' => $width,
|
||||
'height' => $height,
|
||||
'loading' => $loading_val,
|
||||
@@ -4064,8 +4132,7 @@ function wp_get_image_editor( $path, $args = array() ) {
|
||||
|
||||
// Check and set the output mime type mapped to the input type.
|
||||
if ( isset( $args['mime_type'] ) ) {
|
||||
/** This filter is documented in wp-includes/class-wp-image-editor.php */
|
||||
$output_format = apply_filters( 'image_editor_output_format', array(), $path, $args['mime_type'] );
|
||||
$output_format = wp_get_image_editor_output_format( $path, $args['mime_type'] );
|
||||
if ( isset( $output_format[ $args['mime_type'] ] ) ) {
|
||||
$args['output_mime_type'] = $output_format[ $args['mime_type'] ];
|
||||
}
|
||||
@@ -4124,7 +4191,22 @@ function _wp_image_editor_choose( $args = array() ) {
|
||||
* 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD'.
|
||||
*/
|
||||
$implementations = apply_filters( 'wp_image_editors', array( 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD' ) );
|
||||
$supports_input = false;
|
||||
|
||||
$editors = wp_cache_get( 'wp_image_editor_choose', 'image_editor' );
|
||||
|
||||
if ( ! is_array( $editors ) ) {
|
||||
$editors = array();
|
||||
}
|
||||
|
||||
// Cache the chosen editor implementation based on specific args and available implementations.
|
||||
$cache_key = md5( serialize( array( $args, $implementations ) ) );
|
||||
|
||||
if ( isset( $editors[ $cache_key ] ) ) {
|
||||
return $editors[ $cache_key ];
|
||||
}
|
||||
|
||||
// Assume no support until a capable implementation is identified.
|
||||
$editor = false;
|
||||
|
||||
foreach ( $implementations as $implementation ) {
|
||||
if ( ! call_user_func( array( $implementation, 'test' ), $args ) ) {
|
||||
@@ -4158,15 +4240,20 @@ function _wp_image_editor_choose( $args = array() ) {
|
||||
* This implementation supports the input type but not the output type.
|
||||
* Keep looking to see if we can find an implementation that supports both.
|
||||
*/
|
||||
$supports_input = $implementation;
|
||||
$editor = $implementation;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Favor the implementation that supports both input and output mime types.
|
||||
return $implementation;
|
||||
$editor = $implementation;
|
||||
break;
|
||||
}
|
||||
|
||||
return $supports_input;
|
||||
$editors[ $cache_key ] = $editor;
|
||||
|
||||
wp_cache_set( 'wp_image_editor_choose', $editors, 'image_editor', DAY_IN_SECONDS );
|
||||
|
||||
return $editor;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -4224,6 +4311,11 @@ function wp_plupload_default_settings() {
|
||||
$defaults['avif_upload_error'] = true;
|
||||
}
|
||||
|
||||
// Check if HEIC images can be edited.
|
||||
if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/heic' ) ) ) {
|
||||
$defaults['heic_upload_error'] = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the Plupload default settings.
|
||||
*
|
||||
@@ -5311,6 +5403,31 @@ function wp_maybe_generate_attachment_metadata( $attachment ) {
|
||||
function attachment_url_to_postid( $url ) {
|
||||
global $wpdb;
|
||||
|
||||
/**
|
||||
* Filters the attachment ID to allow short-circuit the function.
|
||||
*
|
||||
* Allows plugins to short-circuit attachment ID lookups. Plugins making
|
||||
* use of this function should return:
|
||||
*
|
||||
* - 0 (integer) to indicate the attachment is not found,
|
||||
* - attachment ID (integer) to indicate the attachment ID found,
|
||||
* - null to indicate WordPress should proceed with the lookup.
|
||||
*
|
||||
* Warning: The post ID may be null or zero, both of which cast to a
|
||||
* boolean false. For information about casting to booleans see the
|
||||
* {@link https://www.php.net/manual/en/language.types.boolean.php PHP documentation}.
|
||||
* Use the === operator for testing the post ID when developing filters using
|
||||
* this hook.
|
||||
*
|
||||
* @param int|null $post_id The result of the post ID lookup. Null to indicate
|
||||
* no lookup has been attempted. Default null.
|
||||
* @param string $url The URL being looked up.
|
||||
*/
|
||||
$post_id = apply_filters( 'pre_attachment_url_to_postid', null, $url );
|
||||
if ( null !== $post_id ) {
|
||||
return (int) $post_id;
|
||||
}
|
||||
|
||||
$dir = wp_get_upload_dir();
|
||||
$path = $url;
|
||||
|
||||
@@ -5483,12 +5600,17 @@ function _wp_add_additional_image_sizes() {
|
||||
* Callback to enable showing of the user error when uploading .heic images.
|
||||
*
|
||||
* @since 5.5.0
|
||||
* @since 6.7.0 The default behavior is to enable heic uploads as long as the server
|
||||
* supports the format. The uploads are converted to JPEG's by default.
|
||||
*
|
||||
* @param array[] $plupload_settings The settings for Plupload.js.
|
||||
* @return array[] Modified settings for Plupload.js.
|
||||
*/
|
||||
function wp_show_heic_upload_error( $plupload_settings ) {
|
||||
$plupload_settings['heic_upload_error'] = true;
|
||||
// Check if HEIC images can be edited.
|
||||
if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/heic' ) ) ) {
|
||||
$plupload_init['heic_upload_error'] = true;
|
||||
}
|
||||
return $plupload_settings;
|
||||
}
|
||||
|
||||
@@ -5505,9 +5627,7 @@ function wp_show_heic_upload_error( $plupload_settings ) {
|
||||
*/
|
||||
function wp_getimagesize( $filename, ?array &$image_info = null ) {
|
||||
// Don't silence errors when in debug mode, unless running unit tests.
|
||||
if ( defined( 'WP_DEBUG' ) && WP_DEBUG
|
||||
&& ! defined( 'WP_RUN_CORE_TESTS' )
|
||||
) {
|
||||
if ( defined( 'WP_DEBUG' ) && WP_DEBUG && ! defined( 'WP_RUN_CORE_TESTS' ) ) {
|
||||
if ( 2 === func_num_args() ) {
|
||||
$info = getimagesize( $filename, $image_info );
|
||||
} else {
|
||||
@@ -5538,11 +5658,18 @@ function wp_getimagesize( $filename, ?array &$image_info = null ) {
|
||||
return $info;
|
||||
}
|
||||
|
||||
$image_mime_type = wp_get_image_mime( $filename );
|
||||
|
||||
// Not an image?
|
||||
if ( false === $image_mime_type ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* For PHP versions that don't support WebP images,
|
||||
* extract the image size info from the file headers.
|
||||
*/
|
||||
if ( 'image/webp' === wp_get_image_mime( $filename ) ) {
|
||||
if ( 'image/webp' === $image_mime_type ) {
|
||||
$webp_info = wp_get_webp_info( $filename );
|
||||
$width = $webp_info['width'];
|
||||
$height = $webp_info['height'];
|
||||
@@ -5564,7 +5691,7 @@ function wp_getimagesize( $filename, ?array &$image_info = null ) {
|
||||
}
|
||||
|
||||
// For PHP versions that don't support AVIF images, extract the image size info from the file headers.
|
||||
if ( 'image/avif' === wp_get_image_mime( $filename ) ) {
|
||||
if ( 'image/avif' === $image_mime_type ) {
|
||||
$avif_info = wp_get_avif_info( $filename );
|
||||
|
||||
$width = $avif_info['width'];
|
||||
@@ -5586,6 +5713,31 @@ function wp_getimagesize( $filename, ?array &$image_info = null ) {
|
||||
}
|
||||
}
|
||||
|
||||
// For PHP versions that don't support HEIC images, extract the size info using Imagick when available.
|
||||
if ( wp_is_heic_image_mime_type( $image_mime_type ) ) {
|
||||
$editor = wp_get_image_editor( $filename );
|
||||
|
||||
if ( is_wp_error( $editor ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// If the editor for HEICs is Imagick, use it to get the image size.
|
||||
if ( $editor instanceof WP_Image_Editor_Imagick ) {
|
||||
$size = $editor->get_size();
|
||||
return array(
|
||||
$size['width'],
|
||||
$size['height'],
|
||||
IMAGETYPE_HEIC,
|
||||
sprintf(
|
||||
'width="%d" height="%d"',
|
||||
$size['width'],
|
||||
$size['height']
|
||||
),
|
||||
'mime' => 'image/heic',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// The image could not be parsed.
|
||||
return false;
|
||||
}
|
||||
@@ -6069,3 +6221,45 @@ function wp_high_priority_element_flag( $value = null ) {
|
||||
|
||||
return $high_priority_element;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines the output format for the image editor.
|
||||
*
|
||||
* @since 6.7.0
|
||||
* @access private
|
||||
*
|
||||
* @param string $filename Path to the image.
|
||||
* @param string $mime_type The source image mime type.
|
||||
* @return string[] An array of mime type mappings.
|
||||
*/
|
||||
function wp_get_image_editor_output_format( $filename, $mime_type ) {
|
||||
$output_format = array(
|
||||
'image/heic' => 'image/jpeg',
|
||||
'image/heif' => 'image/jpeg',
|
||||
'image/heic-sequence' => 'image/jpeg',
|
||||
'image/heif-sequence' => 'image/jpeg',
|
||||
);
|
||||
|
||||
/**
|
||||
* Filters the image editor output format mapping.
|
||||
*
|
||||
* Enables filtering the mime type used to save images. By default HEIC/HEIF images
|
||||
* are converted to JPEGs.
|
||||
*
|
||||
* @see WP_Image_Editor::get_output_format()
|
||||
*
|
||||
* @since 5.8.0
|
||||
* @since 6.7.0 The default was changed from an empty array to an array
|
||||
* containing the HEIC/HEIF images mime types.
|
||||
*
|
||||
* @param string[] $output_format {
|
||||
* An array of mime type mappings. Maps a source mime type to a new
|
||||
* destination mime type. By default maps HEIC/HEIF input to JPEG output.
|
||||
*
|
||||
* @type string ...$0 The new mime type.
|
||||
* }
|
||||
* @param string $filename Path to the image.
|
||||
* @param string $mime_type The source image mime type.
|
||||
*/
|
||||
return apply_filters( 'image_editor_output_format', $output_format, $filename, $mime_type );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user