Skip to content
Open
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
3 changes: 3 additions & 0 deletions inc/app_replacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,9 @@ public function url_has_dam_flag( $url ) {
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 );
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
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
153 changes: 153 additions & 0 deletions tests/test-generic.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,157 @@ function test_domain_hash() {
$this->assertEquals( $this->to_domain_hash("//www.domain.com/"), $value );
$this->assertNotEquals( $this->to_domain_hash("https://something.com/"), $value );
}

/**
* Test get_unoptimized_url with non-Optimole URLs.
*/
function test_get_unoptimized_url_non_optimole() {
// Initialize config for testing
$settings = new Optml_Settings();
$settings->update( 'service_data', [
'cdn_key' => 'test123',
'cdn_secret' => '12345',
'whitelist' => [ 'example.com', 'example.org' ],
] );
Optml_Config::init( [
'key' => 'test123',
'secret' => '12345',
] );

// Non-Optimole URLs should be returned as-is
$url = 'http://example.org/wp-content/uploads/image.jpg';
$this->assertEquals( $url, $this->get_unoptimized_url( $url ) );

$url = 'https://example.com/image.png';
$this->assertEquals( $url, $this->get_unoptimized_url( $url ) );

$url = '/wp-content/uploads/image.jpg';
$this->assertEquals( $url, $this->get_unoptimized_url( $url ) );
}

/**
* Test get_unoptimized_url with Optimole URLs (regular images).
*/
function test_get_unoptimized_url_optimole_regular() {
// Initialize config for testing
$settings = new Optml_Settings();
$settings->update( 'service_data', [
'cdn_key' => 'test123',
'cdn_secret' => '12345',
'whitelist' => [ 'example.com', 'example.org' ],
] );
Optml_Config::init( [
'key' => 'test123',
'secret' => '12345',
] );

// Optimole URL with regular image - should extract original URL after second 'http'
$optimole_url = 'https://test123.i.optimole.com/cb:test/w:800/h:600/q:mauto/http://example.org/wp-content/uploads/image.jpg';
$expected = 'http://example.org/wp-content/uploads/image.jpg';
$this->assertEquals( $expected, $this->get_unoptimized_url( $optimole_url ) );

// Optimole URL with https in original
$optimole_url = 'https://test123.i.optimole.com/cb:test/w:800/h:600/q:mauto/https://example.org/wp-content/uploads/image.jpg';
$expected = 'https://example.org/wp-content/uploads/image.jpg';
$this->assertEquals( $expected, $this->get_unoptimized_url( $optimole_url ) );

// Optimole URL with query parameters
$optimole_url = 'https://test123.i.optimole.com/cb:test/w:800/h:600/q:mauto/http://example.org/wp-content/uploads/image.jpg?param=value';
$expected = 'http://example.org/wp-content/uploads/image.jpg?param=value';
$this->assertEquals( $expected, $this->get_unoptimized_url( $optimole_url ) );
}

/**
* Test get_unoptimized_url with uploaded images (offloaded images).
*/
function test_get_unoptimized_url_uploaded_image() {
// Initialize config for testing
$settings = new Optml_Settings();
$settings->update( 'service_data', [
'cdn_key' => 'test123',
'cdn_secret' => '12345',
'whitelist' => [ 'example.com', 'example.org' ],
] );
Optml_Config::init( [
'key' => 'test123',
'secret' => '12345',
] );

// Uploaded image URL with /id: pattern - should extract original URL
$uploaded_url = 'https://test123.i.optimole.com/cb:test/w:800/h:600/q:mauto/id:abc123/http://example.org/wp-content/uploads/image.jpg';
$expected = '/id:abc123/http://example.org/wp-content/uploads/image.jpg';
$result = $this->get_unoptimized_url( $uploaded_url );
$this->assertStringContainsString( '/id:abc123/', $result );
$this->assertStringContainsString( 'http://example.org/wp-content/uploads/image.jpg', $result );

// Uploaded image URL with https in original
$uploaded_url = 'https://test123.i.optimole.com/cb:test/w:800/h:600/q:mauto/id:xyz789/https://example.org/wp-content/uploads/image.jpg';
$expected = '/id:xyz789/https://example.org/wp-content/uploads/image.jpg';
$result = $this->get_unoptimized_url( $uploaded_url );
$this->assertStringContainsString( '/id:xyz789/', $result );
$this->assertStringContainsString( 'https://example.org/wp-content/uploads/image.jpg', $result );
}

/**
* Test get_unoptimized_url edge cases.
*/
function test_get_unoptimized_url_edge_cases() {
// Initialize config for testing
$settings = new Optml_Settings();
$settings->update( 'service_data', [
'cdn_key' => 'test123',
'cdn_secret' => '12345',
'whitelist' => [ 'example.com', 'example.org' ],
] );
Optml_Config::init( [
'key' => 'test123',
'secret' => '12345',
] );

// Optimole URL without second 'http' - should return as-is
$optimole_url = 'https://test123.i.optimole.com/cb:test/w:800/h:600/q:mauto';
$this->assertEquals( $optimole_url, $this->get_unoptimized_url( $optimole_url ) );

// Empty string
$this->assertEquals( '', $this->get_unoptimized_url( '' ) );

// URL with only one 'http' occurrence
$optimole_url = 'https://test123.i.optimole.com/cb:test/w:800/h:600/q:mauto/image.jpg';
$this->assertEquals( $optimole_url, $this->get_unoptimized_url( $optimole_url ) );

// Uploaded image URL without matching pattern - should return as-is
$uploaded_url = 'https://test123.i.optimole.com/cb:test/w:800/h:600/q:mauto/id:abc123/image.jpg';
$result = $this->get_unoptimized_url( $uploaded_url );
// Should return the URL as-is since pattern doesn't match
$this->assertEquals( $uploaded_url, $result );
}

/**
* Test get_unoptimized_url with custom domain configuration.
*/
function test_get_unoptimized_url_custom_domain() {
// Initialize config with custom domain
$settings = new Optml_Settings();
$settings->update( 'service_data', [
'cdn_key' => 'test123',
'cdn_secret' => '12345',
'whitelist' => [ 'example.com', 'example.org' ],
'domain' => 'cdn.example.com',
'is_cname_assigned' => 'yes',
] );
Optml_Config::init( [
'key' => 'test123',
'secret' => '12345',
'domain' => 'cdn.example.com',
] );

// Custom domain Optimole URL
$optimole_url = 'https://cdn.example.com/cb:test/w:800/h:600/q:mauto/http://example.org/wp-content/uploads/image.jpg';
$expected = 'http://example.org/wp-content/uploads/image.jpg';
$this->assertEquals( $expected, $this->get_unoptimized_url( $optimole_url ) );

// Non-Optimole URL should still return as-is
$url = 'http://example.org/wp-content/uploads/image.jpg';
$this->assertEquals( $url, $this->get_unoptimized_url( $url ) );
}
}
Loading