PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / trunk
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More vtrunk
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
← All changes | square/helpers/FrmSquareLiteConnectHelper.php +72 -58 6.26 → trunk View file →
@@ -21,8 +21,11 @@
21 21 $settings = FrmSquareLiteAppHelper::get_settings();
22 22
23 23 self::register_settings_scripts();
24 24
25 + FrmSquareLiteAppHelper::fee_education( 'square-global-settings-tip' );
26 +
27 + // phpcs:disable Generic.WhiteSpace.ScopeIndent
25 28 ?>
26 29 <table class="form-table" style="width: 400px;">
27 30 <tr class="form-field">
28 31 <td>
@@ -56,8 +59,9 @@
56 59 </em>
57 60 </div>
58 61 <?php } ?>
59 62 <?php
63 + // phpcs:enable Generic.WhiteSpace.ScopeIndent
60 64 }
61 65
62 66 /**
63 67 * @param string $mode
@@ -64,11 +68,12 @@
64 68 *
65 69 * @return void
66 70 */
67 71 private static function render_settings_for_mode( $mode ) {
72 + // phpcs:disable Generic.WhiteSpace.ScopeIndent
68 73 ?>
69 74 <div class="frm-card-item frm4">
70 - <div class="frm-flex-col">
75 + <div class="frm-flex-col" style="width: 100%;">
71 76 <div>
72 77 <span style="font-size: var(--text-lg); font-weight: 500; margin-right: 5px;">
73 78 <?php
74 79 echo $mode === 'test' ? esc_html__( 'Test', 'formidable' ) : esc_html__( 'Live', 'formidable' );
@@ -75,22 +80,15 @@
75 80 ?>
76 81 </span>
77 82 <?php
78 83
79 - $connected = (bool) self::get_merchant_id( $mode );
80 -
81 - $tag_classes = '';
82 -
83 - if ( $connected ) {
84 - $tag_classes = 'frm-lt-green-tag';
85 - } else {
86 - $tag_classes = 'frm-grey-tag';
87 - }
84 + $connected = (bool) self::get_merchant_id( $mode );
85 + $tag_classes = $connected ? 'frm-lt-green-tag' : 'frm-grey-tag';
88 86 ?>
89 87 <div class="frm-meta-tag <?php echo esc_attr( $tag_classes ); ?>" style="font-size: var(--text-sm); font-weight: 600;">
90 88 <?php
91 89 if ( $connected ) {
92 - FrmAppHelper::icon_by_class( 'frm_icon_font frm_checkmark_icon', array( 'style' => 'width: 10px; position: relative; top: 2px; margin-right: 5px;' ) );
90 + FrmAppHelper::icon_by_class( 'frmfont frm_checkmark_icon', array( 'style' => 'width: 10px; position: relative; top: 2px; margin-right: 5px;' ) );
93 91 echo 'Connected';
94 92 } else {
95 93 echo 'Not configured';
96 94 }
@@ -119,8 +117,9 @@
119 117 </div>
120 118 </div>
121 119 </div>
122 120 <?php
121 + // phpcs:enable Generic.WhiteSpace.ScopeIndent
123 122 }
124 123
125 124 /**
126 125 * @return void
@@ -125,12 +124,11 @@
125 124 /**
126 125 * @return void
127 126 */
128 127 private static function register_settings_scripts() {
129 - $script_url = FrmSquareLiteAppHelper::plugin_url() . '/js/settings.js';
130 - $dependencies = array( 'formidable_dom' );
131 - $plugin_version = FrmAppHelper::plugin_version();
132 - 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 );
133 131 wp_enqueue_script( 'formidable_square_settings' );
134 132 }
135 133
136 134 /**
@@ -136,9 +134,9 @@
136 134 /**
137 135 * @return false|string
138 136 */
139 137 public static function get_oauth_redirect_url() {
140 - $mode = FrmAppHelper::get_post_param( 'mode', 'test', 'sanitize_text_field' );
138 + $mode = self::get_mode_value_from_post();
141 139
142 140 if ( self::get_merchant_id( $mode ) ) {
143 141 // Do not allow for initialize if there is already a configured account id.
144 142 return false;
@@ -158,9 +156,9 @@
158 156 return false;
159 157 }
160 158
161 159 if ( ! empty( $data->password ) ) {
162 - 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 );
163 161 }
164 162
165 163 if ( ! is_object( $data ) || empty( $data->redirect_url ) ) {
166 164 return false;
@@ -183,12 +181,8 @@
183 181 $body = array_merge( $body, $additional_body );
184 182 $url = self::get_url_to_connect_server();
185 183 $headers = self::build_headers_for_post();
186 184
187 - if ( ! $headers ) {
188 - return 'Unable to build headers for post. Is your pro license configured properly?';
189 - }
190 -
191 185 // (Seconds) default timeout is 5. we want a bit more time to work with.
192 186 $timeout = 45;
193 187
194 188 self::try_to_extend_server_timeout( $timeout );
@@ -205,8 +199,9 @@
205 199 if ( empty( $body->success ) ) {
206 200 if ( ! empty( $body->data ) && is_string( $body->data ) ) {
207 201 return $body->data;
208 202 }
203 +
209 204 return 'Response from server was not successful';
210 205 }
211 206
212 207 return $body->data ?? array();
@@ -281,13 +276,9 @@
281 276 * @return string
282 277 */
283 278 private static function strip_lang_from_url( $url ) {
284 279 $split_on_language = explode( '/?lang=', $url );
285 -
286 - if ( 2 === count( $split_on_language ) ) {
287 - $url = $split_on_language[0];
288 - }
289 - return $url;
280 + return 2 === count( $split_on_language ) ? $split_on_language[0] : $url;
290 281 }
291 282
292 283 /**
293 284 * WPML alters the output of home_url.
@@ -318,8 +309,9 @@
318 309 if ( $pro_license ) {
319 310 $password = $pro_license;
320 311 }
321 312 }
313 +
322 314 return ! empty( $password ) ? $password : false;
323 315 }
324 316
325 317 /**
@@ -352,9 +344,9 @@
352 344 * @return string the client password.
353 345 */
354 346 private static function generate_client_password( $mode ) {
355 347 $client_password = wp_generate_password();
356 - 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 );
357 349 return $client_password;
358 350 }
359 351
360 352 /**
@@ -481,19 +473,19 @@
481 473 );
482 474 $data = self::post_to_connect_server( 'oauth_merchant_status', $body );
483 475
484 476 if ( is_object( $data ) && ! empty( $data->merchant_id ) ) {
485 - 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 );
486 478
487 479 $currency = self::get_merchant_currency( true, $mode );
488 480 $location_id = self::get_location_id( true, $mode );
489 481
490 482 if ( $currency ) {
491 - update_option( self::get_merchant_currency_option_name( $mode ), $currency, 'no' );
483 + update_option( self::get_merchant_currency_option_name( $mode ), $currency, false );
492 484 }
493 485
494 486 if ( $location_id ) {
495 - 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 );
496 488 }
497 489
498 490 FrmTransLiteAppController::install();
499 491
@@ -539,10 +531,10 @@
539 531 return $response;
540 532 }
541 533
542 534 if ( is_array( $response ) ) {
543 - // reformat empty arrays as empty objects
544 - // 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.
545 537 return new stdClass();
546 538 }
547 539
548 540 if ( is_string( $response ) ) {
@@ -550,8 +542,9 @@
550 542 FrmTransLiteLog::log_message( 'Square API Error', $response );
551 543 } else {
552 544 self::$latest_error_from_square_api = '';
553 545 }
546 +
554 547 return false;
555 548 }
556 549
557 550 /**
@@ -557,32 +550,52 @@
557 550 /**
558 551 * @return array
559 552 */
560 553 private static function get_standard_authenticated_body() {
561 - $mode = self::get_mode_value_from_post();
562 - return array(
563 - 'merchant_id' => get_option( self::get_merchant_id_option_name( $mode ) ),
564 - 'server_password' => get_option( self::get_server_side_token_option_name( $mode ) ),
565 - 'client_password' => get_option( self::get_client_side_token_option_name( $mode ) ),
566 - );
554 + return self::get_body_for_mode( FrmSquareLiteAppHelper::active_mode() );
567 555 }
568 556
569 557 /**
570 - * 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.
571 560 *
572 561 * @return string 'test' or 'live'
573 562 */
574 563 private static function get_mode_value_from_post() {
575 564 // phpcs:ignore WordPress.Security.NonceVerification.Missing
576 - if ( empty( $_POST ) || ! array_key_exists( 'testMode', $_POST ) ) {
577 - 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';
578 568 }
579 569
580 - $test_mode = FrmAppHelper::get_param( 'testMode', '', 'post', 'absint' );
581 - 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();
582 577 }
583 578
584 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 + /**
585 598 * @return string|null
586 599 */
587 600 public static function get_latest_error_from_square_api() {
588 601 return self::$latest_error_from_square_api;
@@ -590,12 +603,13 @@
590 603
591 604 /**
592 605 * @param string $receipt_id
593 606 *
594 - * @return false|object
607 + * @return bool
595 608 */
596 609 public static function refund_payment( $receipt_id ) {
597 - 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 );
598 612 }
599 613
600 614 /**
601 615 * @param array $info
@@ -623,16 +637,15 @@
623 637
624 638 $request_body = array();
625 639
626 640 if ( 'auto' !== $mode ) {
627 - $_POST['testMode'] = 'test' === $mode ? 1 : 0;
628 - $request_body['frm_square_api_mode'] = $mode;
641 + $request_body = self::get_body_for_mode( $mode );
629 642 }
630 643
631 644 $response = self::post_with_authenticated_body( 'get_location_id', $request_body );
632 645
633 646 if ( is_object( $response ) ) {
634 - 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 );
635 648 return $response->id;
636 649 }
637 650
638 651 return false;
@@ -646,8 +659,9 @@
646 659
647 660 if ( false === $data || empty( $data->event_ids ) ) {
648 661 return array();
649 662 }
663 +
650 664 return $data->event_ids;
651 665 }
652 666
653 667 /**
@@ -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,15 +805,15 @@
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 }
815 +
803 816 wp_send_json_error();
804 817 }
805 818
806 819 /**
@@ -813,7 +826,8 @@
813 826
814 827 if ( is_object( $response ) && is_object( $response->subscription ) ) {
815 828 return $response->subscription;
816 829 }
830 +
817 831 return false;
818 832 }
819 833 }