Skip to content

Commit

Permalink
Merge pull request #109 from rubas/purge_filters
Browse files Browse the repository at this point in the history
Filter hooks for surrogate key for purging
  • Loading branch information
danielbachhuber authored Nov 22, 2017
2 parents 9e5bddc + 969de3b commit c7e1a1a
Showing 1 changed file with 118 additions and 7 deletions.
125 changes: 118 additions & 7 deletions inc/class-purger.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,20 @@ public static function action_clean_post_cache( $post_id ) {
if ( $type && 'revision' === $type ) {
return;
}
pantheon_wp_clear_edge_keys( array( 'post-' . $post_id, 'rest-post-' . $post_id, 'post-huge', 'rest-post-huge' ) );
$keys = array(
'post-' . $post_id,
'rest-post-' . $post_id,
'post-huge',
'rest-post-huge',
);
/**
* Surrogate keys purged when clearing post cache.
*
* @param array $keys Surrogate keys.
* @param array $post_id ID for purged post.
*/
$keys = apply_filters( 'pantheon_purge_clean_post_cache', $keys, $post_id );
pantheon_wp_clear_edge_keys( $keys );
}

/**
Expand All @@ -82,7 +95,19 @@ public static function action_clean_post_cache( $post_id ) {
*/
public static function action_created_term( $term_id, $tt_id, $taxonomy ) {
self::purge_term( $term_id );
pantheon_wp_clear_edge_keys( array( 'rest-' . $taxonomy . '-collection' ) );
$keys = array(
'rest-' . $taxonomy . '-collection',
);
/**
* Surrogate keys purged when creating a new term.
*
* @param array $keys Surrogate keys.
* @param array $term_id ID for new term.
* @param array $tt_id Term taxonomy ID for new term.
* @param string $taxonomy Taxonomy for the new term.
*/
$keys = apply_filters( 'pantheon_purge_create_term', $keys, $term_id, $tt_id, $taxonomy );
pantheon_wp_clear_edge_keys( $keys );
}

/**
Expand Down Expand Up @@ -117,6 +142,13 @@ public static function action_clean_term_cache( $term_ids ) {
}
$keys[] = 'term-huge';
$keys[] = 'rest-term-huge';
/**
* Surrogate keys purged when clearing term cache.
*
* @param array $keys Surrogate keys.
* @param array $term_ids IDs for purged terms.
*/
$keys = apply_filters( 'pantheon_purge_clean_term_cache', $keys, $term_ids );
pantheon_wp_clear_edge_keys( $keys );
}

Expand All @@ -130,7 +162,20 @@ public static function action_wp_insert_comment( $id, $comment ) {
if ( 1 != $comment->comment_approved ) {
return;
}
pantheon_wp_clear_edge_keys( array( 'rest-comment-' . $comment->comment_ID, 'rest-comment-collection', 'rest-comment-huge' ) );
$keys = array(
'rest-comment-' . $comment->comment_ID,
'rest-comment-collection',
'rest-comment-huge',
);
/**
* Surrogate keys purged when inserting a new comment.
*
* @param array $keys Surrogate keys.
* @param integer $id Comment ID.
* @param WP_Comment $comment Comment to be inserted.
*/
$keys = apply_filters( 'pantheon_purge_insert_comment', $keys, $id, $comment );
pantheon_wp_clear_edge_keys( $keys );
}

/**
Expand All @@ -141,7 +186,22 @@ public static function action_wp_insert_comment( $id, $comment ) {
* @param object $comment The comment data.
*/
public static function action_transition_comment_status( $new_status, $old_status, $comment ) {
pantheon_wp_clear_edge_keys( array( 'rest-comment-' . $comment->comment_ID, 'rest-comment-collection', 'rest-comment-huge' ) );
$keys = array(
'rest-comment-' . $comment->comment_ID,
'rest-comment-collection',
'rest-comment-huge',
);
/**
* Surrogate keys purged when transitioning a comment status.
*
* @param array $keys Surrogate keys.
* @param string $new_status New comment status.
* @param string $old_status Old comment status.
* @param WP_Comment $comment Comment being transitioned.
*/
$keys = apply_filters( 'pantheon_purge_transition_comment_status', $keys, $new_status, $old_status, $comment );
pantheon_wp_clear_edge_keys( $keys );
;
}

/**
Expand All @@ -150,7 +210,18 @@ public static function action_transition_comment_status( $new_status, $old_statu
* @param integer $comment_id Modified comment id.
*/
public static function action_clean_comment_cache( $comment_id ) {
pantheon_wp_clear_edge_keys( array( 'rest-comment-' . $comment_id, 'rest-comment-huge' ) );
$keys = array(
'rest-comment-' . $comment_id,
'rest-comment-huge',
);
/**
* Surrogate keys purged when cleaning comment cache.
*
* @param array $keys Surrogate keys.
* @param integer $id Comment ID.
*/
$keys = apply_filters( 'pantheon_purge_clean_comment_cach', $keys, $comment_id );
pantheon_wp_clear_edge_keys( $keys );
}

/**
Expand Down Expand Up @@ -194,6 +265,13 @@ private static function purge_post_with_related( $post ) {
$keys[] = 'term-huge';
}
}
/**
* Related surrogate keys purged when purging a post.
*
* @param array $keys Surrogate keys.
* @param WP_Post $post Post object.
*/
$keys = apply_filters( 'pantheon_purge_post_with_related', $keys, $post );
pantheon_wp_clear_edge_keys( $keys );
}

Expand All @@ -203,7 +281,22 @@ private static function purge_post_with_related( $post ) {
* @param integer $term_id ID for the modified term.
*/
private static function purge_term( $term_id ) {
pantheon_wp_clear_edge_keys( array( 'term-' . $term_id, 'rest-term-' . $term_id, 'post-term-' . $term_id, 'term-huge', 'rest-term-huge', 'post-term-huge' ) );
$keys = array(
'term-' . $term_id,
'rest-term-' . $term_id,
'post-term-' . $term_id,
'term-huge',
'rest-term-huge',
'post-term-huge',
);
/**
* Surrogate keys purged when purging a term.
*
* @param array $keys Surrogate keys.
* @param integer $term_id Term ID.
*/
$keys = apply_filters( 'pantheon_purge_term', $keys, $term_id );
pantheon_wp_clear_edge_keys( $keys );
}


Expand All @@ -219,6 +312,13 @@ public static function action_clean_user_cache( $user_id ) {
'user-huge',
'rest-user-huge',
);
/**
* Surrogate keys purged when clearing user cache.
*
* @param array $keys Surrogate keys.
* @param array $user_id ID for purged user.
*/
$keys = apply_filters( 'pantheon_purge_clean_user_cache', $keys, $user_id );
pantheon_wp_clear_edge_keys( $keys );
}

Expand All @@ -236,7 +336,18 @@ public static function action_updated_option( $option ) {
return;
}
$rest_name = ! empty( $settings[ $option ]['show_in_rest']['name'] ) ? $settings[ $option ]['show_in_rest']['name'] : $option;
pantheon_wp_clear_edge_keys( array( 'rest-setting-' . $rest_name, 'rest-setting-huge' ) );
$keys = array(
'rest-setting-' . $rest_name,
'rest-setting-huge',
);
/**
* Surrogate keys purged when updating an option cache.
*
* @param array $keys Surrogate keys.
* @param string $option Option name.
*/
$keys = apply_filters( 'pantheon_purge_updated_option', $keys, $option );
pantheon_wp_clear_edge_keys( $keys );
}

}

0 comments on commit c7e1a1a

Please sign in to comment.