Skip to content

Commit ff2b4a7

Browse files
Merge feature/auto-purge-on-update into develop
Add Auto-Purge Suggestion After WordPress Core, Plugin, or Theme Updates [develop]
2 parents d568e93 + 4a33d62 commit ff2b4a7

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

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

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

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

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' );
247+
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' );
238252
}
239253

240254
/**

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)