WordPress.org

Make WordPress Core

Ticket #32378: 32378-3.patch

File 32378-3.patch, 5.7 KB (added by dmsnell, 2 years ago)

oops - fix: spaces -> tabs

  • wp-admin/includes/admin-filters.php

    diff --git a/wp-admin/includes/admin-filters.php b/wp-admin/includes/admin-filters.php
    index 141e8aa..f801f61 100644
    a b add_filter( 'async_upload_video', 'get_media_item', 10, 2 ); 
    3131add_filter( 'async_upload_file',  'get_media_item', 10, 2 );
    3232
    3333add_filter( 'attachment_fields_to_save', 'image_attachment_fields_to_save', 10, 2 );
     34add_filter( 'wp_read_image_metadata', 'image_metadata_substitute_defaults', 10, 3 );
    3435
    3536add_filter( 'media_upload_gallery', 'media_upload_gallery' );
    3637add_filter( 'media_upload_library', 'media_upload_library' );
  • wp-admin/includes/image.php

    diff --git a/wp-admin/includes/image.php b/wp-admin/includes/image.php
    index 4ae53b9..ca75bf7 100644
    a b function wp_read_image_metadata( $file ) { 
    507507}
    508508
    509509/**
     510 * Cleans up unwanted default metadata from images
     511 *
     512 * Some cameras store unwanted values in the EXIF data for
     513 * their photos. For example, some older Olympus cameras
     514 * store "OLYMPUS DIGITAL CAMERA" as the image caption.
     515 *
     516 * These values should probably never persist with the images
     517 * because they don't reflect the meaning of the same fields
     518 * from inside of WordPress.
     519 *
     520 * @since 4.7.0
     521 * @uses image_metadata_apply_substitution_rules
     522 *
     523 * @param array $meta image meta data
     524 * @param string $file path to image file
     525 * @param int $sourceImageType type of image
     526 * @return array $meta possibly transformed image meta
     527 */
     528function image_metadata_substitute_defaults( $meta, $file, $sourceImageType ) {
     529        /**
     530         * list of predicate/replacement pairs
     531         *
     532         * each predicate specifies a field name in the image meta
     533         * and a RegExp pattern to apply to the given field in order
     534         * to determine if the value should be replaced
     535         *
     536         * each replacement specifies the kind of replacement to
     537         * perform, the target field receiving the replacement value,
     538         * and the information needed to actually provide that value
     539         *
     540         * replacement types:
     541         *   EXIF   - use the value from a given EXIF field in the image
     542         *   string - use the given string value
     543         */
     544        $rules = array(
     545                'Olympus_Default_Title' => array(
     546                        array( 'field' => 'title', 'pattern' => '^OLYMPUS DIGITAL CAMERA$' ),
     547                        array( 'type' => 'EXIF', 'field' => 'title', 'value' => 'FileName' ),
     548                ),
     549                'Olympus_Default_Caption' => array(
     550                        array( 'field' => 'caption', 'pattern' => '^OLYMPUS DIGITAL CAMERA$' ),
     551                        array( 'type' => 'string', 'field' => 'caption', 'value' => '' ),
     552                )
     553        );
     554
     555        $image_types = apply_filters(
     556                'wp_read_image_metadata_types',
     557                array(
     558                        IMAGETYPE_JPEG,
     559                        IMAGETYPE_TIFF_II,
     560                        IMAGETYPE_TIFF_MM,
     561                )
     562        );
     563
     564        return image_metadata_apply_substitution_rules(
     565                $meta,
     566                $file,
     567                $sourceImageType,
     568                $rules,
     569                'exif_read_data',
     570                $image_types
     571        );
     572}
     573
     574/**
     575 * Applies transformations to image meta to remove unwanted defaults
     576 *
     577 * The rules in this function describe which values are
     578 * unwanted and how they should be replaced.
     579 *
     580 * @since 4.7.0
     581 * @used-by image_metadata_substitute_defaults
     582 * @internal
     583 *
     584 * @param array $meta image meta data
     585 * @param string $file path to image file
     586 * @param int $sourceImageType type of image
     587 * @param array $rules predicate/replacement pairs of metadata to replace
     588 * @param callable $exif_reader 'exif_read_data' or custom function for mock data while testing (file path -> EXIF array)
     589 * @param int[] $image_types list of valid image types on which to operate
     590 * @return array $meta possibly transformed image meta
     591 */
     592function image_metadata_apply_substitution_rules( $meta, $file, $sourceImageType, $rules, $exif_reader, $image_types ) {
     593        /** @var string[] $substitutions list of rules which apply to the current image */
     594        $substitutions = array();
     595
     596        /** @var bool $need_exif_data if any of the applicable rules depend on image EXIF data */
     597        $need_exif_data = false;
     598
     599        // find which (if any) rules apply to the image
     600        foreach( $rules as $rule_name => $rule ) {
     601                list( $predicate, $replacement ) = $rule;
     602
     603                // only apply rule if meta values match given predicate
     604                if ( 1 !== preg_match( $predicate['pattern'], $meta[ $predicate['field'] ] ) ) {
     605                        continue;
     606                }
     607
     608                $substitutions[] = $rule_name;
     609                $need_exif_data = $need_exif_data || ( $replacement['type'] === 'EXIF' );
     610        }
     611
     612        // abort early if no substitutions are needed
     613        if ( 0 === count( $substitutions ) ) {
     614                return $meta;
     615        }
     616
     617        /** @var bool $can_get_exif_data if we _need_ and _can_ read the image EXIF data */
     618        $can_get_exif_data = (
     619                $need_exif_data &&
     620                is_callable( 'exif_read_data' ) &&
     621                in_array( $sourceImageType, $image_types )
     622        );
     623
     624        /** @var array|bool $raw_exif_data we might get `false` if no data is available */
     625        $raw_exif_data = $can_get_exif_data
     626                ? @call_user_func( $exif_reader, $file )
     627                : array();
     628
     629        /** @var array $exif_data normalized exif results where no data is an empty array */
     630        $exif_data = is_array( $raw_exif_data )
     631                ? $raw_exif_data
     632                : array();
     633
     634        /** @var array $updates holds the new meta field name / value pairs for replacement */
     635        $updates = array();
     636
     637        // build the list of replacements
     638        foreach( $substitutions as $rule_name ) {
     639                list( /* predicate */, $replacement ) = $rules[ $rule_name ];
     640
     641                if ( 'EXIF' === $replacement['type'] && ! empty( $exif_data[ $replacement['value'] ] ) ) {
     642                        $updates[ $replacement['field'] ] = trim( $exif_data[ $replacement['value'] ] );
     643                        continue;
     644                }
     645
     646                if ( 'string' === $replacement['type'] ) {
     647                        $updates[ $replacement['field'] ] = $replacement['value'];
     648                        continue;
     649                }
     650        }
     651
     652        // return the transformed metadata
     653        return array_merge( $meta, $updates );
     654}
     655
     656/**
    510657 * Validate that file is an image.
    511658 *
    512659 * @since 2.5.0