Making WordPress.org

Changeset 13974


Ignore:
Timestamp:
08/19/2024 08:03:45 PM (7 weeks ago)
Author:
coffee2code
Message:

Photo Directory, Photo: Add function to retrieve all EXIF data from a photo.

See #7734.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/photo-directory/inc/photo.php

    r13173 r13974  
    968968
    969969    /**
     970     * Strips markups from text and UTF8 encodes it if it appears to be UTF8.
     971     *
     972     * @param string $text Text to strip of tags and UTF8 encode.
     973     * @return string
     974     */
     975    public static function _strip_and_utf8_encode( $text ) {
     976        $text = wp_kses( $text, 'strip' );
     977
     978        if ( $text && ! seems_utf8( $text ) ) {
     979            $text = utf8_encode( $text );
     980        }
     981
     982        return $text;
     983    }
     984
     985    /**
     986     * Returns all EXIF data for a photo, not just the hardcoded subset returned
     987     * by `wp_read_image_metadata()`.
     988     *
     989     * Also returns the data completely raw, without any reformatting other than
     990     * sanitization.
     991     *
     992     * @param int $post_id The ID of the photo post.
     993     * @return false|array The sanitized raw EXIF data, or false if the file was
     994     *                     not found or `exif_read_data()` is not available.
     995     */
     996    public static function get_all_exif( $post_id ) {
     997        $image_id = get_post_thumbnail_id( $post_id );
     998        $file = get_attached_file( $image_id );
     999        $exif = [];
     1000
     1001        // Bail if `exif_read_data()` is not available or the file no longer exists.
     1002        if ( ! is_callable( 'exif_read_data' ) || ! file_exists( $file ) ) {
     1003            return false;
     1004        }
     1005
     1006        $exif = self::exif_read_data_as_data_stream( $file );
     1007
     1008        if ( ! $exif ) {
     1009            return [];
     1010        }
     1011
     1012        // Ignore EXIF keys that are definitely not worth including.
     1013        $ignored_exif = apply_filters(
     1014            'wporg_photos-ignored_exif_keys',
     1015            [ 'UndefinedTag:0x9AAA' ]
     1016        );
     1017        if ( $ignored_exif && is_array( $ignored_exif ) ) {
     1018            foreach ( $ignored_exif as $key ) {
     1019                unset( $exif[ $key ] );
     1020            }
     1021        }
     1022
     1023        return map_deep( $exif, [__CLASS__, '_strip_and_utf8_encode' ] );
     1024    }
     1025
     1026    /**
    9701027     * Returns the IP address for the photo contributor.
    9711028     *
Note: See TracChangeset for help on using the changeset viewer.