Skip to content

Commit

Permalink
Merge pull request #173 from imranhsayed/fix/front-end-page-cache
Browse files Browse the repository at this point in the history
Fix Purge Cache menu caching issue in the front-end
  • Loading branch information
chandrapatel authored Feb 25, 2019
2 parents 2561d04 + 57f094f commit 377c2c1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
33 changes: 28 additions & 5 deletions admin/class-nginx-helper-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,18 @@ public function nginx_helper_toolbar_purge_link( $wp_admin_bar ) {
return;
}

if ( is_admin() ) {
$nginx_helper_urls = 'all';
$link_title = __( 'Purge Cache', 'nginx-helper' );
} else {
$nginx_helper_urls = 'current-url';
$link_title = __( 'Purge Current Page', 'nginx-helper' );
}

$purge_url = add_query_arg(
array(
'nginx_helper_action' => 'purge',
'nginx_helper_urls' => 'all',
'nginx_helper_urls' => $nginx_helper_urls,
)
);

Expand All @@ -213,9 +221,9 @@ public function nginx_helper_toolbar_purge_link( $wp_admin_bar ) {
$wp_admin_bar->add_menu(
array(
'id' => 'nginx-helper-purge-all',
'title' => __( 'Purge Cache', 'nginx-helper' ),
'title' => $link_title,
'href' => $nonced_url,
'meta' => array( 'title' => __( 'Purge Cache', 'nginx-helper' ) ),
'meta' => array( 'title' => $link_title ),
)
);

Expand Down Expand Up @@ -642,12 +650,14 @@ public function update_new_blog_options( $blog_id ) {

/**
* Purge all urls.
* Purge current page cache when purging is requested from front
* and all urls when requested from admin dashboard.
*
* @global object $nginx_purger
*/
public function purge_all() {

global $nginx_purger;
global $nginx_purger, $wp;

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

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

check_admin_referer( 'nginx_helper-purge_all' );

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

if ( ! is_admin() ) {
$action = 'purge_current_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_current_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
Original file line number Diff line number Diff line change
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 377c2c1

Please sign in to comment.