Skip to content

Commit

Permalink
rework flush hooks and add some third-party triggers (pluginkollektiv…
Browse files Browse the repository at this point in the history
…#225)

* Rework hooks for flushing the cache when modifying posts.
* Deprecate register_publish_hooks() and publish_post_types().
* Introduce hooks to flush the cache on WooCommerce product updates.
* Flush the cache along with Autoptimize flushing by default.

Co-authored-by: Stefan Kalscheuer <[email protected]>
  • Loading branch information
timse201 and stklcode authored Nov 20, 2022
1 parent 643353e commit 7a6b3b4
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Requires PHP 5.6 and WordPress 4.7 or above

* Enhance: adjust styling for setup instructions (#215, props timse201)
* Enhance: update hooks for Multisite initialization in WordPress 5.1 and above (#246, props ouun)
* Enhance: rework flush hooks and add some third-party triggers for Autoptimize and WooCommerce (#225, props timse201)


## 2.3.2
Expand Down
102 changes: 97 additions & 5 deletions inc/class-cachify.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,18 @@ public function __construct() {

self::$is_nginx = $GLOBALS['is_nginx'];

/* Publish hooks */
add_action( 'init', array( __CLASS__, 'register_publish_hooks' ), 99 );

/* Flush Hooks */
add_action( 'init', array( __CLASS__, 'register_flush_cache_hooks' ), 10, 0 );

add_action( 'save_post', array( __CLASS__, 'save_update_trash_post' ) );
add_action( 'pre_post_update', array( __CLASS__, 'post_update' ), 10, 2 );
add_action( 'cachify_remove_post_cache', array( __CLASS__, 'remove_page_cache_by_post_id' ) );

/* Flush Hooks - third party */
add_action( 'woocommerce_product_set_stock', array( __CLASS__, 'flush_woocommerce' ) );
add_action( 'woocommerce_variation_set_stock', array( __CLASS__, 'flush_woocommerce' ) );
add_action( 'woocommerce_product_set_stock_status', array( __CLASS__, 'flush_woocommerce' ) );
add_action( 'woocommerce_variation_set_stock_status', array( __CLASS__, 'flush_woocommerce' ) );

/* Register scripts */
add_action( 'init', array( __CLASS__, 'register_scripts' ) );

Expand Down Expand Up @@ -945,7 +949,7 @@ public static function touch_comment( $new_status, $old_status, $comment ) {
* @since 2.1.7 Make the function public
* @since 2.0.3
*
* @return void
* @deprecated no longer used since 2.4
*/
public static function register_publish_hooks() {
/* Available post types */
Expand Down Expand Up @@ -975,6 +979,8 @@ public static function register_publish_hooks() {
*
* @param integer $post_id Post ID.
* @param object $post Post object.
*
* @deprecated no longer used since 2.4
*/
public static function publish_post_types( $post_id, $post ) {
/* No post_id? */
Expand All @@ -1000,6 +1006,90 @@ public static function publish_post_types( $post_id, $post ) {
}
}

/**
* Removes the post type cache if saved or updated
*
* @since 2.0.3
* @since 2.1.7 Make the function public.
* @since 2.4 Renamed to save_update_trash_post with $id parameter.
*
* @param integer $id Post ID.
*/
public static function save_update_trash_post( $id ) {
$status = get_post_status( $id );

/* Post type published? */
if ( 'publish' === $status ) {
self::flush_cache_for_posts( $id );
}
}

/**
* Removes the post type cache before an existing post type is updated in the db
*
* @since 2.0.3
* @since 2.3.0
* @since 2.4 Renamed to post_update.
*
* @param integer $id Post ID.
* @param array $data Post data.
*/
public static function post_update( $id, $data ) {
$new_status = $data['post_status'];
$old_status = get_post_status( $id );

/* Was it published and is it not trashed now? */
if ( 'trash' !== $new_status && 'publish' === $old_status ) {
self::flush_cache_for_posts( $id );
}
}

/**
* Clear cache when any post type has been created or updated
*
* @since 2.4
*
* @param integer|WP_Post $post Post ID or object.
*/
public static function flush_cache_for_posts( $post ) {
if ( is_int( $post ) ) {
$post_id = $post;
$data = get_post( $post_id );

if ( ! is_object( $data ) ) {
return;
}
} elseif ( is_object( $post ) ) {
$post_id = $post->ID;
} else {
return;
}

/* Remove cache OR flush */
if ( 1 !== self::$options['reset_on_post'] ) {
self::remove_page_cache_by_post_id( $post_id );
} else {
self::flush_total_cache();
}
}

/**
* Flush post cache on WooCommerce stock changes.
*
* @since 2.4
*
* @param int|WC_Product $product Product ID or object.
*/
public static function flush_woocommerce( $product ) {
if ( is_int( $product ) ) {
$id = $product;
} else {
$id = $product->get_id();
}

self::flush_cache_for_posts( $id );
}

/**
* Removes a page (id) from cache
*
Expand Down Expand Up @@ -1174,6 +1264,8 @@ public static function register_flush_cache_hooks() {
'user_register' => 10,
'edit_user_profile_update' => 10,
'delete_user' => 10,
/* third party */
'autoptimize_action_cachepurged' => 10,
);

$flush_cache_hooks = apply_filters( 'cachify_flush_cache_hooks', $flush_cache_hooks );
Expand Down

0 comments on commit 7a6b3b4

Please sign in to comment.