From 1fe91fa9d041922222ce93e5fbedbedcd049a515 Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Tue, 3 Mar 2026 17:13:58 +0530 Subject: [PATCH 1/2] fix: save featured image on post save --- inc/dam.php | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/inc/dam.php b/inc/dam.php index 240b3650..b9574940 100644 --- a/inc/dam.php +++ b/inc/dam.php @@ -112,8 +112,8 @@ public function insert_attachments( $images ) { $existing = $this->check_existing_attachments( $images ); foreach ( $images as $image ) { - if ( ! isset( $image['isEdit'] ) && array_key_exists( $image['meta']['resourceS3'], $existing ) ) { - $ids[] = $existing[ $image['meta']['resourceS3'] ]; + if ( ! isset( $image['isEdit'] ) && array_key_exists( $image['resourceS3'], $existing ) ) { + $ids[] = $existing[ $image['resourceS3'] ]; continue; } @@ -143,7 +143,7 @@ private function insert_attachment( $image ) { $args = [ 'post_title' => $name, 'post_type' => 'attachment', - 'post_mime_type' => $image['meta']['mimeType'], + 'post_mime_type' => $image['mimeType'], 'guid' => $image['url'], ]; @@ -153,7 +153,7 @@ private function insert_attachment( $image ) { return $id; } - update_post_meta( $id, self::OM_DAM_IMPORTED_FLAG, $image['meta']['resourceS3'] ); + update_post_meta( $id, self::OM_DAM_IMPORTED_FLAG, $image['resourceS3'] ); if ( isset( $image['isEdit'] ) ) { update_post_meta( $id, self::IS_EDIT_FLAG, true ); @@ -161,16 +161,16 @@ private function insert_attachment( $image ) { $metadata = []; - $metadata['file'] = '/id:' . $image['meta']['resourceS3'] . '/' . get_home_url() . '/' . $filename; - $metadata['mime-type'] = $image['meta']['mimeType']; + $metadata['file'] = '/id:' . $image['resourceS3'] . '/' . get_home_url() . '/' . $filename; + $metadata['mime-type'] = $image['mimeType']; - if ( isset( $image['meta']['filesize'] ) ) { - $metadata['filesize'] = $image['meta']['fileSize']; + if ( isset( $image['filesize'] ) ) { + $metadata['filesize'] = $image['fileSize']; } - if ( isset( $image['meta']['originalWidth'] ) && isset( $image['meta']['originalHeight'] ) ) { - $metadata['width'] = $image['meta']['originalWidth']; - $metadata['height'] = $image['meta']['originalHeight']; + if ( isset( $image['originalWidth'] ) && isset( $image['originalHeight'] ) ) { + $metadata['width'] = $image['originalWidth']; + $metadata['height'] = $image['originalHeight']; } wp_update_attachment_metadata( $id, $metadata ); @@ -197,7 +197,6 @@ private function insert_attachment( $image ) { * @return array $image. */ public function alter_attachment_image_src( $image, $attachment_id, $size, $icon ) { - // Skip if not DAM image. if ( ! $this->is_dam_imported_image( $attachment_id ) ) { return $image; @@ -287,7 +286,7 @@ private function check_existing_attachments( $images ) { $remaining = array_filter( $images, function ( $image ) use ( $already_imported ) { - return ! array_key_exists( $image['meta']['resourceS3'], $already_imported ); + return ! array_key_exists( $image['resourceS3'], $already_imported ); } ); @@ -314,7 +313,7 @@ private function get_dam_imported_attachments( $images ) { $s3_ids = []; foreach ( $images as $image ) { - $s3_ids[] = esc_sql( strval( $image['meta']['resourceS3'] ) ); + $s3_ids[] = esc_sql( strval( $image['resourceS3'] ) ); } $meta_values_str = "'" . join( "', '", $s3_ids ) . "'"; @@ -364,7 +363,7 @@ private function get_offloaded_attachments( $images ) { $map = []; foreach ( $images as $image ) { - $like = '%id:' . $image['meta']['resourceS3'] . '%'; + $like = '%id:' . $image['resourceS3'] . '%'; $found_attachments = $wpdb->get_results( $wpdb->prepare( @@ -378,7 +377,7 @@ private function get_offloaded_attachments( $images ) { return []; } - $map[ $image['meta']['resourceS3'] ] = (int) $found_attachments[0]->post_id; + $map[ $image['resourceS3'] ] = (int) $found_attachments[0]->post_id; } return $map; From 4151d8bb74e35db3a21486e7796e9f986a5629a0 Mon Sep 17 00:00:00 2001 From: girishpanchal30 Date: Tue, 3 Mar 2026 18:20:05 +0530 Subject: [PATCH 2/2] fix: phpunit --- inc/dam.php | 4 +- tests/media_rename/test-attachment-model.php | 21 +++---- tests/test-dam.php | 65 +++++++++----------- 3 files changed, 39 insertions(+), 51 deletions(-) diff --git a/inc/dam.php b/inc/dam.php index b9574940..4791e0b1 100644 --- a/inc/dam.php +++ b/inc/dam.php @@ -164,8 +164,8 @@ private function insert_attachment( $image ) { $metadata['file'] = '/id:' . $image['resourceS3'] . '/' . get_home_url() . '/' . $filename; $metadata['mime-type'] = $image['mimeType']; - if ( isset( $image['filesize'] ) ) { - $metadata['filesize'] = $image['fileSize']; + if ( isset( $image['fileSize'] ) ) { + $metadata['fileSize'] = $image['fileSize']; } if ( isset( $image['originalWidth'] ) && isset( $image['originalHeight'] ) ) { diff --git a/tests/media_rename/test-attachment-model.php b/tests/media_rename/test-attachment-model.php index 85277f26..3a791ebe 100644 --- a/tests/media_rename/test-attachment-model.php +++ b/tests/media_rename/test-attachment-model.php @@ -17,18 +17,15 @@ class Test_Attachment_Model extends WP_UnitTestCase { const MOCK_REMOTE_ATTACHMENT = [ 'url' => 'https://cloudUrlTest.test/w:auto/h:auto/q:auto/id:b1b12ee03bf3945d9d9bb963ce79cd4f/https://test-site.test/9.jpg', - 'meta' => - [ - 'originalHeight' => 1800, - 'originalWidth' => 1200, - 'updateTime' => 1688553629048, - 'resourceS3' => 'randomHashForImage1', - 'mimeType' => 'image/jpeg', - 'userKey' => 'mlckcuxuuuyb', - 'fileSize' => 171114, - 'originURL' => 'https://test-site.test/wp-content/uploads/2023/07/9.jpg', - 'domain_hash' => 'dWwtcG9sZWNhdC15dWtpLmluc3Rhd3AueHl6', - ], + 'originalHeight' => 1800, + 'originalWidth' => 1200, + 'updateTime' => 1688553629048, + 'resourceS3' => 'randomHashForImage1', + 'mimeType' => 'image/jpeg', + 'userKey' => 'mlckcuxuuuyb', + 'fileSize' => 171114, + 'originURL' => 'https://test-site.test/wp-content/uploads/2023/07/9.jpg', + 'domain_hash' => 'dWwtcG9sZWNhdC15dWtpLmluc3Rhd3AueHl6', ]; public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { diff --git a/tests/test-dam.php b/tests/test-dam.php index 8836a568..73a18ad1 100644 --- a/tests/test-dam.php +++ b/tests/test-dam.php @@ -30,33 +30,27 @@ class Test_Dam extends WP_UnitTestCase { const MOCK_ATTACHMENTS = [ [ 'url' => 'https://cloudUrlTest.test/w:auto/h:auto/q:auto/id:b1b12ee03bf3945d9d9bb963ce79cd4f/https://test-site.test/9.jpg', - 'meta' => - [ - 'originalHeight' => 1800, - 'originalWidth' => 1200, - 'updateTime' => 1688553629048, - 'resourceS3' => 'randomHashForImage1', - 'mimeType' => 'image/jpeg', - 'userKey' => 'mlckcuxuuuyb', - 'fileSize' => 171114, - 'originURL' => 'https://test-site.test/wp-content/uploads/2023/07/9.jpg', - 'domain_hash' => 'dWwtcG9sZWNhdC15dWtpLmluc3Rhd3AueHl6', - ], + 'originalHeight' => 1800, + 'originalWidth' => 1200, + 'updateTime' => 1688553629048, + 'resourceS3' => 'randomHashForImage1', + 'mimeType' => 'image/jpeg', + 'userKey' => 'mlckcuxuuuyb', + 'fileSize' => 171114, + 'originURL' => 'https://test-site.test/wp-content/uploads/2023/07/9.jpg', + 'domain_hash' => 'dWwtcG9sZWNhdC15dWtpLmluc3Rhd3AueHl6', ], [ 'url' => 'https://cloudUrlTest.test/w:auto/h:auto/q:auto/id:b1b12ee03bf3945d9d9bb963ce79cd4f/https://test-site.test/10.jpg', - 'meta' => - [ - 'originalHeight' => 1800, - 'originalWidth' => 1200, - 'updateTime' => 1688553629048, - 'resourceS3' => 'randomHashForImage2', - 'mimeType' => 'image/jpeg', - 'userKey' => 'mlckcuxuuuyb', - 'fileSize' => 171114, - 'originURL' => 'https://test-site.test/wp-content/uploads/2023/07/10.jpg', - 'domain_hash' => 'dWwtcG9sZWNhdC15dWtpLmluc3Rhd3AueHl6', - ], + 'originalHeight' => 1800, + 'originalWidth' => 1200, + 'updateTime' => 1688553629048, + 'resourceS3' => 'randomHashForImage2', + 'mimeType' => 'image/jpeg', + 'userKey' => 'mlckcuxuuuyb', + 'fileSize' => 171114, + 'originURL' => 'https://test-site.test/wp-content/uploads/2023/07/10.jpg', + 'domain_hash' => 'dWwtcG9sZWNhdC15dWtpLmluc3Rhd3AueHl6', ], ]; @@ -144,7 +138,7 @@ public function test_insert_attachments() { $attachment = get_post( $id ); $this->assertEquals( self::MOCK_ATTACHMENTS[ $index ]['url'], $attachment->guid ); - $this->assertEquals( self::MOCK_ATTACHMENTS[ $index ]['meta']['mimeType'], $attachment->post_mime_type ); + $this->assertEquals( self::MOCK_ATTACHMENTS[ $index ]['mimeType'], $attachment->post_mime_type ); } } @@ -165,18 +159,15 @@ public function test_insert_duplicates() { [ [ 'url' => 'https://cloudUrlTest.test/w:auto/h:auto/q:auto/id:b1b12ee03bf3945d9d9bb963ce79cd4f/https://test-site.test/11.jpg', - 'meta' => - [ - 'originalHeight' => 1800, - 'originalWidth' => 1200, - 'updateTime' => 1688553629048, - 'resourceS3' => 'randomHashForImage3', - 'mimeType' => 'image/jpeg', - 'userKey' => 'mlckcuxuuuyb', - 'fileSize' => 171114, - 'originURL' => 'https://test-site.test/wp-content/uploads/2023/07/11.jpg', - 'domain_hash' => 'dWwtcG9sZWNhdC15dWtpLmluc3Rhd3AueHl6', - ], + 'originalHeight' => 1800, + 'originalWidth' => 1200, + 'updateTime' => 1688553629048, + 'resourceS3' => 'randomHashForImage3', + 'mimeType' => 'image/jpeg', + 'userKey' => 'mlckcuxuuuyb', + 'fileSize' => 171114, + 'originURL' => 'https://test-site.test/wp-content/uploads/2023/07/11.jpg', + 'domain_hash' => 'dWwtcG9sZWNhdC15dWtpLmluc3Rhd3AueHl6', ], ], );