PluginProbe
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript / 1.9.6
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript v1.9.6
4.1.16 4.1.15 4.1.14 4.1.13 4.1.12 4.1.11 4.1.10 4.0.30 4.0.29 4.0.28 4.0.27 4.0.26 4.0.24 4.0.25 4.0.23 4.0.22 4.0.21 4.0.19 4.0.18 4.0.17 4.0.16 1.9.3 1.9.4 1.9.5 1.9.6 All 170 releases
searchpro / inc / photon / class-styleOptimizer.php

class-styleOptimizer.php in BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript 1.9.6, at inc/photon/class-styleOptimizer.php

322 lines 10.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class berqStyleOptimizer
4 {
5 public $loading = 'delay';
6 public $noscript = '';
7
8 function set_loading($loading)
9 {
10 $this->loading = $loading;
11 }
12
13 function run_optimization($photonClass, $buffer)
14 {
15 /* if ($this->loading == 'default') { */
16 /* return $buffer; */
17 /* } */
18
19 // Load HTML content using Simple HTML DOM
20 $html = str_get_html($buffer);
21
22 // Process <link> tags with rel="stylesheet"
23 foreach ($html->find('link[rel=stylesheet], link[rel=preload]') as $linkNode) {
24
25 // Delay and preload use javascript to load CSS
26 if ($this->loading == 'delay' || $this->loading == 'preload') {
27 $this->noscript = $this->noscript.$linkNode->outertext;
28 }
29
30 // Skip if the link tag already has data-berqwp attribute
31 if ($linkNode->hasAttribute('data-berqwp')) {
32 continue;
33 }
34
35 $href = trim($linkNode->href);
36 $excluded = false;
37
38 // If tag has a match for exclude url list
39 if (!empty($photonClass->js_css_exclude_urls)) {
40 foreach ($photonClass->js_css_exclude_urls as $js_exclude_keyword) {
41 if (!empty($js_exclude_keyword)) {
42 /* error_log("$href - $js_exclude_keyword"); */
43 if (strpos($href, trim($js_exclude_keyword)) !== false) {
44 $excluded = true;
45 break;
46 }
47 }
48 }
49 }
50
51 if ($excluded) {
52 continue;
53 }
54
55
56
57 if ($linkNode->rel == 'preload' && (!$linkNode->hasAttribute('as') || !$linkNode->hasAttribute('onload') || $linkNode->as !== 'style')) {
58 continue;
59 }
60
61
62 if ($photonClass->use_cdn) {
63
64 $kw_found = false;
65
66 foreach ($photonClass->css_excluded_keywords as $keyword) {
67 if (stripos($href, $keyword) !== false) {
68 $kw_found = true;
69 }
70 }
71 // Check if the script source contains any excluded keywords
72 if (!$kw_found) {
73 // Send the file URL to CDN as GET parameters
74 /* $cdnUrl = 'https://cdn.berqwp.com/'; */
75
76 global $berqCDN;
77 $berqCDN->add_file_in_queue($href);
78 // $cdnUrl = 'https://boost.berqwp.com/photon/cdn/';
79 // $cdnUrl .= '?url=' . urlencode($href);
80 // $cdnUrl .= '&domain=' . $photonClass->domain;
81
82 // $photonClass->add_into_cdn_queue($cdnUrl, $href);
83
84 }
85 }
86
87 if ($this->loading == 'default') {
88 continue;
89 }
90
91 // Process or modify <link> tags as needed
92 $linkNode->setAttribute('data-berqwp-style-href', esc_attr($href));
93 $linkNode->href = '';
94
95 unset($linkNode);
96 }
97
98 // Process <style> tags
99 foreach ($html->find('style') as $styleNode) {
100
101 // Skip if the style tag already has data-berqwp-style attribute
102 if ($styleNode->hasAttribute('data-berqwp')) {
103 continue;
104 }
105
106 // Delay and preload use javascript to load CSS
107 if ($this->loading == 'delay' || $this->loading == 'preload') {
108 $this->noscript = $this->noscript.$styleNode->outertext;
109 }
110
111 if ($photonClass->use_cdn) {
112 $baseUrl = bwp_getBaseUrl($photonClass->site_url);
113 $styleContent = $styleNode->innertext;
114 $matches = bwp_extractUrlsFromCss($styleContent);
115
116 foreach ($matches as $url) {
117 $absolute_url = bwp_rel2abs($url, $baseUrl);
118
119 global $berqCDN;
120 $berqCDN->add_file_in_queue($absolute_url);
121
122 // $cdn_url = "https://boost.berqwp.com/photon/cdn/?url=".urlencode($absolute_url)."&domain=$photonClass->domain";
123 // $photonClass->add_into_cdn_queue($cdn_url, $absolute_url);
124 }
125
126 }
127
128 if ($this->loading == 'default') {
129 continue;
130 }
131
132 // Commented to let style tags load initially - 26 Jun 2024
133 // Process or modify <style> tags as needed
134 $styleContent = $styleNode->innertext;
135 // var_dump($styleContent);
136 $nofontface = preg_replace('/@font-face\s*{[^}]+}/i', '', $styleContent);
137 $styleNode->innertext = $nofontface;
138 $styleNode->setAttribute('data-berqwp-style', esc_attr($styleContent));
139
140 unset($styleNode);
141 }
142
143 // Output the modified HTML
144 $buffer = $html->save();
145 $html->clear();
146 unset($html);
147
148 add_filter('berqwp_buffer_before_closing_body', [$this, 'script']);
149
150 // CSS compatibility for javascript disabled
151 add_filter('berqwp_buffer_before_closing_body', [$this, 'noscript']);
152
153 unset($photonClass);
154
155 return $buffer;
156
157 }
158
159 function preload_script()
160 {
161 $script = "
162 <script id='preload-styles' defer>
163 let initialized_style_preload = false;
164
165 window.addEventListener('load', function () {
166 if (initialized_style_preload) {
167 return;
168 }
169
170 initialized_style_preload = true;
171
172 let berqwp_styles = [];
173 let berqwp_linkTags = document.querySelectorAll('link[data-berqwp-style-href]');
174
175 // Iterate through each link tag
176 berqwp_linkTags.forEach(function (linkTag, index) {
177 berqwp_styles.push(linkTag.getAttribute('data-berqwp-style-href'))
178 });
179
180 (async () => {
181 // Call the function to start fetching all scripts
182 await berqwp_add_assets_browser_cache(berqwp_styles);
183
184 berqwp_preload_css();
185
186 })();
187
188 });
189
190
191 function BerqWPcacheResource(resourceUrl) {
192 return caches.open('berqwp-cache').then(cache => {
193 return fetch(resourceUrl).then(response => {
194 return cache.put(resourceUrl, response);
195 });
196 });
197 }
198
199 function berqwp_preload_css() {
200 // Get all link tags containing data-berqwp-style-href
201 let berqwp_linkTags = document.querySelectorAll('link[data-berqwp-style-href]');
202
203 // Iterate through each link tag
204 berqwp_linkTags.forEach(function (linkTag, index) {
205 // Set the href attribute of each link tag
206 linkTag.setAttribute('href', linkTag.getAttribute('data-berqwp-style-href'));
207
208 });
209
210 // Get all style tags containing data-berqwp-style
211 var berqwp_styleTags = document.querySelectorAll('style[data-berqwp-style]');
212
213 // Loop through each style tag
214 berqwp_styleTags.forEach(function (styleTag) {
215 // Get the value of data-berqwp-style
216 var berqwpStyle = styleTag.getAttribute('data-berqwp-style');
217
218 // Set the content of the style tag with the value of data-berqwp-style
219 styleTag.textContent = berqwpStyle;
220 });
221
222 // Trigger event when styles are loaded
223 let berqwp_styles_event = new CustomEvent('berqwpStylesLoaded');
224
225 // Dispatch the custom event
226 window.dispatchEvent(berqwp_styles_event);
227
228
229 }
230
231 </script>
232 ";
233
234 return $script;
235
236 }
237
238 function delay_script()
239 {
240 $script = "
241 <script id='delay-styles' defer>
242
243 function BerqWPcacheResource(resourceUrl) {
244 return caches.open('berqwp-cache').then(cache => {
245 return fetch(resourceUrl).then(response => {
246 return cache.put(resourceUrl, response);
247 });
248 });
249 }
250
251 function berqwp_css_handleUserInteraction() {
252 // Get all link tags containing data-berqwp-style-href
253 let berqwp_linkTags = document.querySelectorAll('link[data-berqwp-style-href]');
254
255 // Iterate through each link tag
256 berqwp_linkTags.forEach(function (linkTag, index) {
257 // Set the href attribute of each link tag
258 linkTag.setAttribute('href', linkTag.getAttribute('data-berqwp-style-href'));
259
260 });
261
262 // Get all style tags containing data-berqwp-style
263 var berqwp_styleTags = document.querySelectorAll('style[data-berqwp-style]');
264
265 // Loop through each style tag
266 berqwp_styleTags.forEach(function (styleTag) {
267 // Get the value of data-berqwp-style
268 var berqwpStyle = styleTag.getAttribute('data-berqwp-style');
269
270 // Set the content of the style tag with the value of data-berqwp-style
271 styleTag.textContent = berqwpStyle;
272 });
273
274 // Trigger event when styles are loaded
275 let berqwp_styles_event = new CustomEvent('berqwpStylesLoaded');
276
277 // Dispatch the custom event
278 window.dispatchEvent(berqwp_styles_event);
279
280
281 // After running the function, remove all event listeners to ensure it runs only once
282 for (let eventType of berqwp_css_interactionEventTypes) {
283 document.removeEventListener(eventType, berqwp_css_handleUserInteraction);
284 }
285
286 }
287
288 const berqwp_css_interactionEventTypes = ['click', 'mousemove', 'keydown', 'touchstart', 'scroll'];
289
290 for (let eventType of berqwp_css_interactionEventTypes) {
291 document.addEventListener(eventType, berqwp_css_handleUserInteraction);
292 }
293
294 </script>
295 ";
296
297 return $script;
298
299 }
300
301 function script($script_html)
302 {
303 if ($this->loading == 'delay') {
304 $script_html .= $this->delay_script();
305 }
306
307 if ($this->loading == 'preload') {
308 $script_html .= $this->preload_script();
309 }
310
311 return $script_html;
312 }
313
314 function noscript($script_html)
315 {
316 $script_html .= "<noscript>$this->noscript</noscript>";
317
318 return $script_html;
319 }
320
321 }
322