@@ -2122,9 +2122,12 @@ public function alter_attachment_image_src( $image, $attachment_id, $size, $icon
2122
2122
if ( ! $ this ->is_new_offloaded_attachment ( $ attachment_id ) ) {
2123
2123
return $ image ;
2124
2124
}
2125
-
2126
- $ url = get_post ( $ attachment_id );
2127
- $ url = $ url ->guid ;
2125
+ if ( isset ( $ image [0 ] ) ) {
2126
+ $ url = $ image [0 ];
2127
+ } else {
2128
+ $ url = get_post ( $ attachment_id );
2129
+ $ url = $ url ->guid ;
2130
+ }
2128
2131
$ metadata = wp_get_attachment_metadata ( $ attachment_id );
2129
2132
2130
2133
// Use the original size if the requested size is full.
@@ -2136,7 +2139,8 @@ public function alter_attachment_image_src( $image, $attachment_id, $size, $icon
2136
2139
'width ' => $ metadata ['width ' ],
2137
2140
'height ' => $ metadata ['height ' ],
2138
2141
'attachment_id ' => $ attachment_id ,
2139
- ]
2142
+ ],
2143
+ $ metadata
2140
2144
);
2141
2145
2142
2146
return [
@@ -2147,7 +2151,7 @@ public function alter_attachment_image_src( $image, $attachment_id, $size, $icon
2147
2151
];
2148
2152
}
2149
2153
2150
- if ( wp_attachment_is ( 'video ' , $ attachment_id ) && doing_action ( 'wp_insert_post_data ' ) ) {
2154
+ if ( doing_action ( 'wp_insert_post_data ' ) && wp_attachment_is ( 'video ' , $ attachment_id ) ) {
2151
2155
return $ image ;
2152
2156
}
2153
2157
$ sizes = $ this ->size_to_dimension ( $ size , $ metadata );
@@ -2159,7 +2163,8 @@ public function alter_attachment_image_src( $image, $attachment_id, $size, $icon
2159
2163
'height ' => $ sizes ['height ' ],
2160
2164
'resize ' => $ sizes ['resize ' ] ?? [],
2161
2165
'attachment_id ' => $ attachment_id ,
2162
- ]
2166
+ ],
2167
+ $ metadata
2163
2168
);
2164
2169
2165
2170
return [
@@ -2230,24 +2235,27 @@ public function alter_attachment_metadata( $metadata, $id ) {
2230
2235
/**
2231
2236
* Get offloaded image attachment URL for new offloads.
2232
2237
*
2233
- * @param string $url The initial attachment URL.
2234
- * @param int $attachment_id The attachment ID.
2235
- * @param array $args The additional arguments.
2236
- * - width: The width of the image.
2237
- * - height: The height of the image.
2238
- * - crop: Whether to crop the image.
2239
- *
2238
+ * @param string $url The initial attachment URL.
2239
+ * @param int $attachment_id The attachment ID.
2240
+ * @param array $args The additional arguments.
2241
+ * - width: The width of the image.
2242
+ * - height: The height of the image.
2243
+ * - crop: Whether to crop the image.
2244
+ * @param array|null $attachment_metadata The attachment metadata.
2240
2245
* @return string
2241
2246
*/
2242
- private function get_new_offloaded_attachment_url ( $ url , $ attachment_id , $ args = [] ) {
2247
+ private function get_new_offloaded_attachment_url ( $ url , $ attachment_id , $ args = [], $ attachment_metadata = null ) {
2243
2248
$ process_flag = self ::KEYS ['not_processed_flag ' ] . $ attachment_id ;
2244
2249
2245
2250
// Image might have already passed through this filter.
2246
2251
if ( strpos ( $ url , $ process_flag ) !== false ) {
2247
2252
return $ url ;
2248
2253
}
2249
-
2250
- $ meta = wp_get_attachment_metadata ( $ attachment_id );
2254
+ if ( $ attachment_metadata === null ) {
2255
+ $ meta = wp_get_attachment_metadata ( $ attachment_id );
2256
+ } else {
2257
+ $ meta = $ attachment_metadata ;
2258
+ }
2251
2259
if ( ! isset ( $ meta ['file ' ] ) ) {
2252
2260
return $ url ;
2253
2261
}
0 commit comments