Automattic\WooCommerce\Internal\PushNotifications\Dispatchers
InternalNotificationDispatcher::dispatch
JSON-encodes notifications and fires a non-blocking POST to the internal REST endpoint.
Method of the class: InternalNotificationDispatcher{}
No Hooks.
Returns
null. Nothing (null).
Usage
$InternalNotificationDispatcher = new InternalNotificationDispatcher(); $InternalNotificationDispatcher->dispatch( $notifications ): void;
- $notifications(Notification[]) (required)
- The notifications to dispatch.
Changelog
| Since 10.7.0 | Introduced. |
InternalNotificationDispatcher::dispatch() InternalNotificationDispatcher::dispatch code WC 10.8.1
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 ) {
wc_get_logger()->error(
'Failed to JSON-encode push notification payload.',
array( 'source' => PushNotifications::FEATURE_NAME )
);
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.
*/
wp_remote_post(
rest_url( self::SEND_ENDPOINT ),
array(
'blocking' => false,
'timeout' => 1,
'headers' => array(
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $token,
),
'body' => $body,
)
);
}