Created
January 5, 2016 17:18
-
-
Save spacedmonkey/c98afa1f14433368469a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin name: Batcache Manager | |
Plugin URI: http://wordpress.org/extend/plugins/batcache/ | |
Description: This optional plugin improves Batcache. | |
Author: Andy Skelton | |
Author URI: http://andyskelton.com/ | |
Version: 1.3 | |
*/ | |
global $batcache, $wp_object_cache; | |
// Do not load if our advanced-cache.php isn't loaded | |
if ( ! isset( $batcache ) || ! is_object( $batcache ) || ! method_exists( $wp_object_cache, 'incr' ) ) { | |
return; | |
} | |
$batcache->configure_groups(); | |
/** | |
* @param $post_id | |
*/ | |
function batcache_post( $post_id ) { | |
global $batcache; | |
$post = get_post( $post_id ); | |
if ( $post->post_type == 'revision' || ! in_array( get_post_status( $post_id ), array( 'publish', 'trash' ) ) ) { | |
return; | |
} | |
$links = array(); | |
$links[] = home_url(); | |
$links[] = get_permalink( $post_id ); | |
$links[] = get_post_comments_feed_link( $post_id ); | |
$links[] = get_feed_link( 'rdf' ); | |
$links[] = get_feed_link( 'rss' ); | |
$links[] = get_feed_link( 'rss2' ); | |
$links[] = get_feed_link( 'atom' ); | |
if ( $post->post_type == 'post' ) { | |
$links[] = get_author_posts_url( $post->post_author ); | |
$links[] = get_author_feed_link( $post->post_author ); | |
$year = get_the_time( "Y", $post ); | |
$month = get_the_time( "m", $post ); | |
$day = get_the_time( "d", $post ); | |
$links[] = get_year_link( $year ); | |
$links[] = get_month_link( $year, $month ); | |
$links[] = get_day_link( $year, $month, $day ); | |
} else if ( ! in_array( $post->post_type, get_post_types( array( 'public' => true ) ) ) ) { | |
if ( $archive_link = get_post_type_archive_link( $post->post_type ) ) { | |
$links[] = $archive_link; | |
} | |
} | |
batcache_clear_urls( $links ); | |
} | |
// Regen home and permalink on posts and pages | |
add_action( 'clean_post_cache', 'batcache_post' ); | |
// Regen permalink on comments (TODO) | |
//add_action('comment_post', 'batcache_comment'); | |
//add_action('wp_set_comment_status', 'batcache_comment'); | |
//add_action('edit_comment', 'batcache_comment'); | |
/** | |
* @param $ids | |
* @param $taxonomy | |
*/ | |
function batcache_term( $ids, $taxonomy ) { | |
$links = array(); | |
foreach ( $ids as $term ) { | |
$term_link = get_term_link( $term, $taxonomy ); | |
if ( ! is_wp_error( $term_link ) ) { | |
$links[] = $term_link; | |
} | |
$term_link_feed = get_term_feed_link( $term, $taxonomy ); | |
if ( $term_link_feed ) { | |
$links[] = $term_link_feed; | |
} | |
} | |
batcache_clear_urls( $links ); | |
} | |
add_action( 'clean_term_cache', 'batcache_term', 10, 2 ); | |
function batcache_clear_urls( $links ) { | |
$home = parse_url( home_url(), PHP_URL_HOST ); | |
$compare_urls = array( | |
parse_url( get_option( 'home' ), PHP_URL_HOST ), | |
parse_url( get_option( 'siteurl' ), PHP_URL_HOST ), | |
parse_url( site_url(), PHP_URL_HOST ) | |
); | |
foreach ( $compare_urls as $compare_url ) { | |
if ( $compare_url != $home ) { | |
foreach ( $links as $url ) { | |
$links[] = str_replace( $home, $compare_url, $url ); | |
} | |
} | |
} | |
foreach ( $links as $url ) { | |
batcache_clear_url( $url ); | |
} | |
} | |
/** | |
* @param $url | |
* | |
* @return bool|false|int | |
*/ | |
function batcache_clear_url( $url ) { | |
global $batcache, $wp_object_cache; | |
if ( empty( $url ) ) { | |
return false; | |
} | |
$url = set_url_scheme( $url, 'http' ); | |
$url_key = md5( $url ); | |
wp_cache_add( "{$url_key}_version", 0, $batcache->group ); | |
$retval = wp_cache_incr( "{$url_key}_version", 1, $batcache->group ); | |
$batcache_no_remote_group_key = array_search( $batcache->group, (array) $wp_object_cache->no_remote_groups ); | |
if ( false !== $batcache_no_remote_group_key ) { | |
// The *_version key needs to be replicated remotely, otherwise invalidation won't work. | |
// The race condition here should be acceptable. | |
unset( $wp_object_cache->no_remote_groups[ $batcache_no_remote_group_key ] ); | |
$retval = wp_cache_set( "{$url_key}_version", $retval, $batcache->group ); | |
$wp_object_cache->no_remote_groups[ $batcache_no_remote_group_key ] = $batcache->group; | |
} | |
return $retval; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment