Ticket #55443: 55443.diff
File 55443.diff, 48.0 KB (added by , 3 years ago) |
---|
-
src/js/_enqueues/vendor/plupload/handlers.js
diff --git src/js/_enqueues/vendor/plupload/handlers.js src/js/_enqueues/vendor/plupload/handlers.js index fa602daf43..c7e3c253d3 100644
jQuery( document ).ready( function( $ ) { 486 486 487 487 times = tryAgainCount[ file.id ]; 488 488 489 if ( times && times > 4) {489 if ( times && times > 8 ) { 490 490 /* 491 491 * The file may have been uploaded and attachment post created, 492 492 * but post-processing and resizing failed... -
src/js/_enqueues/vendor/plupload/wp-plupload.js
diff --git src/js/_enqueues/vendor/plupload/wp-plupload.js src/js/_enqueues/vendor/plupload/wp-plupload.js index 0fdebf77d1..217b3c09e2 100644
window.wp = window.wp || {}; 138 138 139 139 times = tryAgainCount[ file.id ]; 140 140 141 if ( times && times > 4) {141 if ( times && times > 8 ) { 142 142 /* 143 143 * The file may have been uploaded and attachment post created, 144 144 * but post-processing and resizing failed... -
src/wp-admin/includes/image.php
diff --git src/wp-admin/includes/image.php src/wp-admin/includes/image.php index 2818b3dd5d..ba9ef42712 100644
function _wp_image_meta_replace_original( $saved_data, $original_file, $image_me 217 217 } 218 218 219 219 /** 220 * Creates image sub-sizes, adds the new data to the image meta `sizes` array, and updates the image metadata.220 * Creates image mime variations and sub-sizes, adds the new data to the image meta `sizes` array, and updates the image metadata. 221 221 * 222 222 * Intended for use after an image is uploaded. Saves/updates the image metadata after each 223 223 * sub-size is created. If there was an error, it is added to the returned image metadata array. 224 224 * 225 225 * @since 5.3.0 226 * @since 6.0.0 Generates sub-sizes in alternate mime types based on the `wp_image_mime_transforms` filter. 226 227 * 227 228 * @param string $file Full path to the image file. 228 229 * @param int $attachment_id Attachment ID to process. … … function wp_create_image_subsizes( $file, $attachment_id ) { 252 253 $image_meta['image_meta'] = $exif_meta; 253 254 } 254 255 256 // Calculate the primary and additional mime types to generate. 257 $upload_mime_transforms = wp_upload_image_mime_transforms( $attachment_id ); 258 $mime_type = wp_get_image_mime( $file ); 259 list( $primary_mime_type, $additional_mime_types ) = _wp_get_primary_and_additional_mime_types( $upload_mime_transforms, $mime_type ); 260 261 // Add a top level sources array for the full size images. 262 if ( ! isset( $image_meta['sources'] ) || ! is_array( $image_meta['sources'] ) ) { 263 $image_meta['sources'] = array(); 264 } 265 266 // Populate the top level primary mime type data. 267 if ( empty( $image_meta['sources'][ $primary_mime_type ] ) ) { 268 $image_meta['sources'][ $primary_mime_type ] = _wp_get_sources_from_meta( $image_meta ); 269 wp_update_attachment_metadata( $attachment_id, $image_meta ); 270 } 271 255 272 // Do not scale (large) PNG images. May result in sub-sizes that have greater file size than the original. See #48736. 256 273 if ( 'image/png' !== $imagesize['mime'] ) { 257 274 … … function wp_create_image_subsizes( $file, $attachment_id ) { 301 318 if ( ! is_wp_error( $resized ) ) { 302 319 // Append "-scaled" to the image file name. It will look like "my_image-scaled.jpg". 303 320 // This doesn't affect the sub-sizes names as they are generated from the original image (for best quality). 304 $saved = $editor->save( $editor->generate_filename( 'scaled' ) );321 $saved = $editor->save( $editor->generate_filename( 'scaled' ), $primary_mime_type ); 305 322 306 323 if ( ! is_wp_error( $saved ) ) { 307 324 $image_meta = _wp_image_meta_replace_original( $saved, $file, $image_meta, $attachment_id ); 308 325 326 $image_meta['sources'][ $primary_mime_type ] = _wp_get_sources_from_meta( $saved ); 327 309 328 // If the image was rotated update the stored EXIF data. 310 329 if ( true === $rotated && ! empty( $image_meta['image_meta']['orientation'] ) ) { 311 330 $image_meta['image_meta']['orientation'] = 1; 312 331 } 332 wp_update_attachment_metadata( $attachment_id, $image_meta ); 313 333 } else { 314 334 // TODO: Log errors. 315 335 } 336 337 // Save additional mime types. 338 foreach ( $additional_mime_types as $output_mime_type ) { 339 $extension = wp_get_default_extension_for_mime_type( $output_mime_type ); 340 // @todo Correct issue where filename winds up with duplicate "-scaled" suffix added unless the editor is reset. 341 // adding extension to name for now. 342 $saved = $editor->save( $editor->generate_filename( $extension ), $output_mime_type ); 343 if ( ! is_wp_error( $saved ) ) { 344 $image_meta['sources'][ $output_mime_type ] = _wp_get_sources_from_meta( $saved ); 345 wp_update_attachment_metadata( $attachment_id, $image_meta ); 346 } 347 } 316 348 } else { 317 349 // TODO: Log errors. 318 350 } 319 } elseif ( ! empty( $exif_meta['orientation'] ) && 1 !== (int) $exif_meta['orientation'] ) { 320 // Rotate the whole original image if there is EXIF data and "orientation" is not 1. 351 } else { 352 // Populate the top level additional mime type data. 353 foreach ( $additional_mime_types as $output_mime_type ) { 354 if ( empty( $image_meta['sources'][ $output_mime_type ] ) ) { 355 $extension = wp_get_default_extension_for_mime_type( $output_mime_type ); 356 $saved = $editor->save( $editor->generate_filename( null, null, $extension ), $output_mime_type ); 357 if ( ! is_wp_error( $saved ) ) { 358 $image_meta['sources'][ $output_mime_type ] = _wp_get_sources_from_meta( $saved ); 359 wp_update_attachment_metadata( $attachment_id, $image_meta ); 360 } 361 } 362 } 321 363 322 $editor = wp_get_image_editor( $file ); 364 if ( ! empty( $exif_meta['orientation'] ) && 1 !== (int) $exif_meta['orientation'] ) { 365 // Rotate the whole original image if there is EXIF data and "orientation" is not 1. 323 366 324 if ( is_wp_error( $editor ) ) { 325 // This image cannot be edited. 326 return $image_meta; 327 } 367 $editor = wp_get_image_editor( $file ); 328 368 329 // Rotate the image. 330 $rotated = $editor->maybe_exif_rotate(); 369 if ( is_wp_error( $editor ) ) { 370 // This image cannot be edited. 371 return $image_meta; 372 } 331 373 332 if ( true === $rotated ) { 333 // Append `-rotated` to the image file name. 334 $saved = $editor->save( $editor->generate_filename( 'rotated' ) ); 374 // Rotate the image. 375 $rotated = $editor->maybe_exif_rotate(); 335 376 336 if ( ! is_wp_error( $saved ) ) { 337 $image_meta = _wp_image_meta_replace_original( $saved, $file, $image_meta, $attachment_id ); 377 if ( true === $rotated ) { 378 // Append `-rotated` to the image file name. 379 $saved = $editor->save( $editor->generate_filename( 'rotated' ), $primary_mime_type ); 338 380 339 // Update the stored EXIF data. 340 if ( ! empty( $image_meta['image_meta']['orientation'] ) ) { 341 $image_meta['image_meta']['orientation'] = 1; 381 if ( ! is_wp_error( $saved ) ) { 382 $image_meta = _wp_image_meta_replace_original( $saved, $file, $image_meta, $attachment_id ); 383 384 $image_meta['sources'][ $primary_mime_type ] = _wp_get_sources_from_meta( $saved ); 385 386 // Update the stored EXIF data. 387 if ( ! empty( $image_meta['image_meta']['orientation'] ) ) { 388 $image_meta['image_meta']['orientation'] = 1; 389 } 390 wp_update_attachment_metadata( $attachment_id, $image_meta ); 391 } else { 392 // TODO: Log errors. 393 } 394 395 // Save additional mime types. 396 foreach ( $additional_mime_types as $output_mime_type ) { 397 $extension = wp_get_default_extension_for_mime_type( $output_mime_type ); 398 // @todo Correct issue where filename winds up with duplicate "-rotated" suffix added unless the editor is reset. 399 // adding extension to name for now. 400 $saved = $editor->save( $editor->generate_filename( $extension ), $output_mime_type ); 401 if ( ! is_wp_error( $saved ) ) { 402 $image_meta['sources'][ $output_mime_type ] = _wp_get_sources_from_meta( $saved ); 403 wp_update_attachment_metadata( $attachment_id, $image_meta ); 404 } 342 405 } 343 } else {344 // TODO: Log errors.345 406 } 346 407 } 347 408 } … … function wp_create_image_subsizes( $file, $attachment_id ) { 372 433 return _wp_make_subsizes( $new_sizes, $file, $image_meta, $attachment_id ); 373 434 } 374 435 436 /** 437 * Gets a sources array element from a meta. 438 * 439 * @since 6.0.0 440 * @access private 441 * 442 * @param array $meta The meta to get the source from. 443 * @return array The source array element. 444 */ 445 function _wp_get_sources_from_meta( $meta ) { 446 return array( 447 'file' => wp_basename( $meta['file'] ), 448 'filesize' => isset( $meta['filesize'] ) ? $meta['filesize'] : wp_filesize( $meta['path'] ), 449 ); 450 } 451 375 452 /** 376 453 * Low-level function to create image sub-sizes. 377 454 * … … function wp_create_image_subsizes( $file, $attachment_id ) { 379 456 * Errors are stored in the returned image metadata array. 380 457 * 381 458 * @since 5.3.0 459 * @since 6.0.0 Support for generating multiple mime type sub-sizes was added. 382 460 * @access private 383 461 * 384 462 * @param array $new_sizes Array defining what sizes to create. … … function _wp_make_subsizes( $new_sizes, $file, $image_meta, $attachment_id ) { 414 492 return $image_meta; 415 493 } 416 494 495 // Calculate the primary and additional mime types to generate. 496 $upload_mime_transforms = wp_upload_image_mime_transforms( $attachment_id ); 497 $mime_type = wp_get_image_mime( $file ); 498 list( $primary_mime_type, $additional_mime_types ) = _wp_get_primary_and_additional_mime_types( $upload_mime_transforms, $mime_type ); 499 417 500 /* 418 501 * Sort the image sub-sizes in order of priority when creating them. 419 502 * This ensures there is an appropriate sub-size the user can access immediately … … function _wp_make_subsizes( $new_sizes, $file, $image_meta, $attachment_id ) { 445 528 } 446 529 447 530 if ( method_exists( $editor, 'make_subsize' ) ) { 531 // Make the primary mime type sub sized images 448 532 foreach ( $new_sizes as $new_size_name => $new_size_data ) { 449 $new_size_meta = $editor->make_subsize( $new_size_data ); 450 533 $new_size_meta = $editor->make_subsize( $new_size_data, $primary_mime_type ); 451 534 if ( is_wp_error( $new_size_meta ) ) { 452 535 // TODO: Log errors. 453 536 } else { 454 537 // Save the size meta value. 455 538 $image_meta['sizes'][ $new_size_name ] = $new_size_meta; 539 if ( ! isset( $image_meta['sizes'][ $new_size_name ]['sources'] ) ) { 540 $image_meta['sizes'][ $new_size_name ]['sources'] = array(); 541 } 542 $image_meta['sizes'][ $new_size_name ]['sources'][ $primary_mime_type ] = _wp_get_sources_from_meta( $new_size_meta ); 456 543 wp_update_attachment_metadata( $attachment_id, $image_meta ); 457 544 } 545 546 // Save additional mime types for this size. 547 foreach ( $additional_mime_types as $output_mime_type ) { 548 $new_size_meta = $editor->make_subsize( $new_size_data, $output_mime_type ); 549 550 if ( ! is_wp_error( $new_size_meta ) ) { 551 $image_meta['sizes'][ $new_size_name ]['sources'][ $output_mime_type ] = _wp_get_sources_from_meta( $new_size_meta ); 552 wp_update_attachment_metadata( $attachment_id, $image_meta ); 553 } 554 } 458 555 } 459 556 } else { 460 557 // Fall back to `$editor->multi_resize()`. … … function wp_generate_attachment_metadata( $attachment_id, $file ) { 651 748 */ 652 749 return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id, 'create' ); 653 750 } 654 655 751 /** 656 752 * Convert a fraction string to a decimal. 657 753 * … … function _copy_image_file( $attachment_id ) { 1143 1239 1144 1240 return $dst_file; 1145 1241 } 1242 1243 /** 1244 * Returns an array with the list of valid mime types that a specific mime type should be converted into. 1245 * For example an `image/jpeg` should be converted into an `image/jpeg` and `image/webp`. The first type 1246 * is considered the primary output type for this image. 1247 * 1248 * @since 6.0.0 1249 * 1250 * @param $attachment_id int The attachment ID. 1251 * @return array<string, array<string>> An array of valid mime types, where the key is the mime type and the value is the extension type. 1252 */ 1253 function wp_upload_image_mime_transforms( $attachment_id ) { 1254 $image_mime_transforms = array( 1255 'image/jpeg' => array( 'image/jpeg', 'image/webp' ), 1256 'image/webp' => array( 'image/webp', 'image/jpeg' ), 1257 ); 1258 1259 /** 1260 * Filter to the output mime types for a given input mime type. 1261 * 1262 * @since 6.0.0 1263 * 1264 * @param array $image_mime_transforms A map with the valid mime transforms. 1265 * @param int $attachment_id The ID of the attachment where the hook was dispatched. 1266 */ 1267 return (array) apply_filters( 'wp_upload_image_mime_transforms', $image_mime_transforms, $attachment_id ); 1268 } 1269 1270 /** 1271 * Extract the primary and additional mime output types for an image from the $image_mime_transforms. 1272 * 1273 * @since 6.0.0 1274 * @access private 1275 * 1276 * @param $image_mime_transforms array<string, array<string>> An array of valid mime types, where the key is the mime type and the value is the extension type. 1277 * @param $original_mime_type string The mime type of the image. 1278 * @return array<string, array<string>> An array with the primary mime type and the additional mime types. 1279 */ 1280 function _wp_get_primary_and_additional_mime_types( $image_mime_transforms, $original_mime_type ) { 1281 $output_mime_types = isset( $image_mime_transforms[ $original_mime_type ] ) ? $image_mime_transforms[ $original_mime_type ] : array( $original_mime_type ); 1282 1283 // Use original mime type as primary mime type, or alternatively the first one. 1284 $primary_mime_type_key = array_search( $original_mime_type, $output_mime_types, true ); 1285 if ( false === $primary_mime_type_key ) { 1286 $primary_mime_type_key = 0; 1287 } 1288 // Split output mime types into primary mime type and additional mime types. 1289 $additional_mime_types = $output_mime_types; 1290 list( $primary_mime_type ) = array_splice( $additional_mime_types, $primary_mime_type_key, 1 ); 1291 1292 // Ensure $primary_mime_type is set. 1293 if ( empty( $primary_mime_type ) ) { 1294 $primary_mime_type = $original_mime_type; 1295 } 1296 return array( 1297 $primary_mime_type, 1298 $additional_mime_types, 1299 ); 1300 } -
src/wp-includes/class-wp-image-editor-gd.php
diff --git src/wp-includes/class-wp-image-editor-gd.php src/wp-includes/class-wp-image-editor-gd.php index 80e58f77cf..4d7cc73f06 100644
class WP_Image_Editor_GD extends WP_Image_Editor { 247 247 $metadata = array(); 248 248 249 249 foreach ( $sizes as $size => $size_data ) { 250 $meta = $this->make_subsize( $size_data );250 $meta = $this->make_subsize( $size_data, null ); 251 251 252 252 if ( ! is_wp_error( $meta ) ) { 253 253 $metadata[ $size ] = $meta; … … class WP_Image_Editor_GD extends WP_Image_Editor { 261 261 * Create an image sub-size and return the image meta data value for it. 262 262 * 263 263 * @since 5.3.0 264 * @since 6.0.0 Added the $mime_type parameter. 264 265 * 265 * @param array $size_data {266 * @param array $size_data { 266 267 * Array of size data. 267 268 * 268 269 * @type int $width The maximum width in pixels. 269 270 * @type int $height The maximum height in pixels. 270 271 * @type bool $crop Whether to crop the image to exact dimensions. 271 272 * } 273 * @param string $mime_type Optional. The mime type of the image. 272 274 * @return array|WP_Error The image data array for inclusion in the `sizes` array in the image meta, 273 275 * WP_Error object on error. 274 276 */ 275 public function make_subsize( $size_data ) {277 public function make_subsize( $size_data, $mime_type = null ) { 276 278 if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) { 277 279 return new WP_Error( 'image_subsize_create_error', __( 'Cannot resize the image. Both width and height are not set.' ) ); 278 280 } … … class WP_Image_Editor_GD extends WP_Image_Editor { 296 298 if ( is_wp_error( $resized ) ) { 297 299 $saved = $resized; 298 300 } else { 299 $saved = $this->_save( $resized );301 $saved = $this->_save( $resized, null, $mime_type ); 300 302 imagedestroy( $resized ); 301 303 } 302 304 -
src/wp-includes/class-wp-image-editor-imagick.php
diff --git src/wp-includes/class-wp-image-editor-imagick.php src/wp-includes/class-wp-image-editor-imagick.php index 7301c9b710..8ba376e192 100644
class WP_Image_Editor_Imagick extends WP_Image_Editor { 458 458 $metadata = array(); 459 459 460 460 foreach ( $sizes as $size => $size_data ) { 461 $meta = $this->make_subsize( $size_data );461 $meta = $this->make_subsize( $size_data, null ); 462 462 463 463 if ( ! is_wp_error( $meta ) ) { 464 464 $metadata[ $size ] = $meta; … … class WP_Image_Editor_Imagick extends WP_Image_Editor { 472 472 * Create an image sub-size and return the image meta data value for it. 473 473 * 474 474 * @since 5.3.0 475 * @since 6.0.0 Added the $mime_type parameter. 475 476 * 476 * @param array $size_data {477 * @param array $size_data { 477 478 * Array of size data. 478 479 * 479 480 * @type int $width The maximum width in pixels. 480 481 * @type int $height The maximum height in pixels. 481 482 * @type bool $crop Whether to crop the image to exact dimensions. 482 483 * } 484 * @param string $mime_type Optional. The mime type of the image. 483 485 * @return array|WP_Error The image data array for inclusion in the `sizes` array in the image meta, 484 486 * WP_Error object on error. 485 487 */ 486 public function make_subsize( $size_data ) {488 public function make_subsize( $size_data, $mime_type = null ) { 487 489 if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) { 488 490 return new WP_Error( 'image_subsize_create_error', __( 'Cannot resize the image. Both width and height are not set.' ) ); 489 491 } … … class WP_Image_Editor_Imagick extends WP_Image_Editor { 508 510 if ( is_wp_error( $resized ) ) { 509 511 $saved = $resized; 510 512 } else { 511 $saved = $this->_save( $this->image );513 $saved = $this->_save( $this->image, null, $mime_type ); 512 514 513 515 $this->image->clear(); 514 516 $this->image->destroy(); -
src/wp-includes/media.php
diff --git src/wp-includes/media.php src/wp-includes/media.php index a0e7ef9c6c..ab72f8fc8c 100644
function wp_filter_content_tags( $content, $context = null ) { 1843 1843 $filtered_image = wp_img_tag_add_loading_attr( $filtered_image, $context ); 1844 1844 } 1845 1845 1846 // Use alternate mime types when specified and available. 1847 if ( $attachment_id > 0 ) { 1848 $filtered_image = wp_image_use_alternate_mime_types( $filtered_image, $context, $attachment_id ); 1849 } 1850 1846 1851 if ( $filtered_image !== $match[0] ) { 1847 1852 $content = str_replace( $match[0], $filtered_image, $content ); 1848 1853 } … … function wp_filter_content_tags( $content, $context = null ) { 1866 1871 return $content; 1867 1872 } 1868 1873 1874 /** 1875 * Use alternate mime type images in the content output when available. 1876 * 1877 * @since 6.0.0 1878 * 1879 * @param string $image The HTML `img` tag where the attribute should be added. 1880 * @param string $context Additional context to pass to the filters. 1881 * @param int $attachment_id The attachment ID. 1882 * @return string Converted `img` tag with `loading` attribute added. 1883 */ 1884 function wp_image_use_alternate_mime_types( $image, $context, $attachment_id ) { 1885 $metadata = wp_get_attachment_metadata( $attachment_id ); 1886 if ( empty( $metadata['file'] ) ) { 1887 return $image; 1888 } 1889 1890 // Only alter images with a `sources` attribute 1891 if ( empty( $metadata['sources'] ) ) { 1892 return $image; 1893 }; 1894 1895 $target_mimes = array( 'image/webp', 'image/jpeg' ); 1896 1897 /** 1898 * Filter the content image mime type output selection and order. 1899 * 1900 * When outputting images in the content, the first mime type available will be used. 1901 * 1902 * @since 6.0.0 1903 * 1904 * @param array $target_mimes The image output mime type and order. Default is array( 'image/webp', 'image/jpeg' ). 1905 * @param int $attachment_id The attachment ID. 1906 * @param string $context Additional context to pass to the filters. 1907 * @return array The filtered output mime type and order. Return an empty array to skip mime type substitution. 1908 */ 1909 $target_mimes = apply_filters( 'wp_content_image_mimes', $target_mimes, $attachment_id, $context ); 1910 1911 if ( false === $target_mimes ) { 1912 return $image; 1913 } 1914 1915 // Find the appropriate size for the provided URL in the first available mime type. 1916 foreach ( $target_mimes as $target_mime ) { 1917 if ( ! isset( $metadata['sources'][ $target_mime ] ) || empty( $metadata['sources'][ $target_mime ]['file'] ) ) { 1918 continue; 1919 } 1920 1921 // Go through each image and replace with the first available mime type version. 1922 foreach ( $metadata['sizes'] as $name => $size_data ) { 1923 // Check if size has a file. 1924 if ( empty( $size_data['file'] ) ) { 1925 continue; 1926 } 1927 1928 // Check if size has a source in the desired mime type. 1929 if ( empty( $size_data['sources'][ $target_mime ]['file'] ) ) { 1930 continue; 1931 } 1932 $target_file = $size_data['sources'][ $target_mime ]['file']; 1933 1934 // Replace the existing output image for this size. 1935 $src_filename = wp_basename( $size_data['file'] ); 1936 1937 // This is the same as the file we want to replace nothing to do here. 1938 if ( $target_file === $src_filename ) { 1939 continue; 1940 } 1941 1942 // Found a match, replace with the new filename and stop searching. 1943 $image = str_replace( $src_filename, $size_data['sources'][ $target_mime ]['file'], $image ); 1944 continue; 1945 } 1946 1947 // Handle full size image replacement. 1948 $src_filename = wp_basename( $metadata['file'] ); 1949 1950 // This is the same as the file we want to replace nothing else to do here. 1951 if ( $metadata['sources'][ $target_mime ]['file'] === $src_filename ) { 1952 return $image; 1953 } 1954 1955 $image = str_replace( $src_filename, $metadata['sources'][ $target_mime ]['file'], $image ); 1956 } 1957 return $image; 1958 } 1959 1869 1960 /** 1870 1961 * Adds `loading` attribute to an `img` HTML tag. 1871 1962 * -
src/wp-includes/post.php
diff --git src/wp-includes/post.php src/wp-includes/post.php index 3906e21f7c..4c1aa0327b 100644
function wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ) { 6480 6480 $deleted = false; 6481 6481 } 6482 6482 } 6483 // Check for alternate size mime types in the sizeinfo['sources'] array to delete. 6484 if ( isset( $sizeinfo['sources'] ) && is_array( $sizeinfo['sources'] ) && count( $sizeinfo['sources'] ) > 1 ) { 6485 $sources = $sizeinfo['sources']; 6486 array_shift( $sources ); 6487 foreach ( $sources as $mime => $properties ) { 6488 if ( ! is_array( $properties ) || empty( $properties['file'] ) ) { 6489 continue; 6490 } 6491 6492 $intermediate_file = str_replace( wp_basename( $file ), $properties['file'], $file ); 6493 if ( ! wp_delete_file_from_directory( $intermediate_file, $intermediate_dir ) ) { 6494 $deleted = false; 6495 } 6496 } 6497 } 6483 6498 } 6484 6499 } 6485 6500 … … function wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ) { 6499 6514 } 6500 6515 } 6501 6516 6517 // Check for alternate full size mime types in the root sources array to delete. 6518 if ( isset( $meta['sources'] ) && is_array( $meta['sources'] ) ) { 6519 $sources = $meta['sources']; 6520 array_shift( $sources ); 6521 foreach ( $sources as $mime => $properties ) { 6522 if ( ! is_array( $properties ) || empty( $properties['file'] ) ) { 6523 continue; 6524 } 6525 6526 $intermediate_file = str_replace( wp_basename( $file ), $properties['file'], $file ); 6527 if ( ! wp_delete_file_from_directory( $intermediate_file, $intermediate_dir ) ) { 6528 $deleted = false; 6529 } 6530 } 6531 } 6532 6502 6533 if ( is_array( $backup_sizes ) ) { 6503 6534 $del_dir = path_join( $uploadpath['basedir'], dirname( $meta['file'] ) ); 6504 6535 -
tests/phpunit/tests/image/editor.php
diff --git tests/phpunit/data/images/test-image.jpeg tests/phpunit/data/images/test-image.jpeg new file mode 100644 index 0000000000..534aac1d6b Binary files /dev/null and tests/phpunit/data/images/test-image.jpeg differ diff --git tests/phpunit/tests/image/editor.php tests/phpunit/tests/image/editor.php index 487dad0664..06d31e83f1 100644
class Tests_Image_Editor extends WP_Image_UnitTestCase { 361 361 ), 362 362 ); 363 363 } 364 /** 365 * Create the original image mime type when the image is uploaded 366 * 367 * @dataProvider provider_image_with_default_behaviors_during_upload 368 * 369 * @since 6.0.0 370 */ 371 public function it_should_create_the_original_image_mime_type_when_the_image_is_uploaded( $file_location, $expected_mime, $targeted_mime ) { 372 $attachment_id = $this->factory->attachment->create_upload_object( $file_location ); 373 374 $metadata = wp_get_attachment_metadata( $attachment_id ); 375 376 $this->assertIsArray( $metadata ); 377 foreach ( $metadata['sizes'] as $size_name => $properties ) { 378 $this->assertArrayHasKey( 'sources', $properties ); 379 $this->assertIsArray( $properties['sources'] ); 380 $this->assertArrayHasKey( $expected_mime, $properties['sources'] ); 381 $this->assertArrayHasKey( 'filesize', $properties['sources'][ $expected_mime ] ); 382 $this->assertArrayHasKey( 'file', $properties['sources'][ $expected_mime ] ); 383 $this->assertArrayHasKey( $targeted_mime, $properties['sources'] ); 384 $this->assertArrayHasKey( 'filesize', $properties['sources'][ $targeted_mime ] ); 385 $this->assertArrayHasKey( 'file', $properties['sources'][ $targeted_mime ] ); 386 } 387 } 388 389 public function provider_image_with_default_behaviors_during_upload() { 390 yield 'JPEG image' => array( 391 DIR_TESTDATA . '/images/test-image.jpg', 392 'image/jpeg', 393 'image/webp', 394 ); 395 396 yield 'WebP image' => array( 397 DIR_TESTDATA . '/images/webp-lossy.webp', 398 'image/webp', 399 'image/jpeg', 400 ); 401 } 402 403 /** 404 * Not create the sources property if no transform is provided 405 * 406 * @since 6.0.0 407 */ 408 public function it_should_not_create_the_sources_property_if_no_transform_is_provided() { 409 add_filter( 'webp_uploads_supported_image_mime_transforms', '__return_empty_array' ); 410 411 $attachment_id = $this->factory->attachment->create_upload_object( 412 DIR_TESTDATA . '/images/test-image.jpg' 413 ); 414 415 $metadata = wp_get_attachment_metadata( $attachment_id ); 416 417 $this->assertIsArray( $metadata ); 418 foreach ( $metadata['sizes'] as $size_name => $properties ) { 419 $this->assertArrayNotHasKey( 'sources', $properties ); 420 } 421 } 422 423 /** 424 * Create the sources property when no transform is available 425 * 426 * @since 6.0.0 427 */ 428 public function it_should_create_the_sources_property_when_no_transform_is_available() { 429 add_filter( 430 'webp_uploads_supported_image_mime_transforms', 431 function () { 432 return array( 'image/jpeg' => array() ); 433 } 434 ); 435 436 $attachment_id = $this->factory->attachment->create_upload_object( 437 DIR_TESTDATA . '/images/test-image.jpg' 438 ); 439 440 $metadata = wp_get_attachment_metadata( $attachment_id ); 441 442 $this->assertIsArray( $metadata ); 443 foreach ( $metadata['sizes'] as $size_name => $properties ) { 444 $this->assertArrayHasKey( 'sources', $properties ); 445 $this->assertIsArray( $properties['sources'] ); 446 $this->assertArrayHasKey( 'image/jpeg', $properties['sources'] ); 447 $this->assertArrayHasKey( 'filesize', $properties['sources']['image/jpeg'] ); 448 $this->assertArrayHasKey( 'file', $properties['sources']['image/jpeg'] ); 449 $this->assertArrayNotHasKey( 'image/webp', $properties['sources'] ); 450 } 451 } 452 453 /** 454 * Not create the sources property if the mime is not specified on the transforms images 455 * 456 * @since 6.0.0 457 */ 458 public function it_should_not_create_the_sources_property_if_the_mime_is_not_specified_on_the_transforms_images() { 459 add_filter( 460 'webp_uploads_supported_image_mime_transforms', 461 function () { 462 return array( 'image/jpeg' => array() ); 463 } 464 ); 465 466 $attachment_id = $this->factory->attachment->create_upload_object( 467 DIR_TESTDATA . '/images/webp-lossy.webp' 468 ); 469 470 $metadata = wp_get_attachment_metadata( $attachment_id ); 471 472 $this->assertIsArray( $metadata ); 473 foreach ( $metadata['sizes'] as $size_name => $properties ) { 474 $this->assertArrayNotHasKey( 'sources', $properties ); 475 } 476 } 477 478 /** 479 * Create a WebP version with all the required properties 480 * 481 * @since 6.0.0 482 */ 483 public function it_should_create_a_webp_version_with_all_the_required_properties() { 484 $attachment_id = $this->factory->attachment->create_upload_object( 485 DIR_TESTDATA . '/images/test-image.jpg' 486 ); 487 488 $metadata = wp_get_attachment_metadata( $attachment_id ); 489 $this->assertArrayHasKey( 'sources', $metadata['sizes']['thumbnail'] ); 490 $this->assertArrayHasKey( 'image/jpeg', $metadata['sizes']['thumbnail']['sources'] ); 491 $this->assertArrayHasKey( 'filesize', $metadata['sizes']['thumbnail']['sources']['image/jpeg'] ); 492 $this->assertArrayHasKey( 'file', $metadata['sizes']['thumbnail']['sources']['image/jpeg'] ); 493 $this->assertArrayHasKey( 'image/webp', $metadata['sizes']['thumbnail']['sources'] ); 494 $this->assertArrayHasKey( 'filesize', $metadata['sizes']['thumbnail']['sources']['image/webp'] ); 495 $this->assertArrayHasKey( 'file', $metadata['sizes']['thumbnail']['sources']['image/webp'] ); 496 $this->assertStringEndsNotWith( '.jpeg', $metadata['sizes']['thumbnail']['sources']['image/webp']['file'] ); 497 $this->assertStringEndsWith( '.webp', $metadata['sizes']['thumbnail']['sources']['image/webp']['file'] ); 498 } 499 500 /** 501 * Remove `scaled` suffix from the generated filename 502 * 503 * @since 6.0.0 504 */ 505 public function it_should_remove_scaled_suffix_from_the_generated_filename() { 506 // The leafs image is 1080 pixels wide with this filter we ensure a -scaled version is created. 507 add_filter( 508 'big_image_size_threshold', 509 function () { 510 return 850; 511 } 512 ); 513 514 $attachment_id = $this->factory->attachment->create_upload_object( 515 DIR_TESTDATA . '/images/test-image.jpg' 516 ); 517 $metadata = wp_get_attachment_metadata( $attachment_id ); 518 $this->assertStringEndsWith( '-scaled.jpg', get_attached_file( $attachment_id ) ); 519 $this->assertArrayHasKey( 'image/webp', $metadata['sizes']['medium']['sources'] ); 520 $this->assertStringEndsNotWith( '-scaled.webp', $metadata['sizes']['medium']['sources']['image/webp']['file'] ); 521 $this->assertStringEndsWith( '-300x200.webp', $metadata['sizes']['medium']['sources']['image/webp']['file'] ); 522 } 523 524 /** 525 * Remove the generated webp images when the attachment is deleted 526 * 527 * @since 6.0.0 528 */ 529 public function it_should_remove_the_generated_webp_images_when_the_attachment_is_deleted() { 530 // Make sure no editor is available. 531 $attachment_id = $this->factory->attachment->create_upload_object( 532 DIR_TESTDATA . '/images/test-image.jpg' 533 ); 534 535 $file = get_attached_file( $attachment_id, true ); 536 $dirname = pathinfo( $file, PATHINFO_DIRNAME ); 537 538 $this->assertIsString( $file ); 539 $this->assertFileExists( $file ); 540 541 $metadata = wp_get_attachment_metadata( $attachment_id ); 542 $sizes = array( 'thumbnail', 'medium' ); 543 544 foreach ( $sizes as $size_name ) { 545 $this->assertArrayHasKey( 'image/webp', $metadata['sizes'][ $size_name ]['sources'] ); 546 $this->assertArrayHasKey( 'file', $metadata['sizes'][ $size_name ]['sources']['image/webp'] ); 547 $this->assertFileExists( 548 path_join( $dirname, $metadata['sizes'][ $size_name ]['sources']['image/webp']['file'] ) 549 ); 550 } 551 552 wp_delete_attachment( $attachment_id ); 553 554 foreach ( $sizes as $size_name ) { 555 $this->assertFileDoesNotExist( 556 path_join( $dirname, $metadata['sizes'][ $size_name ]['sources']['image/webp']['file'] ) 557 ); 558 } 559 } 560 561 /** 562 * Remove the attached WebP version if the attachment is force deleted but empty trash day is not defined 563 * 564 * @since 6.0.0 565 */ 566 public function it_should_remove_the_attached_webp_version_if_the_attachment_is_force_deleted_but_empty_trash_day_is_not_defined() { 567 // Make sure no editor is available. 568 $attachment_id = $this->factory->attachment->create_upload_object( 569 DIR_TESTDATA . '/images/test-image.jpg' 570 ); 571 572 $file = get_attached_file( $attachment_id, true ); 573 $dirname = pathinfo( $file, PATHINFO_DIRNAME ); 574 575 $this->assertIsString( $file ); 576 $this->assertFileExists( $file ); 577 578 $metadata = wp_get_attachment_metadata( $attachment_id ); 579 580 $this->assertFileExists( 581 path_join( $dirname, $metadata['sizes']['thumbnail']['sources']['image/webp']['file'] ) 582 ); 583 584 wp_delete_attachment( $attachment_id, true ); 585 586 $this->assertFileDoesNotExist( 587 path_join( $dirname, $metadata['sizes']['thumbnail']['sources']['image/webp']['file'] ) 588 ); 589 } 590 591 /** 592 * Remove the WebP version of the image if the image is force deleted and empty trash days is set to zero 593 * 594 * @since 6.0.0 595 */ 596 public function it_should_remove_the_webp_version_of_the_image_if_the_image_is_force_deleted_and_empty_trash_days_is_set_to_zero() { 597 // Make sure no editor is available. 598 $attachment_id = $this->factory->attachment->create_upload_object( 599 DIR_TESTDATA . '/images/test-image.jpg' 600 ); 601 602 $file = get_attached_file( $attachment_id, true ); 603 $dirname = pathinfo( $file, PATHINFO_DIRNAME ); 604 605 $this->assertIsString( $file ); 606 $this->assertFileExists( $file ); 607 608 $metadata = wp_get_attachment_metadata( $attachment_id ); 609 610 $this->assertFileExists( 611 path_join( $dirname, $metadata['sizes']['thumbnail']['sources']['image/webp']['file'] ) 612 ); 613 614 define( 'EMPTY_TRASH_DAYS', 0 ); 615 616 wp_delete_attachment( $attachment_id, true ); 617 618 $this->assertFileDoesNotExist( 619 path_join( $dirname, $metadata['sizes']['thumbnail']['sources']['image/webp']['file'] ) 620 ); 621 } 622 623 /** 624 * Avoid the change of URLs of images that are not part of the media library 625 * 626 * @group webp_uploads_update_image_references 627 * 628 * @since 6.0.0 629 */ 630 public function it_should_avoid_the_change_of_urls_of_images_that_are_not_part_of_the_media_library() { 631 $paragraph = '<p>Donec accumsan, sapien et <img src="https://ia600200.us.archive.org/16/items/SPD-SLRSY-1867/hubblesite_2001_06.jpg">, id commodo nisi sapien et est. Mauris nisl odio, iaculis vitae pellentesque nec.</p>'; 632 633 $this->assertSame( $paragraph, webp_uploads_update_image_references( $paragraph ) ); 634 } 635 636 /** 637 * Avoid replacing not existing attachment IDs 638 * 639 * @group webp_uploads_update_image_references 640 * 641 * @since 6.0.0 642 */ 643 public function it_should_avoid_replacing_not_existing_attachment_i_ds() { 644 $paragraph = '<p>Donec accumsan, sapien et <img class="wp-image-0" src="https://ia600200.us.archive.org/16/items/SPD-SLRSY-1867/hubblesite_2001_06.jpg">, id commodo nisi sapien et est. Mauris nisl odio, iaculis vitae pellentesque nec.</p>'; 645 646 $this->assertSame( $paragraph, webp_uploads_update_image_references( $paragraph ) ); 647 } 648 649 /** 650 * Prevent replacing a WebP image 651 * 652 * @group webp_uploads_update_image_references 653 * 654 * @since 6.0.0 655 */ 656 public function it_should_prevent_replacing_a_webp_image() { 657 $attachment_id = $this->factory->attachment->create_upload_object( 658 DIR_TESTDATA . '/images/webp-lossy.webp' 659 ); 660 661 $tag = wp_get_attachment_image( $attachment_id, 'medium', false, array( 'class' => "wp-image-{$attachment_id}" ) ); 662 663 $this->assertSame( $tag, webp_uploads_img_tag_update_mime_type( $tag, 'the_content', $attachment_id ) ); 664 } 665 666 /** 667 * Prevent replacing a jpg image if the image does not have the target class name 668 * 669 * @since 6.0.0 670 */ 671 public function it_should_prevent_replacing_a_jpg_image_if_the_image_does_not_have_the_target_class_name() { 672 $attachment_id = $this->factory->attachment->create_upload_object( 673 DIR_TESTDATA . '/images/test-image.jpg' 674 ); 675 676 $tag = wp_get_attachment_image( $attachment_id, 'medium' ); 677 678 $this->assertSame( $tag, webp_uploads_update_image_references( $tag ) ); 679 } 680 681 /** 682 * Replace the references to a JPG image to a WebP version 683 * 684 * @dataProvider provider_replace_images_with_different_extensions 685 * @group webp_uploads_update_image_references 686 * 687 * @since 6.0.0 688 */ 689 public function it_should_replace_the_references_to_a_jpg_image_to_a_webp_version( $image_path ) { 690 $attachment_id = $this->factory->attachment->create_upload_object( $image_path ); 691 692 $tag = wp_get_attachment_image( $attachment_id, 'medium', false, array( 'class' => "wp-image-{$attachment_id}" ) ); 693 $expected_tag = $tag; 694 $metadata = wp_get_attachment_metadata( $attachment_id ); 695 foreach ( $metadata['sizes'] as $size => $properties ) { 696 $expected_tag = str_replace( $properties['sources']['image/jpeg']['file'], $properties['sources']['image/webp']['file'], $expected_tag ); 697 } 698 699 $this->assertNotEmpty( $expected_tag ); 700 $this->assertNotSame( $tag, $expected_tag ); 701 $this->assertSame( $expected_tag, webp_uploads_img_tag_update_mime_type( $tag, 'the_content', $attachment_id ) ); 702 } 703 704 public function provider_replace_images_with_different_extensions() { 705 yield 'An image with a .jpg extension' => array( DIR_TESTDATA . '/images/test-image.jpg' ); 706 yield 'An image with a .jpeg extension' => array( DIR_TESTDATA . '/images/test-image.jpeg' ); 707 } 708 709 /** 710 * Contain the full image size from the original mime 711 * 712 * @group webp_uploads_update_image_references 713 * 714 * @since 6.0.0 715 */ 716 public function it_should_contain_the_full_image_size_from_the_original_mime() { 717 $attachment_id = $this->factory->attachment->create_upload_object( 718 DIR_TESTDATA . '/images/test-image.jpg' 719 ); 720 721 $tag = wp_get_attachment_image( $attachment_id, 'full', false, array( 'class' => "wp-image-{$attachment_id}" ) ); 722 723 $expected = array( 724 'ext' => 'jpg', 725 'type' => 'image/jpeg', 726 ); 727 $this->assertSame( $expected, wp_check_filetype( get_attached_file( $attachment_id ) ) ); 728 $this->assertContains( wp_basename( get_attached_file( $attachment_id ) ), webp_uploads_img_tag_update_mime_type( $tag, 'the_content', $attachment_id ) ); 729 } 730 731 /** 732 * Prevent replacing an image with no available sources 733 * 734 * @group webp_uploads_update_image_references 735 * 736 * @since 6.0.0 737 */ 738 public function it_should_prevent_replacing_an_image_with_no_available_sources() { 739 add_filter( 'webp_uploads_supported_image_mime_transforms', '__return_empty_array' ); 740 741 $attachment_id = $this->factory->attachment->create_upload_object( DIR_TESTDATA . '/images/test-image.jpg' ); 742 743 $tag = wp_get_attachment_image( $attachment_id, 'full', false, array( 'class' => "wp-image-{$attachment_id}" ) ); 744 $this->assertSame( $tag, webp_uploads_img_tag_update_mime_type( $tag, 'the_content', $attachment_id ) ); 745 } 746 747 /** 748 * Prevent update not supported images with no available sources 749 * 750 * @dataProvider data_provider_not_supported_webp_images 751 * @group webp_uploads_update_image_references 752 * 753 * @since 6.0.0 754 */ 755 public function it_should_prevent_update_not_supported_images_with_no_available_sources( $image_path ) { 756 $attachment_id = $this->factory->attachment->create_upload_object( $image_path ); 757 758 $this->assertIsNumeric( $attachment_id ); 759 $tag = wp_get_attachment_image( $attachment_id, 'full', false, array( 'class' => "wp-image-{$attachment_id}" ) ); 760 761 $this->assertSame( $tag, webp_uploads_img_tag_update_mime_type( $tag, 'the_content', $attachment_id ) ); 762 } 763 764 public function data_provider_not_supported_webp_images() { 765 yield 'PNG image' => array( DIR_TESTDATA . '/images/test-image.png' ); 766 yield 'GIFT image' => array( DIR_TESTDATA . '/images/test-image.gif' ); 767 } 768 769 364 770 365 771 } -
tests/phpunit/tests/image/functions.php
diff --git tests/phpunit/tests/image/functions.php tests/phpunit/tests/image/functions.php index c02862b3e7..39bf09cf0c 100644
class Tests_Image_Functions extends WP_UnitTestCase { 480 480 $this->assertNotEmpty( $attachment_id ); 481 481 482 482 $temp_dir = get_temp_dir(); 483 484 483 $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file ); 485 484 486 485 $expected = array( … … class Tests_Image_Functions extends WP_UnitTestCase { 498 497 'height' => 300, 499 498 'mime-type' => 'image/jpeg', 500 499 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-232x300.jpg' ), 500 'sources' => array( 501 'image/jpeg' => array( 502 'file' => 'wordpress-gsoc-flyer-pdf-232x300.jpg', 503 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-232x300.jpg' ), 504 505 ), 506 ), 501 507 ), 502 508 'large' => array( 503 509 'file' => 'wordpress-gsoc-flyer-pdf-791x1024.jpg', … … class Tests_Image_Functions extends WP_UnitTestCase { 505 511 'height' => 1024, 506 512 'mime-type' => 'image/jpeg', 507 513 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-791x1024.jpg' ), 514 'sources' => array( 515 'image/jpeg' => array( 516 'file' => 'wordpress-gsoc-flyer-pdf-791x1024.jpg', 517 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-791x1024.jpg' ), 518 ), 519 ), 508 520 ), 509 521 'thumbnail' => array( 510 522 'file' => 'wordpress-gsoc-flyer-pdf-116x150.jpg', … … class Tests_Image_Functions extends WP_UnitTestCase { 512 524 'height' => 150, 513 525 'mime-type' => 'image/jpeg', 514 526 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-116x150.jpg' ), 527 'sources' => array( 528 'image/jpeg' => array( 529 'file' => 'wordpress-gsoc-flyer-pdf-116x150.jpg', 530 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-116x150.jpg' ), 531 ), 532 ), 515 533 ), 516 534 ), 517 535 'filesize' => wp_filesize( $test_file ), … … class Tests_Image_Functions extends WP_UnitTestCase { 557 575 $this->assertNotEmpty( $attachment_id ); 558 576 559 577 $temp_dir = get_temp_dir(); 560 561 578 $metadata = wp_generate_attachment_metadata( $attachment_id, $test_file ); 562 579 563 580 $expected = array( … … class Tests_Image_Functions extends WP_UnitTestCase { 575 592 'height' => 300, 576 593 'mime-type' => 'image/jpeg', 577 594 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-300x300.jpg' ), 595 'sources' => array( 596 'image/jpeg' => array( 597 'file' => 'wordpress-gsoc-flyer-pdf-300x300.jpg', 598 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-300x300.jpg' ), 599 ), 600 ), 578 601 ), 579 602 'large' => array( 580 603 'file' => 'wordpress-gsoc-flyer-pdf-791x1024.jpg', … … class Tests_Image_Functions extends WP_UnitTestCase { 582 605 'height' => 1024, 583 606 'mime-type' => 'image/jpeg', 584 607 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-791x1024.jpg' ), 608 'sources' => array( 609 'image/jpeg' => array( 610 'file' => 'wordpress-gsoc-flyer-pdf-791x1024.jpg', 611 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-791x1024.jpg' ), 612 ), 613 ), 614 585 615 ), 586 616 'thumbnail' => array( 587 617 'file' => 'wordpress-gsoc-flyer-pdf-116x150.jpg', … … class Tests_Image_Functions extends WP_UnitTestCase { 589 619 'height' => 150, 590 620 'mime-type' => 'image/jpeg', 591 621 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-116x150.jpg' ), 622 'sources' => array( 623 'image/jpeg' => array( 624 'file' => 'wordpress-gsoc-flyer-pdf-116x150.jpg', 625 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-116x150.jpg' ), 626 ), 627 ), 592 628 ), 593 629 ), 594 630 'filesize' => wp_filesize( $test_file ), … … class Tests_Image_Functions extends WP_UnitTestCase { 642 678 'height' => 100, 643 679 'mime-type' => 'image/jpeg', 644 680 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-77x100.jpg' ), 681 'sources' => array( 682 'image/jpeg' => array( 683 'file' => 'wordpress-gsoc-flyer-pdf-77x100.jpg', 684 'filesize' => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-77x100.jpg' ), 685 ), 686 ), 645 687 ); 646 688 647 689 // Different environments produce slightly different filesize results. -
tests/phpunit/tests/media.php
diff --git tests/phpunit/tests/media.php tests/phpunit/tests/media.php index 5d92d9718b..2e9e2173bc 100644
EOF; 2252 2252 // Do not add width, height, and loading. 2253 2253 add_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' ); 2254 2254 add_filter( 'wp_img_tag_add_loading_attr', '__return_false' ); 2255 add_filter( 'wp_content_image_mimes', '__return_empty_array' ); 2255 2256 2256 2257 $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) ); 2257 2258 2258 2259 remove_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' ); 2259 2260 remove_filter( 'wp_img_tag_add_loading_attr', '__return_false' ); 2261 remove_filter( 'wp_content_image_mimes', '__return_empty_array' ); 2262 2260 2263 } 2261 2264 2262 2265 /** … … EOF; 2288 2291 $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); 2289 2292 $img = wp_img_tag_add_loading_attr( $img, 'test' ); 2290 2293 $img = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . 'srcset="image2x.jpg 2x" />', $img ); 2294 add_filter( 'wp_content_image_mimes', '__return_empty_array' ); 2291 2295 2292 2296 // The content filter should return the image unchanged. 2293 2297 $this->assertSame( $img, wp_filter_content_tags( $img ) ); 2298 2299 remove_filter( 'wp_content_image_mimes', '__return_empty_array' ); 2294 2300 } 2295 2301 2296 2302 /** … … EOF; 2343 2349 * @requires function imagejpeg 2344 2350 */ 2345 2351 public function test_wp_filter_content_tags_schemes() { 2352 add_filter( 'wp_content_image_mimes', '__return_empty_array' ); 2346 2353 $image_meta = wp_get_attachment_metadata( self::$large_id ); 2347 2354 $size_array = $this->get_image_size_array_from_meta( $image_meta, 'medium' ); 2348 2355 … … EOF; 2387 2394 $actual = wp_filter_content_tags( $unfiltered ); 2388 2395 2389 2396 $this->assertSame( $expected, $actual ); 2397 remove_filter( 'wp_content_image_mimes', '__return_empty_array' ); 2390 2398 } 2391 2399 2392 2400 /** … … EOF; 2804 2812 * @requires function imagejpeg 2805 2813 */ 2806 2814 public function test_wp_filter_content_tags_width_height() { 2815 add_filter( 'wp_content_image_mimes', '__return_empty_array' ); 2816 2807 2817 $image_meta = wp_get_attachment_metadata( self::$large_id ); 2808 2818 $size_array = $this->get_image_size_array_from_meta( $image_meta, 'medium' ); 2809 2819 … … EOF; 2837 2847 // Do not add loading, srcset, and sizes. 2838 2848 add_filter( 'wp_img_tag_add_loading_attr', '__return_false' ); 2839 2849 add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 2850 add_filter( 'wp_content_image_mimes', '__return_empty_array' ); 2840 2851 2841 2852 $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) ); 2842 2853 2843 2854 remove_filter( 'wp_img_tag_add_loading_attr', '__return_false' ); 2844 2855 remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 2856 remove_filter( 'wp_content_image_mimes', '__return_empty_array' ); 2845 2857 } 2846 2858 2847 2859 /** … … EOF; 2895 2907 // Do not add width, height, srcset, and sizes. 2896 2908 add_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' ); 2897 2909 add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 2910 add_filter( 'wp_content_image_mimes', '__return_empty_array' ); 2898 2911 2899 2912 $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) ); 2900 2913 2901 2914 remove_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' ); 2902 2915 remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 2916 remove_filter( 'wp_content_image_mimes', '__return_empty_array' ); 2903 2917 } 2904 2918 2905 2919 /** … … EOF; 2927 2941 // Enable globally for all tags. 2928 2942 add_filter( 'wp_lazy_loading_enabled', '__return_true' ); 2929 2943 2944 add_filter( 'wp_content_image_mimes', '__return_empty_array' ); 2945 2930 2946 $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) ); 2931 2947 remove_filter( 'wp_lazy_loading_enabled', '__return_true' ); 2932 2948 remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 2949 remove_filter( 'wp_content_image_mimes', '__return_empty_array' ); 2950 2933 2951 } 2934 2952 2935 2953 /** … … EOF; 2953 2971 // Disable globally for all tags. 2954 2972 add_filter( 'wp_lazy_loading_enabled', '__return_false' ); 2955 2973 2974 add_filter( 'wp_content_image_mimes', '__return_empty_array' ); 2975 2956 2976 $this->assertSame( $content, wp_filter_content_tags( $content ) ); 2957 2977 remove_filter( 'wp_lazy_loading_enabled', '__return_false' ); 2958 2978 remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 2979 remove_filter( 'wp_content_image_mimes', '__return_empty_array' ); 2959 2980 } 2960 2981 2961 2982 /** … … EOF; 3381 3402 */ 3382 3403 function test_wp_filter_content_tags_with_wp_get_loading_attr_default() { 3383 3404 global $wp_query, $wp_the_query; 3405 add_filter( 'wp_content_image_mimes', '__return_empty_array' ); 3384 3406 3385 3407 $img1 = get_image_tag( self::$large_id, '', '', '', 'large' ); 3386 3408 $iframe1 = '<iframe src="https://www.example.com" width="640" height="360"></iframe>'; … … EOF; 3415 3437 $content_filtered = wp_filter_content_tags( $content_unfiltered, 'the_content' ); 3416 3438 remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' ); 3417 3439 } 3440 remove_filter( 'wp_content_image_mimes', '__return_empty_array' ); 3418 3441 3419 3442 // After filtering, the first image should not be lazy-loaded while the other ones should be. 3420 3443 $this->assertSame( $content_expected, $content_filtered );