| 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 |
const lazyloadRunObserver = () => { |
| 61 |
const lazyloadBackgrounds = document.querySelectorAll( `.e-con.e-parent:not(.e-lazyloaded)` ); |
| 62 |
const lazyloadBackgroundObserver = new IntersectionObserver( ( entries ) => { |
| 63 |
entries.forEach( ( entry ) => { |
| 64 |
if ( entry.isIntersecting ) { |
| 65 |
let lazyloadBackground = entry.target; |
| 66 |
if( lazyloadBackground ) { |
| 67 |
lazyloadBackground.classList.add( 'e-lazyloaded' ); |
| 68 |
} |
| 69 |
lazyloadBackgroundObserver.unobserve( entry.target ); |
| 70 |
} |
| 71 |
}); |
| 72 |
}, { rootMargin: '200px 0px 200px 0px' } ); |
| 73 |
lazyloadBackgrounds.forEach( ( lazyloadBackground ) => { |
| 74 |
lazyloadBackgroundObserver.observe( lazyloadBackground ); |
| 75 |
} ); |
| 76 |
}; |
| 77 |
const events = [ |
| 78 |
'DOMContentLoaded', |
| 79 |
'elementor/lazyload/observe', |
| 80 |
]; |
| 81 |
events.forEach( ( event ) => { |
| 82 |
document.addEventListener( event, lazyloadRunObserver ); |
| 83 |
} ); |
| 84 |
</script> |
| 85 |
<?php |
| 86 |
} ); |
| 87 |
} |
| 88 |
|
| 89 |
private function should_lazy_load_background_images(): bool { |
| 90 |
return ! is_admin() && ! Plugin::$instance->preview->is_preview_mode() && ! Plugin::$instance->editor->is_edit_mode(); |
| 91 |
} |
| 92 |
|
| 93 |
private static function is_lazy_load_background_images_enabled(): bool { |
| 94 |
return '1' === get_option( 'elementor_lazy_load_background_images', '1' ); |
| 95 |
} |
| 96 |
} |
| 97 |
|