Skip to content

Commit 64e7217

Browse files
feat: add auto purge on update feature
1 parent 4284c67 commit 64e7217

File tree

4 files changed

+121
-4
lines changed

4 files changed

+121
-4
lines changed

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( '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( '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( '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+
echo esc_html__( '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( '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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,33 @@
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" value="1" 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( __( '(NOTE: This feature is enabled via the <strong>rt_wp_nginx_helper_enable_auto_purge_on_any_update</strong> filter. To disable, remove this filter from your code.)', 'nginx-helper' ) );
788+
} else {
789+
echo wp_kses_post( __( '(NOTE: To enable, return true in the <strong>rt_wp_nginx_helper_enable_auto_purge_on_any_update</strong> filter.)', 'nginx-helper' ) );
790+
}
791+
?>
792+
</p>
793+
</td>
794+
</tr>
795+
</table>
769796
</div> <!-- End of .inside -->
770797
</div>
771798
<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( '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

0 commit comments

Comments
 (0)