2013-03-19 10:16:59 +00:00
|
|
|
<?php
|
2016-01-15 20:18:18 +00:00
|
|
|
|
|
|
|
defined('ABSPATH') or die("Walk away.");
|
|
|
|
|
2013-03-19 10:16:59 +00:00
|
|
|
/**
|
|
|
|
* advanced cache worker of WordPress plugin WP-FFPC
|
|
|
|
*/
|
|
|
|
|
2016-01-15 20:18:18 +00:00
|
|
|
function __wp_ffpc_debug__ ( $text ) {
|
|
|
|
if ( defined('WP_FFPC__DEBUG_MODE') && WP_FFPC__DEBUG_MODE == true)
|
2016-04-22 16:27:57 +01:00
|
|
|
error_log ( 'WP_FFPC_ACache' . ': ' . $text );
|
2015-10-13 11:13:13 +01:00
|
|
|
}
|
|
|
|
|
2013-03-19 10:16:59 +00:00
|
|
|
/* check for WP cache enabled*/
|
2016-01-15 20:18:18 +00:00
|
|
|
if ( !defined('WP_CACHE') || WP_CACHE != true ) {
|
|
|
|
__wp_ffpc_debug__('WP_CACHE is not true');
|
2013-03-19 10:16:59 +00:00
|
|
|
return false;
|
2016-01-15 20:18:18 +00:00
|
|
|
}
|
2013-03-19 10:16:59 +00:00
|
|
|
|
|
|
|
/* no cache for post request (comments, plugins and so on) */
|
2016-01-15 20:18:18 +00:00
|
|
|
if ($_SERVER["REQUEST_METHOD"] == 'POST') {
|
|
|
|
__wp_ffpc_debug__('POST requests are never cached');
|
2013-03-19 10:16:59 +00:00
|
|
|
return false;
|
2016-01-15 20:18:18 +00:00
|
|
|
}
|
2013-03-19 10:16:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to avoid enabling the cache if sessions are managed
|
|
|
|
* with request parameters and a session is active
|
|
|
|
*/
|
2016-01-15 20:18:18 +00:00
|
|
|
if (defined('SID') && SID != '') {
|
|
|
|
__wp_ffpc_debug__('SID found, skipping cache');
|
2013-03-19 10:16:59 +00:00
|
|
|
return false;
|
2016-01-15 20:18:18 +00:00
|
|
|
}
|
2013-03-19 10:16:59 +00:00
|
|
|
|
2015-01-31 10:24:04 +00:00
|
|
|
/* check for config */
|
2016-01-15 20:18:18 +00:00
|
|
|
if (!isset($wp_ffpc_config)) {
|
|
|
|
__wp_ffpc_debug__('wp_ffpc_config variable not found');
|
2015-01-31 10:24:04 +00:00
|
|
|
return false;
|
2016-01-15 20:18:18 +00:00
|
|
|
}
|
2015-01-31 10:24:04 +00:00
|
|
|
|
2013-03-19 10:16:59 +00:00
|
|
|
/* request uri */
|
|
|
|
$wp_ffpc_uri = $_SERVER['REQUEST_URI'];
|
|
|
|
|
|
|
|
/* no cache for robots.txt */
|
2015-10-13 11:13:13 +01:00
|
|
|
if ( stripos($wp_ffpc_uri, 'robots.txt') ) {
|
2016-01-15 20:18:18 +00:00
|
|
|
__wp_ffpc_debug__ ( 'Skippings robots.txt hit');
|
2013-03-19 10:16:59 +00:00
|
|
|
return false;
|
2015-10-13 11:13:13 +01:00
|
|
|
}
|
2013-03-19 10:16:59 +00:00
|
|
|
|
|
|
|
/* multisite files can be too large for memcached */
|
2015-10-13 11:13:13 +01:00
|
|
|
if ( function_exists('is_multisite') && stripos($wp_ffpc_uri, '/files/') && is_multisite() ) {
|
2016-01-15 20:18:18 +00:00
|
|
|
__wp_ffpc_debug__ ( 'Skippings multisite /files/ hit');
|
2014-09-04 10:49:14 +01:00
|
|
|
return false;
|
2015-10-13 11:13:13 +01:00
|
|
|
}
|
2013-03-19 10:16:59 +00:00
|
|
|
|
|
|
|
/* check if config is network active: use network config */
|
2016-01-15 20:18:18 +00:00
|
|
|
if (!empty ( $wp_ffpc_config['network'] ) ) {
|
2013-03-19 10:16:59 +00:00
|
|
|
$wp_ffpc_config = $wp_ffpc_config['network'];
|
2016-01-15 20:18:18 +00:00
|
|
|
__wp_ffpc_debug__('using "network" level config');
|
|
|
|
}
|
2013-03-19 10:16:59 +00:00
|
|
|
/* check if config is active for site : use site config */
|
2016-01-15 20:18:18 +00:00
|
|
|
elseif ( !empty ( $wp_ffpc_config[ $_SERVER['HTTP_HOST'] ] ) ) {
|
2013-03-19 10:16:59 +00:00
|
|
|
$wp_ffpc_config = $wp_ffpc_config[ $_SERVER['HTTP_HOST'] ];
|
2016-01-15 20:18:18 +00:00
|
|
|
__wp_ffpc_debug__("using {$_SERVER['HTTP_HOST']} level config");
|
|
|
|
}
|
2013-03-19 10:16:59 +00:00
|
|
|
/* plugin config not found :( */
|
2016-01-15 20:18:18 +00:00
|
|
|
else {
|
|
|
|
__wp_ffpc_debug__("no usable config found");
|
2013-03-19 10:16:59 +00:00
|
|
|
return false;
|
2016-01-15 20:18:18 +00:00
|
|
|
}
|
2013-03-19 10:16:59 +00:00
|
|
|
|
2016-04-21 16:32:26 +01:00
|
|
|
/* no cache for WooCommerce URL patterns */
|
|
|
|
if ( isset($wp_ffpc_config['nocache_woocommerce']) && !empty($wp_ffpc_config['nocache_woocommerce']) &&
|
|
|
|
isset($wp_ffpc_config['nocache_woocommerce_url']) && trim($wp_ffpc_config['nocache_woocommerce_url']) ) {
|
|
|
|
$pattern = sprintf('#%s#', trim($wp_ffpc_config['nocache_woocommerce_url']));
|
|
|
|
if ( preg_match($pattern, $wp_ffpc_uri) ) {
|
|
|
|
__wp_ffpc_debug__ ( "Cache exception based on WooCommenrce URL regex pattern matched, skipping");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-31 10:24:04 +00:00
|
|
|
/* no cache for uri with query strings, things usually go bad that way */
|
2015-10-13 11:13:13 +01:00
|
|
|
if ( isset($wp_ffpc_config['nocache_dyn']) && !empty($wp_ffpc_config['nocache_dyn']) && stripos($wp_ffpc_uri, '?') !== false ) {
|
2016-01-15 20:18:18 +00:00
|
|
|
__wp_ffpc_debug__ ( 'Dynamic url cache is disabled ( url with "?" ), skipping');
|
2015-01-31 10:24:04 +00:00
|
|
|
return false;
|
2015-10-13 11:13:13 +01:00
|
|
|
}
|
2015-01-31 10:24:04 +00:00
|
|
|
|
2014-09-19 09:38:54 +01:00
|
|
|
/* check for cookies that will make us not cache the content, like logged in WordPress cookie */
|
2013-06-07 17:56:56 +01:00
|
|
|
if ( isset($wp_ffpc_config['nocache_cookies']) && !empty($wp_ffpc_config['nocache_cookies']) ) {
|
|
|
|
$nocache_cookies = array_map('trim',explode(",", $wp_ffpc_config['nocache_cookies'] ) );
|
2013-04-19 21:42:09 +01:00
|
|
|
|
2013-06-07 17:56:56 +01:00
|
|
|
if ( !empty( $nocache_cookies ) ) {
|
|
|
|
foreach ($_COOKIE as $n=>$v) {
|
|
|
|
/* check for any matches to user-added cookies to no-cache */
|
2013-04-19 21:42:09 +01:00
|
|
|
foreach ( $nocache_cookies as $nocache_cookie ) {
|
|
|
|
if( strpos( $n, $nocache_cookie ) === 0 ) {
|
2016-01-15 20:18:18 +00:00
|
|
|
__wp_ffpc_debug__ ( "Cookie exception matched: {$n}, skipping");
|
2013-04-19 21:42:09 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2013-03-19 10:16:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-22 11:55:25 +01:00
|
|
|
/* no cache for excluded URL patterns */
|
|
|
|
if ( isset($wp_ffpc_config['nocache_url']) && trim($wp_ffpc_config['nocache_url']) ) {
|
|
|
|
$pattern = sprintf('#%s#', trim($wp_ffpc_config['nocache_url']));
|
|
|
|
if ( preg_match($pattern, $wp_ffpc_uri) ) {
|
2016-01-15 20:18:18 +00:00
|
|
|
__wp_ffpc_debug__ ( "Cache exception based on URL regex pattern matched, skipping");
|
2014-08-22 11:55:25 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-05 09:50:42 +01:00
|
|
|
/* canonical redirect storage */
|
|
|
|
$wp_ffpc_redirect = null;
|
|
|
|
/* fires up the backend storage array with current config */
|
|
|
|
include_once ('wp-ffpc-backend.php');
|
2015-10-13 11:13:13 +01:00
|
|
|
$backend_class = 'WP_FFPC_Backend_' . $wp_ffpc_config['cache_type'];
|
|
|
|
$wp_ffpc_backend = new $backend_class ( $wp_ffpc_config );
|
|
|
|
|
|
|
|
//$wp_ffpc_backend = new WP_FFPC_Backend( $wp_ffpc_config );
|
2015-06-05 09:50:42 +01:00
|
|
|
|
2013-06-07 17:56:56 +01:00
|
|
|
/* no cache for for logged in users unless it's set
|
|
|
|
identifier cookies are listed in backend as var for easier usage
|
|
|
|
*/
|
|
|
|
if ( !isset($wp_ffpc_config['cache_loggedin']) || $wp_ffpc_config['cache_loggedin'] == 0 || empty($wp_ffpc_config['cache_loggedin']) ) {
|
|
|
|
|
|
|
|
foreach ($_COOKIE as $n=>$v) {
|
|
|
|
foreach ( $wp_ffpc_backend->cookies as $nocache_cookie ) {
|
|
|
|
if( strpos( $n, $nocache_cookie ) === 0 ) {
|
2016-01-15 20:18:18 +00:00
|
|
|
__wp_ffpc_debug__ ( "No cache for cookie: {$n}, skipping");
|
2013-06-07 17:56:56 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-28 22:27:54 +00:00
|
|
|
/* will store time of page generation */
|
2013-03-25 13:11:49 +00:00
|
|
|
$wp_ffpc_gentime = 0;
|
2013-03-19 10:16:59 +00:00
|
|
|
|
2013-03-28 22:27:54 +00:00
|
|
|
/* backend connection failed, no caching :( */
|
2015-10-13 11:13:13 +01:00
|
|
|
if ( $wp_ffpc_backend->status() === false ) {
|
2016-01-15 20:18:18 +00:00
|
|
|
__wp_ffpc_debug__ ( "Backend offline");
|
2013-03-19 10:16:59 +00:00
|
|
|
return false;
|
2015-10-13 11:13:13 +01:00
|
|
|
}
|
2013-03-19 10:16:59 +00:00
|
|
|
|
2013-03-28 22:27:54 +00:00
|
|
|
/* try to get data & meta keys for current page */
|
2013-03-31 22:22:02 +01:00
|
|
|
$wp_ffpc_keys = array ( 'meta' => $wp_ffpc_config['prefix_meta'], 'data' => $wp_ffpc_config['prefix_data'] );
|
2013-03-19 10:16:59 +00:00
|
|
|
$wp_ffpc_values = array();
|
|
|
|
|
2016-01-15 20:18:18 +00:00
|
|
|
__wp_ffpc_debug__ ( "Trying to fetch entries");
|
2015-10-13 11:13:13 +01:00
|
|
|
|
2013-03-31 22:22:02 +01:00
|
|
|
foreach ( $wp_ffpc_keys as $internal => $key ) {
|
2014-01-01 12:38:59 +00:00
|
|
|
$key = $wp_ffpc_backend->key ( $key );
|
|
|
|
$value = $wp_ffpc_backend->get ( $key );
|
2013-03-19 10:16:59 +00:00
|
|
|
|
2013-03-19 18:08:34 +00:00
|
|
|
if ( ! $value ) {
|
2016-04-22 16:27:57 +01:00
|
|
|
__wp_ffpc_debug__("No cached data found");
|
2013-03-28 22:27:54 +00:00
|
|
|
/* does not matter which is missing, we need both, if one fails, no caching */
|
2013-03-19 10:16:59 +00:00
|
|
|
wp_ffpc_start();
|
|
|
|
return;
|
|
|
|
}
|
2013-03-19 18:08:34 +00:00
|
|
|
else {
|
2013-03-28 22:27:54 +00:00
|
|
|
/* store results */
|
2013-03-31 22:22:02 +01:00
|
|
|
$wp_ffpc_values[ $internal ] = $value;
|
2016-01-15 20:18:18 +00:00
|
|
|
__wp_ffpc_debug__('Got value for ' . $internal);
|
2013-03-19 10:16:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* serve cache 404 status */
|
2013-04-09 05:32:07 +01:00
|
|
|
if ( isset( $wp_ffpc_values['meta']['status'] ) && $wp_ffpc_values['meta']['status'] == 404 ) {
|
2016-01-15 20:18:18 +00:00
|
|
|
__wp_ffpc_debug__("Serving 404");
|
2013-03-19 10:16:59 +00:00
|
|
|
header("HTTP/1.1 404 Not Found");
|
2014-09-04 10:49:14 +01:00
|
|
|
/* if I kill the page serving here, the 404 page will not be showed at all, so we do not do that
|
|
|
|
* flush();
|
|
|
|
* die();
|
|
|
|
*/
|
2013-03-19 10:16:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* server redirect cache */
|
2013-04-09 05:32:07 +01:00
|
|
|
if ( isset( $wp_ffpc_values['meta']['redirect'] ) && $wp_ffpc_values['meta']['redirect'] ) {
|
2016-01-15 20:18:18 +00:00
|
|
|
__wp_ffpc_debug__("Serving redirect to {$wp_ffpc_values['meta']['redirect']}");
|
2013-03-19 10:16:59 +00:00
|
|
|
header('Location: ' . $wp_ffpc_values['meta']['redirect'] );
|
2014-09-04 10:49:14 +01:00
|
|
|
/* cut the connection as fast as possible */
|
2013-03-19 10:16:59 +00:00
|
|
|
flush();
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* page is already cached on client side (chrome likes to do this, anyway, it's quite efficient) */
|
|
|
|
if ( array_key_exists( "HTTP_IF_MODIFIED_SINCE" , $_SERVER ) && !empty( $wp_ffpc_values['meta']['lastmodified'] ) ) {
|
|
|
|
$if_modified_since = strtotime(preg_replace('/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"]));
|
|
|
|
/* check is cache is still valid */
|
|
|
|
if ( $if_modified_since >= $wp_ffpc_values['meta']['lastmodified'] ) {
|
2016-01-15 20:18:18 +00:00
|
|
|
__wp_ffpc_debug__("Serving 304 Not Modified");
|
2013-03-19 10:16:59 +00:00
|
|
|
header("HTTP/1.0 304 Not Modified");
|
2014-09-04 10:49:14 +01:00
|
|
|
/* connection cut for faster serving */
|
2013-03-19 10:16:59 +00:00
|
|
|
flush();
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-28 22:27:54 +00:00
|
|
|
/*** SERVING CACHED PAGE ***/
|
|
|
|
|
2013-03-19 10:16:59 +00:00
|
|
|
/* if we reach this point it means data was found & correct, serve it */
|
2013-06-18 10:57:31 +01:00
|
|
|
if (!empty ( $wp_ffpc_values['meta']['mime'] ) )
|
|
|
|
header('Content-Type: ' . $wp_ffpc_values['meta']['mime']);
|
2013-03-19 10:16:59 +00:00
|
|
|
|
2015-10-23 12:40:09 +01:00
|
|
|
/* set expiry date */
|
|
|
|
if (isset($wp_ffpc_values['meta']['expire']) && !empty ( $wp_ffpc_values['meta']['expire'] ) ) {
|
|
|
|
$hash = md5 ( $wp_ffpc_uri . $wp_ffpc_values['meta']['expire'] );
|
|
|
|
|
|
|
|
switch ($wp_ffpc_values['meta']['type']) {
|
|
|
|
case 'home':
|
|
|
|
case 'feed':
|
|
|
|
$expire = $wp_ffpc_config['browsercache_home'];
|
|
|
|
break;
|
|
|
|
case 'archive':
|
|
|
|
$expire = $wp_ffpc_config['browsercache_taxonomy'];
|
|
|
|
break;
|
|
|
|
case 'single':
|
|
|
|
$expire = $wp_ffpc_config['browsercache'];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$expire = 0;
|
|
|
|
}
|
2013-03-19 10:16:59 +00:00
|
|
|
|
2015-10-23 12:40:09 +01:00
|
|
|
header('Cache-Control: public,max-age='.$expire.',s-maxage='.$expire.',must-revalidate');
|
|
|
|
header('Expires: ' . gmdate("D, d M Y H:i:s", $wp_ffpc_values['meta']['expire'] ) . " GMT");
|
|
|
|
header('ETag: '. $hash);
|
|
|
|
unset($expire, $hash);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* in case there is no expiry set, expire immediately and don't serve Etag; browser cache is disabled */
|
|
|
|
header('Expires: ' . gmdate("D, d M Y H:i:s", time() ) . " GMT");
|
|
|
|
/* if I set these, the 304 not modified will never, ever kick in, so not setting these
|
|
|
|
* leaving here as a reminder why it should not be set */
|
|
|
|
//header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0, s-maxage=0, post-check=0, pre-check=0');
|
|
|
|
//header('Pragma: no-cache');
|
|
|
|
}
|
2013-03-19 10:16:59 +00:00
|
|
|
|
|
|
|
/* if shortlinks were set */
|
2015-10-23 12:40:09 +01:00
|
|
|
if (isset($wp_ffpc_values['meta']['shortlink']) && !empty ( $wp_ffpc_values['meta']['shortlink'] ) )
|
2013-03-19 10:16:59 +00:00
|
|
|
header( 'Link:<'. $wp_ffpc_values['meta']['shortlink'] .'>; rel=shortlink' );
|
|
|
|
|
|
|
|
/* if last modifications were set (for posts & pages) */
|
2015-10-23 12:40:09 +01:00
|
|
|
if (isset($wp_ffpc_values['meta']['lastmodified']) && !empty($wp_ffpc_values['meta']['lastmodified']) )
|
2013-03-19 10:16:59 +00:00
|
|
|
header( 'Last-Modified: ' . gmdate("D, d M Y H:i:s", $wp_ffpc_values['meta']['lastmodified'] ). " GMT" );
|
|
|
|
|
|
|
|
/* pingback urls, if existx */
|
2015-10-23 12:40:09 +01:00
|
|
|
if ( isset($wp_ffpc_values['meta']['pingback']) && !empty( $wp_ffpc_values['meta']['pingback'] ) && isset($wp_ffpc_config['pingback_header']) && $wp_ffpc_config['pingback_header'] )
|
2013-03-19 10:16:59 +00:00
|
|
|
header( 'X-Pingback: ' . $wp_ffpc_values['meta']['pingback'] );
|
|
|
|
|
|
|
|
/* for debugging */
|
2015-10-23 12:40:09 +01:00
|
|
|
if ( isset($wp_ffpc_config['response_header']) && $wp_ffpc_config['response_header'] )
|
2013-04-19 21:42:09 +01:00
|
|
|
header( 'X-Cache-Engine: WP-FFPC with ' . $wp_ffpc_config['cache_type'] .' via PHP');
|
2013-03-19 10:16:59 +00:00
|
|
|
|
|
|
|
/* HTML data */
|
2015-10-23 12:40:09 +01:00
|
|
|
if ( isset($wp_ffpc_config['generate_time']) && $wp_ffpc_config['generate_time'] == '1' && stripos($wp_ffpc_values['data'], '</body>') ) {
|
2015-05-07 14:22:33 +01:00
|
|
|
$mtime = explode ( " ", microtime() );
|
|
|
|
$wp_ffpc_gentime = ( $mtime[1] + $mtime[0] ) - $wp_ffpc_gentime;
|
|
|
|
|
2015-06-05 09:50:42 +01:00
|
|
|
$insertion = "\n<!-- WP-FFPC cache output stats\n\tcache engine: ". $wp_ffpc_config['cache_type'] ."\n\tUNIX timestamp: ". time() . "\n\tdate: ". date( 'c' ) . "\n\tfrom server: ". $_SERVER['SERVER_ADDR'] . " -->\n";
|
2015-05-07 14:22:33 +01:00
|
|
|
$index = stripos( $wp_ffpc_values['data'] , '</body>' );
|
|
|
|
|
|
|
|
$wp_ffpc_values['data'] = substr_replace( $wp_ffpc_values['data'], $insertion, $index, 0);
|
|
|
|
}
|
|
|
|
|
2016-01-15 20:18:18 +00:00
|
|
|
__wp_ffpc_debug__("Serving data");
|
2015-04-29 09:40:12 +01:00
|
|
|
echo trim($wp_ffpc_values['data']);
|
2014-12-08 09:19:01 +00:00
|
|
|
|
2013-03-19 10:16:59 +00:00
|
|
|
flush();
|
|
|
|
die();
|
|
|
|
|
2013-03-28 22:27:54 +00:00
|
|
|
/*** END SERVING CACHED PAGE ***/
|
|
|
|
|
|
|
|
|
|
|
|
/*** GENERATING CACHE ENTRY ***/
|
2013-03-19 10:16:59 +00:00
|
|
|
/**
|
|
|
|
* starts caching function
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function wp_ffpc_start( ) {
|
2013-03-28 22:27:54 +00:00
|
|
|
/* set start time */
|
2013-03-25 13:11:49 +00:00
|
|
|
global $wp_ffpc_gentime;
|
|
|
|
$mtime = explode ( " ", microtime() );
|
|
|
|
$wp_ffpc_gentime = $mtime[1] + $mtime[0];
|
|
|
|
|
2013-03-19 10:16:59 +00:00
|
|
|
/* start object "colleting" and pass it the the actual storer function */
|
|
|
|
ob_start('wp_ffpc_callback');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* callback function for WordPress redirect urls
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
function wp_ffpc_redirect_callback ($redirect_url, $requested_url) {
|
|
|
|
global $wp_ffpc_redirect;
|
|
|
|
$wp_ffpc_redirect = $redirect_url;
|
|
|
|
return $redirect_url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* write cache function, called when page generation ended
|
|
|
|
*/
|
|
|
|
function wp_ffpc_callback( $buffer ) {
|
2013-03-28 22:27:54 +00:00
|
|
|
/* use global config */
|
2013-03-19 10:16:59 +00:00
|
|
|
global $wp_ffpc_config;
|
2013-03-28 22:27:54 +00:00
|
|
|
/* backend was already set up, try to use it */
|
2013-03-19 10:16:59 +00:00
|
|
|
global $wp_ffpc_backend;
|
2013-03-28 22:27:54 +00:00
|
|
|
/* check is it's a redirect */
|
2013-03-19 10:16:59 +00:00
|
|
|
global $wp_ffpc_redirect;
|
|
|
|
|
2013-03-28 22:27:54 +00:00
|
|
|
/* no is_home = error, WordPress functions are not availabe */
|
2013-03-19 10:16:59 +00:00
|
|
|
if (!function_exists('is_home'))
|
|
|
|
return $buffer;
|
|
|
|
|
2013-03-28 22:27:54 +00:00
|
|
|
/* no <body> close tag = not HTML, also no <rss>, not feed, don't cache */
|
|
|
|
if ( stripos($buffer, '</body>') === false && stripos($buffer, '</rss>') === false )
|
2013-03-19 10:16:59 +00:00
|
|
|
return $buffer;
|
|
|
|
|
|
|
|
/* reset meta to solve conflicts */
|
|
|
|
$meta = array();
|
|
|
|
|
|
|
|
/* trim unneeded whitespace from beginning / ending of buffer */
|
|
|
|
$buffer = trim( $buffer );
|
|
|
|
|
|
|
|
/* Can be a trackback or other things without a body.
|
|
|
|
We do not cache them, WP needs to get those calls. */
|
|
|
|
if (strlen($buffer) == 0)
|
|
|
|
return '';
|
|
|
|
|
2015-10-14 20:14:09 +01:00
|
|
|
if ( isset($wp_ffpc_config[ 'nocache_comment' ]) && !empty($wp_ffpc_config[ 'nocache_comment' ]) && trim($wp_ffpc_config[ 'nocache_comment' ])) {
|
|
|
|
$pattern = sprintf('#%s#', trim($wp_ffpc_config['nocache_comment']));
|
2016-01-15 20:18:18 +00:00
|
|
|
__wp_ffpc_debug__ ( sprintf("Testing comment with pattern: %s", $pattern));
|
2015-10-14 20:14:09 +01:00
|
|
|
if ( preg_match($pattern, $buffer) ) {
|
2016-01-15 20:18:18 +00:00
|
|
|
__wp_ffpc_debug__ ( "Cache exception based on content regex pattern matched, skipping");
|
2015-10-14 20:14:09 +01:00
|
|
|
return $buffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-23 12:40:09 +01:00
|
|
|
if ( is_home() || is_feed() ) {
|
|
|
|
if (is_home())
|
|
|
|
$meta['type'] = 'home';
|
|
|
|
elseif(is_feed())
|
|
|
|
$meta['type'] = 'feed';
|
|
|
|
|
|
|
|
if (isset($wp_ffpc_config['browsercache_home']) && !empty($wp_ffpc_config['browsercache_home']) && $wp_ffpc_config['browsercache_home'] > 0) {
|
|
|
|
$meta['expire'] = time() + $wp_ffpc_config['browsercache_home'];
|
|
|
|
}
|
|
|
|
|
2016-01-15 20:18:18 +00:00
|
|
|
__wp_ffpc_debug__( 'Getting latest post for for home & feed');
|
2015-10-23 12:40:09 +01:00
|
|
|
/* get newest post and set last modified accordingly */
|
|
|
|
$args = array(
|
|
|
|
'numberposts' => 1,
|
|
|
|
'orderby' => 'modified',
|
|
|
|
'order' => 'DESC',
|
|
|
|
'post_status' => 'publish',
|
|
|
|
);
|
|
|
|
|
|
|
|
$recent_post = wp_get_recent_posts( $args, OBJECT );
|
|
|
|
if ( !empty($recent_post)) {
|
|
|
|
$recent_post = array_pop($recent_post);
|
|
|
|
if (!empty ( $recent_post->post_modified_gmt ) ) {
|
|
|
|
$meta['lastmodified'] = strtotime ( $recent_post->post_modified_gmt );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
elseif ( is_archive() ) {
|
2013-03-19 10:16:59 +00:00
|
|
|
$meta['type'] = 'archive';
|
2015-10-23 12:40:09 +01:00
|
|
|
if (isset($wp_ffpc_config['browsercache_taxonomy']) && !empty($wp_ffpc_config['browsercache_taxonomy']) && $wp_ffpc_config['browsercache_taxonomy'] > 0) {
|
|
|
|
$meta['expire'] = time() + $wp_ffpc_config['browsercache_taxonomy'];
|
|
|
|
}
|
|
|
|
|
|
|
|
global $wp_query;
|
|
|
|
|
|
|
|
if ( null != $wp_query->tax_query && !empty($wp_query->tax_query)) {
|
2016-01-15 20:18:18 +00:00
|
|
|
__wp_ffpc_debug__( 'Getting latest post for taxonomy: ' . json_encode($wp_query->tax_query));
|
2015-10-23 12:40:09 +01:00
|
|
|
|
|
|
|
$args = array(
|
|
|
|
'numberposts' => 1,
|
|
|
|
'orderby' => 'modified',
|
|
|
|
'order' => 'DESC',
|
|
|
|
'post_status' => 'publish',
|
|
|
|
'tax_query' => $wp_query->tax_query,
|
|
|
|
);
|
|
|
|
|
|
|
|
$recent_post = get_posts( $args, OBJECT );
|
|
|
|
|
|
|
|
if ( !empty($recent_post)) {
|
|
|
|
$recent_post = array_pop($recent_post);
|
|
|
|
if (!empty ( $recent_post->post_modified_gmt ) ) {
|
|
|
|
$meta['lastmodified'] = strtotime ( $recent_post->post_modified_gmt );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
elseif ( is_single() || is_page() ) {
|
2013-03-19 10:16:59 +00:00
|
|
|
$meta['type'] = 'single';
|
2015-10-23 12:40:09 +01:00
|
|
|
if (isset($wp_ffpc_config['browsercache']) && !empty($wp_ffpc_config['browsercache']) && $wp_ffpc_config['browsercache'] > 0) {
|
|
|
|
$meta['expire'] = time() + $wp_ffpc_config['browsercache'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* try if post is available
|
|
|
|
if made with archieve, last listed post can make this go bad
|
|
|
|
*/
|
|
|
|
global $post;
|
|
|
|
if ( !empty($post) && !empty ( $post->post_modified_gmt ) ) {
|
|
|
|
/* get last modification data */
|
|
|
|
$meta['lastmodified'] = strtotime ( $post->post_modified_gmt );
|
|
|
|
|
|
|
|
/* get shortlink, if possible */
|
|
|
|
if (function_exists('wp_get_shortlink')) {
|
|
|
|
$shortlink = wp_get_shortlink( );
|
|
|
|
if (!empty ( $shortlink ) )
|
|
|
|
$meta['shortlink'] = $shortlink;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
2013-03-19 10:16:59 +00:00
|
|
|
$meta['type'] = 'unknown';
|
2015-10-23 12:40:09 +01:00
|
|
|
}
|
2013-03-19 10:16:59 +00:00
|
|
|
|
2013-06-25 17:27:23 +01:00
|
|
|
if ( $meta['type'] != 'unknown' ) {
|
|
|
|
/* check if caching is disabled for page type */
|
|
|
|
$nocache_key = 'nocache_'. $meta['type'];
|
2013-03-19 10:16:59 +00:00
|
|
|
|
2013-06-25 17:27:23 +01:00
|
|
|
/* don't cache if prevented by rule */
|
|
|
|
if ( $wp_ffpc_config[ $nocache_key ] == 1 ) {
|
|
|
|
return $buffer;
|
|
|
|
}
|
2013-03-19 10:16:59 +00:00
|
|
|
}
|
|
|
|
|
2014-08-22 11:55:25 +01:00
|
|
|
if ( is_404() )
|
2013-03-19 10:16:59 +00:00
|
|
|
$meta['status'] = 404;
|
|
|
|
|
|
|
|
/* redirect page */
|
|
|
|
if ( $wp_ffpc_redirect != null)
|
|
|
|
$meta['redirect'] = $wp_ffpc_redirect;
|
|
|
|
|
|
|
|
/* feed is xml, all others forced to be HTML */
|
|
|
|
if ( is_feed() )
|
|
|
|
$meta['mime'] = 'text/xml;charset=';
|
|
|
|
else
|
|
|
|
$meta['mime'] = 'text/html;charset=';
|
|
|
|
|
|
|
|
/* set mimetype */
|
|
|
|
$meta['mime'] = $meta['mime'] . $wp_ffpc_config['charset'];
|
|
|
|
|
|
|
|
/* store pingback url if pingbacks are enabled */
|
|
|
|
if ( get_option ( 'default_ping_status' ) == 'open' )
|
|
|
|
$meta['pingback'] = get_bloginfo('pingback_url');
|
|
|
|
|
2015-10-13 11:13:13 +01:00
|
|
|
$to_store = $buffer;
|
|
|
|
|
2013-03-28 22:27:54 +00:00
|
|
|
/* add generation info is option is set, but only to HTML */
|
|
|
|
if ( $wp_ffpc_config['generate_time'] == '1' && stripos($buffer, '</body>') ) {
|
2013-03-25 13:11:49 +00:00
|
|
|
global $wp_ffpc_gentime;
|
|
|
|
$mtime = explode ( " ", microtime() );
|
|
|
|
$wp_ffpc_gentime = ( $mtime[1] + $mtime[0] )- $wp_ffpc_gentime;
|
|
|
|
|
2015-06-05 09:50:42 +01:00
|
|
|
$insertion = "\n<!-- WP-FFPC cache generation stats" . "\n\tgeneration time: ". round( $wp_ffpc_gentime, 3 ) ." seconds\n\tgeneraton UNIX timestamp: ". time() . "\n\tgeneraton date: ". date( 'c' ) . "\n\tgenerator server: ". $_SERVER['SERVER_ADDR'] . " -->\n";
|
2013-03-25 13:11:49 +00:00
|
|
|
$index = stripos( $buffer , '</body>' );
|
|
|
|
|
2015-06-05 09:50:42 +01:00
|
|
|
$to_store = substr_replace( $buffer, $insertion, $index, 0);
|
2013-03-25 13:11:49 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 10:19:37 +00:00
|
|
|
/**
|
|
|
|
* Allows to edit the content to be stored in cache.
|
|
|
|
*
|
|
|
|
* This hooks allows the user to edit the page content right before it is about
|
|
|
|
* to be stored in the cache. This could be useful for alterations like
|
|
|
|
* minification.
|
|
|
|
*
|
|
|
|
* @since 1.10.2
|
|
|
|
*
|
|
|
|
* @param string $to_store The content to be stored in cache.
|
|
|
|
*/
|
|
|
|
$to_store = apply_filters( 'wp-ffpc-to-store', $to_store );
|
2016-01-15 20:18:18 +00:00
|
|
|
|
2014-04-09 19:16:35 +01:00
|
|
|
$prefix_meta = $wp_ffpc_backend->key ( $wp_ffpc_config['prefix_meta'] );
|
|
|
|
$wp_ffpc_backend->set ( $prefix_meta, $meta );
|
|
|
|
|
|
|
|
$prefix_data = $wp_ffpc_backend->key ( $wp_ffpc_config['prefix_data'] );
|
2015-06-05 09:50:42 +01:00
|
|
|
$wp_ffpc_backend->set ( $prefix_data , $to_store );
|
2013-03-19 10:16:59 +00:00
|
|
|
|
2014-04-09 21:34:14 +01:00
|
|
|
if ( !empty( $meta['status'] ) && $meta['status'] == 404 ) {
|
2013-11-07 10:06:49 +00:00
|
|
|
header("HTTP/1.1 404 Not Found");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* vital for nginx, make no problem at other places */
|
|
|
|
header("HTTP/1.1 200 OK");
|
|
|
|
}
|
2013-03-19 10:16:59 +00:00
|
|
|
|
|
|
|
/* echoes HTML out */
|
2015-04-29 09:40:12 +01:00
|
|
|
return trim($buffer);
|
2013-03-19 10:16:59 +00:00
|
|
|
}
|
2013-03-28 22:27:54 +00:00
|
|
|
/*** END GENERATING CACHE ENTRY ***/
|