wp_cache_serve_cache_file() │ WPSCache 1.0
No Hooks.
Returns
null. Nothing (null).
Usage
wp_cache_serve_cache_file();
wp_cache_serve_cache_file() wp cache serve cache file code WPSCache 3.1.0
function wp_cache_serve_cache_file() {
global $key, $blogcacheid, $wp_cache_request_uri, $file_prefix, $blog_cache_dir, $meta_file, $cache_file, $cache_filename, $meta_pathname, $wp_cache_gzip_encoding, $meta;
global $cache_compression, $wp_cache_slash_check, $wp_supercache_304, $wp_cache_home_path, $wp_cache_no_cache_for_get;
global $wp_cache_disable_utf8, $wp_cache_mfunc_enabled, $wpsc_served_header;
if ( wpsc_is_backend() ) {
wp_cache_debug( 'Not serving wp-admin requests.', 5 );
return false;
}
if ( $wp_cache_no_cache_for_get && wpsc_is_get_query() ) {
wp_cache_debug( 'Non empty GET request. Caching disabled on settings page. ' . wpsc_dump_get_request(), 1 );
return false;
}
if ( defined( 'WPSC_SERVE_DISABLED' ) ) {
wp_cache_debug( 'wp_cache_serve_cache_file: WPSC_SERVE_DISABLED defined. Not serving cached files.' );
return false;
}
if ( wpsc_get_accept_header() !== 'text/html' ) {
wp_cache_debug( 'wp_cache_serve_cache_file: visitor does not accept text/html. Not serving cached file.' );
return false;
}
extract( wp_super_cache_init() ); // $key, $cache_filename, $meta_file, $cache_file, $meta_pathname
// Look for wp-cache file + meta file for the current URL
// If we can't find them, we will look for supercache html files. These files don't have any meta data
// which is why the code below does more work setting up the headers, etc.
if (
! defined( 'WPSC_SUPERCACHE_ONLY' ) &&
(
( $cache_file && file_exists( $cache_file ) ) ||
file_exists( get_current_url_supercache_dir() . 'meta-' . $cache_filename )
)
) {
if ( file_exists( get_current_url_supercache_dir() . 'meta-' . $cache_filename ) ) {
$cache_file = get_current_url_supercache_dir() . $cache_filename;
$meta_pathname = get_current_url_supercache_dir() . 'meta-' . $cache_filename;
} elseif ( ! file_exists( $cache_file ) ) {
wp_cache_debug( 'wp_cache_serve_cache_file: found cache file but then it disappeared!' );
return false;
}
if ( ! $meta_pathname ) {
wp_cache_debug( 'wp_cache_serve_cache_file: meta pathname is empty. Could not load wp-cache meta file.' );
return true;
}
wp_cache_debug( "wp-cache file exists: $cache_file", 5 );
if ( ! ( $meta = json_decode( wp_cache_get_legacy_cache( $meta_pathname ), true ) ) ) {
wp_cache_debug( "couldn't load wp-cache meta file", 5 );
return true;
}
if ( is_array( $meta ) == false ) {
wp_cache_debug( "meta array corrupt, deleting $meta_pathname and $cache_file", 1 );
@unlink( $meta_pathname );
@unlink( $cache_file );
return true;
}
} else { // no wp-cache file, look for a supercache file
global $wpsc_save_headers;
global $cache_max_time;
// last chance, check if a supercache file exists. Just in case .htaccess rules don't work on this host
$file = get_current_url_supercache_dir() . supercache_filename();
if ( false == file_exists( $file ) ) {
wp_cache_debug( "No Super Cache file found for current URL: $file" );
return false;
} elseif ( wpsc_is_get_query() ) {
wp_cache_debug( 'GET array not empty. Cannot serve a supercache file. ' . wpsc_dump_get_request() );
return false;
} elseif ( wp_cache_get_cookies_values() != '' ) {
wp_cache_debug( 'Cookies found. Cannot serve a supercache file. ' . wp_cache_get_cookies_values() );
return false;
} elseif ( wpsc_get_accept_header() !== 'text/html' ) {
wp_cache_debug( 'Accept header is not text/html. Cannot serve supercache file.' . wp_cache_get_cookies_values() );
return false;
} elseif ( isset( $wpsc_save_headers ) && $wpsc_save_headers ) {
wp_cache_debug( 'Saving headers. Cannot serve a supercache file.' );
return false;
} elseif ( $cache_max_time > 0 && ( filemtime( $file ) + $cache_max_time ) < time() ) {
wp_cache_debug( sprintf( 'Cache has expired and is older than %d seconds old.', $cache_max_time ) );
return false;
}
if ( isset( $wp_cache_mfunc_enabled ) == false ) {
$wp_cache_mfunc_enabled = 0;
}
if ( false == isset( $wp_cache_home_path ) ) {
$wp_cache_home_path = '/';
}
// make sure ending slashes are ok
if ( $wp_cache_request_uri == $wp_cache_home_path || ( $wp_cache_slash_check && substr( $wp_cache_request_uri, -1 ) == '/' ) || ( $wp_cache_slash_check == 0 && substr( $wp_cache_request_uri, -1 ) != '/' ) ) {
if ( $wp_cache_mfunc_enabled == 0 ) {
// get data from file
if ( $wp_cache_gzip_encoding ) {
if ( file_exists( $file . '.gz' ) ) {
$cachefiledata = file_get_contents( $file . '.gz' );
if ( false === $cachefiledata ) {
wp_cache_debug( 'The cached gzip file could not be read. Must generate a new one.' );
return false;
}
wp_cache_debug( "Fetched gzip static page data from supercache file using PHP. File: $file.gz" );
} else {
$cachefiledata = file_get_contents( $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
if ( false === $cachefiledata ) {
wp_cache_debug( 'The cached file could not be read. Must generate a new one.' );
return false;
}
$cachefiledata = gzencode( $cachefiledata, 6, FORCE_GZIP );
wp_cache_debug( "Fetched static page data from supercache file using PHP and gzipped it. File: $file" );
}
} else {
$cachefiledata = file_get_contents( $file );
if ( false === $cachefiledata ) {
wp_cache_debug( 'The cached file could not be read. Must generate a new one.' );
return false;
}
wp_cache_debug( "Fetched static page data from supercache file using PHP. File: $file" );
}
} else {
// get dynamic data from filtered file
$cachefiledata = file_get_contents( $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
if ( false === $cachefiledata ) {
wp_cache_debug( 'The cached file could not be read. Must generate a new one.' );
return false;
}
$cachefiledata = do_cacheaction( 'wpsc_cachedata', $cachefiledata );
if ( $wp_cache_gzip_encoding ) {
$cachefiledata = gzencode( $cachefiledata, 6, FORCE_GZIP );
wp_cache_debug( "Fetched dynamic page data from supercache file using PHP and gzipped it. File: $file" );
} else {
wp_cache_debug( "Fetched dynamic page data from supercache file using PHP. File: $file" );
}
}
if ( isset( $wp_cache_disable_utf8 ) == false || $wp_cache_disable_utf8 == 0 ) {
header( 'Content-type: text/html; charset=UTF-8' );
}
if ( defined( 'WPSC_VARY_HEADER' ) ) {
if ( WPSC_VARY_HEADER != '' ) {
header( 'Vary: ' . WPSC_VARY_HEADER );
}
} else {
header( 'Vary: Accept-Encoding, Cookie' );
}
if ( defined( 'WPSC_CACHE_CONTROL_HEADER' ) ) {
if ( WPSC_CACHE_CONTROL_HEADER != '' ) {
header( 'Cache-Control: ' . WPSC_CACHE_CONTROL_HEADER );
}
} else {
header( 'Cache-Control: max-age=3, must-revalidate' );
}
$size = ( function_exists( 'mb_strlen' ) && function_exists( 'is_utf8_charset' ) ) ? mb_strlen( $cachefiledata, '8bit' ) : strlen( $cachefiledata );
if ( $wp_cache_gzip_encoding ) {
if ( isset( $wpsc_served_header ) && $wpsc_served_header ) {
header( 'X-WP-Super-Cache: Served supercache gzip file from PHP' );
}
header( 'Content-Encoding: ' . $wp_cache_gzip_encoding );
header( 'Content-Length: ' . $size );
} elseif ( $wp_supercache_304 ) {
if ( isset( $wpsc_served_header ) && $wpsc_served_header ) {
header( 'X-WP-Super-Cache: Served supercache 304 file from PHP' );
}
header( 'Content-Length: ' . $size );
} elseif ( isset( $wpsc_served_header ) && $wpsc_served_header ) {
header( 'X-WP-Super-Cache: Served supercache file from PHP' );
}
// don't try to match modified dates if using dynamic code.
if ( $wp_cache_mfunc_enabled == 0 && $wp_supercache_304 ) {
wp_cache_debug( 'wp_cache_serve_cache_file: checking age of cached vs served files.' );
$headers = wpsc_apache_request_headers();
$remote_mod_time = isset( $headers['If-Modified-Since'] ) ? $headers['If-Modified-Since'] : null;
if ( $remote_mod_time === null && isset( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
$remote_mod_time = $_SERVER['HTTP_IF_MODIFIED_SINCE'];
}
$local_mod_time = gmdate( 'D, d M Y H:i:s', filemtime( $file ) ) . ' GMT';
if ( $remote_mod_time !== null && $remote_mod_time == $local_mod_time ) {
wp_cache_debug( 'wp_cache_serve_cache_file: Send 304 Not Modified header.' );
header( $_SERVER['SERVER_PROTOCOL'] . ' 304 Not Modified' );
exit( 0 );
} else {
wp_cache_debug( 'wp_cache_serve_cache_file: 304 browser caching not possible as timestamps differ.' );
}
header( 'Last-Modified: ' . $local_mod_time );
}
echo $cachefiledata;
exit( 0 );
} else {
wp_cache_debug( 'No wp-cache file exists. Must generate a new one.' );
return false;
}
}
$cache_file = do_cacheaction( 'wp_cache_served_cache_file', $cache_file );
// Sometimes the gzip headers are lost. Make sure html returned isn't compressed!
$do_not_serve_gzip_data = true;
if ( $cache_compression && $wp_cache_gzip_encoding ) {
if ( ! in_array( 'Content-Encoding: ' . $wp_cache_gzip_encoding, $meta['headers'], true ) ) {
wp_cache_debug( 'GZIP headers not found. Force uncompressed output.' );
} else {
$do_not_serve_gzip_data = false;
if ( isset( $meta['dynamic'] ) ) {
unset( $meta['headers']['Content-Length'] ); // this is set later after the output data is compressed
}
wp_cache_debug( 'GZIP headers found. Serving compressed output.' );
}
}
foreach ( $meta['headers'] as $t => $header ) {
// godaddy fix, via http://blog.gneu.org/2008/05/wp-supercache-on-godaddy/ and http://www.littleredrails.com/blog/2007/09/08/using-wp-cache-on-godaddy-500-error/
if ( strpos( $header, 'Last-Modified:' ) === false ) {
header( $header );
wp_cache_debug( 'Sending Header: ' . $header );
}
}
if ( isset( $wpsc_served_header ) && $wpsc_served_header ) {
header( 'X-WP-Super-Cache: Served WPCache cache file' );
}
if ( isset( $meta['dynamic'] ) ) {
wp_cache_debug( 'Serving wp-cache dynamic file', 5 );
if ( $do_not_serve_gzip_data ) {
// attempt to uncompress the cached file just in case it's gzipped
$cache = wp_cache_get_legacy_cache( $cache_file );
$uncompressed = @gzuncompress( $cache );
if ( $uncompressed ) {
wp_cache_debug( 'Uncompressed gzipped cache from wp-cache: ' . $cache_file );
$cache = $uncompressed;
unset( $uncompressed );
}
$cache = do_cacheaction( 'wpsc_cachedata', $cache );
} else {
wp_cache_debug( 'Compressed cache data from wp-cache: ' . $cache_file );
$cache = gzencode(
do_cacheaction(
'wpsc_cachedata',
wp_cache_get_legacy_cache( $cache_file )
),
6,
FORCE_GZIP
);
$size = ( function_exists( 'mb_strlen' ) && function_exists( 'is_utf8_charset' ) ) ? mb_strlen( $cache, '8bit' ) : strlen( $cache );
wp_cache_debug( 'Sending Header: Content-Length: ' . $size );
header( 'Content-Length: ' . $size );
}
} elseif ( $do_not_serve_gzip_data ) {
$cache = wp_cache_get_legacy_cache( $cache_file );
$uncompressed = @gzuncompress( $cache ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- there is a small chance the cache isn't gzipped despite being configured to be.
if ( $uncompressed ) {
$cache = $uncompressed;
unset( $uncompressed );
wp_cache_debug( 'Uncompressed gzipped cache data from wp-cache file: ' . $cache_file );
} else {
wp_cache_debug( 'Sending already uncompressed cache file from wp-cache to browser: ' . $cache_file );
}
} else {
wp_cache_debug( 'Sending wp-cache file to browser: ' . $cache_file );
$cache = wp_cache_get_legacy_cache( $cache_file );
}
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- this is the cached version of the current page. It will have been escaped already.
echo $cache;
wp_cache_debug( 'exit request', 5 );
die( 0 );
}