PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 2.2.5
WooCommerce Square v2.2.5
5.5.0 5.4.3 5.4.2 5.4.1 5.4.0 trunk 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.2.0 3.3.0 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.7.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 4.0.0 4.1.0 4.2.0 4.2.1 4.2.2 4.2.3 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.4.2 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.9.0 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.6 4.9.7 4.9.8 4.9.9 5.0.0 5.0.1 5.1.0 5.1.1 5.1.2 5.2.0 5.3.0 5.3.1 5.3.2 5.3.3
woocommerce-square / includes / Settings.php
woocommerce-square / includes Last commit date
API 6 years ago Admin 5 years ago Emails 6 years ago Gateway 5 years ago Handlers 5 years ago Sync 5 years ago Utilities 6 years ago AJAX.php 5 years ago API.php 6 years ago Admin.php 6 years ago Functions.php 6 years ago Gateway.php 5 years ago Lifecycle.php 6 years ago Plugin.php 5 years ago Settings.php 5 years ago
Settings.php
948 lines
1 <?php
2 /**
3 * WooCommerce Square
4 *
5 * This source file is subject to the GNU General Public License v3.0
6 * that is bundled with this package in the file license.txt.
7 * It is also available through the world-wide-web at this URL:
8 * http://www.gnu.org/licenses/gpl-3.0.html
9 * If you did not receive a copy of the license and are unable to
10 * obtain it through the world-wide-web, please send an email
11 * to license@woocommerce.com so we can send you a copy immediately.
12 *
13 * DISCLAIMER
14 *
15 * Do not edit or add to this file if you wish to upgrade WooCommerce Square to newer
16 * versions in the future. If you wish to customize WooCommerce Square for your
17 * needs please refer to https://docs.woocommerce.com/document/woocommerce-square/
18 *
19 * @author WooCommerce
20 * @copyright Copyright: (c) 2019, Automattic, Inc.
21 * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
22 */
23
24 namespace WooCommerce\Square;
25
26 defined( 'ABSPATH' ) || exit;
27
28 use SkyVerge\WooCommerce\PluginFramework\v5_4_0 as Framework;
29
30 /**
31 * The settings API class.
32 *
33 * This handles registering, getting, and storing the general plugin options.
34 *
35 * Note that this is separate from the gateway settings.
36 *
37 * @since 2.0.0
38 */
39 class Settings extends \WC_Settings_API {
40
41
42 /**
43 * Square system of record.
44 *
45 * @var string square system of record indicator
46 */
47 const SYSTEM_OF_RECORD_SQUARE = 'square';
48
49 /**
50 * Woocommerce system of record.
51 *
52 * @var string square system of record indicator
53 */
54 const SYSTEM_OF_RECORD_WOOCOMMERCE = 'woocommerce';
55
56 /**
57 * Disabled system of record.
58 *
59 * @var string system of record indicator for disabled sync
60 */
61 const SYSTEM_OF_RECORD_DISABLED = 'disabled';
62
63
64 /**
65 * Refresh token
66 *
67 * @var string un-encrypted refresh token
68 */
69 protected $refresh_token;
70
71 /**
72 * Access token
73 *
74 * @var string un-encrypted access token
75 */
76 protected $access_token;
77
78 /**
79 * Square business locations
80 *
81 * @var array business locations returned by the API
82 */
83 protected $locations;
84
85 /**
86 * Square plugin instance
87 *
88 * @var Plugin plugin instance
89 */
90 protected $plugin;
91
92
93 /**
94 * Constructs the class.
95 *
96 * @since 2.0.0
97 *
98 * @param Plugin $plugin plugin instance.
99 */
100 public function __construct( Plugin $plugin ) {
101
102 $this->plugin = $plugin;
103 $this->plugin_id = 'wc_';
104 $this->id = $plugin->get_id();
105
106 $this->init_form_fields();
107
108 $this->init_settings();
109
110 // remove some of our custom fields that shouldn't be saved.
111 add_action(
112 'woocommerce_settings_api_sanitized_fields_' . $this->id,
113 function( $fields ) {
114
115 unset( $fields['general'], $fields['connect'], $fields['import_products'] );
116
117 if ( $this->is_sandbox() ) {
118 $this->update_access_token( $fields['sandbox_token'] );
119 $this->access_token = false; // Remove encrypted token.
120 $this->refresh_token = false; // Remove encrypted token.
121 }
122
123 $this->init_form_fields(); // Reload form fields after saving token.
124
125 return $fields;
126 }
127 );
128 }
129
130
131 /**
132 * Initializes the form fields.
133 *
134 * @since 2.0.0
135 */
136 public function init_form_fields() {
137
138 if ( $this->is_connected() ) {
139
140 $general_description = sprintf(
141 /* translators: Placeholders: %1$s - <a> tag, %2$s - </a> tag */
142 __( 'Sync your products and inventory and also accept credit and debit card payments at checkout. %1$sClick here%2$s to configure payments.', 'woocommerce-square' ),
143 '<a href="' . esc_url( $this->get_plugin()->get_payment_gateway_configuration_url( $this->get_plugin()->get_gateway()->get_id() ) ) . '">',
144 '</a>'
145 );
146
147 } else {
148
149 $general_description = __( 'Connect with Square to start syncing your products and inventory and also accept credit and debit card payments at checkout.', 'woocommerce-square' );
150 }
151
152 $fields = array(
153 'general' => array(
154 'type' => 'title',
155 'description' => $general_description,
156 ),
157 );
158
159 $fields['enable_sandbox'] = array(
160 'title' => __( 'Enable Sandbox Mode', 'woocommerce-square' ),
161 'label' => '<span>' . __( 'Enable to set the plugin in sandbox mode.', 'woocommerce-square' ) . '</span>',
162 'type' => 'checkbox',
163 'description' => __( 'After enabling you’ll see a new Sandbox settings section with two fields; Sandbox Application ID & Sandbox Access Token.', 'woocommerce-square' ),
164 );
165
166 $fields['sandbox_settings'] = array(
167 'type' => 'title',
168 'title' => __( 'Sandbox settings', 'woocommerce-square' ),
169 'id' => 'wc_square_sandbox_settings',
170 'description' => sprintf(
171 // translators: Placeholders: %1$s - URL.
172 __( 'Sandbox details can be created at: %s', 'woocommerce-square' ),
173 sprintf( '<a href="%1$s">%1$s</a>', 'https://developer.squareup.com/apps' )
174 ),
175 );
176
177 $fields['sandbox_application_id'] = array(
178 'type' => 'input',
179 'title' => __( 'Sandbox Application ID', 'woocommerce-square' ),
180 'class' => 'wc_square_sandbox_settings',
181 'description' => __( 'Application ID for the Sandbox Application, see the details in the My Applications section.', 'woocommerce-square' ),
182 );
183
184 $fields['sandbox_token'] = array(
185 'type' => 'input',
186 'title' => __( 'Sandbox Access Token', 'woocommerce-square' ),
187 'class' => 'wc_square_sandbox_settings',
188 'description' => __( 'Access Token for the Sandbox Test Account, see the details in the Sandbox Test Account section. Make sure you use the correct Sandbox Access Token for your application. For a given Sandbox Test Account, each Authorized Application is assigned a different Access Token.', 'woocommerce-square' ),
189 );
190
191 // display these fields only if connected.
192 if ( $this->is_connected() ) {
193
194 $fields[ $this->get_environment() . '_location_id' ] = array(
195 'title' => __( 'Business location', 'woocommerce-square' ),
196 'type' => 'select',
197 'class' => 'wc-enhanced-select',
198 'description' => sprintf(
199 /* translators: Placeholders: %1$s - <strong> tag, %2$s - </strong> tag, %3$s - <a> tag, %4$s - </a> tag */
200 __( 'Select a location to link to this site. Only %1$sactive%2$s %3$slocations%4$s that support credit card processing in Square can be linked.', 'woocommerce-square' ),
201 '<strong>',
202 '</strong>',
203 '<a href="https://docs.woocommerce.com/document/woocommerce-square/#section-4" target="_blank">',
204 '</a>'
205 ),
206 'options' => array(), // this is populated on display.
207 );
208
209 $fields['system_of_record'] = array(
210 'title' => __( 'Product system of record', 'woocommerce-square' ),
211 'type' => 'select',
212 'class' => 'wc-enhanced-select',
213 'description' => sprintf(
214 /* translators: Placeholders: %1$s - <strong> tag, %2$s - </strong> tag, %3$s - <a> tag, %4$s - </a> tag */
215 __( 'Choose where you will update data for synced products. Inventory in Square is %1$salways%2$s checked for adjustments when sync is enabled. %3$sClick here%4$s to read more about choosing a system of record.', 'woocommerce-square' ),
216 '<strong>',
217 '</strong>',
218 '<a href="' . esc_url( wc_square()->get_documentation_url() ) . '#section-7">',
219 '</a>'
220 ),
221 'options' => array(
222 self::SYSTEM_OF_RECORD_DISABLED => __( 'Do not sync product data', 'woocommerce-square' ),
223 self::SYSTEM_OF_RECORD_SQUARE => __( 'Square', 'woocommerce-square' ),
224 self::SYSTEM_OF_RECORD_WOOCOMMERCE => __( 'WooCommerce', 'woocommerce-square' ),
225 ),
226 'default' => 'disabled',
227 );
228
229 $fields['enable_inventory_sync'] = array(
230 'title' => __( 'Sync inventory', 'woocommerce-square' ),
231 'label' => '<span>' . __( 'Enable to sync product inventory with Square', 'woocommerce-square' ) . '</span>',
232 'type' => 'checkbox',
233 'description' => __( 'Inventory is fetched from Square periodically and updated in WooCommerce', 'woocommerce-square' ),
234 );
235
236 $fields['hide_missing_products'] = array(
237 'title' => __( 'Handle missing products', 'woocommerce-square' ),
238 'label' => __( 'Hide synced products when not found in Square', 'woocommerce-square' ),
239 'type' => 'checkbox',
240 'description' => __( 'Products not found in Square will be hidden in the WooCommerce product catalog.', 'woocommerce-square' ),
241 );
242
243 $fields['import_products'] = array(
244 'title' => __( 'Import Products', 'woocommerce-square' ),
245 'type' => 'import_products',
246 'desc_tip' => __( 'Run an import to create new products in this WooCommerce store for each new product created in Square that has a unique SKU not existing in here. Needs to be run each time new items are created in Square.', 'woocommerce-square' ),
247 );
248 }
249
250 // In sandbox mode we don't want to intially display the connect button, only disconnect.
251 if ( ! ( $this->is_sandbox() && ! $this->is_connected() ) ) {
252 $fields = array_merge(
253 $fields,
254 array(
255 'connect' => array(
256 'title' => __( 'Connection', 'woocommerce-square' ),
257 'type' => 'connect',
258 'desc_tip' => '',
259 ),
260 )
261 );
262 }
263
264 // Always display these fields.
265 $fields = array_merge(
266 $fields,
267 array(
268 'debug_logging_enabled' => array(
269 'title' => __( 'Enable Logging', 'woocommerce-square' ),
270 'type' => 'checkbox',
271 'label' => sprintf(
272 /* translators: Placeholders: %1$s - <a> tag, %2$s - </a> tag */
273 __( 'Log debug messages to the %1$sWooCommerce status log%2$s', 'woocommerce-square' ),
274 '<a href="' . esc_url( admin_url( 'admin.php?page=wc-status&tab=logs' ) ) . '">',
275 '</a>'
276 ),
277 ),
278 )
279 );
280
281 $this->form_fields = $fields;
282 }
283
284
285 /**
286 * Gets the form fields.
287 *
288 * Overridden to populate the Location settings options on display.
289 *
290 * @since 2.0.0
291 *
292 * @return array
293 */
294 public function get_form_fields() {
295
296 $fields = parent::get_form_fields();
297
298 // Confirm our local enable sandbox setting matches what is sent from the front end
299 // to account for changes from sandbox to production incorrectly fetching sandbox locations.
300 if ( $this->settings && isset( $_POST['wc_square_environment'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
301
302 $environment = 'yes' === $this->settings['enable_sandbox'] ? 'sandbox' : 'production';
303
304 if ( $environment !== $_POST['wc_square_environment'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
305 return $fields;
306 }
307 }
308
309 $location_id_field_key = '';
310 // Get the location_id field.
311 foreach ( $fields as $key => $value ) {
312 if ( strpos( $key, 'location_id' ) ) {
313 $location_id_field_key = $key;
314 break;
315 }
316 }
317
318 if ( did_action( 'wc_square_initialized' ) && $this->is_admin_settings_screen() && ! empty( $location_id_field_key ) ) {
319
320 $locations = array(
321 '' => __( 'Please choose a location', 'woocommerce-square' ),
322 );
323
324 if ( ! empty( $this->get_locations() ) ) {
325 foreach ( $this->get_locations() as $location ) {
326 if ( 'ACTIVE' === $location->getStatus() && in_array( 'CREDIT_CARD_PROCESSING', (array) $location->getCapabilities(), true ) ) {
327 $locations[ $location->getId() ] = $location->getName();
328 }
329 }
330 }
331
332 $fields[ $location_id_field_key ]['options'] = $locations;
333 }
334
335 return $fields;
336 }
337
338
339 /**
340 * Generates the HTML for import products button.
341 *
342 * @param string $id form id.
343 * @param array $field form fields.
344 */
345 public function generate_import_products_html( $id, $field ) {
346
347 ob_start();
348 ?>
349 <tr valign="top">
350 <th scope="row" class="titledesc">
351 <label for="<?php echo esc_attr( $id ); ?>"><?php echo wp_kses_post( $field['title'] ); ?> <?php echo $this->get_tooltip_html( $field ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></label>
352 </th>
353 <td class="forminp">
354 <a href='#' class='button js-import-square-products'>
355 <?php echo esc_html__( 'Import all products from Square', 'woocommerce-square' ); ?>
356 </a>
357 </td>
358 </tr>
359 <?php
360
361 return ob_get_clean();
362 }
363
364
365 /**
366 * Generates the Connection field HTML.
367 *
368 * @since 2.0.0
369 *
370 * @param string $id field ID.
371 * @param array $field field data.
372 * @return string
373 */
374 public function generate_connect_html( $id, $field ) {
375
376 ob_start();
377 ?>
378 <tr valign="top">
379 <th scope="row" class="titledesc">
380 <label for="<?php echo esc_attr( $id ); ?>"><?php echo wp_kses_post( $field['title'] ); ?> <?php echo $this->get_tooltip_html( $field ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></label>
381 </th>
382 <td class="forminp">
383 <?php
384 if ( $this->get_access_token() ) {
385 echo $this->get_plugin()->get_connection_handler()->get_disconnect_button_html(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
386 } else {
387 echo $this->get_plugin()->get_connection_handler()->get_connect_button_html( $this->is_sandbox() ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
388 }
389 ?>
390 </td>
391 </tr>
392 <?php
393
394 return ob_get_clean();
395 }
396
397
398 /**
399 * Updates the stored refresh token.
400 *
401 * @since 2.0.0
402 *
403 * @param string $token refresh token.
404 */
405 public function update_refresh_token( $token ) {
406
407 $refresh_tokens = $this->get_refresh_tokens();
408 $environment = $this->get_environment();
409
410 if ( ! empty( $token ) ) {
411
412 $this->refresh_token = $token;
413
414 if ( Utilities\Encryption_Utility::is_encryption_supported() ) {
415
416 $encryption = new Utilities\Encryption_Utility();
417
418 try {
419
420 $token = $encryption->encrypt_data( $token );
421
422 } catch ( Framework\SV_WC_Plugin_Exception $exception ) {
423
424 // log the event, but don't halt the process.
425 $this->get_plugin()->log( 'Could not encrypt refresh token. ' . $exception->getMessage() );
426 }
427 }
428
429 $refresh_tokens[ $environment ] = $token;
430 }
431
432 update_option( 'wc_square_refresh_tokens', $refresh_tokens );
433 }
434
435
436 /**
437 * Updates the stored access token.
438 *
439 * @since 2.0.0
440 *
441 * @param string $token access token.
442 */
443 public function update_access_token( $token ) {
444
445 $access_tokens = $this->get_access_tokens();
446 $environment = $this->get_environment();
447
448 if ( ! empty( $token ) ) {
449
450 $this->access_token = $token;
451
452 if ( Utilities\Encryption_Utility::is_encryption_supported() ) {
453
454 $encryption = new Utilities\Encryption_Utility();
455
456 try {
457
458 $token = $encryption->encrypt_data( $token );
459
460 } catch ( Framework\SV_WC_Plugin_Exception $exception ) {
461
462 // log the event, but don't halt the process.
463 $this->get_plugin()->log( 'Could not encrypt access token. ' . $exception->getMessage() );
464 }
465 }
466
467 $access_tokens[ $environment ] = $token;
468 } elseif ( isset( $access_tokens[ $environment ] ) ) {
469
470 unset( $access_tokens[ $environment ] );
471 }
472
473 update_option( 'wc_square_access_tokens', $access_tokens );
474 }
475
476
477 /**
478 * Clears any stored refresh tokens.
479 *
480 * @since 2.0.0
481 */
482 public function clear_refresh_tokens() {
483 delete_option( 'wc_square_refresh_tokens' );
484 }
485
486
487 /**
488 * Clears any stored access tokens.
489 *
490 * @since 2.0.0
491 */
492 public function clear_access_tokens() {
493
494 delete_option( 'wc_square_access_tokens' );
495 }
496
497
498 /**
499 * Clears the location ID from the settings.
500 *
501 * This is helpful on disconnect / revoke so that previously set location IDs don't stick around and cause confusion.
502 *
503 * @since 2.0.0
504 */
505 public function clear_location_id() {
506
507 $settings = get_option( $this->get_option_key(), array() );
508
509 $settings[ $this->get_environment() . '_location_id' ] = '';
510
511 update_option( $this->get_option_key(), $settings );
512 }
513
514
515 /** Conditional methods *******************************************************************************************/
516
517
518 /**
519 * Determines if WooCommerce is configured to be the system of record.
520 *
521 * @since 2.0.0
522 *
523 * @return bool
524 */
525 public function is_system_of_record_woocommerce() {
526
527 return self::SYSTEM_OF_RECORD_WOOCOMMERCE === $this->get_system_of_record();
528 }
529
530
531 /**
532 * Determines if Square is configured to be the system of record.
533 *
534 * @since 2.0.0
535 *
536 * @return bool
537 */
538 public function is_system_of_record_square() {
539
540 return self::SYSTEM_OF_RECORD_SQUARE === $this->get_system_of_record();
541 }
542
543
544 /**
545 * Determines if there is no system of record.
546 *
547 * @since 2.0.0
548 *
549 * @return bool
550 */
551 public function is_system_of_record_disabled() {
552
553 $sor = $this->get_system_of_record();
554
555 return empty( $sor ) || self::SYSTEM_OF_RECORD_DISABLED === $sor;
556 }
557
558
559 /**
560 * Determines if inventory sync is enabled.
561 *
562 * @since 2.0.0
563 *
564 * @return bool
565 */
566 public function is_inventory_sync_enabled() {
567
568 return (bool) apply_filters( 'wc_square_inventory_sync_enabled', 'yes' === get_option( 'woocommerce_manage_stock' ) && $this->is_product_sync_enabled() && 'yes' === $this->get_option( 'enable_inventory_sync' ) );
569 }
570
571
572 /**
573 * Determines if product sync is enabled.
574 *
575 * @since 2.0.0
576 *
577 * @return bool
578 */
579 public function is_product_sync_enabled() {
580
581 return ! $this->is_system_of_record_disabled();
582 }
583
584
585 /**
586 * Determines whether to hide products that don't exist in square from the catalog.
587 *
588 * @since 2.0.0
589 *
590 * @return bool
591 */
592 public function hide_missing_square_products() {
593
594 return 'yes' === $this->get_option( 'hide_missing_products' );
595 }
596
597
598 /**
599 * Determines if the plugin settings are fully configured.
600 *
601 * @since 2.0.0
602 *
603 * @return bool
604 */
605 public function is_configured() {
606
607 return $this->get_location_id() && $this->get_system_of_record();
608 }
609
610
611 /**
612 * Determines if the plugin is connected to Square.
613 *
614 * @since 2.0.0
615 *
616 * @return bool
617 */
618 public function is_connected() {
619
620 return (bool) $this->get_access_token();
621 }
622
623
624 /**
625 * Determines if configured in the sandbox environment.
626 *
627 * @since 2.0.0
628 *
629 * @return bool
630 */
631 public function is_sandbox() {
632
633 return 'sandbox' === $this->get_environment();
634 }
635
636
637 /**
638 * Determines if debug logging is enabled.
639 *
640 * @since 2.0.0
641 *
642 * @return bool
643 */
644 public function is_debug_enabled() {
645
646 return 'yes' === $this->get_option( 'debug_logging_enabled' );
647 }
648
649
650 /** Getter methods ************************************************************************************************/
651
652
653 /**
654 * Gets the configured location.
655 *
656 * @since 2.0.0
657 *
658 * @return string
659 */
660 public function get_location_id() {
661 $location_id = $this->get_option( $this->get_environment() . '_location_id' );
662
663 if ( empty( $location_id ) ) {
664 $square_db_version = get_option( $this->get_plugin()->get_plugin_version_name() );
665
666 // if the Square DB version is still pre-2.2.0, fetch the location ID using the previous option name
667 if ( ! empty( $square_db_version ) && version_compare( $square_db_version, '2.2.0', '<' ) ) {
668 $location_id = $this->get_option( 'location_id' );
669 }
670 }
671
672 return $location_id;
673 }
674
675
676 /**
677 * Gets the available locations.
678 *
679 * @since 2.0.0
680 *
681 * @return \SquareConnect\Model\Location[]
682 */
683 public function get_locations() {
684
685 if ( is_array( $this->locations ) ) {
686
687 return $this->locations;
688 }
689
690 // don't always need to refetch when not on Settings screen.
691 if ( ! $this->is_admin_settings_screen() ) {
692
693 $this->locations = get_transient( 'wc_square_locations' );
694 }
695
696 if ( ! is_array( $this->locations ) && did_action( 'wc_square_initialized' ) ) {
697
698 $this->locations = array();
699
700 try {
701
702 // cache the locations returned so they can be used elsewhere.
703 $this->locations = $this->get_plugin()->get_api( $this->get_access_token(), $this->is_sandbox() )->get_locations();
704 set_transient( 'wc_square_locations', $this->locations, HOUR_IN_SECONDS );
705
706 // check the returned IDs against what's currently configured.
707 $stored_location_id = $this->get_location_id();
708 $found = ! $stored_location_id;
709
710 foreach ( $this->locations as $location ) {
711
712 if ( $stored_location_id && $location->getId() === $stored_location_id ) {
713 $found = true;
714 break;
715 }
716 }
717
718 // if the currently set location ID is not present in the connected account's available locations, clear it locally.
719 if ( ! $found ) {
720 $this->clear_location_id();
721 }
722 } catch ( Framework\SV_WC_Plugin_Exception $exception ) {
723
724 $this->get_plugin()->log( 'Could not retrieve business locations.' );
725 }
726 }
727
728 return $this->locations;
729 }
730
731
732 /**
733 * Gets the configured system of record.
734 *
735 * @since 2.0.0
736 *
737 * @return string
738 */
739 public function get_system_of_record() {
740
741 return $this->get_option( 'system_of_record' );
742 }
743
744
745 /**
746 * Gets the configured system of record name.
747 *
748 * @since 2.0.0
749 *
750 * @return string or empty string if no system of record is configured
751 */
752 public function get_system_of_record_name() {
753
754 switch ( $this->get_system_of_record() ) {
755
756 case 'square':
757 $sor = __( 'Square', 'woocommerce-square' );
758 break;
759 case 'woocommerce':
760 $sor = __( 'WooCommerce', 'woocommerce-square' );
761 break;
762 default:
763 $sor = '';
764 break;
765 }
766
767 return $sor;
768 }
769
770 /**
771 * Gets the refresh token.
772 *
773 * @since 2.0.0
774 *
775 * @return string|null
776 */
777 public function get_refresh_token() {
778
779 if ( empty( $this->refresh_token ) ) {
780
781 $tokens = $this->get_refresh_tokens();
782 $token = null;
783
784 if ( ! empty( $tokens[ $this->get_environment() ] ) ) {
785 $token = $tokens[ $this->get_environment() ];
786 }
787
788 if ( $token && Utilities\Encryption_Utility::is_encryption_supported() ) {
789
790 $encryption = new Utilities\Encryption_Utility();
791
792 try {
793
794 $token = $encryption->decrypt_data( $token );
795
796 } catch ( Framework\SV_WC_Plugin_Exception $exception ) {
797
798 // log the event, but don't halt the process.
799 $this->get_plugin()->log( 'Could not decrypt refresh token. ' . $exception->getMessage() );
800 }
801 }
802
803 $this->refresh_token = $token;
804 }
805
806 /**
807 * Filters the configured refresh token.
808 *
809 * @since 2.0.0
810 *
811 * @param string $refresh_token
812 */
813 return apply_filters( 'wc_square_refresh_token', $this->refresh_token );
814 }
815
816 /**
817 * Gets the access token.
818 *
819 * @since 2.0.0
820 *
821 * @return string|null
822 */
823 public function get_access_token() {
824
825 if ( empty( $this->access_token ) || $this->is_admin_settings_screen() ) {
826
827 $tokens = $this->get_access_tokens();
828 $token = null;
829
830 if ( ! empty( $tokens[ $this->get_environment() ] ) ) {
831 $token = $tokens[ $this->get_environment() ];
832 }
833
834 if ( $token && Utilities\Encryption_Utility::is_encryption_supported() ) {
835
836 $encryption = new Utilities\Encryption_Utility();
837
838 try {
839
840 $token = $encryption->decrypt_data( $token );
841
842 } catch ( Framework\SV_WC_Plugin_Exception $exception ) {
843
844 // log the event, but don't halt the process.
845 $this->get_plugin()->log( 'Could not decrypt access token. ' . $exception->getMessage() );
846 }
847 }
848
849 $this->access_token = $token;
850 }
851
852 /**
853 * Filters the configured access token.
854 *
855 * @since 2.0.0
856 *
857 * @param string $access_token access token
858 */
859 return apply_filters( 'wc_square_access_token', $this->access_token );
860 }
861
862
863 /**
864 * Gets the stored access tokens.
865 *
866 * Each environment may have its own token.
867 *
868 * @since 2.0.0
869 *
870 * @return array
871 */
872 public function get_access_tokens() {
873 return (array) get_option( 'wc_square_access_tokens', array() );
874 }
875
876
877 /**
878 * Gets the stored refresh tokens.
879 *
880 * Each environment may have its own token.
881 *
882 * @since 2.0.0
883 *
884 * @return array
885 */
886 public function get_refresh_tokens() {
887 return (array) get_option( 'wc_square_refresh_tokens', array() );
888 }
889
890 /**
891 * Gets setting enabled sandbox.
892 *
893 * @since 2.1.2
894 *
895 * @return string
896 */
897 public function get_enable_sandbox() {
898 return $this->get_option( 'enable_sandbox' );
899 }
900
901 /**
902 * Tells is if the setting for enabling sandbox is checked.
903 *
904 * @since 2.1.2
905 *
906 * @return boolean
907 */
908 public function is_sandbox_setting_enabled() {
909 return 'yes' === $this->get_enable_sandbox();
910 }
911
912
913 /**
914 * Gets the configured environment.
915 *
916 * @since 2.0.0
917 *
918 * @return string
919 */
920 public function get_environment() {
921 $sanboxed = ( defined( 'WC_SQUARE_SANDBOX' ) && WC_SQUARE_SANDBOX ) || $this->is_sandbox_setting_enabled();
922 return $sanboxed ? 'sandbox' : 'production';
923 }
924
925
926 /**
927 * Gets the plugin instance.
928 *
929 * @since 2.0.0
930 *
931 * @return Plugin
932 */
933 public function get_plugin() {
934
935 return $this->plugin;
936 }
937
938 /**
939 * Determines if the current request is for the Square admin settings screen.
940 *
941 * @since 2.1.5
942 * @return bool True if the current request is for the Square admin settings, otherwise false.
943 */
944 public function is_admin_settings_screen() {
945 return isset( $_GET['page'], $_GET['tab'] ) && 'wc-settings' === $_GET['page'] && Plugin::PLUGIN_ID === $_GET['tab']; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
946 }
947 }
948