Merge branch 'trunk' into PCP-3786-manual-input

This commit is contained in:
Philipp Stracker 2024-10-31 17:41:01 +01:00 committed by GitHub
commit 6d0a6c8653
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 412 additions and 162 deletions

View file

@ -89,10 +89,38 @@ abstract class AbstractDataModel {
continue;
}
$setter = "set_$key";
if ( method_exists( $this, $setter ) ) {
$setter = $this->get_setter_name( $key );
if ( $setter && method_exists( $this, $setter ) ) {
$this->$setter( $value );
}
}
}
/**
* Generates a setter method name for a given key, stripping the prefix from
* boolean fields (is_, use_, has_).
*
* @param int|string $field_key The key for which to generate a setter name.
*
* @return string The generated setter method name.
*/
private function get_setter_name( $field_key ) : string {
if ( ! is_string( $field_key ) ) {
return '';
}
$prefixes_to_strip = array( 'is_', 'use_', 'has_' );
$stripped_key = $field_key;
foreach ( $prefixes_to_strip as $prefix ) {
if ( str_starts_with( $field_key, $prefix ) ) {
$stripped_key = substr( $field_key, strlen( $prefix ) );
break;
}
}
return $stripped_key ? "set_$stripped_key" : '';
}
}

View file

@ -29,6 +29,13 @@ class OnboardingProfile extends AbstractDataModel {
*/
protected const OPTION_KEY = 'woocommerce-ppcp-data-onboarding';
/**
* List of customization flags, provided by the server (read-only).
*
* @var array
*/
protected array $flags = array();
/**
* Constructor.
*
@ -45,9 +52,9 @@ class OnboardingProfile extends AbstractDataModel {
) {
parent::__construct();
$this->data['can_use_casual_selling'] = $can_use_casual_selling;
$this->data['can_use_vaulting'] = $can_use_vaulting;
$this->data['can_use_card_payments'] = $can_use_card_payments;
$this->flags['can_use_casual_selling'] = $can_use_casual_selling;
$this->flags['can_use_vaulting'] = $can_use_vaulting;
$this->flags['can_use_card_payments'] = $can_use_card_payments;
}
/**
@ -57,15 +64,14 @@ class OnboardingProfile extends AbstractDataModel {
*/
protected function get_defaults() : array {
return array(
'completed' => false,
'step' => 0,
'use_sandbox' => false,
'use_manual_connection' => false,
'client_id' => '',
'client_secret' => '',
'can_use_casual_selling' => null,
'can_use_vaulting' => null,
'can_use_card_payments' => null,
'completed' => false,
'step' => 0,
'use_sandbox' => false,
'use_manual_connection' => false,
'client_id' => '',
'client_secret' => '',
'is_casual_seller' => null,
'products' => array(),
);
}
@ -112,7 +118,7 @@ class OnboardingProfile extends AbstractDataModel {
*
* @return bool
*/
public function get_use_sandbox() : bool {
public function get_sandbox() : bool {
return (bool) $this->data['use_sandbox'];
}
@ -121,7 +127,7 @@ class OnboardingProfile extends AbstractDataModel {
*
* @param bool $use_sandbox Whether to use sandbox mode.
*/
public function set_use_sandbox( bool $use_sandbox ) : void {
public function set_sandbox( bool $use_sandbox ) : void {
$this->data['use_sandbox'] = $use_sandbox;
}
@ -130,7 +136,7 @@ class OnboardingProfile extends AbstractDataModel {
*
* @return bool
*/
public function get_use_manual_connection() : bool {
public function get_manual_connection() : bool {
return (bool) $this->data['use_manual_connection'];
}
@ -139,7 +145,7 @@ class OnboardingProfile extends AbstractDataModel {
*
* @param bool $use_manual_connection Whether to use manual connection.
*/
public function set_use_manual_connection( bool $use_manual_connection ) : void {
public function set_manual_connection( bool $use_manual_connection ) : void {
$this->data['use_manual_connection'] = $use_manual_connection;
}
@ -180,29 +186,47 @@ class OnboardingProfile extends AbstractDataModel {
}
/**
* Gets whether casual selling can be used.
* Gets the casual seller flag.
*
* @return bool
* @return bool|null
*/
public function get_can_use_casual_selling() : bool {
return (bool) $this->data['can_use_casual_selling'];
public function get_casual_seller() : ?bool {
return $this->data['is_casual_seller'];
}
/**
* Gets whether vaulting can be used.
* Sets the casual-seller flag.
*
* @return bool
* @param bool|null $casual_seller Whether the merchant uses a personal account for selling.
*/
public function get_can_use_vaulting() : bool {
return (bool) $this->data['can_use_vaulting'];
public function set_casual_seller( ?bool $casual_seller ) : void {
$this->data['is_casual_seller'] = $casual_seller;
}
/**
* Gets whether Credit Card payments can be used.
* Gets the active product types for this store.
*
* @return bool
* @return string[]
*/
public function get_can_use_card_payments() : bool {
return (bool) $this->data['can_use_card_payments'];
public function get_products() : array {
return $this->data['products'];
}
/**
* Sets the list of active product types.
*
* @param string[] $products Any of ['virtual'|'physical'|'subscriptions'].
*/
public function set_products( array $products ) : void {
$this->data['products'] = $products;
}
/**
* Returns the list of read-only customization flags
*
* @return array
*/
public function get_flags() : array {
return $this->flags;
}
}

View file

@ -41,41 +41,53 @@ class OnboardingRestEndpoint extends RestEndpoint {
* @var array
*/
private array $field_map = array(
'completed' => array(
'completed' => array(
'js_name' => 'completed',
'sanitize' => 'to_boolean',
),
'step' => array(
'step' => array(
'js_name' => 'step',
'sanitize' => 'to_number',
),
'use_sandbox' => array(
'use_sandbox' => array(
'js_name' => 'useSandbox',
'sanitize' => 'to_boolean',
),
'use_manual_connection' => array(
'use_manual_connection' => array(
'js_name' => 'useManualConnection',
'sanitize' => 'to_boolean',
),
'client_id' => array(
'client_id' => array(
'js_name' => 'clientId',
'sanitize' => 'sanitize_text_field',
),
'client_secret' => array(
'client_secret' => array(
'js_name' => 'clientSecret',
'sanitize' => 'sanitize_text_field',
),
'is_casual_seller' => array(
'js_name' => 'isCasualSeller',
'sanitize' => 'to_boolean',
),
'products' => array(
'js_name' => 'products',
),
);
/**
* Map the internal flags to JS names.
*
* @var array
*/
private array $flag_map = array(
'can_use_casual_selling' => array(
'js_name' => 'canUseCasualSelling',
'sanitize' => 'read_only',
'js_name' => 'canUseCasualSelling',
),
'can_use_vaulting' => array(
'js_name' => 'canUseVaulting',
'sanitize' => 'read_only',
'js_name' => 'canUseVaulting',
),
'can_use_card_payments' => array(
'js_name' => 'canUseCardPayments',
'sanitize' => 'read_only',
'js_name' => 'canUseCardPayments',
),
);
@ -86,6 +98,8 @@ class OnboardingRestEndpoint extends RestEndpoint {
*/
public function __construct( OnboardingProfile $profile ) {
$this->profile = $profile;
$this->field_map['products']['sanitize'] = fn( $list ) => array_map( 'sanitize_text_field', $list );
}
/**
@ -128,7 +142,17 @@ class OnboardingRestEndpoint extends RestEndpoint {
$this->field_map
);
return rest_ensure_response( $js_data );
$js_flags = $this->sanitize_for_javascript(
$this->profile->get_flags(),
$this->flag_map
);
return rest_ensure_response(
array(
'data' => $js_data,
'flags' => $js_flags,
)
);
}
/**

View file

@ -65,7 +65,7 @@ class RestEndpoint extends WC_REST_Controller {
if ( null === $sanitation_cb ) {
$sanitized[ $key ] = $value;
} elseif ( method_exists( $this, $sanitation_cb ) ) {
} elseif ( is_string( $sanitation_cb ) && method_exists( $this, $sanitation_cb ) ) {
$sanitized[ $key ] = $this->{$sanitation_cb}( $value );
} elseif ( is_callable( $sanitation_cb ) ) {
$sanitized[ $key ] = $sanitation_cb( $value );