Skip to content

Commit 52c9e6e

Browse files
Merge feature/auto-purge-on-update-master into master
Add Auto-Purge Suggestion After WordPress Core, Plugin, or Theme Updates [master]
2 parents 4284c67 + a2ca46d commit 52c9e6e

File tree

6 files changed

+153
-4
lines changed

6 files changed

+153
-4
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ Replace the path with your own.
7676

7777
Yes. It handles all post-types the same way.
7878

79+
**Q. How can I purge cache automatically after WordPress/plugin/theme updates?**
80+
81+
By default, Nginx Helper does **not** purge cache on WordPress core, plugin, or theme updates.
82+
If you want this behavior, you can enable it using a filter:
83+
84+
`add_filter( 'rt_wp_nginx_helper_enable_auto_purge_on_any_update', '__return_true' );`
85+
86+
Once enabled, cache will be purged automatically whenever WordPress core, plugins, or themes are updated.
87+
If left disabled (default), Nginx Helper will instead show an admin notice after updates, reminding you to purge cache manually.
88+
7989
**Q. How do I know my Nginx config is correct for fastcgi purging?**
8090

8191
Manually purging any page from the cache, by following instructions in the previous answer.

admin/class-nginx-helper-admin.php

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,9 @@ public function nginx_helper_toolbar_purge_link( $wp_admin_bar ) {
223223

224224
$purge_url = add_query_arg(
225225
array(
226-
'nginx_helper_action' => 'purge',
227-
'nginx_helper_urls' => $nginx_helper_urls,
226+
'nginx_helper_action' => 'purge',
227+
'nginx_helper_urls' => $nginx_helper_urls,
228+
'nginx_helper_dismiss' => get_transient( 'rt_wp_nginx_helper_suggest_purge_notice' ),
228229
)
229230
);
230231

@@ -975,6 +976,80 @@ public function nginx_helper_update_role_caps() {
975976
}
976977
}
977978

979+
/**
980+
* Automatically purges Nginx cache on any WordPress core, plugin, or theme update if enabled.
981+
*
982+
* @param WP_Upgrader $upgrader_object WP_Upgrader instance.
983+
* @param array $options Array of bulk item update data.
984+
*/
985+
public function nginx_helper_auto_purge_on_any_update( $upgrader_object, $options ) {
986+
987+
if ( ! isset( $options['action'], $options['type'] )
988+
|| 'update' !== $options['action']
989+
|| ! in_array( $options['type'], array( 'core', 'plugin', 'theme' ), true ) ) {
990+
return;
991+
}
992+
if ( ! defined( 'NGINX_HELPER_AUTO_PURGE_ON_ANY_UPDATE' ) || ! NGINX_HELPER_AUTO_PURGE_ON_ANY_UPDATE ) {
993+
set_transient( 'rt_wp_nginx_helper_suggest_purge_notice', true, HOUR_IN_SECONDS );
994+
return;
995+
}
996+
global $nginx_purger;
997+
998+
$nginx_purger->purge_all();
999+
}
1000+
1001+
/**
1002+
* Displays an admin notice suggesting the user to purge cache after a WordPress update.
1003+
*/
1004+
public function suggest_purge_after_update() {
1005+
1006+
if ( ! get_transient( 'rt_wp_nginx_helper_suggest_purge_notice' ) ) {
1007+
return;
1008+
}
1009+
1010+
$setting_page = is_network_admin() ? 'settings.php' : 'options-general.php';
1011+
$settings_link = network_admin_url( $setting_page . '?page=nginx' );
1012+
$dismiss_url = wp_nonce_url( add_query_arg( 'nginx_helper_dismiss', 'true' ), 'nginx_helper_dismiss_notice' );
1013+
?>
1014+
<div class="notice notice-info">
1015+
<p>
1016+
<?php
1017+
esc_html_e( 'A WordPress update was detected. It is recommended to purge the cache to ensure your site displays the latest changes.', 'nginx-helper' );
1018+
?>
1019+
<a href="<?php echo esc_url( $settings_link ); ?>"><?php esc_html_e( 'Go & Purge Cache', 'nginx-helper' ); ?></a>
1020+
|
1021+
<a href="<?php echo esc_url( $dismiss_url ); ?>">
1022+
<?php esc_html_e( 'Dismiss', 'nginx-helper' ); ?>
1023+
</a>
1024+
</p>
1025+
</div>
1026+
<?php
1027+
}
1028+
1029+
/**
1030+
* Dismisses the "suggest purge" admin notice when the user clicks the dismiss link.
1031+
*/
1032+
public function dismiss_suggest_purge_after_update() {
1033+
1034+
if ( ! isset( $_GET['nginx_helper_dismiss'] ) || ! isset( $_GET['_wpnonce'] ) ) {
1035+
return;
1036+
}
1037+
1038+
$dismiss = sanitize_text_field( wp_unslash( $_GET['nginx_helper_dismiss'] ) );
1039+
$nonce = sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) );
1040+
1041+
// Verify the correct nonce depending on whether this is a purge+dismiss or dismiss-only request.
1042+
$has_purge_params = isset( $_GET['nginx_helper_action'], $_GET['nginx_helper_urls'] );
1043+
$nonce_verified = $has_purge_params ? wp_verify_nonce( $nonce, 'nginx_helper-purge_all' ) : wp_verify_nonce( $nonce, 'nginx_helper_dismiss_notice' );
1044+
1045+
if ( $dismiss && $nonce_verified ) {
1046+
1047+
delete_transient( 'rt_wp_nginx_helper_suggest_purge_notice' );
1048+
wp_safe_redirect( remove_query_arg( array( 'nginx_helper_dismiss', '_wpnonce' ) ) );
1049+
exit;
1050+
}
1051+
}
1052+
9781053
/**
9791054
* Initialize WooCommerce hooks if enabled.
9801055
*

admin/partials/nginx-helper-general-options.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,45 @@
766766
</td>
767767
</tr>
768768
</table>
769+
<table class="form-table rtnginx-table">
770+
<tr valign="top">
771+
<th scope="row">
772+
<h4><?php esc_html_e( 'Advance conditions:', 'nginx-helper' ); ?></h4>
773+
</th>
774+
<td>
775+
<label for="enable_auto_purge">
776+
<input
777+
disabled
778+
type="checkbox" id="enable_auto_purge" name="enable_auto_purge" <?php checked( NGINX_HELPER_AUTO_PURGE_ON_ANY_UPDATE, 1 ); ?> />
779+
&nbsp;
780+
<?php
781+
esc_html_e( 'Auto Purge when Core, Plugin or Theme updates.', 'nginx-helper' );
782+
?>
783+
</label>
784+
<p>
785+
<?php
786+
if ( NGINX_HELPER_AUTO_PURGE_ON_ANY_UPDATE ) {
787+
echo wp_kses_post(
788+
sprintf(
789+
/* translators: %1$s: Filter name 'rt_wp_nginx_helper_enable_auto_purge_on_any_update' */
790+
__( '(NOTE: This feature is enabled via the %1$s filter. To disable, remove this filter from your code.)', 'nginx-helper' ),
791+
'<strong>rt_wp_nginx_helper_enable_auto_purge_on_any_update</strong>'
792+
)
793+
);
794+
} else {
795+
echo wp_kses_post(
796+
sprintf(
797+
/* translators: %1$s: Filter name 'rt_wp_nginx_helper_enable_auto_purge_on_any_update' */
798+
__( '(NOTE: To enable, return true in the %1$s filter.)', 'nginx-helper' ),
799+
'<strong>rt_wp_nginx_helper_enable_auto_purge_on_any_update</strong>'
800+
)
801+
);
802+
}
803+
?>
804+
</p>
805+
</td>
806+
</tr>
807+
</table>
769808
</div> <!-- End of .inside -->
770809
</div>
771810
<div class="postbox">

admin/partials/nginx-helper-sidebar-display.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212

1313
$purge_url = add_query_arg(
1414
array(
15-
'nginx_helper_action' => 'purge',
16-
'nginx_helper_urls' => 'all',
15+
'nginx_helper_action' => 'purge',
16+
'nginx_helper_urls' => 'all',
17+
'nginx_helper_dismiss' => get_transient( 'rt_wp_nginx_helper_suggest_purge_notice' ),
1718
)
1819
);
1920
$nonced_url = wp_nonce_url( $purge_url, 'nginx_helper-purge_all' );

includes/class-nginx-helper.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ public function __construct() {
8989
define( 'RT_WP_NGINX_HELPER_CACHE_PATH', $cache_path );
9090
}
9191

92+
/**
93+
* Flag to automatically purge Nginx cache on any WordPress update (core, plugin, theme).
94+
* Set to true to enable auto-purge; false to disable.
95+
*/
96+
if ( ! defined( 'NGINX_HELPER_AUTO_PURGE_ON_ANY_UPDATE' ) ) {
97+
$enabled = (bool) apply_filters( 'rt_wp_nginx_helper_enable_auto_purge_on_any_update', false );
98+
define( 'NGINX_HELPER_AUTO_PURGE_ON_ANY_UPDATE', $enabled );
99+
}
100+
92101
$this->load_dependencies();
93102
$this->set_locale();
94103
$this->define_admin_hooks();
@@ -236,6 +245,11 @@ private function define_admin_hooks() {
236245
// add action to update purge caps.
237246
$this->loader->add_action( 'update_site_option_rt_wp_nginx_helper_options', $nginx_helper_admin, 'nginx_helper_update_role_caps' );
238247

248+
// advance purge settings.
249+
$this->loader->add_action( 'upgrader_process_complete', $nginx_helper_admin, 'nginx_helper_auto_purge_on_any_update', 10, 2 );
250+
$this->loader->add_action( 'admin_notices', $nginx_helper_admin, 'suggest_purge_after_update' );
251+
$this->loader->add_action( 'admin_init', $nginx_helper_admin, 'dismiss_suggest_purge_after_update' );
252+
239253
// WooCommerce integration.
240254
$this->loader->add_action( 'plugins_loaded', $nginx_helper_admin, 'init_woocommerce_hooks' );
241255

readme.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ Replace the path with your own.
6969

7070
Yes. It handles all post-types the same way.
7171

72+
**Q. How can I purge cache automatically after WordPress/plugin/theme updates?**
73+
74+
By default, Nginx Helper does **not** purge cache on WordPress core, plugin, or theme updates.
75+
If you want this behavior, you can enable it using a filter:
76+
77+
`add_filter( 'rt_wp_nginx_helper_enable_auto_purge_on_any_update', '__return_true' );`
78+
79+
Once enabled, cache will be purged automatically whenever WordPress core, plugins, or themes are updated.
80+
If left disabled (default), Nginx Helper will instead show an admin notice after updates, reminding you to purge cache manually.
81+
7282
**Q. How do I know my Nginx config is correct for fastcgi purging?**
7383

7484
Manually purging any page from the cache, by following instructions in the previous answer.

0 commit comments

Comments
 (0)