| @@ -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,30 +91,54 @@ | ||
| 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 | - * Wether to run the feature on a page or not | |
| 114 | + * Set initial values for the meta log object | |
| 99 | 115 | * |
| 116 | + * @param string $header - Info on time the log was processed | |
| 117 | + * @return array | |
| 118 | + */ | |
| 119 | + private function meta_log_initialization($header) { | |
| 120 | + return array( | |
| 121 | + 'header' => $header, | |
| 122 | + 'invalidation_reason' => "", | |
| 123 | + 'files_that_changed' => array() | |
| 124 | + ); | |
| 125 | + } | |
| 126 | + | |
| 127 | + /** | |
| 128 | + * Whether to run the feature on a page or not | |
| 129 | + * | |
| 100 | 130 | * @param string $context - Optional, The context where the check is done |
| 101 | 131 | * @return boolean |
| 102 | 132 | */ |
| 103 | 133 | public function run_on_page($context = 'default') { |
| 104 | 134 | /** |
| 105 | - * Filters wether the functionality is ran on the current page. | |
| 135 | + * Filters whether the functionality is run on the current page. | |
| 106 | 136 | * |
| 107 | 137 | * @param boolean $run_on_page |
| 108 | 138 | * @param string $context - Optional, The feature where the check is done |
| 109 | 139 | */ |
| 110 | - return apply_filters( | |
| 140 | + $filtered_value = apply_filters( | |
| 111 | 141 | 'wpo_minify_run_on_page', |
| 112 | 142 | !is_admin() |
| 113 | 143 | && (!defined('SCRIPT_DEBUG') || !SCRIPT_DEBUG) |
| 114 | 144 | && !is_preview() |
| @@ -117,19 +147,20 @@ | ||
| 117 | 147 | && !(function_exists('is_amp_endpoint') && is_amp_endpoint()) |
| 118 | 148 | && !WP_Optimize_Minify_Functions::exclude_contents(), |
| 119 | 149 | $context |
| 120 | 150 | ); |
| 151 | + return is_bool($filtered_value) ? $filtered_value : false; | |
| 121 | 152 | } |
| 122 | 153 | |
| 123 | 154 | /** |
| 124 | 155 | * Inline css in place, instead of inlining the large file |
| 125 | 156 | * |
| 126 | - * @param String $html | |
| 127 | - * @param String $handle | |
| 128 | - * @param String $href | |
| 129 | - * @param String $media | |
| 157 | + * @param string $html | |
| 158 | + * @param string $handle | |
| 159 | + * @param string $href | |
| 160 | + * @param mixed $media | |
| 130 | 161 | * |
| 131 | - * @return String | |
| 162 | + * @return string|boolean | |
| 132 | 163 | */ |
| 133 | 164 | public function inline_css($html, $handle, $href, $media) { |
| 134 | 165 | $exclude_css = $this->get_excluded_css_list(); |
| 135 | 166 | $ignore_list = WP_Optimize_Minify_Functions::compile_ignore_list($exclude_css); |
| @@ -140,9 +171,9 @@ | ||
| 140 | 171 | // make sure href is complete |
| 141 | 172 | $href = WP_Optimize_Minify_Functions::get_hurl($href); |
| 142 | 173 | |
| 143 | 174 | if ($this->options['debug']) { |
| 144 | - 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"; | |
| 145 | 176 | } |
| 146 | 177 | |
| 147 | 178 | |
| 148 | 179 | // skip all this, if the async css option is enabled |
| @@ -152,17 +183,16 @@ | ||
| 152 | 183 | // leave conditionals alone |
| 153 | 184 | if (wp_styles()->get_data($handle, 'conditional')) return $html; |
| 154 | 185 | |
| 155 | 186 | // mediatype fix for some plugins + remove print mediatypes |
| 156 | - if ('screen' == $media | |
| 157 | - || 'screen, print' == $media | |
| 158 | - || empty($media) | |
| 159 | - || is_null($media) | |
| 160 | - || false == $media | |
| 187 | + if (!is_string($media) | |
| 188 | + || '' === $media | |
| 189 | + || 'screen' === $media | |
| 190 | + || 'screen, print' === $media | |
| 161 | 191 | ) { |
| 162 | 192 | $media = 'all'; |
| 163 | 193 | } |
| 164 | - if (!empty($this->options['remove_print_mediatypes']) && 'print' == $media) { | |
| 194 | + if (!empty($this->options['remove_print_mediatypes']) && 'print' === $media) { | |
| 165 | 195 | return false; |
| 166 | 196 | } |
| 167 | 197 | |
| 168 | 198 | // Exclude specific CSS files from PageSpeedInsights? |
| @@ -178,12 +208,17 @@ | ||
| 178 | 208 | if (count($master_ignore) > 0 && WP_Optimize_Minify_Functions::in_arrayi($href, $master_ignore)) { |
| 179 | 209 | return $html; |
| 180 | 210 | } |
| 181 | 211 | |
| 182 | - // check if working with a font awesom link | |
| 212 | + // return if already minified | |
| 213 | + if (WP_Optimize_Minify_Functions::is_minified_css_js_filename($href)) { | |
| 214 | + return $html; | |
| 215 | + } | |
| 216 | + | |
| 217 | + // check if working with a font awesome link | |
| 183 | 218 | if (WP_Optimize_Minify_Functions::is_font_awesome($href)) { |
| 184 | 219 | // font awesome processing, async css |
| 185 | - if ('async' == $this->options['fawesome_method']) { | |
| 220 | + if ('async' === $this->options['fawesome_method']) { | |
| 186 | 221 | WP_Optimize_Minify_Print::async_style($href, $media); |
| 187 | 222 | return false; |
| 188 | 223 | } elseif ('exclude' === $this->options['fawesome_method']) { |
| 189 | 224 | // font awesome processing, async and exclude from PageSpeedIndex |
| @@ -188,17 +223,19 @@ | ||
| 188 | 223 | } elseif ('exclude' === $this->options['fawesome_method']) { |
| 189 | 224 | // font awesome processing, async and exclude from PageSpeedIndex |
| 190 | 225 | WP_Optimize_Minify_Print::exclude_style($href); |
| 191 | 226 | return false; |
| 192 | - } elseif ('inline' == $this->options['fawesome_method']) { | |
| 227 | + } elseif ('inline' === $this->options['fawesome_method']) { | |
| 193 | 228 | WP_Optimize_Minify_Print::inline_style($handle, $href); |
| 194 | 229 | return false; |
| 195 | 230 | } |
| 196 | 231 | } |
| 197 | 232 | |
| 198 | - // Check if working with google font url | |
| 233 | + // Check if working with Google font url | |
| 199 | 234 | |
| 200 | - 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; | |
| 201 | 238 | // check if google fonts should be removed |
| 202 | 239 | if ($this->options['remove_googlefonts']) return false; |
| 203 | 240 | // check if google fonts should be merged |
| 204 | 241 | if ($this->options['merge_google_fonts']) { |
| @@ -220,9 +257,9 @@ | ||
| 220 | 257 | $json = WP_Optimize_Minify_Cache_Functions::get_transient($tkey); |
| 221 | 258 | if (false === $json) { |
| 222 | 259 | $json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $this->options['enable_css_minification'], 'css', $handle); |
| 223 | 260 | if ($this->options['debug']) { |
| 224 | - 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"; | |
| 225 | 262 | } |
| 226 | 263 | WP_Optimize_Minify_Cache_Functions::set_transient($tkey, $json); |
| 227 | 264 | } |
| 228 | 265 | |
| @@ -229,18 +266,20 @@ | ||
| 229 | 266 | // decode |
| 230 | 267 | $res = json_decode($json, true); |
| 231 | 268 | |
| 232 | 269 | // inline css or fail |
| 233 | - if (null !== $res && false != $res['status']) { | |
| 270 | + if (null !== $res && false !== $res['status']) { | |
| 234 | 271 | // add font-display |
| 235 | 272 | // https://developers.google.com/web/updates/2016/02/font-display |
| 236 | 273 | $res['code'] = str_ireplace('font-style:normal;', 'font-display:block;font-style:normal;', $res['code']); |
| 237 | 274 | |
| 238 | - 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"; | |
| 239 | 278 | return false; |
| 240 | 279 | } else { |
| 241 | 280 | if ($this->options['debug']) { |
| 242 | - 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"; | |
| 243 | 282 | } |
| 244 | 283 | return $html; |
| 245 | 284 | } |
| 246 | 285 | } elseif ('async' === $this->options['gfonts_method']) { |
| @@ -272,9 +311,9 @@ | ||
| 272 | 311 | $json = WP_Optimize_Minify_Cache_Functions::get_transient($tkey); |
| 273 | 312 | if (false === $json) { |
| 274 | 313 | $json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $this->options['enable_css_minification'], 'css', $handle); |
| 275 | 314 | if ($this->options['debug']) { |
| 276 | - 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"; | |
| 277 | 316 | } |
| 278 | 317 | WP_Optimize_Minify_Cache_Functions::set_transient($tkey, $json); |
| 279 | 318 | } |
| 280 | 319 | |
| @@ -281,21 +320,25 @@ | ||
| 281 | 320 | // decode |
| 282 | 321 | $res = json_decode($json, true); |
| 283 | 322 | |
| 284 | 323 | // inline it + other inlined children styles |
| 285 | - if (null !== $res && false != $res['status']) { | |
| 286 | - 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"; | |
| 287 | 328 | |
| 288 | 329 | // get inline_styles for this handle, minify and print |
| 289 | 330 | $inline_styles = array(); |
| 290 | 331 | $inline_styles = wp_styles()->get_data($handle, 'after'); |
| 291 | - if (false != $inline_styles) { | |
| 332 | + if ($inline_styles) { | |
| 292 | 333 | |
| 293 | 334 | // string type |
| 294 | 335 | if (is_string($inline_styles)) { |
| 295 | 336 | $code = WP_Optimize_Minify_Functions::get_css($href, $inline_styles, $this->options['enable_css_minification']); |
| 296 | - if (!empty($code) && false != $code) { | |
| 297 | - 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"; | |
| 298 | 341 | } |
| 299 | 342 | } |
| 300 | 343 | |
| 301 | 344 | // array type |
| @@ -301,10 +344,12 @@ | ||
| 301 | 344 | // array type |
| 302 | 345 | if (is_array($inline_styles)) { |
| 303 | 346 | foreach ($inline_styles as $st) { |
| 304 | 347 | $code = WP_Optimize_Minify_Functions::get_css($href, $st, $this->options['enable_css_minification']); |
| 305 | - if (!empty($code) && false != $code) { | |
| 306 | - 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"; | |
| 307 | 352 | } |
| 308 | 353 | } |
| 309 | 354 | } |
| 310 | 355 | } |
| @@ -312,9 +357,9 @@ | ||
| 312 | 357 | // prevent default |
| 313 | 358 | return false; |
| 314 | 359 | } else { |
| 315 | 360 | if ($this->options['debug']) { |
| 316 | - 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"; | |
| 317 | 362 | } |
| 318 | 363 | return $html; |
| 319 | 364 | } |
| 320 | 365 | echo "<!-- ERROR: WPO-Minify couldn't catch the CSS file below. Please report this on https://wordpress.org/support/plugin/wp-optimize/ -->\n"; |
| @@ -321,15 +366,15 @@ | ||
| 321 | 366 | return $html; |
| 322 | 367 | } |
| 323 | 368 | |
| 324 | 369 | /** |
| 325 | - * 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 | |
| 326 | 371 | * |
| 327 | - * @param String $tag | |
| 328 | - * @param String $handle | |
| 329 | - * @param String $src | |
| 372 | + * @param string $tag | |
| 373 | + * @param string $handle | |
| 374 | + * @param string $src | |
| 330 | 375 | * |
| 331 | - * @return String | |
| 376 | + * @return string | |
| 332 | 377 | */ |
| 333 | 378 | public function defer_js($tag, $handle, $src) { |
| 334 | 379 | $wp_domain = trim(str_ireplace(array('http://', 'https://'), '', trim(site_url(), '/'))); |
| 335 | 380 | $exclude_js = $this->get_excluded_js_list(); |
| @@ -335,10 +380,11 @@ | ||
| 335 | 380 | $exclude_js = $this->get_excluded_js_list(); |
| 336 | 381 | $ignore_list = WP_Optimize_Minify_Functions::compile_ignore_list($exclude_js); |
| 337 | 382 | // Should this defer the Poly fills for IE? |
| 338 | 383 | $blacklist = WP_Optimize_Minify_Functions::get_ie_blacklist(); |
| 384 | + | |
| 339 | 385 | // no query strings |
| 340 | - if (false !== stripos($src, '?ver')) { | |
| 386 | + if (false !== stripos($src, '?ver') && WP_Optimize_Minify_Functions::is_already_minified($src)) { | |
| 341 | 387 | $srcf = stristr($src, '?ver', true); |
| 342 | 388 | $tag = str_ireplace($src, $srcf, $tag); |
| 343 | 389 | $src = $srcf; |
| 344 | 390 | } |
| @@ -373,9 +419,9 @@ | ||
| 373 | 419 | } |
| 374 | 420 | |
| 375 | 421 | // should we exclude defer on the login page? |
| 376 | 422 | if ($this->options['exclude_defer_login'] |
| 377 | - && 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(), '/')) | |
| 378 | 424 | ) { |
| 379 | 425 | return $tag; |
| 380 | 426 | } |
| 381 | 427 | |
| @@ -425,11 +471,15 @@ | ||
| 425 | 471 | * @return void |
| 426 | 472 | */ |
| 427 | 473 | public function add_critical_path() { |
| 428 | 474 | if (is_front_page() && !empty($this->options['critical_path_css_is_front_page'])) { |
| 429 | - 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"; | |
| 430 | 478 | } elseif (!empty($this->options['critical_path_css'])) { |
| 431 | - 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"; | |
| 432 | 482 | } |
| 433 | 483 | } |
| 434 | 484 | |
| 435 | 485 | /** |
| @@ -457,8 +507,9 @@ | ||
| 457 | 507 | global $wp_styles; |
| 458 | 508 | if (!is_object($wp_styles)) return false; |
| 459 | 509 | |
| 460 | 510 | $cache_path = WP_Optimize_Minify_Cache_Functions::cache_path(); |
| 511 | + $cache_base_dir = $cache_path['cachebasedir']; | |
| 461 | 512 | $cache_dir = $cache_path['cachedir']; |
| 462 | 513 | $cache_dir_url = $cache_path['cachedirurl']; |
| 463 | 514 | $exclude_css = $this->get_excluded_css_list(); |
| 464 | 515 | $ignore_list = WP_Optimize_Minify_Functions::compile_ignore_list($exclude_css); |
| @@ -467,8 +518,9 @@ | ||
| 467 | 518 | $minify_css = $this->options['enable_css_minification']; |
| 468 | 519 | $merge_css = $this->options['enable_merging_of_css']; |
| 469 | 520 | $merge_inline_extra_css_js = $this->options['merge_inline_extra_css_js']; |
| 470 | 521 | $process_css = $minify_css || $merge_css; |
| 522 | + $disable_google_fonts_processing = $this->options['disable_google_fonts_processing']; | |
| 471 | 523 | $styles = clone $wp_styles; |
| 472 | 524 | $styles->all_deps($styles->queue); |
| 473 | 525 | $done = $styles->done; |
| 474 | 526 | $header = array(); |
| @@ -497,9 +549,9 @@ | ||
| 497 | 549 | } |
| 498 | 550 | |
| 499 | 551 | // mediatype |
| 500 | 552 | $mt = isset($wp_styles->registered[$handle]->args) ? $wp_styles->registered[$handle]->args : 'all'; |
| 501 | - 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) { | |
| 502 | 554 | $mt = 'all'; |
| 503 | 555 | } |
| 504 | 556 | $mediatype = $mt; |
| 505 | 557 | |
| @@ -521,11 +573,15 @@ | ||
| 521 | 573 | } else { |
| 522 | 574 | $uniq[$key] = $handle; |
| 523 | 575 | } |
| 524 | 576 | } |
| 577 | + | |
| 578 | + $version = '?ver=' . (!empty($version) ? $version : $wp_styles->default_version); | |
| 579 | + $href_versioned = $href . $version; | |
| 580 | + | |
| 525 | 581 | // Exclude specific CSS files from PageSpeedIndex? |
| 526 | 582 | if (WP_Optimize_Minify_Functions::in_arrayi($href, $async_css)) { |
| 527 | - WP_Optimize_Minify_Print::exclude_style($href); | |
| 583 | + WP_Optimize_Minify_Print::exclude_style($href_versioned); | |
| 528 | 584 | $done = array_merge($done, array($handle)); |
| 529 | 585 | continue; |
| 530 | 586 | } |
| 531 | 587 | // Fonts Awesome Processing |
| @@ -530,17 +586,17 @@ | ||
| 530 | 586 | } |
| 531 | 587 | // Fonts Awesome Processing |
| 532 | 588 | if (WP_Optimize_Minify_Functions::is_font_awesome($href)) { |
| 533 | 589 | if ('inline' === $this->options['fawesome_method']) { |
| 534 | - WP_Optimize_Minify_Print::inline_style($handle, $href); | |
| 590 | + WP_Optimize_Minify_Print::inline_style($handle, $href_versioned); | |
| 535 | 591 | $done = array_merge($done, array($handle)); |
| 536 | 592 | continue; |
| 537 | 593 | } elseif ('async' === $this->options['fawesome_method']) { |
| 538 | - WP_Optimize_Minify_Print::async_style($href, $mediatype); | |
| 594 | + WP_Optimize_Minify_Print::async_style($href_versioned, $mediatype); | |
| 539 | 595 | $done = array_merge($done, array($handle)); |
| 540 | 596 | continue; |
| 541 | 597 | } elseif ('exclude' === $this->options['fawesome_method']) { |
| 542 | - WP_Optimize_Minify_Print::exclude_style($href); | |
| 598 | + WP_Optimize_Minify_Print::exclude_style($href_versioned); | |
| 543 | 599 | $done = array_merge($done, array($handle)); |
| 544 | 600 | continue; |
| 545 | 601 | } |
| 546 | 602 | } |
| @@ -559,9 +615,9 @@ | ||
| 559 | 615 | 'mediatype' => $mediatype |
| 560 | 616 | ); |
| 561 | 617 | |
| 562 | 618 | // google fonts to the top (collect and skip process array) |
| 563 | - if (WP_Optimize_Minify_Functions::is_google_font($href)) { | |
| 619 | + if (!$disable_google_fonts_processing && WP_Optimize_Minify_Functions::is_google_font($href)) { | |
| 564 | 620 | if ($this->options['remove_googlefonts']) { |
| 565 | 621 | $done = array_merge($done, array($handle)); |
| 566 | 622 | continue; |
| 567 | 623 | } |
| @@ -580,9 +636,9 @@ | ||
| 580 | 636 | // merge google fonts if force inlining is enabled? |
| 581 | 637 | $nfonts = array(); |
| 582 | 638 | if ($this->options['merge_google_fonts']) { |
| 583 | 639 | $nfonts[] = WP_Optimize_Minify_Fonts::concatenate_google_fonts($google_fonts); |
| 584 | - // 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 | |
| 585 | 641 | if ('inherit' !== $this->options['gfonts_method']) { |
| 586 | 642 | $done = array_merge($done, array_keys($google_fonts)); |
| 587 | 643 | } |
| 588 | 644 | } else { |
| @@ -604,9 +660,9 @@ | ||
| 604 | 660 | // check if the cache is empty or has no code |
| 605 | 661 | if (null === $json || false === $json || empty($json['code'])) { |
| 606 | 662 | $res = WP_Optimize_Minify_Functions::download_and_minify($href, null, $minify_css, 'css', null); |
| 607 | 663 | if ($this->options['debug']) { |
| 608 | - 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"; | |
| 609 | 665 | } |
| 610 | 666 | WP_Optimize_Minify_Cache_Functions::set_transient($tkey, $res); |
| 611 | 667 | // decode |
| 612 | 668 | $json = json_decode($res, true); |
| @@ -616,14 +672,16 @@ | ||
| 616 | 672 | if (null !== $json && !empty($json['code'])) { |
| 617 | 673 | // add font-display |
| 618 | 674 | // https://developers.google.com/web/updates/2016/02/font-display |
| 619 | 675 | $json['code'] = str_ireplace('font-style:normal;', 'font-display:block;font-style:normal;', $json['code']); |
| 620 | - 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"; | |
| 621 | 679 | $done = array_merge($done, array($handle)); |
| 622 | 680 | } else { |
| 623 | - echo "<!-- GOOGLE FONTS REQUEST FAILED for $href -->" . "\n"; | |
| 681 | + echo "<!-- GOOGLE FONTS REQUEST FAILED for " . esc_html($href) . " -->" . "\n"; | |
| 624 | 682 | // inlining failed, so enqueue again |
| 625 | - 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 | |
| 626 | 684 | } |
| 627 | 685 | } elseif ('async' === $this->options['gfonts_method']) { |
| 628 | 686 | WP_Optimize_Minify_Print::async_style($href); |
| 629 | 687 | $done = array_merge($done, array($handle)); |
| @@ -665,16 +723,19 @@ | ||
| 665 | 723 | $file_size = WP_Optimize_Minify_Functions::get_file_size($href); |
| 666 | 724 | |
| 667 | 725 | // If we can't determine file size, then we still need to proceed with normal minify process |
| 668 | 726 | if (!apply_filters('wp_optimize_skip_inlining', false === $file_size || $file_size > 20480 || !$this->options['inline_css'], $file_size, $href)) continue; |
| 669 | - | |
| 670 | - // skip ignore list, conditional css, external css, font-awesome merge | |
| 671 | - if (($process_css && !WP_Optimize_Minify_Functions::in_arrayi($href, $ignore_list) && !isset($conditional) && WP_Optimize_Minify_Functions::internal_url($href, site_url())) | |
| 727 | + | |
| 728 | + // check if the current URL points to a minified file. | |
| 729 | + $exclude_minified_css = WP_Optimize_Minify_Functions::is_minified_css_js_filename($href); | |
| 730 | + $normal_processing = $exclude_minified_css && !$merge_css; | |
| 731 | + // skip ignore list, conditional css, external css, font-awesome merge and already minified js | |
| 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())) | |
| 672 | 733 | || empty($href) |
| 673 | - || ($process_css && 'inherit' == $this->options['fawesome_method'] && WP_Optimize_Minify_Functions::is_font_awesome($href)) | |
| 674 | - || ($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)) | |
| 675 | 736 | ) { |
| 676 | - // colect inline css for this handle | |
| 737 | + // collect inline css for this handle | |
| 677 | 738 | if (isset($wp_styles->registered[$handle]->extra['after']) && is_array($wp_styles->registered[$handle]->extra['after'])) { |
| 678 | 739 | $inline_css[$handle] = WP_Optimize_Minify_Functions::minify_css_string(implode('', $wp_styles->registered[$handle]->extra['after'])); // save |
| 679 | 740 | $wp_styles->registered[$handle]->extra['after'] = null; // dequeue |
| 680 | 741 | } |
| @@ -679,9 +740,9 @@ | ||
| 679 | 740 | $wp_styles->registered[$handle]->extra['after'] = null; // dequeue |
| 680 | 741 | } |
| 681 | 742 | |
| 682 | 743 | // process |
| 683 | - 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) { | |
| 684 | 745 | array_push($header, array('handles' => array(), 'media' => $mediatype, 'versions' => array())); |
| 685 | 746 | } |
| 686 | 747 | |
| 687 | 748 | // push it to the array |
| @@ -691,11 +752,12 @@ | ||
| 691 | 752 | // https://plugins.trac.wordpress.org/browser/mailin/tags/3.1.54/sendinblue.php#L456 |
| 692 | 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; |
| 693 | 754 | array_push($header[count($header)-1]['versions'], $version_str); |
| 694 | 755 | |
| 756 | + $header[count($header) - 1]['last_modified'][] = WP_Optimize_Minify_Functions::get_modification_time($wp_styles->registered[$handle]->src); | |
| 757 | + | |
| 695 | 758 | // external and ignored css |
| 696 | 759 | } else { |
| 697 | - | |
| 698 | 760 | // normal enqueuing |
| 699 | 761 | array_push($header, array('handle' => $handle)); |
| 700 | 762 | } |
| 701 | 763 | } |
| @@ -710,25 +772,29 @@ | ||
| 710 | 772 | $header = apply_filters('wpo_minify_stylesheets', $header, 'header'); |
| 711 | 773 | |
| 712 | 774 | // loop through header css and merge |
| 713 | 775 | for ($i=0,$l=count($header); $i<$l; $i++) { |
| 776 | + | |
| 714 | 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 | + | |
| 715 | 782 | if ($merge_css) { |
| 716 | 783 | // get hash for the inline css in this group |
| 717 | 784 | $inline_css_group = array(); |
| 718 | - foreach ($header[$i]['handles'] as $h) { | |
| 719 | - if (!empty($inline_css) && isset($inline_css[$h]) && !empty($inline_css[$h])) { | |
| 720 | - $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 | + } | |
| 721 | 791 | } |
| 722 | 792 | } |
| 723 | 793 | $inline_css_hash = md5(implode('', $inline_css_group)); |
| 724 | - // We need this because some plugins/theme use empty array as version | |
| 725 | - // https://plugins.trac.wordpress.org/browser/mailin/tags/3.1.54/sendinblue.php#L456 | |
| 726 | - $version_str = $this->array_to_string_conversion($header[$i]['versions']); | |
| 727 | - $handle_str = $this->array_to_string_conversion($header[$i]['handles']); | |
| 728 | - $hash = hash('adler32', $handle_str . $inline_css_hash . $version_str); | |
| 794 | + $hash = hash('adler32', $handle_str . $inline_css_hash . $last_modified_str); | |
| 729 | 795 | } else { |
| 730 | - $hash = implode('', $header[$i]['handles']) . implode('', $header[$i]['versions']); | |
| 796 | + $hash = sanitize_file_name($handle_str . $last_modified_str); | |
| 731 | 797 | } |
| 732 | 798 | |
| 733 | 799 | // static cache file info |
| 734 | 800 | $file_name = 'wpo-minify-header-'.$hash.($minify_css ? '.min' : ''); |
| @@ -734,9 +800,9 @@ | ||
| 734 | 800 | $file_name = 'wpo-minify-header-'.$hash.($minify_css ? '.min' : ''); |
| 735 | 801 | |
| 736 | 802 | // create cache files and urls |
| 737 | 803 | $file = $cache_dir.'/'.$file_name.'.css'; |
| 738 | - | |
| 804 | + $meta_json_file = $cache_base_dir.'/'.'meta.json'; | |
| 739 | 805 | $file_url = WP_Optimize_Minify_Functions::get_protocol("$cache_dir_url/$file_name.css"); |
| 740 | 806 | |
| 741 | 807 | // generate a new cache file |
| 742 | 808 | clearstatcache(); |
| @@ -742,12 +808,14 @@ | ||
| 742 | 808 | clearstatcache(); |
| 743 | 809 | if (!file_exists($file)) { |
| 744 | 810 | |
| 745 | 811 | // code and log initialization |
| 812 | + $log_header = $this->get_log_header(); | |
| 746 | 813 | $log = array( |
| 747 | - 'header' => "PROCESSED on ".date('r')." from ".home_url(add_query_arg(null, null)), | |
| 814 | + 'header' => $log_header, | |
| 748 | 815 | 'files' => array() |
| 749 | 816 | ); |
| 817 | + $meta_log = $this->meta_log_initialization($log_header); | |
| 750 | 818 | $code = ''; |
| 751 | 819 | |
| 752 | 820 | // minify and write to file |
| 753 | 821 | foreach ($header[$i]['handles'] as $handle) { |
| @@ -753,10 +821,12 @@ | ||
| 753 | 821 | foreach ($header[$i]['handles'] as $handle) { |
| 754 | 822 | if (!empty($wp_styles->registered[$handle]->src)) { |
| 755 | 823 | |
| 756 | 824 | // get href per handle |
| 757 | - $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); | |
| 758 | 827 | $version = $wp_styles->registered[$handle]->ver; |
| 828 | + $last_modified = WP_Optimize_Minify_Functions::get_modification_time($handle_src); | |
| 759 | 829 | |
| 760 | 830 | // inlined scripts without file |
| 761 | 831 | if (empty($href)) continue; |
| 762 | 832 | |
| @@ -764,11 +834,12 @@ | ||
| 764 | 834 | $tkey = 'css-'.hash('adler32', $handle . $href).'.css'; |
| 765 | 835 | $json = false; |
| 766 | 836 | $json = WP_Optimize_Minify_Cache_Functions::get_transient($tkey); |
| 767 | 837 | if (false === $json) { |
| 768 | - $json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $this->options['enable_css_minification'], 'css', $handle, $version); | |
| 838 | + $enable_minification = $this->options['enable_css_minification'] && !WP_Optimize_Minify_Functions::is_minified_css_js_filename($href); | |
| 839 | + $json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $enable_minification, 'css', $handle, $version); | |
| 769 | 840 | if ($this->options['debug']) { |
| 770 | - 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"; | |
| 771 | 842 | } |
| 772 | 843 | WP_Optimize_Minify_Cache_Functions::set_transient($tkey, $json); |
| 773 | 844 | } |
| 774 | 845 | |
| @@ -779,13 +850,17 @@ | ||
| 779 | 850 | |
| 780 | 851 | if ($this->should_reset_minify_assets($res, $href, 'css', $handle, $version)) { |
| 781 | 852 | WP_Optimize_Minify_Cache_Functions::reset(); |
| 782 | 853 | $this->minify_cache_incremented = true; |
| 854 | + $meta_log['invalidation_reason'] = 'Checksum Mismatch and version changed from ' . $res['request']['version'] . ' to ' . $version; | |
| 855 | + $meta_log['files_that_changed'][$handle] = $res['log']; | |
| 783 | 856 | } |
| 784 | 857 | |
| 785 | 858 | // response has failed |
| 786 | - if (true != $res['status']) { | |
| 859 | + if (true !== $res['status']) { | |
| 787 | 860 | $log['files'][$handle] = $res['log']; |
| 861 | + $log['files'][$handle]['version'] = $version; | |
| 862 | + $log['files'][$handle]['last_modified'] = $last_modified; | |
| 788 | 863 | continue; |
| 789 | 864 | } |
| 790 | 865 | |
| 791 | 866 | // Only add the $handle to $done if it was successfully downloaded |
| @@ -790,10 +865,12 @@ | ||
| 790 | 865 | |
| 791 | 866 | // Only add the $handle to $done if it was successfully downloaded |
| 792 | 867 | $done[] = $handle; |
| 793 | 868 | // append code to merged file |
| 794 | - $code .= isset($res['code']) ? $res['code'] : ''; | |
| 869 | + $code .= $res['code'] ?? ''; | |
| 795 | 870 | $log['files'][$handle] = $res['log']; |
| 871 | + $log['files'][$handle]['version'] = $version; | |
| 872 | + $log['files'][$handle]['last_modified'] = $last_modified; | |
| 796 | 873 | |
| 797 | 874 | // append inlined styles |
| 798 | 875 | if ($merge_inline_extra_css_js && isset($inline_css[$handle]) && !empty($inline_css[$handle])) { |
| 799 | 876 | $code.= $inline_css[$handle]; |
| @@ -807,8 +884,12 @@ | ||
| 807 | 884 | }; |
| 808 | 885 | |
| 809 | 886 | // generate cache, write log |
| 810 | 887 | if (!empty($code)) { |
| 888 | + // Only generate meta.json file if cache is reset | |
| 889 | + if (!empty($meta_log['invalidation_reason'])) { | |
| 890 | + file_put_contents($meta_json_file, wp_json_encode($meta_log)); | |
| 891 | + } | |
| 811 | 892 | WP_Optimize_Minify_Print::write_combined_asset($file, $code, $log); |
| 812 | 893 | } |
| 813 | 894 | } else { |
| 814 | 895 | $log_file = $file.'.json'; |
| @@ -848,9 +929,9 @@ | ||
| 848 | 929 | } |
| 849 | 930 | } |
| 850 | 931 | } else { |
| 851 | 932 | // file could not be generated, output something meaningful |
| 852 | - 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))." -->"; | |
| 853 | 934 | echo "<!-- Please check if the path above is correct and ensure your server has write permission there! -->"; |
| 854 | 935 | } |
| 855 | 936 | } |
| 856 | 937 | // other css need to be requeued for the order of files to be kept |
| @@ -875,8 +956,9 @@ | ||
| 875 | 956 | if (!is_object($wp_scripts)) { |
| 876 | 957 | return; |
| 877 | 958 | } |
| 878 | 959 | $cache_path = WP_Optimize_Minify_Cache_Functions::cache_path(); |
| 960 | + $cache_base_dir = $cache_path['cachebasedir']; | |
| 879 | 961 | $cache_dir = $cache_path['cachedir']; |
| 880 | 962 | $cache_dir_url = $cache_path['cachedirurl']; |
| 881 | 963 | |
| 882 | 964 | |
| @@ -893,8 +975,10 @@ | ||
| 893 | 975 | |
| 894 | 976 | // mark as done (as we go) |
| 895 | 977 | $done = $scripts->done; |
| 896 | 978 | |
| 979 | + $previous_load_strategy = null; | |
| 980 | + | |
| 897 | 981 | // get groups of handles |
| 898 | 982 | foreach ($scripts->to_do as $handle) : |
| 899 | 983 | |
| 900 | 984 | // get full url |
| @@ -905,9 +989,9 @@ | ||
| 905 | 989 | continue; |
| 906 | 990 | } |
| 907 | 991 | |
| 908 | 992 | // Exclude JS files from PageSpeedIndex (Async) takes priority over the ignore list |
| 909 | - if (false != $async_js || is_array($async_js)) { | |
| 993 | + if (!empty($async_js)) { | |
| 910 | 994 | |
| 911 | 995 | // check for string match |
| 912 | 996 | $skipjs = false; |
| 913 | 997 | foreach ($async_js as $l) { |
| @@ -912,15 +996,16 @@ | ||
| 912 | 996 | $skipjs = false; |
| 913 | 997 | foreach ($async_js as $l) { |
| 914 | 998 | if (stripos($href, $l) !== false) { |
| 915 | 999 | // print code if there are no linebreaks, or return |
| 916 | - 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); | |
| 917 | 1002 | $skipjs = true; |
| 918 | 1003 | $done = array_merge($done, array($handle)); |
| 919 | 1004 | break; |
| 920 | 1005 | } |
| 921 | 1006 | } |
| 922 | - if (false != $skipjs) { | |
| 1007 | + if (false !== $skipjs) { | |
| 923 | 1008 | continue; |
| 924 | 1009 | } |
| 925 | 1010 | } |
| 926 | 1011 | |
| @@ -929,15 +1014,24 @@ | ||
| 929 | 1014 | if ($ieonly) { |
| 930 | 1015 | continue; |
| 931 | 1016 | } |
| 932 | 1017 | |
| 1018 | + // check if the current URL points to a minified file. | |
| 1019 | + $exclude_minified_js = WP_Optimize_Minify_Functions::is_minified_css_js_filename($href); | |
| 1020 | + $normal_processing = $exclude_minified_js && !$merge_js; | |
| 1021 | + | |
| 933 | 1022 | // skip ignore list, scripts with conditionals, external scripts |
| 934 | - if (($process_js && !WP_Optimize_Minify_Functions::in_arrayi($href, $ignore_list) && !isset($wp_scripts->registered[$handle]->extra["conditional"]) && WP_Optimize_Minify_Functions::internal_url($href, site_url())) | |
| 1023 | + if (($process_js && !$normal_processing && !WP_Optimize_Minify_Functions::in_arrayi($href, $ignore_list) && !isset($wp_scripts->registered[$handle]->extra["conditional"]) && WP_Optimize_Minify_Functions::internal_url($href, site_url())) | |
| 935 | 1024 | || empty($href) |
| 936 | 1025 | ) { |
| 937 | 1026 | |
| 938 | 1027 | // process |
| 939 | - if (isset($footer[count($footer)-1]['handle']) || !count($footer) || !$merge_js) { | |
| 1028 | + if (isset($footer[count($footer)-1]['handle']) | |
| 1029 | + || !count($footer) | |
| 1030 | + || !$merge_js | |
| 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) | |
| 1033 | + ) { | |
| 940 | 1034 | array_push($footer, array('handles' => array(), 'versions' => array())); |
| 941 | 1035 | } |
| 942 | 1036 | |
| 943 | 1037 | if (isset($wp_scripts->registered[$handle]->extra['before'])) { |
| @@ -948,9 +1042,17 @@ | ||
| 948 | 1042 | |
| 949 | 1043 | // push it to the array |
| 950 | 1044 | array_push($footer[count($footer)-1]['handles'], $handle); |
| 951 | 1045 | array_push($footer[count($footer)-1]['versions'], $wp_scripts->registered[$handle]->ver); |
| 1046 | + | |
| 1047 | + // Register strategy if it is used | |
| 1048 | + if (isset($scripts->registered[$handle]->extra['strategy'])) $footer[count($footer)-1]['strategy'] = $scripts->registered[$handle]->extra['strategy']; | |
| 1049 | + | |
| 1050 | + $footer[count($footer) - 1]['last_modified'][] = WP_Optimize_Minify_Functions::get_modification_time($wp_scripts->registered[$handle]->src); | |
| 952 | 1051 | // external and ignored scripts |
| 1052 | + | |
| 1053 | + $previous_load_strategy = $scripts->registered[$handle]->extra['strategy'] ?? null; | |
| 1054 | + | |
| 953 | 1055 | } else { |
| 954 | 1056 | array_push($footer, array('handle' => $handle)); |
| 955 | 1057 | } |
| 956 | 1058 | endforeach; |
| @@ -956,15 +1058,19 @@ | ||
| 956 | 1058 | endforeach; |
| 957 | 1059 | |
| 958 | 1060 | // loop through footer scripts and merge |
| 959 | 1061 | for ($i=0,$l=count($footer); $i<$l; $i++) { |
| 1062 | + | |
| 960 | 1063 | if (!isset($footer[$i]['handle'])) { |
| 961 | 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 | + | |
| 962 | 1068 | if ($merge_js) { |
| 963 | - // Change the hash based on version numbers | |
| 964 | - $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); | |
| 965 | 1071 | } else { |
| 966 | - $hash = implode('', $footer[$i]['handles']) . implode('', $footer[$i]['versions']); | |
| 1072 | + $hash = sanitize_file_name($handles_str . $last_modified_str); | |
| 967 | 1073 | } |
| 968 | 1074 | |
| 969 | 1075 | // static cache file info |
| 970 | 1076 | $file_name = 'wpo-minify-footer-'.$hash.($minify_js ? '.min' : ''); |
| @@ -970,8 +1076,9 @@ | ||
| 970 | 1076 | $file_name = 'wpo-minify-footer-'.$hash.($minify_js ? '.min' : ''); |
| 971 | 1077 | |
| 972 | 1078 | // create cache files and urls |
| 973 | 1079 | $file = $cache_dir.'/'.$file_name.'.js'; |
| 1080 | + $meta_json_file = $cache_base_dir.'/'.'meta.json'; | |
| 974 | 1081 | $file_url = WP_Optimize_Minify_Functions::get_protocol($cache_dir_url.'/'.$file_name.'.js'); |
| 975 | 1082 | |
| 976 | 1083 | // generate a new cache file |
| 977 | 1084 | clearstatcache(); |
| @@ -977,12 +1084,14 @@ | ||
| 977 | 1084 | clearstatcache(); |
| 978 | 1085 | if (!file_exists($file)) { |
| 979 | 1086 | |
| 980 | 1087 | // code and log initialization |
| 1088 | + $log_header = $this->get_log_header(); | |
| 981 | 1089 | $log = array( |
| 982 | - 'header' => "PROCESSED on ".date('r')." from ".home_url(add_query_arg(null, null)), | |
| 1090 | + 'header' => $log_header, | |
| 983 | 1091 | 'files' => array() |
| 984 | 1092 | ); |
| 1093 | + $meta_log = $this->meta_log_initialization($log_header); | |
| 985 | 1094 | $before_code = ''; |
| 986 | 1095 | $code = ''; |
| 987 | 1096 | $after_code = ''; |
| 988 | 1097 | |
| @@ -988,11 +1097,16 @@ | ||
| 988 | 1097 | |
| 989 | 1098 | // minify and write to file |
| 990 | 1099 | foreach ($footer[$i]['handles'] as $handle) : |
| 991 | 1100 | if (!empty($wp_scripts->registered[$handle]->src)) { |
| 1101 | + // only add minified scripts once to the bundle | |
| 1102 | + if (in_array($handle, $done)) continue; | |
| 1103 | + | |
| 992 | 1104 | // get href per handle |
| 993 | - $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); | |
| 994 | 1107 | $version = $wp_scripts->registered[$handle]->ver; |
| 1108 | + $last_modified = WP_Optimize_Minify_Functions::get_modification_time($handle_src); | |
| 995 | 1109 | // inlined scripts without file |
| 996 | 1110 | if (empty($href)) { |
| 997 | 1111 | continue; |
| 998 | 1112 | } |
| @@ -1000,11 +1114,12 @@ | ||
| 1000 | 1114 | $tkey = 'js-'.hash('adler32', $handle . $href).'.js'; |
| 1001 | 1115 | $json = false; |
| 1002 | 1116 | $json = WP_Optimize_Minify_Cache_Functions::get_transient($tkey); |
| 1003 | 1117 | if (false === $json) { |
| 1004 | - $json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $minify_js, 'js', $handle, $version); | |
| 1118 | + $enable_minification = $minify_js && !WP_Optimize_Minify_Functions::is_minified_css_js_filename($href); | |
| 1119 | + $json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $enable_minification, 'js', $handle, $version); | |
| 1005 | 1120 | if ($this->options['debug']) { |
| 1006 | - 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"; | |
| 1007 | 1122 | } |
| 1008 | 1123 | WP_Optimize_Minify_Cache_Functions::set_transient($tkey, $json); |
| 1009 | 1124 | } |
| 1010 | 1125 | |
| @@ -1015,13 +1130,18 @@ | ||
| 1015 | 1130 | |
| 1016 | 1131 | if ($this->should_reset_minify_assets($res, $href, 'js', $handle, $version)) { |
| 1017 | 1132 | WP_Optimize_Minify_Cache_Functions::reset(); |
| 1018 | 1133 | $this->minify_cache_incremented = true; |
| 1134 | + $meta_log['invalidation_reason'] = 'Checksum Mismatch and version changed from ' . $res['request']['version'] . ' to ' . $version; | |
| 1135 | + $meta_log['files_that_changed'][$handle] = $res['log']; | |
| 1019 | 1136 | } |
| 1020 | 1137 | |
| 1021 | 1138 | // response has failed |
| 1022 | - if (true != $res['status']) { | |
| 1139 | + if (true !== $res['status']) { | |
| 1023 | 1140 | $log['files'][$handle] = $res['log']; |
| 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; | |
| 1024 | 1144 | continue; |
| 1025 | 1145 | } |
| 1026 | 1146 | |
| 1027 | 1147 | $done[] = $handle; |
| @@ -1049,12 +1169,15 @@ | ||
| 1049 | 1169 | } |
| 1050 | 1170 | |
| 1051 | 1171 | // append code to merged file |
| 1052 | 1172 | $code .= $before_code . "\n"; |
| 1053 | - $code .= isset($res['code']) ? $res['code'] : ''; | |
| 1173 | + $code .= $res['code'] ?? ''; | |
| 1054 | 1174 | $code .= "\n" . $after_code . "\n"; |
| 1055 | 1175 | $code = WP_Optimize_Minify_Functions::prepare_merged_js($code, $href); |
| 1056 | 1176 | $log['files'][$handle] = $res['log']; |
| 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; | |
| 1057 | 1180 | |
| 1058 | 1181 | // consider dependencies on handles with an empty src |
| 1059 | 1182 | } else { |
| 1060 | 1183 | wp_dequeue_script($handle); |
| @@ -1063,8 +1186,12 @@ | ||
| 1063 | 1186 | endforeach; |
| 1064 | 1187 | |
| 1065 | 1188 | // generate cache, write log |
| 1066 | 1189 | if (!empty($code)) { |
| 1190 | + // Only generate meta.json file if cache is reset | |
| 1191 | + if (!empty($meta_log['invalidation_reason'])) { | |
| 1192 | + file_put_contents($meta_json_file, wp_json_encode($meta_log)); | |
| 1193 | + } | |
| 1067 | 1194 | WP_Optimize_Minify_Print::write_combined_asset($file, $code, $log); |
| 1068 | 1195 | } |
| 1069 | 1196 | } else { |
| 1070 | 1197 | $log_file = $file.'.json'; |
| @@ -1087,9 +1214,9 @@ | ||
| 1087 | 1214 | } |
| 1088 | 1215 | } |
| 1089 | 1216 | |
| 1090 | 1217 | // register minified file |
| 1091 | - 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 | |
| 1092 | 1219 | |
| 1093 | 1220 | // add all extra data from wp_localize_script |
| 1094 | 1221 | $before_code = ''; |
| 1095 | 1222 | $data = array(); |
| @@ -1125,11 +1252,16 @@ | ||
| 1125 | 1252 | wp_enqueue_script("wpo_min-footer-$i"); |
| 1126 | 1253 | if (!empty($after_code)) { |
| 1127 | 1254 | wp_add_inline_script("wpo_min-footer-$i", $after_code, 'after'); |
| 1128 | 1255 | } |
| 1256 | + | |
| 1257 | + if (isset($footer[$i]['strategy'])) { | |
| 1258 | + // Backward compatible way of doing this | |
| 1259 | + wp_script_add_data( "wpo_min-footer-$i", 'strategy', $footer[$i]['strategy']); | |
| 1260 | + } | |
| 1129 | 1261 | } else { |
| 1130 | 1262 | // file could not be generated, output something meaningful |
| 1131 | - 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))." -->"; | |
| 1132 | 1264 | echo "<!-- Please check if the path above is correct and ensure your server has write permission there! -->"; |
| 1133 | 1265 | } |
| 1134 | 1266 | |
| 1135 | 1267 | // other scripts need to be requeued for the order of files to be kept |
| @@ -1144,9 +1276,9 @@ | ||
| 1144 | 1276 | } |
| 1145 | 1277 | |
| 1146 | 1278 | /** |
| 1147 | 1279 | * Process header JavaScript |
| 1148 | - * Dependant on 'enable_js' option | |
| 1280 | + * Dependent on 'enable_js' option | |
| 1149 | 1281 | * |
| 1150 | 1282 | * @return void |
| 1151 | 1283 | */ |
| 1152 | 1284 | public function process_header_scripts() { |
| @@ -1152,8 +1284,9 @@ | ||
| 1152 | 1284 | public function process_header_scripts() { |
| 1153 | 1285 | global $wp_scripts; |
| 1154 | 1286 | if (!is_object($wp_scripts)) return; |
| 1155 | 1287 | $cache_path = WP_Optimize_Minify_Cache_Functions::cache_path(); |
| 1288 | + $cache_base_dir = $cache_path['cachebasedir']; | |
| 1156 | 1289 | $cache_dir = $cache_path['cachedir']; |
| 1157 | 1290 | $cache_dir_url = $cache_path['cachedirurl']; |
| 1158 | 1291 | $exclude_js = $this->get_excluded_js_list(); |
| 1159 | 1292 | $ignore_list = WP_Optimize_Minify_Functions::compile_ignore_list($exclude_js); |
| @@ -1168,8 +1301,10 @@ | ||
| 1168 | 1301 | // mark as done (as we go) |
| 1169 | 1302 | $done = $scripts->done; |
| 1170 | 1303 | $excluded_dependencies = array(); |
| 1171 | 1304 | |
| 1305 | + $previous_load_strategy = null; | |
| 1306 | + | |
| 1172 | 1307 | // Prepare and separate assets (get groups of handles) |
| 1173 | 1308 | foreach ($scripts->to_do as $handle) { |
| 1174 | 1309 | // get full url |
| 1175 | 1310 | $href = WP_Optimize_Minify_Functions::get_hurl($wp_scripts->registered[$handle]->src); |
| @@ -1174,9 +1309,9 @@ | ||
| 1174 | 1309 | // get full url |
| 1175 | 1310 | $href = WP_Optimize_Minify_Functions::get_hurl($wp_scripts->registered[$handle]->src); |
| 1176 | 1311 | // inlined scripts without file |
| 1177 | 1312 | if (empty($href)) { |
| 1178 | - wp_enqueue_script($handle, false); | |
| 1313 | + wp_enqueue_script($handle, false); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- Not needed | |
| 1179 | 1314 | continue; |
| 1180 | 1315 | } |
| 1181 | 1316 | |
| 1182 | 1317 | // Only go through items without a group |
| @@ -1185,9 +1320,10 @@ | ||
| 1185 | 1320 | // check for string match |
| 1186 | 1321 | $skipjs = false; |
| 1187 | 1322 | foreach ($async_js as $l) { |
| 1188 | 1323 | if (stripos($href, $l) !== false) { |
| 1189 | - 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); | |
| 1190 | 1326 | $skipjs = true; |
| 1191 | 1327 | $done = array_merge($done, array($handle)); |
| 1192 | 1328 | break; |
| 1193 | 1329 | } |
| @@ -1208,14 +1344,23 @@ | ||
| 1208 | 1344 | ) { |
| 1209 | 1345 | continue; |
| 1210 | 1346 | } |
| 1211 | 1347 | |
| 1348 | + // check if the current URL points to a minified file. | |
| 1349 | + $exclude_minified_js = WP_Optimize_Minify_Functions::is_minified_css_js_filename($href); | |
| 1350 | + $normal_processing = $exclude_minified_js && !$merge_js; | |
| 1351 | + | |
| 1212 | 1352 | // Group handles - skip ignore list, scripts with conditionals, external scripts |
| 1213 | - if (($process_js && !WP_Optimize_Minify_Functions::in_arrayi($href, $ignore_list) && !isset($wp_scripts->registered[$handle]->extra["conditional"]) && WP_Optimize_Minify_Functions::internal_url($href, site_url())) | |
| 1353 | + if (($process_js && !$normal_processing && !WP_Optimize_Minify_Functions::in_arrayi($href, $ignore_list) && !isset($wp_scripts->registered[$handle]->extra["conditional"]) && WP_Optimize_Minify_Functions::internal_url($href, site_url())) | |
| 1214 | 1354 | || empty($href) |
| 1215 | 1355 | ) { |
| 1216 | 1356 | // process |
| 1217 | - if (isset($header[count($header)-1]['handle']) || !count($header) || !$merge_js) { | |
| 1357 | + if (isset($header[count($header)-1]['handle']) | |
| 1358 | + || !count($header) | |
| 1359 | + || !$merge_js | |
| 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) | |
| 1362 | + ) { | |
| 1218 | 1363 | array_push($header, array('handles' => array(), 'versions' => array())); |
| 1219 | 1364 | } |
| 1220 | 1365 | |
| 1221 | 1366 | // Force loading of dependencies |
| @@ -1248,9 +1393,16 @@ | ||
| 1248 | 1393 | |
| 1249 | 1394 | // push it to the array |
| 1250 | 1395 | array_push($header[count($header)-1]['handles'], $handle); |
| 1251 | 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); | |
| 1252 | 1398 | |
| 1399 | + // Register strategy if it is used | |
| 1400 | + if (isset($scripts->registered[$handle]->extra['strategy'])) $header[count($header)-1]['strategy'] = $scripts->registered[$handle]->extra['strategy']; | |
| 1401 | + | |
| 1402 | + $previous_load_strategy = $scripts->registered[$handle]->extra['strategy'] ?? null; | |
| 1403 | + | |
| 1404 | + | |
| 1253 | 1405 | // external and ignored scripts |
| 1254 | 1406 | } else { |
| 1255 | 1407 | // add the ignored assets |
| 1256 | 1408 | array_push($header, array('handle' => $handle)); |
| @@ -1257,20 +1409,24 @@ | ||
| 1257 | 1409 | } |
| 1258 | 1410 | |
| 1259 | 1411 | // make sure that the scripts skipped here, show up in the footer |
| 1260 | 1412 | } else { |
| 1261 | - 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 | |
| 1262 | 1414 | } |
| 1263 | 1415 | } |
| 1264 | 1416 | |
| 1265 | 1417 | // loop through header scripts and merge |
| 1266 | 1418 | for ($i=0,$l=count($header); $i < $l; $i++) { |
| 1419 | + | |
| 1267 | 1420 | if (!isset($header[$i]['handle'])) { |
| 1268 | - | |
| 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 | + | |
| 1269 | 1425 | if ($merge_js) { |
| 1270 | - $hash = hash('adler32', implode('', $header[$i]['handles']) . implode('', $header[$i]['versions'])); | |
| 1426 | + $hash = hash('adler32', $handles_str . $last_modified_str); | |
| 1271 | 1427 | } else { |
| 1272 | - $hash = implode('', $header[$i]['handles']) . implode('', $header[$i]['versions']); | |
| 1428 | + $hash = sanitize_file_name($handles_str . $last_modified_str); | |
| 1273 | 1429 | } |
| 1274 | 1430 | |
| 1275 | 1431 | // static cache file info |
| 1276 | 1432 | $file_name = 'wpo-minify-header-'.$hash.($minify_js ? '.min' : ''); |
| @@ -1275,8 +1431,9 @@ | ||
| 1275 | 1431 | // static cache file info |
| 1276 | 1432 | $file_name = 'wpo-minify-header-'.$hash.($minify_js ? '.min' : ''); |
| 1277 | 1433 | // create cache files and urls |
| 1278 | 1434 | $file = $cache_dir.'/'.$file_name.'.js'; |
| 1435 | + $meta_json_file = $cache_base_dir.'/'.'meta.json'; | |
| 1279 | 1436 | $file_url = WP_Optimize_Minify_Functions::get_protocol($cache_dir_url.'/'.$file_name.'.js'); |
| 1280 | 1437 | |
| 1281 | 1438 | // generate a new cache file |
| 1282 | 1439 | clearstatcache(); |
| @@ -1282,12 +1439,14 @@ | ||
| 1282 | 1439 | clearstatcache(); |
| 1283 | 1440 | if (!file_exists($file)) { |
| 1284 | 1441 | |
| 1285 | 1442 | // code and log initialization |
| 1443 | + $log_header = $this->get_log_header(); | |
| 1286 | 1444 | $log = array( |
| 1287 | - 'header' => "PROCESSED on ".date('r')." from ".home_url(add_query_arg(null, null)), | |
| 1445 | + 'header' => $log_header, | |
| 1288 | 1446 | 'files' => array() |
| 1289 | 1447 | ); |
| 1448 | + $meta_log = $this->meta_log_initialization($log_header); | |
| 1290 | 1449 | $before_code = ''; |
| 1291 | 1450 | $code = ''; |
| 1292 | 1451 | $after_code = ''; |
| 1293 | 1452 | |
| @@ -1293,12 +1452,16 @@ | ||
| 1293 | 1452 | |
| 1294 | 1453 | // minify and write to file |
| 1295 | 1454 | foreach ($header[$i]['handles'] as $handle) { |
| 1296 | 1455 | if (!empty($wp_scripts->registered[$handle]->src)) { |
| 1456 | + // only add minified scripts once to the bundle | |
| 1457 | + if (in_array($handle, $done)) continue; | |
| 1297 | 1458 | |
| 1298 | 1459 | // get href per handle |
| 1299 | - $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); | |
| 1300 | 1462 | $version = $wp_scripts->registered[$handle]->ver; |
| 1463 | + $last_modified = WP_Optimize_Minify_Functions::get_modification_time($handle_src); | |
| 1301 | 1464 | if (empty($href)) continue; |
| 1302 | 1465 | // download, minify, cache |
| 1303 | 1466 | $tkey = 'js-'.hash('adler32', $handle . $href).'.js'; |
| 1304 | 1467 | $json = false; |
| @@ -1303,11 +1466,12 @@ | ||
| 1303 | 1466 | $tkey = 'js-'.hash('adler32', $handle . $href).'.js'; |
| 1304 | 1467 | $json = false; |
| 1305 | 1468 | $json = WP_Optimize_Minify_Cache_Functions::get_transient($tkey); |
| 1306 | 1469 | if (false === $json) { |
| 1307 | - $json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $minify_js, 'js', $handle, $version); | |
| 1470 | + $enable_minification = $minify_js && !WP_Optimize_Minify_Functions::is_minified_css_js_filename($href); | |
| 1471 | + $json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $enable_minification, 'js', $handle, $version); | |
| 1308 | 1472 | if ($this->options['debug']) { |
| 1309 | - 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"; | |
| 1310 | 1474 | } |
| 1311 | 1475 | WP_Optimize_Minify_Cache_Functions::set_transient($tkey, $json); |
| 1312 | 1476 | } |
| 1313 | 1477 | |
| @@ -1318,13 +1482,18 @@ | ||
| 1318 | 1482 | |
| 1319 | 1483 | if ($this->should_reset_minify_assets($res, $href, 'js', $handle, $version)) { |
| 1320 | 1484 | WP_Optimize_Minify_Cache_Functions::reset(); |
| 1321 | 1485 | $this->minify_cache_incremented = true; |
| 1486 | + $meta_log['invalidation_reason'] = 'Checksum Mismatch and version changed from ' . $res['request']['version'] . ' to ' . $version; | |
| 1487 | + $meta_log['files_that_changed'][$handle] = $res['log']; | |
| 1322 | 1488 | } |
| 1323 | 1489 | |
| 1324 | 1490 | // response has failed |
| 1325 | - if (true != $res['status']) { | |
| 1491 | + if (true !== $res['status']) { | |
| 1326 | 1492 | $log['files'][$handle] = $res['log']; |
| 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; | |
| 1327 | 1496 | continue; |
| 1328 | 1497 | } |
| 1329 | 1498 | |
| 1330 | 1499 | // Add extra data from wp_add_inline_script before |
| @@ -1358,8 +1527,11 @@ | ||
| 1358 | 1527 | $code .= isset($res['code']) ? $res['code'] : ''; |
| 1359 | 1528 | $code .= "\n" . $after_code . "\n"; |
| 1360 | 1529 | $code = WP_Optimize_Minify_Functions::prepare_merged_js($code, $href); |
| 1361 | 1530 | $log['files'][$handle] = $res['log']; |
| 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; | |
| 1362 | 1534 | |
| 1363 | 1535 | // consider dependencies on handles with an empty src |
| 1364 | 1536 | } else { |
| 1365 | 1537 | wp_dequeue_script($handle); |
| @@ -1368,8 +1540,12 @@ | ||
| 1368 | 1540 | } |
| 1369 | 1541 | |
| 1370 | 1542 | // generate cache, write log |
| 1371 | 1543 | if (!empty($code)) { |
| 1544 | + // Only generate meta.json file if cache is reset | |
| 1545 | + if (!empty($meta_log['invalidation_reason'])) { | |
| 1546 | + file_put_contents($meta_json_file, wp_json_encode($meta_log)); | |
| 1547 | + } | |
| 1372 | 1548 | WP_Optimize_Minify_Print::write_combined_asset($file, $code, $log); |
| 1373 | 1549 | } |
| 1374 | 1550 | } else { |
| 1375 | 1551 | $log_file = $file.'.json'; |
| @@ -1394,9 +1570,9 @@ | ||
| 1394 | 1570 | } |
| 1395 | 1571 | |
| 1396 | 1572 | // register minified file |
| 1397 | 1573 | $dependencies = isset($excluded_dependencies[$i]) ? $excluded_dependencies[$i] : array(); |
| 1398 | - 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 | |
| 1399 | 1575 | |
| 1400 | 1576 | // add all extra data from wp_localize_script |
| 1401 | 1577 | $before_code = ''; |
| 1402 | 1578 | $data = array(); |
| @@ -1433,11 +1609,16 @@ | ||
| 1433 | 1609 | wp_enqueue_script("wpo_min-header-$i"); |
| 1434 | 1610 | if (!empty($after_code)) { |
| 1435 | 1611 | wp_add_inline_script("wpo_min-header-$i", $after_code, 'after'); |
| 1436 | 1612 | } |
| 1613 | + | |
| 1614 | + if (isset($header[$i]['strategy'])) { | |
| 1615 | + // Backward compatible way of doing this | |
| 1616 | + wp_script_add_data( "wpo_min-header-$i", 'strategy', $header[$i]['strategy']); | |
| 1617 | + } | |
| 1437 | 1618 | } else { |
| 1438 | 1619 | // file could not be generated, output something meaningful |
| 1439 | - 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))." -->"; | |
| 1440 | 1621 | echo "<!-- Please check if the path above is correct and ensure your server has write permission there! -->"; |
| 1441 | 1622 | echo "<!-- If you found a bug, please report this on https://wordpress.org/support/plugin/wp-optimize/ -->"; |
| 1442 | 1623 | } |
| 1443 | 1624 | |
| @@ -1460,8 +1641,9 @@ | ||
| 1460 | 1641 | public function process_footer_css() { |
| 1461 | 1642 | global $wp_styles; |
| 1462 | 1643 | if (!is_object($wp_styles)) return; |
| 1463 | 1644 | $cache_path = WP_Optimize_Minify_Cache_Functions::cache_path(); |
| 1645 | + $cache_base_dir = $cache_path['cachebasedir']; | |
| 1464 | 1646 | $cache_dir = $cache_path['cachedir']; |
| 1465 | 1647 | $cache_dir_url = $cache_path['cachedirurl']; |
| 1466 | 1648 | $exclude_css = $this->get_excluded_css_list(); |
| 1467 | 1649 | $ignore_list = WP_Optimize_Minify_Functions::compile_ignore_list($exclude_css); |
| @@ -1468,8 +1650,9 @@ | ||
| 1468 | 1650 | $async_css = $this->get_async_css_list(); |
| 1469 | 1651 | $minify_css = $this->options['enable_css_minification']; |
| 1470 | 1652 | $merge_css = $this->options['enable_merging_of_css']; |
| 1471 | 1653 | $process_css = $minify_css || $merge_css; |
| 1654 | + $disable_google_fonts_processing = $this->options['disable_google_fonts_processing']; | |
| 1472 | 1655 | $log = ""; |
| 1473 | 1656 | $code = ""; |
| 1474 | 1657 | $styles = clone $wp_styles; |
| 1475 | 1658 | $styles->all_deps($styles->queue); |
| @@ -1488,15 +1671,15 @@ | ||
| 1488 | 1671 | $wp_styles->done = $done; |
| 1489 | 1672 | return; |
| 1490 | 1673 | } |
| 1491 | 1674 | |
| 1492 | - // dequeue and get a list of google fonts, or requeue external | |
| 1675 | + // dequeue and get a list of Google fonts, or requeue external | |
| 1493 | 1676 | foreach ($styles->to_do as $handle) { |
| 1494 | 1677 | $href = WP_Optimize_Minify_Functions::get_hurl($wp_styles->registered[$handle]->src); |
| 1495 | 1678 | // inlined scripts without file |
| 1496 | 1679 | if (empty($href)) continue; |
| 1497 | 1680 | |
| 1498 | - if (WP_Optimize_Minify_Functions::is_google_font($href)) { | |
| 1681 | + if (!$disable_google_fonts_processing && WP_Optimize_Minify_Functions::is_google_font($href)) { | |
| 1499 | 1682 | wp_dequeue_style($handle); |
| 1500 | 1683 | if ($this->options['remove_googlefonts']) { |
| 1501 | 1684 | $done = array_merge($done, array($handle)); |
| 1502 | 1685 | continue; |
| @@ -1557,15 +1740,14 @@ | ||
| 1557 | 1740 | WP_Optimize_Minify_Print::inline_style($handle, $href); |
| 1558 | 1741 | $done = array_merge($done, array($handle)); |
| 1559 | 1742 | } else { |
| 1560 | 1743 | // fallback, enqueue google fonts |
| 1561 | - 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 | |
| 1562 | 1745 | } |
| 1563 | 1746 | } |
| 1564 | 1747 | } |
| 1565 | 1748 | } |
| 1566 | 1749 | |
| 1567 | - | |
| 1568 | 1750 | // get groups of handles |
| 1569 | 1751 | $uniq = array(); |
| 1570 | 1752 | foreach ($styles->to_do as $handle) { |
| 1571 | 1753 | |
| @@ -1578,11 +1760,11 @@ | ||
| 1578 | 1760 | $conditional = null; if (isset($wp_styles->registered[$handle]->extra["conditional"])) { |
| 1579 | 1761 | $conditional = $wp_styles->registered[$handle]->extra["conditional"]; // such as ie7, ie8, ie9, etc |
| 1580 | 1762 | } |
| 1581 | 1763 | |
| 1582 | - // mediatype | |
| 1764 | + // media type | |
| 1583 | 1765 | $mt = isset($wp_styles->registered[$handle]->args) ? $wp_styles->registered[$handle]->args : 'all'; |
| 1584 | - if ('screen' == $mt || 'screen, print' == $mt || empty($mt) || is_null($mt) || false == $mt) { | |
| 1766 | + if ('screen' === $mt || 'screen, print' === $mt || empty($mt)) { | |
| 1585 | 1767 | $mt = 'all'; |
| 1586 | 1768 | } |
| 1587 | 1769 | $mediatype = $mt; |
| 1588 | 1770 | |
| @@ -1612,11 +1794,14 @@ | ||
| 1612 | 1794 | if ($ieonly) { |
| 1613 | 1795 | continue; |
| 1614 | 1796 | } |
| 1615 | 1797 | |
| 1798 | + $version = '?ver=' . (!empty($version) ? $version : $wp_styles->default_version); | |
| 1799 | + $href_versioned = $href . $version; | |
| 1800 | + | |
| 1616 | 1801 | // Exclude specific CSS files from PageSpeedIndex? |
| 1617 | - if (false != $async_css && is_array($async_css) && WP_Optimize_Minify_Functions::in_arrayi($href, $async_css)) { | |
| 1618 | - 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); | |
| 1619 | 1804 | $done = array_merge($done, array($handle)); |
| 1620 | 1805 | continue; |
| 1621 | 1806 | } |
| 1622 | 1807 | |
| @@ -1621,17 +1806,17 @@ | ||
| 1621 | 1806 | } |
| 1622 | 1807 | |
| 1623 | 1808 | if (WP_Optimize_Minify_Functions::is_font_awesome($href)) { |
| 1624 | 1809 | if ('inline' === $this->options['fawesome_method']) { |
| 1625 | - WP_Optimize_Minify_Print::inline_style($handle, $href); | |
| 1810 | + WP_Optimize_Minify_Print::inline_style($handle, $href_versioned); | |
| 1626 | 1811 | $done = array_merge($done, array($handle)); |
| 1627 | 1812 | continue; |
| 1628 | 1813 | } elseif ('async' === $this->options['fawesome_method']) { |
| 1629 | - WP_Optimize_Minify_Print::async_style($href, $mediatype); | |
| 1814 | + WP_Optimize_Minify_Print::async_style($href_versioned, $mediatype); | |
| 1630 | 1815 | $done = array_merge($done, array($handle)); |
| 1631 | 1816 | continue; |
| 1632 | 1817 | } elseif ('exclude' === $this->options['fawesome_method']) { |
| 1633 | - WP_Optimize_Minify_Print::exclude_style($href); | |
| 1818 | + WP_Optimize_Minify_Print::exclude_style($href_versioned); | |
| 1634 | 1819 | $done = array_merge($done, array($handle)); |
| 1635 | 1820 | continue; |
| 1636 | 1821 | } |
| 1637 | 1822 | } |
| @@ -1641,14 +1826,17 @@ | ||
| 1641 | 1826 | $done = array_merge($done, array($handle)); |
| 1642 | 1827 | continue; |
| 1643 | 1828 | } |
| 1644 | 1829 | |
| 1645 | - // skip ignore list, conditional css, external css, font-awesome merge | |
| 1646 | - if (($process_css && !WP_Optimize_Minify_Functions::in_arrayi($href, $ignore_list) && !isset($conditional) && WP_Optimize_Minify_Functions::internal_url($href, site_url())) | |
| 1830 | + // check if the current URL points to a minified file. | |
| 1831 | + $exclude_minified_css = WP_Optimize_Minify_Functions::is_minified_css_js_filename($href); | |
| 1832 | + $normal_processing = $exclude_minified_css && !$merge_css; | |
| 1833 | + // skip ignore list, conditional css, external css, font-awesome merge and already minified css | |
| 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())) | |
| 1647 | 1835 | || empty($href) |
| 1648 | 1836 | ) { |
| 1649 | 1837 | |
| 1650 | - // colect inline css for this handle | |
| 1838 | + // collect inline css for this handle | |
| 1651 | 1839 | if (isset($wp_styles->registered[$handle]->extra['after']) && is_array($wp_styles->registered[$handle]->extra['after'])) { |
| 1652 | 1840 | $inline_css[$handle] = WP_Optimize_Minify_Functions::minify_css_string(implode('', $wp_styles->registered[$handle]->extra['after'])); // save |
| 1653 | 1841 | $wp_styles->registered[$handle]->extra['after'] = null; // dequeue |
| 1654 | 1842 | } |
| @@ -1653,9 +1841,9 @@ | ||
| 1653 | 1841 | $wp_styles->registered[$handle]->extra['after'] = null; // dequeue |
| 1654 | 1842 | } |
| 1655 | 1843 | |
| 1656 | 1844 | // process |
| 1657 | - 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) { | |
| 1658 | 1846 | array_push($footer, array('handles' => array(), 'media' => $mediatype, 'versions' => array())); |
| 1659 | 1847 | } |
| 1660 | 1848 | |
| 1661 | 1849 | // push it to the array get latest modified time |
| @@ -1660,8 +1848,9 @@ | ||
| 1660 | 1848 | |
| 1661 | 1849 | // push it to the array get latest modified time |
| 1662 | 1850 | array_push($footer[count($footer)-1]['handles'], $handle); |
| 1663 | 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); | |
| 1664 | 1853 | |
| 1665 | 1854 | // external and ignored css |
| 1666 | 1855 | } else { |
| 1667 | 1856 | |
| @@ -1680,21 +1869,28 @@ | ||
| 1680 | 1869 | $footer = apply_filters('wpo_minify_stylesheets', $footer, 'footer'); |
| 1681 | 1870 | |
| 1682 | 1871 | // loop through footer css and merge |
| 1683 | 1872 | for ($i=0,$l=count($footer); $i<$l; $i++) { |
| 1873 | + | |
| 1684 | 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 | + | |
| 1685 | 1879 | if ($merge_css) { |
| 1686 | 1880 | // get hash for the inline css in this group |
| 1687 | 1881 | $inline_css_group = array(); |
| 1688 | - foreach ($footer[$i]['handles'] as $h) { | |
| 1689 | - if (isset($inline_css[$h]) && !empty($inline_css[$h])) { | |
| 1690 | - $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 | + } | |
| 1691 | 1887 | } |
| 1692 | 1888 | } |
| 1693 | 1889 | $inline_css_hash = md5(implode('', $inline_css_group)); |
| 1694 | - $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); | |
| 1695 | 1891 | } else { |
| 1696 | - $hash = implode('', $footer[$i]['handles']) . implode('', $footer[$i]['versions']); | |
| 1892 | + $hash = sanitize_file_name($handles_str . $last_modified_str); | |
| 1697 | 1893 | } |
| 1698 | 1894 | |
| 1699 | 1895 | // static cache file info |
| 1700 | 1896 | $file_name = 'wpo-minify-footer-'.$hash.($minify_css ? '.min' : ''); |
| @@ -1700,8 +1896,9 @@ | ||
| 1700 | 1896 | $file_name = 'wpo-minify-footer-'.$hash.($minify_css ? '.min' : ''); |
| 1701 | 1897 | |
| 1702 | 1898 | // create cache files and urls |
| 1703 | 1899 | $file = $cache_dir.'/'.$file_name.'.css'; |
| 1900 | + $meta_json_file = $cache_base_dir.'/'.'meta.json'; | |
| 1704 | 1901 | $file_url = WP_Optimize_Minify_Functions::get_protocol($cache_dir_url.'/'.$file_name.'.css'); |
| 1705 | 1902 | |
| 1706 | 1903 | // generate a new cache file |
| 1707 | 1904 | clearstatcache(); |
| @@ -1707,12 +1904,14 @@ | ||
| 1707 | 1904 | clearstatcache(); |
| 1708 | 1905 | if (!file_exists($file)) { |
| 1709 | 1906 | |
| 1710 | 1907 | // code and log initialization |
| 1908 | + $log_header = $this->get_log_header(); | |
| 1711 | 1909 | $log = array( |
| 1712 | - 'header' => "PROCESSED on ".date('r')." from ".home_url(add_query_arg(null, null)), | |
| 1910 | + 'header' => $log_header, | |
| 1713 | 1911 | 'files' => array() |
| 1714 | 1912 | ); |
| 1913 | + $meta_log = $this->meta_log_initialization($log_header); | |
| 1715 | 1914 | $code = ''; |
| 1716 | 1915 | |
| 1717 | 1916 | // minify and write to file |
| 1718 | 1917 | foreach ($footer[$i]['handles'] as $handle) { |
| @@ -1718,11 +1917,13 @@ | ||
| 1718 | 1917 | foreach ($footer[$i]['handles'] as $handle) { |
| 1719 | 1918 | if (!empty($wp_styles->registered[$handle]->src)) { |
| 1720 | 1919 | |
| 1721 | 1920 | // get href per handle |
| 1722 | - $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); | |
| 1723 | 1923 | $version = $wp_styles->registered[$handle]->ver; |
| 1724 | - // inlined scripts without hreffile | |
| 1924 | + $last_modified = WP_Optimize_Minify_Functions::get_modification_time($handle_src); | |
| 1925 | + // inlined scripts without href file | |
| 1725 | 1926 | if (empty($href)) continue; |
| 1726 | 1927 | |
| 1727 | 1928 | // download, minify, cache |
| 1728 | 1929 | $tkey = 'css-'.hash('adler32', $handle . $href).'.css'; |
| @@ -1728,11 +1929,12 @@ | ||
| 1728 | 1929 | $tkey = 'css-'.hash('adler32', $handle . $href).'.css'; |
| 1729 | 1930 | $json = false; |
| 1730 | 1931 | $json = WP_Optimize_Minify_Cache_Functions::get_transient($tkey); |
| 1731 | 1932 | if (false === $json) { |
| 1732 | - $json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $minify_css, 'css', $handle, $version); | |
| 1933 | + $enable_minification = $minify_css && !WP_Optimize_Minify_Functions::is_minified_css_js_filename($href); | |
| 1934 | + $json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $enable_minification, 'css', $handle, $version); | |
| 1733 | 1935 | if ($this->options['debug']) { |
| 1734 | - 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"; | |
| 1735 | 1937 | } |
| 1736 | 1938 | WP_Optimize_Minify_Cache_Functions::set_transient($tkey, $json); |
| 1737 | 1939 | } |
| 1738 | 1940 | |
| @@ -1743,13 +1945,17 @@ | ||
| 1743 | 1945 | |
| 1744 | 1946 | if ($this->should_reset_minify_assets($res, $href, 'css', $handle, $version)) { |
| 1745 | 1947 | WP_Optimize_Minify_Cache_Functions::reset(); |
| 1746 | 1948 | $this->minify_cache_incremented = true; |
| 1949 | + $meta_log['invalidation_reason'] = 'Checksum Mismatch and version changed from ' . $res['request']['version'] . ' to ' . $version; | |
| 1950 | + $meta_log['files_that_changed'][$handle] = $res['log']; | |
| 1747 | 1951 | } |
| 1748 | 1952 | |
| 1749 | 1953 | // response has failed |
| 1750 | - if (true != $res['status']) { | |
| 1954 | + if (true !== $res['status']) { | |
| 1751 | 1955 | $log['files'][$handle] = $res['log']; |
| 1956 | + $log['files'][$handle]['version'] = $version; | |
| 1957 | + $log['files'][$handle]['last_modified'] = $last_modified; | |
| 1752 | 1958 | continue; |
| 1753 | 1959 | } |
| 1754 | 1960 | |
| 1755 | 1961 | $done[] = $handle; |
| @@ -1754,10 +1960,12 @@ | ||
| 1754 | 1960 | |
| 1755 | 1961 | $done[] = $handle; |
| 1756 | 1962 | |
| 1757 | 1963 | // append code to merged file |
| 1758 | - $code .= isset($res['code']) ? $res['code'] : ''; | |
| 1964 | + $code .= $res['code'] ?? ''; | |
| 1759 | 1965 | $log['files'][$handle] = $res['log']; |
| 1966 | + $log['files'][$handle]['version'] = $version; | |
| 1967 | + $log['files'][$handle]['last_modified'] = $last_modified; | |
| 1760 | 1968 | |
| 1761 | 1969 | // append inlined styles |
| 1762 | 1970 | if (isset($inline_css[$handle]) && !empty($inline_css[$handle])) { |
| 1763 | 1971 | $code.= $inline_css[$handle]; |
| @@ -1767,12 +1975,16 @@ | ||
| 1767 | 1975 | } else { |
| 1768 | 1976 | wp_dequeue_script($handle); |
| 1769 | 1977 | wp_enqueue_script($handle); |
| 1770 | 1978 | } |
| 1771 | - }; | |
| 1979 | + } | |
| 1772 | 1980 | |
| 1773 | 1981 | // generate cache, add inline css, write log |
| 1774 | 1982 | if (!empty($code)) { |
| 1983 | + // Only generate meta.json file if cache is reset | |
| 1984 | + if (!empty($meta_log['invalidation_reason'])) { | |
| 1985 | + file_put_contents($meta_json_file, wp_json_encode($meta_log)); | |
| 1986 | + } | |
| 1775 | 1987 | WP_Optimize_Minify_Print::write_combined_asset($file, $code, $log); |
| 1776 | 1988 | } |
| 1777 | 1989 | } else { |
| 1778 | 1990 | $log_file = $file.'.json'; |
| @@ -1807,13 +2019,13 @@ | ||
| 1807 | 2019 | if (filesize($file) < 20000 && $this->options['inline_css']) { |
| 1808 | 2020 | $this->inline_css(file_get_contents($file), $handle, $file_url, $footer[$i]['media']); |
| 1809 | 2021 | } else { |
| 1810 | 2022 | // enqueue it |
| 1811 | - 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 | |
| 1812 | 2024 | } |
| 1813 | 2025 | } else { |
| 1814 | 2026 | // file could not be generated, output something meaningful |
| 1815 | - 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))." -->"; | |
| 1816 | 2028 | echo "<!-- Please check if the path above is correct and ensure your server has write permission there! -->"; |
| 1817 | 2029 | } |
| 1818 | 2030 | } |
| 1819 | 2031 | |
| @@ -1828,8 +2040,129 @@ | ||
| 1828 | 2040 | $wp_styles->done = $done; |
| 1829 | 2041 | } |
| 1830 | 2042 | |
| 1831 | 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 | + /** | |
| 1832 | 2165 | * Orders the CSS per media type |
| 1833 | 2166 | * |
| 1834 | 2167 | * @param array $list - The list of assets |
| 1835 | 2168 | * @return array |
| @@ -1873,9 +2206,9 @@ | ||
| 1873 | 2206 | * @return void; |
| 1874 | 2207 | */ |
| 1875 | 2208 | public function add_google_fonts_merged() { |
| 1876 | 2209 | // must have something to do |
| 1877 | - if (count($this->collect_google_fonts) == 0) return; | |
| 2210 | + if (count($this->collect_google_fonts) === 0) return; | |
| 1878 | 2211 | |
| 1879 | 2212 | // merge google fonts |
| 1880 | 2213 | $href = WP_Optimize_Minify_Fonts::concatenate_google_fonts($this->collect_google_fonts); |
| 1881 | 2214 | if (empty($href)) { |
| @@ -1921,14 +2254,19 @@ | ||
| 1921 | 2254 | // always false for admin pages |
| 1922 | 2255 | if (is_admin()) return false; |
| 1923 | 2256 | |
| 1924 | 2257 | // get host with multisite support and query strings |
| 1925 | - $host = htmlentities($_SERVER['SERVER_NAME']); | |
| 2258 | + $host = isset($_SERVER['SERVER_NAME']) ? sanitize_text_field(wp_unslash($_SERVER['SERVER_NAME'])) : ''; | |
| 1926 | 2259 | if (empty($host)) { |
| 1927 | - $host = htmlentities($_SERVER['HTTP_HOST']); | |
| 2260 | + $host = isset($_SERVER['HTTP_HOST']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_HOST'])) : ''; | |
| 1928 | 2261 | } |
| 1929 | - $request_query = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY); | |
| 1930 | - $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 | + } | |
| 1931 | 2269 | |
| 1932 | 2270 | // initialize headers |
| 1933 | 2271 | $headers = array(); |
| 1934 | 2272 | |
| @@ -1936,9 +2274,9 @@ | ||
| 1936 | 2274 | if ($this->options['enabled_css_preload'] |
| 1937 | 2275 | && count($this->collect_preload_css) > 0 |
| 1938 | 2276 | ) { |
| 1939 | 2277 | foreach ($this->collect_preload_css as $u) { |
| 1940 | - // 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 | |
| 1941 | 2279 | if (strpos($u, '/assets/wpo-minify-footer-') !== false) { |
| 1942 | 2280 | continue; |
| 1943 | 2281 | } |
| 1944 | 2282 | |
| @@ -1952,9 +2290,9 @@ | ||
| 1952 | 2290 | && count($this->collect_preload_js) > 0 |
| 1953 | 2291 | ) { |
| 1954 | 2292 | foreach ($this->collect_preload_js as $u) { |
| 1955 | 2293 | |
| 1956 | - // 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 | |
| 1957 | 2295 | if (false !== strpos($u, '/assets/wpo-minify-footer-')) { |
| 1958 | 2296 | continue; |
| 1959 | 2297 | } |
| 1960 | 2298 | |
| @@ -1963,9 +2301,9 @@ | ||
| 1963 | 2301 | } |
| 1964 | 2302 | } |
| 1965 | 2303 | |
| 1966 | 2304 | // must have something |
| 1967 | - if (count($headers) == 0) { | |
| 2305 | + if (count($headers) === 0) { | |
| 1968 | 2306 | return false; |
| 1969 | 2307 | } else { |
| 1970 | 2308 | $headers = implode("\n", $headers); |
| 1971 | 2309 | } |
| @@ -1981,9 +2319,9 @@ | ||
| 1981 | 2319 | // reset file cache |
| 1982 | 2320 | clearstatcache(); |
| 1983 | 2321 | |
| 1984 | 2322 | // if there are no query strings |
| 1985 | - if ($b == $a) { | |
| 2323 | + if ($b === $a) { | |
| 1986 | 2324 | if (!file_exists($a)) { |
| 1987 | 2325 | WP_Optimize_Minify_Print::write_header($a, $headers); |
| 1988 | 2326 | } |
| 1989 | 2327 | return false; |
| @@ -1996,13 +2334,13 @@ | ||
| 1996 | 2334 | |
| 1997 | 2335 | /** |
| 1998 | 2336 | * Collect all wpo_min JS files and save them to a headers file |
| 1999 | 2337 | * |
| 2000 | - * @param String $html | |
| 2001 | - * @param String $handle | |
| 2002 | - * @param String $src | |
| 2338 | + * @param string $html | |
| 2339 | + * @param string $handle | |
| 2340 | + * @param string $src | |
| 2003 | 2341 | * |
| 2004 | - * @return String | |
| 2342 | + * @return string | |
| 2005 | 2343 | */ |
| 2006 | 2344 | public function collect_js_preload_headers($html, $handle, $src) { |
| 2007 | 2345 | if (false !== strpos($src, 'assets/wpo-minify')) { |
| 2008 | 2346 | if (!in_array($src, $this->collect_preload_js)) { |
| @@ -2049,14 +2387,18 @@ | ||
| 2049 | 2387 | return false; |
| 2050 | 2388 | } |
| 2051 | 2389 | |
| 2052 | 2390 | // get host with multisite support and query strings |
| 2053 | - $host = htmlentities($_SERVER['SERVER_NAME']); | |
| 2391 | + $host = isset($_SERVER['SERVER_NAME']) ? sanitize_text_field(wp_unslash($_SERVER['SERVER_NAME'])) : ''; | |
| 2054 | 2392 | if (empty($host)) { |
| 2055 | - $host = htmlentities($_SERVER['HTTP_HOST']); | |
| 2393 | + $host = isset($_SERVER['HTTP_HOST']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_HOST'])) : ''; | |
| 2056 | 2394 | } |
| 2057 | - $request_query = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY); | |
| 2058 | - $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 | + } | |
| 2059 | 2401 | |
| 2060 | 2402 | // get cache path |
| 2061 | 2403 | $cache_path = WP_Optimize_Minify_Cache_Functions::cache_path(); |
| 2062 | 2404 | $header_dir = $cache_path['headerdir']; |
| @@ -2069,12 +2411,12 @@ | ||
| 2069 | 2411 | // reset file cache |
| 2070 | 2412 | clearstatcache(); |
| 2071 | 2413 | |
| 2072 | 2414 | // return header files or fallback |
| 2073 | - if ($b == $a && file_exists($a)) { | |
| 2415 | + if ($b === $a && file_exists($a)) { | |
| 2074 | 2416 | return file_get_contents($a); |
| 2075 | 2417 | } |
| 2076 | - if ($b != $a && file_exists($b)) { | |
| 2418 | + if ($b !== $a && file_exists($b)) { | |
| 2077 | 2419 | return file_get_contents($b); |
| 2078 | 2420 | } |
| 2079 | 2421 | |
| 2080 | 2422 | return false; |
| @@ -2080,9 +2422,9 @@ | ||
| 2080 | 2422 | return false; |
| 2081 | 2423 | } |
| 2082 | 2424 | |
| 2083 | 2425 | /** |
| 2084 | - * Add pre-connect and pre-load headers | |
| 2426 | + * Add pre-connect and preload headers | |
| 2085 | 2427 | * |
| 2086 | 2428 | * @return void |
| 2087 | 2429 | */ |
| 2088 | 2430 | public function extra_preload_headers() { |
| @@ -2091,9 +2433,9 @@ | ||
| 2091 | 2433 | // fetch headers |
| 2092 | 2434 | $pre_connect = $this->get_pre_connect_list(); |
| 2093 | 2435 | |
| 2094 | 2436 | // preload |
| 2095 | - if (is_array($pre_connect) && count($pre_connect) > 0) { | |
| 2437 | + if (count($pre_connect) > 0) { | |
| 2096 | 2438 | foreach ($pre_connect as $url) { |
| 2097 | 2439 | if (!empty($url) && filter_var($url, FILTER_VALIDATE_URL)) { |
| 2098 | 2440 | header("Link: <$url>; rel=preconnect", false); |
| 2099 | 2441 | } |
| @@ -2100,9 +2442,9 @@ | ||
| 2100 | 2442 | } |
| 2101 | 2443 | } |
| 2102 | 2444 | |
| 2103 | 2445 | $headers = $this->get_preload_headers(); |
| 2104 | - if (false != $headers) { | |
| 2446 | + if ($headers) { | |
| 2105 | 2447 | $nh = array_map('trim', explode("\n", $headers)); |
| 2106 | 2448 | foreach ($nh as $h) { |
| 2107 | 2449 | if (!empty($h)) { |
| 2108 | 2450 | header($h, false); |
| @@ -2161,15 +2503,15 @@ | ||
| 2161 | 2503 | /** |
| 2162 | 2504 | * Handles header meta information removal |
| 2163 | 2505 | */ |
| 2164 | 2506 | private function remove_header_meta_info() { |
| 2165 | - // no resource hints, generator tag, shortlinks, manifest link, etc | |
| 2507 | + // no resource hints, generator tag, shortlink, manifest link, etc | |
| 2166 | 2508 | remove_action('wp_head', 'wp_resource_hints', 2); |
| 2167 | 2509 | remove_action('wp_head', 'wp_generator'); |
| 2168 | 2510 | remove_action('template_redirect', 'wp_shortlink_header', 11); |
| 2169 | 2511 | remove_action('wp_head', 'wlwmanifest_link'); |
| 2170 | 2512 | remove_action('wp_head', 'rsd_link'); |
| 2171 | - remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); | |
| 2513 | + remove_action('wp_head', 'adjacent_posts_rel_link_wp_head'); | |
| 2172 | 2514 | remove_action('wp_head', 'feed_links', 2); |
| 2173 | 2515 | remove_action('wp_head', 'feed_links_extra', 3); |
| 2174 | 2516 | WP_Optimize_Minify_Functions::remove_redundant_shortlink(); |
| 2175 | 2517 | } |
| @@ -2195,11 +2537,65 @@ | ||
| 2195 | 2537 | || ( 'all' === $this->options['enable_defer_js'] && 'async_using_js' === $this->options['defer_js_type'] ) |
| 2196 | 2538 | ) { |
| 2197 | 2539 | add_action('wp_head', array('WP_Optimize_Minify_Print', 'add_load_async'), 0); |
| 2198 | 2540 | } |
| 2541 | + | |
| 2542 | + // Process script module from generated html | |
| 2543 | + ob_start(array($this, "process_script_modules")); | |
| 2199 | 2544 | } |
| 2200 | 2545 | |
| 2201 | 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 | + /** | |
| 2202 | 2598 | * Handles css processing |
| 2203 | 2599 | */ |
| 2204 | 2600 | private function process_css() { |
| 2205 | 2601 | add_action('wp_head', array($this, 'add_critical_path'), 2); |
| @@ -2205,11 +2601,11 @@ | ||
| 2205 | 2601 | add_action('wp_head', array($this, 'add_critical_path'), 2); |
| 2206 | 2602 | |
| 2207 | 2603 | // merge, if inline is selected but prevent optimization for these locations |
| 2208 | 2604 | if ($this->options['inline_css']) { |
| 2209 | - // 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' | |
| 2210 | 2606 | add_filter('style_loader_tag', array($this, 'inline_css'), PHP_INT_MAX, 4); |
| 2211 | - // this prints the google fonts | |
| 2607 | + // this prints the Google fonts | |
| 2212 | 2608 | add_action('wp_print_styles', array($this, 'add_google_fonts_merged'), PHP_INT_MAX); |
| 2213 | 2609 | add_action('wp_print_footer_scripts', array($this, 'add_google_fonts_merged'), PHP_INT_MAX); |
| 2214 | 2610 | } |
| 2215 | 2611 | // Preloading |
| @@ -2215,14 +2611,14 @@ | ||
| 2215 | 2611 | // Preloading |
| 2216 | 2612 | if ($this->options['enabled_css_preload']) { |
| 2217 | 2613 | add_filter('style_loader_tag', array($this, 'collect_css_preload_headers'), PHP_INT_MAX, 3); |
| 2218 | 2614 | } |
| 2219 | - // Optimize the css and collect the google fonts for merging | |
| 2615 | + // Optimize the css and collect the Google fonts for merging | |
| 2220 | 2616 | add_action('wp_print_styles', array($this, 'process_header_css'), PHP_INT_MAX); |
| 2221 | 2617 | add_action('wp_print_footer_scripts', array($this, 'process_footer_css'), 9); |
| 2222 | 2618 | |
| 2223 | 2619 | /** |
| 2224 | - * 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 | |
| 2225 | 2621 | * |
| 2226 | 2622 | * @param boolean $maintain_css_order |
| 2227 | 2623 | * @return boolean |
| 2228 | 2624 | * @default true |
| @@ -2228,14 +2624,16 @@ | ||
| 2228 | 2624 | * @default true |
| 2229 | 2625 | */ |
| 2230 | 2626 | if (!apply_filters('wpo_minify_maintain_css_order', true)) { |
| 2231 | 2627 | // Reorder stylesheets |
| 2232 | - 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')); | |
| 2233 | 2629 | } |
| 2234 | 2630 | } |
| 2235 | 2631 | |
| 2236 | 2632 | /** |
| 2237 | 2633 | * Determines whether we should minify html or not |
| 2634 | + * | |
| 2635 | + * @return bool | |
| 2238 | 2636 | */ |
| 2239 | 2637 | private function should_process_html() { |
| 2240 | 2638 | return $this->options['html_minification'] && !is_admin() && $this->is_cache_preload(); |
| 2241 | 2639 | } |
| @@ -2241,8 +2639,10 @@ | ||
| 2241 | 2639 | } |
| 2242 | 2640 | |
| 2243 | 2641 | /** |
| 2244 | 2642 | * Determines whether to use loadCSS polyfill or not |
| 2643 | + * | |
| 2644 | + * @return bool | |
| 2245 | 2645 | */ |
| 2246 | 2646 | private function should_use_loadCSS() { |
| 2247 | 2647 | return $this->options['loadcss'] |
| 2248 | 2648 | || 'async' === $this->options['fawesome_method'] |
| @@ -2334,19 +2734,20 @@ | ||
| 2334 | 2734 | |
| 2335 | 2735 | /** |
| 2336 | 2736 | * Decides whether minify cache should be incremented or not |
| 2337 | 2737 | * |
| 2338 | - * @param string $res | |
| 2738 | + * @param array $res | |
| 2339 | 2739 | * @param string $href |
| 2340 | 2740 | * @param string $type |
| 2341 | 2741 | * @param string $handle |
| 2342 | - * @param string $version | |
| 2742 | + * @param mixed $version | |
| 2343 | 2743 | * |
| 2344 | 2744 | * @return bool true when cache is invalid, false otherwise |
| 2345 | 2745 | */ |
| 2346 | 2746 | private function should_reset_minify_assets($res, $href, $type, $handle, $version) { |
| 2347 | 2747 | if (isset($res['request']['version']) && $res['request']['version'] != $version && !$this->minify_cache_incremented) { |
| 2348 | - $new_json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $this->options['enable_css_minification'], $type, $handle, $version); | |
| 2748 | + $enable_minification = $this->options['enable_'.$type.'_minification'] && !WP_Optimize_Minify_Functions::is_minified_css_js_filename($href); | |
| 2749 | + $new_json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $enable_minification, $type, $handle, $version); | |
| 2349 | 2750 | $new_res = json_decode($new_json, true); |
| 2350 | 2751 | |
| 2351 | 2752 | /** |
| 2352 | 2753 | * In case `download_and_minify` fails because of |
| @@ -2359,6 +2760,18 @@ | ||
| 2359 | 2760 | $hash = hash('sha256', $res['code']); |
| 2360 | 2761 | $new_hash = hash('sha256', $new_res['code']); |
| 2361 | 2762 | return strcmp($hash, $new_hash); |
| 2362 | 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; | |
| 2363 | 2776 | } |
| 2364 | 2777 | } |