From 66c2f5bb4b47dd35ac7cc9371ae9ba407061be14 Mon Sep 17 00:00:00 2001 From: Imran Sayed Date: Tue, 12 Feb 2019 19:15:31 +0530 Subject: [PATCH 1/5] Change Purge Cache button name and 'nginx-helper_urls' query argument value on front-end admin bar. Conditionally display the purge cache button name as 'Purge Cache' for admin dashboard and 'Purge Current Page' on front-end admin bar. Conditionally set query argument 'nginx_helper_urls' value to 'all' for admin dashaboard purge cache button and 'current-url' for front-end admin bar button --- admin/class-nginx-helper-admin.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/admin/class-nginx-helper-admin.php b/admin/class-nginx-helper-admin.php index d4d2963b..197663a6 100644 --- a/admin/class-nginx-helper-admin.php +++ b/admin/class-nginx-helper-admin.php @@ -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, ) ); @@ -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' => array( 'title' => $link_title ) ), ) ); From b2c2cbef0f496ea974e8555f62aecdd8f3bce063 Mon Sep 17 00:00:00 2001 From: Imran Sayed Date: Tue, 12 Feb 2019 22:23:02 +0530 Subject: [PATCH 2/5] Purge current page cache on front end 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. --- admin/class-nginx-helper-admin.php | 17 +++++++++++++++-- includes/class-nginx-helper.php | 2 +- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/admin/class-nginx-helper-admin.php b/admin/class-nginx-helper-admin.php index 197663a6..4f77c4e6 100644 --- a/admin/class-nginx-helper-admin.php +++ b/admin/class-nginx-helper-admin.php @@ -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 ); @@ -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(); } diff --git a/includes/class-nginx-helper.php b/includes/class-nginx-helper.php index 28ce4bb1..93a05cf7 100644 --- a/includes/class-nginx-helper.php +++ b/includes/class-nginx-helper.php @@ -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' ); From a2703880e7cf973f1c7d526d5f3c6813198c1dd3 Mon Sep 17 00:00:00 2001 From: Imran Sayed Date: Tue, 12 Feb 2019 22:32:12 +0530 Subject: [PATCH 3/5] Add purge_all() header comment --- admin/class-nginx-helper-admin.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/admin/class-nginx-helper-admin.php b/admin/class-nginx-helper-admin.php index 4f77c4e6..3b022fc5 100644 --- a/admin/class-nginx-helper-admin.php +++ b/admin/class-nginx-helper-admin.php @@ -650,6 +650,8 @@ 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 */ From 257be95b17a978071a7196c20e45af983651404f Mon Sep 17 00:00:00 2001 From: Imran Sayed Date: Tue, 12 Feb 2019 22:33:32 +0530 Subject: [PATCH 4/5] Change the name of the action to 'purge_current_page' for front-end purging request --- admin/class-nginx-helper-admin.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/admin/class-nginx-helper-admin.php b/admin/class-nginx-helper-admin.php index 3b022fc5..69dff8c3 100644 --- a/admin/class-nginx-helper-admin.php +++ b/admin/class-nginx-helper-admin.php @@ -688,7 +688,7 @@ public function purge_all() { $current_url = esc_url_raw( user_trailingslashit( home_url( $wp->request ) ) ); if ( ! is_admin() ) { - $action = 'purge_single_page'; + $action = 'purge_current_page'; $redirect_url = $current_url; } else { $redirect_url = add_query_arg( array( 'nginx_helper_action' => 'done' ) ); @@ -698,7 +698,7 @@ public function purge_all() { case 'purge': $nginx_purger->purge_all(); break; - case 'purge_single_page': + case 'purge_current_page': $nginx_purger->purge_url( $current_url ); break; } From 57f094fe6bf6dce0838395e7c81b265241ac12ad Mon Sep 17 00:00:00 2001 From: Imran Sayed Date: Mon, 25 Feb 2019 18:54:32 +0530 Subject: [PATCH 5/5] Put correct value for meta key and remove escaping where not required Put the correct value for the 'meta key' of add_menu() and remove escaping from purge_url() on line 688 as already escaped at the end --- admin/class-nginx-helper-admin.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/admin/class-nginx-helper-admin.php b/admin/class-nginx-helper-admin.php index 69dff8c3..31dd8f27 100644 --- a/admin/class-nginx-helper-admin.php +++ b/admin/class-nginx-helper-admin.php @@ -223,7 +223,7 @@ public function nginx_helper_toolbar_purge_link( $wp_admin_bar ) { 'id' => 'nginx-helper-purge-all', 'title' => $link_title, 'href' => $nonced_url, - 'meta' => array( 'title' => array( 'title' => $link_title ) ), + 'meta' => array( 'title' => $link_title ), ) ); @@ -685,7 +685,7 @@ public function purge_all() { check_admin_referer( 'nginx_helper-purge_all' ); - $current_url = esc_url_raw( user_trailingslashit( home_url( $wp->request ) ) ); + $current_url = user_trailingslashit( home_url( $wp->request ) ); if ( ! is_admin() ) { $action = 'purge_current_page';