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