PluginProbe
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript / 1.9.7
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript v1.9.7
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.7, at inc/photon/class-styleOptimizer.php

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