mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 12:25:15 +08:00
✨ Add data sanitizer to styling data model
This commit is contained in:
parent
57afe4b95e
commit
605760f5fb
3 changed files with 92 additions and 3 deletions
|
@ -68,7 +68,9 @@ return array(
|
|||
);
|
||||
},
|
||||
'settings.data.styling' => static function ( ContainerInterface $container ) : StylingSettings {
|
||||
return new StylingSettings();
|
||||
return new StylingSettings(
|
||||
$container->get( 'settings.service.sanitizer' )
|
||||
);
|
||||
},
|
||||
'settings.rest.onboarding' => static function ( ContainerInterface $container ) : OnboardingRestEndpoint {
|
||||
return new OnboardingRestEndpoint( $container->get( 'settings.data.onboarding' ) );
|
||||
|
|
|
@ -9,7 +9,9 @@ declare( strict_types = 1 );
|
|||
|
||||
namespace WooCommerce\PayPalCommerce\Settings\Data;
|
||||
|
||||
use RuntimeException;
|
||||
use WooCommerce\PayPalCommerce\Settings\DTO\LocationStylingDTO;
|
||||
use WooCommerce\PayPalCommerce\Settings\Service\DataSanitizer;
|
||||
|
||||
/**
|
||||
* Class StylingSettings
|
||||
|
@ -25,6 +27,25 @@ class StylingSettings extends AbstractDataModel {
|
|||
*/
|
||||
protected const OPTION_KEY = 'woocommerce-ppcp-data-styling';
|
||||
|
||||
/**
|
||||
* Data sanitizer service.
|
||||
*
|
||||
* @var DataSanitizer
|
||||
*/
|
||||
protected DataSanitizer $sanitizer;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param DataSanitizer $sanitizer Data sanitizer service.
|
||||
* @throws RuntimeException If the OPTION_KEY is not defined in the child class.
|
||||
*/
|
||||
public function __construct( DataSanitizer $sanitizer ) {
|
||||
$this->sanitizer = $sanitizer;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default values for the model.
|
||||
*
|
||||
|
@ -49,6 +70,16 @@ class StylingSettings extends AbstractDataModel {
|
|||
return $this->data['cart'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Set styling details for Cart and Block Cart.
|
||||
*
|
||||
* @param mixed $styles The new styling details.
|
||||
* @return void
|
||||
*/
|
||||
public function set_cart( $styles ) : void {
|
||||
$this->data['cart'] = $this->sanitizer->sanitize_location_style( $styles );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get styling details for Classic Checkout.
|
||||
*
|
||||
|
@ -58,6 +89,16 @@ class StylingSettings extends AbstractDataModel {
|
|||
return $this->data['classic_checkout'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Set styling details for Classic Checkout.
|
||||
*
|
||||
* @param mixed $styles The new styling details.
|
||||
* @return void
|
||||
*/
|
||||
public function set_classic_checkout( $styles ) : void {
|
||||
$this->data['classic_checkout'] = $this->sanitizer->sanitize_location_style( $styles );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get styling details for Express Checkout.
|
||||
*
|
||||
|
@ -67,6 +108,16 @@ class StylingSettings extends AbstractDataModel {
|
|||
return $this->data['express_checkout'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Set styling details for Express Checkout.
|
||||
*
|
||||
* @param mixed $styles The new styling details.
|
||||
* @return void
|
||||
*/
|
||||
public function set_express_checkout( $styles ) : void {
|
||||
$this->data['express_checkout'] = $this->sanitizer->sanitize_location_style( $styles );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get styling details for Mini Cart
|
||||
*
|
||||
|
@ -76,6 +127,16 @@ class StylingSettings extends AbstractDataModel {
|
|||
return $this->data['mini_cart'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Set styling details for Mini Cart.
|
||||
*
|
||||
* @param mixed $styles The new styling details.
|
||||
* @return void
|
||||
*/
|
||||
public function set_mini_cart( $styles ) : void {
|
||||
$this->data['mini_cart'] = $this->sanitizer->sanitize_location_style( $styles );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get styling details for Product Page.
|
||||
*
|
||||
|
@ -84,4 +145,14 @@ class StylingSettings extends AbstractDataModel {
|
|||
public function get_product() : LocationStylingDTO {
|
||||
return $this->data['product'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Set styling details for Product Page.
|
||||
*
|
||||
* @param mixed $styles The new styling details.
|
||||
* @return void
|
||||
*/
|
||||
public function set_product( $styles ) : void {
|
||||
$this->data['product'] = $this->sanitizer->sanitize_location_style( $styles );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,11 +19,27 @@ class DataSanitizer {
|
|||
/**
|
||||
* Sanitizes the provided styling data.
|
||||
*
|
||||
* @param array $data The styling data to sanitize.
|
||||
* @param mixed $data The styling data to sanitize.
|
||||
* @param ?string $location Name of the location.
|
||||
* @return LocationStylingDTO Styling data.
|
||||
*/
|
||||
public function sanitize_location_style( array $data, string $location = null ) : LocationStylingDTO {
|
||||
public function sanitize_location_style( $data, string $location = null ) : LocationStylingDTO {
|
||||
if ( $data instanceof LocationStylingDTO ) {
|
||||
if ( $location ) {
|
||||
$data->location = $location;
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
if ( is_object( $data ) ) {
|
||||
$data = (array) $data;
|
||||
}
|
||||
|
||||
if ( ! is_array( $data ) ) {
|
||||
return new LocationStylingDTO( $location ?? '' );
|
||||
}
|
||||
|
||||
if ( null === $location ) {
|
||||
$location = $data['location'] ?? '';
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue