| 1 | <?php |
| 2 | |
| 3 | use Automattic\Jetpack_Boost\Lib\Minify\Cleanup_Stored_Paths; |
| 4 | use Automattic\Jetpack_Boost\Lib\Minify\Config; |
| 5 | use Automattic\Jetpack_Boost\Lib\Minify\Dependency_Path_Mapping; |
| 6 | use Automattic\Jetpack_Boost\Lib\Minify\File_Paths; |
| 7 | use Automattic\Jetpack_Boost\Modules\Module; |
| 8 | use Automattic\Jetpack_Boost\Modules\Optimizations\Minify\Minify_CSS; |
| 9 | use Automattic\Jetpack_Boost\Modules\Optimizations\Minify\Minify_JS; |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | function jetpack_boost_minify_cache_buster() { |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | return 2; |
| 22 | } |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | function jetpack_boost_page_optimize_is_already_minified( $fullpath ) { |
| 41 | return (bool) preg_match( '/\.min\.(css|js)$/', (string) $fullpath ); |
| 42 | } |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | function jetpack_boost_minify_concat_max_files() { |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | return apply_filters( 'jetpack_boost_minify_concat_max_files', 150 ); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | function jetpack_boost_should_run_daily_network_cron_job( $hook ) { |
| 61 | |
| 62 | if ( get_site_option( 'jetpack_boost_' . $hook . '_last_run', 0 ) > time() - DAY_IN_SECONDS ) { |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | update_site_option( 'jetpack_boost_' . $hook . '_last_run', time() ); |
| 67 | |
| 68 | return true; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | function jetpack_boost_minify_cron_cache_cleanup() { |
| 75 | if ( ! jetpack_boost_should_run_daily_network_cron_job( 'minify_cron_cache_cleanup' ) ) { |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | jetpack_boost_legacy_minify_cache_cleanup(); |
| 80 | jetpack_boost_minify_cache_cleanup(); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | function jetpack_boost_legacy_minify_cache_cleanup( $file_age = DAY_IN_SECONDS ) { |
| 89 | |
| 90 | |
| 91 | $file_age = is_int( $file_age ) ? $file_age : DAY_IN_SECONDS; |
| 92 | |
| 93 | $cache_folder = Config::get_legacy_cache_dir_path(); |
| 94 | |
| 95 | if ( ! is_dir( $cache_folder ) ) { |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | $cache_files = glob( $cache_folder . '/page-optimize-cache-*' ); |
| 100 | |
| 101 | jetpack_boost_delete_expired_files( $cache_files, $file_age ); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | function jetpack_boost_minify_cache_cleanup( $file_age = DAY_IN_SECONDS ) { |
| 110 | |
| 111 | $file_age = is_int( $file_age ) ? $file_age : DAY_IN_SECONDS; |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | if ( $file_age !== 0 ) { |
| 118 | |
| 119 | jetpack_boost_minify_remove_stale_static_files(); |
| 120 | } |
| 121 | |
| 122 | $cache_files = glob( Config::get_static_cache_dir_path() . '/*.min.*' ); |
| 123 | |
| 124 | jetpack_boost_delete_expired_files( $cache_files, $file_age ); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | function jetpack_boost_delete_expired_files( $files, $file_age ) { |
| 136 | foreach ( $files as $file ) { |
| 137 | if ( ! is_file( $file ) ) { |
| 138 | continue; |
| 139 | } |
| 140 | |
| 141 | if ( $file_age === 0 ) { |
| 142 | wp_delete_file( $file ); |
| 143 | continue; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | |
| 148 | if ( ( time() - $file_age ) > fileatime( $file ) ) { |
| 149 | wp_delete_file( $file ); |
| 150 | } elseif ( ( time() - ( 2 * $file_age ) ) > filemtime( $file ) ) { |
| 151 | wp_delete_file( $file ); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | function jetpack_boost_minify_clear_scheduled_events() { |
| 162 | wp_unschedule_hook( 'jetpack_boost_minify_cron_cache_cleanup' ); |
| 163 | wp_unschedule_hook( 'jetpack_boost_404_tester_cron' ); |
| 164 | Cleanup_Stored_Paths::clear_schedules(); |
| 165 | } |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | function jetpack_boost_page_optimize_deactivate() { |
| 171 | |
| 172 | jetpack_boost_legacy_minify_cache_cleanup( 0 ); |
| 173 | jetpack_boost_minify_cache_cleanup( 0 ); |
| 174 | |
| 175 | jetpack_boost_minify_clear_scheduled_events(); |
| 176 | } |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | function jetpack_boost_enqueued_to_absolute_url( $url ) { |
| 188 | if ( str_starts_with( $url, '/' ) ) { |
| 189 | return home_url( $url ); |
| 190 | } |
| 191 | |
| 192 | return $url; |
| 193 | } |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | function jetpack_boost_page_optimize_js_exclude_list() { |
| 203 | return jetpack_boost_page_optimize_merge_debug_excludes( |
| 204 | jetpack_boost_ds_get( 'minify_js_excludes' ), |
| 205 | 'jb-minify-js-excludes' |
| 206 | ); |
| 207 | } |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | function jetpack_boost_page_optimize_css_exclude_list() { |
| 217 | return jetpack_boost_page_optimize_merge_debug_excludes( |
| 218 | jetpack_boost_ds_get( 'minify_css_excludes' ), |
| 219 | 'jb-minify-css-excludes' |
| 220 | ); |
| 221 | } |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | function jetpack_boost_page_optimize_merge_debug_excludes( $excludes, $param ) { |
| 263 | if ( ! is_array( $excludes ) ) { |
| 264 | $excludes = array(); |
| 265 | } |
| 266 | |
| 267 | |
| 268 | if ( ! isset( $_GET[ $param ] ) || ! is_string( $_GET[ $param ] ) ) { |
| 269 | return $excludes; |
| 270 | } |
| 271 | |
| 272 | |
| 273 | if ( ! function_exists( 'current_user_can' ) || ! current_user_can( 'manage_options' ) ) { |
| 274 | return $excludes; |
| 275 | } |
| 276 | |
| 277 | |
| 278 | $raw_value = wp_unslash( $_GET[ $param ] ); |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | $raw_handles = array_slice( explode( ',', $raw_value ), 0, 100 ); |
| 284 | |
| 285 | $extra_handles = array(); |
| 286 | foreach ( $raw_handles as $handle ) { |
| 287 | $handle = trim( $handle ); |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | if ( $handle !== '' && preg_match( '/^[a-zA-Z0-9_.\-]+$/', $handle ) ) { |
| 296 | $extra_handles[] = $handle; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | if ( empty( $extra_handles ) ) { |
| 301 | return $excludes; |
| 302 | } |
| 303 | |
| 304 | return array_values( array_unique( array_merge( $excludes, $extra_handles ) ) ); |
| 305 | } |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | function jetpack_boost_page_optimize_starts_with( $prefix, $str ) { |
| 311 | $prefix_length = strlen( $prefix ); |
| 312 | if ( strlen( $str ) < $prefix_length ) { |
| 313 | return false; |
| 314 | } |
| 315 | |
| 316 | return substr( $str, 0, $prefix_length ) === $prefix; |
| 317 | } |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | function jetpack_boost_page_optimize_use_concat_base_dir() { |
| 325 | return defined( 'PAGE_OPTIMIZE_CONCAT_BASE_DIR' ) && file_exists( PAGE_OPTIMIZE_CONCAT_BASE_DIR ); |
| 326 | } |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | function jetpack_boost_page_optimize_remove_concat_base_prefix( $original_fs_path ) { |
| 334 | $abspath = Config::get_abspath(); |
| 335 | |
| 336 | |
| 337 | if ( strlen( $abspath ) > strlen( PAGE_OPTIMIZE_CONCAT_BASE_DIR ) ) { |
| 338 | $longer_path = $abspath; |
| 339 | $shorter_path = PAGE_OPTIMIZE_CONCAT_BASE_DIR; |
| 340 | } else { |
| 341 | $longer_path = PAGE_OPTIMIZE_CONCAT_BASE_DIR; |
| 342 | $shorter_path = $abspath; |
| 343 | } |
| 344 | |
| 345 | $prefix_abspath = trailingslashit( $longer_path ); |
| 346 | if ( jetpack_boost_page_optimize_starts_with( $prefix_abspath, $original_fs_path ) ) { |
| 347 | return substr( $original_fs_path, strlen( $prefix_abspath ) ); |
| 348 | } |
| 349 | |
| 350 | $prefix_basedir = trailingslashit( $shorter_path ); |
| 351 | if ( jetpack_boost_page_optimize_starts_with( $prefix_basedir, $original_fs_path ) ) { |
| 352 | return substr( $original_fs_path, strlen( $prefix_basedir ) ); |
| 353 | } |
| 354 | |
| 355 | |
| 356 | return '/page-optimize-resource-outside-base-path/' . basename( $original_fs_path ); |
| 357 | } |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | function jetpack_boost_page_optimize_schedule_404_tester() { |
| 363 | if ( false === wp_next_scheduled( 'jetpack_boost_404_tester_cron' ) ) { |
| 364 | wp_schedule_event( time() + DAY_IN_SECONDS, 'daily', 'jetpack_boost_404_tester_cron' ); |
| 365 | |
| 366 | |
| 367 | jetpack_boost_404_tester(); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | function jetpack_boost_page_optimize_schedule_cache_cleanup() { |
| 375 | |
| 376 | if ( false === wp_next_scheduled( 'jetpack_boost_minify_cron_cache_cleanup' ) ) { |
| 377 | wp_schedule_event( time(), 'daily', 'jetpack_boost_minify_cron_cache_cleanup' ); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | function jetpack_boost_page_optimize_bail() { |
| 388 | static $should_bail = null; |
| 389 | if ( null !== $should_bail ) { |
| 390 | return $should_bail; |
| 391 | } |
| 392 | |
| 393 | $should_bail = false; |
| 394 | |
| 395 | |
| 396 | if ( is_admin() ) { |
| 397 | $should_bail = true; |
| 398 | return true; |
| 399 | } |
| 400 | |
| 401 | |
| 402 | global $wp_customize; |
| 403 | if ( isset( $wp_customize ) ) { |
| 404 | $should_bail = true; |
| 405 | return true; |
| 406 | } |
| 407 | |
| 408 | |
| 409 | |
| 410 | if ( ! empty( $_GET['et_fb'] ) && 'Divi' === wp_get_theme()->get_template() ) { |
| 411 | $should_bail = true; |
| 412 | return true; |
| 413 | } |
| 414 | |
| 415 | |
| 416 | |
| 417 | if ( class_exists( 'Brizy_Editor' ) && method_exists( 'Brizy_Editor', 'prefix' ) && ( isset( $_GET[ Brizy_Editor::prefix( '-edit-iframe' ) ] ) || isset( $_GET[ Brizy_Editor::prefix( '-edit' ) ] ) ) ) { |
| 418 | $should_bail = true; |
| 419 | return true; |
| 420 | } |
| 421 | |
| 422 | |
| 423 | |
| 424 | if ( isset( $_GET['elementor-preview'] ) ) { |
| 425 | $should_bail = true; |
| 426 | return true; |
| 427 | } |
| 428 | |
| 429 | return $should_bail; |
| 430 | } |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | function jetpack_boost_page_optimize_cache_bust_mtime( $path, $siteurl ) { |
| 436 | static $dependency_path_mapping; |
| 437 | |
| 438 | |
| 439 | if ( str_starts_with( $path, '/' ) ) { |
| 440 | $parts = wp_parse_url( $siteurl ); |
| 441 | $siteurl = $parts['scheme'] . '://' . $parts['host']; |
| 442 | } |
| 443 | |
| 444 | $url = $siteurl . $path; |
| 445 | |
| 446 | if ( str_contains( $url, '?m=' ) ) { |
| 447 | return $url; |
| 448 | } |
| 449 | |
| 450 | $parts = wp_parse_url( $url ); |
| 451 | if ( ! isset( $parts['path'] ) || empty( $parts['path'] ) ) { |
| 452 | return $url; |
| 453 | } |
| 454 | |
| 455 | if ( empty( $dependency_path_mapping ) ) { |
| 456 | $dependency_path_mapping = new Dependency_Path_Mapping(); |
| 457 | } |
| 458 | |
| 459 | $file = $dependency_path_mapping->dependency_src_to_fs_path( $url ); |
| 460 | |
| 461 | $mtime = false; |
| 462 | if ( file_exists( $file ) ) { |
| 463 | $mtime = filemtime( $file ); |
| 464 | } |
| 465 | |
| 466 | if ( ! $mtime ) { |
| 467 | return $url; |
| 468 | } |
| 469 | |
| 470 | if ( ! str_contains( $url, '?' ) ) { |
| 471 | $q = ''; |
| 472 | } else { |
| 473 | list( $url, $q ) = explode( '?', $url, 2 ); |
| 474 | if ( strlen( $q ) ) { |
| 475 | $q = '&' . $q; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | return "$url?m={$mtime}{$q}"; |
| 480 | } |
| 481 | |
| 482 | |
| 483 | |
| 484 | |
| 485 | |
| 486 | function jetpack_boost_get_static_prefix() { |
| 487 | $prefix = defined( 'JETPACK_BOOST_STATIC_PREFIX' ) ? JETPACK_BOOST_STATIC_PREFIX : '/_jb_static/'; |
| 488 | |
| 489 | if ( ! str_starts_with( $prefix, '/' ) ) { |
| 490 | $prefix = '/' . $prefix; |
| 491 | } |
| 492 | |
| 493 | return trailingslashit( $prefix ); |
| 494 | } |
| 495 | |
| 496 | function jetpack_boost_get_minify_url( $file_name = '' ) { |
| 497 | return content_url( '/boost-cache/static/' . $file_name ); |
| 498 | } |
| 499 | |
| 500 | function jetpack_boost_get_minify_file_path( $file_name = '' ) { |
| 501 | return WP_CONTENT_DIR . '/boost-cache/static/' . $file_name; |
| 502 | } |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | function jetpack_boost_minify_serve_concatenated() { |
| 510 | |
| 511 | if ( isset( $_SERVER['REQUEST_URI'] ) ) { |
| 512 | |
| 513 | $request_path = explode( '?', wp_unslash( $_SERVER['REQUEST_URI'] ) )[0]; |
| 514 | $prefix = jetpack_boost_get_static_prefix(); |
| 515 | if ( $prefix === substr( $request_path, -strlen( $prefix ), strlen( $prefix ) ) ) { |
| 516 | require_once __DIR__ . '/functions-service-fallback.php'; |
| 517 | jetpack_boost_page_optimize_service_request(); |
| 518 | exit( 0 ); |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | function jetpack_boost_minify_activation() { |
| 531 | |
| 532 | jetpack_boost_page_optimize_schedule_cache_cleanup(); |
| 533 | |
| 534 | Cleanup_Stored_Paths::setup_schedule(); |
| 535 | |
| 536 | |
| 537 | jetpack_boost_404_setup(); |
| 538 | } |
| 539 | |
| 540 | function jetpack_boost_minify_is_enabled() { |
| 541 | $minify_css = new Module( new Minify_CSS() ); |
| 542 | $minify_js = new Module( new Minify_JS() ); |
| 543 | |
| 544 | return $minify_css->is_enabled() || $minify_js->is_enabled(); |
| 545 | } |
| 546 | |
| 547 | |
| 548 | |
| 549 | |
| 550 | |
| 551 | |
| 552 | function jetpack_boost_minify_init() { |
| 553 | add_action( 'jetpack_boost_minify_cron_cache_cleanup', 'jetpack_boost_minify_cron_cache_cleanup' ); |
| 554 | Cleanup_Stored_Paths::add_cleanup_actions(); |
| 555 | |
| 556 | if ( jetpack_boost_page_optimize_bail() ) { |
| 557 | return; |
| 558 | } |
| 559 | |
| 560 | |
| 561 | add_filter( 'jetpack_force_disable_site_accelerator', '__return_true' ); |
| 562 | } |
| 563 | |
| 564 | function jetpack_boost_page_optimize_generate_concat_path( $url_paths, $dependency_path_mapping ) { |
| 565 | $fs_paths = array(); |
| 566 | foreach ( $url_paths as $url_path ) { |
| 567 | $fs_paths[] = $dependency_path_mapping->uri_path_to_fs_path( $url_path ); |
| 568 | } |
| 569 | |
| 570 | $mtime = max( array_map( 'filemtime', $fs_paths ) ); |
| 571 | if ( jetpack_boost_page_optimize_use_concat_base_dir() ) { |
| 572 | $paths = array_map( 'jetpack_boost_page_optimize_remove_concat_base_prefix', $fs_paths ); |
| 573 | } else { |
| 574 | $paths = $url_paths; |
| 575 | } |
| 576 | |
| 577 | $file_paths = new File_Paths(); |
| 578 | $file_paths->set( $paths, $mtime, jetpack_boost_minify_cache_buster() ); |
| 579 | $file_paths->store(); |
| 580 | |
| 581 | return $file_paths->get_cache_id(); |
| 582 | } |