PluginProbe
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization / 3.11.1
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization v3.11.1
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 3.12.1 All 134 releases
optimole-wp / inc / compatibilities / elementor_builder.php

elementor_builder.php in Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization 3.11.1, at inc/compatibilities/elementor_builder.php

125 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class Optml_elementor_builder
5 *
6 * @reason Adding selectors for background lazyload
7 */
8 class Optml_elementor_builder extends Optml_compatibility {
9
10 /**
11 * Should we load the integration logic.
12 *
13 * @return bool Should we load.
14 */
15 function should_load() {
16 include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
17
18 return is_plugin_active( 'elementor/elementor.php' );
19 }
20
21 /**
22 * Register integration details.
23 */
24 public function register() {
25 add_action( 'elementor/frontend/after_enqueue_styles', [$this, 'add_src'], PHP_INT_MIN, 1 );
26
27 add_filter( 'elementor/frontend/builder_content/before_enqueue_css_file', [$this, 'add_src_filter'], PHP_INT_MIN, 1 );
28
29 add_filter( 'elementor/frontend/builder_content/before_print_css', [$this, 'remove_src_filter'], PHP_INT_MIN, 1 );
30
31 add_filter(
32 'optml_lazyload_bg_selectors',
33 function ( $all_watchers ) {
34 $all_watchers[] = '.elementor-widget-container';
35 $all_watchers[] = '.elementor-background-slideshow__slide__image';
36 return $all_watchers;
37 }
38 );
39 add_action(
40 'optml_settings_updated',
41 function () {
42 if ( did_action( 'elementor/loaded' ) ) {
43 if ( class_exists( '\Elementor\Plugin' ) ) {
44 \Elementor\Plugin::instance()->files_manager->clear_cache();
45 }
46 }
47 }
48 );
49
50 }
51
52 /**
53 * Remove filter for the image src after the css is saved by elementor.
54 *
55 * @param bool $with_css Flag used to determine if the css will be inline or not. Not used.
56 * @return bool
57 * @uses filter:elementor/frontend/builder_content/before_print_css
58 */
59 public function remove_src_filter( $with_css ) {
60
61 remove_filter( 'wp_get_attachment_image_src', [$this, 'optimize_src'], PHP_INT_MAX );
62
63 return $with_css;
64 }
65 /**
66 * Add filter for the image src after the css is enqueued.
67 *
68 * @param object $css Elementor css info. Not used.
69 * @return object
70 * @uses filter:elementor/frontend/builder_content/before_enqueue_css_file
71 */
72 public function add_src_filter( $css ) {
73
74 // check if the filter was added to avoid duplication
75 if ( has_filter( 'wp_get_attachment_image_src', [$this, 'optimize_src'] ) ) {
76 return $css;
77 }
78
79 add_filter( 'wp_get_attachment_image_src', [$this, 'optimize_src'], PHP_INT_MAX, 4 );
80
81 return $css;
82 }
83
84 /**
85 * Add action to add the filter for the image src.
86 *
87 * @return void
88 */
89 public function add_src() {
90 if ( ! has_filter( 'wp_get_attachment_image_src', [$this, 'optimize_src'] ) ) {
91 add_filter( 'wp_get_attachment_image_src', [$this, 'optimize_src'], PHP_INT_MAX, 4 );
92 }
93 }
94
95 /**
96 * Optimize the image src when it is requested in elementor.
97 *
98 * @param array $image Image data.
99 * @param int $attachment_id Attachment id.
100 * @param string|int[] $size Image size.
101 * @param bool $icon Whether to use icon or not.
102 * @return array
103 * @uses filter:wp_get_attachment_image_src
104 */
105 public function optimize_src( $image, $attachment_id, $size, $icon ) {
106
107 if ( ! isset( $image[0] ) ) {
108 return $image;
109 }
110
111 $image[0] = Optml_Main::instance()->manager->url_replacer->build_url( $image[0] );
112
113 return $image;
114
115 }
116 /**
117 * Should we early load the compatibility?
118 *
119 * @return bool Whether to load the compatibility or not.
120 */
121 public function should_load_early() {
122 return true;
123 }
124 }
125