Make WordPress Core

Changeset 53751

Timestamp:
07/21/2022 06:01:01 PM (2 years ago)
Author:
adamsilverstein
Message:

Media: enable generating multiple mime types for image uploads; specifically WebP versions for JPEG images by default.

This changeset adds the capability for core media uploads to generate sub sized images in more than a single mime type. The output formats for each mime type can be controlled through a filter. WebP is used as an additional output format for JPEG images by default to improve front end performance.

When generating additional mime types, only images which are smaller than the respective original are retained. By default, additional mime type images are only generated for the built-in core image sizes and any custom sizes that have opted in.

Image meta is updated with a new 'sources' array containing file details for each mime type. Each image size in the 'sizes' array also gets a new 'sources' array that contains the image file details for each mime type.

This change also increases image upload retries to accommodate additional image sizes. It also adds a $mime_type parameter to the wp_get_missing_image_subsizes function and filter.

This change adds three new filters to enable full control of secondary mime image generation and output:

  • A new filter wp_image_sizes_with_additional_mime_type_support that filters the sizes that support secondary mime type output. Developers can use this to control the output of additional mime type sub-sized images on a per size basis.
  • A new filter wp_upload_image_mime_transforms that filters the output mime types for a given input mime type. Developers can use this to control generation of additional mime types for a given input mime type or even override the original mime type.
  • A new filter wp_content_image_mimes which controls image mime type output selection and order for frontend content. Developers can use this to control the mime type output preference order for content images. Content images inserted from the media library will use the available image versions based on the order from this filter.

Thanks to the many contributors who helped develop, test and give feedback on this feature.

A haiku to summarize:

Upload a JPEG
Images of all sizes
Output as WebPs

Props flixos90, MatthiasReinholz, studiolxv, markhowellsmead, eatingrules, pbiron, mukesh27, joegrainger, mehulkaklotar, tweetythierry, akshitsethi, peterwilsoncc, eugenemanuilov, mitogh, shetheliving, clarkeemily, codekraft, mikeschroder, clorith, kasparsd, spacedmonkey, trevorpfromsandee, jb510, scofennellgmailcom, seedsca, cagsmith, karinclimber, dainemawer, baxbridge, grapplerulrich, sobatkras, chynnabenton, tonylocalword, barneydavey, kwillmorth, garymatthews919, olliejones, imarkinteractive, jeffpaul, feastdesignco, webbeetle, masteradhoc.

See #55443.

Location:
trunk
Files:
3 added
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/js/_enqueues/vendor/plupload/handlers.js

    r51947 r53751  
    487487        times = tryAgainCount[ file.id ];
    488488
    489         if ( times && times > 4 ) {
     489        if ( times && times > ) {
    490490            /*
    491491             * The file may have been uploaded and attachment post created,
  • trunk/src/js/_enqueues/vendor/plupload/wp-plupload.js

    r51227 r53751  
    139139            times = tryAgainCount[ file.id ];
    140140
    141             if ( times && times > 4 ) {
     141            if ( times && times > ) {
    142142                /*
    143143                 * The file may have been uploaded and attachment post created,
  • trunk/src/wp-admin/includes/image.php

    r53547 r53751  
    7878 *
    7979 * @since 5.3.0
    80  *
    81  * @param int $attachment_id The image attachment post ID.
     80 * @since 6.1.0 The $mime_type parameter was added.
     81 *
     82 * @param int    $attachment_id The image attachment post ID.
     83 * @param string $mime_type     Optional. The mime type to check for missing sizes. Default is the primary image mime.
    8284 * @return array[] Associative array of arrays of image sub-size information for
    8385 *                 missing image sizes, keyed by image size name.
    8486 */
    85 function wp_get_missing_image_subsizes( $attachment_id ) {
     87function wp_get_missing_image_subsizes( $attachment_id ) {
    8688    if ( ! wp_attachment_is_image( $attachment_id ) ) {
    8789        return array();
     90
     91
     92
     93
     94
    8895    }
    8996
     
    130137     * as the image may have been used in an older post.
    131138     */
    132     $missing_sizes = array_diff_key( $possible_sizes, $image_meta['sizes'] );
     139    $missing_sizes = array();
     140    foreach ( $possible_sizes as $size_name => $size_data ) {
     141        if ( ! isset( $image_meta['sizes'][ $size_name ] ) ) {
     142            $missing_sizes[ $size_name ] = $size_data;
     143            continue;
     144        }
     145
     146        if ( ( isset( $size_data['mime-type'] ) && $size_data['mime-type'] === $mime_type ) || isset( $size_data['sources'][ $mime_type ] ) ) {
     147            continue;
     148        }
     149
     150        $missing_sizes[ $size_name ] = $size_data;
     151    }
     152
     153    // Filter secondary mime types to those sizes that are enabled.
     154    if ( $primary_mime_type !== $mime_type ) {
     155        $missing_sizes = _wp_filter_image_sizes_additional_mime_type_support( $missing_sizes, $attachment_id );
     156    }
    133157
    134158    /**
     
    136160     *
    137161     * @since 5.3.0
     162
    138163     *
    139164     * @param array[] $missing_sizes Associative array of arrays of image sub-size information for
     
    141166     * @param array   $image_meta    The image meta data.
    142167     * @param int     $attachment_id The image attachment post ID.
     168
    143169     */
    144     return apply_filters( 'wp_get_missing_image_subsizes', $missing_sizes, $image_meta, $attachment_id );
     170    return apply_filters( 'wp_get_missing_image_subsizes', $missing_sizes, $image_meta, $attachment_id );
    145171}
    146172
     
    150176 *
    151177 * @since 5.3.0
     178
    152179 *
    153180 * @param int $attachment_id The image attachment post ID.
     
    168195        }
    169196    } else {
    170         $missing_sizes = wp_get_missing_image_subsizes( $attachment_id );
    171 
    172         if ( empty( $missing_sizes ) ) {
    173             return $image_meta;
    174         }
    175 
    176         // This also updates the image meta.
    177         $image_meta = _wp_make_subsizes( $missing_sizes, $image_file, $image_meta, $attachment_id );
     197        // Get the primary and additional mime types to generate.
     198        list( $primary_mime_type, $additional_mime_types ) = _wp_get_primary_and_additional_mime_types( $image_file, $attachment_id );
     199
     200        // Generate missing 'full' image files for additional mime types.
     201        if ( ! empty( $additional_mime_types ) ) {
     202            if ( isset( $image_meta['sources'] ) ) {
     203                $missing_mime_types = array_diff( $additional_mime_types, array_keys( $image_meta['sources'] ) );
     204            } else {
     205                $missing_mime_types = $additional_mime_types;
     206            }
     207            if ( ! empty( $missing_mime_types ) ) {
     208                $image_meta = _wp_make_additional_mime_types( $missing_mime_types, $image_file, $image_meta, $attachment_id );
     209            }
     210        }
     211
     212        // Generate missing image sub-sizes for each mime type.
     213        $all_mime_types = array_merge( array( $primary_mime_type ), $additional_mime_types );
     214        foreach ( $all_mime_types as $mime_type ) {
     215            $missing_sizes = wp_get_missing_image_subsizes( $attachment_id, $mime_type );
     216
     217            if ( empty( $missing_sizes ) ) {
     218                continue;
     219            }
     220
     221            // This also updates the image meta.
     222            $image_meta = _wp_make_subsizes( $missing_sizes, $image_file, $image_meta, $attachment_id, $mime_type );
     223        }
    178224    }
    179225
     
    223269
    224270/**
    225  * Creates image sub-sizes, adds the new data to the image meta `sizes` array, and updates the image metadata.
     271 * Creates image sub-sizes, adds the new data to the image meta `sizes` array, and updates the image metadata.
    226272 *
    227273 * Intended for use after an image is uploaded. Saves/updates the image metadata after each
     
    229275 *
    230276 * @since 5.3.0
     277
    231278 *
    232279 * @param string $file          Full path to the image file.
     
    249296        'filesize' => wp_filesize( $file ),
    250297        'sizes'    => array(),
     298
    251299    );
    252300
     
    258306    }
    259307
     308
     309
     310
     311
     312
     313
     314
     315
     316
     317
     318
     319
     320
     321
     322
     323
     324
     325
     326
     327
     328
     329
     330
     331
     332
     333
     334
     335
     336
     337
     338
     339
     340
     341
     342
     343
     344
     345
     346
     347
     348
     349
     350
     351
     352
     353
     354
     355
     356
     357
     358
     359
     360
     361
     362
     363
     364
     365
     366
     367
     368
     369
     370
     371
     372
     373
     374
     375
     376
     377
     378
     379
     380
     381
     382
     383
     384
     385
     386
     387
     388
     389
     390
     391
     392
     393
     394
     395
     396
     397
     398
     399
     400
     401
     402
     403
     404
     405
     406
     407
     408
     409
     410
     411
    260412    // Do not scale (large) PNG images. May result in sub-sizes that have greater file size than the original. See #48736.
    261     if ( 'image/png' !== $imagesize['mime'] ) {
    262 
     413    if ( 'image/png' !== $mime_type ) {
    263414        /**
    264415         * Filters the "BIG image" threshold value.
     
    286437        // If the original image's dimensions are over the threshold,
    287438        // scale the image and use it as the "full" size.
    288         if ( $threshold && ( $image_meta['width'] > $threshold || $image_meta['height'] > $threshold ) ) {
    289             $editor = wp_get_image_editor( $file );
    290 
    291             if ( is_wp_error( $editor ) ) {
    292                 // This image cannot be edited.
    293                 return $image_meta;
    294             }
    295 
     439        if ( $threshold && ( $imagesize[0] > $threshold || $imagesize[1] > $threshold ) ) {
    296440            // Resize the image.
    297441            $resized = $editor->resize( $threshold, $threshold );
    298             $rotated = null;
    299442
    300443            // If there is EXIF data, rotate according to EXIF Orientation.
    301444            if ( ! is_wp_error( $resized ) && is_array( $exif_meta ) ) {
    302                 $resized = $editor->maybe_exif_rotate();
    303                 $rotated = $resized;
    304             }
    305 
    306             if ( ! is_wp_error( $resized ) ) {
    307                 // Append "-scaled" to the image file name. It will look like "my_image-scaled.jpg".
    308                 // This doesn't affect the sub-sizes names as they are generated from the original image (for best quality).
    309                 $saved = $editor->save( $editor->generate_filename( 'scaled' ) );
    310 
    311                 if ( ! is_wp_error( $saved ) ) {
    312                     $image_meta = _wp_image_meta_replace_original( $saved, $file, $image_meta, $attachment_id );
    313 
    314                     // If the image was rotated update the stored EXIF data.
    315                     if ( true === $rotated && ! empty( $image_meta['image_meta']['orientation'] ) ) {
    316                         $image_meta['image_meta']['orientation'] = 1;
    317                     }
    318                 } else {
    319                     // TODO: Log errors.
    320                 }
    321             } else {
    322                 // TODO: Log errors.
     445                $rotated = $editor->maybe_exif_rotate();
    323446            }
    324447        } elseif ( ! empty( $exif_meta['orientation'] ) && 1 !== (int) $exif_meta['orientation'] ) {
    325448            // Rotate the whole original image if there is EXIF data and "orientation" is not 1.
    326 
    327             $editor = wp_get_image_editor( $file );
    328 
    329             if ( is_wp_error( $editor ) ) {
    330                 // This image cannot be edited.
    331                 return $image_meta;
    332             }
    333 
    334             // Rotate the image.
    335449            $rotated = $editor->maybe_exif_rotate();
    336 
    337             if ( true === $rotated ) {
    338                 // Append `-rotated` to the image file name.
    339                 $saved = $editor->save( $editor->generate_filename( 'rotated' ) );
    340 
    341                 if ( ! is_wp_error( $saved ) ) {
    342                     $image_meta = _wp_image_meta_replace_original( $saved, $file, $image_meta, $attachment_id );
    343 
    344                     // Update the stored EXIF data.
    345                     if ( ! empty( $image_meta['image_meta']['orientation'] ) ) {
    346                         $image_meta['image_meta']['orientation'] = 1;
    347                     }
    348                 } else {
    349                     // TODO: Log errors.
    350                 }
    351             }
    352         }
    353     }
    354 
    355     /*
    356      * Initial save of the new metadata.
    357      * At this point the file was uploaded and moved to the uploads directory
    358      * but the image sub-sizes haven't been created yet and the `sizes` array is empty.
    359      */
    360     wp_update_attachment_metadata( $attachment_id, $image_meta );
    361 
    362     $new_sizes = wp_get_registered_image_subsizes();
    363 
    364     /**
    365      * Filters the image sizes automatically generated when uploading an image.
    366      *
    367      * @since 2.9.0
    368      * @since 4.4.0 Added the `$image_meta` argument.
    369      * @since 5.3.0 Added the `$attachment_id` argument.
    370      *
    371      * @param array $new_sizes     Associative array of image sizes to be created.
    372      * @param array $image_meta    The image meta data: width, height, file, sizes, etc.
    373      * @param int   $attachment_id The attachment post ID for the image.
    374      */
    375     $new_sizes = apply_filters( 'intermediate_image_sizes_advanced', $new_sizes, $image_meta, $attachment_id );
    376 
    377     return _wp_make_subsizes( $new_sizes, $file, $image_meta, $attachment_id );
     450        }
     451    }
     452
     453    return array( $editor, $resized, $rotated );
     454}
     455
     456/**
     457 * Gets the suffix to use for image files based on resizing and rotating.
     458 *
     459 * @since 6.1.0
     460 * @access private
     461 *
     462 * @param bool|WP_Error Whether the image was resized, or an error if resizing failed.
     463 * @param bool|WP_Error Whether the image was rotated, or an error if rotating failed.
     464 * @return string The suffix to use for the file name, or empty string if none.
     465 */
     466function _wp_get_image_suffix( $resized, $rotated ) {
     467    if ( $resized && ! is_wp_error( $resized ) ) {
     468        // Append "-scaled" to the image file name. It will look like "my_image-scaled.jpg".
     469        // This doesn't affect the sub-sizes names as they are generated from the original image (for best quality).
     470        return 'scaled';
     471    }
     472
     473    if ( true === $rotated ) {
     474        // Append `-rotated` to the image file name.
     475        return 'rotated';
     476    }
     477
     478    if ( is_wp_error( $resized ) || is_wp_error( $rotated ) ) {
     479        // TODO: Log errors.
     480    }
     481    return '';
     482}
     483
     484/**
     485 * Gets a sources array element from a meta.
     486 *
     487 * @since 6.1.0
     488 * @access private
     489 *
     490 * @param array $meta The meta to get the source from.
     491 * @return array The source array element.
     492 */
     493function _wp_get_sources_from_meta( $meta ) {
     494    return array(
     495        'file'     => isset( $meta['file'] ) ? wp_basename( $meta['file'] ) : '',
     496        'filesize' => isset( $meta['filesize'] ) ? $meta['filesize'] : wp_filesize( $meta['path'] ),
     497    );
    378498}
    379499
     
    385505 *
    386506 * @since 5.3.0
     507
    387508 * @access private
    388509 *
    389  * @param array  $new_sizes     Array defining what sizes to create.
    390  * @param string $file          Full path to the image file.
    391  * @param array  $image_meta    The attachment meta data array.
    392  * @param int    $attachment_id Attachment ID to process.
     510 * @param array  $new_sizes       Array defining what sizes to create.
     511 * @param string $file            Full path to the image file.
     512 * @param array  $image_meta      The attachment meta data array.
     513 * @param int    $attachment_id   Attachment ID to process.
     514 * @param string $mime_type       Optional. The mime type to check for missing sizes. Default is the image mime of $file.
    393515 * @return array The attachment meta data with updated `sizes` array. Includes an array of errors encountered while resizing.
    394516 */
    395 function _wp_make_subsizes( $new_sizes, $file, $image_meta, $attachment_id ) {
     517function _wp_make_subsizes( $new_sizes, $file, $image_meta, $attachment_id ) {
    396518    if ( empty( $image_meta ) || ! is_array( $image_meta ) ) {
    397519        // Not an image attachment.
    398520        return array();
     521
     522
     523
     524
    399525    }
    400526
     
    408534             */
    409535            if ( array_key_exists( $size_name, $new_sizes ) ) {
    410                 unset( $new_sizes[ $size_name ] );
     536                // Unset the size if it is either the required mime type already exists either as main mime type or
     537                // within sources.
     538                if ( $size_meta['mime-type'] === $mime_type || isset( $size_meta['sources'][ $mime_type ] ) ) {
     539                    unset( $new_sizes[ $size_name ] );
     540                }
    411541            }
    412542        }
     
    434564    $new_sizes = array_filter( array_merge( $priority, $new_sizes ) );
    435565
    436     $editor = wp_get_image_editor( $file );
     566    $editor = wp_get_image_editor( $file );
    437567
    438568    if ( is_wp_error( $editor ) ) {
     
    440570        return $image_meta;
    441571    }
     572
     573
    442574
    443575    // If stored EXIF data exists, rotate the source image before creating sub-sizes.
     
    458590            } else {
    459591                // Save the size meta value.
    460                 $image_meta['sizes'][ $new_size_name ] = $new_size_meta;
     592                if ( ! isset( $image_meta['sizes'][ $new_size_name ] ) ) {
     593                    $image_meta['sizes'][ $new_size_name ] = $new_size_meta;
     594                } else {
     595                    // Remove any newly generated images that are larger than the primary mime type.
     596                    $new_size     = isset( $new_size_meta['filesize'] ) ? $new_size_meta['filesize'] : 0;
     597                    $primary_size = isset( $image_meta['sizes'][ $new_size_name ]['filesize'] ) ? $image_meta['sizes'][ $new_size_name ]['filesize'] : 0;
     598
     599                    if ( $new_size && $primary_size && $new_size >= $primary_size ) {
     600                        wp_delete_file( dirname( $file ) . '/' . $new_size_meta['file'] );
     601                        continue;
     602                    }
     603                }
     604                if ( ! isset( $image_meta['sizes'][ $new_size_name ]['sources'] ) ) {
     605                    $image_meta['sizes'][ $new_size_name ]['sources'] = array();
     606                }
     607                $image_meta['sizes'][ $new_size_name ]['sources'][ $mime_type ] = _wp_get_sources_from_meta( $new_size_meta );
    461608                wp_update_attachment_metadata( $attachment_id, $image_meta );
    462609            }
     
    467614
    468615        if ( ! empty( $created_sizes ) ) {
    469             $image_meta['sizes'] = array_merge( $image_meta['sizes'], $created_sizes );
     616            foreach ( $created_sizes as $created_size_name => $created_size_meta ) {
     617
     618                // Primary mime type is set in 'sizes' array.
     619                if ( ! isset( $image_meta['sizes'][ $created_size_name ] ) ) {
     620                    $image_meta['sizes'][ $created_size_name ] = $created_size_meta;
     621                } else {
     622                    // Remove any newly generated images that are larger than the primary mime type.
     623                    $new_size     = isset( $created_size_meta['filesize'] ) ? $created_size_meta['filesize'] : 0;
     624                    $primary_size = isset( $image_meta['sizes'][ $created_size_name ]['filesize'] ) ? $image_meta['sizes'][ $created_size_name ]['filesize'] : 0;
     625
     626                    if ( $new_size && $primary_size && $new_size >= $primary_size ) {
     627                        wp_delete_file( dirname( $file ) . '/' . $created_size_meta['file'] );
     628                        continue;
     629                    }
     630                }
     631                if ( ! isset( $image_meta['sizes'][ $created_size_name ]['sources'] ) ) {
     632                    $image_meta['sizes'][ $created_size_name ]['sources'] = array();
     633                }
     634                $image_meta['sizes'][ $created_size_name ]['sources'][ $mime_type ] = _wp_get_sources_from_meta( $new_size_meta );
     635            }
    470636            wp_update_attachment_metadata( $attachment_id, $image_meta );
    471637        }
     
    473639
    474640    return $image_meta;
     641
     642
     643
     644
     645
     646
     647
     648
     649
     650
     651
     652
     653
     654
     655
     656
     657
     658
     659
     660
     661
     662
     663
     664
     665
     666
     667
     668
     669
     670
     671
     672
     673
     674
     675
     676
     677
     678
     679
     680
     681
     682
     683
     684
     685
     686
     687
     688
     689
     690
     691
     692
     693
     694
     695
     696
     697
     698
     699
     700
     701
     702
     703
     704
     705
     706
     707
     708
     709
     710
     711
     712
     713
     714
     715
     716
     717
     718
     719
     720
     721
     722
     723
     724
     725
     726
     727
     728
     729
     730
     731
     732
     733
     734
     735
     736
     737
     738
     739
     740
     741
     742
     743
     744
     745
     746
     747
     748
     749
     750
     751
     752
     753
     754
     755
     756
     757
     758
     759
    475760}
    476761
     
    631916
    632917                    // Create sub-sizes saving the image meta after each.
    633                     $metadata = _wp_make_subsizes( $merged_sizes, $image_file, $metadata, $attachment_id );
     918                    $metadata = _wp_make_subsizes( $merged_sizes, $image_file, $metadata, $attachment_id );
    634919                }
    635920            }
     
    11581443    return $dst_file;
    11591444}
     1445
     1446
     1447
     1448
     1449
     1450
     1451
     1452
     1453
     1454
     1455
     1456
     1457
     1458
     1459
     1460
     1461
     1462
     1463
     1464
     1465
     1466
     1467
     1468
     1469
     1470
     1471
     1472
     1473
     1474
     1475
     1476
     1477
     1478
     1479
     1480
     1481
     1482
     1483
     1484
     1485
     1486
     1487
     1488
     1489
     1490
     1491
     1492
     1493
     1494
     1495
     1496
     1497
     1498
     1499
     1500
     1501
     1502
     1503
     1504
     1505
     1506
     1507
     1508
     1509
     1510
     1511
     1512
     1513
     1514
     1515
     1516
     1517
     1518
     1519
     1520
     1521
     1522
     1523
     1524
     1525
     1526
     1527
     1528
     1529
     1530
     1531
     1532
     1533
     1534
     1535
     1536
     1537
     1538
  • trunk/src/wp-includes/class-wp-image-editor.php

    r53547 r53751  
    334334    protected function get_output_format( $filename = null, $mime_type = null ) {
    335335        $new_ext = null;
     336
     337
     338
     339
     340
    336341
    337342        // By default, assume specified type takes priority.
     
    426431
    427432    /**
    428      * Builds an output filename based on current file, and adding proper suffix
    429      *
    430      * @since 3.5.0
    431      *
    432      * @param string $suffix
    433      * @param string $dest_path
    434      * @param string $extension
    435      * @return string filename
     433     * Builds an output filename based on current file, and adding proper suffix.
     434     *
     435     * @since 3.5.0
     436     * @since 6.1.0 Skips adding a suffix when set to an empty string. When the
     437     *              file extension being generated doesn't match the image file extension,
     438     *              add the extension to the suffix
     439     *
     440     * @param string $suffix    Optional. Suffix to add to the filename. The default null
     441     *                          will result in a 'widthxheight' suffix. Passing
     442     *                          an empty string will result in no suffix.
     443     * @param string $dest_path Optional. The path to save the file to. The default null
     444     *                          will use the image file path.
     445     * @param string $extension Optional. The file extension to use. The default null
     446     *                          will use the image file extension.
     447     * @return string filename The generated file name.
    436448     */
    437449    public function generate_filename( $suffix = null, $dest_path = null, $extension = null ) {
    438450        // $suffix will be appended to the destination filename, just before the extension.
    439         if ( ! $suffix ) {
     451        if ( $suffix ) {
    440452            $suffix = $this->get_suffix();
    441453        }
     
    458470        }
    459471
    460         return trailingslashit( $dir ) . "{$name}-{$suffix}.{$new_ext}";
     472        if ( empty( $suffix ) ) {
     473            $suffix = '';
     474        } else {
     475            $suffix = "-{$suffix}";
     476        }
     477
     478        // When the file extension being generated doesn't match the image file extension,
     479        // add the extension to the suffix to ensure a unique file name. Prevents
     480        // name conflicts when a single image type can have multiple extensions,
     481        // eg. .jpg, .jpeg and .jpe are all valid JPEG extensions.
     482        if ( ! empty( $extension ) && $extension !== $ext ) {
     483            $suffix .= "-{$ext}";
     484        }
     485
     486        return trailingslashit( $dir ) . "{$name}{$suffix}.{$new_ext}";
    461487    }
    462488
     
    638664        return wp_get_default_extension_for_mime_type( $mime_type );
    639665    }
     666
     667
     668
     669
     670
     671
     672
     673
     674
     675
     676
     677
     678
     679
     680
     681
     682
     683
     684
     685
     686
     687
     688
     689
    640690}
    641 
  • trunk/src/wp-includes/media.php

    r53715 r53751  
    18531853            }
    18541854
     1855
     1856
     1857
     1858
     1859
    18551860            /**
    18561861             * Filters an img tag within the content for a given context.
     
    18971902
    18981903    return $content;
     1904
     1905
     1906
     1907
     1908
     1909
     1910
     1911
     1912
     1913
     1914
     1915
     1916
     1917
     1918
     1919
     1920
     1921
     1922
     1923
     1924
     1925
     1926
     1927
     1928
     1929
     1930
     1931
     1932
     1933
     1934
     1935
     1936
     1937
     1938
     1939
     1940
     1941
     1942
     1943
     1944
     1945
     1946
     1947
     1948
     1949
     1950
     1951
     1952
     1953
     1954
     1955
     1956
     1957
     1958
     1959
     1960
     1961
     1962
     1963
     1964
     1965
     1966
     1967
     1968
     1969
     1970
     1971
     1972
     1973
     1974
     1975
     1976
     1977
     1978
     1979
     1980
     1981
     1982
     1983
     1984
     1985
     1986
     1987
     1988
     1989
     1990
     1991
     1992
     1993
     1994
     1995
     1996
     1997
     1998
     1999
     2000
     2001
     2002
     2003
     2004
     2005
     2006
     2007
     2008
     2009
     2010
     2011
     2012
     2013
     2014
    18992015}
    19002016
  • trunk/src/wp-includes/post.php

    r53723 r53751  
    64826482
    64836483        foreach ( $meta['sizes'] as $size => $sizeinfo ) {
    6484             $intermediate_file = str_replace( wp_basename( $file ), $sizeinfo['file'], $file );
    6485 
    6486             if ( ! empty( $intermediate_file ) ) {
    6487                 $intermediate_file = path_join( $uploadpath['basedir'], $intermediate_file );
    6488 
    6489                 if ( ! wp_delete_file_from_directory( $intermediate_file, $intermediate_dir ) ) {
    6490                     $deleted = false;
     6484
     6485            // Check for alternate size mime types in the sizeinfo['sources'] array to delete.
     6486            if ( isset( $sizeinfo['sources'] ) && is_array( $sizeinfo['sources'] ) ) {
     6487                foreach ( $sizeinfo['sources'] as $mime => $properties ) {
     6488                    $intermediate_file = str_replace( wp_basename( $file ), $properties['file'], $file );
     6489                    if ( ! empty( $intermediate_file ) ) {
     6490                        $intermediate_file = path_join( $uploadpath['basedir'], $intermediate_file );
     6491                        if ( ! wp_delete_file_from_directory( $intermediate_file, $intermediate_dir ) ) {
     6492                            $deleted = false;
     6493                        }
     6494                    }
     6495                }
     6496            } else {
     6497                // Otherwise, delete files from the sizeinfo data.
     6498                $intermediate_file = str_replace( wp_basename( $file ), $sizeinfo['file'], $file );
     6499
     6500                if ( ! empty( $intermediate_file ) ) {
     6501                    $intermediate_file = path_join( $uploadpath['basedir'], $intermediate_file );
     6502
     6503                    if ( ! wp_delete_file_from_directory( $intermediate_file, $intermediate_dir ) ) {
     6504                        $deleted = false;
     6505                    }
    64916506                }
    64926507            }
     
    65106525    }
    65116526
     6527
     6528
     6529
     6530
     6531
     6532
     6533
     6534
     6535
     6536
     6537
     6538
     6539
     6540
     6541
     6542
     6543
     6544
     6545
    65126546    if ( is_array( $backup_sizes ) ) {
     6547
    65136548        $del_dir = path_join( $uploadpath['basedir'], dirname( $meta['file'] ) );
    6514 
     6549        // Delete the root (edited) file which was not deleted above.
     6550        if ( ! wp_delete_file_from_directory( $file, $uploadpath['basedir'] ) ) {
     6551            $deleted = false;
     6552        }
    65156553        foreach ( $backup_sizes as $size ) {
    6516             $del_file = path_join( dirname( $meta['file'] ), $size['file'] );
    6517 
    6518             if ( ! empty( $del_file ) ) {
    6519                 $del_file = path_join( $uploadpath['basedir'], $del_file );
    6520 
    6521                 if ( ! wp_delete_file_from_directory( $del_file, $del_dir ) ) {
    6522                     $deleted = false;
     6554            // Delete files from 'sources' data if available, otherwise from 'sizes' data.
     6555            if ( isset( $meta['sources'] ) && is_array( $meta['sources'] ) ) {
     6556                // Delete any backup images stored in the 'sources' array.
     6557                if ( isset( $size['sources'] ) && is_array( $size['sources'] ) ) {
     6558                    foreach ( $size['sources'] as $mime => $properties ) {
     6559                        $del_file = path_join( dirname( $meta['file'] ), $properties['file'] );
     6560                        if ( ! empty( $del_file ) ) {
     6561                            $del_file = path_join( $uploadpath['basedir'], $del_file );
     6562                            if ( ! wp_delete_file_from_directory( $del_file, $del_dir ) ) {
     6563                                $deleted = false;
     6564                            }
     6565                        }
     6566                    }
     6567                }
     6568            } else {
     6569                $del_file = path_join( dirname( $meta['file'] ), $size['file'] );
     6570
     6571                if ( ! empty( $del_file ) ) {
     6572                    $del_file = path_join( $uploadpath['basedir'], $del_file );
     6573                    if ( ! wp_delete_file_from_directory( $del_file, $del_dir ) ) {
     6574                        $deleted = false;
     6575                    }
    65236576                }
    65246577            }
    65256578        }
    6526     }
    6527 
    6528     if ( ! wp_delete_file_from_directory( $file, $uploadpath['basedir'] ) ) {
    6529         $deleted = false;
    65306579    }
    65316580
  • trunk/tests/phpunit/tests/image/editor.php

    r52248 r53751  
    132132
    133133        // Removing PNG to WEBP conversion on save. Quality setting should reset to the default.
     134
    134135        remove_filter( 'image_editor_output_format', array( $this, 'image_editor_output_formats' ) );
    135136        $editor->save();
     
    155156
    156157        // After removing the conversion the quality setting should reset to the filtered value for the original image type, JPEG.
     158
    157159        remove_filter( 'image_editor_output_format', array( $this, 'image_editor_output_formats' ) );
    158160        $editor->save();
     
    227229
    228230        // Test with a suffix only.
    229         $this->assertSame( 'canola-100x50.png', wp_basename( $editor->generate_filename( null, null, 'png' ) ) );
     231        $this->assertSame( 'canola-100x50.png', wp_basename( $editor->generate_filename( null, null, 'png' ) ) );
    230232
    231233        // Combo!
    232         $this->assertSame( trailingslashit( realpath( get_temp_dir() ) ) . 'canola-new.png', $editor->generate_filename( 'new', realpath( get_temp_dir() ), 'png' ) );
     234        $this->assertSame( trailingslashit( realpath( get_temp_dir() ) ) . 'canola-new.png', $editor->generate_filename( 'new', realpath( get_temp_dir() ), 'png' ) );
    233235
    234236        // Test with a stream destination.
     
    363365    }
    364366
     367
     368
     369
     370
     371
     372
     373
     374
     375
     376
     377
     378
     379
     380
     381
     382
     383
     384
     385
     386
     387
     388
     389
     390
     391
     392
     393
     394
     395
     396
     397
     398
     399
     400
     401
     402
     403
     404
     405
     406
     407
     408
     409
     410
     411
     412
     413
     414
     415
     416
     417
     418
     419
     420
     421
     422
     423
     424
     425
     426
     427
     428
     429
     430
     431
     432
     433
     434
     435
     436
     437
     438
     439
     440
     441
     442
     443
     444
     445
     446
     447
     448
     449
     450
     451
     452
     453
     454
     455
     456
     457
     458
     459
     460
     461
     462
     463
     464
     465
     466
     467
     468
     469
     470
     471
     472
     473
     474
     475
     476
     477
     478
     479
     480
     481
     482
     483
     484
     485
     486
     487
     488
     489
     490
     491
     492
     493
     494
     495
     496
     497
     498
     499
     500
     501
     502
     503
     504
     505
     506
     507
     508
     509
     510
     511
     512
     513
     514
     515
     516
     517
     518
     519
     520
     521
     522
     523
     524
     525
     526
     527
     528
     529
     530
     531
     532
     533
     534
     535
     536
     537
     538
     539
     540
     541
     542
     543
     544
     545
     546
     547
     548
     549
     550
     551
     552
     553
     554
     555
     556
     557
     558
     559
     560
     561
     562
     563
     564
     565
     566
     567
     568
     569
     570
     571
     572
     573
     574
     575
     576
     577
     578
     579
     580
     581
     582
     583
     584
     585
     586
     587
     588
     589
     590
     591
     592
     593
     594
     595
     596
     597
     598
     599
     600
     601
     602
     603
     604
     605
     606
     607
     608
     609
     610
     611
     612
     613
     614
     615
     616
     617
     618
     619
     620
     621
     622
     623
     624
     625
     626
     627
     628
     629
     630
     631
     632
     633
     634
     635
     636
     637
     638
     639
     640
     641
     642
     643
     644
     645
     646
     647
     648
     649
     650
     651
     652
     653
     654
     655
     656
     657
     658
     659
     660
     661
     662
     663
     664
     665
     666
     667
     668
     669
     670
     671
     672
     673
     674
     675
     676
     677
     678
     679
     680
     681
     682
     683
     684
     685
     686
     687
     688
     689
     690
     691
     692
     693
     694
     695
     696
     697
     698
     699
     700
     701
     702
     703
     704
     705
     706
     707
     708
     709
     710
     711
     712
     713
     714
     715
     716
     717
     718
     719
     720
     721
     722
     723
     724
     725
     726
     727
     728
     729
     730
     731
     732
     733
     734
     735
     736
     737
     738
     739
     740
     741
     742
     743
     744
     745
     746
     747
     748
     749
     750
     751
     752
     753
     754
     755
     756
     757
     758
     759
     760
     761
     762
     763
     764
     765
     766
    365767}
  • trunk/tests/phpunit/tests/image/functions.php

    r53542 r53751  
    640640        }
    641641
     642
     643
     644
    642645        $orig_file = DIR_TESTDATA . '/images/wordpress-gsoc-flyer.pdf';
    643646        $test_file = get_temp_dir() . 'wordpress-gsoc-flyer.pdf';
     
    678681                    'mime-type' => 'image/jpeg',
    679682                    'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-232x300.jpg' ),
     683
     684
     685
     686
     687
     688
    680689                ),
    681690                'large'     => array(
     
    685694                    'mime-type' => 'image/jpeg',
    686695                    'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-791x1024.jpg' ),
     696
     697
     698
     699
     700
     701
    687702                ),
    688703                'thumbnail' => array(
     
    692707                    'mime-type' => 'image/jpeg',
    693708                    'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-116x150.jpg' ),
     709
     710
     711
     712
     713
     714
    694715                ),
    695716            ),
     
    703724            unlink( $temp_dir . $size['file'] );
    704725        }
     726
    705727    }
    706728
     
    716738
    717739        update_option( 'medium_crop', 1 );
     740
     741
     742
    718743
    719744        $orig_file = DIR_TESTDATA . '/images/wordpress-gsoc-flyer.pdf';
     
    755780                    'mime-type' => 'image/jpeg',
    756781                    'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-300x300.jpg' ),
     782
     783
     784
     785
     786
     787
    757788                ),
    758789                'large'     => array(
     
    762793                    'mime-type' => 'image/jpeg',
    763794                    'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-791x1024.jpg' ),
     795
     796
     797
     798
     799
     800
     801
    764802                ),
    765803                'thumbnail' => array(
     
    769807                    'mime-type' => 'image/jpeg',
    770808                    'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-116x150.jpg' ),
     809
     810
     811
     812
     813
     814
    771815                ),
    772816            ),
     
    780824            unlink( $temp_dir . $size['file'] );
    781825        }
     826
     827
    782828    }
    783829
     
    789835            $this->markTestSkipped( 'Rendering PDFs is not supported on this system.' );
    790836        }
     837
     838
     839
    791840
    792841        $orig_file = DIR_TESTDATA . '/images/wordpress-gsoc-flyer.pdf';
     
    822871            'mime-type' => 'image/jpeg',
    823872            'filesize'  => wp_filesize( $temp_dir . 'wordpress-gsoc-flyer-pdf-77x100.jpg' ),
     873
     874
     875
     876
     877
     878
    824879        );
    825880
     
    837892            unlink( $temp_dir . $size['file'] );
    838893        }
     894
    839895    }
    840896
     
    10271083        );
    10281084    }
     1085
     1086
     1087
     1088
     1089
     1090
     1091
     1092
     1093
     1094
     1095
     1096
     1097
     1098
     1099
     1100
     1101
     1102
     1103
     1104
     1105
     1106
     1107
     1108
     1109
     1110
     1111
     1112
     1113
     1114
     1115
     1116
     1117
     1118
     1119
     1120
     1121
     1122
     1123
     1124
     1125
     1126
     1127
     1128
     1129
     1130
     1131
     1132
     1133
     1134
     1135
     1136
     1137
     1138
     1139
     1140
     1141
     1142
     1143
     1144
     1145
     1146
     1147
     1148
     1149
     1150
     1151
     1152
     1153
     1154
     1155
     1156
     1157
     1158
     1159
     1160
     1161
     1162
     1163
     1164
     1165
     1166
     1167
     1168
     1169
     1170
     1171
     1172
     1173
     1174
     1175
     1176
     1177
     1178
     1179
     1180
     1181
     1182
     1183
     1184
     1185
     1186
     1187
     1188
     1189
     1190
     1191
     1192
     1193
     1194
     1195
     1196
     1197
     1198
     1199
     1200
     1201
     1202
     1203
     1204
     1205
     1206
     1207
     1208
     1209
     1210
     1211
     1212
     1213
     1214
     1215
     1216
     1217
     1218
     1219
     1220
     1221
     1222
     1223
     1224
     1225
     1226
     1227
     1228
     1229
     1230
     1231
     1232
     1233
     1234
     1235
     1236
     1237
     1238
     1239
     1240
     1241
     1242
     1243
     1244
     1245
     1246
     1247
     1248
     1249
     1250
     1251
     1252
     1253
     1254
     1255
     1256
     1257
     1258
     1259
     1260
     1261
     1262
     1263
     1264
     1265
     1266
     1267
     1268
     1269
     1270
     1271
     1272
     1273
     1274
     1275
     1276
     1277
     1278
     1279
     1280
     1281
     1282
     1283
     1284
     1285
     1286
     1287
     1288
     1289
     1290
     1291
     1292
     1293
     1294
     1295
     1296
     1297
     1298
     1299
     1300
     1301
     1302
     1303
     1304
     1305
     1306
     1307
     1308
     1309
     1310
     1311
     1312
     1313
     1314
     1315
     1316
     1317
     1318
     1319
     1320
     1321
     1322
     1323
     1324
     1325
     1326
     1327
     1328
     1329
     1330
     1331
     1332
     1333
     1334
     1335
     1336
     1337
     1338
     1339
     1340
     1341
     1342
     1343
     1344
     1345
     1346
     1347
     1348
     1349
     1350
     1351
     1352
     1353
     1354
     1355
     1356
     1357
     1358
     1359
     1360
     1361
     1362
     1363
     1364
     1365
     1366
     1367
     1368
     1369
     1370
     1371
     1372
     1373
     1374
     1375
     1376
     1377
     1378
     1379
     1380
     1381
     1382
     1383
     1384
     1385
     1386
     1387
     1388
     1389
     1390
     1391
     1392
     1393
     1394
     1395
     1396
     1397
     1398
     1399
     1400
     1401
     1402
     1403
     1404
     1405
     1406
     1407
     1408
     1409
     1410
     1411
     1412
     1413
     1414
     1415
     1416
     1417
     1418
     1419
     1420
     1421
     1422
     1423
     1424
     1425
     1426
     1427
     1428
     1429
     1430
     1431
     1432
     1433
     1434
     1435
     1436
     1437
     1438
     1439
     1440
     1441
     1442
     1443
     1444
     1445
     1446
     1447
     1448
     1449
     1450
     1451
     1452
     1453
     1454
     1455
     1456
     1457
     1458
     1459
     1460
     1461
     1462
     1463
     1464
     1465
     1466
     1467
     1468
     1469
     1470
     1471
     1472
     1473
     1474
     1475
     1476
     1477
     1478
     1479
     1480
     1481
     1482
     1483
     1484
     1485
     1486
     1487
     1488
     1489
     1490
     1491
     1492
     1493
     1494
     1495
     1496
     1497
     1498
     1499
     1500
     1501
     1502
     1503
     1504
     1505
     1506
     1507
     1508
     1509
     1510
     1511
    10291512}
  • trunk/tests/phpunit/tests/media.php

    r53558 r53751  
    22522252        add_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' );
    22532253        add_filter( 'wp_img_tag_add_loading_attr', '__return_false' );
     2254
    22542255
    22552256        $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) );
     
    22572258        remove_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' );
    22582259        remove_filter( 'wp_img_tag_add_loading_attr', '__return_false' );
     2260
     2261
    22592262    }
    22602263
     
    22902293        $img = wp_img_tag_add_decoding_attr( $img, 'the_content' );
    22912294        $img = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . 'srcset="image2x.jpg 2x" />', $img );
     2295
    22922296
    22932297        // The content filter should return the image unchanged.
    22942298        $this->assertSame( $img, wp_filter_content_tags( $img ) );
     2299
     2300
    22952301    }
    22962302
     
    23622368        add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
    23632369        add_filter( 'wp_img_tag_add_decoding_attr', '__return_false' );
     2370
    23642371
    23652372        add_filter(
     
    24242431     */
    24252432    public function test_wp_filter_content_tags_schemes() {
     2433
    24262434        $image_meta = wp_get_attachment_metadata( self::$large_id );
    24272435        $size_array = $this->get_image_size_array_from_meta( $image_meta, 'medium' );
     
    24692477
    24702478        $this->assertSame( $expected, $actual );
     2479
    24712480    }
    24722481
     
    29622971        add_filter( 'wp_img_tag_add_loading_attr', '__return_false' );
    29632972        add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
     2973
    29642974
    29652975        $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) );
     
    29672977        remove_filter( 'wp_img_tag_add_loading_attr', '__return_false' );
    29682978        remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
     2979
    29692980    }
    29702981
     
    30423053        add_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' );
    30433054        add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
     3055
    30443056
    30453057        $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) );
     
    30473059        remove_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' );
    30483060        remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
     3061
    30493062    }
    30503063
     
    30753088        add_filter( 'wp_lazy_loading_enabled', '__return_true' );
    30763089
     3090
     3091
    30773092        $this->assertSame( $content_filtered, wp_filter_content_tags( $content_unfiltered ) );
    30783093        remove_filter( 'wp_lazy_loading_enabled', '__return_true' );
    30793094        remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
     3095
     3096
    30803097    }
    30813098
     
    31023119        add_filter( 'wp_lazy_loading_enabled', '__return_false' );
    31033120
     3121
     3122
    31043123        $this->assertSame( $content, wp_filter_content_tags( $content ) );
    31053124        remove_filter( 'wp_lazy_loading_enabled', '__return_false' );
    31063125        remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
     3126
    31073127    }
    31083128
     
    35303550    function test_wp_filter_content_tags_with_wp_get_loading_attr_default() {
    35313551        global $wp_query, $wp_the_query;
     3552
    35323553
    35333554        $img1         = get_image_tag( self::$large_id, '', '', '', 'large' );
     
    35653586            remove_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
    35663587        }
     3588
    35673589
    35683590        // After filtering, the first image should not be lazy-loaded while the other ones should be.
     
    36143636        remove_filter( 'wp_omit_loading_attr_threshold', '__return_null', 100 );
    36153637    }
     3638
     3639
     3640
     3641
     3642
     3643
     3644
     3645
     3646
     3647
     3648
     3649
     3650
     3651
     3652
     3653
     3654
     3655
     3656
     3657
     3658
     3659
     3660
     3661
     3662
     3663
     3664
     3665
     3666
     3667
     3668
     3669
     3670
     3671
     3672
     3673
     3674
     3675
     3676
     3677
     3678
     3679
     3680
     3681
     3682
     3683
     3684
     3685
     3686
     3687
     3688
     3689
     3690
     3691
     3692
     3693
     3694
     3695
     3696
     3697
     3698
     3699
     3700
     3701
     3702
     3703
     3704
     3705
     3706
     3707
     3708
     3709
     3710
     3711
     3712
     3713
     3714
     3715
     3716
     3717
     3718
     3719
     3720
     3721
     3722
     3723
     3724
     3725
     3726
     3727
     3728
     3729
     3730
     3731
     3732
     3733
     3734
     3735
     3736
     3737
     3738
     3739
     3740
     3741
     3742
     3743
     3744
     3745
     3746
     3747
     3748
     3749
     3750
     3751
     3752
     3753
     3754
     3755
     3756
     3757
     3758
     3759
     3760
     3761
     3762
     3763
     3764
     3765
     3766
     3767
     3768
     3769
     3770
     3771
     3772
     3773
     3774
     3775
     3776
     3777
     3778
     3779
     3780
     3781
     3782
     3783
     3784
     3785
     3786
     3787
     3788
     3789
     3790
     3791
     3792
     3793
     3794
     3795
     3796
     3797
     3798
     3799
     3800
    36163801}
    36173802
Note: See TracChangeset for help on using the changeset viewer.