Automattic\WooCommerce\Internal\PushNotifications\Services

NotificationProcessor::process │ public │ WC 10.7.0

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

Method of the class: NotificationProcessor{}

No Hooks.

Returns

bool. 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 11.3.0

public function process( Notification $notification, bool $is_retry = false, int $attempt = 0 ): bool {
	$step_context = array(
		'attempt'  => $attempt,
		'is_retry' => $is_retry,
	);

	/**
	 * This notification has already been sent - don't continue.
	 */
	if ( $notification->has_meta( self::SENT_META_KEY ) ) {
		$this->step_logger->log_notification_step( $notification, 'skipped', 'already_sent', $step_context );
		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 ) ) {
			$this->step_logger->log_notification_step( $notification, 'skipped', 'already_claimed', $step_context );
			return true;
		}

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

	/**
	 * Non-paginated result from get_tokens_for_roles.
	 *
	 * @var PushToken[] $eligible_tokens
	 */
	$eligible_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::get_suppression_reason()} so per-type preference
	 * shapes (simple bool today, parametrized arrays in the future) stay
	 * encapsulated alongside the type's resource access.
	 */
	list( $tokens, $suppression_reasons ) = $this->filter_tokens_by_preferences( $eligible_tokens, $notification );

	$this->log_recipients( $notification, $eligible_tokens, $tokens, $suppression_reasons, $step_context );

	/**
	 * 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 );
		$notification->reset_processing_meta();
		$this->cancel_safety_net( $notification );
		return true;
	}

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

	$this->log_send_outcome( $notification, $tokens, $result, $step_context );

	if ( ! empty( $result['success'] ) ) {
		// Success only, for the reason {@see PushToken::get_last_sent_at_gmt()} gives.
		$this->data_store->record_last_sent_at( $tokens );

		$notification->write_meta( self::SENT_META_KEY );
		$notification->reset_processing_meta();
		$this->cancel_safety_net( $notification );
		return true;
	}

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

	return false;
}