PluginProbe
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization / trunk
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization vtrunk
4.2.14 4.2.13 4.2.12 4.2.11 4.2.10 4.2.9 4.2.8 4.2.7 4.2.6 4.2.5 2.5.5 2.5.6 2.5.7 3.0.0 3.0.1 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.11.0 3.11.1 3.11.2 3.11.3 3.12.0 All 135 releases
← All changes | inc/app_replacer.php +88 -29 3.10.0 → trunk View file →
@@ -1,6 +1,9 @@
1 1 <?php
2 2
3 +use Optimole\Sdk\Optimole;
4 +use Optimole\Sdk\ValueObject\Position;
5 +
3 6 /**
4 7 * Class Optml_App_Replacer
5 8 *
6 9 * @package \Optml\Inc
@@ -6,8 +9,9 @@
6 9 * @package \Optml\Inc
7 10 * @author Optimole <[email protected]>
8 11 */
9 12 abstract class Optml_App_Replacer {
13 + use Optml_Dam_Offload_Utils;
10 14
11 15 /**
12 16 * Filters used for lazyload.
13 17 *
@@ -56,20 +60,14 @@
56 60 * @var Optml_Settings $settings
57 61 */
58 62 public $settings = null;
59 63 /**
60 - * Defines which is the maximum width accepted in the optimization process.
64 + * Cached offloading status.
61 65 *
62 - * @var int
66 + * @var null|bool Offload status.
63 67 */
64 - protected $max_width = 3000;
68 + public static $offload_enabled = null;
65 69 /**
66 - * Defines which is the maximum width accepted in the optimization process.
67 - *
68 - * @var int
69 - */
70 - protected $max_height = 3000;
71 - /**
72 70 * Defines if the dimensions should be limited when images are served.
73 71 *
74 72 * @var bool
75 73 */
@@ -184,9 +182,9 @@
184 182 if ( null !== self::$ignore_lazyload_strings ) {
185 183 return self::$ignore_lazyload_strings;
186 184 }
187 185
188 - self::$possible_src_attributes = apply_filters( 'optml_possible_lazyload_flags', [ 'skip-lazy', 'data-skip-lazy' ] );
186 + self::$possible_src_attributes = apply_filters( 'optml_possible_lazyload_flags', [ 'skip-lazy', 'data-skip-lazy', 'data-opt-src' ] );
189 187
190 188 return array_merge( self::$possible_src_attributes, [ '<noscript' ] );
191 189 }
192 190 /**
@@ -200,9 +198,9 @@
200 198 }
201 199
202 200 self::$ignore_tag_strings = apply_filters( 'optml_skip_optimizations_css_classes', [ 'skip-optimization' ] );
203 201
204 - return self::$ignore_tag_strings;
202 + return self::$ignore_tag_strings;
205 203 }
206 204 /**
207 205 * Returns possible data-opt-src ignore flags attributes.
208 206 *
@@ -222,9 +220,9 @@
222 220 * Size to crop maping.
223 221 *
224 222 * @return array Size mapping.
225 223 */
226 - protected static function size_to_crop() {
224 + public static function size_to_crop() {
227 225 if ( ! empty( self::$size_to_crop ) ) {
228 226 return self::$size_to_crop;
229 227 }
230 228
@@ -259,9 +257,8 @@
259 257 'height' => (int) $height,
260 258 'enlarge' => true,
261 259 'crop' => $crop,
262 260 ];
263 -
264 261 }
265 262
266 263 /**
267 264 * Returns the array of image sizes since `get_intermediate_image_sizes` and image metadata doesn't include the
@@ -269,9 +266,9 @@
269 266 *
270 267 * @return array
271 268 * @global $wp_additional_image_sizes
272 269 */
273 - protected static function image_sizes() {
270 + public static function image_sizes() {
274 271 if ( ! empty( self::$image_sizes ) ) {
275 272 return self::$image_sizes;
276 273 }
277 274
@@ -337,9 +334,9 @@
337 334
338 335 self::$filters = $this->settings->get_filters();
339 336 add_filter(
340 337 'optml_should_avif_ext',
341 - function( $should_avif, $ext ) {
338 + function ( $should_avif, $ext ) {
342 339 return $ext !== 'svg';
343 340 },
344 341 10,
345 342 2
@@ -364,8 +361,11 @@
364 361 return $strings;
365 362 },
366 363 10
367 364 );
365 + if ( self::$offload_enabled === null ) {
366 + self::$offload_enabled = ( new Optml_Settings() )->is_offload_enabled();
367 + }
368 368 }
369 369
370 370
371 371 /**
@@ -403,11 +403,12 @@
403 403 $domain = $service_data['domain'];
404 404 }
405 405 Optml_Config::init(
406 406 [
407 - 'key' => $service_data['cdn_key'],
408 - 'secret' => $service_data['cdn_secret'],
409 - 'domain' => $domain,
407 + 'key' => $service_data['cdn_key'],
408 + 'secret' => $service_data['cdn_secret'],
409 + 'domain' => $domain,
410 + 'api_key' => $this->settings->get( 'api_key' ),
410 411 ]
411 412 );
412 413
413 414 if ( defined( 'OPTML_SITE_MIRROR' ) && constant( 'OPTML_SITE_MIRROR' ) ) {
@@ -440,11 +441,8 @@
440 441 $this->allowed_sources['i1.wp.com'] = true;
441 442 $this->allowed_sources['i2.wp.com'] = true;
442 443 $this->is_allowed_site = count( array_diff_key( $this->possible_sources, $this->allowed_sources ) ) > 0;
443 444
444 - $this->max_height = $this->settings->get( 'max_height' );
445 - $this->max_width = $this->settings->get( 'max_width' );
446 -
447 445 $this->limit_dimensions_enabled = $this->settings->get( 'limit_dimensions' ) === 'enabled';
448 446 if ( $this->limit_dimensions_enabled ) {
449 447 $this->limit_height = $this->settings->get( 'limit_height' );
450 448 $this->limit_width = $this->settings->get( 'limit_width' );
@@ -482,9 +480,9 @@
482 480 * @param mixed $crop Cropping behaviour.
483 481 *
484 482 * @return mixed Original value.
485 483 */
486 - static function listen_to_sizes( $value, $orig_w, $orig_h, $dest_w, $dest_h, $crop ) {
484 + public static function listen_to_sizes( $value, $orig_w, $orig_h, $dest_w, $dest_h, $crop ) {
487 485 self::add_size( $dest_w, $dest_h, $crop );
488 486
489 487 return $value;
490 488 }
@@ -491,9 +489,9 @@
491 489
492 490 /**
493 491 * Extract domains and use them as keys for fast processing.
494 492 *
495 - * @param array $urls Input urls.
493 + * @param array|null $urls Input urls.
496 494 *
497 495 * @return array Array of domains as keys.
498 496 */
499 497 protected function extract_domain_from_urls( $urls = [] ) {
@@ -530,9 +528,9 @@
530 528
531 529 /**
532 530 * Check if we can replace the url.
533 531 *
534 - * @param string $url Url to change.
532 + * @param string|mixed $url Url to change.
535 533 *
536 534 * @return bool Either we can replace this url or not.
537 535 */
538 536 public function can_replace_url( $url ) {
@@ -571,18 +569,27 @@
571 569 *
572 570 * @return string
573 571 **/
574 572 public function strip_image_size_from_url( $url ) {
575 -
576 573 if ( apply_filters( 'optml_should_skip_strip_image_size', false, $url ) === true ) {
577 574 return $url;
578 575 }
579 576 if ( preg_match( '#(-\d+x\d+(?:_c)?|(@2x))\.(' . implode( '|', array_keys( Optml_Config::$image_extensions ) ) . '){1}$#i', $url, $src_parts ) ) {
580 577 $stripped_url = str_replace( $src_parts[1], '', $url );
578 +
579 + if ( $this->settings->is_offload_enabled() ) {
580 + // Treat offloaded attachments differently.
581 + $id = $this->attachment_url_to_post_id( $stripped_url );
582 +
583 + if ( $id !== 0 && $this->is_new_offloaded_attachment( $id ) ) {
584 + return $stripped_url;
585 + }
586 + }
587 +
581 588 // Extracts the file path to the image minus the base url
582 589 $file_path = substr( $stripped_url, strpos( $stripped_url, $this->upload_resource['url'] ) + $this->upload_resource['url_length'] );
583 590 if ( file_exists( $this->upload_resource['directory'] . $file_path ) ) {
584 - $url = $stripped_url;
591 + return $stripped_url;
585 592 }
586 593 }
587 594
588 595 return $url;
@@ -604,8 +611,17 @@
604 611 $crop = ( isset( $width_height_string[3] ) && $width_height_string[3] === '_c' );
605 612 if ( $width && $height ) {
606 613 return [ $width, $height, $crop ];
607 614 }
615 + } else {
616 + $optimized_args = $this->parse_dimension_from_optimized_url( $src );
617 + if ( $optimized_args[0] !== false || $optimized_args[1] !== false ) {
618 + return [
619 + $optimized_args[0] !== 'auto' ? (int) $optimized_args[0] : false,
620 + $optimized_args[1] !== 'auto' ? (int) $optimized_args[1] : false,
621 + false,
622 + ];
623 + }
608 624 }
609 625
610 626 return [ false, false, false ];
611 627 }
@@ -620,11 +636,9 @@
620 636 * @param array $resize Optml crop array.
621 637 * @return string The optimized url.
622 638 */
623 639 public function get_media_optimized_url( $url, $table_id, $width = 'auto', $height = 'auto', $resize = [] ) {
624 - $optimized_url = ( new Optml_Image( $url, ['width' => $width, 'height' => $height, 'resize' => $resize, 'quality' => $this->settings->get_numeric_quality()], $this->settings->get( 'cache_buster' ) ) )->get_url();
625 - $optimized_url = str_replace( $url, Optml_Media_Offload::KEYS['not_processed_flag'] . 'media_cloud' . '/' . Optml_Media_Offload::KEYS['uploaded_flag'] . $table_id . '/' . $url, $optimized_url );
626 - return $optimized_url;
640 + return str_replace( $url, Optml_Media_Offload::KEYS['not_processed_flag'] . 'media_cloud' . '/' . Optml_Media_Offload::KEYS['uploaded_flag'] . $table_id . '/' . $url, $this->get_optimized_image_url( $url, $width, $height, $resize ) );
627 641 }
628 642
629 643 /**
630 644 * Test that the url has the dam flag.
@@ -634,6 +648,51 @@
634 648 * @return bool
635 649 */
636 650 public function url_has_dam_flag( $url ) {
637 651 return strpos( $url, Optml_Dam::URL_DAM_FLAG ) !== false;
652 + }
653 +
654 + /**
655 + * Get the optimized image url for the image url.
656 + *
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.
661 + *
662 + * @return string
663 + */
664 + protected function get_optimized_image_url( $url, $width, $height, $resize = [] ) {
665 + $width = is_int( $width ) ? $width : 'auto';
666 + $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 +
670 + $optimized_image = Optimole::image( $url, $this->settings->get( 'cache_buster' ) )
671 + ->width( $width )
672 + ->height( $height );
673 +
674 + if ( is_array( $resize ) && ! empty( $resize['type'] ) ) {
675 + $optimized_image->resize( $resize['type'], $resize['gravity'] ?? Position::CENTER, $resize['enlarge'] ?? false );
676 +
677 + }
678 +
679 + $optimized_image->quality( $this->settings->to_accepted_quality( $this->settings->get_quality() ) );
680 +
681 + return $optimized_image->getUrl();
682 + }
683 +
684 + /**
685 + * Returns an image id from the url.
686 + *
687 + * @param string $url The image URL.
688 + *
689 + * @return int
690 + */
691 + public function get_id_by_url( $url ) {
692 + $url = $this->get_unoptimized_url( $url );
693 + srand( crc32( $url ) );
694 + $random_id = rand();
695 + srand();
696 + return $random_id;
638 697 }
639 698 }