Skip to content

Commit 2c81c2b

Browse files
authored
Merge pull request #915 from Codeinwp/fix/imp-performance
refactor: improve media offload URL retrieval and enhance dimension c…
2 parents f7eeecc + fd9cead commit 2c81c2b

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed

inc/media_offload.php

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2122,9 +2122,12 @@ public function alter_attachment_image_src( $image, $attachment_id, $size, $icon
21222122
if ( ! $this->is_new_offloaded_attachment( $attachment_id ) ) {
21232123
return $image;
21242124
}
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+
}
21282131
$metadata = wp_get_attachment_metadata( $attachment_id );
21292132

21302133
// 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
21362139
'width' => $metadata['width'],
21372140
'height' => $metadata['height'],
21382141
'attachment_id' => $attachment_id,
2139-
]
2142+
],
2143+
$metadata
21402144
);
21412145

21422146
return [
@@ -2147,7 +2151,7 @@ public function alter_attachment_image_src( $image, $attachment_id, $size, $icon
21472151
];
21482152
}
21492153

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 ) ) {
21512155
return $image;
21522156
}
21532157
$sizes = $this->size_to_dimension( $size, $metadata );
@@ -2159,7 +2163,8 @@ public function alter_attachment_image_src( $image, $attachment_id, $size, $icon
21592163
'height' => $sizes['height'],
21602164
'resize' => $sizes['resize'] ?? [],
21612165
'attachment_id' => $attachment_id,
2162-
]
2166+
],
2167+
$metadata
21632168
);
21642169

21652170
return [
@@ -2230,24 +2235,27 @@ public function alter_attachment_metadata( $metadata, $id ) {
22302235
/**
22312236
* Get offloaded image attachment URL for new offloads.
22322237
*
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.
22402245
* @return string
22412246
*/
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 ) {
22432248
$process_flag = self::KEYS['not_processed_flag'] . $attachment_id;
22442249

22452250
// Image might have already passed through this filter.
22462251
if ( strpos( $url, $process_flag ) !== false ) {
22472252
return $url;
22482253
}
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+
}
22512259
if ( ! isset( $meta['file'] ) ) {
22522260
return $url;
22532261
}

inc/traits/normalizer.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
*/
1212
trait Optml_Normalizer {
1313

14+
/**
15+
* Static cache for dimension calculations
16+
*
17+
* @var array
18+
*/
19+
private static $dimension_cache = [];
20+
1421
/**
1522
* Normalize value to boolean.
1623
*
@@ -205,6 +212,10 @@ public function size_to_dimension( $size, $image_meta ) {
205212
if ( ! $width || ! $height ) {
206213
break;
207214
}
215+
$cache_key = 'a' . $width . '_' . $height . '_' . $sizes['width'] . '_' . $sizes['height'];
216+
if ( isset( self::$dimension_cache[ $cache_key ] ) ) {
217+
return self::$dimension_cache[ $cache_key ];
218+
}
208219
$image_resized = image_resize_dimensions( $sizes['width'], $sizes['height'], $width, $height );
209220
if ( $image_resized ) {
210221
$width = $image_resized[6];
@@ -215,8 +226,13 @@ public function size_to_dimension( $size, $image_meta ) {
215226
}
216227
list( $sizes['width'], $sizes['height'] ) = image_constrain_size_for_editor( $width, $height, $size );
217228

229+
self::$dimension_cache[ $cache_key ] = $sizes;
218230
break;
219231
case 'full' !== $size && isset( $image_args[ $size ] ):
232+
$cache_key = 'b' . $size . '_' . $sizes['width'] . '_' . $sizes['height'];
233+
if ( isset( self::$dimension_cache[ $cache_key ] ) ) {
234+
return self::$dimension_cache[ $cache_key ];
235+
}
220236
$image_resized = image_resize_dimensions( $sizes['width'], $sizes['height'], $image_args[ $size ]['width'], $image_args[ $size ]['height'], $image_args[ $size ]['crop'] );
221237

222238
if ( $image_resized ) { // This could be false when the requested image size is larger than the full-size image.
@@ -230,7 +246,7 @@ public function size_to_dimension( $size, $image_meta ) {
230246
list( $sizes['width'], $sizes['height'] ) = image_constrain_size_for_editor( $sizes['width'], $sizes['height'], $size, 'display' );
231247

232248
$sizes['resize'] = $this->to_optml_crop( $image_args[ $size ]['crop'] );
233-
249+
self::$dimension_cache[ $cache_key ] = $sizes;
234250
break;
235251
}
236252
return $sizes;

0 commit comments

Comments
 (0)