@@ -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}
0 commit comments