PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.7.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.7.0
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
← All changes | minify/class-wp-optimize-minify-front-end.php +267 -182 3.5.0 → 4.7.0 View file →
@@ -64,8 +64,14 @@
64 64 if ($this->options['enable_js']) {
65 65 $this->process_js();
66 66 }
67 67
68 + if ($this->options['host_local_google_fonts']) {
69 + // Add filter to replace Goole fonts urls to local if needed
70 + add_filter('wpo_minify_css_string', array($this, 'replace_gfont_urls_with_local'));
71 + add_filter('wpo_minify_get_css', array($this, 'replace_gfont_urls_with_local'));
72 + }
73 +
68 74 if ($this->options['enable_css']) {
69 75 $this->process_css();
70 76 }
71 77
@@ -74,9 +80,9 @@
74 80 add_action('wp_head', array($this, 'add_assets_preload'), 2);
75 81 }
76 82
77 83 if ($this->should_process_html()) {
78 - add_action('template_redirect', array('WP_Optimize_Minify_Functions', 'html_compression_start'), 9);
84 + add_action('template_redirect', array('WP_Optimize_Minify_Functions', 'html_compression_start'));
79 85 }
80 86
81 87 if ($this->should_use_loadCSS()) {
82 88 add_action('wp_footer', array('WP_Optimize_Minify_Print', 'add_load_css'), PHP_INT_MAX);
@@ -85,14 +91,24 @@
85 91 $this->remove_query_string_from_static_assets();
86 92 }
87 93
88 94 /**
95 + * Replace Google fonts urls to local versions
96 + *
97 + * @param string $content
98 + * @return string
99 + */
100 + public function replace_gfont_urls_with_local($content): string {
101 + return WP_Optimize_Host_Google_Fonts::instance()->replace_gfont_urls_with_local($content);
102 + }
103 +
104 + /**
89 105 * Detects whether cache preloading is running or not
90 106 *
91 107 * @return bool
92 108 */
93 109 private function is_cache_preload() {
94 - return isset($_SERVER['HTTP_X_WP_OPTIMIZE_CACHE_PRELOAD']) && 0 === strcmp($_SERVER['HTTP_X_WP_OPTIMIZE_CACHE_PRELOAD'], 'Yes');
110 + return isset($_SERVER['HTTP_X_WP_OPTIMIZE_CACHE_PRELOAD']) && 0 === strcmp(sanitize_text_field(wp_unslash($_SERVER['HTTP_X_WP_OPTIMIZE_CACHE_PRELOAD'])), 'Yes');
95 111 }
96 112
97 113 /**
98 114 * Set initial values for the meta log object
@@ -108,9 +124,9 @@
108 124 );
109 125 }
110 126
111 127 /**
112 - * Wether to run the feature on a page or not
128 + * Whether to run the feature on a page or not
113 129 *
114 130 * @param string $context - Optional, The context where the check is done
115 131 * @return boolean
116 132 */
@@ -115,14 +131,14 @@
115 131 * @return boolean
116 132 */
117 133 public function run_on_page($context = 'default') {
118 134 /**
119 - * Filters wether the functionality is ran on the current page.
135 + * Filters whether the functionality is run on the current page.
120 136 *
121 137 * @param boolean $run_on_page
122 138 * @param string $context - Optional, The feature where the check is done
123 139 */
124 - return apply_filters(
140 + $filtered_value = apply_filters(
125 141 'wpo_minify_run_on_page',
126 142 !is_admin()
127 143 && (!defined('SCRIPT_DEBUG') || !SCRIPT_DEBUG)
128 144 && !is_preview()
@@ -131,19 +147,20 @@
131 147 && !(function_exists('is_amp_endpoint') && is_amp_endpoint())
132 148 && !WP_Optimize_Minify_Functions::exclude_contents(),
133 149 $context
134 150 );
151 + return is_bool($filtered_value) ? $filtered_value : false;
135 152 }
136 153
137 154 /**
138 155 * Inline css in place, instead of inlining the large file
139 156 *
140 - * @param String $html
141 - * @param String $handle
142 - * @param String $href
143 - * @param String $media
157 + * @param string $html
158 + * @param string $handle
159 + * @param string $href
160 + * @param mixed $media
144 161 *
145 - * @return String
162 + * @return string|boolean
146 163 */
147 164 public function inline_css($html, $handle, $href, $media) {
148 165 $exclude_css = $this->get_excluded_css_list();
149 166 $ignore_list = WP_Optimize_Minify_Functions::compile_ignore_list($exclude_css);
@@ -154,9 +171,9 @@
154 171 // make sure href is complete
155 172 $href = WP_Optimize_Minify_Functions::get_hurl($href);
156 173
157 174 if ($this->options['debug']) {
158 - echo "<!-- wpo_min DEBUG: Inline CSS processing start $handle / $href -->\n";
175 + echo "<!-- wpo_min DEBUG: Inline CSS processing start ". esc_html($handle) . " / " . esc_html($href) . " -->\n";
159 176 }
160 177
161 178
162 179 // skip all this, if the async css option is enabled
@@ -166,17 +183,16 @@
166 183 // leave conditionals alone
167 184 if (wp_styles()->get_data($handle, 'conditional')) return $html;
168 185
169 186 // mediatype fix for some plugins + remove print mediatypes
170 - if ('screen' == $media
171 - || 'screen, print' == $media
172 - || empty($media)
173 - || is_null($media)
174 - || false == $media
187 + if (!is_string($media)
188 + || '' === $media
189 + || 'screen' === $media
190 + || 'screen, print' === $media
175 191 ) {
176 192 $media = 'all';
177 193 }
178 - if (!empty($this->options['remove_print_mediatypes']) && 'print' == $media) {
194 + if (!empty($this->options['remove_print_mediatypes']) && 'print' === $media) {
179 195 return false;
180 196 }
181 197
182 198 // Exclude specific CSS files from PageSpeedInsights?
@@ -197,12 +213,12 @@
197 213 if (WP_Optimize_Minify_Functions::is_minified_css_js_filename($href)) {
198 214 return $html;
199 215 }
200 216
201 - // check if working with a font awesom link
217 + // check if working with a font awesome link
202 218 if (WP_Optimize_Minify_Functions::is_font_awesome($href)) {
203 219 // font awesome processing, async css
204 - if ('async' == $this->options['fawesome_method']) {
220 + if ('async' === $this->options['fawesome_method']) {
205 221 WP_Optimize_Minify_Print::async_style($href, $media);
206 222 return false;
207 223 } elseif ('exclude' === $this->options['fawesome_method']) {
208 224 // font awesome processing, async and exclude from PageSpeedIndex
@@ -207,17 +223,17 @@
207 223 } elseif ('exclude' === $this->options['fawesome_method']) {
208 224 // font awesome processing, async and exclude from PageSpeedIndex
209 225 WP_Optimize_Minify_Print::exclude_style($href);
210 226 return false;
211 - } elseif ('inline' == $this->options['fawesome_method']) {
227 + } elseif ('inline' === $this->options['fawesome_method']) {
212 228 WP_Optimize_Minify_Print::inline_style($handle, $href);
213 229 return false;
214 230 }
215 231 }
216 232
217 - // Check if working with google font url
233 + // Check if working with Google font url
218 234
219 - if ('fonts.googleapis.com' == parse_url($href, PHP_URL_HOST)) {
235 + if ('fonts.googleapis.com' === wp_parse_url($href, PHP_URL_HOST)) {
220 236 // if option disabled google fonts processing
221 237 if ($this->options['disable_google_fonts_processing']) return $html;
222 238 // check if google fonts should be removed
223 239 if ($this->options['remove_googlefonts']) return false;
@@ -241,9 +257,9 @@
241 257 $json = WP_Optimize_Minify_Cache_Functions::get_transient($tkey);
242 258 if (false === $json) {
243 259 $json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $this->options['enable_css_minification'], 'css', $handle);
244 260 if ($this->options['debug']) {
245 - echo "<!-- wpo_min DEBUG: Uncached file processing now for $handle / $href -->\n";
261 + echo "<!-- wpo_min DEBUG: Uncached file processing now for " . esc_html($handle) . " / " . esc_html($href) . " -->\n";
246 262 }
247 263 WP_Optimize_Minify_Cache_Functions::set_transient($tkey, $json);
248 264 }
249 265
@@ -250,18 +266,20 @@
250 266 // decode
251 267 $res = json_decode($json, true);
252 268
253 269 // inline css or fail
254 - if (null !== $res && false != $res['status']) {
270 + if (null !== $res && false !== $res['status']) {
255 271 // add font-display
256 272 // https://developers.google.com/web/updates/2016/02/font-display
257 273 $res['code'] = str_ireplace('font-style:normal;', 'font-display:block;font-style:normal;', $res['code']);
258 274
259 - echo '<style class="optimize_css_1" type="text/css" media="all">'.$res['code'].'</style>' . "\n";
275 + echo '<style class="optimize_css_1" type="text/css" media="all">';
276 + echo $res['code']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- This is already escaped
277 + echo '</style>' . "\n";
260 278 return false;
261 279 } else {
262 280 if ($this->options['debug']) {
263 - echo "<!-- wpo_min DEBUG: Google fonts request failed for $href -->\n";
281 + echo "<!-- wpo_min DEBUG: Google fonts request failed for " . esc_html($href) . " -->\n";
264 282 }
265 283 return $html;
266 284 }
267 285 } elseif ('async' === $this->options['gfonts_method']) {
@@ -293,9 +311,9 @@
293 311 $json = WP_Optimize_Minify_Cache_Functions::get_transient($tkey);
294 312 if (false === $json) {
295 313 $json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $this->options['enable_css_minification'], 'css', $handle);
296 314 if ($this->options['debug']) {
297 - echo "<!-- wpo_min DEBUG: Uncached file processing now for $handle / $href -->" . "\n";
315 + echo "<!-- wpo_min DEBUG: Uncached file processing now for " . esc_html($handle) . " / " . esc_html($href) . " -->" . "\n";
298 316 }
299 317 WP_Optimize_Minify_Cache_Functions::set_transient($tkey, $json);
300 318 }
301 319
@@ -302,21 +320,25 @@
302 320 // decode
303 321 $res = json_decode($json, true);
304 322
305 323 // inline it + other inlined children styles
306 - if (null !== $res && false != $res['status']) {
307 - echo '<style class="optimize_css_2" type="text/css" media="'.$media.'">'.$res['code'].'</style>' . "\n";
324 + if (null !== $res && $res['status']) {
325 + echo '<style class="optimize_css_2" type="text/css" media="'.esc_attr($media).'">';
326 + echo $res['code']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- This is already escaped
327 + echo '</style>' . "\n";
308 328
309 329 // get inline_styles for this handle, minify and print
310 330 $inline_styles = array();
311 331 $inline_styles = wp_styles()->get_data($handle, 'after');
312 - if (false != $inline_styles) {
332 + if ($inline_styles) {
313 333
314 334 // string type
315 335 if (is_string($inline_styles)) {
316 336 $code = WP_Optimize_Minify_Functions::get_css($href, $inline_styles, $this->options['enable_css_minification']);
317 - if (!empty($code) && false != $code) {
318 - echo '<style class="optimize_css_3" type="text/css" media="'.$media.'">'.$code.'</style>' . "\n";
337 + if (!empty($code)) {
338 + echo '<style class="optimize_css_3" type="text/css" media="'.esc_attr($media).'">';
339 + echo $code; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- This is already escaped
340 + echo '</style>' . "\n";
319 341 }
320 342 }
321 343
322 344 // array type
@@ -322,10 +344,12 @@
322 344 // array type
323 345 if (is_array($inline_styles)) {
324 346 foreach ($inline_styles as $st) {
325 347 $code = WP_Optimize_Minify_Functions::get_css($href, $st, $this->options['enable_css_minification']);
326 - if (!empty($code) && false != $code) {
327 - echo '<style class="optimize_css_4" type="text/css" media="'.$media.'">'.$code.'</style>' . "\n";
348 + if (!empty($code)) {
349 + echo '<style class="optimize_css_4" type="text/css" media="'.esc_attr($media).'">';
350 + echo $code; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- This is already escaped
351 + echo '</style>' . "\n";
328 352 }
329 353 }
330 354 }
331 355 }
@@ -333,9 +357,9 @@
333 357 // prevent default
334 358 return false;
335 359 } else {
336 360 if ($this->options['debug']) {
337 - echo "<!-- wpo_min DEBUG: $handle / $href returned an empty from minification -->" . "\n";
361 + echo "<!-- wpo_min DEBUG: " . esc_html($handle) . " / " . esc_html($href) . " returned an empty from minification -->" . "\n";
338 362 }
339 363 return $html;
340 364 }
341 365 echo "<!-- ERROR: WPO-Minify couldn't catch the CSS file below. Please report this on https://wordpress.org/support/plugin/wp-optimize/ -->\n";
@@ -342,15 +366,15 @@
342 366 return $html;
343 367 }
344 368
345 369 /**
346 - * Enable defer for JavaScript (WP 4.1 and above) and remove query strings for ignored files
370 + * Enable defer for JavaScript and remove query strings for ignored and all non-minified files
347 371 *
348 - * @param String $tag
349 - * @param String $handle
350 - * @param String $src
372 + * @param string $tag
373 + * @param string $handle
374 + * @param string $src
351 375 *
352 - * @return String
376 + * @return string
353 377 */
354 378 public function defer_js($tag, $handle, $src) {
355 379 $wp_domain = trim(str_ireplace(array('http://', 'https://'), '', trim(site_url(), '/')));
356 380 $exclude_js = $this->get_excluded_js_list();
@@ -356,10 +380,11 @@
356 380 $exclude_js = $this->get_excluded_js_list();
357 381 $ignore_list = WP_Optimize_Minify_Functions::compile_ignore_list($exclude_js);
358 382 // Should this defer the Poly fills for IE?
359 383 $blacklist = WP_Optimize_Minify_Functions::get_ie_blacklist();
384 +
360 385 // no query strings
361 - if (false !== stripos($src, '?ver')) {
386 + if (false !== stripos($src, '?ver') && WP_Optimize_Minify_Functions::is_already_minified($src)) {
362 387 $srcf = stristr($src, '?ver', true);
363 388 $tag = str_ireplace($src, $srcf, $tag);
364 389 $src = $srcf;
365 390 }
@@ -394,9 +419,9 @@
394 419 }
395 420
396 421 // should we exclude defer on the login page?
397 422 if ($this->options['exclude_defer_login']
398 - && false !== stripos($_SERVER["SCRIPT_NAME"], strrchr(wp_login_url(), '/'))
423 + && isset($_SERVER['SCRIPT_NAME']) && false !== stripos(sanitize_text_field(wp_unslash($_SERVER["SCRIPT_NAME"])), strrchr(wp_login_url(), '/'))
399 424 ) {
400 425 return $tag;
401 426 }
402 427
@@ -446,11 +471,15 @@
446 471 * @return void
447 472 */
448 473 public function add_critical_path() {
449 474 if (is_front_page() && !empty($this->options['critical_path_css_is_front_page'])) {
450 - echo '<style id="critical-path-is-front-page" type="text/css" media="all">' . "\n" . $this->options['critical_path_css_is_front_page'] . "\n" . '</style>' . "\n";
475 + echo '<style id="critical-path-is-front-page" type="text/css" media="all">' . "\n";
476 + echo $this->options['critical_path_css_is_front_page'] . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- This is already escaped
477 + echo '</style>' . "\n";
451 478 } elseif (!empty($this->options['critical_path_css'])) {
452 - echo '<style id="critical-path-global" type="text/css" media="all">' . "\n" . $this->options['critical_path_css'] . "\n" . '</style>' . "\n";
479 + echo '<style id="critical-path-global" type="text/css" media="all">' . "\n";
480 + echo $this->options['critical_path_css'] . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- This is already escaped
481 + echo '</style>' . "\n";
453 482 }
454 483 }
455 484
456 485 /**
@@ -520,9 +549,9 @@
520 549 }
521 550
522 551 // mediatype
523 552 $mt = isset($wp_styles->registered[$handle]->args) ? $wp_styles->registered[$handle]->args : 'all';
524 - if ('screen' == $mt || 'screen, print' == $mt || empty($mt) || is_null($mt) || false == $mt) {
553 + if ('screen' === $mt || 'screen, print' === $mt || empty($mt) || is_null($mt) || false === $mt) {
525 554 $mt = 'all';
526 555 }
527 556 $mediatype = $mt;
528 557
@@ -544,11 +573,15 @@
544 573 } else {
545 574 $uniq[$key] = $handle;
546 575 }
547 576 }
577 +
578 + $version = '?ver=' . (!empty($version) ? $version : $wp_styles->default_version);
579 + $href_versioned = $href . $version;
580 +
548 581 // Exclude specific CSS files from PageSpeedIndex?
549 582 if (WP_Optimize_Minify_Functions::in_arrayi($href, $async_css)) {
550 - WP_Optimize_Minify_Print::exclude_style($href);
583 + WP_Optimize_Minify_Print::exclude_style($href_versioned);
551 584 $done = array_merge($done, array($handle));
552 585 continue;
553 586 }
554 587 // Fonts Awesome Processing
@@ -553,17 +586,17 @@
553 586 }
554 587 // Fonts Awesome Processing
555 588 if (WP_Optimize_Minify_Functions::is_font_awesome($href)) {
556 589 if ('inline' === $this->options['fawesome_method']) {
557 - WP_Optimize_Minify_Print::inline_style($handle, $href);
590 + WP_Optimize_Minify_Print::inline_style($handle, $href_versioned);
558 591 $done = array_merge($done, array($handle));
559 592 continue;
560 593 } elseif ('async' === $this->options['fawesome_method']) {
561 - WP_Optimize_Minify_Print::async_style($href, $mediatype);
594 + WP_Optimize_Minify_Print::async_style($href_versioned, $mediatype);
562 595 $done = array_merge($done, array($handle));
563 596 continue;
564 597 } elseif ('exclude' === $this->options['fawesome_method']) {
565 - WP_Optimize_Minify_Print::exclude_style($href);
598 + WP_Optimize_Minify_Print::exclude_style($href_versioned);
566 599 $done = array_merge($done, array($handle));
567 600 continue;
568 601 }
569 602 }
@@ -582,9 +615,9 @@
582 615 'mediatype' => $mediatype
583 616 );
584 617
585 618 // google fonts to the top (collect and skip process array)
586 - if (WP_Optimize_Minify_Functions::is_google_font($href)) {
619 + if (!$disable_google_fonts_processing && WP_Optimize_Minify_Functions::is_google_font($href)) {
587 620 if ($this->options['remove_googlefonts']) {
588 621 $done = array_merge($done, array($handle));
589 622 continue;
590 623 }
@@ -603,9 +636,9 @@
603 636 // merge google fonts if force inlining is enabled?
604 637 $nfonts = array();
605 638 if ($this->options['merge_google_fonts']) {
606 639 $nfonts[] = WP_Optimize_Minify_Fonts::concatenate_google_fonts($google_fonts);
607 - // mark the google fonts as done so they don't get processed
640 + // mark the Google fonts as done so they don't get processed
608 641 if ('inherit' !== $this->options['gfonts_method']) {
609 642 $done = array_merge($done, array_keys($google_fonts));
610 643 }
611 644 } else {
@@ -627,9 +660,9 @@
627 660 // check if the cache is empty or has no code
628 661 if (null === $json || false === $json || empty($json['code'])) {
629 662 $res = WP_Optimize_Minify_Functions::download_and_minify($href, null, $minify_css, 'css', null);
630 663 if ($this->options['debug']) {
631 - echo "<!-- wpo_min DEBUG: Uncached file processing now for $href -->\n";
664 + echo "<!-- wpo_min DEBUG: Uncached file processing now for " . esc_html($href) . " -->\n";
632 665 }
633 666 WP_Optimize_Minify_Cache_Functions::set_transient($tkey, $res);
634 667 // decode
635 668 $json = json_decode($res, true);
@@ -639,14 +672,16 @@
639 672 if (null !== $json && !empty($json['code'])) {
640 673 // add font-display
641 674 // https://developers.google.com/web/updates/2016/02/font-display
642 675 $json['code'] = str_ireplace('font-style:normal;', 'font-display:block;font-style:normal;', $json['code']);
643 - echo '<style type="text/css" media="all">'.$json['code'].'</style>' . "\n";
676 + echo '<style type="text/css" media="all">';
677 + echo $json['code']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- This is already escaped
678 + echo '</style>' . "\n";
644 679 $done = array_merge($done, array($handle));
645 680 } else {
646 - echo "<!-- GOOGLE FONTS REQUEST FAILED for $href -->" . "\n";
681 + echo "<!-- GOOGLE FONTS REQUEST FAILED for " . esc_html($href) . " -->" . "\n";
647 682 // inlining failed, so enqueue again
648 - wp_enqueue_style($handle, $href, array(), null);
683 + wp_enqueue_style($handle, $href, array(), null); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- Not needed as this is a google font and from cache
649 684 }
650 685 } elseif ('async' === $this->options['gfonts_method']) {
651 686 WP_Optimize_Minify_Print::async_style($href);
652 687 $done = array_merge($done, array($handle));
@@ -695,12 +730,12 @@
695 730 $normal_processing = $exclude_minified_css && !$merge_css;
696 731 // skip ignore list, conditional css, external css, font-awesome merge and already minified js
697 732 if (($process_css && !$normal_processing && !WP_Optimize_Minify_Functions::in_arrayi($href, $ignore_list) && !isset($conditional) && WP_Optimize_Minify_Functions::internal_url($href, site_url()))
698 733 || empty($href)
699 - || ($process_css && 'inherit' == $this->options['fawesome_method'] && WP_Optimize_Minify_Functions::is_font_awesome($href))
700 - || ($process_css && !$disable_google_fonts_processing && 'inherit' == $this->options['gfonts_method'] && WP_Optimize_Minify_Functions::is_google_font($href))
734 + || ($process_css && 'inherit' === $this->options['fawesome_method'] && WP_Optimize_Minify_Functions::is_font_awesome($href))
735 + || ($process_css && !$disable_google_fonts_processing && 'inherit' === $this->options['gfonts_method'] && WP_Optimize_Minify_Functions::is_google_font($href))
701 736 ) {
702 - // colect inline css for this handle
737 + // collect inline css for this handle
703 738 if (isset($wp_styles->registered[$handle]->extra['after']) && is_array($wp_styles->registered[$handle]->extra['after'])) {
704 739 $inline_css[$handle] = WP_Optimize_Minify_Functions::minify_css_string(implode('', $wp_styles->registered[$handle]->extra['after'])); // save
705 740 $wp_styles->registered[$handle]->extra['after'] = null; // dequeue
706 741 }
@@ -705,9 +740,9 @@
705 740 $wp_styles->registered[$handle]->extra['after'] = null; // dequeue
706 741 }
707 742
708 743 // process
709 - if (isset($header[count($header)-1]['handle']) || count($header) == 0 || $header[count($header)-1]['media'] != $mediatype || !$merge_css) {
744 + if (isset($header[count($header)-1]['handle']) || count($header) === 0 || $header[count($header)-1]['media'] !== $mediatype || !$merge_css) {
710 745 array_push($header, array('handles' => array(), 'media' => $mediatype, 'versions' => array()));
711 746 }
712 747
713 748 // push it to the array
@@ -737,23 +772,29 @@
737 772 $header = apply_filters('wpo_minify_stylesheets', $header, 'header');
738 773
739 774 // loop through header css and merge
740 775 for ($i=0,$l=count($header); $i<$l; $i++) {
776 +
741 777 if (!isset($header[$i]['handle'])) {
778 +
779 + $handle_str = is_array($header[$i]['handles']) ? $this->array_to_string_conversion($header[$i]['handles']) : strval($header[$i]['handles']);
780 + $last_modified_str = empty($header[$i]['last_modified']) ? '' : (is_array($header[$i]['last_modified']) ? $this->array_to_string_conversion($header[$i]['last_modified']) : strval($header[$i]['last_modified']));
781 +
742 782 if ($merge_css) {
743 783 // get hash for the inline css in this group
744 784 $inline_css_group = array();
745 - foreach ($header[$i]['handles'] as $h) {
746 - if (!empty($inline_css) && isset($inline_css[$h]) && !empty($inline_css[$h])) {
747 - $inline_css_group[] = $inline_css[$h];
785 +
786 + if (is_array($header[$i]['handles'])) {
787 + foreach ($header[$i]['handles'] as $h) {
788 + if (!empty($inline_css) && isset($inline_css[$h]) && !empty($inline_css[$h])) {
789 + $inline_css_group[] = $inline_css[$h];
790 + }
748 791 }
749 792 }
750 793 $inline_css_hash = md5(implode('', $inline_css_group));
751 - $handle_str = $this->array_to_string_conversion($header[$i]['handles']);
752 - $last_modified_str = $this->array_to_string_conversion($header[$i]['last_modified']);
753 794 $hash = hash('adler32', $handle_str . $inline_css_hash . $last_modified_str);
754 795 } else {
755 - $hash = implode('', $header[$i]['handles']) . implode('', $header[$i]['last_modified']);
796 + $hash = sanitize_file_name($handle_str . $last_modified_str);
756 797 }
757 798
758 799 // static cache file info
759 800 $file_name = 'wpo-minify-header-'.$hash.($minify_css ? '.min' : '');
@@ -767,9 +808,9 @@
767 808 clearstatcache();
768 809 if (!file_exists($file)) {
769 810
770 811 // code and log initialization
771 - $log_header = "PROCESSED on ".date('r')." from ".home_url(add_query_arg(null, null));
812 + $log_header = $this->get_log_header();
772 813 $log = array(
773 814 'header' => $log_header,
774 815 'files' => array()
775 816 );
@@ -796,9 +837,9 @@
796 837 if (false === $json) {
797 838 $enable_minification = $this->options['enable_css_minification'] && !WP_Optimize_Minify_Functions::is_minified_css_js_filename($href);
798 839 $json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $enable_minification, 'css', $handle, $version);
799 840 if ($this->options['debug']) {
800 - echo "<!-- wpo_min DEBUG: Uncached file processing now for $handle / $href / $version -->" . "\n";
841 + echo "<!-- wpo_min DEBUG: Uncached file processing now for " . esc_html($handle) . " / " . esc_html($href) . " / " . esc_html($version) . " -->" . "\n";
801 842 }
802 843 WP_Optimize_Minify_Cache_Functions::set_transient($tkey, $json);
803 844 }
804 845
@@ -814,9 +855,9 @@
814 855 $meta_log['files_that_changed'][$handle] = $res['log'];
815 856 }
816 857
817 858 // response has failed
818 - if (true != $res['status']) {
859 + if (true !== $res['status']) {
819 860 $log['files'][$handle] = $res['log'];
820 861 $log['files'][$handle]['version'] = $version;
821 862 $log['files'][$handle]['last_modified'] = $last_modified;
822 863 continue;
@@ -824,9 +865,9 @@
824 865
825 866 // Only add the $handle to $done if it was successfully downloaded
826 867 $done[] = $handle;
827 868 // append code to merged file
828 - $code .= isset($res['code']) ? $res['code'] : '';
869 + $code .= $res['code'] ?? '';
829 870 $log['files'][$handle] = $res['log'];
830 871 $log['files'][$handle]['version'] = $version;
831 872 $log['files'][$handle]['last_modified'] = $last_modified;
832 873
@@ -845,9 +886,9 @@
845 886 // generate cache, write log
846 887 if (!empty($code)) {
847 888 // Only generate meta.json file if cache is reset
848 889 if (!empty($meta_log['invalidation_reason'])) {
849 - file_put_contents($meta_json_file, json_encode($meta_log));
890 + file_put_contents($meta_json_file, wp_json_encode($meta_log));
850 891 }
851 892 WP_Optimize_Minify_Print::write_combined_asset($file, $code, $log);
852 893 }
853 894 } else {
@@ -888,9 +929,9 @@
888 929 }
889 930 }
890 931 } else {
891 932 // file could not be generated, output something meaningful
892 - echo "<!-- ERROR: WP-Optimize Minify was not allowed to save its cache on - ".str_replace(ABSPATH, '', $file)." -->";
933 + echo "<!-- ERROR: WP-Optimize Minify was not allowed to save its cache on - ".esc_html(str_replace(ABSPATH, '', $file))." -->";
893 934 echo "<!-- Please check if the path above is correct and ensure your server has write permission there! -->";
894 935 }
895 936 }
896 937 // other css need to be requeued for the order of files to be kept
@@ -948,9 +989,9 @@
948 989 continue;
949 990 }
950 991
951 992 // Exclude JS files from PageSpeedIndex (Async) takes priority over the ignore list
952 - if (false != $async_js || is_array($async_js)) {
993 + if (!empty($async_js)) {
953 994
954 995 // check for string match
955 996 $skipjs = false;
956 997 foreach ($async_js as $l) {
@@ -955,15 +996,16 @@
955 996 $skipjs = false;
956 997 foreach ($async_js as $l) {
957 998 if (stripos($href, $l) !== false) {
958 999 // print code if there are no linebreaks, or return
959 - WP_Optimize_Minify_Print::async_script($href);
1000 + $href_versioned = $href . (!empty($wp_scripts->registered[$handle]->ver) ? '?ver=' . $wp_scripts->registered[$handle]->ver : '');
1001 + WP_Optimize_Minify_Print::async_script($href_versioned);
960 1002 $skipjs = true;
961 1003 $done = array_merge($done, array($handle));
962 1004 break;
963 1005 }
964 1006 }
965 - if (false != $skipjs) {
1007 + if (false !== $skipjs) {
966 1008 continue;
967 1009 }
968 1010 }
969 1011
@@ -985,10 +1027,10 @@
985 1027 // process
986 1028 if (isset($footer[count($footer)-1]['handle'])
987 1029 || !count($footer)
988 1030 || !$merge_js
989 - || (isset($scripts->registered[$handle]->extra['strategy']) && $previous_load_strategy != $scripts->registered[$handle]->extra['strategy'])
990 - || (!isset($scripts->registered[$handle]->extra['strategy']) && null != $previous_load_strategy)
1031 + || (isset($scripts->registered[$handle]->extra['strategy']) && $previous_load_strategy !== $scripts->registered[$handle]->extra['strategy'])
1032 + || (!isset($scripts->registered[$handle]->extra['strategy']) && null !== $previous_load_strategy)
991 1033 ) {
992 1034 array_push($footer, array('handles' => array(), 'versions' => array()));
993 1035 }
994 1036
@@ -1006,14 +1048,10 @@
1006 1048 if (isset($scripts->registered[$handle]->extra['strategy'])) $footer[count($footer)-1]['strategy'] = $scripts->registered[$handle]->extra['strategy'];
1007 1049
1008 1050 $footer[count($footer) - 1]['last_modified'][] = WP_Optimize_Minify_Functions::get_modification_time($wp_scripts->registered[$handle]->src);
1009 1051 // external and ignored scripts
1010 -
1011 - if (isset($scripts->registered[$handle]->extra['strategy'])) {
1012 - $previous_load_strategy = $scripts->registered[$handle]->extra['strategy'];
1013 - } else {
1014 - $previous_load_strategy = null;
1015 - }
1052 +
1053 + $previous_load_strategy = $scripts->registered[$handle]->extra['strategy'] ?? null;
1016 1054
1017 1055 } else {
1018 1056 array_push($footer, array('handle' => $handle));
1019 1057 }
@@ -1020,15 +1058,19 @@
1020 1058 endforeach;
1021 1059
1022 1060 // loop through footer scripts and merge
1023 1061 for ($i=0,$l=count($footer); $i<$l; $i++) {
1062 +
1024 1063 if (!isset($footer[$i]['handle'])) {
1025 1064
1065 + $handles_str = is_array($footer[$i]['handles']) ? $this->array_to_string_conversion($footer[$i]['handles']) : strval($footer[$i]['handles']);
1066 + $last_modified_str = empty($footer[$i]['last_modified']) ? '' : (is_array($footer[$i]['last_modified']) ? $this->array_to_string_conversion($footer[$i]['last_modified']) : strval($footer[$i]['last_modified']));
1067 +
1026 1068 if ($merge_js) {
1027 1069 // Change the hash based on last modified timestamp
1028 - $hash = hash('adler32', implode('', $footer[$i]['handles']) . implode('', $footer[$i]['last_modified']));
1070 + $hash = hash('adler32', $handles_str . $last_modified_str);
1029 1071 } else {
1030 - $hash = implode('', $footer[$i]['handles']) . implode('', $footer[$i]['last_modified']);
1072 + $hash = sanitize_file_name($handles_str . $last_modified_str);
1031 1073 }
1032 1074
1033 1075 // static cache file info
1034 1076 $file_name = 'wpo-minify-footer-'.$hash.($minify_js ? '.min' : '');
@@ -1042,9 +1084,9 @@
1042 1084 clearstatcache();
1043 1085 if (!file_exists($file)) {
1044 1086
1045 1087 // code and log initialization
1046 - $log_header = "PROCESSED on ".date('r')." from ".home_url(add_query_arg(null, null));
1088 + $log_header = $this->get_log_header();
1047 1089 $log = array(
1048 1090 'header' => $log_header,
1049 1091 'files' => array()
1050 1092 );
@@ -1075,9 +1117,9 @@
1075 1117 if (false === $json) {
1076 1118 $enable_minification = $minify_js && !WP_Optimize_Minify_Functions::is_minified_css_js_filename($href);
1077 1119 $json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $enable_minification, 'js', $handle, $version);
1078 1120 if ($this->options['debug']) {
1079 - echo "<!-- wpo_min DEBUG: Uncached file processing now for $handle / $href / $version -->\n";
1121 + echo "<!-- wpo_min DEBUG: Uncached file processing now for " . esc_html($handle) . " / " . esc_html($href) . " / " . esc_html($version) . " -->\n";
1080 1122 }
1081 1123 WP_Optimize_Minify_Cache_Functions::set_transient($tkey, $json);
1082 1124 }
1083 1125
@@ -1093,9 +1135,9 @@
1093 1135 $meta_log['files_that_changed'][$handle] = $res['log'];
1094 1136 }
1095 1137
1096 1138 // response has failed
1097 - if (true != $res['status']) {
1139 + if (true !== $res['status']) {
1098 1140 $log['files'][$handle] = $res['log'];
1099 1141 $log['files'][$handle]['uses_loading_strategy'] = isset($footer[$i]['strategy']);
1100 1142 $log['files'][$handle]['version'] = $version;
1101 1143 $log['files'][$handle]['last_modified'] = $last_modified;
@@ -1127,9 +1169,9 @@
1127 1169 }
1128 1170
1129 1171 // append code to merged file
1130 1172 $code .= $before_code . "\n";
1131 - $code .= isset($res['code']) ? $res['code'] : '';
1173 + $code .= $res['code'] ?? '';
1132 1174 $code .= "\n" . $after_code . "\n";
1133 1175 $code = WP_Optimize_Minify_Functions::prepare_merged_js($code, $href);
1134 1176 $log['files'][$handle] = $res['log'];
1135 1177 $log['files'][$handle]['uses_loading_strategy'] = isset($footer[$i]['strategy']);
@@ -1146,9 +1188,9 @@
1146 1188 // generate cache, write log
1147 1189 if (!empty($code)) {
1148 1190 // Only generate meta.json file if cache is reset
1149 1191 if (!empty($meta_log['invalidation_reason'])) {
1150 - file_put_contents($meta_json_file, json_encode($meta_log));
1192 + file_put_contents($meta_json_file, wp_json_encode($meta_log));
1151 1193 }
1152 1194 WP_Optimize_Minify_Print::write_combined_asset($file, $code, $log);
1153 1195 }
1154 1196 } else {
@@ -1172,9 +1214,9 @@
1172 1214 }
1173 1215 }
1174 1216
1175 1217 // register minified file
1176 - wp_register_script("wpo_min-footer-$i", $file_url, array(), null, false);
1218 + wp_register_script("wpo_min-footer-$i", $file_url, array(), null, false); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- Not needed as this is a timestamp based cache
1177 1219
1178 1220 // add all extra data from wp_localize_script
1179 1221 $before_code = '';
1180 1222 $data = array();
@@ -1217,9 +1259,9 @@
1217 1259 wp_script_add_data( "wpo_min-footer-$i", 'strategy', $footer[$i]['strategy']);
1218 1260 }
1219 1261 } else {
1220 1262 // file could not be generated, output something meaningful
1221 - echo "<!-- ERROR: WP-Optimize Minify was not allowed to save its cache on - ".str_replace(ABSPATH, '', $file)." -->";
1263 + echo "<!-- ERROR: WP-Optimize Minify was not allowed to save its cache on - ".esc_html(str_replace(ABSPATH, '', $file))." -->";
1222 1264 echo "<!-- Please check if the path above is correct and ensure your server has write permission there! -->";
1223 1265 }
1224 1266
1225 1267 // other scripts need to be requeued for the order of files to be kept
@@ -1234,9 +1276,9 @@
1234 1276 }
1235 1277
1236 1278 /**
1237 1279 * Process header JavaScript
1238 - * Dependant on 'enable_js' option
1280 + * Dependent on 'enable_js' option
1239 1281 *
1240 1282 * @return void
1241 1283 */
1242 1284 public function process_header_scripts() {
@@ -1267,9 +1309,9 @@
1267 1309 // get full url
1268 1310 $href = WP_Optimize_Minify_Functions::get_hurl($wp_scripts->registered[$handle]->src);
1269 1311 // inlined scripts without file
1270 1312 if (empty($href)) {
1271 - wp_enqueue_script($handle, false);
1313 + wp_enqueue_script($handle, false); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- Not needed
1272 1314 continue;
1273 1315 }
1274 1316
1275 1317 // Only go through items without a group
@@ -1278,9 +1320,10 @@
1278 1320 // check for string match
1279 1321 $skipjs = false;
1280 1322 foreach ($async_js as $l) {
1281 1323 if (stripos($href, $l) !== false) {
1282 - WP_Optimize_Minify_Print::async_script($href);
1324 + $href_versioned = $href . (!empty($wp_scripts->registered[$handle]->ver) ? '?ver=' . $wp_scripts->registered[$handle]->ver : '');
1325 + WP_Optimize_Minify_Print::async_script($href_versioned);
1283 1326 $skipjs = true;
1284 1327 $done = array_merge($done, array($handle));
1285 1328 break;
1286 1329 }
@@ -1313,10 +1356,10 @@
1313 1356 // process
1314 1357 if (isset($header[count($header)-1]['handle'])
1315 1358 || !count($header)
1316 1359 || !$merge_js
1317 - || (isset($scripts->registered[$handle]->extra['strategy']) && $previous_load_strategy != $scripts->registered[$handle]->extra['strategy'])
1318 - || (!isset($scripts->registered[$handle]->extra['strategy']) && null != $previous_load_strategy)
1360 + || (isset($scripts->registered[$handle]->extra['strategy']) && $previous_load_strategy !== $scripts->registered[$handle]->extra['strategy'])
1361 + || (!isset($scripts->registered[$handle]->extra['strategy']) && null !== $previous_load_strategy)
1319 1362 ) {
1320 1363 array_push($header, array('handles' => array(), 'versions' => array()));
1321 1364 }
1322 1365
@@ -1354,15 +1397,11 @@
1354 1397 $header[count($header) - 1]['last_modified'][] = WP_Optimize_Minify_Functions::get_modification_time($wp_scripts->registered[$handle]->src);
1355 1398
1356 1399 // Register strategy if it is used
1357 1400 if (isset($scripts->registered[$handle]->extra['strategy'])) $header[count($header)-1]['strategy'] = $scripts->registered[$handle]->extra['strategy'];
1358 -
1359 - if (isset($scripts->registered[$handle]->extra['strategy'])) {
1360 - $previous_load_strategy = $scripts->registered[$handle]->extra['strategy'];
1361 - } else {
1362 - $previous_load_strategy = null;
1363 - }
1364 1401
1402 + $previous_load_strategy = $scripts->registered[$handle]->extra['strategy'] ?? null;
1403 +
1365 1404
1366 1405 // external and ignored scripts
1367 1406 } else {
1368 1407 // add the ignored assets
@@ -1370,20 +1409,24 @@
1370 1409 }
1371 1410
1372 1411 // make sure that the scripts skipped here, show up in the footer
1373 1412 } else {
1374 - wp_enqueue_script($handle, $href, array(), null, true);
1413 + wp_enqueue_script($handle, $href, array(), null, true); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- Not needed as this is a timestamp based cache
1375 1414 }
1376 1415 }
1377 1416
1378 1417 // loop through header scripts and merge
1379 1418 for ($i=0,$l=count($header); $i < $l; $i++) {
1419 +
1380 1420 if (!isset($header[$i]['handle'])) {
1381 -
1421 +
1422 + $handles_str = is_array($header[$i]['handles']) ? $this->array_to_string_conversion($header[$i]['handles']) : strval($header[$i]['handles']);
1423 + $last_modified_str = empty($header[$i]['last_modified']) ? '' : (is_array($header[$i]['last_modified']) ? $this->array_to_string_conversion($header[$i]['last_modified']) : strval($header[$i]['last_modified']));
1424 +
1382 1425 if ($merge_js) {
1383 - $hash = hash('adler32', implode('', $header[$i]['handles']) . implode('', $header[$i]['last_modified']));
1426 + $hash = hash('adler32', $handles_str . $last_modified_str);
1384 1427 } else {
1385 - $hash = implode('', $header[$i]['handles']) . implode('', $header[$i]['last_modified']);
1428 + $hash = sanitize_file_name($handles_str . $last_modified_str);
1386 1429 }
1387 1430
1388 1431 // static cache file info
1389 1432 $file_name = 'wpo-minify-header-'.$hash.($minify_js ? '.min' : '');
@@ -1396,9 +1439,9 @@
1396 1439 clearstatcache();
1397 1440 if (!file_exists($file)) {
1398 1441
1399 1442 // code and log initialization
1400 - $log_header = "PROCESSED on ".date('r')." from ".home_url(add_query_arg(null, null));
1443 + $log_header = $this->get_log_header();
1401 1444 $log = array(
1402 1445 'header' => $log_header,
1403 1446 'files' => array()
1404 1447 );
@@ -1426,9 +1469,9 @@
1426 1469 if (false === $json) {
1427 1470 $enable_minification = $minify_js && !WP_Optimize_Minify_Functions::is_minified_css_js_filename($href);
1428 1471 $json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $enable_minification, 'js', $handle, $version);
1429 1472 if ($this->options['debug']) {
1430 - echo "<!-- wpo_min DEBUG: Uncached file processing now for $handle / $href / $version -->" . "\n";
1473 + echo "<!-- wpo_min DEBUG: Uncached file processing now for " . esc_html($handle) . " / " . esc_html($href) . " / " . esc_html($version) . " -->" . "\n";
1431 1474 }
1432 1475 WP_Optimize_Minify_Cache_Functions::set_transient($tkey, $json);
1433 1476 }
1434 1477
@@ -1444,9 +1487,9 @@
1444 1487 $meta_log['files_that_changed'][$handle] = $res['log'];
1445 1488 }
1446 1489
1447 1490 // response has failed
1448 - if (true != $res['status']) {
1491 + if (true !== $res['status']) {
1449 1492 $log['files'][$handle] = $res['log'];
1450 1493 $log['files'][$handle]['uses_loading_strategy'] = isset($header[$i]['strategy']);
1451 1494 $log['files'][$handle]['version'] = $version;
1452 1495 $log['files'][$handle]['last_modified'] = $last_modified;
@@ -1499,9 +1542,9 @@
1499 1542 // generate cache, write log
1500 1543 if (!empty($code)) {
1501 1544 // Only generate meta.json file if cache is reset
1502 1545 if (!empty($meta_log['invalidation_reason'])) {
1503 - file_put_contents($meta_json_file, json_encode($meta_log));
1546 + file_put_contents($meta_json_file, wp_json_encode($meta_log));
1504 1547 }
1505 1548 WP_Optimize_Minify_Print::write_combined_asset($file, $code, $log);
1506 1549 }
1507 1550 } else {
@@ -1527,9 +1570,9 @@
1527 1570 }
1528 1571
1529 1572 // register minified file
1530 1573 $dependencies = isset($excluded_dependencies[$i]) ? $excluded_dependencies[$i] : array();
1531 - wp_register_script("wpo_min-header-$i", $file_url, $dependencies, null, false);
1574 + wp_register_script("wpo_min-header-$i", $file_url, $dependencies, null, false); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- Not needed as this is a timestamp based cache
1532 1575
1533 1576 // add all extra data from wp_localize_script
1534 1577 $before_code = '';
1535 1578 $data = array();
@@ -1573,9 +1616,9 @@
1573 1616 wp_script_add_data( "wpo_min-header-$i", 'strategy', $header[$i]['strategy']);
1574 1617 }
1575 1618 } else {
1576 1619 // file could not be generated, output something meaningful
1577 - echo "<!-- ERROR: WP-Optimize minify was not allowed to save its cache on - ".str_replace(ABSPATH, '', $file)." -->";
1620 + echo "<!-- ERROR: WP-Optimize minify was not allowed to save its cache on - ".esc_html(str_replace(ABSPATH, '', $file))." -->";
1578 1621 echo "<!-- Please check if the path above is correct and ensure your server has write permission there! -->";
1579 1622 echo "<!-- If you found a bug, please report this on https://wordpress.org/support/plugin/wp-optimize/ -->";
1580 1623 }
1581 1624
@@ -1628,9 +1671,9 @@
1628 1671 $wp_styles->done = $done;
1629 1672 return;
1630 1673 }
1631 1674
1632 - // dequeue and get a list of google fonts, or requeue external
1675 + // dequeue and get a list of Google fonts, or requeue external
1633 1676 foreach ($styles->to_do as $handle) {
1634 1677 $href = WP_Optimize_Minify_Functions::get_hurl($wp_styles->registered[$handle]->src);
1635 1678 // inlined scripts without file
1636 1679 if (empty($href)) continue;
@@ -1697,15 +1740,14 @@
1697 1740 WP_Optimize_Minify_Print::inline_style($handle, $href);
1698 1741 $done = array_merge($done, array($handle));
1699 1742 } else {
1700 1743 // fallback, enqueue google fonts
1701 - wp_enqueue_style($handle, $href, array(), null, 'all');
1744 + wp_enqueue_style($handle, $href, array(), null, 'all'); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- Not needed as this google font and from cache
1702 1745 }
1703 1746 }
1704 1747 }
1705 1748 }
1706 1749
1707 -
1708 1750 // get groups of handles
1709 1751 $uniq = array();
1710 1752 foreach ($styles->to_do as $handle) {
1711 1753
@@ -1718,11 +1760,11 @@
1718 1760 $conditional = null; if (isset($wp_styles->registered[$handle]->extra["conditional"])) {
1719 1761 $conditional = $wp_styles->registered[$handle]->extra["conditional"]; // such as ie7, ie8, ie9, etc
1720 1762 }
1721 1763
1722 - // mediatype
1764 + // media type
1723 1765 $mt = isset($wp_styles->registered[$handle]->args) ? $wp_styles->registered[$handle]->args : 'all';
1724 - if ('screen' == $mt || 'screen, print' == $mt || empty($mt) || is_null($mt) || false == $mt) {
1766 + if ('screen' === $mt || 'screen, print' === $mt || empty($mt)) {
1725 1767 $mt = 'all';
1726 1768 }
1727 1769 $mediatype = $mt;
1728 1770
@@ -1752,11 +1794,14 @@
1752 1794 if ($ieonly) {
1753 1795 continue;
1754 1796 }
1755 1797
1798 + $version = '?ver=' . (!empty($version) ? $version : $wp_styles->default_version);
1799 + $href_versioned = $href . $version;
1800 +
1756 1801 // Exclude specific CSS files from PageSpeedIndex?
1757 - if (false != $async_css && is_array($async_css) && WP_Optimize_Minify_Functions::in_arrayi($href, $async_css)) {
1758 - WP_Optimize_Minify_Print::exclude_style($href);
1802 + if (is_array($async_css) && WP_Optimize_Minify_Functions::in_arrayi($href, $async_css)) {
1803 + WP_Optimize_Minify_Print::exclude_style($href_versioned);
1759 1804 $done = array_merge($done, array($handle));
1760 1805 continue;
1761 1806 }
1762 1807
@@ -1761,17 +1806,17 @@
1761 1806 }
1762 1807
1763 1808 if (WP_Optimize_Minify_Functions::is_font_awesome($href)) {
1764 1809 if ('inline' === $this->options['fawesome_method']) {
1765 - WP_Optimize_Minify_Print::inline_style($handle, $href);
1810 + WP_Optimize_Minify_Print::inline_style($handle, $href_versioned);
1766 1811 $done = array_merge($done, array($handle));
1767 1812 continue;
1768 1813 } elseif ('async' === $this->options['fawesome_method']) {
1769 - WP_Optimize_Minify_Print::async_style($href, $mediatype);
1814 + WP_Optimize_Minify_Print::async_style($href_versioned, $mediatype);
1770 1815 $done = array_merge($done, array($handle));
1771 1816 continue;
1772 1817 } elseif ('exclude' === $this->options['fawesome_method']) {
1773 - WP_Optimize_Minify_Print::exclude_style($href);
1818 + WP_Optimize_Minify_Print::exclude_style($href_versioned);
1774 1819 $done = array_merge($done, array($handle));
1775 1820 continue;
1776 1821 }
1777 1822 }
@@ -1789,9 +1834,9 @@
1789 1834 if (($process_css && !$normal_processing && !WP_Optimize_Minify_Functions::in_arrayi($href, $ignore_list) && !isset($conditional) && WP_Optimize_Minify_Functions::internal_url($href, site_url()))
1790 1835 || empty($href)
1791 1836 ) {
1792 1837
1793 - // colect inline css for this handle
1838 + // collect inline css for this handle
1794 1839 if (isset($wp_styles->registered[$handle]->extra['after']) && is_array($wp_styles->registered[$handle]->extra['after'])) {
1795 1840 $inline_css[$handle] = WP_Optimize_Minify_Functions::minify_css_string(implode('', $wp_styles->registered[$handle]->extra['after'])); // save
1796 1841 $wp_styles->registered[$handle]->extra['after'] = null; // dequeue
1797 1842 }
@@ -1796,9 +1841,9 @@
1796 1841 $wp_styles->registered[$handle]->extra['after'] = null; // dequeue
1797 1842 }
1798 1843
1799 1844 // process
1800 - if (isset($footer[count($footer)-1]['handle']) || !count($footer) || $footer[count($footer)-1]['media'] != $wp_styles->registered[$handle]->args || !$merge_css) {
1845 + if (isset($footer[count($footer)-1]['handle']) || !count($footer) || $footer[count($footer)-1]['media'] !== $wp_styles->registered[$handle]->args || !$merge_css) {
1801 1846 array_push($footer, array('handles' => array(), 'media' => $mediatype, 'versions' => array()));
1802 1847 }
1803 1848
1804 1849 // push it to the array get latest modified time
@@ -1824,21 +1869,28 @@
1824 1869 $footer = apply_filters('wpo_minify_stylesheets', $footer, 'footer');
1825 1870
1826 1871 // loop through footer css and merge
1827 1872 for ($i=0,$l=count($footer); $i<$l; $i++) {
1873 +
1828 1874 if (!isset($footer[$i]['handle'])) {
1875 +
1876 + $handles_str = is_array($footer[$i]['handles']) ? $this->array_to_string_conversion($footer[$i]['handles']) : strval($footer[$i]['handles']);
1877 + $last_modified_str = empty($footer[$i]['last_modified']) ? '' : (is_array($footer[$i]['last_modified']) ? $this->array_to_string_conversion($footer[$i]['last_modified']) : strval($footer[$i]['last_modified']));
1878 +
1829 1879 if ($merge_css) {
1830 1880 // get hash for the inline css in this group
1831 1881 $inline_css_group = array();
1832 - foreach ($footer[$i]['handles'] as $h) {
1833 - if (isset($inline_css[$h]) && !empty($inline_css[$h])) {
1834 - $inline_css_group[] = $inline_css[$h];
1882 + if (is_array($footer[$i]['handles'])) {
1883 + foreach ($footer[$i]['handles'] as $h) {
1884 + if (isset($inline_css[$h]) && !empty($inline_css[$h])) {
1885 + $inline_css_group[] = $inline_css[$h];
1886 + }
1835 1887 }
1836 1888 }
1837 1889 $inline_css_hash = md5(implode('', $inline_css_group));
1838 - $hash = hash('adler32', implode('', $footer[$i]['handles']) . $inline_css_hash . implode('', $footer[$i]['last_modified']));
1890 + $hash = hash('adler32', $handles_str . $inline_css_hash . $last_modified_str);
1839 1891 } else {
1840 - $hash = implode('', $footer[$i]['handles']) . implode('', $footer[$i]['last_modified']);
1892 + $hash = sanitize_file_name($handles_str . $last_modified_str);
1841 1893 }
1842 1894
1843 1895 // static cache file info
1844 1896 $file_name = 'wpo-minify-footer-'.$hash.($minify_css ? '.min' : '');
@@ -1852,9 +1904,9 @@
1852 1904 clearstatcache();
1853 1905 if (!file_exists($file)) {
1854 1906
1855 1907 // code and log initialization
1856 - $log_header = "PROCESSED on ".date('r')." from ".home_url(add_query_arg(null, null));
1908 + $log_header = $this->get_log_header();
1857 1909 $log = array(
1858 1910 'header' => $log_header,
1859 1911 'files' => array()
1860 1912 );
@@ -1869,9 +1921,9 @@
1869 1921 $handle_src = $wp_styles->registered[$handle]->src;
1870 1922 $href = WP_Optimize_Minify_Functions::get_hurl($handle_src);
1871 1923 $version = $wp_styles->registered[$handle]->ver;
1872 1924 $last_modified = WP_Optimize_Minify_Functions::get_modification_time($handle_src);
1873 - // inlined scripts without hreffile
1925 + // inlined scripts without href file
1874 1926 if (empty($href)) continue;
1875 1927
1876 1928 // download, minify, cache
1877 1929 $tkey = 'css-'.hash('adler32', $handle . $href).'.css';
@@ -1880,9 +1932,9 @@
1880 1932 if (false === $json) {
1881 1933 $enable_minification = $minify_css && !WP_Optimize_Minify_Functions::is_minified_css_js_filename($href);
1882 1934 $json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $enable_minification, 'css', $handle, $version);
1883 1935 if ($this->options['debug']) {
1884 - echo "<!-- wpo_min DEBUG: Uncached file processing now for $handle / $href / $version -->" . "\n";
1936 + echo "<!-- wpo_min DEBUG: Uncached file processing now for " . esc_html($handle) . " / " . esc_html($href) . " / " . esc_html($version) . " -->" . "\n";
1885 1937 }
1886 1938 WP_Optimize_Minify_Cache_Functions::set_transient($tkey, $json);
1887 1939 }
1888 1940
@@ -1898,9 +1950,9 @@
1898 1950 $meta_log['files_that_changed'][$handle] = $res['log'];
1899 1951 }
1900 1952
1901 1953 // response has failed
1902 - if (true != $res['status']) {
1954 + if (true !== $res['status']) {
1903 1955 $log['files'][$handle] = $res['log'];
1904 1956 $log['files'][$handle]['version'] = $version;
1905 1957 $log['files'][$handle]['last_modified'] = $last_modified;
1906 1958 continue;
@@ -1908,9 +1960,9 @@
1908 1960
1909 1961 $done[] = $handle;
1910 1962
1911 1963 // append code to merged file
1912 - $code .= isset($res['code']) ? $res['code'] : '';
1964 + $code .= $res['code'] ?? '';
1913 1965 $log['files'][$handle] = $res['log'];
1914 1966 $log['files'][$handle]['version'] = $version;
1915 1967 $log['files'][$handle]['last_modified'] = $last_modified;
1916 1968
@@ -1923,15 +1975,15 @@
1923 1975 } else {
1924 1976 wp_dequeue_script($handle);
1925 1977 wp_enqueue_script($handle);
1926 1978 }
1927 - };
1979 + }
1928 1980
1929 1981 // generate cache, add inline css, write log
1930 1982 if (!empty($code)) {
1931 1983 // Only generate meta.json file if cache is reset
1932 1984 if (!empty($meta_log['invalidation_reason'])) {
1933 - file_put_contents($meta_json_file, json_encode($meta_log));
1985 + file_put_contents($meta_json_file, wp_json_encode($meta_log));
1934 1986 }
1935 1987 WP_Optimize_Minify_Print::write_combined_asset($file, $code, $log);
1936 1988 }
1937 1989 } else {
@@ -1967,13 +2019,13 @@
1967 2019 if (filesize($file) < 20000 && $this->options['inline_css']) {
1968 2020 $this->inline_css(file_get_contents($file), $handle, $file_url, $footer[$i]['media']);
1969 2021 } else {
1970 2022 // enqueue it
1971 - wp_enqueue_style("wpo_min-footer-$i", $file_url, array(), null, $footer[$i]['media']);
2023 + wp_enqueue_style("wpo_min-footer-$i", $file_url, array(), null, $footer[$i]['media']); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- Not needed as this is a timestamp based cache
1972 2024 }
1973 2025 } else {
1974 2026 // file could not be generated, output something meaningful
1975 - echo "<!-- ERROR: WP-Optimize Minify was not allowed to save its cache on - ".str_replace(ABSPATH, '', $file)." -->";
2027 + echo "<!-- ERROR: WP-Optimize Minify was not allowed to save its cache on - ".esc_html(str_replace(ABSPATH, '', $file))." -->";
1976 2028 echo "<!-- Please check if the path above is correct and ensure your server has write permission there! -->";
1977 2029 }
1978 2030 }
1979 2031
@@ -1990,11 +2042,11 @@
1990 2042
1991 2043 /**
1992 2044 * Process JS Scripts of type module
1993 2045 *
1994 - * @param String $buffer Page HTML.
2046 + * @param string $buffer Page HTML.
1995 2047 *
1996 - * @return String
2048 + * @return string
1997 2049 */
1998 2050 public function process_script_modules($buffer) {
1999 2051
2000 2052 if ('' === $buffer) return '';
@@ -2013,8 +2065,19 @@
2013 2065 foreach ($scripts as $script) {
2014 2066 if ("module" !== $script['type']) continue;
2015 2067
2016 2068 $href = WP_Optimize_Minify_Functions::get_hurl($script['url']);
2069 + if (WP_Optimize_Minify_Functions::is_minified_css_js_filename($href)) continue;
2070 +
2071 + $asset_content = WP_Optimize_Minify_Functions::get_asset_content($href);
2072 + $code = $asset_content['content'];
2073 +
2074 +
2075 + if (WP_Optimize_Minify_Functions::has_js_import_statements($code)) {
2076 + // If the module script contains import statements, we cannot safely minify or cache it, so skip it.
2077 + continue;
2078 + }
2079 +
2017 2080 $last_modified = WP_Optimize_Minify_Functions::get_modification_time($script['url']);
2018 2081 $handle = $script['handle'];
2019 2082 $version = $script['version'];
2020 2083
@@ -2026,9 +2089,9 @@
2026 2089 continue;
2027 2090 }
2028 2091
2029 2092 // code and log initialization
2030 - $log_header = "PROCESSED on ".date('r')." from ".home_url(add_query_arg(null, null));
2093 + $log_header = $this->get_log_header();
2031 2094 $log = array(
2032 2095 'header' => $log_header,
2033 2096 'files' => array()
2034 2097 );
@@ -2047,9 +2110,9 @@
2047 2110 if (false === $json) {
2048 2111 $enable_minification = $minify_js && !WP_Optimize_Minify_Functions::is_minified_css_js_filename($href);
2049 2112 $json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $enable_minification, 'js', $handle_no_slash, $version);
2050 2113 if ($this->options['debug']) {
2051 - echo "<!-- wpo_min DEBUG: Uncached file processing now for $handle / $href / $version -->\n";
2114 + $buffer .= "<!-- wpo_min DEBUG: Uncached file processing now for " . esc_html($handle) . " / " . esc_html($href) . " / " . esc_html($version) . " -->\n";
2052 2115 }
2053 2116 WP_Optimize_Minify_Cache_Functions::set_transient($tkey, $json);
2054 2117 }
2055 2118
@@ -2067,9 +2130,9 @@
2067 2130 }
2068 2131
2069 2132 if (!$res['status']) continue;
2070 2133
2071 - $code .= isset($res['code']) ? $res['code'] : '';
2134 + $code .= $res['code'] ?? '';
2072 2135 $code = WP_Optimize_Minify_Functions::prepare_merged_js($code, $href);
2073 2136 $log['files'][$handle] = $res['log'];
2074 2137 $log['files'][$handle]['version'] = $version;
2075 2138 $log['files'][$handle]['last_modified'] = $last_modified;
@@ -2078,9 +2141,9 @@
2078 2141 if (empty($code)) continue;
2079 2142
2080 2143 // Only generate meta.json file if cache is reset
2081 2144 if (!empty($meta_log['invalidation_reason'])) {
2082 - file_put_contents($meta_json_file, json_encode($meta_log));
2145 + file_put_contents($meta_json_file, wp_json_encode($meta_log));
2083 2146 }
2084 2147 WP_Optimize_Minify_Print::write_combined_asset($file, $code, $log);
2085 2148
2086 2149 // Extract importmap script tag
@@ -2086,17 +2149,14 @@
2086 2149 // Extract importmap script tag
2087 2150 $json_map = $this->get_script_module_importmap($buffer);
2088 2151 $json_map_update = $json_map;
2089 2152
2090 - if (!empty($json_map) && isset($json_map['imports'])) {
2153 + // Update module source file and importmap with new file url (if necessary)
2154 + $buffer = str_replace($script['url'], $file_url, $buffer);
2155 + if (!empty($json_map) && isset($json_map['imports'][$handle])) {
2091 2156 $json_map_update['imports'][$handle] = $file_url;
2092 - } else {
2093 - continue;
2157 + $buffer = str_replace(wp_json_encode($json_map), wp_json_encode($json_map_update), $buffer);
2094 2158 }
2095 -
2096 - // Update module source file and importmap with new file url
2097 - $buffer = str_replace($script['url'], $file_url, $buffer);
2098 - $buffer = str_replace(json_encode($json_map), json_encode($json_map_update), $buffer);
2099 2159 }
2100 2160
2101 2161 return $buffer;
2102 2162 }
@@ -2146,9 +2206,9 @@
2146 2206 * @return void;
2147 2207 */
2148 2208 public function add_google_fonts_merged() {
2149 2209 // must have something to do
2150 - if (count($this->collect_google_fonts) == 0) return;
2210 + if (count($this->collect_google_fonts) === 0) return;
2151 2211
2152 2212 // merge google fonts
2153 2213 $href = WP_Optimize_Minify_Fonts::concatenate_google_fonts($this->collect_google_fonts);
2154 2214 if (empty($href)) {
@@ -2194,14 +2254,19 @@
2194 2254 // always false for admin pages
2195 2255 if (is_admin()) return false;
2196 2256
2197 2257 // get host with multisite support and query strings
2198 - $host = htmlentities($_SERVER['SERVER_NAME']);
2258 + $host = isset($_SERVER['SERVER_NAME']) ? sanitize_text_field(wp_unslash($_SERVER['SERVER_NAME'])) : '';
2199 2259 if (empty($host)) {
2200 - $host = isset($_SERVER['HTTP_HOST']) ? htmlentities($_SERVER['HTTP_HOST']) : '';
2260 + $host = isset($_SERVER['HTTP_HOST']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_HOST'])) : '';
2201 2261 }
2202 - $request_query = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
2203 - $request_uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
2262 +
2263 + $request_query = '';
2264 + $request_uri = isset($_SERVER['REQUEST_URI']) ? esc_url_raw(wp_unslash($_SERVER['REQUEST_URI'])) : '';
2265 + if (!empty($request_uri)) {
2266 + $request_query = wp_parse_url($request_uri, PHP_URL_QUERY);
2267 + $request_uri = wp_parse_url($request_uri, PHP_URL_PATH);
2268 + }
2204 2269
2205 2270 // initialize headers
2206 2271 $headers = array();
2207 2272
@@ -2209,9 +2274,9 @@
2209 2274 if ($this->options['enabled_css_preload']
2210 2275 && count($this->collect_preload_css) > 0
2211 2276 ) {
2212 2277 foreach ($this->collect_preload_css as $u) {
2213 - // filter out footer footer files, because they are not in the critical path
2278 + // filter out footer files, because they are not in the critical path
2214 2279 if (strpos($u, '/assets/wpo-minify-footer-') !== false) {
2215 2280 continue;
2216 2281 }
2217 2282
@@ -2225,9 +2290,9 @@
2225 2290 && count($this->collect_preload_js) > 0
2226 2291 ) {
2227 2292 foreach ($this->collect_preload_js as $u) {
2228 2293
2229 - // filter out footer footer files, because they are not in the critical path
2294 + // filter out footer files, because they are not in the critical path
2230 2295 if (false !== strpos($u, '/assets/wpo-minify-footer-')) {
2231 2296 continue;
2232 2297 }
2233 2298
@@ -2236,9 +2301,9 @@
2236 2301 }
2237 2302 }
2238 2303
2239 2304 // must have something
2240 - if (count($headers) == 0) {
2305 + if (count($headers) === 0) {
2241 2306 return false;
2242 2307 } else {
2243 2308 $headers = implode("\n", $headers);
2244 2309 }
@@ -2254,9 +2319,9 @@
2254 2319 // reset file cache
2255 2320 clearstatcache();
2256 2321
2257 2322 // if there are no query strings
2258 - if ($b == $a) {
2323 + if ($b === $a) {
2259 2324 if (!file_exists($a)) {
2260 2325 WP_Optimize_Minify_Print::write_header($a, $headers);
2261 2326 }
2262 2327 return false;
@@ -2269,13 +2334,13 @@
2269 2334
2270 2335 /**
2271 2336 * Collect all wpo_min JS files and save them to a headers file
2272 2337 *
2273 - * @param String $html
2274 - * @param String $handle
2275 - * @param String $src
2338 + * @param string $html
2339 + * @param string $handle
2340 + * @param string $src
2276 2341 *
2277 - * @return String
2342 + * @return string
2278 2343 */
2279 2344 public function collect_js_preload_headers($html, $handle, $src) {
2280 2345 if (false !== strpos($src, 'assets/wpo-minify')) {
2281 2346 if (!in_array($src, $this->collect_preload_js)) {
@@ -2322,14 +2387,18 @@
2322 2387 return false;
2323 2388 }
2324 2389
2325 2390 // get host with multisite support and query strings
2326 - $host = htmlentities($_SERVER['SERVER_NAME']);
2391 + $host = isset($_SERVER['SERVER_NAME']) ? sanitize_text_field(wp_unslash($_SERVER['SERVER_NAME'])) : '';
2327 2392 if (empty($host)) {
2328 - $host = isset($_SERVER['HTTP_HOST']) ? htmlentities($_SERVER['HTTP_HOST']) : '';
2393 + $host = isset($_SERVER['HTTP_HOST']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_HOST'])) : '';
2329 2394 }
2330 - $request_query = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
2331 - $request_uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
2395 + $request_query = '';
2396 + $request_uri = isset($_SERVER['REQUEST_URI']) ? esc_url_raw(wp_unslash($_SERVER['REQUEST_URI'])) : '';
2397 + if (!empty($request_uri)) {
2398 + $request_query = wp_parse_url($request_uri, PHP_URL_QUERY);
2399 + $request_uri = wp_parse_url($request_uri, PHP_URL_PATH);
2400 + }
2332 2401
2333 2402 // get cache path
2334 2403 $cache_path = WP_Optimize_Minify_Cache_Functions::cache_path();
2335 2404 $header_dir = $cache_path['headerdir'];
@@ -2342,12 +2411,12 @@
2342 2411 // reset file cache
2343 2412 clearstatcache();
2344 2413
2345 2414 // return header files or fallback
2346 - if ($b == $a && file_exists($a)) {
2415 + if ($b === $a && file_exists($a)) {
2347 2416 return file_get_contents($a);
2348 2417 }
2349 - if ($b != $a && file_exists($b)) {
2418 + if ($b !== $a && file_exists($b)) {
2350 2419 return file_get_contents($b);
2351 2420 }
2352 2421
2353 2422 return false;
@@ -2353,9 +2422,9 @@
2353 2422 return false;
2354 2423 }
2355 2424
2356 2425 /**
2357 - * Add pre-connect and pre-load headers
2426 + * Add pre-connect and preload headers
2358 2427 *
2359 2428 * @return void
2360 2429 */
2361 2430 public function extra_preload_headers() {
@@ -2364,9 +2433,9 @@
2364 2433 // fetch headers
2365 2434 $pre_connect = $this->get_pre_connect_list();
2366 2435
2367 2436 // preload
2368 - if (is_array($pre_connect) && count($pre_connect) > 0) {
2437 + if (count($pre_connect) > 0) {
2369 2438 foreach ($pre_connect as $url) {
2370 2439 if (!empty($url) && filter_var($url, FILTER_VALIDATE_URL)) {
2371 2440 header("Link: <$url>; rel=preconnect", false);
2372 2441 }
@@ -2373,9 +2442,9 @@
2373 2442 }
2374 2443 }
2375 2444
2376 2445 $headers = $this->get_preload_headers();
2377 - if (false != $headers) {
2446 + if ($headers) {
2378 2447 $nh = array_map('trim', explode("\n", $headers));
2379 2448 foreach ($nh as $h) {
2380 2449 if (!empty($h)) {
2381 2450 header($h, false);
@@ -2434,15 +2503,15 @@
2434 2503 /**
2435 2504 * Handles header meta information removal
2436 2505 */
2437 2506 private function remove_header_meta_info() {
2438 - // no resource hints, generator tag, shortlinks, manifest link, etc
2507 + // no resource hints, generator tag, shortlink, manifest link, etc
2439 2508 remove_action('wp_head', 'wp_resource_hints', 2);
2440 2509 remove_action('wp_head', 'wp_generator');
2441 2510 remove_action('template_redirect', 'wp_shortlink_header', 11);
2442 2511 remove_action('wp_head', 'wlwmanifest_link');
2443 2512 remove_action('wp_head', 'rsd_link');
2444 - remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
2513 + remove_action('wp_head', 'adjacent_posts_rel_link_wp_head');
2445 2514 remove_action('wp_head', 'feed_links', 2);
2446 2515 remove_action('wp_head', 'feed_links_extra', 3);
2447 2516 WP_Optimize_Minify_Functions::remove_redundant_shortlink();
2448 2517 }
@@ -2532,11 +2601,11 @@
2532 2601 add_action('wp_head', array($this, 'add_critical_path'), 2);
2533 2602
2534 2603 // merge, if inline is selected but prevent optimization for these locations
2535 2604 if ($this->options['inline_css']) {
2536 - // this prints the styles (not fonts) and checks the 'colllect google fonts'
2605 + // this prints the styles (not fonts) and checks the 'collect google fonts'
2537 2606 add_filter('style_loader_tag', array($this, 'inline_css'), PHP_INT_MAX, 4);
2538 - // this prints the google fonts
2607 + // this prints the Google fonts
2539 2608 add_action('wp_print_styles', array($this, 'add_google_fonts_merged'), PHP_INT_MAX);
2540 2609 add_action('wp_print_footer_scripts', array($this, 'add_google_fonts_merged'), PHP_INT_MAX);
2541 2610 }
2542 2611 // Preloading
@@ -2542,14 +2611,14 @@
2542 2611 // Preloading
2543 2612 if ($this->options['enabled_css_preload']) {
2544 2613 add_filter('style_loader_tag', array($this, 'collect_css_preload_headers'), PHP_INT_MAX, 3);
2545 2614 }
2546 - // Optimize the css and collect the google fonts for merging
2615 + // Optimize the css and collect the Google fonts for merging
2547 2616 add_action('wp_print_styles', array($this, 'process_header_css'), PHP_INT_MAX);
2548 2617 add_action('wp_print_footer_scripts', array($this, 'process_footer_css'), 9);
2549 2618
2550 2619 /**
2551 - * Filters whether or not to ignore the order of the CSS files, and group them by media type
2620 + * Filters whether to ignore the order of the CSS files, and group them by media type
2552 2621 *
2553 2622 * @param boolean $maintain_css_order
2554 2623 * @return boolean
2555 2624 * @default true
@@ -2555,14 +2624,16 @@
2555 2624 * @default true
2556 2625 */
2557 2626 if (!apply_filters('wpo_minify_maintain_css_order', true)) {
2558 2627 // Reorder stylesheets
2559 - add_filter('wpo_minify_stylesheets', array($this, 'order_stylesheets_per_media_type'), 10);
2628 + add_filter('wpo_minify_stylesheets', array($this, 'order_stylesheets_per_media_type'));
2560 2629 }
2561 2630 }
2562 2631
2563 2632 /**
2564 2633 * Determines whether we should minify html or not
2634 + *
2635 + * @return bool
2565 2636 */
2566 2637 private function should_process_html() {
2567 2638 return $this->options['html_minification'] && !is_admin() && $this->is_cache_preload();
2568 2639 }
@@ -2568,8 +2639,10 @@
2568 2639 }
2569 2640
2570 2641 /**
2571 2642 * Determines whether to use loadCSS polyfill or not
2643 + *
2644 + * @return bool
2572 2645 */
2573 2646 private function should_use_loadCSS() {
2574 2647 return $this->options['loadcss']
2575 2648 || 'async' === $this->options['fawesome_method']
@@ -2661,13 +2734,13 @@
2661 2734
2662 2735 /**
2663 2736 * Decides whether minify cache should be incremented or not
2664 2737 *
2665 - * @param string $res
2738 + * @param array $res
2666 2739 * @param string $href
2667 2740 * @param string $type
2668 2741 * @param string $handle
2669 - * @param string $version
2742 + * @param mixed $version
2670 2743 *
2671 2744 * @return bool true when cache is invalid, false otherwise
2672 2745 */
2673 2746 private function should_reset_minify_assets($res, $href, $type, $handle, $version) {
@@ -2687,6 +2760,18 @@
2687 2760 $hash = hash('sha256', $res['code']);
2688 2761 $new_hash = hash('sha256', $new_res['code']);
2689 2762 return strcmp($hash, $new_hash);
2690 2763 }
2764 + return false;
2765 + }
2766 +
2767 + /**
2768 + * Returns the log header with the current date and url
2769 + *
2770 + * @return string
2771 + */
2772 + private function get_log_header() {
2773 + $request_uri = isset($_SERVER['REQUEST_URI']) ? esc_url_raw(wp_unslash($_SERVER['REQUEST_URI'])) : '';
2774 + $log_header = "PROCESSED on ".gmdate('r')." from ".home_url($request_uri);
2775 + return $log_header;
2691 2776 }
2692 2777 }