Skip to content

Commit f1ba559

Browse files
author
costdev
committed
Add AJAX handling for dismissing a notice.
1 parent 9bc5aa5 commit f1ba559

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/wp-admin/admin-ajax.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
'health-check-get-sizes',
142142
'toggle-auto-updates',
143143
'send-password-reset',
144+
'dismiss-notice',
144145
);
145146

146147
// Deprecated.

src/wp-admin/includes/ajax-actions.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5610,3 +5610,40 @@ function wp_ajax_send_password_reset() {
56105610
wp_send_json_error( $results->get_error_message() );
56115611
}
56125612
}
5613+
5614+
/**
5615+
* Handles dismissing a notice via AJAX.
5616+
*
5617+
* @since 6.5.0
5618+
*/
5619+
function wp_ajax_dismiss_notice() {
5620+
check_ajax_referer( 'dismiss-notice', 'nonce' );
5621+
5622+
if ( ! isset( $_POST['slug'] ) ) {
5623+
wp_send_json_error( __( 'Failed to dismiss notice: The notice does not have a slug.' ) );
5624+
}
5625+
5626+
$slug = trim( sanitize_text_field( $_POST['slug'] ) );
5627+
if ( '' === $slug ) {
5628+
wp_send_json_error( __( "Failed to dismiss notice: The notice's slug must not be an empty string." ) );
5629+
}
5630+
5631+
$expiration = 0;
5632+
if ( isset( $_POST['expiration'] ) ) {
5633+
if ( ! is_numeric( $_POST['expiration'] ) ) {
5634+
wp_send_json_error( __( 'Failed to dismiss notice: The expiration time must be a number of seconds.' ) );
5635+
}
5636+
5637+
$expiration = (int) $_POST['expiration'];
5638+
5639+
if ( 0 > $_POST['expiration'] ) {
5640+
wp_send_json_error( __( 'Failed to dismiss notice: The expiration time must be greater than or equal to 0.' ) );
5641+
}
5642+
}
5643+
5644+
if ( false === set_site_transient( "wp_admin_notice_dismissed_{$slug}", 1, $expiration ) ) {
5645+
wp_send_json_error( __( 'Failed to dismiss notice: The notice could not be dismissed.' ) );
5646+
}
5647+
5648+
wp_send_json_success( __( 'The notice was successfully dismissed.' ) );
5649+
}

0 commit comments

Comments
 (0)