AbandonedCheckoutProtocolController.php
2 years ago
AbandonedCheckoutsController.php
3 years ago
AccountController.php
2 years ago
ActivationsController.php
3 years ago
AffiliationProductsController.php
2 years ago
AffiliationProtocolController.php
2 years ago
AffiliationRequestsController.php
2 years ago
AffiliationsController.php
2 years ago
AutoFeeProtocolController.php
5 months ago
AutoFeesController.php
5 months ago
BalanceTransactionsController.php
3 years ago
BatchesController.php
5 days ago
BrandController.php
4 months ago
BumpsController.php
3 years ago
CancellationActsController.php
3 years ago
CancellationReasonsController.php
3 years ago
ChargesController.php
2 years ago
CheckEmailController.php
11 months ago
CheckoutsController.php
5 months ago
ClicksController.php
2 years ago
CouponsController.php
3 years ago
CustomerController.php
1 month ago
CustomerNotificationProtocolController.php
2 years ago
CustomerPortalProtocolController.php
1 year ago
DisplayCurrencyController.php
1 year ago
DisputesController.php
9 months ago
DownloadsController.php
3 years ago
DraftCheckoutsController.php
2 years ago
ExportsController.php
2 years ago
FulfillmentsController.php
3 years ago
ImportRowsController.php
2 months ago
IncomingWebhooksController.php
2 months ago
IntegrationCatalogController.php
1 year ago
IntegrationProvidersController.php
3 years ago
IntegrationsController.php
2 months ago
InvoicesController.php
1 year ago
LicensesController.php
3 years ago
LineItemsController.php
1 year ago
LoginController.php
1 month ago
ManualPaymentMethodsController.php
3 years ago
MediasController.php
3 years ago
OrderController.php
1 year ago
OrderProtocolController.php
2 years ago
ParcelTemplateController.php
3 months ago
PaymentIntentsController.php
2 years ago
PaymentMethodsController.php
2 years ago
PayoutGroupsController.php
2 years ago
PayoutsController.php
2 years ago
PeriodsController.php
3 years ago
PluginInstallerController.php
5 days ago
PricesController.php
1 year ago
ProcessorController.php
3 years ago
ProductCollectionsController.php
5 days ago
ProductGroupsController.php
3 years ago
ProductMediaController.php
1 year ago
ProductsController.php
5 days ago
PromotionsController.php
3 years ago
ProvisionalAccountController.php
3 years ago
PurchasesController.php
3 years ago
ReferralItemsController.php
2 years ago
ReferralsController.php
2 years ago
RefundsController.php
3 years ago
RegisteredWebhookController.php
2 years ago
RestController.php
5 days ago
ReturnItemsController.php
2 years ago
ReturnReasonsController.php
2 years ago
ReturnRequestsController.php
2 years ago
ReviewProtocolController.php
4 months ago
ReviewsController.php
5 days ago
RuleSchemaController.php
5 months ago
SettingsController.php
1 month 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
SwapsController.php
1 year ago
TaxOverrideController.php
2 years ago
TaxProtocolController.php
2 years ago
TaxRegistrationController.php
3 years ago
TaxZoneController.php
3 years ago
UploadsController.php
3 years ago
UpsellFunnelsController.php
2 years ago
UpsellsController.php
2 years ago
VariantOptionsController.php
2 years ago
VariantValuesController.php
2 years ago
VariantsController.php
2 years ago
VerificationCodeController.php
1 month ago
WebhookController.php
3 years ago
SettingsController.php
115 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 | 'checkout_show_login_prompt' => (bool) get_option( 'surecart_checkout_show_login_prompt', true ), |
| 27 | 'use_esm_loader' => (bool) get_option( 'surecart_use_esm_loader', false ), |
| 28 | 'slide_out_cart_disabled' => (bool) get_option( 'sc_slide_out_cart_disabled', false ), |
| 29 | 'load_block_assets_on_demand' => (bool) get_option( 'surecart_load_block_assets_on_demand', false ), |
| 30 | 'modern_order_bump' => get_option( 'surecart_order_bump_design', 'modern' ) === 'modern', |
| 31 | ] |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Update |
| 37 | * |
| 38 | * @param \WP_REST_Request $request Rest Request. |
| 39 | * |
| 40 | * @return \WP_REST_Response |
| 41 | */ |
| 42 | public function edit( \WP_REST_Request $request ) { |
| 43 | // save api token. |
| 44 | if ( isset( $request['api_token'] ) ) { |
| 45 | if ( ! empty( $request['api_token'] ) ) { |
| 46 | $validate = $this->validate( $request->get_param( 'api_token' ) ); |
| 47 | if ( is_wp_error( $validate ) ) { |
| 48 | return $validate; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // Update the API token. |
| 53 | ApiToken::save( $request['api_token'] ); |
| 54 | |
| 55 | // Clear the account cache. |
| 56 | \SureCart::account()->clearCache(); |
| 57 | } |
| 58 | |
| 59 | // update uninstall option. |
| 60 | if ( isset( $request['uninstall'] ) ) { |
| 61 | update_option( 'sc_uninstall', $request->get_param( 'uninstall' ) ); |
| 62 | } |
| 63 | |
| 64 | // update stripe payment_element option - used to enable the stripe's legacy card element. |
| 65 | if ( isset( $request['stripe_payment_element'] ) ) { |
| 66 | update_option( 'sc_stripe_payment_element', $request->get_param( 'stripe_payment_element' ) === false ? 0 : 1 ); |
| 67 | } |
| 68 | |
| 69 | // update performance option. |
| 70 | if ( isset( $request['use_esm_loader'] ) ) { |
| 71 | update_option( 'surecart_use_esm_loader', $request->get_param( 'use_esm_loader' ) ); |
| 72 | } |
| 73 | |
| 74 | // update slide out cart option. |
| 75 | if ( isset( $request['slide_out_cart_disabled'] ) ) { |
| 76 | update_option( 'sc_slide_out_cart_disabled', (bool) $request->get_param( 'slide_out_cart_disabled' ) ); |
| 77 | } |
| 78 | |
| 79 | if ( isset( $request['auto_sync_user_to_customer'] ) ) { |
| 80 | update_option( 'surecart_auto_sync_user_to_customer', (bool) $request->get_param( 'auto_sync_user_to_customer' ) ); |
| 81 | } |
| 82 | |
| 83 | if ( isset( $request['checkout_show_login_prompt'] ) ) { |
| 84 | update_option( 'surecart_checkout_show_login_prompt', (bool) $request->get_param( 'checkout_show_login_prompt' ) ); |
| 85 | } |
| 86 | |
| 87 | // update load block styles on demand option. |
| 88 | if ( isset( $request['load_block_assets_on_demand'] ) ) { |
| 89 | update_option( 'surecart_load_block_assets_on_demand', $request->get_param( 'load_block_assets_on_demand' ) ); |
| 90 | } |
| 91 | |
| 92 | // update order bump design option. |
| 93 | if ( isset( $request['modern_order_bump'] ) ) { |
| 94 | update_option( 'surecart_order_bump_design', (bool) $request->get_param( 'modern_order_bump' ) ? 'modern' : 'classic' ); |
| 95 | } |
| 96 | |
| 97 | return rest_ensure_response( $this->find( $request ) ); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Validate the token. |
| 102 | * |
| 103 | * @param string $token The API token. |
| 104 | * |
| 105 | * @return true|\WP_Error |
| 106 | */ |
| 107 | protected function validate( $token = '' ) { |
| 108 | $response = \SureCart::requests()->setToken( $token )->get( 'account' ); |
| 109 | if ( is_wp_error( $response ) ) { |
| 110 | return $response; |
| 111 | } |
| 112 | return true; |
| 113 | } |
| 114 | } |
| 115 |