PluginProbe
Autoptimize / 3.1.7
Autoptimize v3.1.7
2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 All 107 releases
autoptimize / config / autoptimize_404_handler.php

autoptimize_404_handler.php in Autoptimize 3.1.7, at config/autoptimize_404_handler.php

53 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php exit;
2 /**
3 * Autoptimize's magic 404 handler.
4 *
5 * Configure your webserver to have requests for files that are no longer in
6 * /wp-content/cache/autoptimize/ to redirect to this file. AO's .htaccess file
7 * will have a "Errordocument:" directive to automatically do this.
8 *
9 * This file has simple logic to redirect to the "fallback" files that are
10 * created automatically by AO to avoid visitors seeing broken pages or
11 * Googlebot getting utterly confused.
12 *
13 * Error logging is off by default (don't want to flood your php errorlog, but
14 * can be enabled by this code snippet:
15 *
16 * add_filter( 'autoptimize_filter_cache_fallback_log_errors', '__return_true' );
17 *
18 * Warning: the fallback files might not apply to all pages, so this is a just
19 * a temporary solution, you really should clear any page cache to avoid requests
20 * to files that don't exist in AO's cache.
21 */
22
23 $original_request = strtok( $_SERVER['REQUEST_URI'], '?' );
24
25 if ( strpos( $original_request, 'uucss/uucss-' ) !== false ) {
26 $original_request = preg_replace( '/uucss\/uucss-[a-z0-9]{32}-/', 'css/', $original_request );
27 }
28
29 $fallback_target = preg_replace( '/(.*)_(?:[a-z0-9]{32})\.(js|css)$/', '${1}_fallback.${2}', $original_request );
30 $ao_cache_dir = '<!--ao-cache-dir-->';
31 $js_or_css = pathinfo( $original_request, PATHINFO_EXTENSION );
32
33 // add multisite logic.
34 $multisite = false;
35 if ( true === $multisite ) {
36 preg_match( '#\/([0-9]{1,5})\/(?:js|css)\/[a-z0-9]*_fallback\.(?:js|css)$#', $fallback_target, $child_site_id );
37 $ao_root_cache_dir = preg_replace( '#[0-9]*\/$#', '', $ao_cache_dir );
38 $ao_cache_dir = $ao_root_cache_dir . $child_site_id[1] . '/';
39 }
40
41 $fallback_path = $ao_cache_dir . $js_or_css . '/<!--ao-cachefile-prefix-->fallback.' . $js_or_css;
42
43 if ( $original_request !== $fallback_target && file_exists( $fallback_path ) ) {
44 // error_log( 'Autoptimize file ' . $original_request . ' not found, using fallback instead.' );
45 header( 'HTTP/1.1 301 Moved Permanently' );
46 header( 'Location: ' . $fallback_target );
47 } else {
48 // error_log( 'Autoptimize file ' . $original_request . ' not found, sending 410 gone response.' );
49 header( 'HTTP/1.1 410 Gone' );
50 }
51
52 exit();
53