Skip to content

Commit 2d929ba

Browse files
committed
clean up code
1 parent eb1afa9 commit 2d929ba

File tree

6 files changed

+28
-61
lines changed

6 files changed

+28
-61
lines changed

inc/app_replacer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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 & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -203,16 +203,14 @@ public function alter_attachment_image_src( $image, $attachment_id, $size, $icon
203203
return $image;
204204
}
205205
$image_url = wp_get_attachment_url( $attachment_id );
206-
$incoming_size = $this->parse_dimension_from_optimized_url( $image_url );
207-
$width = $incoming_size[0];
208-
$height = $incoming_size[1];
206+
list($width, $height) = $this->parse_dimension_from_optimized_url( $image_url );
209207

210208
// Skip resize in single attachment view on backend.
211209
if ( $this->is_attachment_edit_page( $attachment_id ) ) {
212210
return [
213211
$image_url,
214-
$width,
215-
$height,
212+
$width === 'auto' ? false : $width,
213+
$height === 'auto' ? false : $height,
216214
false,
217215
];
218216
}
@@ -656,53 +654,6 @@ public function alter_attachment_for_js( $response, $attachment, $meta ) {
656654
return $response;
657655
}
658656

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

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: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@ public function init() {
6565
}
6666

6767
/**
68-
* Get the dimensions of the image based on the optimized url fro Optimole.
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.
6974
*
7075
* @param mixed $dimensions The dimensions of the image.
7176
* @param mixed $image_src The source of the image.
@@ -74,9 +79,11 @@ public function init() {
7479
*/
7580
public function filter_image_src_get_dimensions( $dimensions, $image_src, $image_meta, $attachment_id ) {
7681

77-
$incoming_size = $this->parse_dimension_from_optimized_url( $image_src );
78-
list($width, $height) = $incoming_size;
82+
list($width, $height) = $this->parse_dimension_from_optimized_url( $image_src );
7983

84+
if ( false === $width || false === $height ) {
85+
return $dimensions;
86+
}
8087
$sizes = Optml_App_Replacer::image_sizes();
8188

8289
// If this is an image size. Return its dimensions.

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];

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)