Automattic\WooCommerce\Internal\PushNotifications\Controllers

PushNotificationRestController::createpublicWC 10.7.0

Processes the send request by delegating each notification to the processor.

Method of the class: PushNotificationRestController{}

No Hooks.

Returns

WP_REST_Response|WP_Error.

Usage

$PushNotificationRestController = new PushNotificationRestController();
$PushNotificationRestController->create( $request );
$request(WP_REST_Request) (required)
The request object.

Changelog

Since 10.7.0 Introduced.

PushNotificationRestController::create() code WC 10.9.1

public function create( WP_REST_Request $request ) {
	wc_set_time_limit( 30 );

	$body             = json_decode( $request->get_body(), true );
	$notifications    = is_array( $body ) ? ( $body['notifications'] ?? array() ) : array();
	$success_response = new WP_REST_Response( array( 'success' => true ), WP_Http::OK );

	if ( empty( $notifications ) || ! is_array( $notifications ) ) {
		wc_get_logger()->warning(
			'Loopback endpoint received empty or missing notifications array.',
			array( 'source' => PushNotifications::FEATURE_NAME )
		);

		return $success_response;
	}

	$processor = wc_get_container()->get( NotificationProcessor::class );

	foreach ( $notifications as $data ) {
		try {
			$notification = Notification::from_array( $data );
			$processor->process( $notification );
		} catch ( Exception $e ) {
			wc_get_logger()->error(
				sprintf( 'Failed to process notification: %s', $e->getMessage() ),
				array( 'source' => PushNotifications::FEATURE_NAME )
			);
		}
	}

	return $success_response;
}