Automattic\WooCommerce\Internal\Admin\Settings\PaymentsProviders\WooPayments

WooPaymentsService::standardize_onboarding_step_detailsprivateWC 1.0

Standardize (and sanity check) the onboarding step details.

Method of the class: WooPaymentsService{}

No Hooks.

Returns

Array. The standardized onboarding step details.

Usage

// private - for code of main (parent) class only
$result = $this->standardize_onboarding_step_details( $step_details, $location, $rest_path ): array;
$step_details(array) (required)
The onboarding step details to standardize.
$location(string) (required)
The location for which we are onboarding. This is an ISO 3166-1 alpha-2 country code.
$rest_path(string) (required)
The REST API path to use for constructing REST API URLs.

WooPaymentsService::standardize_onboarding_step_details() code WC 10.8.1

private function standardize_onboarding_step_details( array $step_details, string $location, string $rest_path ): array {
	// If the required keys are not present, throw.
	if ( ! isset( $step_details['id'] ) ) {
		/* translators: %s: The required key that is missing. */
		throw new Exception( sprintf( esc_html__( 'The onboarding step is missing required entries: %s', 'woocommerce' ), 'id' ) );
	}
	// Validate the step ID.
	if ( ! $this->is_valid_onboarding_step_id( $step_details['id'] ) ) {
		/* translators: %s: The invalid step ID. */
		throw new Exception( sprintf( esc_html__( 'The onboarding step ID is invalid: %s', 'woocommerce' ), esc_attr( $step_details['id'] ) ) );
	}

	if ( empty( $step_details['status'] ) ) {
		$step_details['status'] = $this->get_onboarding_step_status( $step_details['id'], $location );
	}

	if ( empty( $step_details['errors'] ) ) {
		$step_details['errors'] = array();

		// For blocked or failed steps, we include any stored error.
		if ( in_array( $step_details['status'], array( self::ONBOARDING_STEP_STATUS_BLOCKED, self::ONBOARDING_STEP_STATUS_FAILED ), true ) ) {
			$stored_error = $this->get_onboarding_step_error( $step_details['id'], $location );
			if ( ! empty( $stored_error ) ) {
				$step_details['errors'] = array( $stored_error );
			}
		}
	}
	// Standardize errors to be a list of arrays with `code`, `message`, and optional extra keys.
	$standardized_errors = array();
	// If the errors is not a list of errors or it has any of the reserved entries,
	// treat it as a single error.
	if ( ! is_array( $step_details['errors'] )
		|| array_key_exists( 'code', $step_details['errors'] )
		|| array_key_exists( 'message', $step_details['errors'] )
		|| array_key_exists( 'context', $step_details['errors'] )
	) {
		$raw_errors = array( $step_details['errors'] );
	} else {
		$raw_errors = $step_details['errors'];
	}

	foreach ( $raw_errors as $error ) {
		if ( $error instanceof \WP_Error ) {
			$error = array(
				'code'    => $error->get_error_code(),
				'message' => $error->get_error_message(),
				'context' => $error->get_error_data(),
			);
		} elseif ( is_array( $error ) ) {
			if ( empty( $error['code'] ) ) {
				$error['code'] = 'general_error';
			}
			if ( ! array_key_exists( 'message', $error ) ) {
				$error['message'] = '';
			}
		} else {
			$error = array(
				'code'    => 'general_error',
				'message' => (string) $error,
			);
		}

		$standardized_errors[] = $this->sanitize_onboarding_step_error( $error );
	}
	$step_details['errors'] = $standardized_errors;

	// Ensure that any step has the general actions.
	if ( empty( $step_details['actions'] ) ) {
		$step_details['actions'] = array();
	}
	// Any step can be checked for its status.
	if ( empty( $step_details['actions']['check'] ) ) {
		$step_details['actions']['check'] = array(
			'type' => self::ACTION_TYPE_REST,
			'href' => rest_url( trailingslashit( $rest_path ) . $step_details['id'] . '/check' ),
		);
	}
	// Any step can be cleaned of its progress.
	if ( empty( $step_details['actions']['clean'] ) ) {
		$step_details['actions']['clean'] = array(
			'type' => self::ACTION_TYPE_REST,
			'href' => rest_url( trailingslashit( $rest_path ) . $step_details['id'] . '/clean' ),
		);
	}

	return array(
		'id'             => $step_details['id'],
		'path'           => $step_details['path'] ?? trailingslashit( self::ONBOARDING_PATH_BASE ) . $step_details['id'],
		'required_steps' => $step_details['required_steps'] ?? $this->get_onboarding_step_required_steps( $step_details['id'] ),
		'status'         => $step_details['status'],
		'errors'         => $step_details['errors'],
		'actions'        => $step_details['actions'],
		'context'        => $step_details['context'] ?? array(),
	);
}