This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Plugin.php
117 lines (101 loc) · 3.37 KB
/
Plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php namespace BennoThommo\LazyLoad;
use Lang;
use Event;
use IvoPetkov\HTML5DOMDocument;
use System\Classes\PluginBase;
class Plugin extends PluginBase
{
/**
* @inheritDoc
*/
public function pluginDetails()
{
return [
'name' => 'bennothommo.lazyload::lang.plugin.name',
'description' => 'bennothommo.lazyload::lang.plugin.description',
'author' => 'Ben Thomson',
'icon' => 'icon-picture-o'
];
}
/**
* @inheritDoc
*/
public function register()
{
$this->registerEvents();
}
/**
* Registers the event listeners for this plugin.
*
* @return void
*/
protected function registerEvents()
{
// Add toggle in CMS Pages form
Event::listen('backend.form.extendFields', function ($formWidget) {
if (!$formWidget->getController() instanceof \Cms\Controllers\Index) {
return;
}
if (!$formWidget->model instanceof \Cms\Classes\Page) {
return;
}
// Make "Hidden" field span left
$field = $formWidget->getField('settings[is_hidden]');
$field->span = 'left';
$formWidget->addTabFields([
'settings[lazy_load]' => [
'tab' => 'cms::lang.editor.settings',
'label' => 'bennothommo.lazyload::lang.fields.lazyLoad.label',
'type' => 'checkbox',
'comment' => 'bennothommo.lazyload::lang.fields.lazyLoad.comment',
'span' => 'right'
],
]);
// // Set lazy loading to on by default
if (is_null($formWidget->model->lazy_load)) {
$lazyField = $formWidget->getField('settings[lazy_load]');
$lazyField->value = 1;
}
});
// Process images on loading page
Event::listen('cms.page.postprocess', function ($controller, $url, $page, $dataHolder) {
$lazyLoad = (bool) $page->lazy_load ?? true;
if ($lazyLoad) {
$this->applyLazyLoading($dataHolder);
}
return $dataHolder;
});
}
/**
* Apply lazy loading to all applicable images in content.
*
* @param \stdClass $dataHolder
* @return void
*/
protected function applyLazyLoading($dataHolder)
{
$dom = new HTML5DOMDocument();
$dom->loadHTML($dataHolder->content);
// Find all images
$images = $dom->querySelectorAll('img');
if ($images->count() > 0) {
foreach ($images as $image) {
// Image must have a source
if (!$image->hasAttribute('src') && !$image->hasAttribute('srcset')) {
continue;
}
// Image must have a specified width and height attribute, and not already have a "loading" attribute
if (
!$image->hasAttribute('width')
|| !$image->hasAttribute('height')
|| $image->hasAttribute('loading')
) {
continue;
}
// Add lazy loading attribute to images
$image->setAttribute('loading', 'lazy');
}
}
$dataHolder->content = $dom->saveHtml();
}
}