Skip to content

Commit 965f9c1

Browse files
authored
release: fixes
- Improved type annotations and added runtime type checks in several methods to prevent errors when non-array values are encountered. - Fixed an edge case where background lazyload selectors could be accessed before settings were initialized. - Improved logic to better extract the original URL from Optimole offloaded images, including offloading patterns and custom CDN domains.
2 parents dc01f67 + 650f757 commit 965f9c1

13 files changed

Lines changed: 244 additions & 42 deletions

.github/workflows/test-php.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
strategy:
2929
fail-fast: false
3030
matrix:
31-
php-versions: [ '7.4', '8.0', '8.1', '8.2', '8.3' ]
31+
php-versions: [ '8.0', '8.1', '8.2', '8.3' ]
3232
services:
3333
database:
3434
image: mysql:latest
@@ -41,7 +41,7 @@ jobs:
4141
- name: Setup PHP version
4242
uses: shivammathur/setup-php@v2
4343
with:
44-
php-version: ${{ matrix.php-version }}
44+
php-version: ${{ matrix.php-versions }}
4545
extensions: simplexml, mysql
4646
tools: phpunit-polyfills:1.1
4747
- name: Checkout source code

composer.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inc/admin.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,11 +575,14 @@ public function inline_bootstrap_script() {
575575
/**
576576
* Add settings links in the plugin listing page.
577577
*
578-
* @param string[] $links Old plugin links.
578+
* @param string[]|mixed $links Old plugin links.
579579
*
580-
* @return string[] Altered links.
580+
* @return string[]|mixed Altered links.
581581
*/
582582
public function add_action_links( $links ) {
583+
if ( ! is_array( $links ) ) {
584+
return $links;
585+
}
583586
return array_merge(
584587
$links,
585588
[

inc/app_replacer.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -654,21 +654,24 @@ public function url_has_dam_flag( $url ) {
654654
/**
655655
* Get the optimized image url for the image url.
656656
*
657-
* @param string $url The image URL.
658-
* @param mixed $width The image width.
659-
* @param mixed $height The image height.
660-
* @param array $resize The resize properties.
657+
* @param string $url The image URL.
658+
* @param mixed $width The image width.
659+
* @param mixed $height The image height.
660+
* @param array<string, mixed>|mixed $resize The resize properties.
661661
*
662662
* @return string
663663
*/
664664
protected function get_optimized_image_url( $url, $width, $height, $resize = [] ) {
665665
$width = is_int( $width ) ? $width : 'auto';
666666
$height = is_int( $height ) ? $height : 'auto';
667+
// If the image is already using Optimole URL, we extract the source to rebuild it.
668+
$url = $this->get_unoptimized_url( $url );
669+
667670
$optimized_image = Optimole::image( $url, $this->settings->get( 'cache_buster' ) )
668671
->width( $width )
669672
->height( $height );
670673

671-
if ( ! empty( $resize['type'] ) ) {
674+
if ( is_array( $resize ) && ! empty( $resize['type'] ) ) {
672675
$optimized_image->resize( $resize['type'], $resize['gravity'] ?? Position::CENTER, $resize['enlarge'] ?? false );
673676

674677
}

inc/lazyload_replacer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ public static function get_background_lazyload_selectors() {
115115
return self::$background_lazyload_selectors;
116116
}
117117

118+
if ( self::instance()->settings === null ) {
119+
self::$background_lazyload_selectors = [];
120+
121+
return self::$background_lazyload_selectors;
122+
}
118123
if ( self::instance()->settings->get( 'bg_replacer' ) === 'disabled' ) {
119124
self::$background_lazyload_selectors = [];
120125
return self::$background_lazyload_selectors;

inc/tag_replacer.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -809,15 +809,18 @@ public function change_url_for_size( $original_url, $width, $height, $dpr = 1 )
809809
/**
810810
* Replace image URLs in the srcset attributes and in case there is a resize in action, also replace the sizes.
811811
*
812-
* @param array<int, array{url: string, descriptor: string, value: int}> $sources Array of image sources.
813-
* @param array{0: int, 1: int}|int[] $size_array Array of width and height values in pixels (in that order).
814-
* @param string $image_src The 'src' of the image.
815-
* @param array<string, mixed> $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'.
816-
* @param int $attachment_id Image attachment ID or 0.
812+
* @param array<int, array{url: string, descriptor: string, value: int}>|mixed $sources Array of image sources.
813+
* @param array{0: int, 1: int}|int[] $size_array Array of width and height values in pixels (in that order).
814+
* @param string $image_src The 'src' of the image.
815+
* @param array<string, mixed> $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'.
816+
* @param int $attachment_id Image attachment ID or 0.
817817
*
818-
* @return array
818+
* @return array|mixed
819819
*/
820820
public function filter_srcset_attr( $sources = [], $size_array = [], $image_src = '', $image_meta = [], $attachment_id = 0 ) {
821+
if ( ! is_array( $sources ) ) {
822+
return $sources;
823+
}
821824
if ( Optml_Media_Offload::is_uploaded_image( $image_src ) ) {
822825
return $sources;
823826
}

inc/traits/dam_offload_utils.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33
trait Optml_Dam_Offload_Utils {
44
use Optml_Normalizer;
55

6+
/**
7+
* Check if this contains the DAM flag.
8+
*
9+
* @param string $url The URL to check.
10+
*
11+
* @return bool
12+
*/
13+
private function is_dam_url( $url ) {
14+
return strpos( $url, Optml_Dam::URL_DAM_FLAG ) !== false;
15+
}
616
/**
717
* Checks that the attachment is a DAM image.
818
*
@@ -239,6 +249,31 @@ private function is_completed_offload( $id ) {
239249

240250
return false;
241251
}
252+
/**
253+
* Get the attachment ID from optimole ID.
254+
*
255+
* @param string $optimole_id The optimole ID.
256+
*
257+
* @return int
258+
*/
259+
private function get_attachement_id_from_optimole_id( string $optimole_id ): int {
260+
global $wpdb;
261+
262+
$id = $wpdb->get_var(
263+
$wpdb->prepare(
264+
"
265+
SELECT post_id
266+
FROM {$wpdb->postmeta}
267+
WHERE meta_key = %s
268+
AND meta_value = %s
269+
LIMIT 1
270+
",
271+
Optml_Dam::OM_DAM_IMPORTED_FLAG,
272+
$optimole_id
273+
)
274+
);
275+
return empty( $id ) ? 0 : (int) $id;
276+
}
242277
/**
243278
* Get the attachment ID from URL.
244279
*
@@ -253,6 +288,19 @@ private function attachment_url_to_post_id( $input_url ) {
253288
return (int) $cached;
254289
}
255290

291+
if ( Optml_Media_Offload::is_uploaded_image( $input_url ) ) {
292+
// The DAM are stored as attachments of format /id:<attachment_id>/<original_url>
293+
$pattern = '#/' . Optml_Media_Offload::KEYS['uploaded_flag'] . '([^/]+)#';
294+
if ( preg_match( $pattern, $input_url, $m ) ) {
295+
$attachment_id = $this->get_attachement_id_from_optimole_id( $m[1] );
296+
if ( $attachment_id !== 0 ) {
297+
Optml_Attachment_Cache::set_cached_attachment_id( $input_url, $attachment_id );
298+
299+
return $attachment_id;
300+
}
301+
}
302+
}
303+
256304
$url = $this->strip_image_size( $input_url );
257305

258306
$attachment_id = attachment_url_to_postid( $url );

inc/traits/normalizer.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ public function get_unoptimized_url( $url ) {
5353
}
5454
// If the url is an uploaded image, return the url
5555
if ( Optml_Media_Offload::is_uploaded_image( $url ) ) {
56+
$pattern = '#/id:([^/]+)/((?:https?|http)://\S+)#';
57+
if ( preg_match( $pattern, $url, $matches ) ) {
58+
$url = $matches[0];
59+
}
5660
return $url;
5761
}
5862

inc/url_replacer.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -409,16 +409,6 @@ private function get_dam_url( Image $image ) {
409409
return $url;
410410
}
411411

412-
/**
413-
* Check if this contains the DAM flag.
414-
*
415-
* @param string $url The URL to check.
416-
*
417-
* @return bool
418-
*/
419-
private function is_dam_url( $url ) {
420-
return strpos( $url, Optml_Dam::URL_DAM_FLAG ) !== false;
421-
}
422412

423413
/**
424414
* Check if the URL is offloaded.

phpstan-baseline.neon

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -516,12 +516,6 @@ parameters:
516516
count: 1
517517
path: inc/app_replacer.php
518518

519-
-
520-
message: '#^Method Optml_App_Replacer\:\:get_optimized_image_url\(\) has parameter \$resize with no value type specified in iterable type array\.$#'
521-
identifier: missingType.iterableValue
522-
count: 1
523-
path: inc/app_replacer.php
524-
525519
-
526520
message: '#^Method Optml_App_Replacer\:\:get_upload_resource\(\) return type has no value type specified in iterable type array\.$#'
527521
identifier: missingType.iterableValue
@@ -2790,12 +2784,6 @@ parameters:
27902784
count: 1
27912785
path: inc/tag_replacer.php
27922786

2793-
-
2794-
message: '#^Method Optml_Tag_Replacer\:\:filter_srcset_attr\(\) return type has no value type specified in iterable type array\.$#'
2795-
identifier: missingType.iterableValue
2796-
count: 1
2797-
path: inc/tag_replacer.php
2798-
27992787
-
28002788
message: '#^Method Optml_Tag_Replacer\:\:init\(\) has no return type specified\.$#'
28012789
identifier: missingType.return

0 commit comments

Comments
 (0)