Skip to content
Permalink
Browse files

404 handler, default off for now

  • Loading branch information
futtta committed Nov 30, 2019
1 parent 46b1200 commit 2e9d41d0d5bee9dd8069d86f3b1e269f799a5d50
Showing with 78 additions and 0 deletions.
  1. +47 −0 classes/autoptimizeCache.php
  2. +31 −0 config/autoptimize_404_handler.php
@@ -109,6 +109,16 @@ public function cache( $data, $mime )
} else {
// Write code to cache without doing anything else.
file_put_contents( $this->cachedir . $this->filename, $data );

// save fallback .js or .css file if filter true (to be false by default) but not if snippet or single.
if ( self::do_fallback() && strpos( $this->filename, '_snippet_' ) === false && strpos( $this->filename, '_single_' ) === false ) {
$_extension = pathinfo( $this->filename, PATHINFO_EXTENSION );
$_fallback_file = AUTOPTIMIZE_CACHEFILE_PREFIX . 'fallback.' . $_extension;
if ( ! file_exists( $this->cachedir . $_extension . '/' . $_fallback_file ) ) {
file_put_contents( $this->cachedir . $_extension . '/' . $_fallback_file, $data );
}
}

if ( apply_filters( 'autoptimize_filter_cache_create_static_gzip', false ) ) {
// Create an additional cached gzip file.
file_put_contents( $this->cachedir . $this->filename . '.gz', gzencode( $data, 9, FORCE_GZIP ) );
@@ -571,13 +581,50 @@ public static function cacheavail()
</Files>
</IfModule>';
}

if ( self::do_fallback() ) {
$content .= "\nErrorDocument 404 " . trailingslashit( parse_url( content_url(), PHP_URL_PATH ) ) . 'autoptimize_404_handler.php';
}
@file_put_contents( $htaccess, $content ); // @codingStandardsIgnoreLine
}

if ( self::do_fallback() ) {
self::check_fallback_php();
}

// All OK!
return true;
}

/**
* Checks if fallback-php file exists and create it if not.
*
* Return bool
*/
public static function check_fallback_php() {
$_fallback_filename = 'autoptimize_404_handler.php';
$_fallback_php = trailingslashit( WP_CONTENT_DIR ) . $_fallback_filename;
$_fallback_status = true;

if ( ! file_exists( $_fallback_php ) ) {
$_fallback_php_contents = file_get_contents( AUTOPTIMIZE_PLUGIN_DIR . 'config/' . $_fallback_filename );
$_fallback_php_contents = str_replace( '<?php exit;', '<?php', $_fallback_php_contents );
$_fallback_status = file_put_contents( $_fallback_php, $_fallback_php_contents );
}

return $_fallback_status;
}

/**
* Tells if AO should try to avoid 404's by creating fallback filesize
* and create a php 404 handler and tell .htaccess to redirect to said handler.
*
* Return bool
*/
public static function do_fallback() {
return apply_filters( 'autoptimize_filter_cache_do_fallback', false );
}

/**
* Checks if cache dirs exist and create if not.
* Returns false if not succesful.
@@ -0,0 +1,31 @@
<?php exit;
/*
* Autoptimize's magic 404 handler.
*
* Configure your webserver to have requests for files that are no longer in
* /wp-content/cache/autoptimize/ to redirect to this file. AO's .htaccess file
* will have a "Errordocument:" directive to automatically do this.
*
* This file has simple logic to redirect to the "fallback" files that are
* created automatically by AO to avoid visitors seeing broken pages or
* Googlebot getting utterly confused.
*
* Warning: the fallback files might not apply to all pages, so this is a just
* a temporary solution, you really should clear any page cache to avoid requests
* to files that don't exist in AO's cache.
*
*/

$original_request = $_SERVER[REQUEST_URI];
$fallback_target = preg_replace( '/(.*)_(?:[a-z0-9]{32})\.(js|css)$/', '${1}_fallback.${2}', $original_request );

if ( $original_request !== $fallback_target ) {
error_log( 'Autoptimize file ' . $original_request . ' not found, using fallback instead.' );
header( 'HTTP/1.1 301 Moved Permanently' );
header( 'Location: ' . $fallback_target );
} else {
error_log( 'Autoptimize file ' . $original_request . ' not found, sending 410 gone response.' );
header( 'HTTP/1.1 410 Gone' );
}

exit();

0 comments on commit 2e9d41d

Please sign in to comment.
You can’t perform that action at this time.