Process location-styles in REST endpoint

This commit is contained in:
Philipp Stracker 2025-01-17 18:12:40 +01:00
parent 0b90921dd4
commit 72d84546d1
No known key found for this signature in database
2 changed files with 106 additions and 3 deletions

View file

@ -13,6 +13,7 @@ use WP_REST_Server;
use WP_REST_Response;
use WP_REST_Request;
use WooCommerce\PayPalCommerce\Settings\Data\StylingSettings;
use WooCommerce\PayPalCommerce\Settings\DTO\LocationStylingDTO;
/**
* REST controller for the "Styling" settings tab.
@ -41,8 +42,20 @@ class StylingRestEndpoint extends RestEndpoint {
* @var array
*/
private array $field_map = array(
'shape' => array(
'js_name' => 'shape',
'cart' => array(
'js_name' => 'cart',
),
'classic_checkout' => array(
'js_name' => 'classicCheckout',
),
'express_checkout' => array(
'js_name' => 'expressCheckout',
),
'mini_cart' => array(
'js_name' => 'miniCart',
),
'product' => array(
'js_name' => 'product',
),
);
@ -53,6 +66,12 @@ class StylingRestEndpoint extends RestEndpoint {
*/
public function __construct( StylingSettings $settings ) {
$this->settings = $settings;
$this->field_map['cart']['sanitize'] = array( $this, 'to_location' );
$this->field_map['classic_checkout']['sanitize'] = array( $this, 'to_location' );
$this->field_map['express_checkout']['sanitize'] = array( $this, 'to_location' );
$this->field_map['mini_cart']['sanitize'] = array( $this, 'to_location' );
$this->field_map['product']['sanitize'] = array( $this, 'to_location' );
}
/**
@ -123,4 +142,37 @@ class StylingRestEndpoint extends RestEndpoint {
return $this->get_details();
}
/**
* Converts the plain location-style input to a structured DTO.
*
* @param array $data Raw data received from the request.
* @param string $key The field name.
*
* @return LocationStylingDTO
*/
protected function to_location( array $data, string $key ) : LocationStylingDTO {
$is_enabled = ! isset( $data['enabled'] ) || $data['enabled'];
$methods = array();
$shape = sanitize_text_field( $data['shape'] ?? 'rect' );
$label = sanitize_text_field( $data['label'] ?? 'pay' );
$color = sanitize_text_field( $data['color'] ?? 'gold' );
$layout = sanitize_text_field( $data['layout'] ?? 'vertical' );
$tagline = isset( $data['tagline'] ) && $data['tagline'];
if ( isset( $data['methods'] ) && is_array( $data['methods'] ) ) {
$methods = array_map( 'sanitize_text_field', $data['methods'] );
}
return new LocationStylingDTO(
$key,
$is_enabled,
$methods,
$shape,
$label,
$color,
$layout,
$tagline,
);
}
}