AbandonedCheckoutProtocolController.php
2 years ago
AbandonedCheckoutsController.php
3 years ago
AccountController.php
2 years ago
ActivationsController.php
3 years ago
BalanceTransactionsController.php
3 years ago
BrandController.php
2 years ago
BumpsController.php
3 years ago
CancellationActsController.php
3 years ago
CancellationReasonsController.php
3 years ago
ChargesController.php
2 years ago
CheckEmailController.php
2 years ago
CheckoutsController.php
2 years ago
CouponsController.php
3 years ago
CustomerController.php
3 years ago
CustomerNotificationProtocolController.php
2 years ago
DownloadsController.php
3 years ago
DraftCheckoutsController.php
2 years ago
FulfillmentsController.php
3 years ago
IncomingWebhooksController.php
2 years ago
IntegrationProvidersController.php
3 years ago
IntegrationsController.php
2 years ago
InvoicesController.php
3 years ago
LicensesController.php
3 years ago
LineItemsController.php
3 years ago
LoginController.php
3 years ago
ManualPaymentMethodsController.php
3 years ago
MediasController.php
3 years ago
OrderController.php
3 years ago
OrderProtocolController.php
2 years ago
PaymentIntentsController.php
2 years ago
PaymentMethodsController.php
2 years ago
PeriodsController.php
3 years ago
PortalProtocolController.php
2 years ago
PricesController.php
3 years ago
ProcessorController.php
3 years ago
ProductCollectionsController.php
2 years ago
ProductGroupsController.php
3 years ago
ProductMediaController.php
3 years ago
ProductsController.php
2 years ago
PromotionsController.php
3 years ago
ProvisionalAccountController.php
3 years ago
PurchasesController.php
3 years ago
RefundsController.php
3 years ago
RegisteredWebhookController.php
2 years ago
RestController.php
2 years ago
ReturnItemsController.php
2 years ago
ReturnReasonsController.php
2 years ago
ReturnRequestsController.php
2 years ago
SettingsController.php
2 years ago
ShippingMethodController.php
3 years ago
ShippingProfileController.php
3 years ago
ShippingProtocolController.php
3 years ago
ShippingRateController.php
3 years ago
ShippingZoneController.php
3 years ago
StatisticsController.php
2 years ago
SubscriptionProtocolController.php
2 years ago
SubscriptionsController.php
2 years ago
TaxProtocolController.php
2 years ago
TaxRegistrationController.php
3 years ago
TaxZoneController.php
3 years ago
UploadsController.php
3 years ago
VariantOptionsController.php
2 years ago
VariantValuesController.php
2 years ago
VariantsController.php
2 years ago
VerificationCodeController.php
2 years ago
WebhookController.php
3 years ago
SettingsController.php
93 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SureCart\Controllers\Rest; |
| 4 | |
| 5 | use SureCart\Models\ApiToken; |
| 6 | |
| 7 | /** |
| 8 | * Handle price requests through the REST API |
| 9 | */ |
| 10 | class SettingsController { |
| 11 | /** |
| 12 | * Index |
| 13 | * |
| 14 | * @param \WP_REST_Request $request Rest Request. |
| 15 | * |
| 16 | * @return \WP_REST_Response |
| 17 | */ |
| 18 | public function find( \WP_REST_Request $request ) { |
| 19 | return rest_ensure_response( |
| 20 | [ |
| 21 | 'object' => 'settings', |
| 22 | 'api_token' => ApiToken::get(), |
| 23 | 'uninstall' => (bool) get_option( 'sc_uninstall', false ), |
| 24 | 'stripe_payment_element' => (bool) get_option( 'sc_stripe_payment_element', true ), |
| 25 | 'auto_sync_user_to_customer' => (bool) get_option( 'surecart_auto_sync_user_to_customer', false ), |
| 26 | 'use_esm_loader' => (bool) get_option( 'surecart_use_esm_loader', false ), |
| 27 | 'slide_out_cart_disabled' => (bool) get_option( 'sc_slide_out_cart_disabled', false ), |
| 28 | ] |
| 29 | ); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Update |
| 34 | * |
| 35 | * @param \WP_REST_Request $request Rest Request. |
| 36 | * |
| 37 | * @return \WP_REST_Response |
| 38 | */ |
| 39 | public function edit( \WP_REST_Request $request ) { |
| 40 | // save api token. |
| 41 | if ( isset( $request['api_token'] ) ) { |
| 42 | if ( ! empty( $request['api_token'] ) ) { |
| 43 | $validate = $this->validate( $request->get_param( 'api_token' ) ); |
| 44 | if ( is_wp_error( $validate ) ) { |
| 45 | return $validate; |
| 46 | } |
| 47 | } |
| 48 | ApiToken::save( $request['api_token'] ); |
| 49 | } |
| 50 | |
| 51 | // update uninstall option. |
| 52 | if ( isset( $request['uninstall'] ) ) { |
| 53 | update_option( 'sc_uninstall', $request->get_param( 'uninstall' ) ); |
| 54 | } |
| 55 | |
| 56 | // update stripe payment_element option - used to enable the stripe's legacy card element. |
| 57 | if ( isset( $request['stripe_payment_element'] ) ) { |
| 58 | update_option( 'sc_stripe_payment_element', $request->get_param( 'stripe_payment_element' ) === false ? 0 : 1 ); |
| 59 | } |
| 60 | |
| 61 | // update performance option. |
| 62 | if ( isset( $request['use_esm_loader'] ) ) { |
| 63 | update_option( 'surecart_use_esm_loader', $request->get_param( 'use_esm_loader' ) ); |
| 64 | } |
| 65 | |
| 66 | // update slide out cart option. |
| 67 | if ( isset( $request['slide_out_cart_disabled'] ) ) { |
| 68 | update_option( 'sc_slide_out_cart_disabled', (bool) $request->get_param( 'slide_out_cart_disabled' ) ); |
| 69 | } |
| 70 | |
| 71 | if ( isset( $request['auto_sync_user_to_customer'] ) ) { |
| 72 | update_option( 'surecart_auto_sync_user_to_customer', (bool) $request->get_param( 'auto_sync_user_to_customer' ) ); |
| 73 | } |
| 74 | |
| 75 | return rest_ensure_response( $this->find( $request ) ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Validate the token. |
| 80 | * |
| 81 | * @param string $token The API token. |
| 82 | * |
| 83 | * @return true|\WP_Error |
| 84 | */ |
| 85 | protected function validate( $token = '' ) { |
| 86 | $response = \SureCart::requests()->setToken( $token )->get( 'account' ); |
| 87 | if ( is_wp_error( $response ) ) { |
| 88 | return $response; |
| 89 | } |
| 90 | return true; |
| 91 | } |
| 92 | } |
| 93 |