From 65814f51e9e04a2fefca5a85b936c46012f33c39 Mon Sep 17 00:00:00 2001
From: Philipp Stracker
Date: Thu, 21 Nov 2024 17:23:22 +0100
Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20REST=20endpoint=20for=20commo?=
=?UTF-8?q?n=20settings?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
modules/ppcp-settings/services.php | 8 +
.../ppcp-settings/src/Data/CommonSettings.php | 119 +++++++++++++++
.../src/Data/OnboardingProfile.php | 84 +----------
.../src/Endpoint/CommonRestEndpoint.php | 137 ++++++++++++++++++
.../src/Endpoint/OnboardingRestEndpoint.php | 24 +--
modules/ppcp-settings/src/SettingsModule.php | 1 +
6 files changed, 273 insertions(+), 100 deletions(-)
create mode 100644 modules/ppcp-settings/src/Data/CommonSettings.php
create mode 100644 modules/ppcp-settings/src/Endpoint/CommonRestEndpoint.php
diff --git a/modules/ppcp-settings/services.php b/modules/ppcp-settings/services.php
index 9db70b4c3..e42ba5da3 100644
--- a/modules/ppcp-settings/services.php
+++ b/modules/ppcp-settings/services.php
@@ -16,6 +16,8 @@ use WooCommerce\PayPalCommerce\Settings\Endpoint\OnboardingRestEndpoint;
use WooCommerce\PayPalCommerce\Settings\Service\ConnectionUrlGenerator;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
use WooCommerce\PayPalCommerce\Settings\Endpoint\LoginLinkRestEndpoint;
+use WooCommerce\PayPalCommerce\Settings\Data\CommonSettings;
+use WooCommerce\PayPalCommerce\Settings\Endpoint\CommonRestEndpoint;
return array(
'settings.url' => static function ( ContainerInterface $container ) : string {
@@ -46,9 +48,15 @@ return array(
$can_use_card_payments
);
},
+ 'settings.data.common' => static function ( ContainerInterface $container ) : CommonSettings {
+ return new CommonSettings();
+ },
'settings.rest.onboarding' => static function ( ContainerInterface $container ) : OnboardingRestEndpoint {
return new OnboardingRestEndpoint( $container->get( 'settings.data.onboarding' ) );
},
+ 'settings.rest.common' => static function ( ContainerInterface $container ) : CommonRestEndpoint {
+ return new CommonRestEndpoint( $container->get( 'settings.data.common' ) );
+ },
'settings.rest.connect_manual' => static function ( ContainerInterface $container ) : ConnectManualRestEndpoint {
return new ConnectManualRestEndpoint(
$container->get( 'api.paypal-host-production' ),
diff --git a/modules/ppcp-settings/src/Data/CommonSettings.php b/modules/ppcp-settings/src/Data/CommonSettings.php
new file mode 100644
index 000000000..8f7dd1ddf
--- /dev/null
+++ b/modules/ppcp-settings/src/Data/CommonSettings.php
@@ -0,0 +1,119 @@
+ false,
+ 'use_manual_connection' => false,
+ 'client_id' => '',
+ 'client_secret' => '',
+ );
+ }
+
+ // -----
+
+ /**
+ * Gets the 'use sandbox' setting.
+ *
+ * @return bool
+ */
+ public function get_sandbox() : bool {
+ return (bool) $this->data['use_sandbox'];
+ }
+
+ /**
+ * Sets the 'use sandbox' setting.
+ *
+ * @param bool $use_sandbox Whether to use sandbox mode.
+ */
+ public function set_sandbox( bool $use_sandbox ) : void {
+ $this->data['use_sandbox'] = $use_sandbox;
+ }
+
+ /**
+ * Gets the 'use manual connection' setting.
+ *
+ * @return bool
+ */
+ public function get_manual_connection() : bool {
+ return (bool) $this->data['use_manual_connection'];
+ }
+
+ /**
+ * Sets the 'use manual connection' setting.
+ *
+ * @param bool $use_manual_connection Whether to use manual connection.
+ */
+ public function set_manual_connection( bool $use_manual_connection ) : void {
+ $this->data['use_manual_connection'] = $use_manual_connection;
+ }
+
+ /**
+ * Gets the client ID.
+ *
+ * @return string
+ */
+ public function get_client_id() : string {
+ return $this->data['client_id'];
+ }
+
+ /**
+ * Sets the client ID.
+ *
+ * @param string $client_id The client ID.
+ */
+ public function set_client_id( string $client_id ) : void {
+ $this->data['client_id'] = sanitize_text_field( $client_id );
+ }
+
+ /**
+ * Gets the client secret.
+ *
+ * @return string
+ */
+ public function get_client_secret() : string {
+ return $this->data['client_secret'];
+ }
+
+ /**
+ * Sets the client secret.
+ *
+ * @param string $client_secret The client secret.
+ */
+ public function set_client_secret( string $client_secret ) : void {
+ $this->data['client_secret'] = sanitize_text_field( $client_secret );
+ }
+}
diff --git a/modules/ppcp-settings/src/Data/OnboardingProfile.php b/modules/ppcp-settings/src/Data/OnboardingProfile.php
index e1f9e16b4..3e812a009 100644
--- a/modules/ppcp-settings/src/Data/OnboardingProfile.php
+++ b/modules/ppcp-settings/src/Data/OnboardingProfile.php
@@ -64,14 +64,10 @@ 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' => '',
- 'is_casual_seller' => null,
- 'products' => array(),
+ 'completed' => false,
+ 'step' => 0,
+ 'is_casual_seller' => null,
+ 'products' => array(),
);
}
@@ -113,78 +109,6 @@ class OnboardingProfile extends AbstractDataModel {
$this->data['step'] = $step;
}
- /**
- * Gets the 'use sandbox' setting.
- *
- * @return bool
- */
- public function get_sandbox() : bool {
- return (bool) $this->data['use_sandbox'];
- }
-
- /**
- * Sets the 'use sandbox' setting.
- *
- * @param bool $use_sandbox Whether to use sandbox mode.
- */
- public function set_sandbox( bool $use_sandbox ) : void {
- $this->data['use_sandbox'] = $use_sandbox;
- }
-
- /**
- * Gets the 'use manual connection' setting.
- *
- * @return bool
- */
- public function get_manual_connection() : bool {
- return (bool) $this->data['use_manual_connection'];
- }
-
- /**
- * Sets the 'use manual connection' setting.
- *
- * @param bool $use_manual_connection Whether to use manual connection.
- */
- public function set_manual_connection( bool $use_manual_connection ) : void {
- $this->data['use_manual_connection'] = $use_manual_connection;
- }
-
- /**
- * Gets the client ID.
- *
- * @return string
- */
- public function get_client_id() : string {
- return $this->data['client_id'];
- }
-
- /**
- * Sets the client ID.
- *
- * @param string $client_id The client ID.
- */
- public function set_client_id( string $client_id ) : void {
- $this->data['client_id'] = sanitize_text_field( $client_id );
- }
-
- /**
- * Gets the client secret.
- *
- * @return string
- */
- public function get_client_secret() : string {
- return $this->data['client_secret'];
- }
-
- /**
- * Sets the client secret.
- *
- * @param string $client_secret The client secret.
- */
- public function set_client_secret( string $client_secret ) : void {
- $this->data['client_secret'] = sanitize_text_field( $client_secret );
- }
-
/**
* Gets the casual seller flag.
*
diff --git a/modules/ppcp-settings/src/Endpoint/CommonRestEndpoint.php b/modules/ppcp-settings/src/Endpoint/CommonRestEndpoint.php
new file mode 100644
index 000000000..18c9de902
--- /dev/null
+++ b/modules/ppcp-settings/src/Endpoint/CommonRestEndpoint.php
@@ -0,0 +1,137 @@
+ array(
+ 'js_name' => 'useSandbox',
+ 'sanitize' => 'to_boolean',
+ ),
+ 'use_manual_connection' => array(
+ 'js_name' => 'useManualConnection',
+ 'sanitize' => 'to_boolean',
+ ),
+ 'client_id' => array(
+ 'js_name' => 'clientId',
+ 'sanitize' => 'sanitize_text_field',
+ ),
+ 'client_secret' => array(
+ 'js_name' => 'clientSecret',
+ 'sanitize' => 'sanitize_text_field',
+ ),
+ );
+
+ /**
+ * Constructor.
+ *
+ * @param CommonSettings $settings The settings instance.
+ */
+ public function __construct( CommonSettings $settings ) {
+ $this->settings = $settings;
+ }
+
+ /**
+ * Configure REST API routes.
+ */
+ public function register_routes() {
+ register_rest_route(
+ $this->namespace,
+ '/' . $this->rest_base,
+ array(
+ array(
+ 'methods' => WP_REST_Server::READABLE,
+ 'callback' => array( $this, 'get_details' ),
+ 'permission_callback' => array( $this, 'check_permission' ),
+ ),
+ )
+ );
+
+ 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' ),
+ ),
+ )
+ );
+ }
+
+ /**
+ * Returns all common details from the DB.
+ *
+ * @return WP_REST_Response The common settings.
+ */
+ public function get_details() : WP_REST_Response {
+ $js_data = $this->sanitize_for_javascript(
+ $this->settings->to_array(),
+ $this->field_map
+ );
+
+ return rest_ensure_response(
+ array(
+ 'data' => $js_data,
+ )
+ );
+ }
+
+ /**
+ * Updates common details based on the request.
+ *
+ * @param WP_REST_Request $request Full data about the request.
+ *
+ * @return WP_REST_Response The new common settings.
+ */
+ public function update_details( WP_REST_Request $request ) : WP_REST_Response {
+ $wp_data = $this->sanitize_for_wordpress(
+ $request->get_params(),
+ $this->field_map
+ );
+
+ $this->settings->from_array( $wp_data );
+ $this->settings->save();
+
+ return $this->get_details();
+ }
+}
diff --git a/modules/ppcp-settings/src/Endpoint/OnboardingRestEndpoint.php b/modules/ppcp-settings/src/Endpoint/OnboardingRestEndpoint.php
index 6c59b1622..03690d3bf 100644
--- a/modules/ppcp-settings/src/Endpoint/OnboardingRestEndpoint.php
+++ b/modules/ppcp-settings/src/Endpoint/OnboardingRestEndpoint.php
@@ -41,35 +41,19 @@ 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(
- 'js_name' => 'useSandbox',
- 'sanitize' => 'to_boolean',
- ),
- 'use_manual_connection' => array(
- 'js_name' => 'useManualConnection',
- 'sanitize' => 'to_boolean',
- ),
- 'client_id' => array(
- 'js_name' => 'clientId',
- 'sanitize' => 'sanitize_text_field',
- ),
- 'client_secret' => array(
- 'js_name' => 'clientSecret',
- 'sanitize' => 'sanitize_text_field',
- ),
- 'is_casual_seller' => array(
+ 'is_casual_seller' => array(
'js_name' => 'isCasualSeller',
'sanitize' => 'to_boolean',
),
- 'products' => array(
+ 'products' => array(
'js_name' => 'products',
),
);
diff --git a/modules/ppcp-settings/src/SettingsModule.php b/modules/ppcp-settings/src/SettingsModule.php
index 03a3a5e85..1503414a0 100644
--- a/modules/ppcp-settings/src/SettingsModule.php
+++ b/modules/ppcp-settings/src/SettingsModule.php
@@ -110,6 +110,7 @@ class SettingsModule implements ServiceModule, ExecutableModule {
static function () use ( $container ) : void {
$endpoints = array(
$container->get( 'settings.rest.onboarding' ),
+ $container->get( 'settings.rest.common' ),
$container->get( 'settings.rest.connect_manual' ),
$container->get( 'settings.rest.login_link' ),
);