Skip to content

Commit

Permalink
Purge current page cache on front end
Browse files Browse the repository at this point in the history
Change hook from 'admin_init' to 'admin_bar_init' so that purge_all() is called when purge button is clicked on both admin dashboard and front end admin bar.
Check if the user is on front end when set the call purge_url() and purge the current page cache on case basis.
  • Loading branch information
imranhsayed committed Feb 12, 2019
1 parent 66c2f5b commit b2c2cbe
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
17 changes: 15 additions & 2 deletions admin/class-nginx-helper-admin.php
Expand Up @@ -655,7 +655,7 @@ public function update_new_blog_options( $blog_id ) {
*/
public function purge_all() {

global $nginx_purger;
global $nginx_purger, $wp;

$method = filter_input( INPUT_SERVER, 'REQUEST_METHOD', FILTER_SANITIZE_STRING );

Expand All @@ -682,13 +682,26 @@ public function purge_all() {
}

check_admin_referer( 'nginx_helper-purge_all' );

$current_url = esc_url_raw( user_trailingslashit( home_url( $wp->request ) ) );

if ( ! is_admin() ) {
$action = 'purge_single_page';
$redirect_url = $current_url;
} else {
$redirect_url = add_query_arg( array( 'nginx_helper_action' => 'done' ) );
}

switch ( $action ) {
case 'purge':
$nginx_purger->purge_all();
break;
case 'purge_single_page':
$nginx_purger->purge_url( $current_url );
break;
}

wp_redirect( esc_url_raw( add_query_arg( array( 'nginx_helper_action' => 'done' ) ) ) );
wp_redirect( esc_url_raw( $redirect_url ) );
exit();

}
Expand Down
2 changes: 1 addition & 1 deletion includes/class-nginx-helper.php
Expand Up @@ -221,7 +221,7 @@ private function define_admin_hooks() {
$this->loader->add_action( 'edit_term', $nginx_purger, 'purge_on_term_taxonomy_edited', 20, 3 );
$this->loader->add_action( 'delete_term', $nginx_purger, 'purge_on_term_taxonomy_edited', 20, 3 );
$this->loader->add_action( 'check_ajax_referer', $nginx_purger, 'purge_on_check_ajax_referer', 20 );
$this->loader->add_action( 'admin_init', $nginx_helper_admin, 'purge_all' );
$this->loader->add_action( 'admin_bar_init', $nginx_helper_admin, 'purge_all' );

// expose action to allow other plugins to purge the cache.
$this->loader->add_action( 'rt_nginx_helper_purge_all', $nginx_purger, 'purge_all' );
Expand Down

0 comments on commit b2c2cbe

Please sign in to comment.