Automattic\WooCommerce\Internal\PushNotifications\Services

NotificationProcessor::handle_safety_netpublicWC 10.7.0

ActionScheduler callback for the safety net job. This will be scheduled for 60 seconds in the future when a notification is added to the PendingNotificationStore. If the initial send succeeds, or fails and is able to schedule a retry, this action will be unscheduled. If the initial send does not occur, or fails and cannot schedule a retry (e.g. out of memory, retry scheduling error) then this safety net will run.

Method of the class: NotificationProcessor{}

No Hooks.

Returns

null. Nothing (null).

Usage

$NotificationProcessor = new NotificationProcessor();
$NotificationProcessor->handle_safety_net( $type, $resource_id, $extra ): void;
$type(string) (required)
The notification type.
$resource_id(int) (required)
The resource ID.
$extra(array)
Optional subclass-specific extras (e.g. event_type, stock_quantity_at_trigger). Empty for notification types whose state is fully described by type + resource_id.
Default: array()

Changelog

Since 10.7.0 Introduced.

NotificationProcessor::handle_safety_net() code WC 10.9.1

public function handle_safety_net( string $type, int $resource_id, array $extra = array() ): void {
	try {
		// Use the `+` array union operator (not array_merge) so the positional
		// $type and $resource_id always win over any colliding keys in $extra.
		// Defends against a malformed payload reconstructing the wrong target.
		$data = array(
			'type'        => $type,
			'resource_id' => $resource_id,
		) + $extra;

		$notification = Notification::from_array( $data );
	} catch ( Exception $e ) {
		wc_get_logger()->error(
			sprintf( 'Safety net failed: %s', $e->getMessage() ),
			array( 'source' => PushNotifications::FEATURE_NAME )
		);
		return;
	}

	try {
		$this->process( $notification, true );
	} catch ( Exception $e ) {
		wc_get_logger()->error(
			sprintf( 'Safety net failed: %s', $e->getMessage() ),
			array( 'source' => PushNotifications::FEATURE_NAME )
		);
		$this->retry_handler->schedule( $notification, null, 0 );
	}
}