Automattic\WooCommerce\Internal\Admin\Settings\PaymentProviders

PaymentGateway::standardize_recommended_payment_methodprotectedWC 1.0

Standardize a recommended payment method entry.

Method of the class: PaymentGateway{}

No Hooks.

Returns

Array. The standardized recommended payment method entry.

Usage

// protected - for code of main (parent) or child class
$result = $this->standardize_recommended_payment_method( $recommended_pm, $order ): array;
$recommended_pm(array) (required)
The recommended payment method entry to standardize.
$order(int)
The order of the recommended payment method.
Default: 0 if not provided

PaymentGateway::standardize_recommended_payment_method() code WC 9.9.4

protected function standardize_recommended_payment_method( array $recommended_pm, int $order = 0 ): array {
	$standard_details = array(
		'id'          => sanitize_key( $recommended_pm['id'] ),
		'_order'      => $order,
		// Default to enabled if not explicit.
		'enabled'     => filter_var( $recommended_pm['enabled'] ?? true, FILTER_VALIDATE_BOOLEAN ),
		// Default to not required if not explicit.
		'required'    => filter_var( $recommended_pm['required'] ?? false, FILTER_VALIDATE_BOOLEAN ),
		'title'       => sanitize_text_field( $recommended_pm['title'] ),
		'description' => '',
		'icon'        => '',
		'category'    => self::PAYMENT_METHOD_CATEGORY_PRIMARY, // Default to primary.
	);

	// If the payment method has a description, sanitize it before use.
	if ( ! empty( $recommended_pm['description'] ) ) {
		$standard_details['description'] = (string) $recommended_pm['description'];
		// Make sure that if we have HTML tags, we only allow stylistic tags and anchors.
		if ( preg_match( '/<[^>]+>/', $standard_details['description'] ) ) {
			// Only allow stylistic tags with a few modifications.
			$allowed_tags = wp_kses_allowed_html( 'data' );
			$allowed_tags = array_merge(
				$allowed_tags,
				array(
					'a' => array(
						'href'   => true,
						'target' => true,
					),
				)
			);

			$standard_details['description'] = wp_kses( $standard_details['description'], $allowed_tags );
		}
	}

	// If the payment method has an icon, try to use it.
	if ( ! empty( $recommended_pm['icon'] ) && wc_is_valid_url( $recommended_pm['icon'] ) ) {
		$standard_details['icon'] = sanitize_url( $recommended_pm['icon'] );
	}

	// If the payment method has a category, use it if it's one of the known categories.
	if ( ! empty( $recommended_pm['category'] ) &&
		in_array( $recommended_pm['category'], array( self::PAYMENT_METHOD_CATEGORY_PRIMARY, self::PAYMENT_METHOD_CATEGORY_SECONDARY ), true ) ) {
		$standard_details['category'] = $recommended_pm['category'];
	}

	return $standard_details;
}