Skip to content

Commit 66bd97c

Browse files
Copilotswissspidy
andcommitted
Fix flip_rotate_image: guard metadata regeneration behind successful save, validate non-empty metadata
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent c4c14ed commit 66bd97c

1 file changed

Lines changed: 20 additions & 16 deletions

File tree

src/Media_Command.php

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,7 +1486,7 @@ private function process_orientation_fix( $id, $progress, &$successes, &$errors,
14861486
if ( $orientation > 1 ) {
14871487
if ( ! $dry_run ) {
14881488
WP_CLI::log( "{$progress} Fixing orientation for {$att_desc}." );
1489-
if ( false !== $this->flip_rotate_image( $id, is_array( $metadata ) ? $metadata : [], $image_meta, $full_size_path ) ) {
1489+
if ( false !== $this->flip_rotate_image( $id, $image_meta, $full_size_path ) ) {
14901490
++$successes;
14911491
} else {
14921492
++$errors;
@@ -1505,13 +1505,12 @@ private function process_orientation_fix( $id, $progress, &$successes, &$errors,
15051505
* Perform image rotate operations on the image.
15061506
*
15071507
* @param int $id Attachment Id.
1508-
* @param array $metadata Attachment Metadata.
15091508
* @param array $image_meta `image_meta` information for the attachment.
15101509
* @param string $full_size_path Path to original image.
15111510
*
15121511
* @return bool Whether the image rotation operation succeeded.
15131512
*/
1514-
private function flip_rotate_image( $id, $metadata, $image_meta, $full_size_path ) {
1513+
private function flip_rotate_image( $id, $image_meta, $full_size_path ) {
15151514
$editor = wp_get_image_editor( $full_size_path );
15161515

15171516
if ( ! is_wp_error( $editor ) ) {
@@ -1527,26 +1526,31 @@ private function flip_rotate_image( $id, $metadata, $image_meta, $full_size_path
15271526
$editor->flip( $operations['flip'][0], $operations['flip'][1] );
15281527
}
15291528

1530-
// Save the image and generate metadata.
1531-
$saved = $editor->save( $full_size_path );
1529+
$saved = $editor->save( $full_size_path );
1530+
1531+
if ( is_wp_error( $saved ) ) {
1532+
return false;
1533+
}
1534+
1535+
// Regenerate attachment metadata after the corrected image is saved.
15321536
$metadata = wp_generate_attachment_metadata( $id, $full_size_path );
15331537

1534-
// After a successful save, normalize the stored orientation to prevent
1535-
// re-detection on subsequent runs. WP_Image_Editor_Imagick::flip() does not
1536-
// reset the EXIF orientation tag, so the file may still report a non-normal
1537-
// orientation even though the pixels have been corrected. Because $editor->save()
1538-
// succeeded, the transformation was applied to the image; forcing orientation to 0
1539-
// in the stored metadata ensures the next run reports "No orientation fix required".
1540-
if ( ! is_wp_error( $saved ) && isset( $metadata['image_meta']['orientation'] ) ) {
1538+
if ( empty( $metadata ) ) {
1539+
return false;
1540+
}
1541+
1542+
// Normalize the stored orientation to prevent re-detection on subsequent runs.
1543+
// WP_Image_Editor_Imagick::flip() does not reset the EXIF orientation tag in the
1544+
// file, so the file may still report a non-normal orientation even though the pixels
1545+
// have been corrected. Forcing orientation to 0 in the stored metadata ensures the
1546+
// next run reports "No orientation fix required".
1547+
if ( isset( $metadata['image_meta']['orientation'] ) ) {
15411548
$metadata['image_meta']['orientation'] = 0;
15421549
}
15431550

1544-
// Update attachment metadata with newly generated data.
15451551
wp_update_attachment_metadata( $id, $metadata );
15461552

1547-
if ( ! is_wp_error( $saved ) ) {
1548-
return true;
1549-
}
1553+
return true;
15501554
}
15511555

15521556
return false;

0 commit comments

Comments
 (0)