PluginProbe
Elementor Website Builder – more than just a page builder / 3.30.0-beta1
Elementor Website Builder – more than just a page builder v3.30.0-beta1
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 3.30.0-beta1, at modules/lazyload/module.php

95 lines 2.6 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 if ( ! $this->should_lazy_load_background_images() ) {
29 return;
30 }
31
32 add_action( 'wp_head', function() {
33 ?>
34 <style>
35 .e-con.e-parent:nth-of-type(n+4):not(.e-lazyloaded):not(.e-no-lazyload),
36 .e-con.e-parent:nth-of-type(n+4):not(.e-lazyloaded):not(.e-no-lazyload) * {
37 background-image: none !important;
38 }
39 @media screen and (max-height: 1024px) {
40 .e-con.e-parent:nth-of-type(n+3):not(.e-lazyloaded):not(.e-no-lazyload),
41 .e-con.e-parent:nth-of-type(n+3):not(.e-lazyloaded):not(.e-no-lazyload) * {
42 background-image: none !important;
43 }
44 }
45 @media screen and (max-height: 640px) {
46 .e-con.e-parent:nth-of-type(n+2):not(.e-lazyloaded):not(.e-no-lazyload),
47 .e-con.e-parent:nth-of-type(n+2):not(.e-lazyloaded):not(.e-no-lazyload) * {
48 background-image: none !important;
49 }
50 }
51 </style>
52 <?php
53 } );
54
55 add_action( 'wp_footer', function() {
56 ?>
57 <script>
58 const lazyloadRunObserver = () => {
59 const lazyloadBackgrounds = document.querySelectorAll( `.e-con.e-parent:not(.e-lazyloaded)` );
60 const lazyloadBackgroundObserver = new IntersectionObserver( ( entries ) => {
61 entries.forEach( ( entry ) => {
62 if ( entry.isIntersecting ) {
63 let lazyloadBackground = entry.target;
64 if( lazyloadBackground ) {
65 lazyloadBackground.classList.add( 'e-lazyloaded' );
66 }
67 lazyloadBackgroundObserver.unobserve( entry.target );
68 }
69 });
70 }, { rootMargin: '200px 0px 200px 0px' } );
71 lazyloadBackgrounds.forEach( ( lazyloadBackground ) => {
72 lazyloadBackgroundObserver.observe( lazyloadBackground );
73 } );
74 };
75 const events = [
76 'DOMContentLoaded',
77 'elementor/lazyload/observe',
78 ];
79 events.forEach( ( event ) => {
80 document.addEventListener( event, lazyloadRunObserver );
81 } );
82 </script>
83 <?php
84 } );
85 }
86
87 private function should_lazy_load_background_images(): bool {
88 return ! is_admin() && ! Plugin::$instance->preview->is_preview_mode() && ! Plugin::$instance->editor->is_edit_mode();
89 }
90
91 private static function is_lazy_load_background_images_enabled(): bool {
92 return '1' === get_option( 'elementor_lazy_load_background_images', '1' );
93 }
94 }
95