Make WordPress Core

Changeset 31694


Ignore:
Timestamp:
03/10/2015 05:06:39 AM (12 years ago)
Author:
SergeyBiryukov
Message:

EXIF/IPTC captions should populate Caption (post_excerpt) on upload, not Description (post_content).

Make sure the caption is always set if found. Previously, if the caption was less than 80 characters, only the Title field would be set.

props beaulebens, ericlewis, bendoh, SergeyBiryukov.
fixes #22768.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/image.php

    r31645 r31694  
    300300                        if ( ! empty( $iptc['2#120'][0] ) ) { // description / legacy caption
    301301                                $caption = trim( $iptc['2#120'][0] );
    302                                 if ( empty( $meta['title'] ) ) {
    303                                         mbstring_binary_safe_encoding();
    304                                         $caption_length = strlen( $caption );
    305                                         reset_mbstring_encoding();
    306 
     302
     303                                mbstring_binary_safe_encoding();
     304                                $caption_length = strlen( $caption );
     305                                reset_mbstring_encoding();
     306
     307                                if ( empty( $meta['title'] ) && $caption_length < 80 ) {
    307308                                        // Assume the title is stored in 2:120 if it's short.
    308                                         if ( $caption_length < 80 ) {
    309                                                 $meta['title'] = $caption;
    310                                         } else {
    311                                                 $meta['caption'] = $caption;
    312                                         }
    313                                 } elseif ( $caption != $meta['title'] ) {
    314                                         $meta['caption'] = $caption;
     309                                        $meta['title'] = $caption;
    315310                                }
     311
     312                                $meta['caption'] = $caption;
    316313                        }
    317314
    … …  
    347344                                // Assume the title is stored in ImageDescription
    348345                                $meta['title'] = trim( $exif['ImageDescription'] );
    349                                 if ( empty( $meta['caption'] ) && ! empty( $exif['COMPUTED']['UserComment'] ) && trim( $exif['COMPUTED']['UserComment'] ) != $meta['title'] ) {
    350                                         $meta['caption'] = trim( $exif['COMPUTED']['UserComment'] );
    351                                 }
    352                         } elseif ( empty( $meta['caption'] ) && trim( $exif['ImageDescription'] ) != $meta['title'] ) {
     346                        }
     347
     348                        if ( empty( $meta['caption'] ) && ! empty( $exif['COMPUTED']['UserComment'] ) ) {
     349                                $meta['caption'] = trim( $exif['COMPUTED']['UserComment'] );
     350                        }
     351
     352                        if ( empty( $meta['caption'] ) ) {
    353353                                $meta['caption'] = trim( $exif['ImageDescription'] );
    354354                        }
    355                 } elseif ( empty( $meta['caption'] ) && ! empty( $exif['Comments'] ) && trim( $exif['Comments'] ) != $meta['title'] ) {
     355                } elseif ( empty( $meta['caption'] ) && ! empty( $exif['Comments'] ) ) {
    356356                        $meta['caption'] = trim( $exif['Comments'] );
    357357                }
  • trunk/src/wp-admin/includes/media.php

    r31645 r31694  
    281281        $title = $name;
    282282        $content = '';
     283        $excerpt = '';
    283284
    284285        if ( preg_match( '#^audio#', $type ) ) {
    285286                $meta = wp_read_audio_metadata( $file );
    286287
    287                 if ( ! empty( $meta['title'] ) )
     288                if ( ! empty( $meta['title'] ) ) {
    288289                        $title = $meta['title'];
    289 
    290                 $content = '';
     290                }
    291291
    292292                if ( ! empty( $title ) ) {
    … …  
    336336        // Use image exif/iptc data for title and caption defaults if possible.
    337337        } elseif ( 0 === strpos( $type, 'image/' ) && $image_meta = @wp_read_image_metadata( $file ) ) {
    338                 if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) )
     338                if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) {
    339339                        $title = $image_meta['title'];
    340                 if ( trim( $image_meta['caption'] ) )
    341                         $content = $image_meta['caption'];
     340                }
     341
     342                if ( trim( $image_meta['caption'] ) ) {
     343                        $excerpt = $image_meta['caption'];
     344                }
    342345        }
    343346
    … …  
    349352                'post_title' => $title,
    350353                'post_content' => $content,
     354                'post_excerpt' => $excerpt,
    351355        ), $post_data );
    352356
  • trunk/tests/phpunit/tests/media.php

    r31574 r31694  
    541541                $this->assertEquals( $expected, $filetype );
    542542        }
     543
     544        /**
     545         * @ticket 22768
     546         */
     547        public function test_media_handle_upload_sets_post_excerpt() {
     548                $iptc_file = DIR_TESTDATA . '/images/test-image-iptc.jpg';
     549
     550                // Make a copy of this file as it gets moved during the file upload
     551                $tmp_name = wp_tempnam( $iptc_file );
     552
     553                copy( $iptc_file, $tmp_name );
     554
     555                $_FILES['upload'] = array(
     556                        'tmp_name' => $tmp_name,
     557                        'name'     => 'test-image-iptc.jpg',
     558                        'type'     => 'image/jpeg',
     559                        'error'    => 0,
     560                        'size'     => filesize( $iptc_file )
     561                );
     562
     563                $post_id = media_handle_upload( 'upload', 0, array(), array( 'action' => 'test_iptc_upload', 'test_form' => false ) );
     564
     565                unset( $_FILES['upload'] );
     566
     567                $post = get_post( $post_id );
     568
     569                $this->assertEquals( 'This is a comment. / Это комментарий. / Βλέπετε ένα σχόλιο.', $post->post_excerpt );
     570        }
     571
    543572}
Note: See TracChangeset for help on using the changeset viewer.