| @@ -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,19 @@ | ||
| 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)) { | |
| 236 | + // if option disabled google fonts processing | |
| 237 | + if ($this->options['disable_google_fonts_processing']) return $html; | |
| 220 | 238 | // check if google fonts should be removed |
| 221 | 239 | if ($this->options['remove_googlefonts']) return false; |
| 222 | 240 | // check if google fonts should be merged |
| 223 | 241 | if ($this->options['merge_google_fonts']) { |
| @@ -239,9 +257,9 @@ | ||
| 239 | 257 | $json = WP_Optimize_Minify_Cache_Functions::get_transient($tkey); |
| 240 | 258 | if (false === $json) { |
| 241 | 259 | $json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $this->options['enable_css_minification'], 'css', $handle); |
| 242 | 260 | if ($this->options['debug']) { |
| 243 | - 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"; | |
| 244 | 262 | } |
| 245 | 263 | WP_Optimize_Minify_Cache_Functions::set_transient($tkey, $json); |
| 246 | 264 | } |
| 247 | 265 | |
| @@ -248,18 +266,20 @@ | ||
| 248 | 266 | // decode |
| 249 | 267 | $res = json_decode($json, true); |
| 250 | 268 | |
| 251 | 269 | // inline css or fail |
| 252 | - if (null !== $res && false != $res['status']) { | |
| 270 | + if (null !== $res && false !== $res['status']) { | |
| 253 | 271 | // add font-display |
| 254 | 272 | // https://developers.google.com/web/updates/2016/02/font-display |
| 255 | 273 | $res['code'] = str_ireplace('font-style:normal;', 'font-display:block;font-style:normal;', $res['code']); |
| 256 | 274 | |
| 257 | - 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"; | |
| 258 | 278 | return false; |
| 259 | 279 | } else { |
| 260 | 280 | if ($this->options['debug']) { |
| 261 | - 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"; | |
| 262 | 282 | } |
| 263 | 283 | return $html; |
| 264 | 284 | } |
| 265 | 285 | } elseif ('async' === $this->options['gfonts_method']) { |
| @@ -291,9 +311,9 @@ | ||
| 291 | 311 | $json = WP_Optimize_Minify_Cache_Functions::get_transient($tkey); |
| 292 | 312 | if (false === $json) { |
| 293 | 313 | $json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $this->options['enable_css_minification'], 'css', $handle); |
| 294 | 314 | if ($this->options['debug']) { |
| 295 | - 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"; | |
| 296 | 316 | } |
| 297 | 317 | WP_Optimize_Minify_Cache_Functions::set_transient($tkey, $json); |
| 298 | 318 | } |
| 299 | 319 | |
| @@ -300,21 +320,25 @@ | ||
| 300 | 320 | // decode |
| 301 | 321 | $res = json_decode($json, true); |
| 302 | 322 | |
| 303 | 323 | // inline it + other inlined children styles |
| 304 | - if (null !== $res && false != $res['status']) { | |
| 305 | - 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"; | |
| 306 | 328 | |
| 307 | 329 | // get inline_styles for this handle, minify and print |
| 308 | 330 | $inline_styles = array(); |
| 309 | 331 | $inline_styles = wp_styles()->get_data($handle, 'after'); |
| 310 | - if (false != $inline_styles) { | |
| 332 | + if ($inline_styles) { | |
| 311 | 333 | |
| 312 | 334 | // string type |
| 313 | 335 | if (is_string($inline_styles)) { |
| 314 | 336 | $code = WP_Optimize_Minify_Functions::get_css($href, $inline_styles, $this->options['enable_css_minification']); |
| 315 | - if (!empty($code) && false != $code) { | |
| 316 | - 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"; | |
| 317 | 341 | } |
| 318 | 342 | } |
| 319 | 343 | |
| 320 | 344 | // array type |
| @@ -320,10 +344,12 @@ | ||
| 320 | 344 | // array type |
| 321 | 345 | if (is_array($inline_styles)) { |
| 322 | 346 | foreach ($inline_styles as $st) { |
| 323 | 347 | $code = WP_Optimize_Minify_Functions::get_css($href, $st, $this->options['enable_css_minification']); |
| 324 | - if (!empty($code) && false != $code) { | |
| 325 | - 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"; | |
| 326 | 352 | } |
| 327 | 353 | } |
| 328 | 354 | } |
| 329 | 355 | } |
| @@ -331,9 +357,9 @@ | ||
| 331 | 357 | // prevent default |
| 332 | 358 | return false; |
| 333 | 359 | } else { |
| 334 | 360 | if ($this->options['debug']) { |
| 335 | - 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"; | |
| 336 | 362 | } |
| 337 | 363 | return $html; |
| 338 | 364 | } |
| 339 | 365 | echo "<!-- ERROR: WPO-Minify couldn't catch the CSS file below. Please report this on https://wordpress.org/support/plugin/wp-optimize/ -->\n"; |
| @@ -340,15 +366,15 @@ | ||
| 340 | 366 | return $html; |
| 341 | 367 | } |
| 342 | 368 | |
| 343 | 369 | /** |
| 344 | - * 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 | |
| 345 | 371 | * |
| 346 | - * @param String $tag | |
| 347 | - * @param String $handle | |
| 348 | - * @param String $src | |
| 372 | + * @param string $tag | |
| 373 | + * @param string $handle | |
| 374 | + * @param string $src | |
| 349 | 375 | * |
| 350 | - * @return String | |
| 376 | + * @return string | |
| 351 | 377 | */ |
| 352 | 378 | public function defer_js($tag, $handle, $src) { |
| 353 | 379 | $wp_domain = trim(str_ireplace(array('http://', 'https://'), '', trim(site_url(), '/'))); |
| 354 | 380 | $exclude_js = $this->get_excluded_js_list(); |
| @@ -354,10 +380,11 @@ | ||
| 354 | 380 | $exclude_js = $this->get_excluded_js_list(); |
| 355 | 381 | $ignore_list = WP_Optimize_Minify_Functions::compile_ignore_list($exclude_js); |
| 356 | 382 | // Should this defer the Poly fills for IE? |
| 357 | 383 | $blacklist = WP_Optimize_Minify_Functions::get_ie_blacklist(); |
| 384 | + | |
| 358 | 385 | // no query strings |
| 359 | - if (false !== stripos($src, '?ver')) { | |
| 386 | + if (false !== stripos($src, '?ver') && WP_Optimize_Minify_Functions::is_already_minified($src)) { | |
| 360 | 387 | $srcf = stristr($src, '?ver', true); |
| 361 | 388 | $tag = str_ireplace($src, $srcf, $tag); |
| 362 | 389 | $src = $srcf; |
| 363 | 390 | } |
| @@ -392,9 +419,9 @@ | ||
| 392 | 419 | } |
| 393 | 420 | |
| 394 | 421 | // should we exclude defer on the login page? |
| 395 | 422 | if ($this->options['exclude_defer_login'] |
| 396 | - && 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(), '/')) | |
| 397 | 424 | ) { |
| 398 | 425 | return $tag; |
| 399 | 426 | } |
| 400 | 427 | |
| @@ -444,11 +471,15 @@ | ||
| 444 | 471 | * @return void |
| 445 | 472 | */ |
| 446 | 473 | public function add_critical_path() { |
| 447 | 474 | if (is_front_page() && !empty($this->options['critical_path_css_is_front_page'])) { |
| 448 | - 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"; | |
| 449 | 478 | } elseif (!empty($this->options['critical_path_css'])) { |
| 450 | - 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"; | |
| 451 | 482 | } |
| 452 | 483 | } |
| 453 | 484 | |
| 454 | 485 | /** |
| @@ -487,8 +518,9 @@ | ||
| 487 | 518 | $minify_css = $this->options['enable_css_minification']; |
| 488 | 519 | $merge_css = $this->options['enable_merging_of_css']; |
| 489 | 520 | $merge_inline_extra_css_js = $this->options['merge_inline_extra_css_js']; |
| 490 | 521 | $process_css = $minify_css || $merge_css; |
| 522 | + $disable_google_fonts_processing = $this->options['disable_google_fonts_processing']; | |
| 491 | 523 | $styles = clone $wp_styles; |
| 492 | 524 | $styles->all_deps($styles->queue); |
| 493 | 525 | $done = $styles->done; |
| 494 | 526 | $header = array(); |
| @@ -517,9 +549,9 @@ | ||
| 517 | 549 | } |
| 518 | 550 | |
| 519 | 551 | // mediatype |
| 520 | 552 | $mt = isset($wp_styles->registered[$handle]->args) ? $wp_styles->registered[$handle]->args : 'all'; |
| 521 | - 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) { | |
| 522 | 554 | $mt = 'all'; |
| 523 | 555 | } |
| 524 | 556 | $mediatype = $mt; |
| 525 | 557 | |
| @@ -541,11 +573,15 @@ | ||
| 541 | 573 | } else { |
| 542 | 574 | $uniq[$key] = $handle; |
| 543 | 575 | } |
| 544 | 576 | } |
| 577 | + | |
| 578 | + $version = '?ver=' . (!empty($version) ? $version : $wp_styles->default_version); | |
| 579 | + $href_versioned = $href . $version; | |
| 580 | + | |
| 545 | 581 | // Exclude specific CSS files from PageSpeedIndex? |
| 546 | 582 | if (WP_Optimize_Minify_Functions::in_arrayi($href, $async_css)) { |
| 547 | - WP_Optimize_Minify_Print::exclude_style($href); | |
| 583 | + WP_Optimize_Minify_Print::exclude_style($href_versioned); | |
| 548 | 584 | $done = array_merge($done, array($handle)); |
| 549 | 585 | continue; |
| 550 | 586 | } |
| 551 | 587 | // Fonts Awesome Processing |
| @@ -550,17 +586,17 @@ | ||
| 550 | 586 | } |
| 551 | 587 | // Fonts Awesome Processing |
| 552 | 588 | if (WP_Optimize_Minify_Functions::is_font_awesome($href)) { |
| 553 | 589 | if ('inline' === $this->options['fawesome_method']) { |
| 554 | - WP_Optimize_Minify_Print::inline_style($handle, $href); | |
| 590 | + WP_Optimize_Minify_Print::inline_style($handle, $href_versioned); | |
| 555 | 591 | $done = array_merge($done, array($handle)); |
| 556 | 592 | continue; |
| 557 | 593 | } elseif ('async' === $this->options['fawesome_method']) { |
| 558 | - WP_Optimize_Minify_Print::async_style($href, $mediatype); | |
| 594 | + WP_Optimize_Minify_Print::async_style($href_versioned, $mediatype); | |
| 559 | 595 | $done = array_merge($done, array($handle)); |
| 560 | 596 | continue; |
| 561 | 597 | } elseif ('exclude' === $this->options['fawesome_method']) { |
| 562 | - WP_Optimize_Minify_Print::exclude_style($href); | |
| 598 | + WP_Optimize_Minify_Print::exclude_style($href_versioned); | |
| 563 | 599 | $done = array_merge($done, array($handle)); |
| 564 | 600 | continue; |
| 565 | 601 | } |
| 566 | 602 | } |
| @@ -579,9 +615,9 @@ | ||
| 579 | 615 | 'mediatype' => $mediatype |
| 580 | 616 | ); |
| 581 | 617 | |
| 582 | 618 | // google fonts to the top (collect and skip process array) |
| 583 | - if (WP_Optimize_Minify_Functions::is_google_font($href)) { | |
| 619 | + if (!$disable_google_fonts_processing && WP_Optimize_Minify_Functions::is_google_font($href)) { | |
| 584 | 620 | if ($this->options['remove_googlefonts']) { |
| 585 | 621 | $done = array_merge($done, array($handle)); |
| 586 | 622 | continue; |
| 587 | 623 | } |
| @@ -600,9 +636,9 @@ | ||
| 600 | 636 | // merge google fonts if force inlining is enabled? |
| 601 | 637 | $nfonts = array(); |
| 602 | 638 | if ($this->options['merge_google_fonts']) { |
| 603 | 639 | $nfonts[] = WP_Optimize_Minify_Fonts::concatenate_google_fonts($google_fonts); |
| 604 | - // 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 | |
| 605 | 641 | if ('inherit' !== $this->options['gfonts_method']) { |
| 606 | 642 | $done = array_merge($done, array_keys($google_fonts)); |
| 607 | 643 | } |
| 608 | 644 | } else { |
| @@ -624,9 +660,9 @@ | ||
| 624 | 660 | // check if the cache is empty or has no code |
| 625 | 661 | if (null === $json || false === $json || empty($json['code'])) { |
| 626 | 662 | $res = WP_Optimize_Minify_Functions::download_and_minify($href, null, $minify_css, 'css', null); |
| 627 | 663 | if ($this->options['debug']) { |
| 628 | - 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"; | |
| 629 | 665 | } |
| 630 | 666 | WP_Optimize_Minify_Cache_Functions::set_transient($tkey, $res); |
| 631 | 667 | // decode |
| 632 | 668 | $json = json_decode($res, true); |
| @@ -636,14 +672,16 @@ | ||
| 636 | 672 | if (null !== $json && !empty($json['code'])) { |
| 637 | 673 | // add font-display |
| 638 | 674 | // https://developers.google.com/web/updates/2016/02/font-display |
| 639 | 675 | $json['code'] = str_ireplace('font-style:normal;', 'font-display:block;font-style:normal;', $json['code']); |
| 640 | - 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"; | |
| 641 | 679 | $done = array_merge($done, array($handle)); |
| 642 | 680 | } else { |
| 643 | - echo "<!-- GOOGLE FONTS REQUEST FAILED for $href -->" . "\n"; | |
| 681 | + echo "<!-- GOOGLE FONTS REQUEST FAILED for " . esc_html($href) . " -->" . "\n"; | |
| 644 | 682 | // inlining failed, so enqueue again |
| 645 | - 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 | |
| 646 | 684 | } |
| 647 | 685 | } elseif ('async' === $this->options['gfonts_method']) { |
| 648 | 686 | WP_Optimize_Minify_Print::async_style($href); |
| 649 | 687 | $done = array_merge($done, array($handle)); |
| @@ -689,15 +727,15 @@ | ||
| 689 | 727 | |
| 690 | 728 | // check if the current URL points to a minified file. |
| 691 | 729 | $exclude_minified_css = WP_Optimize_Minify_Functions::is_minified_css_js_filename($href); |
| 692 | 730 | $normal_processing = $exclude_minified_css && !$merge_css; |
| 693 | - // skip ignore list, conditional css, external css, font-awesome merge | |
| 731 | + // skip ignore list, conditional css, external css, font-awesome merge and already minified js | |
| 694 | 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())) |
| 695 | 733 | || empty($href) |
| 696 | - || ($process_css && 'inherit' == $this->options['fawesome_method'] && WP_Optimize_Minify_Functions::is_font_awesome($href)) | |
| 697 | - || ($process_css && '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)) | |
| 698 | 736 | ) { |
| 699 | - // colect inline css for this handle | |
| 737 | + // collect inline css for this handle | |
| 700 | 738 | if (isset($wp_styles->registered[$handle]->extra['after']) && is_array($wp_styles->registered[$handle]->extra['after'])) { |
| 701 | 739 | $inline_css[$handle] = WP_Optimize_Minify_Functions::minify_css_string(implode('', $wp_styles->registered[$handle]->extra['after'])); // save |
| 702 | 740 | $wp_styles->registered[$handle]->extra['after'] = null; // dequeue |
| 703 | 741 | } |
| @@ -702,9 +740,9 @@ | ||
| 702 | 740 | $wp_styles->registered[$handle]->extra['after'] = null; // dequeue |
| 703 | 741 | } |
| 704 | 742 | |
| 705 | 743 | // process |
| 706 | - 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) { | |
| 707 | 745 | array_push($header, array('handles' => array(), 'media' => $mediatype, 'versions' => array())); |
| 708 | 746 | } |
| 709 | 747 | |
| 710 | 748 | // push it to the array |
| @@ -714,8 +752,10 @@ | ||
| 714 | 752 | // https://plugins.trac.wordpress.org/browser/mailin/tags/3.1.54/sendinblue.php#L456 |
| 715 | 753 | $version_str = is_array($wp_styles->registered[$handle]->ver) ? $this->array_to_string_conversion($wp_styles->registered[$handle]->ver) : $wp_styles->registered[$handle]->ver; |
| 716 | 754 | array_push($header[count($header)-1]['versions'], $version_str); |
| 717 | 755 | |
| 756 | + $header[count($header) - 1]['last_modified'][] = WP_Optimize_Minify_Functions::get_modification_time($wp_styles->registered[$handle]->src); | |
| 757 | + | |
| 718 | 758 | // external and ignored css |
| 719 | 759 | } else { |
| 720 | 760 | // normal enqueuing |
| 721 | 761 | array_push($header, array('handle' => $handle)); |
| @@ -732,25 +772,29 @@ | ||
| 732 | 772 | $header = apply_filters('wpo_minify_stylesheets', $header, 'header'); |
| 733 | 773 | |
| 734 | 774 | // loop through header css and merge |
| 735 | 775 | for ($i=0,$l=count($header); $i<$l; $i++) { |
| 776 | + | |
| 736 | 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 | + | |
| 737 | 782 | if ($merge_css) { |
| 738 | 783 | // get hash for the inline css in this group |
| 739 | 784 | $inline_css_group = array(); |
| 740 | - foreach ($header[$i]['handles'] as $h) { | |
| 741 | - if (!empty($inline_css) && isset($inline_css[$h]) && !empty($inline_css[$h])) { | |
| 742 | - $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 | + } | |
| 743 | 791 | } |
| 744 | 792 | } |
| 745 | 793 | $inline_css_hash = md5(implode('', $inline_css_group)); |
| 746 | - // We need this because some plugins/theme use empty array as version | |
| 747 | - // https://plugins.trac.wordpress.org/browser/mailin/tags/3.1.54/sendinblue.php#L456 | |
| 748 | - $version_str = $this->array_to_string_conversion($header[$i]['versions']); | |
| 749 | - $handle_str = $this->array_to_string_conversion($header[$i]['handles']); | |
| 750 | - $hash = hash('adler32', $handle_str . $inline_css_hash . $version_str); | |
| 794 | + $hash = hash('adler32', $handle_str . $inline_css_hash . $last_modified_str); | |
| 751 | 795 | } else { |
| 752 | - $hash = implode('', $header[$i]['handles']) . implode('', $header[$i]['versions']); | |
| 796 | + $hash = sanitize_file_name($handle_str . $last_modified_str); | |
| 753 | 797 | } |
| 754 | 798 | |
| 755 | 799 | // static cache file info |
| 756 | 800 | $file_name = 'wpo-minify-header-'.$hash.($minify_css ? '.min' : ''); |
| @@ -764,9 +808,9 @@ | ||
| 764 | 808 | clearstatcache(); |
| 765 | 809 | if (!file_exists($file)) { |
| 766 | 810 | |
| 767 | 811 | // code and log initialization |
| 768 | - $log_header = "PROCESSED on ".date('r')." from ".home_url(add_query_arg(null, null)); | |
| 812 | + $log_header = $this->get_log_header(); | |
| 769 | 813 | $log = array( |
| 770 | 814 | 'header' => $log_header, |
| 771 | 815 | 'files' => array() |
| 772 | 816 | ); |
| @@ -777,10 +821,12 @@ | ||
| 777 | 821 | foreach ($header[$i]['handles'] as $handle) { |
| 778 | 822 | if (!empty($wp_styles->registered[$handle]->src)) { |
| 779 | 823 | |
| 780 | 824 | // get href per handle |
| 781 | - $href = WP_Optimize_Minify_Functions::get_hurl($wp_styles->registered[$handle]->src); | |
| 825 | + $handle_src = $wp_styles->registered[$handle]->src; | |
| 826 | + $href = WP_Optimize_Minify_Functions::get_hurl($handle_src); | |
| 782 | 827 | $version = $wp_styles->registered[$handle]->ver; |
| 828 | + $last_modified = WP_Optimize_Minify_Functions::get_modification_time($handle_src); | |
| 783 | 829 | |
| 784 | 830 | // inlined scripts without file |
| 785 | 831 | if (empty($href)) continue; |
| 786 | 832 | |
| @@ -791,9 +837,9 @@ | ||
| 791 | 837 | if (false === $json) { |
| 792 | 838 | $enable_minification = $this->options['enable_css_minification'] && !WP_Optimize_Minify_Functions::is_minified_css_js_filename($href); |
| 793 | 839 | $json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $enable_minification, 'css', $handle, $version); |
| 794 | 840 | if ($this->options['debug']) { |
| 795 | - 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"; | |
| 796 | 842 | } |
| 797 | 843 | WP_Optimize_Minify_Cache_Functions::set_transient($tkey, $json); |
| 798 | 844 | } |
| 799 | 845 | |
| @@ -809,10 +855,12 @@ | ||
| 809 | 855 | $meta_log['files_that_changed'][$handle] = $res['log']; |
| 810 | 856 | } |
| 811 | 857 | |
| 812 | 858 | // response has failed |
| 813 | - if (true != $res['status']) { | |
| 859 | + if (true !== $res['status']) { | |
| 814 | 860 | $log['files'][$handle] = $res['log']; |
| 861 | + $log['files'][$handle]['version'] = $version; | |
| 862 | + $log['files'][$handle]['last_modified'] = $last_modified; | |
| 815 | 863 | continue; |
| 816 | 864 | } |
| 817 | 865 | |
| 818 | 866 | // Only add the $handle to $done if it was successfully downloaded |
| @@ -817,10 +865,12 @@ | ||
| 817 | 865 | |
| 818 | 866 | // Only add the $handle to $done if it was successfully downloaded |
| 819 | 867 | $done[] = $handle; |
| 820 | 868 | // append code to merged file |
| 821 | - $code .= isset($res['code']) ? $res['code'] : ''; | |
| 869 | + $code .= $res['code'] ?? ''; | |
| 822 | 870 | $log['files'][$handle] = $res['log']; |
| 871 | + $log['files'][$handle]['version'] = $version; | |
| 872 | + $log['files'][$handle]['last_modified'] = $last_modified; | |
| 823 | 873 | |
| 824 | 874 | // append inlined styles |
| 825 | 875 | if ($merge_inline_extra_css_js && isset($inline_css[$handle]) && !empty($inline_css[$handle])) { |
| 826 | 876 | $code.= $inline_css[$handle]; |
| @@ -836,9 +886,9 @@ | ||
| 836 | 886 | // generate cache, write log |
| 837 | 887 | if (!empty($code)) { |
| 838 | 888 | // Only generate meta.json file if cache is reset |
| 839 | 889 | if (!empty($meta_log['invalidation_reason'])) { |
| 840 | - file_put_contents($meta_json_file, json_encode($meta_log)); | |
| 890 | + file_put_contents($meta_json_file, wp_json_encode($meta_log)); | |
| 841 | 891 | } |
| 842 | 892 | WP_Optimize_Minify_Print::write_combined_asset($file, $code, $log); |
| 843 | 893 | } |
| 844 | 894 | } else { |
| @@ -879,9 +929,9 @@ | ||
| 879 | 929 | } |
| 880 | 930 | } |
| 881 | 931 | } else { |
| 882 | 932 | // file could not be generated, output something meaningful |
| 883 | - 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))." -->"; | |
| 884 | 934 | echo "<!-- Please check if the path above is correct and ensure your server has write permission there! -->"; |
| 885 | 935 | } |
| 886 | 936 | } |
| 887 | 937 | // other css need to be requeued for the order of files to be kept |
| @@ -939,9 +989,9 @@ | ||
| 939 | 989 | continue; |
| 940 | 990 | } |
| 941 | 991 | |
| 942 | 992 | // Exclude JS files from PageSpeedIndex (Async) takes priority over the ignore list |
| 943 | - if (false != $async_js || is_array($async_js)) { | |
| 993 | + if (!empty($async_js)) { | |
| 944 | 994 | |
| 945 | 995 | // check for string match |
| 946 | 996 | $skipjs = false; |
| 947 | 997 | foreach ($async_js as $l) { |
| @@ -946,15 +996,16 @@ | ||
| 946 | 996 | $skipjs = false; |
| 947 | 997 | foreach ($async_js as $l) { |
| 948 | 998 | if (stripos($href, $l) !== false) { |
| 949 | 999 | // print code if there are no linebreaks, or return |
| 950 | - 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); | |
| 951 | 1002 | $skipjs = true; |
| 952 | 1003 | $done = array_merge($done, array($handle)); |
| 953 | 1004 | break; |
| 954 | 1005 | } |
| 955 | 1006 | } |
| 956 | - if (false != $skipjs) { | |
| 1007 | + if (false !== $skipjs) { | |
| 957 | 1008 | continue; |
| 958 | 1009 | } |
| 959 | 1010 | } |
| 960 | 1011 | |
| @@ -976,10 +1027,10 @@ | ||
| 976 | 1027 | // process |
| 977 | 1028 | if (isset($footer[count($footer)-1]['handle']) |
| 978 | 1029 | || !count($footer) |
| 979 | 1030 | || !$merge_js |
| 980 | - || (isset($scripts->registered[$handle]->extra['strategy']) && $previous_load_strategy != $scripts->registered[$handle]->extra['strategy']) | |
| 981 | - || (!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) | |
| 982 | 1033 | ) { |
| 983 | 1034 | array_push($footer, array('handles' => array(), 'versions' => array())); |
| 984 | 1035 | } |
| 985 | 1036 | |
| @@ -995,15 +1046,12 @@ | ||
| 995 | 1046 | |
| 996 | 1047 | // Register strategy if it is used |
| 997 | 1048 | if (isset($scripts->registered[$handle]->extra['strategy'])) $footer[count($footer)-1]['strategy'] = $scripts->registered[$handle]->extra['strategy']; |
| 998 | 1049 | |
| 1050 | + $footer[count($footer) - 1]['last_modified'][] = WP_Optimize_Minify_Functions::get_modification_time($wp_scripts->registered[$handle]->src); | |
| 999 | 1051 | // external and ignored scripts |
| 1000 | - | |
| 1001 | - if (isset($scripts->registered[$handle]->extra['strategy'])) { | |
| 1002 | - $previous_load_strategy = $scripts->registered[$handle]->extra['strategy']; | |
| 1003 | - } else { | |
| 1004 | - $previous_load_strategy = null; | |
| 1005 | - } | |
| 1052 | + | |
| 1053 | + $previous_load_strategy = $scripts->registered[$handle]->extra['strategy'] ?? null; | |
| 1006 | 1054 | |
| 1007 | 1055 | } else { |
| 1008 | 1056 | array_push($footer, array('handle' => $handle)); |
| 1009 | 1057 | } |
| @@ -1010,15 +1058,19 @@ | ||
| 1010 | 1058 | endforeach; |
| 1011 | 1059 | |
| 1012 | 1060 | // loop through footer scripts and merge |
| 1013 | 1061 | for ($i=0,$l=count($footer); $i<$l; $i++) { |
| 1062 | + | |
| 1014 | 1063 | if (!isset($footer[$i]['handle'])) { |
| 1015 | 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 | + | |
| 1016 | 1068 | if ($merge_js) { |
| 1017 | - // Change the hash based on version numbers | |
| 1018 | - $hash = hash('adler32', implode('', $footer[$i]['handles']) . implode('', $footer[$i]['versions'])); | |
| 1069 | + // Change the hash based on last modified timestamp | |
| 1070 | + $hash = hash('adler32', $handles_str . $last_modified_str); | |
| 1019 | 1071 | } else { |
| 1020 | - $hash = implode('', $footer[$i]['handles']) . implode('', $footer[$i]['versions']); | |
| 1072 | + $hash = sanitize_file_name($handles_str . $last_modified_str); | |
| 1021 | 1073 | } |
| 1022 | 1074 | |
| 1023 | 1075 | // static cache file info |
| 1024 | 1076 | $file_name = 'wpo-minify-footer-'.$hash.($minify_js ? '.min' : ''); |
| @@ -1032,9 +1084,9 @@ | ||
| 1032 | 1084 | clearstatcache(); |
| 1033 | 1085 | if (!file_exists($file)) { |
| 1034 | 1086 | |
| 1035 | 1087 | // code and log initialization |
| 1036 | - $log_header = "PROCESSED on ".date('r')." from ".home_url(add_query_arg(null, null)); | |
| 1088 | + $log_header = $this->get_log_header(); | |
| 1037 | 1089 | $log = array( |
| 1038 | 1090 | 'header' => $log_header, |
| 1039 | 1091 | 'files' => array() |
| 1040 | 1092 | ); |
| @@ -1049,10 +1101,12 @@ | ||
| 1049 | 1101 | // only add minified scripts once to the bundle |
| 1050 | 1102 | if (in_array($handle, $done)) continue; |
| 1051 | 1103 | |
| 1052 | 1104 | // get href per handle |
| 1053 | - $href = WP_Optimize_Minify_Functions::get_hurl($wp_scripts->registered[$handle]->src); | |
| 1105 | + $handle_src = $wp_scripts->registered[$handle]->src; | |
| 1106 | + $href = WP_Optimize_Minify_Functions::get_hurl($handle_src); | |
| 1054 | 1107 | $version = $wp_scripts->registered[$handle]->ver; |
| 1108 | + $last_modified = WP_Optimize_Minify_Functions::get_modification_time($handle_src); | |
| 1055 | 1109 | // inlined scripts without file |
| 1056 | 1110 | if (empty($href)) { |
| 1057 | 1111 | continue; |
| 1058 | 1112 | } |
| @@ -1063,9 +1117,9 @@ | ||
| 1063 | 1117 | if (false === $json) { |
| 1064 | 1118 | $enable_minification = $minify_js && !WP_Optimize_Minify_Functions::is_minified_css_js_filename($href); |
| 1065 | 1119 | $json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $enable_minification, 'js', $handle, $version); |
| 1066 | 1120 | if ($this->options['debug']) { |
| 1067 | - 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"; | |
| 1068 | 1122 | } |
| 1069 | 1123 | WP_Optimize_Minify_Cache_Functions::set_transient($tkey, $json); |
| 1070 | 1124 | } |
| 1071 | 1125 | |
| @@ -1081,11 +1135,13 @@ | ||
| 1081 | 1135 | $meta_log['files_that_changed'][$handle] = $res['log']; |
| 1082 | 1136 | } |
| 1083 | 1137 | |
| 1084 | 1138 | // response has failed |
| 1085 | - if (true != $res['status']) { | |
| 1139 | + if (true !== $res['status']) { | |
| 1086 | 1140 | $log['files'][$handle] = $res['log']; |
| 1087 | 1141 | $log['files'][$handle]['uses_loading_strategy'] = isset($footer[$i]['strategy']); |
| 1142 | + $log['files'][$handle]['version'] = $version; | |
| 1143 | + $log['files'][$handle]['last_modified'] = $last_modified; | |
| 1088 | 1144 | continue; |
| 1089 | 1145 | } |
| 1090 | 1146 | |
| 1091 | 1147 | $done[] = $handle; |
| @@ -1113,13 +1169,15 @@ | ||
| 1113 | 1169 | } |
| 1114 | 1170 | |
| 1115 | 1171 | // append code to merged file |
| 1116 | 1172 | $code .= $before_code . "\n"; |
| 1117 | - $code .= isset($res['code']) ? $res['code'] : ''; | |
| 1173 | + $code .= $res['code'] ?? ''; | |
| 1118 | 1174 | $code .= "\n" . $after_code . "\n"; |
| 1119 | 1175 | $code = WP_Optimize_Minify_Functions::prepare_merged_js($code, $href); |
| 1120 | 1176 | $log['files'][$handle] = $res['log']; |
| 1121 | 1177 | $log['files'][$handle]['uses_loading_strategy'] = isset($footer[$i]['strategy']); |
| 1178 | + $log['files'][$handle]['version'] = $version; | |
| 1179 | + $log['files'][$handle]['last_modified'] = $last_modified; | |
| 1122 | 1180 | |
| 1123 | 1181 | // consider dependencies on handles with an empty src |
| 1124 | 1182 | } else { |
| 1125 | 1183 | wp_dequeue_script($handle); |
| @@ -1130,9 +1188,9 @@ | ||
| 1130 | 1188 | // generate cache, write log |
| 1131 | 1189 | if (!empty($code)) { |
| 1132 | 1190 | // Only generate meta.json file if cache is reset |
| 1133 | 1191 | if (!empty($meta_log['invalidation_reason'])) { |
| 1134 | - file_put_contents($meta_json_file, json_encode($meta_log)); | |
| 1192 | + file_put_contents($meta_json_file, wp_json_encode($meta_log)); | |
| 1135 | 1193 | } |
| 1136 | 1194 | WP_Optimize_Minify_Print::write_combined_asset($file, $code, $log); |
| 1137 | 1195 | } |
| 1138 | 1196 | } else { |
| @@ -1156,9 +1214,9 @@ | ||
| 1156 | 1214 | } |
| 1157 | 1215 | } |
| 1158 | 1216 | |
| 1159 | 1217 | // register minified file |
| 1160 | - 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 | |
| 1161 | 1219 | |
| 1162 | 1220 | // add all extra data from wp_localize_script |
| 1163 | 1221 | $before_code = ''; |
| 1164 | 1222 | $data = array(); |
| @@ -1201,9 +1259,9 @@ | ||
| 1201 | 1259 | wp_script_add_data( "wpo_min-footer-$i", 'strategy', $footer[$i]['strategy']); |
| 1202 | 1260 | } |
| 1203 | 1261 | } else { |
| 1204 | 1262 | // file could not be generated, output something meaningful |
| 1205 | - 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))." -->"; | |
| 1206 | 1264 | echo "<!-- Please check if the path above is correct and ensure your server has write permission there! -->"; |
| 1207 | 1265 | } |
| 1208 | 1266 | |
| 1209 | 1267 | // other scripts need to be requeued for the order of files to be kept |
| @@ -1218,9 +1276,9 @@ | ||
| 1218 | 1276 | } |
| 1219 | 1277 | |
| 1220 | 1278 | /** |
| 1221 | 1279 | * Process header JavaScript |
| 1222 | - * Dependant on 'enable_js' option | |
| 1280 | + * Dependent on 'enable_js' option | |
| 1223 | 1281 | * |
| 1224 | 1282 | * @return void |
| 1225 | 1283 | */ |
| 1226 | 1284 | public function process_header_scripts() { |
| @@ -1251,9 +1309,9 @@ | ||
| 1251 | 1309 | // get full url |
| 1252 | 1310 | $href = WP_Optimize_Minify_Functions::get_hurl($wp_scripts->registered[$handle]->src); |
| 1253 | 1311 | // inlined scripts without file |
| 1254 | 1312 | if (empty($href)) { |
| 1255 | - wp_enqueue_script($handle, false); | |
| 1313 | + wp_enqueue_script($handle, false); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- Not needed | |
| 1256 | 1314 | continue; |
| 1257 | 1315 | } |
| 1258 | 1316 | |
| 1259 | 1317 | // Only go through items without a group |
| @@ -1262,9 +1320,10 @@ | ||
| 1262 | 1320 | // check for string match |
| 1263 | 1321 | $skipjs = false; |
| 1264 | 1322 | foreach ($async_js as $l) { |
| 1265 | 1323 | if (stripos($href, $l) !== false) { |
| 1266 | - 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); | |
| 1267 | 1326 | $skipjs = true; |
| 1268 | 1327 | $done = array_merge($done, array($handle)); |
| 1269 | 1328 | break; |
| 1270 | 1329 | } |
| @@ -1297,10 +1356,10 @@ | ||
| 1297 | 1356 | // process |
| 1298 | 1357 | if (isset($header[count($header)-1]['handle']) |
| 1299 | 1358 | || !count($header) |
| 1300 | 1359 | || !$merge_js |
| 1301 | - || (isset($scripts->registered[$handle]->extra['strategy']) && $previous_load_strategy != $scripts->registered[$handle]->extra['strategy']) | |
| 1302 | - || (!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) | |
| 1303 | 1362 | ) { |
| 1304 | 1363 | array_push($header, array('handles' => array(), 'versions' => array())); |
| 1305 | 1364 | } |
| 1306 | 1365 | |
| @@ -1334,18 +1393,15 @@ | ||
| 1334 | 1393 | |
| 1335 | 1394 | // push it to the array |
| 1336 | 1395 | array_push($header[count($header)-1]['handles'], $handle); |
| 1337 | 1396 | array_push($header[count($header)-1]['versions'], $scripts->registered[$handle]->ver); |
| 1397 | + $header[count($header) - 1]['last_modified'][] = WP_Optimize_Minify_Functions::get_modification_time($wp_scripts->registered[$handle]->src); | |
| 1338 | 1398 | |
| 1339 | 1399 | // Register strategy if it is used |
| 1340 | 1400 | if (isset($scripts->registered[$handle]->extra['strategy'])) $header[count($header)-1]['strategy'] = $scripts->registered[$handle]->extra['strategy']; |
| 1341 | - | |
| 1342 | - if (isset($scripts->registered[$handle]->extra['strategy'])) { | |
| 1343 | - $previous_load_strategy = $scripts->registered[$handle]->extra['strategy']; | |
| 1344 | - } else { | |
| 1345 | - $previous_load_strategy = null; | |
| 1346 | - } | |
| 1347 | 1401 | |
| 1402 | + $previous_load_strategy = $scripts->registered[$handle]->extra['strategy'] ?? null; | |
| 1403 | + | |
| 1348 | 1404 | |
| 1349 | 1405 | // external and ignored scripts |
| 1350 | 1406 | } else { |
| 1351 | 1407 | // add the ignored assets |
| @@ -1353,20 +1409,24 @@ | ||
| 1353 | 1409 | } |
| 1354 | 1410 | |
| 1355 | 1411 | // make sure that the scripts skipped here, show up in the footer |
| 1356 | 1412 | } else { |
| 1357 | - 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 | |
| 1358 | 1414 | } |
| 1359 | 1415 | } |
| 1360 | 1416 | |
| 1361 | 1417 | // loop through header scripts and merge |
| 1362 | 1418 | for ($i=0,$l=count($header); $i < $l; $i++) { |
| 1419 | + | |
| 1363 | 1420 | if (!isset($header[$i]['handle'])) { |
| 1364 | - | |
| 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 | + | |
| 1365 | 1425 | if ($merge_js) { |
| 1366 | - $hash = hash('adler32', implode('', $header[$i]['handles']) . implode('', $header[$i]['versions'])); | |
| 1426 | + $hash = hash('adler32', $handles_str . $last_modified_str); | |
| 1367 | 1427 | } else { |
| 1368 | - $hash = implode('', $header[$i]['handles']) . implode('', $header[$i]['versions']); | |
| 1428 | + $hash = sanitize_file_name($handles_str . $last_modified_str); | |
| 1369 | 1429 | } |
| 1370 | 1430 | |
| 1371 | 1431 | // static cache file info |
| 1372 | 1432 | $file_name = 'wpo-minify-header-'.$hash.($minify_js ? '.min' : ''); |
| @@ -1379,9 +1439,9 @@ | ||
| 1379 | 1439 | clearstatcache(); |
| 1380 | 1440 | if (!file_exists($file)) { |
| 1381 | 1441 | |
| 1382 | 1442 | // code and log initialization |
| 1383 | - $log_header = "PROCESSED on ".date('r')." from ".home_url(add_query_arg(null, null)); | |
| 1443 | + $log_header = $this->get_log_header(); | |
| 1384 | 1444 | $log = array( |
| 1385 | 1445 | 'header' => $log_header, |
| 1386 | 1446 | 'files' => array() |
| 1387 | 1447 | ); |
| @@ -1396,10 +1456,12 @@ | ||
| 1396 | 1456 | // only add minified scripts once to the bundle |
| 1397 | 1457 | if (in_array($handle, $done)) continue; |
| 1398 | 1458 | |
| 1399 | 1459 | // get href per handle |
| 1400 | - $href = WP_Optimize_Minify_Functions::get_hurl($wp_scripts->registered[$handle]->src); | |
| 1460 | + $handle_src = $wp_scripts->registered[$handle]->src; | |
| 1461 | + $href = WP_Optimize_Minify_Functions::get_hurl($handle_src); | |
| 1401 | 1462 | $version = $wp_scripts->registered[$handle]->ver; |
| 1463 | + $last_modified = WP_Optimize_Minify_Functions::get_modification_time($handle_src); | |
| 1402 | 1464 | if (empty($href)) continue; |
| 1403 | 1465 | // download, minify, cache |
| 1404 | 1466 | $tkey = 'js-'.hash('adler32', $handle . $href).'.js'; |
| 1405 | 1467 | $json = false; |
| @@ -1407,9 +1469,9 @@ | ||
| 1407 | 1469 | if (false === $json) { |
| 1408 | 1470 | $enable_minification = $minify_js && !WP_Optimize_Minify_Functions::is_minified_css_js_filename($href); |
| 1409 | 1471 | $json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $enable_minification, 'js', $handle, $version); |
| 1410 | 1472 | if ($this->options['debug']) { |
| 1411 | - 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"; | |
| 1412 | 1474 | } |
| 1413 | 1475 | WP_Optimize_Minify_Cache_Functions::set_transient($tkey, $json); |
| 1414 | 1476 | } |
| 1415 | 1477 | |
| @@ -1425,11 +1487,13 @@ | ||
| 1425 | 1487 | $meta_log['files_that_changed'][$handle] = $res['log']; |
| 1426 | 1488 | } |
| 1427 | 1489 | |
| 1428 | 1490 | // response has failed |
| 1429 | - if (true != $res['status']) { | |
| 1491 | + if (true !== $res['status']) { | |
| 1430 | 1492 | $log['files'][$handle] = $res['log']; |
| 1431 | 1493 | $log['files'][$handle]['uses_loading_strategy'] = isset($header[$i]['strategy']); |
| 1494 | + $log['files'][$handle]['version'] = $version; | |
| 1495 | + $log['files'][$handle]['last_modified'] = $last_modified; | |
| 1432 | 1496 | continue; |
| 1433 | 1497 | } |
| 1434 | 1498 | |
| 1435 | 1499 | // Add extra data from wp_add_inline_script before |
| @@ -1464,8 +1528,10 @@ | ||
| 1464 | 1528 | $code .= "\n" . $after_code . "\n"; |
| 1465 | 1529 | $code = WP_Optimize_Minify_Functions::prepare_merged_js($code, $href); |
| 1466 | 1530 | $log['files'][$handle] = $res['log']; |
| 1467 | 1531 | $log['files'][$handle]['uses_loading_strategy'] = isset($header[$i]['strategy']); |
| 1532 | + $log['files'][$handle]['version'] = $version; | |
| 1533 | + $log['files'][$handle]['last_modified'] = $last_modified; | |
| 1468 | 1534 | |
| 1469 | 1535 | // consider dependencies on handles with an empty src |
| 1470 | 1536 | } else { |
| 1471 | 1537 | wp_dequeue_script($handle); |
| @@ -1476,9 +1542,9 @@ | ||
| 1476 | 1542 | // generate cache, write log |
| 1477 | 1543 | if (!empty($code)) { |
| 1478 | 1544 | // Only generate meta.json file if cache is reset |
| 1479 | 1545 | if (!empty($meta_log['invalidation_reason'])) { |
| 1480 | - file_put_contents($meta_json_file, json_encode($meta_log)); | |
| 1546 | + file_put_contents($meta_json_file, wp_json_encode($meta_log)); | |
| 1481 | 1547 | } |
| 1482 | 1548 | WP_Optimize_Minify_Print::write_combined_asset($file, $code, $log); |
| 1483 | 1549 | } |
| 1484 | 1550 | } else { |
| @@ -1504,9 +1570,9 @@ | ||
| 1504 | 1570 | } |
| 1505 | 1571 | |
| 1506 | 1572 | // register minified file |
| 1507 | 1573 | $dependencies = isset($excluded_dependencies[$i]) ? $excluded_dependencies[$i] : array(); |
| 1508 | - 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 | |
| 1509 | 1575 | |
| 1510 | 1576 | // add all extra data from wp_localize_script |
| 1511 | 1577 | $before_code = ''; |
| 1512 | 1578 | $data = array(); |
| @@ -1550,9 +1616,9 @@ | ||
| 1550 | 1616 | wp_script_add_data( "wpo_min-header-$i", 'strategy', $header[$i]['strategy']); |
| 1551 | 1617 | } |
| 1552 | 1618 | } else { |
| 1553 | 1619 | // file could not be generated, output something meaningful |
| 1554 | - 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))." -->"; | |
| 1555 | 1621 | echo "<!-- Please check if the path above is correct and ensure your server has write permission there! -->"; |
| 1556 | 1622 | echo "<!-- If you found a bug, please report this on https://wordpress.org/support/plugin/wp-optimize/ -->"; |
| 1557 | 1623 | } |
| 1558 | 1624 | |
| @@ -1584,8 +1650,9 @@ | ||
| 1584 | 1650 | $async_css = $this->get_async_css_list(); |
| 1585 | 1651 | $minify_css = $this->options['enable_css_minification']; |
| 1586 | 1652 | $merge_css = $this->options['enable_merging_of_css']; |
| 1587 | 1653 | $process_css = $minify_css || $merge_css; |
| 1654 | + $disable_google_fonts_processing = $this->options['disable_google_fonts_processing']; | |
| 1588 | 1655 | $log = ""; |
| 1589 | 1656 | $code = ""; |
| 1590 | 1657 | $styles = clone $wp_styles; |
| 1591 | 1658 | $styles->all_deps($styles->queue); |
| @@ -1604,15 +1671,15 @@ | ||
| 1604 | 1671 | $wp_styles->done = $done; |
| 1605 | 1672 | return; |
| 1606 | 1673 | } |
| 1607 | 1674 | |
| 1608 | - // dequeue and get a list of google fonts, or requeue external | |
| 1675 | + // dequeue and get a list of Google fonts, or requeue external | |
| 1609 | 1676 | foreach ($styles->to_do as $handle) { |
| 1610 | 1677 | $href = WP_Optimize_Minify_Functions::get_hurl($wp_styles->registered[$handle]->src); |
| 1611 | 1678 | // inlined scripts without file |
| 1612 | 1679 | if (empty($href)) continue; |
| 1613 | 1680 | |
| 1614 | - if (WP_Optimize_Minify_Functions::is_google_font($href)) { | |
| 1681 | + if (!$disable_google_fonts_processing && WP_Optimize_Minify_Functions::is_google_font($href)) { | |
| 1615 | 1682 | wp_dequeue_style($handle); |
| 1616 | 1683 | if ($this->options['remove_googlefonts']) { |
| 1617 | 1684 | $done = array_merge($done, array($handle)); |
| 1618 | 1685 | continue; |
| @@ -1673,15 +1740,14 @@ | ||
| 1673 | 1740 | WP_Optimize_Minify_Print::inline_style($handle, $href); |
| 1674 | 1741 | $done = array_merge($done, array($handle)); |
| 1675 | 1742 | } else { |
| 1676 | 1743 | // fallback, enqueue google fonts |
| 1677 | - 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 | |
| 1678 | 1745 | } |
| 1679 | 1746 | } |
| 1680 | 1747 | } |
| 1681 | 1748 | } |
| 1682 | 1749 | |
| 1683 | - | |
| 1684 | 1750 | // get groups of handles |
| 1685 | 1751 | $uniq = array(); |
| 1686 | 1752 | foreach ($styles->to_do as $handle) { |
| 1687 | 1753 | |
| @@ -1694,11 +1760,11 @@ | ||
| 1694 | 1760 | $conditional = null; if (isset($wp_styles->registered[$handle]->extra["conditional"])) { |
| 1695 | 1761 | $conditional = $wp_styles->registered[$handle]->extra["conditional"]; // such as ie7, ie8, ie9, etc |
| 1696 | 1762 | } |
| 1697 | 1763 | |
| 1698 | - // mediatype | |
| 1764 | + // media type | |
| 1699 | 1765 | $mt = isset($wp_styles->registered[$handle]->args) ? $wp_styles->registered[$handle]->args : 'all'; |
| 1700 | - if ('screen' == $mt || 'screen, print' == $mt || empty($mt) || is_null($mt) || false == $mt) { | |
| 1766 | + if ('screen' === $mt || 'screen, print' === $mt || empty($mt)) { | |
| 1701 | 1767 | $mt = 'all'; |
| 1702 | 1768 | } |
| 1703 | 1769 | $mediatype = $mt; |
| 1704 | 1770 | |
| @@ -1728,11 +1794,14 @@ | ||
| 1728 | 1794 | if ($ieonly) { |
| 1729 | 1795 | continue; |
| 1730 | 1796 | } |
| 1731 | 1797 | |
| 1798 | + $version = '?ver=' . (!empty($version) ? $version : $wp_styles->default_version); | |
| 1799 | + $href_versioned = $href . $version; | |
| 1800 | + | |
| 1732 | 1801 | // Exclude specific CSS files from PageSpeedIndex? |
| 1733 | - if (false != $async_css && is_array($async_css) && WP_Optimize_Minify_Functions::in_arrayi($href, $async_css)) { | |
| 1734 | - 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); | |
| 1735 | 1804 | $done = array_merge($done, array($handle)); |
| 1736 | 1805 | continue; |
| 1737 | 1806 | } |
| 1738 | 1807 | |
| @@ -1737,17 +1806,17 @@ | ||
| 1737 | 1806 | } |
| 1738 | 1807 | |
| 1739 | 1808 | if (WP_Optimize_Minify_Functions::is_font_awesome($href)) { |
| 1740 | 1809 | if ('inline' === $this->options['fawesome_method']) { |
| 1741 | - WP_Optimize_Minify_Print::inline_style($handle, $href); | |
| 1810 | + WP_Optimize_Minify_Print::inline_style($handle, $href_versioned); | |
| 1742 | 1811 | $done = array_merge($done, array($handle)); |
| 1743 | 1812 | continue; |
| 1744 | 1813 | } elseif ('async' === $this->options['fawesome_method']) { |
| 1745 | - WP_Optimize_Minify_Print::async_style($href, $mediatype); | |
| 1814 | + WP_Optimize_Minify_Print::async_style($href_versioned, $mediatype); | |
| 1746 | 1815 | $done = array_merge($done, array($handle)); |
| 1747 | 1816 | continue; |
| 1748 | 1817 | } elseif ('exclude' === $this->options['fawesome_method']) { |
| 1749 | - WP_Optimize_Minify_Print::exclude_style($href); | |
| 1818 | + WP_Optimize_Minify_Print::exclude_style($href_versioned); | |
| 1750 | 1819 | $done = array_merge($done, array($handle)); |
| 1751 | 1820 | continue; |
| 1752 | 1821 | } |
| 1753 | 1822 | } |
| @@ -1760,14 +1829,14 @@ | ||
| 1760 | 1829 | |
| 1761 | 1830 | // check if the current URL points to a minified file. |
| 1762 | 1831 | $exclude_minified_css = WP_Optimize_Minify_Functions::is_minified_css_js_filename($href); |
| 1763 | 1832 | $normal_processing = $exclude_minified_css && !$merge_css; |
| 1764 | - // skip ignore list, conditional css, external css, font-awesome merge | |
| 1833 | + // skip ignore list, conditional css, external css, font-awesome merge and already minified css | |
| 1765 | 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())) |
| 1766 | 1835 | || empty($href) |
| 1767 | 1836 | ) { |
| 1768 | 1837 | |
| 1769 | - // colect inline css for this handle | |
| 1838 | + // collect inline css for this handle | |
| 1770 | 1839 | if (isset($wp_styles->registered[$handle]->extra['after']) && is_array($wp_styles->registered[$handle]->extra['after'])) { |
| 1771 | 1840 | $inline_css[$handle] = WP_Optimize_Minify_Functions::minify_css_string(implode('', $wp_styles->registered[$handle]->extra['after'])); // save |
| 1772 | 1841 | $wp_styles->registered[$handle]->extra['after'] = null; // dequeue |
| 1773 | 1842 | } |
| @@ -1772,9 +1841,9 @@ | ||
| 1772 | 1841 | $wp_styles->registered[$handle]->extra['after'] = null; // dequeue |
| 1773 | 1842 | } |
| 1774 | 1843 | |
| 1775 | 1844 | // process |
| 1776 | - 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) { | |
| 1777 | 1846 | array_push($footer, array('handles' => array(), 'media' => $mediatype, 'versions' => array())); |
| 1778 | 1847 | } |
| 1779 | 1848 | |
| 1780 | 1849 | // push it to the array get latest modified time |
| @@ -1779,8 +1848,9 @@ | ||
| 1779 | 1848 | |
| 1780 | 1849 | // push it to the array get latest modified time |
| 1781 | 1850 | array_push($footer[count($footer)-1]['handles'], $handle); |
| 1782 | 1851 | array_push($footer[count($footer)-1]['versions'], $version); |
| 1852 | + $footer[count($footer) - 1]['last_modified'][] = WP_Optimize_Minify_Functions::get_modification_time($wp_styles->registered[$handle]->src); | |
| 1783 | 1853 | |
| 1784 | 1854 | // external and ignored css |
| 1785 | 1855 | } else { |
| 1786 | 1856 | |
| @@ -1799,21 +1869,28 @@ | ||
| 1799 | 1869 | $footer = apply_filters('wpo_minify_stylesheets', $footer, 'footer'); |
| 1800 | 1870 | |
| 1801 | 1871 | // loop through footer css and merge |
| 1802 | 1872 | for ($i=0,$l=count($footer); $i<$l; $i++) { |
| 1873 | + | |
| 1803 | 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 | + | |
| 1804 | 1879 | if ($merge_css) { |
| 1805 | 1880 | // get hash for the inline css in this group |
| 1806 | 1881 | $inline_css_group = array(); |
| 1807 | - foreach ($footer[$i]['handles'] as $h) { | |
| 1808 | - if (isset($inline_css[$h]) && !empty($inline_css[$h])) { | |
| 1809 | - $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 | + } | |
| 1810 | 1887 | } |
| 1811 | 1888 | } |
| 1812 | 1889 | $inline_css_hash = md5(implode('', $inline_css_group)); |
| 1813 | - $hash = hash('adler32', implode('', $footer[$i]['handles']) . $inline_css_hash . implode('', $footer[$i]['versions'])); | |
| 1890 | + $hash = hash('adler32', $handles_str . $inline_css_hash . $last_modified_str); | |
| 1814 | 1891 | } else { |
| 1815 | - $hash = implode('', $footer[$i]['handles']) . implode('', $footer[$i]['versions']); | |
| 1892 | + $hash = sanitize_file_name($handles_str . $last_modified_str); | |
| 1816 | 1893 | } |
| 1817 | 1894 | |
| 1818 | 1895 | // static cache file info |
| 1819 | 1896 | $file_name = 'wpo-minify-footer-'.$hash.($minify_css ? '.min' : ''); |
| @@ -1827,9 +1904,9 @@ | ||
| 1827 | 1904 | clearstatcache(); |
| 1828 | 1905 | if (!file_exists($file)) { |
| 1829 | 1906 | |
| 1830 | 1907 | // code and log initialization |
| 1831 | - $log_header = "PROCESSED on ".date('r')." from ".home_url(add_query_arg(null, null)); | |
| 1908 | + $log_header = $this->get_log_header(); | |
| 1832 | 1909 | $log = array( |
| 1833 | 1910 | 'header' => $log_header, |
| 1834 | 1911 | 'files' => array() |
| 1835 | 1912 | ); |
| @@ -1840,11 +1917,13 @@ | ||
| 1840 | 1917 | foreach ($footer[$i]['handles'] as $handle) { |
| 1841 | 1918 | if (!empty($wp_styles->registered[$handle]->src)) { |
| 1842 | 1919 | |
| 1843 | 1920 | // get href per handle |
| 1844 | - $href = WP_Optimize_Minify_Functions::get_hurl($wp_styles->registered[$handle]->src); | |
| 1921 | + $handle_src = $wp_styles->registered[$handle]->src; | |
| 1922 | + $href = WP_Optimize_Minify_Functions::get_hurl($handle_src); | |
| 1845 | 1923 | $version = $wp_styles->registered[$handle]->ver; |
| 1846 | - // inlined scripts without hreffile | |
| 1924 | + $last_modified = WP_Optimize_Minify_Functions::get_modification_time($handle_src); | |
| 1925 | + // inlined scripts without href file | |
| 1847 | 1926 | if (empty($href)) continue; |
| 1848 | 1927 | |
| 1849 | 1928 | // download, minify, cache |
| 1850 | 1929 | $tkey = 'css-'.hash('adler32', $handle . $href).'.css'; |
| @@ -1853,9 +1932,9 @@ | ||
| 1853 | 1932 | if (false === $json) { |
| 1854 | 1933 | $enable_minification = $minify_css && !WP_Optimize_Minify_Functions::is_minified_css_js_filename($href); |
| 1855 | 1934 | $json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $enable_minification, 'css', $handle, $version); |
| 1856 | 1935 | if ($this->options['debug']) { |
| 1857 | - 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"; | |
| 1858 | 1937 | } |
| 1859 | 1938 | WP_Optimize_Minify_Cache_Functions::set_transient($tkey, $json); |
| 1860 | 1939 | } |
| 1861 | 1940 | |
| @@ -1871,10 +1950,12 @@ | ||
| 1871 | 1950 | $meta_log['files_that_changed'][$handle] = $res['log']; |
| 1872 | 1951 | } |
| 1873 | 1952 | |
| 1874 | 1953 | // response has failed |
| 1875 | - if (true != $res['status']) { | |
| 1954 | + if (true !== $res['status']) { | |
| 1876 | 1955 | $log['files'][$handle] = $res['log']; |
| 1956 | + $log['files'][$handle]['version'] = $version; | |
| 1957 | + $log['files'][$handle]['last_modified'] = $last_modified; | |
| 1877 | 1958 | continue; |
| 1878 | 1959 | } |
| 1879 | 1960 | |
| 1880 | 1961 | $done[] = $handle; |
| @@ -1879,10 +1960,12 @@ | ||
| 1879 | 1960 | |
| 1880 | 1961 | $done[] = $handle; |
| 1881 | 1962 | |
| 1882 | 1963 | // append code to merged file |
| 1883 | - $code .= isset($res['code']) ? $res['code'] : ''; | |
| 1964 | + $code .= $res['code'] ?? ''; | |
| 1884 | 1965 | $log['files'][$handle] = $res['log']; |
| 1966 | + $log['files'][$handle]['version'] = $version; | |
| 1967 | + $log['files'][$handle]['last_modified'] = $last_modified; | |
| 1885 | 1968 | |
| 1886 | 1969 | // append inlined styles |
| 1887 | 1970 | if (isset($inline_css[$handle]) && !empty($inline_css[$handle])) { |
| 1888 | 1971 | $code.= $inline_css[$handle]; |
| @@ -1892,15 +1975,15 @@ | ||
| 1892 | 1975 | } else { |
| 1893 | 1976 | wp_dequeue_script($handle); |
| 1894 | 1977 | wp_enqueue_script($handle); |
| 1895 | 1978 | } |
| 1896 | - }; | |
| 1979 | + } | |
| 1897 | 1980 | |
| 1898 | 1981 | // generate cache, add inline css, write log |
| 1899 | 1982 | if (!empty($code)) { |
| 1900 | 1983 | // Only generate meta.json file if cache is reset |
| 1901 | 1984 | if (!empty($meta_log['invalidation_reason'])) { |
| 1902 | - file_put_contents($meta_json_file, json_encode($meta_log)); | |
| 1985 | + file_put_contents($meta_json_file, wp_json_encode($meta_log)); | |
| 1903 | 1986 | } |
| 1904 | 1987 | WP_Optimize_Minify_Print::write_combined_asset($file, $code, $log); |
| 1905 | 1988 | } |
| 1906 | 1989 | } else { |
| @@ -1936,13 +2019,13 @@ | ||
| 1936 | 2019 | if (filesize($file) < 20000 && $this->options['inline_css']) { |
| 1937 | 2020 | $this->inline_css(file_get_contents($file), $handle, $file_url, $footer[$i]['media']); |
| 1938 | 2021 | } else { |
| 1939 | 2022 | // enqueue it |
| 1940 | - 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 | |
| 1941 | 2024 | } |
| 1942 | 2025 | } else { |
| 1943 | 2026 | // file could not be generated, output something meaningful |
| 1944 | - 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))." -->"; | |
| 1945 | 2028 | echo "<!-- Please check if the path above is correct and ensure your server has write permission there! -->"; |
| 1946 | 2029 | } |
| 1947 | 2030 | } |
| 1948 | 2031 | |
| @@ -1957,8 +2040,129 @@ | ||
| 1957 | 2040 | $wp_styles->done = $done; |
| 1958 | 2041 | } |
| 1959 | 2042 | |
| 1960 | 2043 | /** |
| 2044 | + * Process JS Scripts of type module | |
| 2045 | + * | |
| 2046 | + * @param string $buffer Page HTML. | |
| 2047 | + * | |
| 2048 | + * @return string | |
| 2049 | + */ | |
| 2050 | + public function process_script_modules($buffer) { | |
| 2051 | + | |
| 2052 | + if ('' === $buffer) return ''; | |
| 2053 | + | |
| 2054 | + if (!WP_Optimize_Utils::is_valid_html($buffer)) return $buffer; | |
| 2055 | + | |
| 2056 | + $minify_js = $this->options['enable_js_minification']; | |
| 2057 | + $cache_path = WP_Optimize_Minify_Cache_Functions::cache_path(); | |
| 2058 | + $cache_dir = $cache_path['cachedir']; | |
| 2059 | + $cache_dir_url = $cache_path['cachedirurl']; | |
| 2060 | + $cache_base_dir = $cache_path['cachebasedir']; | |
| 2061 | + | |
| 2062 | + $scripts = $this->get_scripts($buffer); | |
| 2063 | + if (empty($scripts)) return $buffer; | |
| 2064 | + | |
| 2065 | + foreach ($scripts as $script) { | |
| 2066 | + if ("module" !== $script['type']) continue; | |
| 2067 | + | |
| 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 | + | |
| 2080 | + $last_modified = WP_Optimize_Minify_Functions::get_modification_time($script['url']); | |
| 2081 | + $handle = $script['handle']; | |
| 2082 | + $version = $script['version']; | |
| 2083 | + | |
| 2084 | + // Module script handles use slashes and @ symbols, | |
| 2085 | + // replace them with '-' and empty string, break if handle is still '' | |
| 2086 | + if (!empty($handle)) { | |
| 2087 | + $handle_no_slash = str_replace(array('/', '@'), array('-', ''), $handle); | |
| 2088 | + } else { | |
| 2089 | + continue; | |
| 2090 | + } | |
| 2091 | + | |
| 2092 | + // code and log initialization | |
| 2093 | + $log_header = $this->get_log_header(); | |
| 2094 | + $log = array( | |
| 2095 | + 'header' => $log_header, | |
| 2096 | + 'files' => array() | |
| 2097 | + ); | |
| 2098 | + $meta_log = $this->meta_log_initialization($log_header); | |
| 2099 | + $code = ''; | |
| 2100 | + | |
| 2101 | + // create cache files and urls | |
| 2102 | + $file_name = 'wpo-minify-'.$handle_no_slash.($minify_js ? '.min' : ''); | |
| 2103 | + $file = $cache_dir.'/'.$file_name.'.js'; | |
| 2104 | + $meta_json_file = $cache_base_dir.'/'.'meta.json'; | |
| 2105 | + $file_url = WP_Optimize_Minify_Functions::get_protocol($cache_dir_url.'/'.$file_name.'.js'); | |
| 2106 | + | |
| 2107 | + // download, minify, cache | |
| 2108 | + $tkey = 'js-'.hash('adler32', $handle_no_slash . $href).'.js'; | |
| 2109 | + $json = WP_Optimize_Minify_Cache_Functions::get_transient($tkey); | |
| 2110 | + if (false === $json) { | |
| 2111 | + $enable_minification = $minify_js && !WP_Optimize_Minify_Functions::is_minified_css_js_filename($href); | |
| 2112 | + $json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $enable_minification, 'js', $handle_no_slash, $version); | |
| 2113 | + if ($this->options['debug']) { | |
| 2114 | + $buffer .= "<!-- wpo_min DEBUG: Uncached file processing now for " . esc_html($handle) . " / " . esc_html($href) . " / " . esc_html($version) . " -->\n"; | |
| 2115 | + } | |
| 2116 | + WP_Optimize_Minify_Cache_Functions::set_transient($tkey, $json); | |
| 2117 | + } | |
| 2118 | + | |
| 2119 | + // decode | |
| 2120 | + $res = json_decode($json, true); | |
| 2121 | + | |
| 2122 | + if (null === $res) continue; | |
| 2123 | + | |
| 2124 | + // Reset minified script module | |
| 2125 | + if ($this->should_reset_minify_assets($res, $href, 'js', $handle, $version)) { | |
| 2126 | + WP_Optimize_Minify_Cache_Functions::reset(); | |
| 2127 | + $this->minify_cache_incremented = true; | |
| 2128 | + $meta_log['invalidation_reason'] = 'Checksum Mismatch and version changed from ' . $res['request']['version'] . ' to ' . $version; | |
| 2129 | + $meta_log['files_that_changed'][$handle] = $res['log']; | |
| 2130 | + } | |
| 2131 | + | |
| 2132 | + if (!$res['status']) continue; | |
| 2133 | + | |
| 2134 | + $code .= $res['code'] ?? ''; | |
| 2135 | + $code = WP_Optimize_Minify_Functions::prepare_merged_js($code, $href); | |
| 2136 | + $log['files'][$handle] = $res['log']; | |
| 2137 | + $log['files'][$handle]['version'] = $version; | |
| 2138 | + $log['files'][$handle]['last_modified'] = $last_modified; | |
| 2139 | + | |
| 2140 | + // If code is empty (nothing to minify) break loop | |
| 2141 | + if (empty($code)) continue; | |
| 2142 | + | |
| 2143 | + // Only generate meta.json file if cache is reset | |
| 2144 | + if (!empty($meta_log['invalidation_reason'])) { | |
| 2145 | + file_put_contents($meta_json_file, wp_json_encode($meta_log)); | |
| 2146 | + } | |
| 2147 | + WP_Optimize_Minify_Print::write_combined_asset($file, $code, $log); | |
| 2148 | + | |
| 2149 | + // Extract importmap script tag | |
| 2150 | + $json_map = $this->get_script_module_importmap($buffer); | |
| 2151 | + $json_map_update = $json_map; | |
| 2152 | + | |
| 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])) { | |
| 2156 | + $json_map_update['imports'][$handle] = $file_url; | |
| 2157 | + $buffer = str_replace(wp_json_encode($json_map), wp_json_encode($json_map_update), $buffer); | |
| 2158 | + } | |
| 2159 | + } | |
| 2160 | + | |
| 2161 | + return $buffer; | |
| 2162 | + } | |
| 2163 | + | |
| 2164 | + /** | |
| 1961 | 2165 | * Orders the CSS per media type |
| 1962 | 2166 | * |
| 1963 | 2167 | * @param array $list - The list of assets |
| 1964 | 2168 | * @return array |
| @@ -2002,9 +2206,9 @@ | ||
| 2002 | 2206 | * @return void; |
| 2003 | 2207 | */ |
| 2004 | 2208 | public function add_google_fonts_merged() { |
| 2005 | 2209 | // must have something to do |
| 2006 | - if (count($this->collect_google_fonts) == 0) return; | |
| 2210 | + if (count($this->collect_google_fonts) === 0) return; | |
| 2007 | 2211 | |
| 2008 | 2212 | // merge google fonts |
| 2009 | 2213 | $href = WP_Optimize_Minify_Fonts::concatenate_google_fonts($this->collect_google_fonts); |
| 2010 | 2214 | if (empty($href)) { |
| @@ -2050,14 +2254,19 @@ | ||
| 2050 | 2254 | // always false for admin pages |
| 2051 | 2255 | if (is_admin()) return false; |
| 2052 | 2256 | |
| 2053 | 2257 | // get host with multisite support and query strings |
| 2054 | - $host = htmlentities($_SERVER['SERVER_NAME']); | |
| 2258 | + $host = isset($_SERVER['SERVER_NAME']) ? sanitize_text_field(wp_unslash($_SERVER['SERVER_NAME'])) : ''; | |
| 2055 | 2259 | if (empty($host)) { |
| 2056 | - $host = isset($_SERVER['HTTP_HOST']) ? htmlentities($_SERVER['HTTP_HOST']) : ''; | |
| 2260 | + $host = isset($_SERVER['HTTP_HOST']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_HOST'])) : ''; | |
| 2057 | 2261 | } |
| 2058 | - $request_query = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY); | |
| 2059 | - $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 | + } | |
| 2060 | 2269 | |
| 2061 | 2270 | // initialize headers |
| 2062 | 2271 | $headers = array(); |
| 2063 | 2272 | |
| @@ -2065,9 +2274,9 @@ | ||
| 2065 | 2274 | if ($this->options['enabled_css_preload'] |
| 2066 | 2275 | && count($this->collect_preload_css) > 0 |
| 2067 | 2276 | ) { |
| 2068 | 2277 | foreach ($this->collect_preload_css as $u) { |
| 2069 | - // 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 | |
| 2070 | 2279 | if (strpos($u, '/assets/wpo-minify-footer-') !== false) { |
| 2071 | 2280 | continue; |
| 2072 | 2281 | } |
| 2073 | 2282 | |
| @@ -2081,9 +2290,9 @@ | ||
| 2081 | 2290 | && count($this->collect_preload_js) > 0 |
| 2082 | 2291 | ) { |
| 2083 | 2292 | foreach ($this->collect_preload_js as $u) { |
| 2084 | 2293 | |
| 2085 | - // 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 | |
| 2086 | 2295 | if (false !== strpos($u, '/assets/wpo-minify-footer-')) { |
| 2087 | 2296 | continue; |
| 2088 | 2297 | } |
| 2089 | 2298 | |
| @@ -2092,9 +2301,9 @@ | ||
| 2092 | 2301 | } |
| 2093 | 2302 | } |
| 2094 | 2303 | |
| 2095 | 2304 | // must have something |
| 2096 | - if (count($headers) == 0) { | |
| 2305 | + if (count($headers) === 0) { | |
| 2097 | 2306 | return false; |
| 2098 | 2307 | } else { |
| 2099 | 2308 | $headers = implode("\n", $headers); |
| 2100 | 2309 | } |
| @@ -2110,9 +2319,9 @@ | ||
| 2110 | 2319 | // reset file cache |
| 2111 | 2320 | clearstatcache(); |
| 2112 | 2321 | |
| 2113 | 2322 | // if there are no query strings |
| 2114 | - if ($b == $a) { | |
| 2323 | + if ($b === $a) { | |
| 2115 | 2324 | if (!file_exists($a)) { |
| 2116 | 2325 | WP_Optimize_Minify_Print::write_header($a, $headers); |
| 2117 | 2326 | } |
| 2118 | 2327 | return false; |
| @@ -2125,13 +2334,13 @@ | ||
| 2125 | 2334 | |
| 2126 | 2335 | /** |
| 2127 | 2336 | * Collect all wpo_min JS files and save them to a headers file |
| 2128 | 2337 | * |
| 2129 | - * @param String $html | |
| 2130 | - * @param String $handle | |
| 2131 | - * @param String $src | |
| 2338 | + * @param string $html | |
| 2339 | + * @param string $handle | |
| 2340 | + * @param string $src | |
| 2132 | 2341 | * |
| 2133 | - * @return String | |
| 2342 | + * @return string | |
| 2134 | 2343 | */ |
| 2135 | 2344 | public function collect_js_preload_headers($html, $handle, $src) { |
| 2136 | 2345 | if (false !== strpos($src, 'assets/wpo-minify')) { |
| 2137 | 2346 | if (!in_array($src, $this->collect_preload_js)) { |
| @@ -2178,14 +2387,18 @@ | ||
| 2178 | 2387 | return false; |
| 2179 | 2388 | } |
| 2180 | 2389 | |
| 2181 | 2390 | // get host with multisite support and query strings |
| 2182 | - $host = htmlentities($_SERVER['SERVER_NAME']); | |
| 2391 | + $host = isset($_SERVER['SERVER_NAME']) ? sanitize_text_field(wp_unslash($_SERVER['SERVER_NAME'])) : ''; | |
| 2183 | 2392 | if (empty($host)) { |
| 2184 | - $host = isset($_SERVER['HTTP_HOST']) ? htmlentities($_SERVER['HTTP_HOST']) : ''; | |
| 2393 | + $host = isset($_SERVER['HTTP_HOST']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_HOST'])) : ''; | |
| 2185 | 2394 | } |
| 2186 | - $request_query = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY); | |
| 2187 | - $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 | + } | |
| 2188 | 2401 | |
| 2189 | 2402 | // get cache path |
| 2190 | 2403 | $cache_path = WP_Optimize_Minify_Cache_Functions::cache_path(); |
| 2191 | 2404 | $header_dir = $cache_path['headerdir']; |
| @@ -2198,12 +2411,12 @@ | ||
| 2198 | 2411 | // reset file cache |
| 2199 | 2412 | clearstatcache(); |
| 2200 | 2413 | |
| 2201 | 2414 | // return header files or fallback |
| 2202 | - if ($b == $a && file_exists($a)) { | |
| 2415 | + if ($b === $a && file_exists($a)) { | |
| 2203 | 2416 | return file_get_contents($a); |
| 2204 | 2417 | } |
| 2205 | - if ($b != $a && file_exists($b)) { | |
| 2418 | + if ($b !== $a && file_exists($b)) { | |
| 2206 | 2419 | return file_get_contents($b); |
| 2207 | 2420 | } |
| 2208 | 2421 | |
| 2209 | 2422 | return false; |
| @@ -2209,9 +2422,9 @@ | ||
| 2209 | 2422 | return false; |
| 2210 | 2423 | } |
| 2211 | 2424 | |
| 2212 | 2425 | /** |
| 2213 | - * Add pre-connect and pre-load headers | |
| 2426 | + * Add pre-connect and preload headers | |
| 2214 | 2427 | * |
| 2215 | 2428 | * @return void |
| 2216 | 2429 | */ |
| 2217 | 2430 | public function extra_preload_headers() { |
| @@ -2220,9 +2433,9 @@ | ||
| 2220 | 2433 | // fetch headers |
| 2221 | 2434 | $pre_connect = $this->get_pre_connect_list(); |
| 2222 | 2435 | |
| 2223 | 2436 | // preload |
| 2224 | - if (is_array($pre_connect) && count($pre_connect) > 0) { | |
| 2437 | + if (count($pre_connect) > 0) { | |
| 2225 | 2438 | foreach ($pre_connect as $url) { |
| 2226 | 2439 | if (!empty($url) && filter_var($url, FILTER_VALIDATE_URL)) { |
| 2227 | 2440 | header("Link: <$url>; rel=preconnect", false); |
| 2228 | 2441 | } |
| @@ -2229,9 +2442,9 @@ | ||
| 2229 | 2442 | } |
| 2230 | 2443 | } |
| 2231 | 2444 | |
| 2232 | 2445 | $headers = $this->get_preload_headers(); |
| 2233 | - if (false != $headers) { | |
| 2446 | + if ($headers) { | |
| 2234 | 2447 | $nh = array_map('trim', explode("\n", $headers)); |
| 2235 | 2448 | foreach ($nh as $h) { |
| 2236 | 2449 | if (!empty($h)) { |
| 2237 | 2450 | header($h, false); |
| @@ -2290,15 +2503,15 @@ | ||
| 2290 | 2503 | /** |
| 2291 | 2504 | * Handles header meta information removal |
| 2292 | 2505 | */ |
| 2293 | 2506 | private function remove_header_meta_info() { |
| 2294 | - // no resource hints, generator tag, shortlinks, manifest link, etc | |
| 2507 | + // no resource hints, generator tag, shortlink, manifest link, etc | |
| 2295 | 2508 | remove_action('wp_head', 'wp_resource_hints', 2); |
| 2296 | 2509 | remove_action('wp_head', 'wp_generator'); |
| 2297 | 2510 | remove_action('template_redirect', 'wp_shortlink_header', 11); |
| 2298 | 2511 | remove_action('wp_head', 'wlwmanifest_link'); |
| 2299 | 2512 | remove_action('wp_head', 'rsd_link'); |
| 2300 | - remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); | |
| 2513 | + remove_action('wp_head', 'adjacent_posts_rel_link_wp_head'); | |
| 2301 | 2514 | remove_action('wp_head', 'feed_links', 2); |
| 2302 | 2515 | remove_action('wp_head', 'feed_links_extra', 3); |
| 2303 | 2516 | WP_Optimize_Minify_Functions::remove_redundant_shortlink(); |
| 2304 | 2517 | } |
| @@ -2324,11 +2537,65 @@ | ||
| 2324 | 2537 | || ( 'all' === $this->options['enable_defer_js'] && 'async_using_js' === $this->options['defer_js_type'] ) |
| 2325 | 2538 | ) { |
| 2326 | 2539 | add_action('wp_head', array('WP_Optimize_Minify_Print', 'add_load_async'), 0); |
| 2327 | 2540 | } |
| 2541 | + | |
| 2542 | + // Process script module from generated html | |
| 2543 | + ob_start(array($this, "process_script_modules")); | |
| 2328 | 2544 | } |
| 2329 | 2545 | |
| 2330 | 2546 | /** |
| 2547 | + * Extract scripts tags from html buffer using regex | |
| 2548 | + * | |
| 2549 | + * @param string $html - HTML document as string | |
| 2550 | + * @return array | |
| 2551 | + */ | |
| 2552 | + private function get_scripts($html) { | |
| 2553 | + | |
| 2554 | + preg_match_all('/<script([^>]*)>/Umi', $html, $script_matches, PREG_SET_ORDER); | |
| 2555 | + | |
| 2556 | + $modified_script_matches = array(); | |
| 2557 | + foreach ($script_matches as $script) { | |
| 2558 | + $script['url'] = ''; | |
| 2559 | + $script['handle'] = ''; | |
| 2560 | + $script['version'] = ''; | |
| 2561 | + $script['type'] = ''; | |
| 2562 | + $attributes = WP_Optimize_Utils::parse_attributes($script[1]); | |
| 2563 | + | |
| 2564 | + if (isset($attributes['src'])) { | |
| 2565 | + $script['url'] = $attributes['src']; | |
| 2566 | + $url_query = wp_parse_url($script['url'], PHP_URL_QUERY); | |
| 2567 | + $output = array(); | |
| 2568 | + wp_parse_str($url_query, $output); | |
| 2569 | + if (isset($output['ver'])) $script['version'] = $output['ver']; | |
| 2570 | + } | |
| 2571 | + | |
| 2572 | + // Extract the id/handle from attribute's array if present | |
| 2573 | + if (isset($attributes['id'])) $script['handle'] = $attributes['id']; | |
| 2574 | + | |
| 2575 | + // Extract the type from attribute's array if present | |
| 2576 | + if (isset($attributes['type'])) $script['type'] = $attributes['type']; | |
| 2577 | + | |
| 2578 | + $modified_script_matches[] = $script; | |
| 2579 | + } | |
| 2580 | + | |
| 2581 | + return $modified_script_matches; | |
| 2582 | + } | |
| 2583 | + | |
| 2584 | + /** | |
| 2585 | + * Extract `importmap` json object from html buffer using regex | |
| 2586 | + * | |
| 2587 | + * @param string $html - HTML document as string | |
| 2588 | + * @return array | |
| 2589 | + */ | |
| 2590 | + private function get_script_module_importmap($html) { | |
| 2591 | + preg_match('/<script\s+type\s*=\s*[\'"]importmap.*>(?<importmap>.*)<\/script>/Umsi', $html, $match); | |
| 2592 | + if (!empty($match) && isset($match['importmap'])) return json_decode($match['importmap'], true); | |
| 2593 | + | |
| 2594 | + return array(); | |
| 2595 | + } | |
| 2596 | + | |
| 2597 | + /** | |
| 2331 | 2598 | * Handles css processing |
| 2332 | 2599 | */ |
| 2333 | 2600 | private function process_css() { |
| 2334 | 2601 | add_action('wp_head', array($this, 'add_critical_path'), 2); |
| @@ -2334,11 +2601,11 @@ | ||
| 2334 | 2601 | add_action('wp_head', array($this, 'add_critical_path'), 2); |
| 2335 | 2602 | |
| 2336 | 2603 | // merge, if inline is selected but prevent optimization for these locations |
| 2337 | 2604 | if ($this->options['inline_css']) { |
| 2338 | - // 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' | |
| 2339 | 2606 | add_filter('style_loader_tag', array($this, 'inline_css'), PHP_INT_MAX, 4); |
| 2340 | - // this prints the google fonts | |
| 2607 | + // this prints the Google fonts | |
| 2341 | 2608 | add_action('wp_print_styles', array($this, 'add_google_fonts_merged'), PHP_INT_MAX); |
| 2342 | 2609 | add_action('wp_print_footer_scripts', array($this, 'add_google_fonts_merged'), PHP_INT_MAX); |
| 2343 | 2610 | } |
| 2344 | 2611 | // Preloading |
| @@ -2344,14 +2611,14 @@ | ||
| 2344 | 2611 | // Preloading |
| 2345 | 2612 | if ($this->options['enabled_css_preload']) { |
| 2346 | 2613 | add_filter('style_loader_tag', array($this, 'collect_css_preload_headers'), PHP_INT_MAX, 3); |
| 2347 | 2614 | } |
| 2348 | - // Optimize the css and collect the google fonts for merging | |
| 2615 | + // Optimize the css and collect the Google fonts for merging | |
| 2349 | 2616 | add_action('wp_print_styles', array($this, 'process_header_css'), PHP_INT_MAX); |
| 2350 | 2617 | add_action('wp_print_footer_scripts', array($this, 'process_footer_css'), 9); |
| 2351 | 2618 | |
| 2352 | 2619 | /** |
| 2353 | - * 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 | |
| 2354 | 2621 | * |
| 2355 | 2622 | * @param boolean $maintain_css_order |
| 2356 | 2623 | * @return boolean |
| 2357 | 2624 | * @default true |
| @@ -2357,14 +2624,16 @@ | ||
| 2357 | 2624 | * @default true |
| 2358 | 2625 | */ |
| 2359 | 2626 | if (!apply_filters('wpo_minify_maintain_css_order', true)) { |
| 2360 | 2627 | // Reorder stylesheets |
| 2361 | - 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')); | |
| 2362 | 2629 | } |
| 2363 | 2630 | } |
| 2364 | 2631 | |
| 2365 | 2632 | /** |
| 2366 | 2633 | * Determines whether we should minify html or not |
| 2634 | + * | |
| 2635 | + * @return bool | |
| 2367 | 2636 | */ |
| 2368 | 2637 | private function should_process_html() { |
| 2369 | 2638 | return $this->options['html_minification'] && !is_admin() && $this->is_cache_preload(); |
| 2370 | 2639 | } |
| @@ -2370,8 +2639,10 @@ | ||
| 2370 | 2639 | } |
| 2371 | 2640 | |
| 2372 | 2641 | /** |
| 2373 | 2642 | * Determines whether to use loadCSS polyfill or not |
| 2643 | + * | |
| 2644 | + * @return bool | |
| 2374 | 2645 | */ |
| 2375 | 2646 | private function should_use_loadCSS() { |
| 2376 | 2647 | return $this->options['loadcss'] |
| 2377 | 2648 | || 'async' === $this->options['fawesome_method'] |
| @@ -2463,13 +2734,13 @@ | ||
| 2463 | 2734 | |
| 2464 | 2735 | /** |
| 2465 | 2736 | * Decides whether minify cache should be incremented or not |
| 2466 | 2737 | * |
| 2467 | - * @param string $res | |
| 2738 | + * @param array $res | |
| 2468 | 2739 | * @param string $href |
| 2469 | 2740 | * @param string $type |
| 2470 | 2741 | * @param string $handle |
| 2471 | - * @param string $version | |
| 2742 | + * @param mixed $version | |
| 2472 | 2743 | * |
| 2473 | 2744 | * @return bool true when cache is invalid, false otherwise |
| 2474 | 2745 | */ |
| 2475 | 2746 | private function should_reset_minify_assets($res, $href, $type, $handle, $version) { |
| @@ -2489,6 +2760,18 @@ | ||
| 2489 | 2760 | $hash = hash('sha256', $res['code']); |
| 2490 | 2761 | $new_hash = hash('sha256', $new_res['code']); |
| 2491 | 2762 | return strcmp($hash, $new_hash); |
| 2492 | 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; | |
| 2493 | 2776 | } |
| 2494 | 2777 | } |