all repos — wp-ffpc @ 9f029e2414bf239fe5b67df58708f00e2c0f028c

1.10.0

proper browser cache support:
* new options to set real browser cache expiry for singles, taxonomy and home
* added Etag support based on browser cache expiry
* added proper Expires header according to cache entry generation time + browser cache expiry
* added support for Last Modified header for home & taxonomy ( singles already had it) based on the last post modified date within the taxonomy
Peter Molnar hello@petermolnar.eu
Fri, 23 Oct 2015 12:40:09 +0100
commit

9f029e2414bf239fe5b67df58708f00e2c0f028c

parent

c84498d06997f6f6d87d38182ee1b5f10648576a

5 files changed, 169 insertions(+), 42 deletions(-)

jump to
M readme.txtreadme.txt

@@ -4,7 +4,7 @@ Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=XU3DG7LLA76WC

Tags: cache, page cache, full page cache, nginx, memcached, apc, speed Requires at least: 3.0 Tested up to: 4.3.1 -Stable tag: 1.9.1 +Stable tag: 1.10.0 License: GPLv3 License URI: http://www.gnu.org/licenses/gpl-3.0.html

@@ -79,6 +79,7 @@ * Meint Post

* Knut Sparhell * Christian Kernbeis * Gausden Barry +* Maksim Bukreyeu == Installation ==

@@ -127,6 +128,18 @@

* every A. indicates BIG changes. * every .B version indicates new features. * every ..C indicates bugfixes for A.B version. + += 1.10.0 = +*2015-10-23* + +*IMPORTANT, READ THIS* + +proper browser cache support: +* new options to set real browser cache expiry for singles, taxonomy and home +* added Etag support based on browser cache expiry +* added proper Expires header according to cache entry generation time + browser cache expiry +* added support for Last Modified header for home & taxonomy ( singles already had it) based on the last post modified date within the taxonomy + = 1.9.1 = *2015-10-18*
M wp-ffpc-acache.phpwp-ffpc-acache.php

@@ -139,6 +139,7 @@ }

else { /* store results */ $wp_ffpc_values[ $internal ] = $value; + __debug__('Got value for ' . $internal); } }

@@ -177,32 +178,57 @@ /* if we reach this point it means data was found & correct, serve it */

if (!empty ( $wp_ffpc_values['meta']['mime'] ) ) header('Content-Type: ' . $wp_ffpc_values['meta']['mime']); -/* don't allow browser caching of page */ -header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0'); -header('Pragma: no-cache'); +/* 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'] ); -/* expire at this very moment */ -header('Expires: ' . gmdate("D, d M Y H:i:s", time() ) . " GMT"); + 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; + } + + 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'); +} /* if shortlinks were set */ -if (!empty ( $wp_ffpc_values['meta']['shortlink'] ) ) +if (isset($wp_ffpc_values['meta']['shortlink']) && !empty ( $wp_ffpc_values['meta']['shortlink'] ) ) header( 'Link:<'. $wp_ffpc_values['meta']['shortlink'] .'>; rel=shortlink' ); /* if last modifications were set (for posts & pages) */ -if ( !empty($wp_ffpc_values['meta']['lastmodified']) ) +if (isset($wp_ffpc_values['meta']['lastmodified']) && !empty($wp_ffpc_values['meta']['lastmodified']) ) header( 'Last-Modified: ' . gmdate("D, d M Y H:i:s", $wp_ffpc_values['meta']['lastmodified'] ). " GMT" ); /* pingback urls, if existx */ -if ( !empty( $wp_ffpc_values['meta']['pingback'] ) && $wp_ffpc_config['pingback_header'] ) +if ( isset($wp_ffpc_values['meta']['pingback']) && !empty( $wp_ffpc_values['meta']['pingback'] ) && isset($wp_ffpc_config['pingback_header']) && $wp_ffpc_config['pingback_header'] ) header( 'X-Pingback: ' . $wp_ffpc_values['meta']['pingback'] ); /* for debugging */ -if ( $wp_ffpc_config['response_header'] ) +if ( isset($wp_ffpc_config['response_header']) && $wp_ffpc_config['response_header'] ) header( 'X-Cache-Engine: WP-FFPC with ' . $wp_ffpc_config['cache_type'] .' via PHP'); /* HTML data */ - -if ( $wp_ffpc_config['generate_time'] == '1' && stripos($wp_ffpc_values['data'], '</body>') ) { +if ( isset($wp_ffpc_config['generate_time']) && $wp_ffpc_config['generate_time'] == '1' && stripos($wp_ffpc_values['data'], '</body>') ) { $mtime = explode ( " ", microtime() ); $wp_ffpc_gentime = ( $mtime[1] + $mtime[0] ) - $wp_ffpc_gentime;

@@ -284,18 +310,90 @@ return $buffer;

} } - if ( is_home() ) - $meta['type'] = 'home'; - elseif (is_feed() ) - $meta['type'] = 'feed'; - elseif ( is_archive() ) + 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']; + } + + __debug__( 'Getting latest post for for home & feed'); + /* 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() ) { $meta['type'] = 'archive'; - elseif ( is_single() ) + 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)) { + __debug__( 'Getting latest post for taxonomy: ' . json_encode($wp_query->tax_query)); + + $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() ) { $meta['type'] = 'single'; - elseif ( is_page() ) - $meta['type'] = 'page'; - else + 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 { $meta['type'] = 'unknown'; + } if ( $meta['type'] != 'unknown' ) { /* check if caching is disabled for page type */

@@ -322,22 +420,6 @@ $meta['mime'] = 'text/html;charset=';

/* set mimetype */ $meta['mime'] = $meta['mime'] . $wp_ffpc_config['charset']; - - /* try if post is available - if made with archieve, last listed post can make this go bad - */ - global $post; - if ( !empty($post) && ( $meta['type'] == 'single' || $meta['type'] == 'page' ) && !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; - } - } /* store pingback url if pingbacks are enabled */ if ( get_option ( 'default_ping_status' ) == 'open' )
M wp-ffpc-backend.phpwp-ffpc-backend.php

@@ -386,12 +386,17 @@ $terms = get_terms ( $taxonomy->name , $sargs );

if ( !empty ( $terms ) ) { foreach ( $terms as $term ) { + + /* skip terms that have no post associated and somehow slipped + * throught hide_empty */ + if ( $term->count == 0) + continue; + /* get the permalink for the term */ $link = get_term_link ( $term->slug, $taxonomy->name ); /* add to container */ $links[ $link ] = true; - /* remove the taxonomy name from the link, lots of plugins remove this for SEO, it's better to include them than leave them out - in worst case, we cache some 404 as well + /* remove the taxonomy name from the link, lots of plugins remove this for SEO, it's better to include them than leave them out in worst case, we cache some 404 as well */ $link = str_replace ( '/'.$taxonomy->rewrite['slug'], '', $link ); /* add to container */
M wp-ffpc-class.phpwp-ffpc-class.php

@@ -492,6 +492,14 @@ <span class="description"><?php _e('Sets validity time of post entry in seconds, including custom post types and pages.', 'wp-ffpc'); ?></span>

</dd> <dt> + <label for="browsercache"><?php _e('Browser cache expiration time of posts', 'wp-ffpc'); ?></label> + </dt> + <dd> + <input type="number" name="browsercache" id="browsercache" value="<?php echo $this->options['browsercache']; ?>" /> + <span class="description"><?php _e('Sets validity time of posts/pages/singles for the browser cache.', 'wp-ffpc'); ?></span> + </dd> + + <dt> <label for="expire_taxonomy"><?php _e('Expiration time for taxonomy', 'wp-ffpc'); ?></label> </dt> <dd>

@@ -500,11 +508,27 @@ <span class="description"><?php _e('Sets validity time of taxonomy entry in seconds, including custom taxonomy.', 'wp-ffpc'); ?></span>

</dd> <dt> + <label for="browsercache_taxonomy"><?php _e('Browser cache expiration time of taxonomy', 'wp-ffpc'); ?></label> + </dt> + <dd> + <input type="number" name="browsercache_taxonomy" id="browsercache_taxonomy" value="<?php echo $this->options['browsercache_taxonomy']; ?>" /> + <span class="description"><?php _e('Sets validity time of taxonomy for the browser cache.', 'wp-ffpc'); ?></span> + </dd> + + <dt> <label for="expire_home"><?php _e('Expiration time for home', 'wp-ffpc'); ?></label> </dt> <dd> <input type="number" name="expire_home" id="expire_home" value="<?php echo $this->options['expire_home']; ?>" /> - <span class="description"><?php _e('Sets validity time of home.', 'wp-ffpc'); ?></span> + <span class="description"><?php _e('Sets validity time of home on server side.', 'wp-ffpc'); ?></span> + </dd> + + <dt> + <label for="browsercache_home"><?php _e('Browser cache expiration time of home', 'wp-ffpc'); ?></label> + </dt> + <dd> + <input type="number" name="browsercache_home" id="browsercache_home" value="<?php echo $this->options['browsercache_home']; ?>" /> + <span class="description"><?php _e('Sets validity time of home for the browser cache.', 'wp-ffpc'); ?></span> </dd> <dt>
M wp-ffpc.phpwp-ffpc.php

@@ -3,7 +3,7 @@ /*

Plugin Name: WP-FFPC Plugin URI: https://github.com/petermolnar/wp-ffpc Description: WordPress in-memory full page cache plugin -Version: 1.9.1 +Version: 1.10.0 Author: Peter Molnar <hello@petermolnar.eu> Author URI: http://petermolnar.eu/ License: GPLv3

@@ -32,6 +32,9 @@ 'hosts'=>'127.0.0.1:11211',

'memcached_binary' => false, 'authpass' => '', 'authuser' => '', + 'browsercache' => 0, + 'browsercache_home' => 0, + 'browsercache_taxonomy' => 0, 'expire' => 300, 'expire_home' => 300, 'expire_taxonomy' => 300,

@@ -60,4 +63,4 @@ 'pingback_header' => false,

'hashkey' => false, ); -$wp_ffpc = new WP_FFPC ( 'wp-ffpc', '1.8.2', 'WP-FFPC', $wp_ffpc_defaults, 'PeterMolnar_WordPressPlugins_wp-ffpc_HU' , 'WP-FFPC' , 'FA3NT7XDVHPWU' ); +$wp_ffpc = new WP_FFPC ( 'wp-ffpc', '1.10.1', 'WP-FFPC', $wp_ffpc_defaults, 'PeterMolnar_WordPressPlugins_wp-ffpc_HU' , 'WP-FFPC' , 'FA3NT7XDVHPWU' );