Automattic\WooCommerce\Internal\PushNotifications\Services

NotificationProcessor::processpublicWC 10.7.0

Processes a single notification: checks meta, sends to WPCOM, marks sent.

Method of the class: NotificationProcessor{}

No Hooks.

Returns

true|false. True if successfully sent (or already sent).

Usage

$NotificationProcessor = new NotificationProcessor();
$NotificationProcessor->process( $notification, $is_retry, $attempt ): bool;
$notification(Notification) (required)
The notification to process.
$is_retry(true|false)
Whether this is a retry or safety net attempt.
Default: false
$attempt(int)
The current attempt number (0 = first attempt).

Changelog

Since 10.7.0 Introduced.

NotificationProcessor::process() code WC 10.9.1

public function process( Notification $notification, bool $is_retry = false, int $attempt = 0 ): bool {
	/**
	 * This notification has already been sent - don't continue.
	 */
	if ( $notification->has_meta( self::SENT_META_KEY ) ) {
		return true;
	}

	if ( ! $is_retry ) {
		/**
		 * This notification has already been claimed for sending, and since
		 * this is not a retry, this is not expected and means some other
		 * process is handling the notification (e.g. race condition) -
		 * don't continue.
		 */
		if ( $notification->has_meta( self::CLAIMED_META_KEY ) ) {
			return true;
		}

		$notification->write_meta( self::CLAIMED_META_KEY );
	}

	/**
	 * Non-paginated result from get_tokens_for_roles.
	 *
	 * @var PushToken[] $tokens
	 */
	$tokens = $this->data_store->get_tokens_for_roles(
		PushNotifications::ROLES_WITH_PUSH_NOTIFICATIONS_ENABLED
	);

	/**
	 * Filter out tokens whose owning user does not want this notification.
	 * The decision is delegated to the notification itself via
	 * {@see Notification::should_send_to_user()} so per-type preference
	 * shapes (simple bool today, parametrized arrays in the future) stay
	 * encapsulated alongside the type's resource access.
	 */
	$tokens = $this->filter_tokens_by_preferences( $tokens, $notification );

	/**
	 * There are no recipients to send to (either no tokens at all, or
	 * every owning user opted out of this notification type). We don't
	 * want to retry as this isn't a 'recoverable error', so mark as sent
	 * and return.
	 */
	if ( empty( $tokens ) ) {
		$notification->write_meta( self::SENT_META_KEY );
		$this->cancel_safety_net( $notification );
		return true;
	}

	$result = $this->dispatcher->dispatch( $notification, $tokens );

	if ( ! empty( $result['success'] ) ) {
		$notification->write_meta( self::SENT_META_KEY );
		$notification->delete_meta( self::CLAIMED_META_KEY );
		$this->cancel_safety_net( $notification );
		return true;
	}

	$this->retry_handler->schedule( $notification, $result['retry_after'] ?? null, $attempt );
	$this->cancel_safety_net( $notification );

	return false;
}