| @@ -64,8 +64,14 @@ | ||
| 64 | 64 | if ($this->options['enable_js']) { |
| 65 | 65 | $this->process_js(); |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | + if ($this->options['host_local_google_fonts']) { | |
| 69 | + // Add filter to replace Goole fonts urls to local if needed | |
| 70 | + add_filter('wpo_minify_css_string', array($this, 'replace_gfont_urls_with_local')); | |
| 71 | + add_filter('wpo_minify_get_css', array($this, 'replace_gfont_urls_with_local')); | |
| 72 | + } | |
| 73 | + | |
| 68 | 74 | if ($this->options['enable_css']) { |
| 69 | 75 | $this->process_css(); |
| 70 | 76 | } |
| 71 | 77 | |
| @@ -74,9 +80,9 @@ | ||
| 74 | 80 | add_action('wp_head', array($this, 'add_assets_preload'), 2); |
| 75 | 81 | } |
| 76 | 82 | |
| 77 | 83 | if ($this->should_process_html()) { |
| 78 | - add_action('template_redirect', array('WP_Optimize_Minify_Functions', 'html_compression_start'), 9); | |
| 84 | + add_action('template_redirect', array('WP_Optimize_Minify_Functions', 'html_compression_start')); | |
| 79 | 85 | } |
| 80 | 86 | |
| 81 | 87 | if ($this->should_use_loadCSS()) { |
| 82 | 88 | add_action('wp_footer', array('WP_Optimize_Minify_Print', 'add_load_css'), PHP_INT_MAX); |
| @@ -85,14 +91,24 @@ | ||
| 85 | 91 | $this->remove_query_string_from_static_assets(); |
| 86 | 92 | } |
| 87 | 93 | |
| 88 | 94 | /** |
| 95 | + * Replace Google fonts urls to local versions | |
| 96 | + * | |
| 97 | + * @param string $content | |
| 98 | + * @return string | |
| 99 | + */ | |
| 100 | + public function replace_gfont_urls_with_local($content): string { | |
| 101 | + return WP_Optimize_Host_Google_Fonts::instance()->replace_gfont_urls_with_local($content); | |
| 102 | + } | |
| 103 | + | |
| 104 | + /** | |
| 89 | 105 | * Detects whether cache preloading is running or not |
| 90 | 106 | * |
| 91 | 107 | * @return bool |
| 92 | 108 | */ |
| 93 | 109 | private function is_cache_preload() { |
| 94 | - return isset($_SERVER['HTTP_X_WP_OPTIMIZE_CACHE_PRELOAD']) && 0 === strcmp($_SERVER['HTTP_X_WP_OPTIMIZE_CACHE_PRELOAD'], 'Yes'); | |
| 110 | + return isset($_SERVER['HTTP_X_WP_OPTIMIZE_CACHE_PRELOAD']) && 0 === strcmp(sanitize_text_field(wp_unslash($_SERVER['HTTP_X_WP_OPTIMIZE_CACHE_PRELOAD'])), 'Yes'); | |
| 95 | 111 | } |
| 96 | 112 | |
| 97 | 113 | /** |
| 98 | 114 | * Set initial values for the meta log object |
| @@ -108,9 +124,9 @@ | ||
| 108 | 124 | ); |
| 109 | 125 | } |
| 110 | 126 | |
| 111 | 127 | /** |
| 112 | - * Wether to run the feature on a page or not | |
| 128 | + * Whether to run the feature on a page or not | |
| 113 | 129 | * |
| 114 | 130 | * @param string $context - Optional, The context where the check is done |
| 115 | 131 | * @return boolean |
| 116 | 132 | */ |
| @@ -115,14 +131,14 @@ | ||
| 115 | 131 | * @return boolean |
| 116 | 132 | */ |
| 117 | 133 | public function run_on_page($context = 'default') { |
| 118 | 134 | /** |
| 119 | - * Filters wether the functionality is ran on the current page. | |
| 135 | + * Filters whether the functionality is run on the current page. | |
| 120 | 136 | * |
| 121 | 137 | * @param boolean $run_on_page |
| 122 | 138 | * @param string $context - Optional, The feature where the check is done |
| 123 | 139 | */ |
| 124 | - return apply_filters( | |
| 140 | + $filtered_value = apply_filters( | |
| 125 | 141 | 'wpo_minify_run_on_page', |
| 126 | 142 | !is_admin() |
| 127 | 143 | && (!defined('SCRIPT_DEBUG') || !SCRIPT_DEBUG) |
| 128 | 144 | && !is_preview() |
| @@ -131,19 +147,20 @@ | ||
| 131 | 147 | && !(function_exists('is_amp_endpoint') && is_amp_endpoint()) |
| 132 | 148 | && !WP_Optimize_Minify_Functions::exclude_contents(), |
| 133 | 149 | $context |
| 134 | 150 | ); |
| 151 | + return is_bool($filtered_value) ? $filtered_value : false; | |
| 135 | 152 | } |
| 136 | 153 | |
| 137 | 154 | /** |
| 138 | 155 | * Inline css in place, instead of inlining the large file |
| 139 | 156 | * |
| 140 | - * @param String $html | |
| 141 | - * @param String $handle | |
| 142 | - * @param String $href | |
| 143 | - * @param String $media | |
| 157 | + * @param string $html | |
| 158 | + * @param string $handle | |
| 159 | + * @param string $href | |
| 160 | + * @param mixed $media | |
| 144 | 161 | * |
| 145 | - * @return String | |
| 162 | + * @return string|boolean | |
| 146 | 163 | */ |
| 147 | 164 | public function inline_css($html, $handle, $href, $media) { |
| 148 | 165 | $exclude_css = $this->get_excluded_css_list(); |
| 149 | 166 | $ignore_list = WP_Optimize_Minify_Functions::compile_ignore_list($exclude_css); |
| @@ -154,9 +171,9 @@ | ||
| 154 | 171 | // make sure href is complete |
| 155 | 172 | $href = WP_Optimize_Minify_Functions::get_hurl($href); |
| 156 | 173 | |
| 157 | 174 | if ($this->options['debug']) { |
| 158 | - echo "<!-- wpo_min DEBUG: Inline CSS processing start $handle / $href -->\n"; | |
| 175 | + echo "<!-- wpo_min DEBUG: Inline CSS processing start ". esc_html($handle) . " / " . esc_html($href) . " -->\n"; | |
| 159 | 176 | } |
| 160 | 177 | |
| 161 | 178 | |
| 162 | 179 | // skip all this, if the async css option is enabled |
| @@ -166,17 +183,16 @@ | ||
| 166 | 183 | // leave conditionals alone |
| 167 | 184 | if (wp_styles()->get_data($handle, 'conditional')) return $html; |
| 168 | 185 | |
| 169 | 186 | // mediatype fix for some plugins + remove print mediatypes |
| 170 | - if ('screen' == $media | |
| 171 | - || 'screen, print' == $media | |
| 172 | - || empty($media) | |
| 173 | - || is_null($media) | |
| 174 | - || false == $media | |
| 187 | + if (!is_string($media) | |
| 188 | + || '' === $media | |
| 189 | + || 'screen' === $media | |
| 190 | + || 'screen, print' === $media | |
| 175 | 191 | ) { |
| 176 | 192 | $media = 'all'; |
| 177 | 193 | } |
| 178 | - if (!empty($this->options['remove_print_mediatypes']) && 'print' == $media) { | |
| 194 | + if (!empty($this->options['remove_print_mediatypes']) && 'print' === $media) { | |
| 179 | 195 | return false; |
| 180 | 196 | } |
| 181 | 197 | |
| 182 | 198 | // Exclude specific CSS files from PageSpeedInsights? |
| @@ -197,12 +213,12 @@ | ||
| 197 | 213 | if (WP_Optimize_Minify_Functions::is_minified_css_js_filename($href)) { |
| 198 | 214 | return $html; |
| 199 | 215 | } |
| 200 | 216 | |
| 201 | - // check if working with a font awesom link | |
| 217 | + // check if working with a font awesome link | |
| 202 | 218 | if (WP_Optimize_Minify_Functions::is_font_awesome($href)) { |
| 203 | 219 | // font awesome processing, async css |
| 204 | - if ('async' == $this->options['fawesome_method']) { | |
| 220 | + if ('async' === $this->options['fawesome_method']) { | |
| 205 | 221 | WP_Optimize_Minify_Print::async_style($href, $media); |
| 206 | 222 | return false; |
| 207 | 223 | } elseif ('exclude' === $this->options['fawesome_method']) { |
| 208 | 224 | // font awesome processing, async and exclude from PageSpeedIndex |
| @@ -207,17 +223,17 @@ | ||
| 207 | 223 | } elseif ('exclude' === $this->options['fawesome_method']) { |
| 208 | 224 | // font awesome processing, async and exclude from PageSpeedIndex |
| 209 | 225 | WP_Optimize_Minify_Print::exclude_style($href); |
| 210 | 226 | return false; |
| 211 | - } elseif ('inline' == $this->options['fawesome_method']) { | |
| 227 | + } elseif ('inline' === $this->options['fawesome_method']) { | |
| 212 | 228 | WP_Optimize_Minify_Print::inline_style($handle, $href); |
| 213 | 229 | return false; |
| 214 | 230 | } |
| 215 | 231 | } |
| 216 | 232 | |
| 217 | - // Check if working with google font url | |
| 233 | + // Check if working with Google font url | |
| 218 | 234 | |
| 219 | - if ('fonts.googleapis.com' == parse_url($href, PHP_URL_HOST)) { | |
| 235 | + if ('fonts.googleapis.com' === wp_parse_url($href, PHP_URL_HOST)) { | |
| 220 | 236 | // if option disabled google fonts processing |
| 221 | 237 | if ($this->options['disable_google_fonts_processing']) return $html; |
| 222 | 238 | // check if google fonts should be removed |
| 223 | 239 | if ($this->options['remove_googlefonts']) return false; |
| @@ -241,9 +257,9 @@ | ||
| 241 | 257 | $json = WP_Optimize_Minify_Cache_Functions::get_transient($tkey); |
| 242 | 258 | if (false === $json) { |
| 243 | 259 | $json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $this->options['enable_css_minification'], 'css', $handle); |
| 244 | 260 | if ($this->options['debug']) { |
| 245 | - echo "<!-- wpo_min DEBUG: Uncached file processing now for $handle / $href -->\n"; | |
| 261 | + echo "<!-- wpo_min DEBUG: Uncached file processing now for " . esc_html($handle) . " / " . esc_html($href) . " -->\n"; | |
| 246 | 262 | } |
| 247 | 263 | WP_Optimize_Minify_Cache_Functions::set_transient($tkey, $json); |
| 248 | 264 | } |
| 249 | 265 | |
| @@ -250,18 +266,20 @@ | ||
| 250 | 266 | // decode |
| 251 | 267 | $res = json_decode($json, true); |
| 252 | 268 | |
| 253 | 269 | // inline css or fail |
| 254 | - if (null !== $res && false != $res['status']) { | |
| 270 | + if (null !== $res && false !== $res['status']) { | |
| 255 | 271 | // add font-display |
| 256 | 272 | // https://developers.google.com/web/updates/2016/02/font-display |
| 257 | 273 | $res['code'] = str_ireplace('font-style:normal;', 'font-display:block;font-style:normal;', $res['code']); |
| 258 | 274 | |
| 259 | - echo '<style class="optimize_css_1" type="text/css" media="all">'.$res['code'].'</style>' . "\n"; | |
| 275 | + echo '<style class="optimize_css_1" type="text/css" media="all">'; | |
| 276 | + echo $res['code']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- This is already escaped | |
| 277 | + echo '</style>' . "\n"; | |
| 260 | 278 | return false; |
| 261 | 279 | } else { |
| 262 | 280 | if ($this->options['debug']) { |
| 263 | - echo "<!-- wpo_min DEBUG: Google fonts request failed for $href -->\n"; | |
| 281 | + echo "<!-- wpo_min DEBUG: Google fonts request failed for " . esc_html($href) . " -->\n"; | |
| 264 | 282 | } |
| 265 | 283 | return $html; |
| 266 | 284 | } |
| 267 | 285 | } elseif ('async' === $this->options['gfonts_method']) { |
| @@ -293,9 +311,9 @@ | ||
| 293 | 311 | $json = WP_Optimize_Minify_Cache_Functions::get_transient($tkey); |
| 294 | 312 | if (false === $json) { |
| 295 | 313 | $json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $this->options['enable_css_minification'], 'css', $handle); |
| 296 | 314 | if ($this->options['debug']) { |
| 297 | - echo "<!-- wpo_min DEBUG: Uncached file processing now for $handle / $href -->" . "\n"; | |
| 315 | + echo "<!-- wpo_min DEBUG: Uncached file processing now for " . esc_html($handle) . " / " . esc_html($href) . " -->" . "\n"; | |
| 298 | 316 | } |
| 299 | 317 | WP_Optimize_Minify_Cache_Functions::set_transient($tkey, $json); |
| 300 | 318 | } |
| 301 | 319 | |
| @@ -302,21 +320,25 @@ | ||
| 302 | 320 | // decode |
| 303 | 321 | $res = json_decode($json, true); |
| 304 | 322 | |
| 305 | 323 | // inline it + other inlined children styles |
| 306 | - if (null !== $res && false != $res['status']) { | |
| 307 | - echo '<style class="optimize_css_2" type="text/css" media="'.$media.'">'.$res['code'].'</style>' . "\n"; | |
| 324 | + if (null !== $res && $res['status']) { | |
| 325 | + echo '<style class="optimize_css_2" type="text/css" media="'.esc_attr($media).'">'; | |
| 326 | + echo $res['code']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- This is already escaped | |
| 327 | + echo '</style>' . "\n"; | |
| 308 | 328 | |
| 309 | 329 | // get inline_styles for this handle, minify and print |
| 310 | 330 | $inline_styles = array(); |
| 311 | 331 | $inline_styles = wp_styles()->get_data($handle, 'after'); |
| 312 | - if (false != $inline_styles) { | |
| 332 | + if ($inline_styles) { | |
| 313 | 333 | |
| 314 | 334 | // string type |
| 315 | 335 | if (is_string($inline_styles)) { |
| 316 | 336 | $code = WP_Optimize_Minify_Functions::get_css($href, $inline_styles, $this->options['enable_css_minification']); |
| 317 | - if (!empty($code) && false != $code) { | |
| 318 | - echo '<style class="optimize_css_3" type="text/css" media="'.$media.'">'.$code.'</style>' . "\n"; | |
| 337 | + if (!empty($code)) { | |
| 338 | + echo '<style class="optimize_css_3" type="text/css" media="'.esc_attr($media).'">'; | |
| 339 | + echo $code; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- This is already escaped | |
| 340 | + echo '</style>' . "\n"; | |
| 319 | 341 | } |
| 320 | 342 | } |
| 321 | 343 | |
| 322 | 344 | // array type |
| @@ -322,10 +344,12 @@ | ||
| 322 | 344 | // array type |
| 323 | 345 | if (is_array($inline_styles)) { |
| 324 | 346 | foreach ($inline_styles as $st) { |
| 325 | 347 | $code = WP_Optimize_Minify_Functions::get_css($href, $st, $this->options['enable_css_minification']); |
| 326 | - if (!empty($code) && false != $code) { | |
| 327 | - echo '<style class="optimize_css_4" type="text/css" media="'.$media.'">'.$code.'</style>' . "\n"; | |
| 348 | + if (!empty($code)) { | |
| 349 | + echo '<style class="optimize_css_4" type="text/css" media="'.esc_attr($media).'">'; | |
| 350 | + echo $code; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- This is already escaped | |
| 351 | + echo '</style>' . "\n"; | |
| 328 | 352 | } |
| 329 | 353 | } |
| 330 | 354 | } |
| 331 | 355 | } |
| @@ -333,9 +357,9 @@ | ||
| 333 | 357 | // prevent default |
| 334 | 358 | return false; |
| 335 | 359 | } else { |
| 336 | 360 | if ($this->options['debug']) { |
| 337 | - echo "<!-- wpo_min DEBUG: $handle / $href returned an empty from minification -->" . "\n"; | |
| 361 | + echo "<!-- wpo_min DEBUG: " . esc_html($handle) . " / " . esc_html($href) . " returned an empty from minification -->" . "\n"; | |
| 338 | 362 | } |
| 339 | 363 | return $html; |
| 340 | 364 | } |
| 341 | 365 | echo "<!-- ERROR: WPO-Minify couldn't catch the CSS file below. Please report this on https://wordpress.org/support/plugin/wp-optimize/ -->\n"; |
| @@ -342,15 +366,15 @@ | ||
| 342 | 366 | return $html; |
| 343 | 367 | } |
| 344 | 368 | |
| 345 | 369 | /** |
| 346 | - * Enable defer for JavaScript (WP 4.1 and above) and remove query strings for ignored files | |
| 370 | + * Enable defer for JavaScript and remove query strings for ignored and all non-minified files | |
| 347 | 371 | * |
| 348 | - * @param String $tag | |
| 349 | - * @param String $handle | |
| 350 | - * @param String $src | |
| 372 | + * @param string $tag | |
| 373 | + * @param string $handle | |
| 374 | + * @param string $src | |
| 351 | 375 | * |
| 352 | - * @return String | |
| 376 | + * @return string | |
| 353 | 377 | */ |
| 354 | 378 | public function defer_js($tag, $handle, $src) { |
| 355 | 379 | $wp_domain = trim(str_ireplace(array('http://', 'https://'), '', trim(site_url(), '/'))); |
| 356 | 380 | $exclude_js = $this->get_excluded_js_list(); |
| @@ -356,10 +380,11 @@ | ||
| 356 | 380 | $exclude_js = $this->get_excluded_js_list(); |
| 357 | 381 | $ignore_list = WP_Optimize_Minify_Functions::compile_ignore_list($exclude_js); |
| 358 | 382 | // Should this defer the Poly fills for IE? |
| 359 | 383 | $blacklist = WP_Optimize_Minify_Functions::get_ie_blacklist(); |
| 384 | + | |
| 360 | 385 | // no query strings |
| 361 | - if (false !== stripos($src, '?ver')) { | |
| 386 | + if (false !== stripos($src, '?ver') && WP_Optimize_Minify_Functions::is_already_minified($src)) { | |
| 362 | 387 | $srcf = stristr($src, '?ver', true); |
| 363 | 388 | $tag = str_ireplace($src, $srcf, $tag); |
| 364 | 389 | $src = $srcf; |
| 365 | 390 | } |
| @@ -394,9 +419,9 @@ | ||
| 394 | 419 | } |
| 395 | 420 | |
| 396 | 421 | // should we exclude defer on the login page? |
| 397 | 422 | if ($this->options['exclude_defer_login'] |
| 398 | - && false !== stripos($_SERVER["SCRIPT_NAME"], strrchr(wp_login_url(), '/')) | |
| 423 | + && isset($_SERVER['SCRIPT_NAME']) && false !== stripos(sanitize_text_field(wp_unslash($_SERVER["SCRIPT_NAME"])), strrchr(wp_login_url(), '/')) | |
| 399 | 424 | ) { |
| 400 | 425 | return $tag; |
| 401 | 426 | } |
| 402 | 427 | |
| @@ -446,11 +471,15 @@ | ||
| 446 | 471 | * @return void |
| 447 | 472 | */ |
| 448 | 473 | public function add_critical_path() { |
| 449 | 474 | if (is_front_page() && !empty($this->options['critical_path_css_is_front_page'])) { |
| 450 | - echo '<style id="critical-path-is-front-page" type="text/css" media="all">' . "\n" . $this->options['critical_path_css_is_front_page'] . "\n" . '</style>' . "\n"; | |
| 475 | + echo '<style id="critical-path-is-front-page" type="text/css" media="all">' . "\n"; | |
| 476 | + echo $this->options['critical_path_css_is_front_page'] . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- This is already escaped | |
| 477 | + echo '</style>' . "\n"; | |
| 451 | 478 | } elseif (!empty($this->options['critical_path_css'])) { |
| 452 | - echo '<style id="critical-path-global" type="text/css" media="all">' . "\n" . $this->options['critical_path_css'] . "\n" . '</style>' . "\n"; | |
| 479 | + echo '<style id="critical-path-global" type="text/css" media="all">' . "\n"; | |
| 480 | + echo $this->options['critical_path_css'] . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- This is already escaped | |
| 481 | + echo '</style>' . "\n"; | |
| 453 | 482 | } |
| 454 | 483 | } |
| 455 | 484 | |
| 456 | 485 | /** |
| @@ -520,9 +549,9 @@ | ||
| 520 | 549 | } |
| 521 | 550 | |
| 522 | 551 | // mediatype |
| 523 | 552 | $mt = isset($wp_styles->registered[$handle]->args) ? $wp_styles->registered[$handle]->args : 'all'; |
| 524 | - if ('screen' == $mt || 'screen, print' == $mt || empty($mt) || is_null($mt) || false == $mt) { | |
| 553 | + if ('screen' === $mt || 'screen, print' === $mt || empty($mt) || is_null($mt) || false === $mt) { | |
| 525 | 554 | $mt = 'all'; |
| 526 | 555 | } |
| 527 | 556 | $mediatype = $mt; |
| 528 | 557 | |
| @@ -544,11 +573,15 @@ | ||
| 544 | 573 | } else { |
| 545 | 574 | $uniq[$key] = $handle; |
| 546 | 575 | } |
| 547 | 576 | } |
| 577 | + | |
| 578 | + $version = '?ver=' . (!empty($version) ? $version : $wp_styles->default_version); | |
| 579 | + $href_versioned = $href . $version; | |
| 580 | + | |
| 548 | 581 | // Exclude specific CSS files from PageSpeedIndex? |
| 549 | 582 | if (WP_Optimize_Minify_Functions::in_arrayi($href, $async_css)) { |
| 550 | - WP_Optimize_Minify_Print::exclude_style($href); | |
| 583 | + WP_Optimize_Minify_Print::exclude_style($href_versioned); | |
| 551 | 584 | $done = array_merge($done, array($handle)); |
| 552 | 585 | continue; |
| 553 | 586 | } |
| 554 | 587 | // Fonts Awesome Processing |
| @@ -553,17 +586,17 @@ | ||
| 553 | 586 | } |
| 554 | 587 | // Fonts Awesome Processing |
| 555 | 588 | if (WP_Optimize_Minify_Functions::is_font_awesome($href)) { |
| 556 | 589 | if ('inline' === $this->options['fawesome_method']) { |
| 557 | - WP_Optimize_Minify_Print::inline_style($handle, $href); | |
| 590 | + WP_Optimize_Minify_Print::inline_style($handle, $href_versioned); | |
| 558 | 591 | $done = array_merge($done, array($handle)); |
| 559 | 592 | continue; |
| 560 | 593 | } elseif ('async' === $this->options['fawesome_method']) { |
| 561 | - WP_Optimize_Minify_Print::async_style($href, $mediatype); | |
| 594 | + WP_Optimize_Minify_Print::async_style($href_versioned, $mediatype); | |
| 562 | 595 | $done = array_merge($done, array($handle)); |
| 563 | 596 | continue; |
| 564 | 597 | } elseif ('exclude' === $this->options['fawesome_method']) { |
| 565 | - WP_Optimize_Minify_Print::exclude_style($href); | |
| 598 | + WP_Optimize_Minify_Print::exclude_style($href_versioned); | |
| 566 | 599 | $done = array_merge($done, array($handle)); |
| 567 | 600 | continue; |
| 568 | 601 | } |
| 569 | 602 | } |
| @@ -582,9 +615,9 @@ | ||
| 582 | 615 | 'mediatype' => $mediatype |
| 583 | 616 | ); |
| 584 | 617 | |
| 585 | 618 | // google fonts to the top (collect and skip process array) |
| 586 | - if (WP_Optimize_Minify_Functions::is_google_font($href)) { | |
| 619 | + if (!$disable_google_fonts_processing && WP_Optimize_Minify_Functions::is_google_font($href)) { | |
| 587 | 620 | if ($this->options['remove_googlefonts']) { |
| 588 | 621 | $done = array_merge($done, array($handle)); |
| 589 | 622 | continue; |
| 590 | 623 | } |
| @@ -603,9 +636,9 @@ | ||
| 603 | 636 | // merge google fonts if force inlining is enabled? |
| 604 | 637 | $nfonts = array(); |
| 605 | 638 | if ($this->options['merge_google_fonts']) { |
| 606 | 639 | $nfonts[] = WP_Optimize_Minify_Fonts::concatenate_google_fonts($google_fonts); |
| 607 | - // mark the google fonts as done so they don't get processed | |
| 640 | + // mark the Google fonts as done so they don't get processed | |
| 608 | 641 | if ('inherit' !== $this->options['gfonts_method']) { |
| 609 | 642 | $done = array_merge($done, array_keys($google_fonts)); |
| 610 | 643 | } |
| 611 | 644 | } else { |
| @@ -627,9 +660,9 @@ | ||
| 627 | 660 | // check if the cache is empty or has no code |
| 628 | 661 | if (null === $json || false === $json || empty($json['code'])) { |
| 629 | 662 | $res = WP_Optimize_Minify_Functions::download_and_minify($href, null, $minify_css, 'css', null); |
| 630 | 663 | if ($this->options['debug']) { |
| 631 | - echo "<!-- wpo_min DEBUG: Uncached file processing now for $href -->\n"; | |
| 664 | + echo "<!-- wpo_min DEBUG: Uncached file processing now for " . esc_html($href) . " -->\n"; | |
| 632 | 665 | } |
| 633 | 666 | WP_Optimize_Minify_Cache_Functions::set_transient($tkey, $res); |
| 634 | 667 | // decode |
| 635 | 668 | $json = json_decode($res, true); |
| @@ -639,14 +672,16 @@ | ||
| 639 | 672 | if (null !== $json && !empty($json['code'])) { |
| 640 | 673 | // add font-display |
| 641 | 674 | // https://developers.google.com/web/updates/2016/02/font-display |
| 642 | 675 | $json['code'] = str_ireplace('font-style:normal;', 'font-display:block;font-style:normal;', $json['code']); |
| 643 | - echo '<style type="text/css" media="all">'.$json['code'].'</style>' . "\n"; | |
| 676 | + echo '<style type="text/css" media="all">'; | |
| 677 | + echo $json['code']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- This is already escaped | |
| 678 | + echo '</style>' . "\n"; | |
| 644 | 679 | $done = array_merge($done, array($handle)); |
| 645 | 680 | } else { |
| 646 | - echo "<!-- GOOGLE FONTS REQUEST FAILED for $href -->" . "\n"; | |
| 681 | + echo "<!-- GOOGLE FONTS REQUEST FAILED for " . esc_html($href) . " -->" . "\n"; | |
| 647 | 682 | // inlining failed, so enqueue again |
| 648 | - wp_enqueue_style($handle, $href, array(), null); | |
| 683 | + wp_enqueue_style($handle, $href, array(), null); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- Not needed as this is a google font and from cache | |
| 649 | 684 | } |
| 650 | 685 | } elseif ('async' === $this->options['gfonts_method']) { |
| 651 | 686 | WP_Optimize_Minify_Print::async_style($href); |
| 652 | 687 | $done = array_merge($done, array($handle)); |
| @@ -695,12 +730,12 @@ | ||
| 695 | 730 | $normal_processing = $exclude_minified_css && !$merge_css; |
| 696 | 731 | // skip ignore list, conditional css, external css, font-awesome merge and already minified js |
| 697 | 732 | if (($process_css && !$normal_processing && !WP_Optimize_Minify_Functions::in_arrayi($href, $ignore_list) && !isset($conditional) && WP_Optimize_Minify_Functions::internal_url($href, site_url())) |
| 698 | 733 | || empty($href) |
| 699 | - || ($process_css && 'inherit' == $this->options['fawesome_method'] && WP_Optimize_Minify_Functions::is_font_awesome($href)) | |
| 700 | - || ($process_css && !$disable_google_fonts_processing && 'inherit' == $this->options['gfonts_method'] && WP_Optimize_Minify_Functions::is_google_font($href)) | |
| 734 | + || ($process_css && 'inherit' === $this->options['fawesome_method'] && WP_Optimize_Minify_Functions::is_font_awesome($href)) | |
| 735 | + || ($process_css && !$disable_google_fonts_processing && 'inherit' === $this->options['gfonts_method'] && WP_Optimize_Minify_Functions::is_google_font($href)) | |
| 701 | 736 | ) { |
| 702 | - // colect inline css for this handle | |
| 737 | + // collect inline css for this handle | |
| 703 | 738 | if (isset($wp_styles->registered[$handle]->extra['after']) && is_array($wp_styles->registered[$handle]->extra['after'])) { |
| 704 | 739 | $inline_css[$handle] = WP_Optimize_Minify_Functions::minify_css_string(implode('', $wp_styles->registered[$handle]->extra['after'])); // save |
| 705 | 740 | $wp_styles->registered[$handle]->extra['after'] = null; // dequeue |
| 706 | 741 | } |
| @@ -705,9 +740,9 @@ | ||
| 705 | 740 | $wp_styles->registered[$handle]->extra['after'] = null; // dequeue |
| 706 | 741 | } |
| 707 | 742 | |
| 708 | 743 | // process |
| 709 | - if (isset($header[count($header)-1]['handle']) || count($header) == 0 || $header[count($header)-1]['media'] != $mediatype || !$merge_css) { | |
| 744 | + if (isset($header[count($header)-1]['handle']) || count($header) === 0 || $header[count($header)-1]['media'] !== $mediatype || !$merge_css) { | |
| 710 | 745 | array_push($header, array('handles' => array(), 'media' => $mediatype, 'versions' => array())); |
| 711 | 746 | } |
| 712 | 747 | |
| 713 | 748 | // push it to the array |
| @@ -741,9 +776,9 @@ | ||
| 741 | 776 | |
| 742 | 777 | if (!isset($header[$i]['handle'])) { |
| 743 | 778 | |
| 744 | 779 | $handle_str = is_array($header[$i]['handles']) ? $this->array_to_string_conversion($header[$i]['handles']) : strval($header[$i]['handles']); |
| 745 | - $last_modified_str = is_array($header[$i]['last_modified']) ? $this->array_to_string_conversion($header[$i]['last_modified']) : strval($header[$i]['last_modified']); | |
| 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'])); | |
| 746 | 781 | |
| 747 | 782 | if ($merge_css) { |
| 748 | 783 | // get hash for the inline css in this group |
| 749 | 784 | $inline_css_group = array(); |
| @@ -757,9 +792,9 @@ | ||
| 757 | 792 | } |
| 758 | 793 | $inline_css_hash = md5(implode('', $inline_css_group)); |
| 759 | 794 | $hash = hash('adler32', $handle_str . $inline_css_hash . $last_modified_str); |
| 760 | 795 | } else { |
| 761 | - $hash = $handle_str . $last_modified_str; | |
| 796 | + $hash = sanitize_file_name($handle_str . $last_modified_str); | |
| 762 | 797 | } |
| 763 | 798 | |
| 764 | 799 | // static cache file info |
| 765 | 800 | $file_name = 'wpo-minify-header-'.$hash.($minify_css ? '.min' : ''); |
| @@ -773,9 +808,9 @@ | ||
| 773 | 808 | clearstatcache(); |
| 774 | 809 | if (!file_exists($file)) { |
| 775 | 810 | |
| 776 | 811 | // code and log initialization |
| 777 | - $log_header = "PROCESSED on ".date('r')." from ".home_url(add_query_arg(null, null)); | |
| 812 | + $log_header = $this->get_log_header(); | |
| 778 | 813 | $log = array( |
| 779 | 814 | 'header' => $log_header, |
| 780 | 815 | 'files' => array() |
| 781 | 816 | ); |
| @@ -802,9 +837,9 @@ | ||
| 802 | 837 | if (false === $json) { |
| 803 | 838 | $enable_minification = $this->options['enable_css_minification'] && !WP_Optimize_Minify_Functions::is_minified_css_js_filename($href); |
| 804 | 839 | $json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $enable_minification, 'css', $handle, $version); |
| 805 | 840 | if ($this->options['debug']) { |
| 806 | - 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"; | |
| 807 | 842 | } |
| 808 | 843 | WP_Optimize_Minify_Cache_Functions::set_transient($tkey, $json); |
| 809 | 844 | } |
| 810 | 845 | |
| @@ -820,9 +855,9 @@ | ||
| 820 | 855 | $meta_log['files_that_changed'][$handle] = $res['log']; |
| 821 | 856 | } |
| 822 | 857 | |
| 823 | 858 | // response has failed |
| 824 | - if (true != $res['status']) { | |
| 859 | + if (true !== $res['status']) { | |
| 825 | 860 | $log['files'][$handle] = $res['log']; |
| 826 | 861 | $log['files'][$handle]['version'] = $version; |
| 827 | 862 | $log['files'][$handle]['last_modified'] = $last_modified; |
| 828 | 863 | continue; |
| @@ -830,9 +865,9 @@ | ||
| 830 | 865 | |
| 831 | 866 | // Only add the $handle to $done if it was successfully downloaded |
| 832 | 867 | $done[] = $handle; |
| 833 | 868 | // append code to merged file |
| 834 | - $code .= isset($res['code']) ? $res['code'] : ''; | |
| 869 | + $code .= $res['code'] ?? ''; | |
| 835 | 870 | $log['files'][$handle] = $res['log']; |
| 836 | 871 | $log['files'][$handle]['version'] = $version; |
| 837 | 872 | $log['files'][$handle]['last_modified'] = $last_modified; |
| 838 | 873 | |
| @@ -851,9 +886,9 @@ | ||
| 851 | 886 | // generate cache, write log |
| 852 | 887 | if (!empty($code)) { |
| 853 | 888 | // Only generate meta.json file if cache is reset |
| 854 | 889 | if (!empty($meta_log['invalidation_reason'])) { |
| 855 | - file_put_contents($meta_json_file, json_encode($meta_log)); | |
| 890 | + file_put_contents($meta_json_file, wp_json_encode($meta_log)); | |
| 856 | 891 | } |
| 857 | 892 | WP_Optimize_Minify_Print::write_combined_asset($file, $code, $log); |
| 858 | 893 | } |
| 859 | 894 | } else { |
| @@ -894,9 +929,9 @@ | ||
| 894 | 929 | } |
| 895 | 930 | } |
| 896 | 931 | } else { |
| 897 | 932 | // file could not be generated, output something meaningful |
| 898 | - 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))." -->"; | |
| 899 | 934 | echo "<!-- Please check if the path above is correct and ensure your server has write permission there! -->"; |
| 900 | 935 | } |
| 901 | 936 | } |
| 902 | 937 | // other css need to be requeued for the order of files to be kept |
| @@ -954,9 +989,9 @@ | ||
| 954 | 989 | continue; |
| 955 | 990 | } |
| 956 | 991 | |
| 957 | 992 | // Exclude JS files from PageSpeedIndex (Async) takes priority over the ignore list |
| 958 | - if (false != $async_js || is_array($async_js)) { | |
| 993 | + if (!empty($async_js)) { | |
| 959 | 994 | |
| 960 | 995 | // check for string match |
| 961 | 996 | $skipjs = false; |
| 962 | 997 | foreach ($async_js as $l) { |
| @@ -961,15 +996,16 @@ | ||
| 961 | 996 | $skipjs = false; |
| 962 | 997 | foreach ($async_js as $l) { |
| 963 | 998 | if (stripos($href, $l) !== false) { |
| 964 | 999 | // print code if there are no linebreaks, or return |
| 965 | - 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); | |
| 966 | 1002 | $skipjs = true; |
| 967 | 1003 | $done = array_merge($done, array($handle)); |
| 968 | 1004 | break; |
| 969 | 1005 | } |
| 970 | 1006 | } |
| 971 | - if (false != $skipjs) { | |
| 1007 | + if (false !== $skipjs) { | |
| 972 | 1008 | continue; |
| 973 | 1009 | } |
| 974 | 1010 | } |
| 975 | 1011 | |
| @@ -991,10 +1027,10 @@ | ||
| 991 | 1027 | // process |
| 992 | 1028 | if (isset($footer[count($footer)-1]['handle']) |
| 993 | 1029 | || !count($footer) |
| 994 | 1030 | || !$merge_js |
| 995 | - || (isset($scripts->registered[$handle]->extra['strategy']) && $previous_load_strategy != $scripts->registered[$handle]->extra['strategy']) | |
| 996 | - || (!isset($scripts->registered[$handle]->extra['strategy']) && null != $previous_load_strategy) | |
| 1031 | + || (isset($scripts->registered[$handle]->extra['strategy']) && $previous_load_strategy !== $scripts->registered[$handle]->extra['strategy']) | |
| 1032 | + || (!isset($scripts->registered[$handle]->extra['strategy']) && null !== $previous_load_strategy) | |
| 997 | 1033 | ) { |
| 998 | 1034 | array_push($footer, array('handles' => array(), 'versions' => array())); |
| 999 | 1035 | } |
| 1000 | 1036 | |
| @@ -1012,14 +1048,10 @@ | ||
| 1012 | 1048 | if (isset($scripts->registered[$handle]->extra['strategy'])) $footer[count($footer)-1]['strategy'] = $scripts->registered[$handle]->extra['strategy']; |
| 1013 | 1049 | |
| 1014 | 1050 | $footer[count($footer) - 1]['last_modified'][] = WP_Optimize_Minify_Functions::get_modification_time($wp_scripts->registered[$handle]->src); |
| 1015 | 1051 | // external and ignored scripts |
| 1016 | - | |
| 1017 | - if (isset($scripts->registered[$handle]->extra['strategy'])) { | |
| 1018 | - $previous_load_strategy = $scripts->registered[$handle]->extra['strategy']; | |
| 1019 | - } else { | |
| 1020 | - $previous_load_strategy = null; | |
| 1021 | - } | |
| 1052 | + | |
| 1053 | + $previous_load_strategy = $scripts->registered[$handle]->extra['strategy'] ?? null; | |
| 1022 | 1054 | |
| 1023 | 1055 | } else { |
| 1024 | 1056 | array_push($footer, array('handle' => $handle)); |
| 1025 | 1057 | } |
| @@ -1030,15 +1062,15 @@ | ||
| 1030 | 1062 | |
| 1031 | 1063 | if (!isset($footer[$i]['handle'])) { |
| 1032 | 1064 | |
| 1033 | 1065 | $handles_str = is_array($footer[$i]['handles']) ? $this->array_to_string_conversion($footer[$i]['handles']) : strval($footer[$i]['handles']); |
| 1034 | - $last_modified_str = is_array($footer[$i]['last_modified']) ? $this->array_to_string_conversion($footer[$i]['last_modified']) : strval($footer[$i]['last_modified']); | |
| 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'])); | |
| 1035 | 1067 | |
| 1036 | 1068 | if ($merge_js) { |
| 1037 | 1069 | // Change the hash based on last modified timestamp |
| 1038 | 1070 | $hash = hash('adler32', $handles_str . $last_modified_str); |
| 1039 | 1071 | } else { |
| 1040 | - $hash = $handles_str . $last_modified_str; | |
| 1072 | + $hash = sanitize_file_name($handles_str . $last_modified_str); | |
| 1041 | 1073 | } |
| 1042 | 1074 | |
| 1043 | 1075 | // static cache file info |
| 1044 | 1076 | $file_name = 'wpo-minify-footer-'.$hash.($minify_js ? '.min' : ''); |
| @@ -1052,9 +1084,9 @@ | ||
| 1052 | 1084 | clearstatcache(); |
| 1053 | 1085 | if (!file_exists($file)) { |
| 1054 | 1086 | |
| 1055 | 1087 | // code and log initialization |
| 1056 | - $log_header = "PROCESSED on ".date('r')." from ".home_url(add_query_arg(null, null)); | |
| 1088 | + $log_header = $this->get_log_header(); | |
| 1057 | 1089 | $log = array( |
| 1058 | 1090 | 'header' => $log_header, |
| 1059 | 1091 | 'files' => array() |
| 1060 | 1092 | ); |
| @@ -1085,9 +1117,9 @@ | ||
| 1085 | 1117 | if (false === $json) { |
| 1086 | 1118 | $enable_minification = $minify_js && !WP_Optimize_Minify_Functions::is_minified_css_js_filename($href); |
| 1087 | 1119 | $json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $enable_minification, 'js', $handle, $version); |
| 1088 | 1120 | if ($this->options['debug']) { |
| 1089 | - 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"; | |
| 1090 | 1122 | } |
| 1091 | 1123 | WP_Optimize_Minify_Cache_Functions::set_transient($tkey, $json); |
| 1092 | 1124 | } |
| 1093 | 1125 | |
| @@ -1103,9 +1135,9 @@ | ||
| 1103 | 1135 | $meta_log['files_that_changed'][$handle] = $res['log']; |
| 1104 | 1136 | } |
| 1105 | 1137 | |
| 1106 | 1138 | // response has failed |
| 1107 | - if (true != $res['status']) { | |
| 1139 | + if (true !== $res['status']) { | |
| 1108 | 1140 | $log['files'][$handle] = $res['log']; |
| 1109 | 1141 | $log['files'][$handle]['uses_loading_strategy'] = isset($footer[$i]['strategy']); |
| 1110 | 1142 | $log['files'][$handle]['version'] = $version; |
| 1111 | 1143 | $log['files'][$handle]['last_modified'] = $last_modified; |
| @@ -1137,9 +1169,9 @@ | ||
| 1137 | 1169 | } |
| 1138 | 1170 | |
| 1139 | 1171 | // append code to merged file |
| 1140 | 1172 | $code .= $before_code . "\n"; |
| 1141 | - $code .= isset($res['code']) ? $res['code'] : ''; | |
| 1173 | + $code .= $res['code'] ?? ''; | |
| 1142 | 1174 | $code .= "\n" . $after_code . "\n"; |
| 1143 | 1175 | $code = WP_Optimize_Minify_Functions::prepare_merged_js($code, $href); |
| 1144 | 1176 | $log['files'][$handle] = $res['log']; |
| 1145 | 1177 | $log['files'][$handle]['uses_loading_strategy'] = isset($footer[$i]['strategy']); |
| @@ -1156,9 +1188,9 @@ | ||
| 1156 | 1188 | // generate cache, write log |
| 1157 | 1189 | if (!empty($code)) { |
| 1158 | 1190 | // Only generate meta.json file if cache is reset |
| 1159 | 1191 | if (!empty($meta_log['invalidation_reason'])) { |
| 1160 | - file_put_contents($meta_json_file, json_encode($meta_log)); | |
| 1192 | + file_put_contents($meta_json_file, wp_json_encode($meta_log)); | |
| 1161 | 1193 | } |
| 1162 | 1194 | WP_Optimize_Minify_Print::write_combined_asset($file, $code, $log); |
| 1163 | 1195 | } |
| 1164 | 1196 | } else { |
| @@ -1182,9 +1214,9 @@ | ||
| 1182 | 1214 | } |
| 1183 | 1215 | } |
| 1184 | 1216 | |
| 1185 | 1217 | // register minified file |
| 1186 | - 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 | |
| 1187 | 1219 | |
| 1188 | 1220 | // add all extra data from wp_localize_script |
| 1189 | 1221 | $before_code = ''; |
| 1190 | 1222 | $data = array(); |
| @@ -1227,9 +1259,9 @@ | ||
| 1227 | 1259 | wp_script_add_data( "wpo_min-footer-$i", 'strategy', $footer[$i]['strategy']); |
| 1228 | 1260 | } |
| 1229 | 1261 | } else { |
| 1230 | 1262 | // file could not be generated, output something meaningful |
| 1231 | - 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))." -->"; | |
| 1232 | 1264 | echo "<!-- Please check if the path above is correct and ensure your server has write permission there! -->"; |
| 1233 | 1265 | } |
| 1234 | 1266 | |
| 1235 | 1267 | // other scripts need to be requeued for the order of files to be kept |
| @@ -1244,9 +1276,9 @@ | ||
| 1244 | 1276 | } |
| 1245 | 1277 | |
| 1246 | 1278 | /** |
| 1247 | 1279 | * Process header JavaScript |
| 1248 | - * Dependant on 'enable_js' option | |
| 1280 | + * Dependent on 'enable_js' option | |
| 1249 | 1281 | * |
| 1250 | 1282 | * @return void |
| 1251 | 1283 | */ |
| 1252 | 1284 | public function process_header_scripts() { |
| @@ -1277,9 +1309,9 @@ | ||
| 1277 | 1309 | // get full url |
| 1278 | 1310 | $href = WP_Optimize_Minify_Functions::get_hurl($wp_scripts->registered[$handle]->src); |
| 1279 | 1311 | // inlined scripts without file |
| 1280 | 1312 | if (empty($href)) { |
| 1281 | - wp_enqueue_script($handle, false); | |
| 1313 | + wp_enqueue_script($handle, false); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion -- Not needed | |
| 1282 | 1314 | continue; |
| 1283 | 1315 | } |
| 1284 | 1316 | |
| 1285 | 1317 | // Only go through items without a group |
| @@ -1288,9 +1320,10 @@ | ||
| 1288 | 1320 | // check for string match |
| 1289 | 1321 | $skipjs = false; |
| 1290 | 1322 | foreach ($async_js as $l) { |
| 1291 | 1323 | if (stripos($href, $l) !== false) { |
| 1292 | - 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); | |
| 1293 | 1326 | $skipjs = true; |
| 1294 | 1327 | $done = array_merge($done, array($handle)); |
| 1295 | 1328 | break; |
| 1296 | 1329 | } |
| @@ -1323,10 +1356,10 @@ | ||
| 1323 | 1356 | // process |
| 1324 | 1357 | if (isset($header[count($header)-1]['handle']) |
| 1325 | 1358 | || !count($header) |
| 1326 | 1359 | || !$merge_js |
| 1327 | - || (isset($scripts->registered[$handle]->extra['strategy']) && $previous_load_strategy != $scripts->registered[$handle]->extra['strategy']) | |
| 1328 | - || (!isset($scripts->registered[$handle]->extra['strategy']) && null != $previous_load_strategy) | |
| 1360 | + || (isset($scripts->registered[$handle]->extra['strategy']) && $previous_load_strategy !== $scripts->registered[$handle]->extra['strategy']) | |
| 1361 | + || (!isset($scripts->registered[$handle]->extra['strategy']) && null !== $previous_load_strategy) | |
| 1329 | 1362 | ) { |
| 1330 | 1363 | array_push($header, array('handles' => array(), 'versions' => array())); |
| 1331 | 1364 | } |
| 1332 | 1365 | |
| @@ -1364,15 +1397,11 @@ | ||
| 1364 | 1397 | $header[count($header) - 1]['last_modified'][] = WP_Optimize_Minify_Functions::get_modification_time($wp_scripts->registered[$handle]->src); |
| 1365 | 1398 | |
| 1366 | 1399 | // Register strategy if it is used |
| 1367 | 1400 | if (isset($scripts->registered[$handle]->extra['strategy'])) $header[count($header)-1]['strategy'] = $scripts->registered[$handle]->extra['strategy']; |
| 1368 | - | |
| 1369 | - if (isset($scripts->registered[$handle]->extra['strategy'])) { | |
| 1370 | - $previous_load_strategy = $scripts->registered[$handle]->extra['strategy']; | |
| 1371 | - } else { | |
| 1372 | - $previous_load_strategy = null; | |
| 1373 | - } | |
| 1374 | 1401 | |
| 1402 | + $previous_load_strategy = $scripts->registered[$handle]->extra['strategy'] ?? null; | |
| 1403 | + | |
| 1375 | 1404 | |
| 1376 | 1405 | // external and ignored scripts |
| 1377 | 1406 | } else { |
| 1378 | 1407 | // add the ignored assets |
| @@ -1380,9 +1409,9 @@ | ||
| 1380 | 1409 | } |
| 1381 | 1410 | |
| 1382 | 1411 | // make sure that the scripts skipped here, show up in the footer |
| 1383 | 1412 | } else { |
| 1384 | - 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 | |
| 1385 | 1414 | } |
| 1386 | 1415 | } |
| 1387 | 1416 | |
| 1388 | 1417 | // loop through header scripts and merge |
| @@ -1390,14 +1419,14 @@ | ||
| 1390 | 1419 | |
| 1391 | 1420 | if (!isset($header[$i]['handle'])) { |
| 1392 | 1421 | |
| 1393 | 1422 | $handles_str = is_array($header[$i]['handles']) ? $this->array_to_string_conversion($header[$i]['handles']) : strval($header[$i]['handles']); |
| 1394 | - $last_modified_str = is_array($header[$i]['last_modified']) ? $this->array_to_string_conversion($header[$i]['last_modified']) : strval($header[$i]['last_modified']); | |
| 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'])); | |
| 1395 | 1424 | |
| 1396 | 1425 | if ($merge_js) { |
| 1397 | 1426 | $hash = hash('adler32', $handles_str . $last_modified_str); |
| 1398 | 1427 | } else { |
| 1399 | - $hash = $handles_str . $last_modified_str; | |
| 1428 | + $hash = sanitize_file_name($handles_str . $last_modified_str); | |
| 1400 | 1429 | } |
| 1401 | 1430 | |
| 1402 | 1431 | // static cache file info |
| 1403 | 1432 | $file_name = 'wpo-minify-header-'.$hash.($minify_js ? '.min' : ''); |
| @@ -1410,9 +1439,9 @@ | ||
| 1410 | 1439 | clearstatcache(); |
| 1411 | 1440 | if (!file_exists($file)) { |
| 1412 | 1441 | |
| 1413 | 1442 | // code and log initialization |
| 1414 | - $log_header = "PROCESSED on ".date('r')." from ".home_url(add_query_arg(null, null)); | |
| 1443 | + $log_header = $this->get_log_header(); | |
| 1415 | 1444 | $log = array( |
| 1416 | 1445 | 'header' => $log_header, |
| 1417 | 1446 | 'files' => array() |
| 1418 | 1447 | ); |
| @@ -1440,9 +1469,9 @@ | ||
| 1440 | 1469 | if (false === $json) { |
| 1441 | 1470 | $enable_minification = $minify_js && !WP_Optimize_Minify_Functions::is_minified_css_js_filename($href); |
| 1442 | 1471 | $json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $enable_minification, 'js', $handle, $version); |
| 1443 | 1472 | if ($this->options['debug']) { |
| 1444 | - 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"; | |
| 1445 | 1474 | } |
| 1446 | 1475 | WP_Optimize_Minify_Cache_Functions::set_transient($tkey, $json); |
| 1447 | 1476 | } |
| 1448 | 1477 | |
| @@ -1458,9 +1487,9 @@ | ||
| 1458 | 1487 | $meta_log['files_that_changed'][$handle] = $res['log']; |
| 1459 | 1488 | } |
| 1460 | 1489 | |
| 1461 | 1490 | // response has failed |
| 1462 | - if (true != $res['status']) { | |
| 1491 | + if (true !== $res['status']) { | |
| 1463 | 1492 | $log['files'][$handle] = $res['log']; |
| 1464 | 1493 | $log['files'][$handle]['uses_loading_strategy'] = isset($header[$i]['strategy']); |
| 1465 | 1494 | $log['files'][$handle]['version'] = $version; |
| 1466 | 1495 | $log['files'][$handle]['last_modified'] = $last_modified; |
| @@ -1513,9 +1542,9 @@ | ||
| 1513 | 1542 | // generate cache, write log |
| 1514 | 1543 | if (!empty($code)) { |
| 1515 | 1544 | // Only generate meta.json file if cache is reset |
| 1516 | 1545 | if (!empty($meta_log['invalidation_reason'])) { |
| 1517 | - file_put_contents($meta_json_file, json_encode($meta_log)); | |
| 1546 | + file_put_contents($meta_json_file, wp_json_encode($meta_log)); | |
| 1518 | 1547 | } |
| 1519 | 1548 | WP_Optimize_Minify_Print::write_combined_asset($file, $code, $log); |
| 1520 | 1549 | } |
| 1521 | 1550 | } else { |
| @@ -1541,9 +1570,9 @@ | ||
| 1541 | 1570 | } |
| 1542 | 1571 | |
| 1543 | 1572 | // register minified file |
| 1544 | 1573 | $dependencies = isset($excluded_dependencies[$i]) ? $excluded_dependencies[$i] : array(); |
| 1545 | - 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 | |
| 1546 | 1575 | |
| 1547 | 1576 | // add all extra data from wp_localize_script |
| 1548 | 1577 | $before_code = ''; |
| 1549 | 1578 | $data = array(); |
| @@ -1587,9 +1616,9 @@ | ||
| 1587 | 1616 | wp_script_add_data( "wpo_min-header-$i", 'strategy', $header[$i]['strategy']); |
| 1588 | 1617 | } |
| 1589 | 1618 | } else { |
| 1590 | 1619 | // file could not be generated, output something meaningful |
| 1591 | - 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))." -->"; | |
| 1592 | 1621 | echo "<!-- Please check if the path above is correct and ensure your server has write permission there! -->"; |
| 1593 | 1622 | echo "<!-- If you found a bug, please report this on https://wordpress.org/support/plugin/wp-optimize/ -->"; |
| 1594 | 1623 | } |
| 1595 | 1624 | |
| @@ -1642,9 +1671,9 @@ | ||
| 1642 | 1671 | $wp_styles->done = $done; |
| 1643 | 1672 | return; |
| 1644 | 1673 | } |
| 1645 | 1674 | |
| 1646 | - // dequeue and get a list of google fonts, or requeue external | |
| 1675 | + // dequeue and get a list of Google fonts, or requeue external | |
| 1647 | 1676 | foreach ($styles->to_do as $handle) { |
| 1648 | 1677 | $href = WP_Optimize_Minify_Functions::get_hurl($wp_styles->registered[$handle]->src); |
| 1649 | 1678 | // inlined scripts without file |
| 1650 | 1679 | if (empty($href)) continue; |
| @@ -1711,15 +1740,14 @@ | ||
| 1711 | 1740 | WP_Optimize_Minify_Print::inline_style($handle, $href); |
| 1712 | 1741 | $done = array_merge($done, array($handle)); |
| 1713 | 1742 | } else { |
| 1714 | 1743 | // fallback, enqueue google fonts |
| 1715 | - 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 | |
| 1716 | 1745 | } |
| 1717 | 1746 | } |
| 1718 | 1747 | } |
| 1719 | 1748 | } |
| 1720 | 1749 | |
| 1721 | - | |
| 1722 | 1750 | // get groups of handles |
| 1723 | 1751 | $uniq = array(); |
| 1724 | 1752 | foreach ($styles->to_do as $handle) { |
| 1725 | 1753 | |
| @@ -1732,11 +1760,11 @@ | ||
| 1732 | 1760 | $conditional = null; if (isset($wp_styles->registered[$handle]->extra["conditional"])) { |
| 1733 | 1761 | $conditional = $wp_styles->registered[$handle]->extra["conditional"]; // such as ie7, ie8, ie9, etc |
| 1734 | 1762 | } |
| 1735 | 1763 | |
| 1736 | - // mediatype | |
| 1764 | + // media type | |
| 1737 | 1765 | $mt = isset($wp_styles->registered[$handle]->args) ? $wp_styles->registered[$handle]->args : 'all'; |
| 1738 | - if ('screen' == $mt || 'screen, print' == $mt || empty($mt) || is_null($mt) || false == $mt) { | |
| 1766 | + if ('screen' === $mt || 'screen, print' === $mt || empty($mt)) { | |
| 1739 | 1767 | $mt = 'all'; |
| 1740 | 1768 | } |
| 1741 | 1769 | $mediatype = $mt; |
| 1742 | 1770 | |
| @@ -1766,11 +1794,14 @@ | ||
| 1766 | 1794 | if ($ieonly) { |
| 1767 | 1795 | continue; |
| 1768 | 1796 | } |
| 1769 | 1797 | |
| 1798 | + $version = '?ver=' . (!empty($version) ? $version : $wp_styles->default_version); | |
| 1799 | + $href_versioned = $href . $version; | |
| 1800 | + | |
| 1770 | 1801 | // Exclude specific CSS files from PageSpeedIndex? |
| 1771 | - if (false != $async_css && is_array($async_css) && WP_Optimize_Minify_Functions::in_arrayi($href, $async_css)) { | |
| 1772 | - 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); | |
| 1773 | 1804 | $done = array_merge($done, array($handle)); |
| 1774 | 1805 | continue; |
| 1775 | 1806 | } |
| 1776 | 1807 | |
| @@ -1775,17 +1806,17 @@ | ||
| 1775 | 1806 | } |
| 1776 | 1807 | |
| 1777 | 1808 | if (WP_Optimize_Minify_Functions::is_font_awesome($href)) { |
| 1778 | 1809 | if ('inline' === $this->options['fawesome_method']) { |
| 1779 | - WP_Optimize_Minify_Print::inline_style($handle, $href); | |
| 1810 | + WP_Optimize_Minify_Print::inline_style($handle, $href_versioned); | |
| 1780 | 1811 | $done = array_merge($done, array($handle)); |
| 1781 | 1812 | continue; |
| 1782 | 1813 | } elseif ('async' === $this->options['fawesome_method']) { |
| 1783 | - WP_Optimize_Minify_Print::async_style($href, $mediatype); | |
| 1814 | + WP_Optimize_Minify_Print::async_style($href_versioned, $mediatype); | |
| 1784 | 1815 | $done = array_merge($done, array($handle)); |
| 1785 | 1816 | continue; |
| 1786 | 1817 | } elseif ('exclude' === $this->options['fawesome_method']) { |
| 1787 | - WP_Optimize_Minify_Print::exclude_style($href); | |
| 1818 | + WP_Optimize_Minify_Print::exclude_style($href_versioned); | |
| 1788 | 1819 | $done = array_merge($done, array($handle)); |
| 1789 | 1820 | continue; |
| 1790 | 1821 | } |
| 1791 | 1822 | } |
| @@ -1803,9 +1834,9 @@ | ||
| 1803 | 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())) |
| 1804 | 1835 | || empty($href) |
| 1805 | 1836 | ) { |
| 1806 | 1837 | |
| 1807 | - // colect inline css for this handle | |
| 1838 | + // collect inline css for this handle | |
| 1808 | 1839 | if (isset($wp_styles->registered[$handle]->extra['after']) && is_array($wp_styles->registered[$handle]->extra['after'])) { |
| 1809 | 1840 | $inline_css[$handle] = WP_Optimize_Minify_Functions::minify_css_string(implode('', $wp_styles->registered[$handle]->extra['after'])); // save |
| 1810 | 1841 | $wp_styles->registered[$handle]->extra['after'] = null; // dequeue |
| 1811 | 1842 | } |
| @@ -1810,9 +1841,9 @@ | ||
| 1810 | 1841 | $wp_styles->registered[$handle]->extra['after'] = null; // dequeue |
| 1811 | 1842 | } |
| 1812 | 1843 | |
| 1813 | 1844 | // process |
| 1814 | - 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) { | |
| 1815 | 1846 | array_push($footer, array('handles' => array(), 'media' => $mediatype, 'versions' => array())); |
| 1816 | 1847 | } |
| 1817 | 1848 | |
| 1818 | 1849 | // push it to the array get latest modified time |
| @@ -1842,9 +1873,9 @@ | ||
| 1842 | 1873 | |
| 1843 | 1874 | if (!isset($footer[$i]['handle'])) { |
| 1844 | 1875 | |
| 1845 | 1876 | $handles_str = is_array($footer[$i]['handles']) ? $this->array_to_string_conversion($footer[$i]['handles']) : strval($footer[$i]['handles']); |
| 1846 | - $last_modified_str = is_array($footer[$i]['last_modified']) ? $this->array_to_string_conversion($footer[$i]['last_modified']) : strval($footer[$i]['last_modified']); | |
| 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'])); | |
| 1847 | 1878 | |
| 1848 | 1879 | if ($merge_css) { |
| 1849 | 1880 | // get hash for the inline css in this group |
| 1850 | 1881 | $inline_css_group = array(); |
| @@ -1857,9 +1888,9 @@ | ||
| 1857 | 1888 | } |
| 1858 | 1889 | $inline_css_hash = md5(implode('', $inline_css_group)); |
| 1859 | 1890 | $hash = hash('adler32', $handles_str . $inline_css_hash . $last_modified_str); |
| 1860 | 1891 | } else { |
| 1861 | - $hash = $handles_str . $last_modified_str; | |
| 1892 | + $hash = sanitize_file_name($handles_str . $last_modified_str); | |
| 1862 | 1893 | } |
| 1863 | 1894 | |
| 1864 | 1895 | // static cache file info |
| 1865 | 1896 | $file_name = 'wpo-minify-footer-'.$hash.($minify_css ? '.min' : ''); |
| @@ -1873,9 +1904,9 @@ | ||
| 1873 | 1904 | clearstatcache(); |
| 1874 | 1905 | if (!file_exists($file)) { |
| 1875 | 1906 | |
| 1876 | 1907 | // code and log initialization |
| 1877 | - $log_header = "PROCESSED on ".date('r')." from ".home_url(add_query_arg(null, null)); | |
| 1908 | + $log_header = $this->get_log_header(); | |
| 1878 | 1909 | $log = array( |
| 1879 | 1910 | 'header' => $log_header, |
| 1880 | 1911 | 'files' => array() |
| 1881 | 1912 | ); |
| @@ -1890,9 +1921,9 @@ | ||
| 1890 | 1921 | $handle_src = $wp_styles->registered[$handle]->src; |
| 1891 | 1922 | $href = WP_Optimize_Minify_Functions::get_hurl($handle_src); |
| 1892 | 1923 | $version = $wp_styles->registered[$handle]->ver; |
| 1893 | 1924 | $last_modified = WP_Optimize_Minify_Functions::get_modification_time($handle_src); |
| 1894 | - // inlined scripts without hreffile | |
| 1925 | + // inlined scripts without href file | |
| 1895 | 1926 | if (empty($href)) continue; |
| 1896 | 1927 | |
| 1897 | 1928 | // download, minify, cache |
| 1898 | 1929 | $tkey = 'css-'.hash('adler32', $handle . $href).'.css'; |
| @@ -1901,9 +1932,9 @@ | ||
| 1901 | 1932 | if (false === $json) { |
| 1902 | 1933 | $enable_minification = $minify_css && !WP_Optimize_Minify_Functions::is_minified_css_js_filename($href); |
| 1903 | 1934 | $json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $enable_minification, 'css', $handle, $version); |
| 1904 | 1935 | if ($this->options['debug']) { |
| 1905 | - 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"; | |
| 1906 | 1937 | } |
| 1907 | 1938 | WP_Optimize_Minify_Cache_Functions::set_transient($tkey, $json); |
| 1908 | 1939 | } |
| 1909 | 1940 | |
| @@ -1919,9 +1950,9 @@ | ||
| 1919 | 1950 | $meta_log['files_that_changed'][$handle] = $res['log']; |
| 1920 | 1951 | } |
| 1921 | 1952 | |
| 1922 | 1953 | // response has failed |
| 1923 | - if (true != $res['status']) { | |
| 1954 | + if (true !== $res['status']) { | |
| 1924 | 1955 | $log['files'][$handle] = $res['log']; |
| 1925 | 1956 | $log['files'][$handle]['version'] = $version; |
| 1926 | 1957 | $log['files'][$handle]['last_modified'] = $last_modified; |
| 1927 | 1958 | continue; |
| @@ -1929,9 +1960,9 @@ | ||
| 1929 | 1960 | |
| 1930 | 1961 | $done[] = $handle; |
| 1931 | 1962 | |
| 1932 | 1963 | // append code to merged file |
| 1933 | - $code .= isset($res['code']) ? $res['code'] : ''; | |
| 1964 | + $code .= $res['code'] ?? ''; | |
| 1934 | 1965 | $log['files'][$handle] = $res['log']; |
| 1935 | 1966 | $log['files'][$handle]['version'] = $version; |
| 1936 | 1967 | $log['files'][$handle]['last_modified'] = $last_modified; |
| 1937 | 1968 | |
| @@ -1944,15 +1975,15 @@ | ||
| 1944 | 1975 | } else { |
| 1945 | 1976 | wp_dequeue_script($handle); |
| 1946 | 1977 | wp_enqueue_script($handle); |
| 1947 | 1978 | } |
| 1948 | - }; | |
| 1979 | + } | |
| 1949 | 1980 | |
| 1950 | 1981 | // generate cache, add inline css, write log |
| 1951 | 1982 | if (!empty($code)) { |
| 1952 | 1983 | // Only generate meta.json file if cache is reset |
| 1953 | 1984 | if (!empty($meta_log['invalidation_reason'])) { |
| 1954 | - file_put_contents($meta_json_file, json_encode($meta_log)); | |
| 1985 | + file_put_contents($meta_json_file, wp_json_encode($meta_log)); | |
| 1955 | 1986 | } |
| 1956 | 1987 | WP_Optimize_Minify_Print::write_combined_asset($file, $code, $log); |
| 1957 | 1988 | } |
| 1958 | 1989 | } else { |
| @@ -1988,13 +2019,13 @@ | ||
| 1988 | 2019 | if (filesize($file) < 20000 && $this->options['inline_css']) { |
| 1989 | 2020 | $this->inline_css(file_get_contents($file), $handle, $file_url, $footer[$i]['media']); |
| 1990 | 2021 | } else { |
| 1991 | 2022 | // enqueue it |
| 1992 | - 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 | |
| 1993 | 2024 | } |
| 1994 | 2025 | } else { |
| 1995 | 2026 | // file could not be generated, output something meaningful |
| 1996 | - 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))." -->"; | |
| 1997 | 2028 | echo "<!-- Please check if the path above is correct and ensure your server has write permission there! -->"; |
| 1998 | 2029 | } |
| 1999 | 2030 | } |
| 2000 | 2031 | |
| @@ -2011,11 +2042,11 @@ | ||
| 2011 | 2042 | |
| 2012 | 2043 | /** |
| 2013 | 2044 | * Process JS Scripts of type module |
| 2014 | 2045 | * |
| 2015 | - * @param String $buffer Page HTML. | |
| 2046 | + * @param string $buffer Page HTML. | |
| 2016 | 2047 | * |
| 2017 | - * @return String | |
| 2048 | + * @return string | |
| 2018 | 2049 | */ |
| 2019 | 2050 | public function process_script_modules($buffer) { |
| 2020 | 2051 | |
| 2021 | 2052 | if ('' === $buffer) return ''; |
| @@ -2034,8 +2065,19 @@ | ||
| 2034 | 2065 | foreach ($scripts as $script) { |
| 2035 | 2066 | if ("module" !== $script['type']) continue; |
| 2036 | 2067 | |
| 2037 | 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 | + | |
| 2038 | 2080 | $last_modified = WP_Optimize_Minify_Functions::get_modification_time($script['url']); |
| 2039 | 2081 | $handle = $script['handle']; |
| 2040 | 2082 | $version = $script['version']; |
| 2041 | 2083 | |
| @@ -2047,9 +2089,9 @@ | ||
| 2047 | 2089 | continue; |
| 2048 | 2090 | } |
| 2049 | 2091 | |
| 2050 | 2092 | // code and log initialization |
| 2051 | - $log_header = "PROCESSED on ".date('r')." from ".home_url(add_query_arg(null, null)); | |
| 2093 | + $log_header = $this->get_log_header(); | |
| 2052 | 2094 | $log = array( |
| 2053 | 2095 | 'header' => $log_header, |
| 2054 | 2096 | 'files' => array() |
| 2055 | 2097 | ); |
| @@ -2068,9 +2110,9 @@ | ||
| 2068 | 2110 | if (false === $json) { |
| 2069 | 2111 | $enable_minification = $minify_js && !WP_Optimize_Minify_Functions::is_minified_css_js_filename($href); |
| 2070 | 2112 | $json = WP_Optimize_Minify_Functions::download_and_minify($href, null, $enable_minification, 'js', $handle_no_slash, $version); |
| 2071 | 2113 | if ($this->options['debug']) { |
| 2072 | - echo "<!-- wpo_min DEBUG: Uncached file processing now for $handle / $href / $version -->\n"; | |
| 2114 | + $buffer .= "<!-- wpo_min DEBUG: Uncached file processing now for " . esc_html($handle) . " / " . esc_html($href) . " / " . esc_html($version) . " -->\n"; | |
| 2073 | 2115 | } |
| 2074 | 2116 | WP_Optimize_Minify_Cache_Functions::set_transient($tkey, $json); |
| 2075 | 2117 | } |
| 2076 | 2118 | |
| @@ -2088,9 +2130,9 @@ | ||
| 2088 | 2130 | } |
| 2089 | 2131 | |
| 2090 | 2132 | if (!$res['status']) continue; |
| 2091 | 2133 | |
| 2092 | - $code .= isset($res['code']) ? $res['code'] : ''; | |
| 2134 | + $code .= $res['code'] ?? ''; | |
| 2093 | 2135 | $code = WP_Optimize_Minify_Functions::prepare_merged_js($code, $href); |
| 2094 | 2136 | $log['files'][$handle] = $res['log']; |
| 2095 | 2137 | $log['files'][$handle]['version'] = $version; |
| 2096 | 2138 | $log['files'][$handle]['last_modified'] = $last_modified; |
| @@ -2099,9 +2141,9 @@ | ||
| 2099 | 2141 | if (empty($code)) continue; |
| 2100 | 2142 | |
| 2101 | 2143 | // Only generate meta.json file if cache is reset |
| 2102 | 2144 | if (!empty($meta_log['invalidation_reason'])) { |
| 2103 | - file_put_contents($meta_json_file, json_encode($meta_log)); | |
| 2145 | + file_put_contents($meta_json_file, wp_json_encode($meta_log)); | |
| 2104 | 2146 | } |
| 2105 | 2147 | WP_Optimize_Minify_Print::write_combined_asset($file, $code, $log); |
| 2106 | 2148 | |
| 2107 | 2149 | // Extract importmap script tag |
| @@ -2107,17 +2149,14 @@ | ||
| 2107 | 2149 | // Extract importmap script tag |
| 2108 | 2150 | $json_map = $this->get_script_module_importmap($buffer); |
| 2109 | 2151 | $json_map_update = $json_map; |
| 2110 | 2152 | |
| 2111 | - if (!empty($json_map) && isset($json_map['imports'])) { | |
| 2153 | + // Update module source file and importmap with new file url (if necessary) | |
| 2154 | + $buffer = str_replace($script['url'], $file_url, $buffer); | |
| 2155 | + if (!empty($json_map) && isset($json_map['imports'][$handle])) { | |
| 2112 | 2156 | $json_map_update['imports'][$handle] = $file_url; |
| 2113 | - } else { | |
| 2114 | - continue; | |
| 2157 | + $buffer = str_replace(wp_json_encode($json_map), wp_json_encode($json_map_update), $buffer); | |
| 2115 | 2158 | } |
| 2116 | - | |
| 2117 | - // Update module source file and importmap with new file url | |
| 2118 | - $buffer = str_replace($script['url'], $file_url, $buffer); | |
| 2119 | - $buffer = str_replace(json_encode($json_map), json_encode($json_map_update), $buffer); | |
| 2120 | 2159 | } |
| 2121 | 2160 | |
| 2122 | 2161 | return $buffer; |
| 2123 | 2162 | } |
| @@ -2167,9 +2206,9 @@ | ||
| 2167 | 2206 | * @return void; |
| 2168 | 2207 | */ |
| 2169 | 2208 | public function add_google_fonts_merged() { |
| 2170 | 2209 | // must have something to do |
| 2171 | - if (count($this->collect_google_fonts) == 0) return; | |
| 2210 | + if (count($this->collect_google_fonts) === 0) return; | |
| 2172 | 2211 | |
| 2173 | 2212 | // merge google fonts |
| 2174 | 2213 | $href = WP_Optimize_Minify_Fonts::concatenate_google_fonts($this->collect_google_fonts); |
| 2175 | 2214 | if (empty($href)) { |
| @@ -2215,14 +2254,19 @@ | ||
| 2215 | 2254 | // always false for admin pages |
| 2216 | 2255 | if (is_admin()) return false; |
| 2217 | 2256 | |
| 2218 | 2257 | // get host with multisite support and query strings |
| 2219 | - $host = htmlentities($_SERVER['SERVER_NAME']); | |
| 2258 | + $host = isset($_SERVER['SERVER_NAME']) ? sanitize_text_field(wp_unslash($_SERVER['SERVER_NAME'])) : ''; | |
| 2220 | 2259 | if (empty($host)) { |
| 2221 | - $host = isset($_SERVER['HTTP_HOST']) ? htmlentities($_SERVER['HTTP_HOST']) : ''; | |
| 2260 | + $host = isset($_SERVER['HTTP_HOST']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_HOST'])) : ''; | |
| 2222 | 2261 | } |
| 2223 | - $request_query = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY); | |
| 2224 | - $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 | + } | |
| 2225 | 2269 | |
| 2226 | 2270 | // initialize headers |
| 2227 | 2271 | $headers = array(); |
| 2228 | 2272 | |
| @@ -2230,9 +2274,9 @@ | ||
| 2230 | 2274 | if ($this->options['enabled_css_preload'] |
| 2231 | 2275 | && count($this->collect_preload_css) > 0 |
| 2232 | 2276 | ) { |
| 2233 | 2277 | foreach ($this->collect_preload_css as $u) { |
| 2234 | - // 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 | |
| 2235 | 2279 | if (strpos($u, '/assets/wpo-minify-footer-') !== false) { |
| 2236 | 2280 | continue; |
| 2237 | 2281 | } |
| 2238 | 2282 | |
| @@ -2246,9 +2290,9 @@ | ||
| 2246 | 2290 | && count($this->collect_preload_js) > 0 |
| 2247 | 2291 | ) { |
| 2248 | 2292 | foreach ($this->collect_preload_js as $u) { |
| 2249 | 2293 | |
| 2250 | - // 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 | |
| 2251 | 2295 | if (false !== strpos($u, '/assets/wpo-minify-footer-')) { |
| 2252 | 2296 | continue; |
| 2253 | 2297 | } |
| 2254 | 2298 | |
| @@ -2257,9 +2301,9 @@ | ||
| 2257 | 2301 | } |
| 2258 | 2302 | } |
| 2259 | 2303 | |
| 2260 | 2304 | // must have something |
| 2261 | - if (count($headers) == 0) { | |
| 2305 | + if (count($headers) === 0) { | |
| 2262 | 2306 | return false; |
| 2263 | 2307 | } else { |
| 2264 | 2308 | $headers = implode("\n", $headers); |
| 2265 | 2309 | } |
| @@ -2275,9 +2319,9 @@ | ||
| 2275 | 2319 | // reset file cache |
| 2276 | 2320 | clearstatcache(); |
| 2277 | 2321 | |
| 2278 | 2322 | // if there are no query strings |
| 2279 | - if ($b == $a) { | |
| 2323 | + if ($b === $a) { | |
| 2280 | 2324 | if (!file_exists($a)) { |
| 2281 | 2325 | WP_Optimize_Minify_Print::write_header($a, $headers); |
| 2282 | 2326 | } |
| 2283 | 2327 | return false; |
| @@ -2290,13 +2334,13 @@ | ||
| 2290 | 2334 | |
| 2291 | 2335 | /** |
| 2292 | 2336 | * Collect all wpo_min JS files and save them to a headers file |
| 2293 | 2337 | * |
| 2294 | - * @param String $html | |
| 2295 | - * @param String $handle | |
| 2296 | - * @param String $src | |
| 2338 | + * @param string $html | |
| 2339 | + * @param string $handle | |
| 2340 | + * @param string $src | |
| 2297 | 2341 | * |
| 2298 | - * @return String | |
| 2342 | + * @return string | |
| 2299 | 2343 | */ |
| 2300 | 2344 | public function collect_js_preload_headers($html, $handle, $src) { |
| 2301 | 2345 | if (false !== strpos($src, 'assets/wpo-minify')) { |
| 2302 | 2346 | if (!in_array($src, $this->collect_preload_js)) { |
| @@ -2343,14 +2387,18 @@ | ||
| 2343 | 2387 | return false; |
| 2344 | 2388 | } |
| 2345 | 2389 | |
| 2346 | 2390 | // get host with multisite support and query strings |
| 2347 | - $host = htmlentities($_SERVER['SERVER_NAME']); | |
| 2391 | + $host = isset($_SERVER['SERVER_NAME']) ? sanitize_text_field(wp_unslash($_SERVER['SERVER_NAME'])) : ''; | |
| 2348 | 2392 | if (empty($host)) { |
| 2349 | - $host = isset($_SERVER['HTTP_HOST']) ? htmlentities($_SERVER['HTTP_HOST']) : ''; | |
| 2393 | + $host = isset($_SERVER['HTTP_HOST']) ? sanitize_text_field(wp_unslash($_SERVER['HTTP_HOST'])) : ''; | |
| 2350 | 2394 | } |
| 2351 | - $request_query = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY); | |
| 2352 | - $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 | + } | |
| 2353 | 2401 | |
| 2354 | 2402 | // get cache path |
| 2355 | 2403 | $cache_path = WP_Optimize_Minify_Cache_Functions::cache_path(); |
| 2356 | 2404 | $header_dir = $cache_path['headerdir']; |
| @@ -2363,12 +2411,12 @@ | ||
| 2363 | 2411 | // reset file cache |
| 2364 | 2412 | clearstatcache(); |
| 2365 | 2413 | |
| 2366 | 2414 | // return header files or fallback |
| 2367 | - if ($b == $a && file_exists($a)) { | |
| 2415 | + if ($b === $a && file_exists($a)) { | |
| 2368 | 2416 | return file_get_contents($a); |
| 2369 | 2417 | } |
| 2370 | - if ($b != $a && file_exists($b)) { | |
| 2418 | + if ($b !== $a && file_exists($b)) { | |
| 2371 | 2419 | return file_get_contents($b); |
| 2372 | 2420 | } |
| 2373 | 2421 | |
| 2374 | 2422 | return false; |
| @@ -2374,9 +2422,9 @@ | ||
| 2374 | 2422 | return false; |
| 2375 | 2423 | } |
| 2376 | 2424 | |
| 2377 | 2425 | /** |
| 2378 | - * Add pre-connect and pre-load headers | |
| 2426 | + * Add pre-connect and preload headers | |
| 2379 | 2427 | * |
| 2380 | 2428 | * @return void |
| 2381 | 2429 | */ |
| 2382 | 2430 | public function extra_preload_headers() { |
| @@ -2385,9 +2433,9 @@ | ||
| 2385 | 2433 | // fetch headers |
| 2386 | 2434 | $pre_connect = $this->get_pre_connect_list(); |
| 2387 | 2435 | |
| 2388 | 2436 | // preload |
| 2389 | - if (is_array($pre_connect) && count($pre_connect) > 0) { | |
| 2437 | + if (count($pre_connect) > 0) { | |
| 2390 | 2438 | foreach ($pre_connect as $url) { |
| 2391 | 2439 | if (!empty($url) && filter_var($url, FILTER_VALIDATE_URL)) { |
| 2392 | 2440 | header("Link: <$url>; rel=preconnect", false); |
| 2393 | 2441 | } |
| @@ -2394,9 +2442,9 @@ | ||
| 2394 | 2442 | } |
| 2395 | 2443 | } |
| 2396 | 2444 | |
| 2397 | 2445 | $headers = $this->get_preload_headers(); |
| 2398 | - if (false != $headers) { | |
| 2446 | + if ($headers) { | |
| 2399 | 2447 | $nh = array_map('trim', explode("\n", $headers)); |
| 2400 | 2448 | foreach ($nh as $h) { |
| 2401 | 2449 | if (!empty($h)) { |
| 2402 | 2450 | header($h, false); |
| @@ -2455,15 +2503,15 @@ | ||
| 2455 | 2503 | /** |
| 2456 | 2504 | * Handles header meta information removal |
| 2457 | 2505 | */ |
| 2458 | 2506 | private function remove_header_meta_info() { |
| 2459 | - // no resource hints, generator tag, shortlinks, manifest link, etc | |
| 2507 | + // no resource hints, generator tag, shortlink, manifest link, etc | |
| 2460 | 2508 | remove_action('wp_head', 'wp_resource_hints', 2); |
| 2461 | 2509 | remove_action('wp_head', 'wp_generator'); |
| 2462 | 2510 | remove_action('template_redirect', 'wp_shortlink_header', 11); |
| 2463 | 2511 | remove_action('wp_head', 'wlwmanifest_link'); |
| 2464 | 2512 | remove_action('wp_head', 'rsd_link'); |
| 2465 | - remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); | |
| 2513 | + remove_action('wp_head', 'adjacent_posts_rel_link_wp_head'); | |
| 2466 | 2514 | remove_action('wp_head', 'feed_links', 2); |
| 2467 | 2515 | remove_action('wp_head', 'feed_links_extra', 3); |
| 2468 | 2516 | WP_Optimize_Minify_Functions::remove_redundant_shortlink(); |
| 2469 | 2517 | } |
| @@ -2553,11 +2601,11 @@ | ||
| 2553 | 2601 | add_action('wp_head', array($this, 'add_critical_path'), 2); |
| 2554 | 2602 | |
| 2555 | 2603 | // merge, if inline is selected but prevent optimization for these locations |
| 2556 | 2604 | if ($this->options['inline_css']) { |
| 2557 | - // 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' | |
| 2558 | 2606 | add_filter('style_loader_tag', array($this, 'inline_css'), PHP_INT_MAX, 4); |
| 2559 | - // this prints the google fonts | |
| 2607 | + // this prints the Google fonts | |
| 2560 | 2608 | add_action('wp_print_styles', array($this, 'add_google_fonts_merged'), PHP_INT_MAX); |
| 2561 | 2609 | add_action('wp_print_footer_scripts', array($this, 'add_google_fonts_merged'), PHP_INT_MAX); |
| 2562 | 2610 | } |
| 2563 | 2611 | // Preloading |
| @@ -2563,14 +2611,14 @@ | ||
| 2563 | 2611 | // Preloading |
| 2564 | 2612 | if ($this->options['enabled_css_preload']) { |
| 2565 | 2613 | add_filter('style_loader_tag', array($this, 'collect_css_preload_headers'), PHP_INT_MAX, 3); |
| 2566 | 2614 | } |
| 2567 | - // Optimize the css and collect the google fonts for merging | |
| 2615 | + // Optimize the css and collect the Google fonts for merging | |
| 2568 | 2616 | add_action('wp_print_styles', array($this, 'process_header_css'), PHP_INT_MAX); |
| 2569 | 2617 | add_action('wp_print_footer_scripts', array($this, 'process_footer_css'), 9); |
| 2570 | 2618 | |
| 2571 | 2619 | /** |
| 2572 | - * 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 | |
| 2573 | 2621 | * |
| 2574 | 2622 | * @param boolean $maintain_css_order |
| 2575 | 2623 | * @return boolean |
| 2576 | 2624 | * @default true |
| @@ -2576,14 +2624,16 @@ | ||
| 2576 | 2624 | * @default true |
| 2577 | 2625 | */ |
| 2578 | 2626 | if (!apply_filters('wpo_minify_maintain_css_order', true)) { |
| 2579 | 2627 | // Reorder stylesheets |
| 2580 | - 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')); | |
| 2581 | 2629 | } |
| 2582 | 2630 | } |
| 2583 | 2631 | |
| 2584 | 2632 | /** |
| 2585 | 2633 | * Determines whether we should minify html or not |
| 2634 | + * | |
| 2635 | + * @return bool | |
| 2586 | 2636 | */ |
| 2587 | 2637 | private function should_process_html() { |
| 2588 | 2638 | return $this->options['html_minification'] && !is_admin() && $this->is_cache_preload(); |
| 2589 | 2639 | } |
| @@ -2589,8 +2639,10 @@ | ||
| 2589 | 2639 | } |
| 2590 | 2640 | |
| 2591 | 2641 | /** |
| 2592 | 2642 | * Determines whether to use loadCSS polyfill or not |
| 2643 | + * | |
| 2644 | + * @return bool | |
| 2593 | 2645 | */ |
| 2594 | 2646 | private function should_use_loadCSS() { |
| 2595 | 2647 | return $this->options['loadcss'] |
| 2596 | 2648 | || 'async' === $this->options['fawesome_method'] |
| @@ -2682,13 +2734,13 @@ | ||
| 2682 | 2734 | |
| 2683 | 2735 | /** |
| 2684 | 2736 | * Decides whether minify cache should be incremented or not |
| 2685 | 2737 | * |
| 2686 | - * @param string $res | |
| 2738 | + * @param array $res | |
| 2687 | 2739 | * @param string $href |
| 2688 | 2740 | * @param string $type |
| 2689 | 2741 | * @param string $handle |
| 2690 | - * @param string $version | |
| 2742 | + * @param mixed $version | |
| 2691 | 2743 | * |
| 2692 | 2744 | * @return bool true when cache is invalid, false otherwise |
| 2693 | 2745 | */ |
| 2694 | 2746 | private function should_reset_minify_assets($res, $href, $type, $handle, $version) { |
| @@ -2708,6 +2760,18 @@ | ||
| 2708 | 2760 | $hash = hash('sha256', $res['code']); |
| 2709 | 2761 | $new_hash = hash('sha256', $new_res['code']); |
| 2710 | 2762 | return strcmp($hash, $new_hash); |
| 2711 | 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; | |
| 2712 | 2776 | } |
| 2713 | 2777 | } |