PluginProbe
WooCommerce Square / 3.4.2
WooCommerce Square v3.4.2
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 All 132 releases
woocommerce-square / includes / Settings.php

Settings.php in WooCommerce Square 3.4.2, at includes/Settings.php

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