is_connected() ) {
add_action( 'admin_bar_menu', array( $this, 'admin_bar_menu' ), 100 );
add_action( 'wp_footer', array( $this, 'admin_bar_script' ), 100 );
add_action( 'admin_footer', array( $this, 'admin_bar_script' ), 100 );
}
if ( ! $this->should_auto_purge() ) {
return;
}
// Post hooks.
add_action( 'save_post', array( $this, 'on_post_save' ), 20, 2 );
add_action( 'delete_post', array( $this, 'on_post_delete' ), 20 );
add_action( 'trashed_post', array( $this, 'on_post_delete' ), 20 );
add_action( 'transition_post_status', array( $this, 'on_status_change' ), 20, 3 );
// Term hooks.
add_action( 'edited_term', array( $this, 'on_term_edit' ), 20, 3 );
add_action( 'delete_term', array( $this, 'on_term_delete' ), 20, 4 );
// Comment hooks.
add_action( 'comment_post', array( $this, 'on_comment' ), 20, 2 );
add_action( 'edit_comment', array( $this, 'on_comment_edit' ), 20 );
add_action( 'transition_comment_status', array( $this, 'on_comment_status' ), 20, 3 );
// Process pending purges on shutdown.
add_action( 'shutdown', array( $this, 'process_pending_purges' ) );
}
/**
* Check if CF is connected.
*
* @return bool
*/
private function is_connected() {
return (bool) get_option( 'ajaxpress_cf_cache_enabled', false );
}
/**
* Add admin bar menu.
*
* @param WP_Admin_Bar $wp_admin_bar Admin bar instance.
*/
public function admin_bar_menu( $wp_admin_bar ) {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
// Parent menu (icon only).
$wp_admin_bar->add_node(
array(
'id' => 'ajaxpress-cache',
'title' => '',
'href' => '#',
'meta' => array(
'title' => 'AjaxPress Cache',
),
)
);
// Purge current URL.
$current_url = is_admin() ? home_url( '/' ) : $this->get_current_url();
$wp_admin_bar->add_node(
array(
'id' => 'ajaxpress-purge-url',
'parent' => 'ajaxpress-cache',
'title' => 'Purge This Page',
'href' => '#',
'meta' => array(
'onclick' => 'return ajaxpressPurge("url", "' . esc_js( $current_url ) . '");',
),
)
);
// Purge all.
$wp_admin_bar->add_node(
array(
'id' => 'ajaxpress-purge-all',
'parent' => 'ajaxpress-cache',
'title' => 'Purge Everything',
'href' => '#',
'meta' => array(
'onclick' => 'return ajaxpressPurge("all");',
),
)
);
}
/**
* Admin bar inline script for AJAX purge.
*/
public function admin_bar_script() {
if ( ! current_user_can( 'manage_options' ) || ! is_admin_bar_showing() ) {
return;
}
?>
post_status ) {
return;
}
$this->queue_post_purge( $post_id );
}
/**
* Handle post deletion.
*
* @param int $post_id Post ID.
*/
public function on_post_delete( $post_id ) {
$this->queue_post_purge( $post_id );
}
/**
* Handle post status transition.
*
* @param string $new_status New status.
* @param string $old_status Old status.
* @param WP_Post $post Post object.
*/
public function on_status_change( $new_status, $old_status, $post ) {
// Purge when publishing or unpublishing.
if ( 'publish' === $new_status || 'publish' === $old_status ) {
$this->queue_post_purge( $post->ID );
}
}
/**
* Queue URLs for a post.
*
* @param int $post_id Post ID.
*/
private function queue_post_purge( $post_id ) {
$cache = new Cache();
$urls = $cache->get_post_urls( $post_id );
foreach ( $urls as $url ) {
self::$pending_urls[ $url ] = true;
}
}
/**
* Handle term edit.
*
* @param int $term_id Term ID.
* @param int $tt_id Term taxonomy ID.
* @param string $taxonomy Taxonomy slug.
*/
public function on_term_edit( $term_id, $tt_id, $taxonomy ) {
$this->queue_term_purge( $term_id, $taxonomy );
}
/**
* Handle term deletion.
*
* @param int $term_id Term ID.
* @param int $tt_id Term taxonomy ID.
* @param string $taxonomy Taxonomy slug.
* @param mixed $deleted_term Deleted term object.
*/
public function on_term_delete( $term_id, $tt_id, $taxonomy, $deleted_term ) {
// Home page.
self::$pending_urls[ home_url( '/' ) ] = true;
}
/**
* Queue URLs for a term.
*
* @param int $term_id Term ID.
* @param string $taxonomy Taxonomy slug.
*/
private function queue_term_purge( $term_id, $taxonomy ) {
$term_link = get_term_link( $term_id, $taxonomy );
if ( ! is_wp_error( $term_link ) ) {
self::$pending_urls[ $term_link ] = true;
}
// Home page.
self::$pending_urls[ home_url( '/' ) ] = true;
}
/**
* Handle new comment.
*
* @param int $comment_id Comment ID.
* @param int|string $comment_approved Approval status.
*/
public function on_comment( $comment_id, $comment_approved ) {
if ( 1 === $comment_approved ) {
$comment = get_comment( $comment_id );
if ( $comment ) {
$this->queue_post_purge( $comment->comment_post_ID );
}
}
}
/**
* Handle comment edit.
*
* @param int $comment_id Comment ID.
*/
public function on_comment_edit( $comment_id ) {
$comment = get_comment( $comment_id );
if ( $comment && '1' === $comment->comment_approved ) {
$this->queue_post_purge( $comment->comment_post_ID );
}
}
/**
* Handle comment status transition.
*
* @param string $new_status New status.
* @param string $old_status Old status.
* @param WP_Comment $comment Comment object.
*/
public function on_comment_status( $new_status, $old_status, $comment ) {
if ( 'approved' === $new_status || 'approved' === $old_status ) {
$this->queue_post_purge( $comment->comment_post_ID );
}
}
/**
* Process pending purges on shutdown.
*/
public function process_pending_purges() {
if ( empty( self::$pending_urls ) ) {
return;
}
$urls = array_keys( self::$pending_urls );
$cache = new Cache();
// If too many URLs, purge everything instead.
if ( count( $urls ) > 25 ) {
$cache->purge_all();
} else {
$cache->purge_urls( $urls );
}
self::$pending_urls = array();
}
}
Hooks::start();
}