Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented new invalidation method; added filter hook. #25

Merged
merged 9 commits into from Sep 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 59 additions & 12 deletions wp-ffpc-backend.php
Expand Up @@ -85,6 +85,27 @@ public function __construct( $config, $network = false ) {

}


public static function parse_urimap($uri, $default_urimap=null) {
$uri_parts = parse_url( $uri );

$uri_map = array(
'$scheme' => $uri_parts['scheme'],
'$host' => $uri_parts['host'],
'$request_uri' => $uri_parts['path']
);

if (is_array($default_urimap)) {
$uri_map = array_merge($default_urimap, $uri_map);
}

return $uri_map;
}

public static function map_urimap($urimap, $subject) {
return str_replace(array_keys($urimap), $urimap, $subject);
}

/*********************** PUBLIC / PROXY FUNCTIONS ***********************/

/**
Expand All @@ -95,7 +116,7 @@ public function __construct( $config, $network = false ) {
*/
public function key ( &$prefix ) {
/* data is string only with content, meta is not used in nginx */
$key = $prefix . str_replace ( array_keys( $this->urimap ), $this->urimap, $this->options['key'] );
$key = $prefix . self::map_urimap($this->urimap, $this->options['key']);
$this->log ( sprintf( __translate__( 'original key configuration: %s', $this->plugin_constant ), $this->options['key'] ) );
$this->log ( sprintf( __translate__( 'setting key to: %s', $this->plugin_constant ), $key ) );
return $key;
Expand Down Expand Up @@ -200,33 +221,59 @@ public function clear ( $post_id = false, $force = false ) {
$this->taxonomy_links( $to_clear );
}

/* clear pasts index page if settings requires it */
if ( $this->options['invalidation_method'] == 3 ) {
$posts_page_id = get_option( 'page_for_posts' );
$post_type = get_post_type( $post_id );

if ($post_type === 'post' && $posts_page_id != $post_id) {
$this->clear($posts_page_id, $force);
}
}


/* if there's a post id pushed, it needs to be invalidated in all cases */
if ( !empty ( $post_id ) ) {

/* need permalink functions */
if ( !function_exists('get_permalink') )
include_once ( ABSPATH . 'wp-includes/link-template.php' );

/* get path from permalink */
$path = substr ( get_permalink( $post_id ) , 7 );
/* get permalink */
$permalink = get_permalink( $post_id );

/* no path, don't do anything */
if ( empty( $path ) ) {
if ( empty( $permalink ) ) {
$this->log ( sprintf( __translate__( 'unable to determine path from Post Permalink, post ID: %s', $this->plugin_constant ), $post_id ), LOG_WARNING );
return false;
}

if ( isset($_SERVER['HTTPS']) && ( ( strtolower($_SERVER['HTTPS']) == 'on' ) || ( $_SERVER['HTTPS'] == '1' ) ) )
$protocol = 'https://';
else
$protocol = 'http://';
/*
* It is possible that post/page is paginated with <!--nextpage-->
* Wordpress doesn't seem to expose the number of pages via API.
* So let's just count it.
*/
$content_post = get_post( $post_id );
$content = $content_post->post_content;
$number_of_pages = 1 + (int)preg_match_all('/<!--nextpage-->/', $content, $matches);

$current_page_id = '';
do {
/* urimap */
$urimap = self::parse_urimap($permalink, $this->urimap);
$urimap['$request_uri'] = $urimap['$request_uri'] . ($current_page_id ? $current_page_id . '/' : '');

$clear_cache_key = self::map_urimap($urimap, $this->options['key']);

/* elements to clear
values are keys, faster to process and eliminates duplicates
*/
$to_clear[ $protocol . $path ] = true;
$to_clear[ $clear_cache_key ] = true;

$current_page_id = 1+(int)$current_page_id;
} while ($number_of_pages>1 && $current_page_id<=$number_of_pages);
}

/* Hook to custom clearing array. */
$to_clear = apply_filters('wp_ffpc_to_clear_array', $to_clear, $post_id);

foreach ( $to_clear as $link => $dummy ) {
/* clear all feeds as well */
$to_clear[ $link. 'feed' ] = true;
Expand Down
16 changes: 15 additions & 1 deletion wp-ffpc-class.php
Expand Up @@ -140,6 +140,7 @@ public function plugin_pre_init() {
0 => __( 'flush cache' , $this->plugin_constant ),
1 => __( 'only modified post' , $this->plugin_constant ),
2 => __( 'modified post and all taxonomies' , $this->plugin_constant ),
3 => __( 'modified post and posts index page' , $this->plugin_constant ),
);

/* map of possible key masks */
Expand Down Expand Up @@ -483,7 +484,20 @@ public function plugin_admin_panel() {
<select name="invalidation_method" id="invalidation_method">
<?php $this->print_select_options ( $this->select_invalidation_method , $this->options['invalidation_method'] ) ?>
</select>
<span class="description"><?php _e('Select cache invalidation method. <ol><li><em>flush cache</em> - clears everything in storage, <strong>including values set by other applications</strong></li><li><em>only modified post</em> - clear only the modified posts entry, everything else remains in cache</li><li><em>modified post and all taxonomies</em> - removes all taxonomy term cache ( categories, tags, home, etc ) and the modified post as well</li></ol>', $this->plugin_constant); ?></span>
<div class="description"><?php _e('Select cache invalidation method.', $this->plugin_constant); ?>
<ol>
<?php
$invalidation_method_description = array(
'clears everything in storage, <strong>including values set by other applications</strong>',
'clear only the modified posts entry, everything else remains in cache',
'removes all taxonomy term cache ( categories, tags, home, etc ) and the modified post as well<br><strong>Caution! Slows down page/post saving when there are many tags.</strong>',
'clear cache for modified post and posts index page'
);
foreach ($this->select_invalidation_method AS $current_key => $current_invalidation_method) {
printf('<li><em>%1$s</em> - %2$s</li>', $current_invalidation_method, $invalidation_method_description[$current_key]);
} ?>
</ol>
</div>
</dd>

<dt>
Expand Down