PluginProbe
Elementor Website Builder – more than just a page builder / 4.2.4
Elementor Website Builder – more than just a page builder v4.2.4
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / modules / lazyload / module.php

module.php in Elementor Website Builder – more than just a page builder 4.2.4, at modules/lazyload/module.php

99 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Modules\LazyLoad;
3
4 use Elementor\Core\Base\Module as BaseModule;
5 use Elementor\Plugin;
6
7 if ( ! defined( 'ABSPATH' ) ) {
8 exit; // Exit if accessed directly.
9 }
10
11 class Module extends BaseModule {
12
13 public function get_name() {
14 return 'lazyload';
15 }
16
17 public function __construct() {
18 parent::__construct();
19
20 add_action( 'init', [ $this, 'init' ] );
21 }
22
23 public function init() {
24 if ( ! $this->is_lazy_load_background_images_enabled() ) {
25 return;
26 }
27
28 add_action( 'wp_head', function() {
29 if ( ! $this->should_lazy_load_background_images() ) {
30 return;
31 }
32 ?>
33 <style>
34 .e-con.e-parent:nth-of-type(n+4):not(.e-lazyloaded):not(.e-no-lazyload),
35 .e-con.e-parent:nth-of-type(n+4):not(.e-lazyloaded):not(.e-no-lazyload) * {
36 background-image: none !important;
37 }
38 @media screen and (max-height: 1024px) {
39 .e-con.e-parent:nth-of-type(n+3):not(.e-lazyloaded):not(.e-no-lazyload),
40 .e-con.e-parent:nth-of-type(n+3):not(.e-lazyloaded):not(.e-no-lazyload) * {
41 background-image: none !important;
42 }
43 }
44 @media screen and (max-height: 640px) {
45 .e-con.e-parent:nth-of-type(n+2):not(.e-lazyloaded):not(.e-no-lazyload),
46 .e-con.e-parent:nth-of-type(n+2):not(.e-lazyloaded):not(.e-no-lazyload) * {
47 background-image: none !important;
48 }
49 }
50 </style>
51 <?php
52 } );
53
54 add_action( 'wp_footer', function() {
55 if ( ! $this->should_lazy_load_background_images() ) {
56 return;
57 }
58 ?>
59 <script>
60 ( () => {
61 const lazyloadRunObserver = () => {
62 const lazyloadBackgrounds = document.querySelectorAll( `.e-con.e-parent:not(.e-lazyloaded)` );
63 const lazyloadBackgroundObserver = new IntersectionObserver( ( entries ) => {
64 entries.forEach( ( entry ) => {
65 if ( entry.isIntersecting ) {
66 let lazyloadBackground = entry.target;
67 if( lazyloadBackground ) {
68 lazyloadBackground.classList.add( 'e-lazyloaded' );
69 }
70 lazyloadBackgroundObserver.unobserve( entry.target );
71 }
72 });
73 }, { rootMargin: '200px 0px 200px 0px' } );
74 lazyloadBackgrounds.forEach( ( lazyloadBackground ) => {
75 lazyloadBackgroundObserver.observe( lazyloadBackground );
76 } );
77 };
78 const events = [
79 'DOMContentLoaded',
80 'elementor/lazyload/observe',
81 ];
82 events.forEach( ( event ) => {
83 document.addEventListener( event, lazyloadRunObserver );
84 } );
85 } )();
86 </script>
87 <?php
88 } );
89 }
90
91 private function should_lazy_load_background_images(): bool {
92 return ! is_admin() && ! Plugin::$instance->preview->is_preview_mode() && ! Plugin::$instance->editor->is_edit_mode();
93 }
94
95 private static function is_lazy_load_background_images_enabled(): bool {
96 return '1' === get_option( 'elementor_lazy_load_background_images', '1' );
97 }
98 }
99