WC_Admin_Notices::ajax_hide_notice │ public static │ WC 11.1.0

AJAX handler to hide a notice without a page reload.

Accepts the same nonce as the query-string based dismissal in hide_notices(), which is kept as a no-JS fallback.

Method of the class: WC_Admin_Notices{}

Returns

void. Nothing (null).

Usage

$result = WC_Admin_Notices::ajax_hide_notice();

Changelog

Since 11.1.0 Introduced.

WC_Admin_Notices::ajax_hide_notice() code WC 11.1.2

public static function ajax_hide_notice() {
	check_ajax_referer( 'woocommerce_hide_notices_nonce', '_wc_notice_nonce' );

	$notice_name = isset( $_POST['wc-hide-notice'] ) ? sanitize_text_field( wp_unslash( $_POST['wc-hide-notice'] ) ) : '';

	if ( '' === $notice_name ) {
		wp_send_json_error( null, 400 );
	}

	/**
	 * This filter is documented above, in hide_notices().
	 *
	 * @since 6.7.0
	 */
	$required_capability = apply_filters( 'woocommerce_dismiss_admin_notice_capability', 'manage_woocommerce', $notice_name );

	if ( ! current_user_can( $required_capability ) ) {
		wp_send_json_error( null, 403 );
	}

	self::hide_notice( $notice_name );

	// Notices are normally stored to the DB on 'shutdown'; save explicitly so the dismissal is persisted even if that hook was not registered for this request.
	self::store_notices();

	wp_send_json_success();
}