Automattic\WooCommerce\Internal\PushNotifications\Dispatchers

InternalNotificationDispatcher{} │ WC 10.7.0

Fires a non-blocking POST to the internal REST endpoint with JSON-encoded notification data and a signed JWT.

Called directly by PendingNotificationStore::dispatch_all() on shutdown.

Хуков нет.

Использование

$InternalNotificationDispatcher = new InternalNotificationDispatcher();
// use class methods

Методы

  1. public dispatch( array $notifications )
  2. public init( NotificationStepLogger $step_logger )

Список изменений

С версии 10.7.0 Введена.

Код InternalNotificationDispatcher{} WC 11.3.0

class InternalNotificationDispatcher {

	/**
	 * REST route for the send endpoint.
	 */
	const SEND_ENDPOINT = 'wc-push-notifications/send';

	/**
	 * Query parameter carrying the JWT as a fallback credential.
	 *
	 * Some hosting setups (e.g. nginx to PHP-FPM, or Plesk's nginx-to-Apache
	 * proxying) do not pass the `Authorization` header through to PHP. On
	 * those hosts a header-only credential silently fails and every
	 * notification degrades to the slower ActionScheduler safety net. The
	 * same token is therefore also sent in the URL, where nothing strips it.
	 */
	const TOKEN_QUERY_PARAM = 'wcpn_token';

	/**
	 * JWT expiry in seconds.
	 */
	const JWT_EXPIRY_SECONDS = 30;

	/**
	 * The step logger.
	 *
	 * @var NotificationStepLogger
	 */
	private NotificationStepLogger $step_logger;

	/**
	 * Initialize injected dependencies.
	 *
	 * @internal
	 *
	 * @param NotificationStepLogger $step_logger The step logger.
	 *
	 * @since 11.3.0
	 */
	final public function init( NotificationStepLogger $step_logger ): void {
		$this->step_logger = $step_logger;
	}

	/**
	 * JSON-encodes notifications and fires a non-blocking POST to the internal
	 * REST endpoint.
	 *
	 * @param Notification[] $notifications The notifications to dispatch.
	 * @return void
	 *
	 * @since 10.7.0
	 */
	public function dispatch( array $notifications ): void {
		if ( empty( $notifications ) ) {
			return;
		}

		$encoded = array_map( fn ( Notification $notification ) => $notification->to_array(), $notifications );
		$body    = wp_json_encode( array( 'notifications' => $encoded ) );

		if ( false === $body ) {
			foreach ( $notifications as $notification ) {
				$this->step_logger->log_failure(
					$notification,
					'loopback_requested',
					'encode_failed',
					'error',
					'Failed to JSON-encode push notification payload.',
					array( 'batch_size' => count( $notifications ) )
				);
			}
			return;
		}

		$token = JsonWebToken::create(
			array(
				'iss'       => get_site_url(),
				'exp'       => time() + self::JWT_EXPIRY_SECONDS,
				'body_hash' => hash( 'sha256', $body ),
			),
			wp_salt( 'auth' )
		);

		/**
		 * The request is non-blocking so the response is not handled anywhere.
		 * If the request fails, the ActionScheduler safety net will pick up
		 * unsent notifications after 60 seconds.
		 *
		 * The token travels both as an Authorization header and as a query
		 * parameter: the receiver prefers the header and falls back to the
		 * parameter on hosts that strip the header (see TOKEN_QUERY_PARAM).
		 */
		$response = wp_remote_post(
			add_query_arg( self::TOKEN_QUERY_PARAM, $token, rest_url( self::SEND_ENDPOINT ) ),
			array(
				'blocking' => false,
				'timeout'  => 1,
				'headers'  => array(
					'Content-Type'  => 'application/json',
					'Authorization' => 'Bearer ' . $token,
				),
				'body'     => $body,
			)
		);

		/*
		 * One request carries the whole batch, so one line records it, naming
		 * each notification so a per-notification read can find it. The
		 * identifier is carried as well as the type and resource ID, because a
		 * subclass may build it from more: two stock events for one product
		 * share a type and a resource ID and differ only there.
		 */
		$context = array(
			'batch_size'    => count( $notifications ),
			'notifications' => array_map(
				fn( Notification $notification ) => array(
					'type'        => $notification->get_type(),
					'resource_id' => $notification->get_resource_id(),
					'identifier'  => $notification->get_identifier(),
				),
				$notifications
			),
		);

		if ( is_wp_error( $response ) ) {
			$this->step_logger->log_unattributed_failure(
				'loopback_requested',
				'request_failed',
				'warning',
				sprintf( 'Loopback request failed: %s', $response->get_error_message() ),
				$context
			);

			return;
		}

		$this->step_logger->log_batch_step( 'loopback_requested', 'ok', $context );
	}
}