Last active
August 30, 2019 04:43
-
-
Save wpmudev-sls/d2833d70d661907754000d9f0572b4b8 to your computer and use it in GitHub Desktop.
[Hummingbird Pro 2.1.0] - Disable Auto Clear Cache On Non-Public Post Type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [Hummingbird Pro] - Disable Auto Clear Cache On Non-Public Post Type - Required 2.1.0 | |
* Description: [Hummingbird Pro] - Disable auto clear cache on non-public post type, please note maybe the non-public post used in a shortcode in that case you must manually delete the page that contains it - 1130634756742664 | |
* Author: Thobk @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
add_action( 'plugins_loaded', 'wpmudev_hmbp_fix_auto_clear_page_cache_invisible_posttype_func', 100 ); | |
function wpmudev_hmbp_fix_auto_clear_page_cache_invisible_posttype_func() { | |
if( defined('WPHB_VERSION') && class_exists( 'Hummingbird\WP_Hummingbird' ) && class_exists('Hummingbird\Core\Settings') ){ | |
if( ! Hummingbird\Core\Settings::get_setting( 'enabled', 'page_cache' ) ){ | |
return; | |
} | |
// return; | |
global $wphb_cache_config; | |
if( empty( $wphb_cache_config->clear_on_update ) ) return; | |
class WPMUDEV_HMBP_Fix_Auto_Clear_Cache_Page{ | |
private static $_page_cache; | |
private static $_post_type_status = []; | |
private $checked_in_status = false; | |
private $exclude_post_types; | |
public function __construct(){ | |
self::$_page_cache = Hummingbird\Core\Utils::get_module( 'page_cache' ); | |
$this->exclude_post_types = (array) apply_filters( 'wpmudev_hmbp_page_cache_exclude_non_public_post_type', [] ); | |
remove_action( 'transition_post_status', array( self::$_page_cache, 'post_status_change' ), 10, 3 ); | |
add_action( 'transition_post_status', array( $this, 'post_status_change' ), 10, 3 ); | |
remove_action( 'edit_post', array( self::$_page_cache, 'post_edit' ), 0 ); | |
add_action( 'edit_post', array( $this, 'post_edit' ), 0, 2 ); | |
} | |
public function is_not_public_post_type( $post_type ){ | |
if( ! isset( self::$_post_type_status[ $post_type ] ) ){ | |
$publicly_queryable = false; | |
$post_type_obj = get_post_type_object( $post_type ); | |
if( $post_type_obj ){ | |
$publicly_queryable = $post_type_obj->publicly_queryable; | |
} | |
self::$_post_type_status[ $post_type ] = $publicly_queryable; | |
} | |
return ! self::$_post_type_status[ $post_type ]; | |
} | |
public function post_status_change( $new_status, $old_status, $post ){ | |
if( ( ! $this->exclude_post_types || ! in_array( $post_type, $this->exclude_post_types ) ) | |
&& $this->is_not_public_post_type( $post->post_type ) ) | |
{ | |
return; | |
} | |
self::$_page_cache->post_status_change( $new_status, $old_status, $post ); | |
} | |
public function post_edit( $post_id, $post ){ | |
if( ( ! $this->exclude_post_types || ! in_array( $post_type, $this->exclude_post_types ) ) | |
&& $this->is_not_public_post_type( $post->post_type ) ) | |
{ | |
return; | |
} | |
self::$_page_cache->post_edit( $post_id ); | |
} | |
} | |
$run = new WPMUDEV_HMBP_Fix_Auto_Clear_Cache_Page; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment