| 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 | return 1; |
| 17 | } |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | function jetpack_boost_minify_concat_max_files() { |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | return apply_filters( 'jetpack_boost_minify_concat_max_files', 150 ); |
| 30 | } |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | function jetpack_boost_should_run_daily_network_cron_job( $hook ) { |
| 36 | |
| 37 | if ( get_site_option( 'jetpack_boost_' . $hook . '_last_run', 0 ) > time() - DAY_IN_SECONDS ) { |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | update_site_option( 'jetpack_boost_' . $hook . '_last_run', time() ); |
| 42 | |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | function jetpack_boost_minify_cron_cache_cleanup() { |
| 50 | if ( ! jetpack_boost_should_run_daily_network_cron_job( 'minify_cron_cache_cleanup' ) ) { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | jetpack_boost_legacy_minify_cache_cleanup(); |
| 55 | jetpack_boost_minify_cache_cleanup(); |
| 56 | } |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | function jetpack_boost_legacy_minify_cache_cleanup( $file_age = DAY_IN_SECONDS ) { |
| 64 | |
| 65 | |
| 66 | $file_age = is_int( $file_age ) ? $file_age : DAY_IN_SECONDS; |
| 67 | |
| 68 | $cache_folder = Config::get_legacy_cache_dir_path(); |
| 69 | |
| 70 | if ( ! is_dir( $cache_folder ) ) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | $cache_files = glob( $cache_folder . '/page-optimize-cache-*' ); |
| 75 | |
| 76 | jetpack_boost_delete_expired_files( $cache_files, $file_age ); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | function jetpack_boost_minify_cache_cleanup( $file_age = DAY_IN_SECONDS ) { |
| 85 | |
| 86 | $file_age = is_int( $file_age ) ? $file_age : DAY_IN_SECONDS; |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | if ( $file_age !== 0 ) { |
| 93 | |
| 94 | jetpack_boost_minify_remove_stale_static_files(); |
| 95 | } |
| 96 | |
| 97 | $cache_files = glob( Config::get_static_cache_dir_path() . '/*.min.*' ); |
| 98 | |
| 99 | jetpack_boost_delete_expired_files( $cache_files, $file_age ); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | function jetpack_boost_delete_expired_files( $files, $file_age ) { |
| 111 | foreach ( $files as $file ) { |
| 112 | if ( ! is_file( $file ) ) { |
| 113 | continue; |
| 114 | } |
| 115 | |
| 116 | if ( $file_age === 0 ) { |
| 117 | wp_delete_file( $file ); |
| 118 | continue; |
| 119 | } |
| 120 | |
| 121 | |
| 122 | |
| 123 | if ( ( time() - $file_age ) > fileatime( $file ) ) { |
| 124 | wp_delete_file( $file ); |
| 125 | } elseif ( ( time() - ( 2 * $file_age ) ) > filemtime( $file ) ) { |
| 126 | wp_delete_file( $file ); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | function jetpack_boost_minify_clear_scheduled_events() { |
| 137 | wp_unschedule_hook( 'jetpack_boost_minify_cron_cache_cleanup' ); |
| 138 | wp_unschedule_hook( 'jetpack_boost_404_tester_cron' ); |
| 139 | Cleanup_Stored_Paths::clear_schedules(); |
| 140 | } |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | function jetpack_boost_page_optimize_deactivate() { |
| 146 | |
| 147 | jetpack_boost_legacy_minify_cache_cleanup( 0 ); |
| 148 | jetpack_boost_minify_cache_cleanup( 0 ); |
| 149 | |
| 150 | jetpack_boost_minify_clear_scheduled_events(); |
| 151 | } |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | function jetpack_boost_enqueued_to_absolute_url( $url ) { |
| 163 | if ( str_starts_with( $url, '/' ) ) { |
| 164 | return home_url( $url ); |
| 165 | } |
| 166 | |
| 167 | return $url; |
| 168 | } |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | function jetpack_boost_page_optimize_js_exclude_list() { |
| 174 | return jetpack_boost_ds_get( 'minify_js_excludes' ); |
| 175 | } |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | function jetpack_boost_page_optimize_css_exclude_list() { |
| 181 | return jetpack_boost_ds_get( 'minify_css_excludes' ); |
| 182 | } |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | function jetpack_boost_page_optimize_starts_with( $prefix, $str ) { |
| 188 | $prefix_length = strlen( $prefix ); |
| 189 | if ( strlen( $str ) < $prefix_length ) { |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | return substr( $str, 0, $prefix_length ) === $prefix; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | function jetpack_boost_page_optimize_use_concat_base_dir() { |
| 202 | return defined( 'PAGE_OPTIMIZE_CONCAT_BASE_DIR' ) && file_exists( PAGE_OPTIMIZE_CONCAT_BASE_DIR ); |
| 203 | } |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | function jetpack_boost_page_optimize_remove_concat_base_prefix( $original_fs_path ) { |
| 211 | $abspath = Config::get_abspath(); |
| 212 | |
| 213 | |
| 214 | if ( strlen( $abspath ) > strlen( PAGE_OPTIMIZE_CONCAT_BASE_DIR ) ) { |
| 215 | $longer_path = $abspath; |
| 216 | $shorter_path = PAGE_OPTIMIZE_CONCAT_BASE_DIR; |
| 217 | } else { |
| 218 | $longer_path = PAGE_OPTIMIZE_CONCAT_BASE_DIR; |
| 219 | $shorter_path = $abspath; |
| 220 | } |
| 221 | |
| 222 | $prefix_abspath = trailingslashit( $longer_path ); |
| 223 | if ( jetpack_boost_page_optimize_starts_with( $prefix_abspath, $original_fs_path ) ) { |
| 224 | return substr( $original_fs_path, strlen( $prefix_abspath ) ); |
| 225 | } |
| 226 | |
| 227 | $prefix_basedir = trailingslashit( $shorter_path ); |
| 228 | if ( jetpack_boost_page_optimize_starts_with( $prefix_basedir, $original_fs_path ) ) { |
| 229 | return substr( $original_fs_path, strlen( $prefix_basedir ) ); |
| 230 | } |
| 231 | |
| 232 | |
| 233 | return '/page-optimize-resource-outside-base-path/' . basename( $original_fs_path ); |
| 234 | } |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | function jetpack_boost_page_optimize_schedule_404_tester() { |
| 240 | if ( false === wp_next_scheduled( 'jetpack_boost_404_tester_cron' ) ) { |
| 241 | wp_schedule_event( time() + DAY_IN_SECONDS, 'daily', 'jetpack_boost_404_tester_cron' ); |
| 242 | |
| 243 | |
| 244 | jetpack_boost_404_tester(); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | function jetpack_boost_page_optimize_schedule_cache_cleanup() { |
| 252 | |
| 253 | if ( false === wp_next_scheduled( 'jetpack_boost_minify_cron_cache_cleanup' ) ) { |
| 254 | wp_schedule_event( time(), 'daily', 'jetpack_boost_minify_cron_cache_cleanup' ); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | function jetpack_boost_page_optimize_bail() { |
| 265 | static $should_bail = null; |
| 266 | if ( null !== $should_bail ) { |
| 267 | return $should_bail; |
| 268 | } |
| 269 | |
| 270 | $should_bail = false; |
| 271 | |
| 272 | |
| 273 | if ( is_admin() ) { |
| 274 | $should_bail = true; |
| 275 | return true; |
| 276 | } |
| 277 | |
| 278 | |
| 279 | global $wp_customize; |
| 280 | if ( isset( $wp_customize ) ) { |
| 281 | $should_bail = true; |
| 282 | return true; |
| 283 | } |
| 284 | |
| 285 | |
| 286 | |
| 287 | if ( ! empty( $_GET['et_fb'] ) && 'Divi' === wp_get_theme()->get_template() ) { |
| 288 | $should_bail = true; |
| 289 | return true; |
| 290 | } |
| 291 | |
| 292 | |
| 293 | |
| 294 | if ( class_exists( 'Brizy_Editor' ) && method_exists( 'Brizy_Editor', 'prefix' ) && ( isset( $_GET[ Brizy_Editor::prefix( '-edit-iframe' ) ] ) || isset( $_GET[ Brizy_Editor::prefix( '-edit' ) ] ) ) ) { |
| 295 | $should_bail = true; |
| 296 | return true; |
| 297 | } |
| 298 | |
| 299 | |
| 300 | |
| 301 | if ( isset( $_GET['elementor-preview'] ) ) { |
| 302 | $should_bail = true; |
| 303 | return true; |
| 304 | } |
| 305 | |
| 306 | return $should_bail; |
| 307 | } |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | function jetpack_boost_page_optimize_cache_bust_mtime( $path, $siteurl ) { |
| 313 | static $dependency_path_mapping; |
| 314 | |
| 315 | |
| 316 | if ( str_starts_with( $path, '/' ) ) { |
| 317 | $parts = wp_parse_url( $siteurl ); |
| 318 | $siteurl = $parts['scheme'] . '://' . $parts['host']; |
| 319 | } |
| 320 | |
| 321 | $url = $siteurl . $path; |
| 322 | |
| 323 | if ( str_contains( $url, '?m=' ) ) { |
| 324 | return $url; |
| 325 | } |
| 326 | |
| 327 | $parts = wp_parse_url( $url ); |
| 328 | if ( ! isset( $parts['path'] ) || empty( $parts['path'] ) ) { |
| 329 | return $url; |
| 330 | } |
| 331 | |
| 332 | if ( empty( $dependency_path_mapping ) ) { |
| 333 | $dependency_path_mapping = new Dependency_Path_Mapping(); |
| 334 | } |
| 335 | |
| 336 | $file = $dependency_path_mapping->dependency_src_to_fs_path( $url ); |
| 337 | |
| 338 | $mtime = false; |
| 339 | if ( file_exists( $file ) ) { |
| 340 | $mtime = filemtime( $file ); |
| 341 | } |
| 342 | |
| 343 | if ( ! $mtime ) { |
| 344 | return $url; |
| 345 | } |
| 346 | |
| 347 | if ( ! str_contains( $url, '?' ) ) { |
| 348 | $q = ''; |
| 349 | } else { |
| 350 | list( $url, $q ) = explode( '?', $url, 2 ); |
| 351 | if ( strlen( $q ) ) { |
| 352 | $q = '&' . $q; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | return "$url?m={$mtime}{$q}"; |
| 357 | } |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | function jetpack_boost_get_static_prefix() { |
| 364 | $prefix = defined( 'JETPACK_BOOST_STATIC_PREFIX' ) ? JETPACK_BOOST_STATIC_PREFIX : '/_jb_static/'; |
| 365 | |
| 366 | if ( ! str_starts_with( $prefix, '/' ) ) { |
| 367 | $prefix = '/' . $prefix; |
| 368 | } |
| 369 | |
| 370 | return trailingslashit( $prefix ); |
| 371 | } |
| 372 | |
| 373 | function jetpack_boost_get_minify_url( $file_name = '' ) { |
| 374 | return content_url( '/boost-cache/static/' . $file_name ); |
| 375 | } |
| 376 | |
| 377 | function jetpack_boost_get_minify_file_path( $file_name = '' ) { |
| 378 | return WP_CONTENT_DIR . '/boost-cache/static/' . $file_name; |
| 379 | } |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | function jetpack_boost_minify_serve_concatenated() { |
| 387 | |
| 388 | |
| 389 | if ( isset( $_SERVER['REQUEST_URI'] ) ) { |
| 390 | |
| 391 | $request_path = explode( '?', wp_unslash( $_SERVER['REQUEST_URI'] ) )[0]; |
| 392 | $prefix = jetpack_boost_get_static_prefix(); |
| 393 | if ( $prefix === substr( $request_path, -strlen( $prefix ), strlen( $prefix ) ) ) { |
| 394 | require_once __DIR__ . '/functions-service-fallback.php'; |
| 395 | jetpack_boost_page_optimize_service_request(); |
| 396 | exit( 0 ); |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | function jetpack_boost_minify_activation() { |
| 409 | |
| 410 | jetpack_boost_page_optimize_schedule_cache_cleanup(); |
| 411 | |
| 412 | Cleanup_Stored_Paths::setup_schedule(); |
| 413 | |
| 414 | |
| 415 | jetpack_boost_404_setup(); |
| 416 | } |
| 417 | |
| 418 | function jetpack_boost_minify_is_enabled() { |
| 419 | $minify_css = new Module( new Minify_CSS() ); |
| 420 | $minify_js = new Module( new Minify_JS() ); |
| 421 | |
| 422 | return $minify_css->is_enabled() || $minify_js->is_enabled(); |
| 423 | } |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | function jetpack_boost_minify_init() { |
| 431 | add_action( 'jetpack_boost_minify_cron_cache_cleanup', 'jetpack_boost_minify_cron_cache_cleanup' ); |
| 432 | Cleanup_Stored_Paths::add_cleanup_actions(); |
| 433 | |
| 434 | if ( jetpack_boost_page_optimize_bail() ) { |
| 435 | return; |
| 436 | } |
| 437 | |
| 438 | |
| 439 | add_filter( 'jetpack_force_disable_site_accelerator', '__return_true' ); |
| 440 | } |
| 441 | |
| 442 | function jetpack_boost_page_optimize_generate_concat_path( $url_paths, $dependency_path_mapping ) { |
| 443 | $fs_paths = array(); |
| 444 | foreach ( $url_paths as $url_path ) { |
| 445 | $fs_paths[] = $dependency_path_mapping->uri_path_to_fs_path( $url_path ); |
| 446 | } |
| 447 | |
| 448 | $mtime = max( array_map( 'filemtime', $fs_paths ) ); |
| 449 | if ( jetpack_boost_page_optimize_use_concat_base_dir() ) { |
| 450 | $paths = array_map( 'jetpack_boost_page_optimize_remove_concat_base_prefix', $fs_paths ); |
| 451 | } else { |
| 452 | $paths = $url_paths; |
| 453 | } |
| 454 | |
| 455 | $file_paths = new File_Paths(); |
| 456 | $file_paths->set( $paths, $mtime, jetpack_boost_minify_cache_buster() ); |
| 457 | $file_paths->store(); |
| 458 | |
| 459 | return $file_paths->get_cache_id(); |
| 460 | } |