Make WordPress Core

Ticket #43454: 43454.2.diff

File 43454.2.diff, 8.6 KB (added by audrasjb, 6 years ago)

adds gps-location class and related css is the attachement is gps localized (first attempt for visual rendering)

  • src/wp-admin/includes/image.php

    diff --git a/src/wp-admin/includes/image.php b/src/wp-admin/includes/image.php
    index 0f03e2c..a729715 100644
    a b function wp_exif_date2ts( $str ) { 
    337337}
    338338
    339339/**
     340 * Convert the EXIF geographical longitude and latitude from degrees, minutes
     341 * and seconds to degrees format.
     342 *
     343 * @since 5.0.0
     344 *
     345 * @param string $coordinate The coordinate to convert to degrees format.
     346 * @return float Coordinate in degrees format.
     347 */
     348function wp_exif_gps_convert( $coordinate ) {
     349        @list( $degree, $minute, $second ) = $coordinate;
     350        $float = wp_exif_frac2dec( $degree ) + ( wp_exif_frac2dec( $minute ) / 60 ) + ( wp_exif_frac2dec( $second ) / 3600 );
     351
     352        return ( ( is_float( $float ) || ( is_int( $float ) && $degree == $float ) ) && ( abs( $float ) <= 180 ) ) ? $float : 999;
     353}
     354
     355/**
    340356 * Get extended image metadata, exif or iptc as available.
    341357 *
    342358 * Retrieves the EXIF metadata aperture, credit, camera, caption, copyright, iso
    343  * created_timestamp, focal_length, shutter_speed, and title.
     359 * created_timestamp, focal_length, shutter_speed, title, and location
     360 * (latitude & longitude).
    344361 *
    345362 * The IPTC metadata that is retrieved is APP13, credit, byline, created date
    346363 * and time, caption, copyright, and title. Also includes FNumber, Model,
    function wp_read_image_metadata( $file ) { 
    501518                if ( ! empty( $exif['Orientation'] ) ) {
    502519                        $meta['orientation'] = $exif['Orientation'];
    503520                }
     521
     522                /**
     523                 * Filters whether geographical EXIF data should parsed and stored in
     524                 * meta.
     525                 *
     526                 * @param bool true Default is to parse geographical data.
     527                 */
     528                $process_geo_data = (bool) apply_filters( 'wp_read_image_geo_data', true );
     529
     530                if ( $process_geo_data ) {
     531                        $meta['location'] = array(
     532                                'longitude' => '',
     533                                'latitude'  => '',
     534                        );
     535
     536                        if ( ! empty( $exif['GPSLongitude'] ) && count( $exif['GPSLongitude'] ) == 3 && ! empty( $exif['GPSLongitudeRef'] ) ) {
     537                                $meta['location']['longitude'] = round( ( $exif['GPSLongitudeRef'] == 'W' ? - 1 : 1 ) * wp_exif_gps_convert( $exif['GPSLongitude'] ), 7 );
     538                        }
     539                        if ( ! empty( $exif['GPSLatitude'] ) && count( $exif['GPSLatitude'] ) == 3 && ! empty( $exif['GPSLatitudeRef'] ) ) {
     540                                $meta['location']['latitude'] = round( ( $exif['GPSLatitudeRef'] == 'S' ? - 1 : 1 ) * wp_exif_gps_convert( $exif['GPSLatitude'] ), 7 );
     541                        }
     542                }
    504543        }
    505544
    506545        foreach ( array( 'title', 'caption', 'credit', 'copyright', 'camera', 'iso' ) as $key ) {
  • src/wp-includes/css/media-views.css

    diff --git a/src/wp-includes/css/media-views.css b/src/wp-includes/css/media-views.css
    index d86c2bd..f0de45c 100644
    a b  
    889889        max-height: 100%;
    890890}
    891891
     892.wp-core-ui .attachment-preview.gps-location:after {
     893        content: "\f230";
     894        font: normal 20px/1 dashicons;
     895        speak: none;
     896        vertical-align: middle;
     897        -webkit-font-smoothing: antialiased;
     898        -moz-osx-font-smoothing: grayscale;
     899        display: block;
     900        position: absolute;
     901        top: 0;
     902        right: 0;
     903        margin: 0;
     904        padding: .2em;
     905        background: rgba(200, 200, 200, .5)
     906}
     907
    892908.wp-core-ui .attachment .thumbnail:after {
    893909        content: "";
    894910        display: block;
  • src/wp-includes/media-template.php

    diff --git a/src/wp-includes/media-template.php b/src/wp-includes/media-template.php
    index 0dbba58..f05d6c2 100644
    a b function wp_print_media_templates() { 
    375375                                        <# } #>
    376376                                <# } #>
    377377
     378                                <# if ( data.longitude && data.latitude ) { #>
     379                                        <div class="location"><strong><?php _e( 'GPS coordinates:' ); ?></strong> {{ data.longitude }},{{ data.latitude }}</div>
     380                                <# } #>
     381
    378382                                <# if ( data.fileLength ) { #>
    379383                                        <div class="file-length"><strong><?php _e( 'Length:' ); ?></strong> {{ data.fileLength }}</div>
    380384                                <# } #>
    function wp_print_media_templates() { 
    473477        </script>
    474478
    475479        <script type="text/html" id="tmpl-attachment">
    476                 <div class="attachment-preview js--select-attachment type-{{ data.type }} subtype-{{ data.subtype }} {{ data.orientation }}">
     480                <div class="attachment-preview js--select-attachment type-{{ data.type }} subtype-{{ data.subtype }} {{ data.orientation }} {{ data.gpsClass }}">
    477481                        <div class="thumbnail">
    478482                                <# if ( data.uploading ) { #>
    479483                                        <div class="media-progress-bar"><div style="width: {{ data.percent }}%"></div></div>
  • src/wp-includes/media.php

    diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php
    index 8965da9..8321bdc 100644
    a b function wp_prepare_attachment_for_js( $attachment ) { 
    32073207        $attachment_url = wp_get_attachment_url( $attachment->ID );
    32083208        $base_url       = str_replace( wp_basename( $attachment_url ), '', $attachment_url );
    32093209
     3210        $gps_class = '';
     3211        if ( isset( $meta['image_meta']['location']['longitude'] ) && isset( $meta['image_meta']['location']['latitude'] ) ) {
     3212                $gps_class = 'gps-location';
     3213        }
     3214       
    32103215        $response = array(
    32113216                'id'            => $attachment->ID,
    32123217                'title'         => $attachment->post_title,
    function wp_prepare_attachment_for_js( $attachment ) { 
    32283233                'subtype'       => $subtype,
    32293234                'icon'          => wp_mime_type_icon( $attachment->ID ),
    32303235                'dateFormatted' => mysql2date( __( 'F j, Y' ), $attachment->post_date ),
     3236                'longitude'     => $meta['image_meta']['location']['longitude'],
     3237                'latitude'      => $meta['image_meta']['location']['latitude'],
     3238                'gpsClass'              => $gps_class,
    32313239                'nonces'        => array(
    32323240                        'update' => false,
    32333241                        'delete' => false,
    function wp_prepare_attachment_for_js( $attachment ) { 
    32893297        if ( current_user_can( 'delete_post', $attachment->ID ) ) {
    32903298                $response['nonces']['delete'] = wp_create_nonce( 'delete-post_' . $attachment->ID );
    32913299        }
    3292 
     3300       
    32933301        if ( $meta && ( 'image' === $type || ! empty( $meta['sizes'] ) ) ) {
    32943302                $sizes = array();
    32953303
  • tests/phpunit/tests/image/meta.php

    diff --git a/tests/phpunit/tests/image/meta.php b/tests/phpunit/tests/image/meta.php
    index 0d791b9..b688881 100644
    a b class Tests_Image_Meta extends WP_UnitTestCase { 
    3131                $this->assertEquals( 400, $out['iso'] );
    3232                $this->assertEquals( 1 / 40, $out['shutter_speed'] );
    3333                $this->assertEquals( '', $out['title'] );
     34                $this->assertEquals( array( 'longitude' => '', 'latitude' => '' ), $out['location'] );
    3435        }
    3536
    3637        function test_exif_d70_mf() {
    class Tests_Image_Meta extends WP_UnitTestCase { 
    4748                $this->assertEquals( 0, $out['iso'] ); // interesting - a Nikon bug?
    4849                $this->assertEquals( 1 / 500, $out['shutter_speed'] );
    4950                $this->assertEquals( '', $out['title'] );
     51                $this->assertEquals( array( 'longitude' => '', 'latitude' => '' ), $out['location'] );
    5052                #$this->assertEquals(array('Flowers'), $out['keywords']);
    5153        }
    5254
    class Tests_Image_Meta extends WP_UnitTestCase { 
    6466                $this->assertEquals( 200, $out['iso'] );
    6567                $this->assertEquals( 1 / 25, $out['shutter_speed'] );
    6668                $this->assertEquals( 'IPTC Headline', $out['title'] );
     69                $this->assertEquals( array( 'longitude' => '', 'latitude' => '' ), $out['location'] );
    6770        }
    6871
    6972        function test_exif_fuji() {
    class Tests_Image_Meta extends WP_UnitTestCase { 
    8083                $this->assertEquals( 64, $out['iso'] );
    8184                $this->assertEquals( 1 / 320, $out['shutter_speed'] );
    8285                $this->assertEquals( '', $out['title'] );
    83 
     86                $this->assertEquals( array( 'longitude' => '', 'latitude' => '' ), $out['location'] );
    8487        }
    8588
    8689        /**
    class Tests_Image_Meta extends WP_UnitTestCase { 
    102105                $this->assertEquals( 0, $out['iso'] );
    103106                $this->assertEquals( 0, $out['shutter_speed'] );
    104107                $this->assertEquals( '', $out['title'] );
     108                $this->assertEquals( array( 'longitude' => '', 'latitude' => '' ), $out['location'] );
    105109        }
    106110
    107111        function test_exif_no_data() {
    class Tests_Image_Meta extends WP_UnitTestCase { 
    118122                $this->assertEquals( 0, $out['iso'] );
    119123                $this->assertEquals( 0, $out['shutter_speed'] );
    120124                $this->assertEquals( '', $out['title'] );
     125                $this->assertEquals( array( 'longitude' => '', 'latitude' => '' ), $out['location'] );
    121126        }
    122127
    123128        /**
    class Tests_Image_Meta extends WP_UnitTestCase { 
    160165                $this->assertEquals( 'Photoshop Document Ttitle', $out['title'] );
    161166                $this->assertEquals( 1, $out['orientation'] );
    162167                $this->assertEquals( array( 'beach', 'baywatch', 'LA', 'sunset' ), $out['keywords'] );
     168                $this->assertEquals( array( 'longitude' => '', 'latitude' => '' ), $out['location'] );
    163169        }
    164170
     171        /**
     172         * @ticket 9257
     173         */
     174        function test_exif_location() {
     175                $out = wp_read_image_metadata( DIR_TESTDATA . '/images/test-gps-location.jpg' );
     176
     177                $this->assertEquals( '39.0058333', $out['location']['longitude'] );
     178                $this->assertEquals( '-69.0028333', $out['location']['latitude'] );
     179        }
    165180}