Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active June 13, 2024 01:48
Show Gist options
  • Save wpmudev-sls/1f61199694ace4b2285a3e9e143c205c to your computer and use it in GitHub Desktop.
Save wpmudev-sls/1f61199694ace4b2285a3e9e143c205c to your computer and use it in GitHub Desktop.
[Hummingbird Pro] Clear homepage cache programmatically when a post is updated
<?php
/**
* Plugin Name: [Hummingbird Pro] Clear homepage cache programmatically when a post is updated
* Description: Useful when the "Clear full cache when post/page is updated" option needs to remain disabled
* Author: Anderson Salas @ WPMUDEV
* Task: SLS-6215
* Author URI: https://premium.wpmudev.org
* License: GPLv2 or later
*/
add_action( 'plugins_loaded', function() {
if ( ! defined( 'WPHB_VERSION' ) ) {
return; // Hummingbird is not installed/enabled.
}
if ( class_exists( 'WPMUDEV_Hummingbrd_Custom_Cache_Clear' ) ) {
return; // Already loaded.
}
class WPMUDEV_Hummingbrd_Custom_Cache_Clear {
private static $instance;
// [>>>] Snippet settings BEGIN ----------------------------------------- //
// The ID(s) of the page(s) which cache will be cleared after a post edit.
private $target_pages = [ 32 ];
// [>>>] Snippet settings END ------------------------------------------- //
private $cache;
public static function get() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
private function __construct() {
$cache = Hummingbird\WP_Hummingbird::get_instance()->core->modules[ 'page_cache' ] ?? null;
if ( empty( $cache ) || false === $cache->is_active() ) {
return; // Page Cache is not enabled.
}
$this->cache = $cache;
add_action( 'edit_post', array( $this, 'post_edit' ), 1 );
add_action( 'transition_post_status', array( $this, 'post_status_change' ), 11, 3 );
}
public function post_edit( $post_id ) {
if ( wp_is_post_revision( $post_id ) ) {
return;
}
$post = get_post( $post_id );
if ( ! isset( $post->post_type ) || ! is_post_type_viewable( $post->post_type ) ) {
return;
}
$this->purge_post_cache( $post_id );
}
public function post_status_change( $new_status, $old_status, $post ) {
global $post_trashed;
if ( ( ! isset( $post->post_type ) || ! is_post_type_viewable( $post->post_type ) ) ||
( $new_status === $old_status || wp_is_post_revision( $post->ID ) ) ||
( 'auto-draft' === $new_status || 'draft' === $new_status ) ) {
return;
}
$post_trashed = false;
if ( 'trash' === $new_status ) {
$post_trashed = true;
}
if ( ( 'publish' === $new_status && 'publish' !== $old_status ) || 'trash' === $new_status ) {
$this->purge_post_cache( $post->ID );
}
}
private function purge_post_cache( $post_id ) {
$post_ids = array_merge( [ $post_id ], $this->target_pages );
$method = new ReflectionMethod( $this->cache, 'purge_post_cache' );
$method->setAccessible( true );
foreach( $post_ids as $id ) {
$method->invoke( $this->cache, $id );
}
}
}
WPMUDEV_Hummingbrd_Custom_Cache_Clear::get();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment