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

WooPaymentsService::sanitize_onboarding_step_errorprivateWC 1.0

Sanitize an error for an onboarding step.

Method of the class: WooPaymentsService{}

No Hooks.

Returns

Array. The sanitized error.

Usage

// private - for code of main (parent) class only
$result = $this->sanitize_onboarding_step_error( $error ): array;
$error(array) (required)
The error to sanitize.

WooPaymentsService::sanitize_onboarding_step_error() code WC 10.8.1

private function sanitize_onboarding_step_error( array $error ): array {
	$sanitized_error = array(
		'code'    => isset( $error['code'] ) ? sanitize_text_field( $error['code'] ) : '',
		'message' => isset( $error['message'] ) ? sanitize_text_field( $error['message'] ) : '',
		'context' => array(),
	);

	// Move all extra keys (not code, message, context) into the context.
	$reserved_keys = array( 'code', 'message', 'context' );
	foreach ( $error as $key => $value ) {
		if ( ! in_array( $key, $reserved_keys, true ) ) {
			$sanitized_error['context'][ $key ] = $value;
		}
	}

	// Merge any existing context data.
	if ( isset( $error['context'] ) && ( is_array( $error['context'] ) || is_object( $error['context'] ) ) ) {
		// Make sure we are dealing with an array.
		$existing_context = json_decode( wp_json_encode( $error['context'] ), true );
		if ( is_array( $existing_context ) ) {
			$sanitized_error['context'] = array_merge( $sanitized_error['context'], $existing_context );
		}
	}

	// Flatten any nested 'context' key (e.g., from WP_Error data that includes its own context).
	// The nested context values take precedence over the top-level values.
	if ( isset( $sanitized_error['context']['context'] ) && is_array( $sanitized_error['context']['context'] ) ) {
		$nested_context = $sanitized_error['context']['context'];
		unset( $sanitized_error['context']['context'] );
		$sanitized_error['context'] = array_merge( $sanitized_error['context'], $nested_context );
	}

	if ( ! empty( $sanitized_error['context'] ) ) {

		// Sanitize the context data.
		// It can only contain strings or arrays of strings.
		// Scalar values will be converted to strings. Other types will be ignored.
		foreach ( $sanitized_error['context'] as $key => $value ) {
			if ( is_string( $value ) ) {
				$sanitized_error['context'][ $key ] = sanitize_text_field( $value );
			} elseif ( is_array( $value ) ) {
				// Arrays can only contain strings.
				$sanitized_error['context'][ $key ] = array_map(
					function ( $item ) {
						if ( is_string( $item ) ) {
							return sanitize_text_field( $item );
						} elseif ( is_scalar( $item ) ) {
							return sanitize_text_field( (string) $item );
						} else {
							return '';
						}
					},
					$value
				);
				// Remove any empty values from the array.
				$sanitized_error['context'][ $key ] = array_filter(
					$sanitized_error['context'][ $key ],
					function ( $item ) {
						return '' !== $item;
					}
				);
			} else {
				unset( $sanitized_error['context'][ $key ] );
			}
		}
	}

	return $sanitized_error;
}