Automator_Notifications::dismiss()

Dismiss notification via AJAX.


Source Source

File: src/core/admin/notifications/notifications.php

	public function dismiss() {
		// Run a security check.
		if ( ! wp_verify_nonce( automator_filter_input( 'nonce', INPUT_POST ), 'uncanny_automator' ) ) {
			return;
		}
		$notification_id = automator_filter_input( 'id', INPUT_POST );
		// Check for access and required param.
		if ( ! $this->has_access() || empty( $notification_id ) ) {
			wp_send_json_error();
		}
		$id = sanitize_text_field( wp_unslash( $notification_id ) );
		$option = $this->get_option();
		// Dismiss all notifications and add them to dissmiss array.
		if ( 'all' === $id ) {
			if ( is_array( $option['feed'] ) && ! empty( $option['feed'] ) ) {
				foreach ( $option['feed'] as $key => $notification ) {
					array_unshift( $option['dismissed'], $notification );
					unset( $option['feed'][ $key ] );
				}
			}
			if ( is_array( $option['events'] ) && ! empty( $option['events'] ) ) {
				foreach ( $option['events'] as $key => $notification ) {
					array_unshift( $option['dismissed'], $notification );
					unset( $option['events'][ $key ] );
				}
			}
		}
		$type = is_numeric( $id ) ? 'feed' : 'events';
		// Remove notification and add in dismissed array.
		if ( is_array( $option[ $type ] ) && ! empty( $option[ $type ] ) ) {
			foreach ( $option[ $type ] as $key => $notification ) {
				if ( $notification['id'] == $id ) { // phpcs:ignore WordPress.PHP.StrictComparisons
					// Add notification to dismissed array.
					array_unshift( $option['dismissed'], $notification );
					// Remove notification from feed or events.
					unset( $option[ $type ][ $key ] );
					break;
				}
			}
		}
		update_option( $this->option_name, $option, false );
		wp_send_json_success();
	}


Top ↑

Changelog Changelog

Changelog
VersionDescription
{VERSION}Introduced.