| @@ -23,8 +23,9 @@ | ||
| 23 | 23 | self::register_settings_scripts(); |
| 24 | 24 | |
| 25 | 25 | FrmSquareLiteAppHelper::fee_education( 'square-global-settings-tip' ); |
| 26 | 26 | |
| 27 | + // phpcs:disable Generic.WhiteSpace.ScopeIndent | |
| 27 | 28 | ?> |
| 28 | 29 | <table class="form-table" style="width: 400px;"> |
| 29 | 30 | <tr class="form-field"> |
| 30 | 31 | <td> |
| @@ -58,8 +59,9 @@ | ||
| 58 | 59 | </em> |
| 59 | 60 | </div> |
| 60 | 61 | <?php } ?> |
| 61 | 62 | <?php |
| 63 | + // phpcs:enable Generic.WhiteSpace.ScopeIndent | |
| 62 | 64 | } |
| 63 | 65 | |
| 64 | 66 | /** |
| 65 | 67 | * @param string $mode |
| @@ -66,11 +68,12 @@ | ||
| 66 | 68 | * |
| 67 | 69 | * @return void |
| 68 | 70 | */ |
| 69 | 71 | private static function render_settings_for_mode( $mode ) { |
| 72 | + // phpcs:disable Generic.WhiteSpace.ScopeIndent | |
| 70 | 73 | ?> |
| 71 | 74 | <div class="frm-card-item frm4"> |
| 72 | - <div class="frm-flex-col"> | |
| 75 | + <div class="frm-flex-col" style="width: 100%;"> | |
| 73 | 76 | <div> |
| 74 | 77 | <span style="font-size: var(--text-lg); font-weight: 500; margin-right: 5px;"> |
| 75 | 78 | <?php |
| 76 | 79 | echo $mode === 'test' ? esc_html__( 'Test', 'formidable' ) : esc_html__( 'Live', 'formidable' ); |
| @@ -114,8 +117,9 @@ | ||
| 114 | 117 | </div> |
| 115 | 118 | </div> |
| 116 | 119 | </div> |
| 117 | 120 | <?php |
| 121 | + // phpcs:enable Generic.WhiteSpace.ScopeIndent | |
| 118 | 122 | } |
| 119 | 123 | |
| 120 | 124 | /** |
| 121 | 125 | * @return void |
| @@ -120,12 +124,11 @@ | ||
| 120 | 124 | /** |
| 121 | 125 | * @return void |
| 122 | 126 | */ |
| 123 | 127 | private static function register_settings_scripts() { |
| 124 | - $script_url = FrmSquareLiteAppHelper::plugin_url() . '/js/settings.js'; | |
| 125 | - $dependencies = array( 'formidable_dom' ); | |
| 126 | - $plugin_version = FrmAppHelper::plugin_version(); | |
| 127 | - wp_register_script( 'formidable_square_settings', $script_url, $dependencies, $plugin_version, true ); | |
| 128 | + $script_url = FrmSquareLiteAppHelper::plugin_url() . '/js/settings.js'; | |
| 129 | + $dependencies = array( 'formidable_dom' ); | |
| 130 | + wp_register_script( 'formidable_square_settings', $script_url, $dependencies, FrmAppHelper::plugin_version(), true ); | |
| 128 | 131 | wp_enqueue_script( 'formidable_square_settings' ); |
| 129 | 132 | } |
| 130 | 133 | |
| 131 | 134 | /** |
| @@ -131,9 +134,9 @@ | ||
| 131 | 134 | /** |
| 132 | 135 | * @return false|string |
| 133 | 136 | */ |
| 134 | 137 | public static function get_oauth_redirect_url() { |
| 135 | - $mode = FrmAppHelper::get_post_param( 'mode', 'test', 'sanitize_text_field' ); | |
| 138 | + $mode = self::get_mode_value_from_post(); | |
| 136 | 139 | |
| 137 | 140 | if ( self::get_merchant_id( $mode ) ) { |
| 138 | 141 | // Do not allow for initialize if there is already a configured account id. |
| 139 | 142 | return false; |
| @@ -153,9 +156,9 @@ | ||
| 153 | 156 | return false; |
| 154 | 157 | } |
| 155 | 158 | |
| 156 | 159 | if ( ! empty( $data->password ) ) { |
| 157 | - update_option( self::get_server_side_token_option_name( $mode ), $data->password, 'no' ); | |
| 160 | + update_option( self::get_server_side_token_option_name( $mode ), $data->password, false ); | |
| 158 | 161 | } |
| 159 | 162 | |
| 160 | 163 | if ( ! is_object( $data ) || empty( $data->redirect_url ) ) { |
| 161 | 164 | return false; |
| @@ -178,12 +181,8 @@ | ||
| 178 | 181 | $body = array_merge( $body, $additional_body ); |
| 179 | 182 | $url = self::get_url_to_connect_server(); |
| 180 | 183 | $headers = self::build_headers_for_post(); |
| 181 | 184 | |
| 182 | - if ( ! $headers ) { | |
| 183 | - return 'Unable to build headers for post. Is your pro license configured properly?'; | |
| 184 | - } | |
| 185 | - | |
| 186 | 185 | // (Seconds) default timeout is 5. we want a bit more time to work with. |
| 187 | 186 | $timeout = 45; |
| 188 | 187 | |
| 189 | 188 | self::try_to_extend_server_timeout( $timeout ); |
| @@ -277,14 +276,9 @@ | ||
| 277 | 276 | * @return string |
| 278 | 277 | */ |
| 279 | 278 | private static function strip_lang_from_url( $url ) { |
| 280 | 279 | $split_on_language = explode( '/?lang=', $url ); |
| 281 | - | |
| 282 | - if ( 2 === count( $split_on_language ) ) { | |
| 283 | - $url = $split_on_language[0]; | |
| 284 | - } | |
| 285 | - | |
| 286 | - return $url; | |
| 280 | + return 2 === count( $split_on_language ) ? $split_on_language[0] : $url; | |
| 287 | 281 | } |
| 288 | 282 | |
| 289 | 283 | /** |
| 290 | 284 | * WPML alters the output of home_url. |
| @@ -350,9 +344,9 @@ | ||
| 350 | 344 | * @return string the client password. |
| 351 | 345 | */ |
| 352 | 346 | private static function generate_client_password( $mode ) { |
| 353 | 347 | $client_password = wp_generate_password(); |
| 354 | - update_option( self::get_client_side_token_option_name( $mode ), $client_password, 'no' ); | |
| 348 | + update_option( self::get_client_side_token_option_name( $mode ), $client_password, false ); | |
| 355 | 349 | return $client_password; |
| 356 | 350 | } |
| 357 | 351 | |
| 358 | 352 | /** |
| @@ -479,19 +473,19 @@ | ||
| 479 | 473 | ); |
| 480 | 474 | $data = self::post_to_connect_server( 'oauth_merchant_status', $body ); |
| 481 | 475 | |
| 482 | 476 | if ( is_object( $data ) && ! empty( $data->merchant_id ) ) { |
| 483 | - update_option( self::get_merchant_id_option_name( $mode ), $data->merchant_id, 'no' ); | |
| 477 | + update_option( self::get_merchant_id_option_name( $mode ), $data->merchant_id, false ); | |
| 484 | 478 | |
| 485 | 479 | $currency = self::get_merchant_currency( true, $mode ); |
| 486 | 480 | $location_id = self::get_location_id( true, $mode ); |
| 487 | 481 | |
| 488 | 482 | if ( $currency ) { |
| 489 | - update_option( self::get_merchant_currency_option_name( $mode ), $currency, 'no' ); | |
| 483 | + update_option( self::get_merchant_currency_option_name( $mode ), $currency, false ); | |
| 490 | 484 | } |
| 491 | 485 | |
| 492 | 486 | if ( $location_id ) { |
| 493 | - update_option( self::get_location_id_option_name( $mode ), $location_id, 'no' ); | |
| 487 | + update_option( self::get_location_id_option_name( $mode ), $location_id, false ); | |
| 494 | 488 | } |
| 495 | 489 | |
| 496 | 490 | FrmTransLiteAppController::install(); |
| 497 | 491 | |
| @@ -537,10 +531,10 @@ | ||
| 537 | 531 | return $response; |
| 538 | 532 | } |
| 539 | 533 | |
| 540 | 534 | if ( is_array( $response ) ) { |
| 541 | - // reformat empty arrays as empty objects | |
| 542 | - // if the response is an array, it's because it's empty. Everything with data is already an object. | |
| 535 | + // Reformat empty arrays as empty objects | |
| 536 | + // If the response is an array, it's because it's empty. Everything with data is already an object. | |
| 543 | 537 | return new stdClass(); |
| 544 | 538 | } |
| 545 | 539 | |
| 546 | 540 | if ( is_string( $response ) ) { |
| @@ -556,32 +550,52 @@ | ||
| 556 | 550 | /** |
| 557 | 551 | * @return array |
| 558 | 552 | */ |
| 559 | 553 | private static function get_standard_authenticated_body() { |
| 560 | - $mode = self::get_mode_value_from_post(); | |
| 561 | - return array( | |
| 562 | - 'merchant_id' => get_option( self::get_merchant_id_option_name( $mode ) ), | |
| 563 | - 'server_password' => get_option( self::get_server_side_token_option_name( $mode ) ), | |
| 564 | - 'client_password' => get_option( self::get_client_side_token_option_name( $mode ) ), | |
| 565 | - ); | |
| 554 | + return self::get_body_for_mode( FrmSquareLiteAppHelper::active_mode() ); | |
| 566 | 555 | } |
| 567 | 556 | |
| 568 | 557 | /** |
| 569 | - * Check $_POST for live or test mode value as it can be updated in real time from Stripe Settings and can be configured before the update is saved. | |
| 558 | + * Check $_POST for live or test mode value as it can be updated in real time from Square Settings and can be configured before the update is saved. | |
| 559 | + * The connect request sends a 'mode' string, and the disconnect request sends a 'testMode' flag, so check for both. | |
| 570 | 560 | * |
| 571 | 561 | * @return string 'test' or 'live' |
| 572 | 562 | */ |
| 573 | 563 | private static function get_mode_value_from_post() { |
| 574 | 564 | // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 575 | - if ( empty( $_POST ) || ! array_key_exists( 'testMode', $_POST ) ) { | |
| 576 | - return FrmSquareLiteAppHelper::active_mode(); | |
| 565 | + if ( array_key_exists( 'mode', $_POST ) ) { | |
| 566 | + $mode = FrmAppHelper::get_post_param( 'mode', '', 'sanitize_text_field' ); | |
| 567 | + return 'test' === $mode ? 'test' : 'live'; | |
| 577 | 568 | } |
| 578 | 569 | |
| 579 | - $test_mode = FrmAppHelper::get_param( 'testMode', '', 'post', 'absint' ); | |
| 580 | - return $test_mode ? 'test' : 'live'; | |
| 570 | + // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 571 | + if ( array_key_exists( 'testMode', $_POST ) ) { | |
| 572 | + $test_mode = FrmAppHelper::get_post_param( 'testMode', '', 'absint' ); | |
| 573 | + return $test_mode ? 'test' : 'live'; | |
| 574 | + } | |
| 575 | + | |
| 576 | + return FrmSquareLiteAppHelper::active_mode(); | |
| 581 | 577 | } |
| 582 | 578 | |
| 583 | 579 | /** |
| 580 | + * Get the standard body with account id, mode, and passwords to send to the connect server. | |
| 581 | + * | |
| 582 | + * @since 6.32.1 | |
| 583 | + * | |
| 584 | + * @param string $mode 'live' or 'test'. | |
| 585 | + * | |
| 586 | + * @return array | |
| 587 | + */ | |
| 588 | + private static function get_body_for_mode( $mode ) { | |
| 589 | + return array( | |
| 590 | + 'merchant_id' => get_option( self::get_merchant_id_option_name( $mode ) ), | |
| 591 | + 'server_password' => get_option( self::get_server_side_token_option_name( $mode ) ), | |
| 592 | + 'client_password' => get_option( self::get_client_side_token_option_name( $mode ) ), | |
| 593 | + 'frm_square_api_mode' => $mode, | |
| 594 | + ); | |
| 595 | + } | |
| 596 | + | |
| 597 | + /** | |
| 584 | 598 | * @return string|null |
| 585 | 599 | */ |
| 586 | 600 | public static function get_latest_error_from_square_api() { |
| 587 | 601 | return self::$latest_error_from_square_api; |
| @@ -589,12 +603,13 @@ | ||
| 589 | 603 | |
| 590 | 604 | /** |
| 591 | 605 | * @param string $receipt_id |
| 592 | 606 | * |
| 593 | - * @return false|object | |
| 607 | + * @return bool | |
| 594 | 608 | */ |
| 595 | 609 | public static function refund_payment( $receipt_id ) { |
| 596 | - return self::post_with_authenticated_body( 'refund_payment', array( 'receipt_id' => $receipt_id ) ); | |
| 610 | + $data = self::post_with_authenticated_body( 'refund_payment', array( 'receipt_id' => $receipt_id ) ); | |
| 611 | + return is_object( $data ); | |
| 597 | 612 | } |
| 598 | 613 | |
| 599 | 614 | /** |
| 600 | 615 | * @param array $info |
| @@ -622,16 +637,15 @@ | ||
| 622 | 637 | |
| 623 | 638 | $request_body = array(); |
| 624 | 639 | |
| 625 | 640 | if ( 'auto' !== $mode ) { |
| 626 | - $_POST['testMode'] = 'test' === $mode ? 1 : 0; | |
| 627 | - $request_body['frm_square_api_mode'] = $mode; | |
| 641 | + $request_body = self::get_body_for_mode( $mode ); | |
| 628 | 642 | } |
| 629 | 643 | |
| 630 | 644 | $response = self::post_with_authenticated_body( 'get_location_id', $request_body ); |
| 631 | 645 | |
| 632 | 646 | if ( is_object( $response ) ) { |
| 633 | - update_option( self::get_location_id_option_name( $mode ), $response->id, 'no' ); | |
| 647 | + update_option( self::get_location_id_option_name( $mode ), $response->id, false ); | |
| 634 | 648 | return $response->id; |
| 635 | 649 | } |
| 636 | 650 | |
| 637 | 651 | return false; |
| @@ -702,17 +716,18 @@ | ||
| 702 | 716 | |
| 703 | 717 | /** |
| 704 | 718 | * @param string $subscription_id |
| 705 | 719 | * |
| 706 | - * @return false|object | |
| 720 | + * @return bool | |
| 707 | 721 | */ |
| 708 | 722 | public static function cancel_subscription( $subscription_id ) { |
| 709 | - return self::post_with_authenticated_body( 'cancel_subscription', compact( 'subscription_id' ) ); | |
| 723 | + return false !== self::post_with_authenticated_body( 'cancel_subscription', compact( 'subscription_id' ) ); | |
| 710 | 724 | } |
| 711 | 725 | |
| 712 | 726 | public static function handle_disconnect() { |
| 713 | 727 | self::disconnect(); |
| 714 | 728 | self::reset_square_api_integration(); |
| 729 | + FrmTransLiteAppHelper::trigger_gateway_disconnected_hook( 'square', self::get_mode_value_from_post() ); | |
| 715 | 730 | wp_send_json_success(); |
| 716 | 731 | } |
| 717 | 732 | |
| 718 | 733 | /** |
| @@ -718,11 +733,10 @@ | ||
| 718 | 733 | /** |
| 719 | 734 | * @return false|object |
| 720 | 735 | */ |
| 721 | 736 | private static function disconnect() { |
| 722 | - $additional_body = array( | |
| 723 | - 'frm_square_api_mode' => self::get_mode_value_from_post(), | |
| 724 | - ); | |
| 737 | + $mode = self::get_mode_value_from_post(); | |
| 738 | + $additional_body = self::get_body_for_mode( $mode ); | |
| 725 | 739 | return self::post_with_authenticated_body( 'disconnect', $additional_body ); |
| 726 | 740 | } |
| 727 | 741 | |
| 728 | 742 | /** |
| @@ -756,16 +770,15 @@ | ||
| 756 | 770 | |
| 757 | 771 | $request_body = array(); |
| 758 | 772 | |
| 759 | 773 | if ( 'auto' !== $mode ) { |
| 760 | - $_POST['testMode'] = 'test' === $mode ? 1 : 0; | |
| 761 | - $request_body['frm_square_api_mode'] = $mode; | |
| 774 | + $request_body = self::get_body_for_mode( $mode ); | |
| 762 | 775 | } |
| 763 | 776 | |
| 764 | 777 | $response = self::post_with_authenticated_body( 'get_merchant_currency', $request_body ); |
| 765 | 778 | |
| 766 | 779 | if ( is_object( $response ) && ! empty( $response->currency ) ) { |
| 767 | - update_option( self::get_merchant_currency_option_name( $mode ), $response->currency, 'no' ); | |
| 780 | + update_option( self::get_merchant_currency_option_name( $mode ), $response->currency, false ); | |
| 768 | 781 | return $response->currency; |
| 769 | 782 | } |
| 770 | 783 | |
| 771 | 784 | return false; |
| @@ -792,13 +805,12 @@ | ||
| 792 | 805 | } |
| 793 | 806 | |
| 794 | 807 | $site_identifier = FrmAppHelper::get_post_param( 'site_identifier' ); |
| 795 | 808 | $usage = new FrmUsage(); |
| 796 | - $uuid = $usage->uuid(); | |
| 797 | 809 | |
| 798 | 810 | update_option( $option_name, time() ); |
| 799 | 811 | |
| 800 | - if ( $site_identifier === $uuid ) { | |
| 812 | + if ( $site_identifier === $usage->uuid() ) { | |
| 801 | 813 | wp_send_json_success(); |
| 802 | 814 | } |
| 803 | 815 | |
| 804 | 816 | wp_send_json_error(); |