Skip to content

Commit

Permalink
New: crp_delete_cache_by_post_id()
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaydsouza committed Oct 23, 2023
1 parent 90b66be commit 301531a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 24 deletions.
3 changes: 3 additions & 0 deletions includes/admin/modules/metabox.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,9 @@ function crp_save_meta_box( $post_id ) {
update_post_meta( $post_id, 'crp_post_meta', $post_meta_filtered );
}

// Clear cache of current post.
crp_delete_cache_by_post_id( $post_id );

/**
* Action triggered when saving Contextual Related Posts meta box settings
*
Expand Down
28 changes: 4 additions & 24 deletions includes/modules/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,39 +137,19 @@ function crp_cache_get_meta_keys( $post_id = 0 ) {
return apply_filters( 'crp_cache_get_meta_keys', $meta_keys );
}


/**
* Function to clear cache on post save.
* Delete cache by post ID.
*
* @since 2.5.0
* @since 3.4.0
*
* @param mixed $post_id Post ID.
* @param int $post_id Post ID.
*/
function crp_delete_cache_post_save( $post_id ) {

// Bail if we're doing an auto save.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}

// If our nonce isn't there, or we can't verify it, bail.
if ( ! isset( $_POST['crp_meta_box_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['crp_meta_box_nonce'] ), 'crp_meta_box' ) ) {
return;
}

// If our current user can't edit this post, bail.
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}

// Clear cache of current post.
function crp_delete_cache_by_post_id( $post_id ) {
$meta_keys = crp_cache_get_meta_keys( $post_id );
foreach ( $meta_keys as $meta_key ) {
delete_post_meta( $post_id, $meta_key );
}
}
add_action( 'crp_save_meta_box', 'crp_delete_cache_post_save' );


/**
* Get the meta key based on a list of parameters.
Expand Down
3 changes: 3 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ Features:
* Caching of the entire HTML output is enabled by default. You can disable it in the settings page. This will reduce the number of database queries and improve performance. If you have customised the output, you will need to clear the cache for the changes to take effect. Applies to new installs and when you reset the settings
* The plugin no longer check for pre v2.5 settings key

* Bug Fixes:
* The post cache was not always cleared when a post was updated

* Deprecated:
* `get_crp_posts_id` has been completed deprecated and will use `get_crp_posts` instead. The function will continue to work but will be removed in a future version

Expand Down

0 comments on commit 301531a

Please sign in to comment.