Skip to content

Commit c129189

Browse files
authored
release: fixes
Fix extraneous quotation mark in image URL when changing fetchpriority. Fix resize behavior for certain image sizes that were improperly resized.
2 parents 62f639d + 45fa436 commit c129189

File tree

8 files changed

+90
-71
lines changed

8 files changed

+90
-71
lines changed

inc/app_replacer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ public static function possible_data_ignore_flags() {
221221
*
222222
* @return array Size mapping.
223223
*/
224-
protected static function size_to_crop() {
224+
public static function size_to_crop() {
225225
if ( ! empty( self::$size_to_crop ) ) {
226226
return self::$size_to_crop;
227227
}
@@ -614,7 +614,7 @@ protected function parse_dimensions_from_filename( $src ) {
614614
}
615615
} else {
616616
$optimized_args = $this->parse_dimension_from_optimized_url( $src );
617-
if ( $optimized_args[0] !== 'auto' || $optimized_args[1] !== 'auto' ) {
617+
if ( $optimized_args[0] !== false || $optimized_args[1] !== false ) {
618618
return [
619619
$optimized_args[0] !== 'auto' ? (int) $optimized_args[0] : false,
620620
$optimized_args[1] !== 'auto' ? (int) $optimized_args[1] : false,

inc/dam.php

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ public function __construct() {
6565
add_filter( 'wp_get_attachment_metadata', [ $this, 'alter_attachment_metadata' ], 10, 2 );
6666
add_filter( 'image_downsize', [ $this, 'catch_downsize' ], 10, 3 );
6767
add_filter( 'wp_prepare_attachment_for_js', [ $this, 'alter_attachment_for_js' ], 10, 3 );
68-
add_filter( 'wp_image_src_get_dimensions', [ $this, 'alter_img_tag_w_h' ], 10, 4 );
6968
add_filter( 'get_attached_file', [ $this, 'alter_attached_file_response' ], 10, 2 );
7069
add_filter( 'wp_calculate_image_srcset', [ $this, 'disable_dam_images_srcset' ], 1, 5 );
7170

@@ -204,16 +203,14 @@ public function alter_attachment_image_src( $image, $attachment_id, $size, $icon
204203
return $image;
205204
}
206205
$image_url = wp_get_attachment_url( $attachment_id );
207-
$incoming_size = $this->parse_dimension_from_optimized_url( $image_url );
208-
$width = $incoming_size[0];
209-
$height = $incoming_size[1];
206+
list($width, $height) = $this->parse_dimension_from_optimized_url( $image_url );
210207

211208
// Skip resize in single attachment view on backend.
212209
if ( $this->is_attachment_edit_page( $attachment_id ) ) {
213210
return [
214211
$image_url,
215-
$width,
216-
$height,
212+
$width === 'auto' ? false : $width,
213+
$height === 'auto' ? false : $height,
217214
false,
218215
];
219216
}
@@ -657,53 +654,6 @@ public function alter_attachment_for_js( $response, $attachment, $meta ) {
657654
return $response;
658655
}
659656

660-
/**
661-
* We have to short-circuit the logic that adds width and height to the img tag.
662-
* It compares the URL basename, and the `file` param for each image.
663-
* This happens for any image that gets its size set non-explicitly
664-
* e.g. an image block with its size set from the sidebar to `thumbnail`).
665-
*
666-
* Optimole has a single basename for all image resizes in its URL.
667-
*
668-
* @param array|false $dimensions Array with first element being the width
669-
* and second element being the height, or
670-
* false if dimensions could not be determined.
671-
* @param string $image_src The image URL (will be Optimole URL).
672-
* @param array $image_meta The image metadata.
673-
* @param int $attachment_id The image attachment ID. Default 0.
674-
*/
675-
public function alter_img_tag_w_h( $dimensions, $image_src, $image_meta, $attachment_id ) {
676-
if ( ! $this->is_dam_imported_image( $attachment_id ) ) {
677-
return $dimensions;
678-
}
679-
680-
// Get the dimensions from the optimized URL.
681-
$incoming_size = $this->parse_dimension_from_optimized_url( $image_src );
682-
$width = $incoming_size[0];
683-
$height = $incoming_size[1];
684-
685-
$sizes = Optml_App_Replacer::image_sizes();
686-
687-
// If this is an image size. Return its dimensions.
688-
foreach ( $sizes as $size => $args ) {
689-
if ( (int) $args['width'] !== (int) $width ) {
690-
continue;
691-
}
692-
693-
if ( (int) $args['height'] !== (int) $height ) {
694-
continue;
695-
}
696-
697-
return [
698-
$args['width'],
699-
$args['height'],
700-
];
701-
}
702-
703-
// Fall-through with the original dimensions.
704-
return $dimensions;
705-
}
706-
707657
/**
708658
* Replace the image size params in DAM URLs inside a string.
709659
*

inc/lazyload_replacer.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,17 @@ public function lazyload_tag_replace( $new_tag, $original_url, $new_url, $optml_
253253
}
254254
// Remove fetchpriority high attribute when the image is lazyloaded
255255
if ( strpos( $new_tag, 'fetchpriority' ) !== false ) {
256-
$new_tag = preg_replace( '/\s+fetchpriority=(high|[\'"][^\'"]*high[^\'"]*)(?=[\'"\s>]|$)/i', '', $new_tag );
256+
$new_tag = str_ireplace(
257+
[
258+
'fetchpriority=high',
259+
'fetchpriority="high"',
260+
"fetchpriority='high'",
261+
'fetchpriority=\"high\"',
262+
"fetchpriority=\'high\'",
263+
],
264+
'',
265+
$new_tag
266+
);
257267
}
258268

259269
if ( self::instance()->settings->get( 'native_lazyload' ) === 'enabled' ) {

inc/media_offload.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2441,6 +2441,9 @@ public function filter_saved_data( $post_data, $postarr, $unsanitized_postarr, $
24412441

24422442
$size = $this->parse_dimension_from_optimized_url( $url );
24432443

2444+
if ( $size[0] === false || $size[1] === false ) {
2445+
continue;
2446+
}
24442447
if ( $size[0] === 'auto' || $size[1] === 'auto' ) {
24452448
continue;
24462449
}

inc/tag_replacer.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,53 @@ public function init() {
5858
add_filter( 'image_downsize', [ $this, 'filter_image_downsize' ], PHP_INT_MAX, 3 );
5959
add_filter( 'wp_calculate_image_srcset', [ $this, 'filter_srcset_attr' ], PHP_INT_MAX - 1, 5 );
6060
add_filter( 'wp_calculate_image_sizes', [ $this, 'filter_sizes_attr' ], 1, 2 );
61+
add_filter( 'wp_image_src_get_dimensions', [ $this, 'filter_image_src_get_dimensions' ], 99, 4 );
6162
if ( $this->settings->get( 'retina_images' ) === 'enabled' ) {
6263
add_filter( 'wp_get_attachment_image_attributes', [ $this, 'filter_attachment_image_attributes' ], 99, 3 );
6364
}
6465
}
66+
67+
/**
68+
* We have to short-circuit the logic that adds width and height to the img tag.
69+
* It compares the URL basename, and the `file` param for each image.
70+
* This happens for any image that gets its size set non-explicitly
71+
* e.g. an image block with its size set from the sidebar to `thumbnail`).
72+
*
73+
* Optimole has a single basename for all image resizes in its URL.
74+
*
75+
* @param mixed $dimensions The dimensions of the image.
76+
* @param mixed $image_src The source of the image.
77+
* @param mixed $image_meta The meta of the image.
78+
* @param mixed $attachment_id The ID of the attachment.
79+
*/
80+
public function filter_image_src_get_dimensions( $dimensions, $image_src, $image_meta, $attachment_id ) {
81+
82+
list($width, $height) = $this->parse_dimension_from_optimized_url( $image_src );
83+
84+
if ( false === $width || false === $height ) {
85+
return $dimensions;
86+
}
87+
$sizes = Optml_App_Replacer::image_sizes();
88+
89+
// If this is an image size. Return its dimensions.
90+
foreach ( $sizes as $size => $args ) {
91+
if ( (int) $args['width'] !== (int) $width ) {
92+
continue;
93+
}
94+
95+
if ( (int) $args['height'] !== (int) $height ) {
96+
continue;
97+
}
98+
99+
return [
100+
$args['width'],
101+
$args['height'],
102+
];
103+
}
104+
105+
// Fall-through with the original dimensions.
106+
return $dimensions;
107+
}
65108
/**
66109
* Filter the attachment image attributes to add the srcset attribute with retina support.
67110
*
@@ -511,7 +554,8 @@ public function filter_image_downsize( $image, $attachment_id, $size ) {
511554
}
512555

513556
$image_meta = wp_get_attachment_metadata( $attachment_id );
514-
$sizes = $this->size_to_dimension( $size, $image_meta );
557+
$sizes = $this->size_to_dimension( $size, $image_meta, $attachment_id );
558+
515559
$image_url = $this->strip_image_size_from_url( $image_url );
516560

517561
$new_url = apply_filters( 'optml_content_url', $image_url, $sizes );

inc/traits/dam_offload_utils.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ private function get_all_image_sizes() {
9191
*/
9292
private function parse_dimension_from_optimized_url( $url ) {
9393
$catch = [];
94-
$height = 'auto';
95-
$width = 'auto';
94+
$height = false;
95+
$width = false;
9696
preg_match( '/\/w:(.*)\/h:(.*)\/q:/', $url, $catch );
9797
if ( isset( $catch[1] ) && isset( $catch[2] ) ) {
9898
$width = $catch[1];

inc/traits/normalizer.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -195,16 +195,18 @@ public function to_bound_integer( $value, $min, $max ) {
195195
*
196196
* @param mixed $size The size of the image. Can be an array of width and height, a predefined size, or 'full'.
197197
* @param array $image_meta Metadata of the image, including width and height.
198+
* @param int $attachment_id The ID of the attachment.
198199
*
199200
* @return array The dimensions of the image, including width, height, and optional resize parameters.
200201
*/
201-
public function size_to_dimension( $size, $image_meta ) {
202+
public function size_to_dimension( $size, $image_meta, $attachment_id = null ) {
202203
// default size
203204
$sizes = [
204205
'width' => isset( $image_meta['width'] ) ? intval( $image_meta['width'] ) : false,
205206
'height' => isset( $image_meta['height'] ) ? intval( $image_meta['height'] ) : false,
206207
];
207208
$image_args = Optml_App_Replacer::image_sizes();
209+
$sizes2crop = Optml_App_Replacer::size_to_crop();
208210
switch ( $size ) {
209211
case is_array( $size ):
210212
$width = isset( $size[0] ) ? (int) $size[0] : false;
@@ -216,16 +218,20 @@ public function size_to_dimension( $size, $image_meta ) {
216218
if ( isset( self::$dimension_cache[ $cache_key ] ) ) {
217219
return self::$dimension_cache[ $cache_key ];
218220
}
219-
$image_resized = image_resize_dimensions( $sizes['width'], $sizes['height'], $width, $height );
220-
if ( $image_resized ) {
221-
$width = $image_resized[6];
222-
$height = $image_resized[7];
223-
} else {
224-
$width = $image_meta['width'];
225-
$height = $image_meta['height'];
221+
if ( $attachment_id ) {
222+
$intermediate = image_get_intermediate_size( $attachment_id, $size );
223+
if ( $intermediate ) {
224+
$sizes['width'] = $intermediate['width'];
225+
$sizes['height'] = $intermediate['height'];
226+
}
226227
}
227-
list( $sizes['width'], $sizes['height'] ) = image_constrain_size_for_editor( $width, $height, $size );
228228

229+
list( $sizes['width'], $sizes['height'] ) = image_constrain_size_for_editor( $sizes['width'], $sizes['height'], $size );
230+
$resize = apply_filters( 'optml_default_crop', [] );
231+
if ( isset( $sizes2crop[ $sizes['width'] . $sizes['height'] ] ) ) {
232+
$resize = $this->to_optml_crop( $sizes2crop[ $sizes['width'] . $sizes['height'] ] );
233+
}
234+
$sizes['resize'] = $resize;
229235
self::$dimension_cache[ $cache_key ] = $sizes;
230236
break;
231237
case 'full' !== $size && isset( $image_args[ $size ] ):

tests/test-dam.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ class Test_Dam extends WP_UnitTestCase {
2222
*/
2323
private $dam;
2424

25+
/**
26+
* @var Optml_Manager
27+
*/
28+
private $manager;
29+
2530
const MOCK_ATTACHMENTS = [
2631
[
2732
'url' => 'https://cloudUrlTest.test/w:auto/h:auto/q:auto/id:b1b12ee03bf3945d9d9bb963ce79cd4f/https://test-site.test/9.jpg',
@@ -83,6 +88,7 @@ public function setUp(): void {
8388

8489
$plugin = Optml_Main::instance();
8590
$this->dam = $plugin->dam;
91+
$this->manager = $plugin->manager;
8692
$this->settings = $plugin->admin->settings;
8793
$this->settings->update( 'service_data', [
8894
'cdn_key' => 'test123',
@@ -339,7 +345,7 @@ public function test_alter_image_tag_w_h() {
339345
// Set these images as thumbnails.
340346
$args = [ 'width' => 150, 'height' => 150, 'crop' => true ];
341347
$test_url = $this->dam->replace_dam_url_args( $args, $current_attachment['url'] );
342-
$altered_dimensions = $this->dam->alter_img_tag_w_h( $other_dimensions, $test_url, [], $id );
348+
$altered_dimensions = $this->manager->tag_replacer->filter_image_src_get_dimensions( $other_dimensions, $test_url, [], $id );
343349

344350
$this->assertEquals( 150, $altered_dimensions[0] );
345351
$this->assertEquals( 150, $altered_dimensions[1] );
@@ -350,7 +356,7 @@ public function test_alter_image_tag_w_h() {
350356
$args = $this->dam->size_to_dimension( $size, wp_get_attachment_metadata($id) );
351357

352358
$test_url = $this->dam->replace_dam_url_args( $args, $current_attachment['url'] );
353-
$altered_dimensions = $this->dam->alter_img_tag_w_h( $other_dimensions, $test_url, [], $id );
359+
$altered_dimensions = $this->manager->tag_replacer->filter_image_src_get_dimensions( $other_dimensions, $test_url, [], $id );
354360

355361
$this->assertEquals( $image_size_args['width'], $altered_dimensions[0] );
356362
$this->assertEquals( $image_size_args['height'], $altered_dimensions[1] );
@@ -364,7 +370,7 @@ public function test_alter_image_tag_w_h() {
364370
];
365371

366372
$test_url = $this->dam->replace_dam_url_args( $args, $current_attachment['url'] );
367-
$altered_dimensions = $this->dam->alter_img_tag_w_h( $other_dimensions, $test_url, [], $id );
373+
$altered_dimensions = $this->manager->tag_replacer->filter_image_src_get_dimensions( $other_dimensions, $test_url, [], $id );
368374

369375
$this->assertEquals( $other_dimensions[0], $altered_dimensions[0] );
370376
$this->assertEquals( $other_dimensions[1], $altered_dimensions[1] );

0 commit comments

Comments
 (0)