Skip to content

Commit 377c2c1

Browse files
authored
Merge pull request #173 from imranhsayed/fix/front-end-page-cache
Fix Purge Cache menu caching issue in the front-end
2 parents 2561d04 + 57f094f commit 377c2c1

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

admin/class-nginx-helper-admin.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,18 @@ public function nginx_helper_toolbar_purge_link( $wp_admin_bar ) {
201201
return;
202202
}
203203

204+
if ( is_admin() ) {
205+
$nginx_helper_urls = 'all';
206+
$link_title = __( 'Purge Cache', 'nginx-helper' );
207+
} else {
208+
$nginx_helper_urls = 'current-url';
209+
$link_title = __( 'Purge Current Page', 'nginx-helper' );
210+
}
211+
204212
$purge_url = add_query_arg(
205213
array(
206214
'nginx_helper_action' => 'purge',
207-
'nginx_helper_urls' => 'all',
215+
'nginx_helper_urls' => $nginx_helper_urls,
208216
)
209217
);
210218

@@ -213,9 +221,9 @@ public function nginx_helper_toolbar_purge_link( $wp_admin_bar ) {
213221
$wp_admin_bar->add_menu(
214222
array(
215223
'id' => 'nginx-helper-purge-all',
216-
'title' => __( 'Purge Cache', 'nginx-helper' ),
224+
'title' => $link_title,
217225
'href' => $nonced_url,
218-
'meta' => array( 'title' => __( 'Purge Cache', 'nginx-helper' ) ),
226+
'meta' => array( 'title' => $link_title ),
219227
)
220228
);
221229

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

643651
/**
644652
* Purge all urls.
653+
* Purge current page cache when purging is requested from front
654+
* and all urls when requested from admin dashboard.
645655
*
646656
* @global object $nginx_purger
647657
*/
648658
public function purge_all() {
649659

650-
global $nginx_purger;
660+
global $nginx_purger, $wp;
651661

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

@@ -674,13 +684,26 @@ public function purge_all() {
674684
}
675685

676686
check_admin_referer( 'nginx_helper-purge_all' );
687+
688+
$current_url = user_trailingslashit( home_url( $wp->request ) );
689+
690+
if ( ! is_admin() ) {
691+
$action = 'purge_current_page';
692+
$redirect_url = $current_url;
693+
} else {
694+
$redirect_url = add_query_arg( array( 'nginx_helper_action' => 'done' ) );
695+
}
696+
677697
switch ( $action ) {
678698
case 'purge':
679699
$nginx_purger->purge_all();
680700
break;
701+
case 'purge_current_page':
702+
$nginx_purger->purge_url( $current_url );
703+
break;
681704
}
682705

683-
wp_redirect( esc_url_raw( add_query_arg( array( 'nginx_helper_action' => 'done' ) ) ) );
706+
wp_redirect( esc_url_raw( $redirect_url ) );
684707
exit();
685708

686709
}

includes/class-nginx-helper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ private function define_admin_hooks() {
221221
$this->loader->add_action( 'edit_term', $nginx_purger, 'purge_on_term_taxonomy_edited', 20, 3 );
222222
$this->loader->add_action( 'delete_term', $nginx_purger, 'purge_on_term_taxonomy_edited', 20, 3 );
223223
$this->loader->add_action( 'check_ajax_referer', $nginx_purger, 'purge_on_check_ajax_referer', 20 );
224-
$this->loader->add_action( 'admin_init', $nginx_helper_admin, 'purge_all' );
224+
$this->loader->add_action( 'admin_bar_init', $nginx_helper_admin, 'purge_all' );
225225

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

0 commit comments

Comments
 (0)