Automattic\WooCommerce\Internal\Admin\Settings\PaymentsProviders

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 10.7.0

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'     => wc_string_to_bool( $recommended_pm['enabled'] ?? true ),
		// Default to not required if not explicit.
		'required'    => wc_string_to_bool( $recommended_pm['required'] ?? false ),
		'title'       => sanitize_text_field( $recommended_pm['title'] ),
		'description' => '',
		'icon'        => '',
		'category'    => self::PAYMENT_METHOD_CATEGORY_PRIMARY, // Default to primary.
		'notice'      => array(
			'badge'     => '',
			'message'   => '',
			'link_text' => '',
			'link_url'  => '',
		),
	);

	// 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'];
	}

	// If the payment method has a notice, sanitize and use its fields.
	if ( ! empty( $recommended_pm['notice'] ) && is_array( $recommended_pm['notice'] ) ) {
		$notice = $recommended_pm['notice'];

		if ( ! empty( $notice['badge'] ) ) {
			$standard_details['notice']['badge'] = sanitize_text_field( $notice['badge'] );
		}

		if ( ! empty( $notice['message'] ) ) {
			$standard_details['notice']['message'] = sanitize_text_field( $notice['message'] );
		}

		if ( ! empty( $notice['link_text'] ) ) {
			$standard_details['notice']['link_text'] = sanitize_text_field( $notice['link_text'] );
		}

		if ( ! empty( $notice['link_url'] ) && is_string( $notice['link_url'] ) && wc_is_valid_url( $notice['link_url'] ) ) {
			$standard_details['notice']['link_url'] = sanitize_url( $notice['link_url'] );
		}
	}

	return $standard_details;
}