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/dam.php +63 -122 3.11.1 → trunk View file →
@@ -8,8 +8,12 @@
8 8 * @package \Optimole\Inc
9 9 * @author Optimole <[email protected]>
10 10 */
11 11
12 +use Optimole\Sdk\Resource\ImageProperty\GravityProperty;
13 +use Optimole\Sdk\Resource\ImageProperty\ResizeTypeProperty;
14 +use Optimole\Sdk\ValueObject\Position;
15 +
12 16 /**
13 17 * Class Optml_Dam
14 18 */
15 19 class Optml_Dam {
@@ -50,19 +54,20 @@
50 54 add_action( 'wp_enqueue_media', [ $this, 'enqueue_media_scripts' ] );
51 55 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_page_scripts' ] );
52 56 }
53 57
54 - if ( defined( 'OPTML_DAM_ENDPOINT' ) && constant( 'OPTML_DAM_ENDPOINT' ) ) {
58 + if ( defined( 'OPTML_DAM_ENDPOINT' ) ) {
55 59 $this->dam_endpoint = constant( 'OPTML_DAM_ENDPOINT' );
56 60 }
57 -
61 + if ( $this->settings->get( 'retina_images' ) === 'enabled' ) {
62 + add_filter( 'wp_get_attachment_image_attributes', [ Optml_Tag_Replacer::instance(), 'filter_attachment_image_attributes' ], 99, 3 );
63 + }
58 64 add_filter( 'wp_get_attachment_image_src', [ $this, 'alter_attachment_image_src' ], 10, 4 );
59 65 add_filter( 'wp_get_attachment_metadata', [ $this, 'alter_attachment_metadata' ], 10, 2 );
60 66 add_filter( 'image_downsize', [ $this, 'catch_downsize' ], 10, 3 );
61 - add_filter( 'wp_prepare_attachment_for_js', [$this, 'alter_attachment_for_js'], 10, 3 );
62 - add_filter( 'wp_image_src_get_dimensions', [$this, 'alter_img_tag_w_h'], 10, 4 );
63 - add_filter( 'get_attached_file', [$this, 'alter_attached_file_response'], 10, 2 );
64 - add_filter( 'wp_calculate_image_srcset', [$this, 'disable_dam_images_srcset'], 1, 5 );
67 + add_filter( 'wp_prepare_attachment_for_js', [ $this, 'alter_attachment_for_js' ], 10, 3 );
68 + add_filter( 'get_attached_file', [ $this, 'alter_attached_file_response' ], 10, 2 );
69 + add_filter( 'wp_calculate_image_srcset', [ $this, 'disable_dam_images_srcset' ], 1, 5 );
65 70
66 71 add_filter(
67 72 'elementor/image_size/get_attachment_image_html',
68 73 [
@@ -191,32 +196,29 @@
191 196 *
192 197 * @return array $image.
193 198 */
194 199 public function alter_attachment_image_src( $image, $attachment_id, $size, $icon ) {
200 +
195 201 // Skip if not DAM image.
196 202 if ( ! $this->is_dam_imported_image( $attachment_id ) ) {
197 203 return $image;
198 204 }
199 -
200 205 $image_url = wp_get_attachment_url( $attachment_id );
201 - $incoming_size = $this->parse_dimension_from_optimized_url( $image_url );
202 - $width = $incoming_size[0];
203 - $height = $incoming_size[1];
206 + list($width, $height) = $this->parse_dimension_from_optimized_url( $image_url );
204 207
205 208 // Skip resize in single attachment view on backend.
206 209 if ( $this->is_attachment_edit_page( $attachment_id ) ) {
207 210 return [
208 211 $image_url,
209 - $width,
210 - $height,
212 + $width === 'auto' ? false : $width,
213 + $height === 'auto' ? false : $height,
211 214 false,
212 215 ];
213 216 }
214 217
218 + $metadata = wp_get_attachment_metadata( $attachment_id );
215 219 // Use the original size if the requested size is full.
216 220 if ( $size === 'full' ) {
217 - $metadata = wp_get_attachment_metadata( $attachment_id );
218 -
219 221 $image_url = $this->replace_dam_url_args(
220 222 [
221 223 'width' => $metadata['width'],
222 224 'height' => $metadata['height'],
@@ -231,47 +233,23 @@
231 233 $metadata['height'],
232 234 false,
233 235 ];
234 236 }
237 + $sizes = $this->size_to_dimension( $size, $metadata );
235 238
236 - $crop = false;
237 -
238 - // Size can be int [] containing width and height.
239 - if ( is_array( $size ) ) {
240 - $width = $size[0];
241 - $height = $size[1];
242 - $crop = true;
243 - } else {
244 - $sizes = $this->get_all_image_sizes();
245 -
246 - if ( ! isset( $sizes[ $size ] ) ) {
247 - return [
248 - $image_url,
249 - $width,
250 - $height,
251 - false,
252 - ];
253 - }
254 -
255 - $width = $sizes[ $size ]['width'];
256 - $height = $sizes[ $size ]['height'];
257 - $crop = (bool) $sizes[ $size ]['crop'];
258 - }
259 -
260 239 $image_url = $this->replace_dam_url_args(
261 240 [
262 - 'width' => $width,
263 - 'height' => $height,
264 - 'crop' => $crop,
241 + 'width' => $sizes['width'],
242 + 'height' => $sizes['height'],
243 + 'crop' => $sizes['resize'] ?? false,
265 244 ],
266 245 $image_url
267 246 );
268 -
269 247 return [
270 248 $image_url,
271 - $width,
272 - $height,
273 - $crop,
249 + $sizes['width'],
250 + $sizes['height'],
251 + $size === 'full', // @phpstan-ignore-line
274 252 ];
275 253 }
276 254
277 255 /**
@@ -445,12 +423,17 @@
445 423 * @return void
446 424 */
447 425 public function render_dashboard_page() {
448 426 ?>
449 - <iframe id="om-dam" style="display: none;" src="<?php echo( $this->build_iframe_url() ); ?>"></iframe>
427 + <style>
428 + .notice:not(.optml-notice-optin){
429 + display: none !important;
430 + }
431 + </style>
432 + <iframe id="om-dam" style="display: none;" src="<?php echo( $this->build_iframe_url() ); ?>" allow="clipboard-read; clipboard-write"></iframe>
450 433 <div class="om-dam-loader">
451 434 <img src="<?php echo esc_url( OPTML_URL . 'assets/img/logo.png' ); ?>" alt="Optimole Logo"
452 - class="om-dam-logo">
435 + class="om-dam-logo">
453 436 <p><?php echo esc_html__( 'Loading', 'optimole-wp' ); ?>...</p>
454 437 </div>
455 438 <?php
456 439 }
@@ -486,13 +469,16 @@
486 469
487 470 $data = json_encode( $data );
488 471 $data = rtrim( base64_encode( $data ), '=' );
489 472
490 - return add_query_arg(
491 - [
492 - 'data' => $data,
493 - ],
494 - $this->dam_endpoint
473 + return tsdk_translate_link(
474 + add_query_arg(
475 + [
476 + 'data' => $data,
477 + ],
478 + $this->dam_endpoint
479 + ),
480 + 'query'
495 481 );
496 482 }
497 483
498 484 /**
@@ -548,8 +534,10 @@
548 534
549 535 wp_enqueue_script( OPTML_NAMESPACE . '-admin-page' );
550 536
551 537 wp_enqueue_style( OPTML_NAMESPACE . '-admin-page', OPTML_URL . 'assets/build/media/admin-page.css' );
538 +
539 + do_action( 'themeisle_internal_page', OPTML_PRODUCT_SLUG, 'dam' );
552 540 }
553 541
554 542 /**
555 543 * Get localized variables for the media modal.
@@ -614,15 +602,15 @@
614 602
615 603 return $this->replace_dam_url_args( $custom_dimensions, $html );
616 604 }
617 605
618 - $all_sizes = $this->get_all_image_sizes();
606 + $all_sizes = Optml_App_Replacer::image_sizes();
619 607
620 - if ( ! isset( $all_sizes[ $settings['image_size'] ] ) ) {
608 + if ( ! isset( $all_sizes[ $image_size_key ] ) ) {
621 609 return $html;
622 610 }
623 611
624 - return $this->replace_dam_url_args( $all_sizes[ $settings['image_size'] ], $html );
612 + return $this->replace_dam_url_args( $all_sizes[ $image_size_key ], $html );
625 613 }
626 614
627 615 /**
628 616 * Needed as some blocks might use the image sizes.
@@ -637,21 +625,23 @@
637 625 if ( ! $this->is_dam_imported_image( $attachment->ID ) ) {
638 626 return $response;
639 627 }
640 628
641 - $sizes = $this->get_all_image_sizes();
629 + $sizes = Optml_App_Replacer::image_sizes();
642 630
631 + $meta = [];
632 + if ( isset( $response['width'] ) ) {
633 + $meta['width'] = $response['width'];
634 + }
635 + if ( isset( $response['height'] ) ) {
636 + $meta['height'] = $response['height'];
637 + }
643 638 foreach ( $sizes as $size => $args ) {
644 639 if ( isset( $response['sizes'][ $size ] ) ) {
645 640 continue;
646 641 }
642 + $args = $this->size_to_dimension( $size, $meta );
647 643
648 - $args = [
649 - 'height' => $args['height'],
650 - 'width' => $args['width'],
651 - 'crop' => true,
652 - ];
653 -
654 644 $response['sizes'][ $size ] = array_merge(
655 645 $args,
656 646 [
657 647 'url' => $this->replace_dam_url_args( $args, $response['url'] ),
@@ -658,68 +648,14 @@
658 648 'orientation' => ( $args['height'] > $args['width'] ) ? 'portrait' : 'landscape',
659 649 ]
660 650 );
661 651 }
652 + $response['url'] = $this->replace_dam_url_args( $meta, $response['url'] );
662 653
663 - $url_args = [
664 - 'height' => $response['height'],
665 - 'width' => $response['width'],
666 - 'crop' => false,
667 - ];
668 -
669 - $response['url'] = $this->replace_dam_url_args( $url_args, $response['url'] );
670 -
671 654 return $response;
672 655 }
673 656
674 657 /**
675 - * We have to short-circuit the logic that adds width and height to the img tag.
676 - * It compares the URL basename, and the `file` param for each image.
677 - * This happens for any image that gets its size set non-explicitly
678 - * e.g. an image block with its size set from the sidebar to `thumbnail`).
679 - *
680 - * Optimole has a single basename for all image resizes in its URL.
681 - *
682 - * @param array|false $dimensions Array with first element being the width
683 - * and second element being the height, or
684 - * false if dimensions could not be determined.
685 - * @param string $image_src The image URL (will be Optimole URL).
686 - * @param array $image_meta The image metadata.
687 - * @param int $attachment_id The image attachment ID. Default 0.
688 - */
689 - public function alter_img_tag_w_h( $dimensions, $image_src, $image_meta, $attachment_id ) {
690 - if ( ! $this->is_dam_imported_image( $attachment_id ) ) {
691 - return $dimensions;
692 - }
693 -
694 - // Get the dimensions from the optimized URL.
695 - $incoming_size = $this->parse_dimension_from_optimized_url( $image_src );
696 - $width = $incoming_size[0];
697 - $height = $incoming_size[1];
698 -
699 - $sizes = $this->get_all_image_sizes();
700 -
701 - // If this is an image size. Return its dimensions.
702 - foreach ( $sizes as $size => $args ) {
703 - if ( (int) $args['width'] !== (int) $width ) {
704 - continue;
705 - }
706 -
707 - if ( (int) $args['height'] !== (int) $height ) {
708 - continue;
709 - }
710 -
711 - return [
712 - $args['width'],
713 - $args['height'],
714 - ];
715 - }
716 -
717 - // Fall-through with the original dimensions.
718 - return $dimensions;
719 - }
720 -
721 - /**
722 658 * Replace the image size params in DAM URLs inside a string.
723 659 *
724 660 * @param array $args The arguments to replace.
725 661 * - width: The width of the image.
@@ -729,18 +665,18 @@
729 665 *
730 666 * @return string
731 667 */
732 668 public function replace_dam_url_args( $args, $subject ) {
733 - $args = wp_parse_args( $args, [ 'width' => 'auto', 'height' => 'auto', 'crop' => false, 'dam' => true] );
669 + $args = wp_parse_args( $args, [ 'width' => 'auto', 'height' => 'auto', 'dam' => true ] );
734 670
735 671 $width = $args['width'];
736 672 $height = $args['height'];
737 - $crop = (bool) $args['crop'];
673 + $crop = $args['crop'] ?? $args['resize'] ?? false;
738 674
739 - $gravity = Optml_Resize::GRAVITY_CENTER;
675 + $gravity = Position::CENTER;
740 676
741 677 if ( $this->settings->get( 'resize_smart' ) === 'enabled' ) {
742 - $gravity = Optml_Resize::GRAVITY_SMART;
678 + $gravity = GravityProperty::SMART;
743 679 }
744 680
745 681 if ( $width === 0 ) {
746 682 $width = 'auto';
@@ -752,10 +688,15 @@
752 688
753 689 // Use the proper replacement for the image size.
754 690 $replacement = '/w:' . $width . '/h:' . $height;
755 691
756 - if ( $crop ) {
692 + if ( $crop === true ) {
757 693 $replacement .= '/g:' . $gravity . '/rt:fill';
694 + } elseif ( is_array( $crop ) && ! empty( $crop ) ) {
695 +
696 + $replacement .= '/' . ( new GravityProperty( $crop['gravity'] ) ) .
697 + '/' . new ResizeTypeProperty( $crop['type'] ) .
698 + ( $crop['enlarge'] ? '/el:1' : '' );
758 699 }
759 700
760 701 $replacement .= '/q:';
761 702