mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-04 08:47:23 +08:00
✨ Persist onboarding data in the DB
This commit is contained in:
parent
94708d10e9
commit
e884547582
3 changed files with 106 additions and 6 deletions
|
@ -11,6 +11,7 @@ namespace WooCommerce\PayPalCommerce\Settings;
|
|||
|
||||
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
||||
use WooCommerce\PayPalCommerce\Settings\Endpoint\OnboardingRestEndpoint;
|
||||
use WooCommerce\PayPalCommerce\Settings\Data\OnboardingProfile;
|
||||
|
||||
return array(
|
||||
'settings.url' => static function ( ContainerInterface $container ) : string {
|
||||
|
@ -24,7 +25,10 @@ return array(
|
|||
dirname( realpath( __FILE__ ), 3 ) . '/woocommerce-paypal-payments.php'
|
||||
);
|
||||
},
|
||||
'settings.data.onboarding' => static function ( ContainerInterface $container ) : OnboardingProfile {
|
||||
return new OnboardingProfile();
|
||||
},
|
||||
'settings.rest.onboarding' => static function ( ContainerInterface $container ) : OnboardingRestEndpoint {
|
||||
return new OnboardingRestEndpoint();
|
||||
return new OnboardingRestEndpoint( $container->get( 'settings.data.onboarding' ) );
|
||||
},
|
||||
);
|
||||
|
|
42
modules/ppcp-settings/src/Data/OnboardingProfile.php
Normal file
42
modules/ppcp-settings/src/Data/OnboardingProfile.php
Normal file
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
/**
|
||||
* Settings container class
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\Settings\Data
|
||||
*/
|
||||
|
||||
declare( strict_types = 1 );
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\Settings\Data;
|
||||
|
||||
/**
|
||||
* This class serves as a container for managing the onboarding profile details
|
||||
* within the WooCommerce PayPal Commerce plugin. It provides methods to retrieve
|
||||
* and save the onboarding profile data using WordPress options.
|
||||
*/
|
||||
class OnboardingProfile {
|
||||
/**
|
||||
* Options key where profile details are stored.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private const KEY = 'woocommerce-ppcp-data-onboarding';
|
||||
|
||||
/**
|
||||
* Returns the current onboarding profile details.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_data() : array {
|
||||
return get_option( self::KEY, array() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the onboarding profile details.
|
||||
*
|
||||
* @param array $data The profile details to save.
|
||||
*/
|
||||
public function save_data( array $data ) : void {
|
||||
update_option( self::KEY, $data );
|
||||
}
|
||||
}
|
|
@ -11,6 +11,8 @@ namespace WooCommerce\PayPalCommerce\Settings\Endpoint;
|
|||
|
||||
use WP_REST_Server;
|
||||
use WP_REST_Response;
|
||||
use WP_REST_Request;
|
||||
use WooCommerce\PayPalCommerce\Settings\Data\OnboardingProfile;
|
||||
|
||||
/**
|
||||
* REST controller for the onboarding module.
|
||||
|
@ -25,6 +27,22 @@ class OnboardingRestEndpoint extends RestEndpoint {
|
|||
*/
|
||||
protected $rest_base = 'onboarding';
|
||||
|
||||
/**
|
||||
* The settings instance.
|
||||
*
|
||||
* @var OnboardingProfile
|
||||
*/
|
||||
protected $profile;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param OnboardingProfile $profile The settings instance.
|
||||
*/
|
||||
public function __construct( OnboardingProfile $profile ) {
|
||||
$this->profile = $profile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure REST API routes.
|
||||
*/
|
||||
|
@ -40,6 +58,18 @@ class OnboardingRestEndpoint extends RestEndpoint {
|
|||
),
|
||||
)
|
||||
);
|
||||
|
||||
register_rest_route(
|
||||
$this->namespace,
|
||||
'/' . $this->rest_base,
|
||||
array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array( $this, 'update_details' ),
|
||||
'permission_callback' => array( $this, 'check_permission' ),
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -49,11 +79,35 @@ class OnboardingRestEndpoint extends RestEndpoint {
|
|||
* @return WP_REST_Response The current state of the onboarding wizard.
|
||||
*/
|
||||
public function get_details() : WP_REST_Response {
|
||||
// TODO: Retrieve the onboarding details from the database.
|
||||
$details = array(
|
||||
'step' => 1,
|
||||
'completed' => false,
|
||||
);
|
||||
$details = $this->profile->get_data();
|
||||
|
||||
return rest_ensure_response( $details );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Receives an object with onboarding details and persists it in the DB.
|
||||
*
|
||||
* @param WP_REST_Request $request Full data about the request.
|
||||
*
|
||||
* @return WP_REST_Response The current state of the onboarding wizard.
|
||||
*/
|
||||
public function update_details( WP_REST_Request $request ) : WP_REST_Response {
|
||||
$details = $this->profile->get_data();
|
||||
|
||||
$get_param = fn( $key ) => wc_clean( wp_unslash( $request->get_param( $key ) ) );
|
||||
|
||||
$raw_step = $get_param( 'step' );
|
||||
$raw_completed = $get_param( 'completed' );
|
||||
|
||||
if ( is_numeric( $raw_step ) ) {
|
||||
$details['step'] = intval( $raw_step );
|
||||
}
|
||||
if ( null !== $raw_completed ) {
|
||||
$details['completed'] = (bool) $raw_completed;
|
||||
}
|
||||
|
||||
$this->profile->save_data( $details );
|
||||
|
||||
return rest_ensure_response( $details );
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue