Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/test-php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php-versions: [ '7.4', '8.0', '8.1', '8.2', '8.3' ]
php-versions: [ '8.0', '8.1', '8.2', '8.3' ]
services:
database:
image: mysql:latest
Expand All @@ -41,7 +41,7 @@ jobs:
- name: Setup PHP version
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
php-version: ${{ matrix.php-versions }}
extensions: simplexml, mysql
tools: phpunit-polyfills:1.1
- name: Checkout source code
Expand Down
8 changes: 4 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions inc/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -575,11 +575,14 @@ public function inline_bootstrap_script() {
/**
* Add settings links in the plugin listing page.
*
* @param string[] $links Old plugin links.
* @param string[]|mixed $links Old plugin links.
*
* @return string[] Altered links.
* @return string[]|mixed Altered links.
*/
public function add_action_links( $links ) {
if ( ! is_array( $links ) ) {
return $links;
}
return array_merge(
$links,
[
Expand Down
13 changes: 8 additions & 5 deletions inc/app_replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -654,21 +654,24 @@ public function url_has_dam_flag( $url ) {
/**
* Get the optimized image url for the image url.
*
* @param string $url The image URL.
* @param mixed $width The image width.
* @param mixed $height The image height.
* @param array $resize The resize properties.
* @param string $url The image URL.
* @param mixed $width The image width.
* @param mixed $height The image height.
* @param array<string, mixed>|mixed $resize The resize properties.
*
* @return string
*/
protected function get_optimized_image_url( $url, $width, $height, $resize = [] ) {
$width = is_int( $width ) ? $width : 'auto';
$height = is_int( $height ) ? $height : 'auto';
// If the image is already using Optimole URL, we extract the source to rebuild it.
$url = $this->get_unoptimized_url( $url );

$optimized_image = Optimole::image( $url, $this->settings->get( 'cache_buster' ) )
->width( $width )
->height( $height );

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

}
Expand Down
5 changes: 5 additions & 0 deletions inc/lazyload_replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ public static function get_background_lazyload_selectors() {
return self::$background_lazyload_selectors;
}

if ( self::instance()->settings === null ) {
self::$background_lazyload_selectors = [];

return self::$background_lazyload_selectors;
}
if ( self::instance()->settings->get( 'bg_replacer' ) === 'disabled' ) {
self::$background_lazyload_selectors = [];
return self::$background_lazyload_selectors;
Expand Down
15 changes: 9 additions & 6 deletions inc/tag_replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -809,15 +809,18 @@ public function change_url_for_size( $original_url, $width, $height, $dpr = 1 )
/**
* Replace image URLs in the srcset attributes and in case there is a resize in action, also replace the sizes.
*
* @param array<int, array{url: string, descriptor: string, value: int}> $sources Array of image sources.
* @param array{0: int, 1: int}|int[] $size_array Array of width and height values in pixels (in that order).
* @param string $image_src The 'src' of the image.
* @param array<string, mixed> $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'.
* @param int $attachment_id Image attachment ID or 0.
* @param array<int, array{url: string, descriptor: string, value: int}>|mixed $sources Array of image sources.
* @param array{0: int, 1: int}|int[] $size_array Array of width and height values in pixels (in that order).
* @param string $image_src The 'src' of the image.
* @param array<string, mixed> $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'.
* @param int $attachment_id Image attachment ID or 0.
*
* @return array
* @return array|mixed
*/
public function filter_srcset_attr( $sources = [], $size_array = [], $image_src = '', $image_meta = [], $attachment_id = 0 ) {
if ( ! is_array( $sources ) ) {
return $sources;
}
if ( Optml_Media_Offload::is_uploaded_image( $image_src ) ) {
return $sources;
}
Expand Down
48 changes: 48 additions & 0 deletions inc/traits/dam_offload_utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
trait Optml_Dam_Offload_Utils {
use Optml_Normalizer;

/**
* Check if this contains the DAM flag.
*
* @param string $url The URL to check.
*
* @return bool
*/
private function is_dam_url( $url ) {
return strpos( $url, Optml_Dam::URL_DAM_FLAG ) !== false;
}
/**
* Checks that the attachment is a DAM image.
*
Expand Down Expand Up @@ -239,6 +249,31 @@ private function is_completed_offload( $id ) {

return false;
}
/**
* Get the attachment ID from optimole ID.
*
* @param string $optimole_id The optimole ID.
*
* @return int
*/
private function get_attachement_id_from_optimole_id( string $optimole_id ): int {
global $wpdb;

$id = $wpdb->get_var(
$wpdb->prepare(
"
SELECT post_id
FROM {$wpdb->postmeta}
WHERE meta_key = %s
AND meta_value = %s
LIMIT 1
",
Optml_Dam::OM_DAM_IMPORTED_FLAG,
$optimole_id
)
);
return empty( $id ) ? 0 : (int) $id;
}
/**
* Get the attachment ID from URL.
*
Expand All @@ -253,6 +288,19 @@ private function attachment_url_to_post_id( $input_url ) {
return (int) $cached;
}

if ( Optml_Media_Offload::is_uploaded_image( $input_url ) ) {
// The DAM are stored as attachments of format /id:<attachment_id>/<original_url>
$pattern = '#/' . Optml_Media_Offload::KEYS['uploaded_flag'] . '([^/]+)#';
if ( preg_match( $pattern, $input_url, $m ) ) {
$attachment_id = $this->get_attachement_id_from_optimole_id( $m[1] );
if ( $attachment_id !== 0 ) {
Optml_Attachment_Cache::set_cached_attachment_id( $input_url, $attachment_id );

return $attachment_id;
}
}
}

$url = $this->strip_image_size( $input_url );

$attachment_id = attachment_url_to_postid( $url );
Expand Down
4 changes: 4 additions & 0 deletions inc/traits/normalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ public function get_unoptimized_url( $url ) {
}
// If the url is an uploaded image, return the url
if ( Optml_Media_Offload::is_uploaded_image( $url ) ) {
$pattern = '#/id:([^/]+)/((?:https?|http)://\S+)#';
if ( preg_match( $pattern, $url, $matches ) ) {
$url = $matches[0];
}
return $url;
}

Expand Down
10 changes: 0 additions & 10 deletions inc/url_replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,16 +409,6 @@ private function get_dam_url( Image $image ) {
return $url;
}

/**
* Check if this contains the DAM flag.
*
* @param string $url The URL to check.
*
* @return bool
*/
private function is_dam_url( $url ) {
return strpos( $url, Optml_Dam::URL_DAM_FLAG ) !== false;
}

/**
* Check if the URL is offloaded.
Expand Down
12 changes: 0 additions & 12 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -516,12 +516,6 @@ parameters:
count: 1
path: inc/app_replacer.php

-
message: '#^Method Optml_App_Replacer\:\:get_optimized_image_url\(\) has parameter \$resize with no value type specified in iterable type array\.$#'
identifier: missingType.iterableValue
count: 1
path: inc/app_replacer.php

-
message: '#^Method Optml_App_Replacer\:\:get_upload_resource\(\) return type has no value type specified in iterable type array\.$#'
identifier: missingType.iterableValue
Expand Down Expand Up @@ -2790,12 +2784,6 @@ parameters:
count: 1
path: inc/tag_replacer.php

-
message: '#^Method Optml_Tag_Replacer\:\:filter_srcset_attr\(\) return type has no value type specified in iterable type array\.$#'
identifier: missingType.iterableValue
count: 1
path: inc/tag_replacer.php

-
message: '#^Method Optml_Tag_Replacer\:\:init\(\) has no return type specified\.$#'
identifier: missingType.return
Expand Down
5 changes: 5 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ function( $message ) {
}
}
require dirname( dirname( __FILE__ ) ) . '/optimole-wp.php';

// Prevent cache clearing actions during tests to avoid errors from cache compatibility classes
// This filter prevents optml_settings_updated and optml_clear_cache from being triggered
add_filter( 'optml_dont_trigger_settings_updated', '__return_true' );
}

tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
Expand All @@ -83,6 +87,7 @@ function( $message ) {
// Activate Optimole plugin
activate_plugin( 'optimole-wp/optimole-wp.php' );


// Set up the current logged in user
global $current_user;

Expand Down
Loading
Loading