From 5e78085a7ae0f7a695245c692637ff3c05837e0e Mon Sep 17 00:00:00 2001 From: inpsyde-maticluznar Date: Thu, 12 Dec 2024 06:04:03 +0100 Subject: [PATCH] Copy endpoint --- .../src/Endpoint/WebhookSettingsEndpoint.php | 167 ++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 modules/ppcp-settings/src/Endpoint/WebhookSettingsEndpoint.php diff --git a/modules/ppcp-settings/src/Endpoint/WebhookSettingsEndpoint.php b/modules/ppcp-settings/src/Endpoint/WebhookSettingsEndpoint.php new file mode 100644 index 000000000..328ff54c4 --- /dev/null +++ b/modules/ppcp-settings/src/Endpoint/WebhookSettingsEndpoint.php @@ -0,0 +1,167 @@ + array( + 'js_name' => 'completed', + 'sanitize' => 'to_boolean', + ), + 'step' => array( + 'js_name' => 'step', + 'sanitize' => 'to_number', + ), + 'is_casual_seller' => array( + 'js_name' => 'isCasualSeller', + 'sanitize' => 'to_boolean', + ), + 'are_optional_payment_methods_enabled' => array( + 'js_name' => 'areOptionalPaymentMethodsEnabled', + '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', + ), + 'can_use_vaulting' => array( + 'js_name' => 'canUseVaulting', + ), + 'can_use_card_payments' => array( + 'js_name' => 'canUseCardPayments', + ), + 'can_use_subscriptions' => array( + 'js_name' => 'canUseSubscriptions', + ), + ); + + /** + * Constructor. + * + * @param OnboardingProfile $profile The settings instance. + */ + public function __construct( OnboardingProfile $profile ) { + $this->profile = $profile; + + $this->field_map['products']['sanitize'] = fn( $list ) => array_map( 'sanitize_text_field', $list ); + } + + /** + * 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 details of the current onboarding wizard progress. + * + * @return WP_REST_Response The current state of the onboarding wizard. + */ + public function get_details() : WP_REST_Response { + $js_data = $this->sanitize_for_javascript( + $this->profile->to_array(), + $this->field_map + ); + + $js_flags = $this->sanitize_for_javascript( + $this->profile->get_flags(), + $this->flag_map + ); + + return $this->return_success( + $js_data, + array( + 'flags' => $js_flags, + ) + ); + } + + /** + * Updates onboarding details based on the request. + * + * @param WP_REST_Request $request Full data about the request. + * + * @return WP_REST_Response The updated state of the onboarding wizard. + */ + public function update_details( WP_REST_Request $request ) : WP_REST_Response { + $wp_data = $this->sanitize_for_wordpress( + $request->get_params(), + $this->field_map + ); + + $this->profile->from_array( $wp_data ); + $this->profile->save(); + + return $this->get_details(); + } +}