diff --git a/src/wp-admin/includes/image.php b/src/wp-admin/includes/image.php index 9171247d2f..355a63d737 100644 --- a/src/wp-admin/includes/image.php +++ b/src/wp-admin/includes/image.php @@ -299,20 +299,17 @@ function wp_read_image_metadata( $file ) { if ( ! empty( $iptc['2#120'][0] ) ) { // description / legacy caption $caption = trim( $iptc['2#120'][0] ); - if ( empty( $meta['title'] ) ) { - mbstring_binary_safe_encoding(); - $caption_length = strlen( $caption ); - reset_mbstring_encoding(); + mbstring_binary_safe_encoding(); + $caption_length = strlen( $caption ); + reset_mbstring_encoding(); + + if ( empty( $meta['title'] ) && $caption_length < 80 ) { // Assume the title is stored in 2:120 if it's short. - if ( $caption_length < 80 ) { - $meta['title'] = $caption; - } else { - $meta['caption'] = $caption; - } - } elseif ( $caption != $meta['title'] ) { - $meta['caption'] = $caption; + $meta['title'] = $caption; } + + $meta['caption'] = $caption; } if ( ! empty( $iptc['2#110'][0] ) ) // credit @@ -346,13 +343,16 @@ function wp_read_image_metadata( $file ) { if ( empty( $meta['title'] ) && $description_length < 80 ) { // Assume the title is stored in ImageDescription $meta['title'] = trim( $exif['ImageDescription'] ); - if ( empty( $meta['caption'] ) && ! empty( $exif['COMPUTED']['UserComment'] ) && trim( $exif['COMPUTED']['UserComment'] ) != $meta['title'] ) { - $meta['caption'] = trim( $exif['COMPUTED']['UserComment'] ); - } - } elseif ( empty( $meta['caption'] ) && trim( $exif['ImageDescription'] ) != $meta['title'] ) { + } + + if ( empty( $meta['caption'] ) && ! empty( $exif['COMPUTED']['UserComment'] ) ) { + $meta['caption'] = trim( $exif['COMPUTED']['UserComment'] ); + } + + if ( empty( $meta['caption'] ) ) { $meta['caption'] = trim( $exif['ImageDescription'] ); } - } elseif ( empty( $meta['caption'] ) && ! empty( $exif['Comments'] ) && trim( $exif['Comments'] ) != $meta['title'] ) { + } elseif ( empty( $meta['caption'] ) && ! empty( $exif['Comments'] ) ) { $meta['caption'] = trim( $exif['Comments'] ); } diff --git a/src/wp-admin/includes/media.php b/src/wp-admin/includes/media.php index 5f6d08c7ab..2b3196c008 100644 --- a/src/wp-admin/includes/media.php +++ b/src/wp-admin/includes/media.php @@ -280,14 +280,14 @@ function media_handle_upload($file_id, $post_id, $post_data = array(), $override $file = $file['file']; $title = $name; $content = ''; + $excerpt = ''; if ( preg_match( '#^audio#', $type ) ) { $meta = wp_read_audio_metadata( $file ); - if ( ! empty( $meta['title'] ) ) + if ( ! empty( $meta['title'] ) ) { $title = $meta['title']; - - $content = ''; + } if ( ! empty( $title ) ) { @@ -335,10 +335,13 @@ function media_handle_upload($file_id, $post_id, $post_data = array(), $override // Use image exif/iptc data for title and caption defaults if possible. } elseif ( 0 === strpos( $type, 'image/' ) && $image_meta = @wp_read_image_metadata( $file ) ) { - if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) + if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) ) { $title = $image_meta['title']; - if ( trim( $image_meta['caption'] ) ) - $content = $image_meta['caption']; + } + + if ( trim( $image_meta['caption'] ) ) { + $excerpt = $image_meta['caption']; + } } // Construct the attachment array @@ -348,6 +351,7 @@ function media_handle_upload($file_id, $post_id, $post_data = array(), $override 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, + 'post_excerpt' => $excerpt, ), $post_data ); // This should never be set as it would then overwrite an existing attachment. diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 877bf601f2..57e00a5d3a 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -540,4 +540,33 @@ VIDEO; ); $this->assertEquals( $expected, $filetype ); } + + /** + * @ticket 22768 + */ + public function test_media_handle_upload_sets_post_excerpt() { + $iptc_file = DIR_TESTDATA . '/images/test-image-iptc.jpg'; + + // Make a copy of this file as it gets moved during the file upload + $tmp_name = wp_tempnam( $iptc_file ); + + copy( $iptc_file, $tmp_name ); + + $_FILES['upload'] = array( + 'tmp_name' => $tmp_name, + 'name' => 'test-image-iptc.jpg', + 'type' => 'image/jpeg', + 'error' => 0, + 'size' => filesize( $iptc_file ) + ); + + $post_id = media_handle_upload( 'upload', 0, array(), array( 'action' => 'test_iptc_upload', 'test_form' => false ) ); + + unset( $_FILES['upload'] ); + + $post = get_post( $post_id ); + + $this->assertEquals( 'This is a comment. / Это комментарий. / Βλέπετε ένα σχόλιο.', $post->post_excerpt ); + } + }