diff --git a/modules/ppcp-settings/src/Data/StylingSettings.php b/modules/ppcp-settings/src/Data/StylingSettings.php index ee942693c..9c81aa408 100644 --- a/modules/ppcp-settings/src/Data/StylingSettings.php +++ b/modules/ppcp-settings/src/Data/StylingSettings.php @@ -9,6 +9,8 @@ declare( strict_types = 1 ); namespace WooCommerce\PayPalCommerce\Settings\Data; +use WooCommerce\PayPalCommerce\Settings\DTO\LocationStylingDTO; + /** * Class StylingSettings * @@ -30,7 +32,56 @@ class StylingSettings extends AbstractDataModel { */ protected function get_defaults() : array { return array( - 'shape' => 'rect', + 'cart' => new LocationStylingDTO( 'cart' ), + 'classic_checkout' => new LocationStylingDTO( 'classic_checkout' ), + 'express_checkout' => new LocationStylingDTO( 'express_checkout' ), + 'mini_cart' => new LocationStylingDTO( 'mini_cart' ), + 'product' => new LocationStylingDTO( 'product' ), ); } + + /** + * Get styling details for Cart and Block Cart. + * + * @return LocationStylingDTO + */ + public function get_cart() : LocationStylingDTO { + return $this->data['cart']; + } + + /** + * Get styling details for Classic Checkout. + * + * @return LocationStylingDTO + */ + public function get_classic_checkout() : LocationStylingDTO { + return $this->data['classic_checkout']; + } + + /** + * Get styling details for Express Checkout. + * + * @return LocationStylingDTO + */ + public function get_express_checkout() : LocationStylingDTO { + return $this->data['express_checkout']; + } + + /** + * Get styling details for Mini Cart + * + * @return LocationStylingDTO + */ + public function get_mini_cart() : LocationStylingDTO { + return $this->data['mini_cart']; + } + + /** + * Get styling details for Product Page. + * + * @return LocationStylingDTO + */ + public function get_product() : LocationStylingDTO { + return $this->data['product']; + } } diff --git a/modules/ppcp-settings/src/Endpoint/StylingRestEndpoint.php b/modules/ppcp-settings/src/Endpoint/StylingRestEndpoint.php index 97b1158f2..8ad08f631 100644 --- a/modules/ppcp-settings/src/Endpoint/StylingRestEndpoint.php +++ b/modules/ppcp-settings/src/Endpoint/StylingRestEndpoint.php @@ -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, + ); + } }