Automattic\WooCommerce\Internal\PushNotifications\Controllers

PushNotificationRestController::create │ public │ WC 10.7.0

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

Метод класса: PushNotificationRestController{}

Хуков нет.

Возвращает

WP_REST_Response|WP_Error.

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

$PushNotificationRestController = new PushNotificationRestController();
$PushNotificationRestController->create( $request );
$request(WP_REST_Request) (обязательный)
The request object.

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

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

Код PushNotificationRestController::create() WC 11.3.0

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 );

	$step_logger = wc_get_container()->get( NotificationStepLogger::class );

	if ( empty( $notifications ) || ! is_array( $notifications ) ) {
		$step_logger->log_unattributed_failure(
			'loopback_started',
			'malformed_body',
			'warning',
			'Loopback endpoint received empty or missing notifications array.'
		);

		return $success_response;
	}

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

	foreach ( $notifications as $data ) {
		try {
			$notification = Notification::from_array( $data );
		} catch ( Exception $e ) {
			$step_logger->log_unattributed_failure(
				'loopback_started',
				'invalid_notification',
				'error',
				sprintf( 'Failed to process notification: %s', $e->getMessage() ),
				array(
					'type'        => is_array( $data ) ? (string) ( $data['type'] ?? '' ) : '',
					'resource_id' => is_array( $data ) ? (int) ( $data['resource_id'] ?? 0 ) : 0,
				)
			);
			continue;
		}

		$step_logger->log_notification_step( $notification, 'loopback_started', 'ok' );

		try {
			$processor->process( $notification );
		} catch ( Exception $e ) {
			$step_logger->log_failure(
				$notification,
				'processing',
				'exception',
				'error',
				sprintf( 'Failed to process notification: %s', $e->getMessage() )
			);
		}
	}

	return $success_response;
}