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/controllers/FrmSquareLiteActionsController.php +68 -44 6.25 → trunk View file →
@@ -9,8 +9,9 @@
9 9 * @since 6.22
10 10 *
11 11 * @param string $callback
12 12 * @param array|false|object $field
13 + *
13 14 * @return string
14 15 */
15 16 public static function maybe_show_card( $callback, $field = false ) {
16 17 if ( false === $field ) {
@@ -19,13 +20,15 @@
19 20 }
20 21
21 22 $form_id = is_object( $field ) ? $field->form_id : $field['form_id'];
22 23 $actions = self::get_actions_before_submit( $form_id );
23 - if ( empty( $actions ) ) {
24 +
25 + if ( ! $actions ) {
24 26 return $callback;
25 27 }
26 28
27 29 $field_id = is_object( $field ) ? $field->id : $field['id'];
30 +
28 31 foreach ( $actions as $action ) {
29 32 if ( (int) $action->post_content['credit_card'] === (int) $field_id ) {
30 33 return self::class . '::show_card';
31 34 }
@@ -41,8 +44,9 @@
41 44 *
42 45 * @param array $field
43 46 * @param string $field_name
44 47 * @param array $atts
48 + *
45 49 * @return void
46 50 */
47 51 public static function show_card( $field, $field_name, $atts ) {
48 52 $actions = self::get_actions_before_submit( $field['form_id'] );
@@ -67,19 +71,23 @@
67 71 *
68 72 * @since 6.22
69 73 *
70 74 * @param int|string $form_id
75 + *
71 76 * @return array
72 77 */
73 78 public static function get_actions_before_submit( $form_id ) {
74 79 $payment_actions = self::get_actions_for_form( $form_id );
80 +
75 81 foreach ( $payment_actions as $k => $payment_action ) {
76 82 $gateway = $payment_action->post_content['gateway'];
77 83 $is_square = $gateway === 'square' || ( is_array( $gateway ) && in_array( 'square', $gateway, true ) );
84 +
78 85 if ( ! $is_square || empty( $payment_action->post_content['amount'] ) ) {
79 86 unset( $payment_actions[ $k ] );
80 87 }
81 88 }
89 +
82 90 return $payment_actions;
83 91 }
84 92
85 93 /**
@@ -88,8 +96,9 @@
88 96 *
89 97 * @param WP_Post $action
90 98 * @param stdClass $entry
91 99 * @param mixed $form
100 + *
92 101 * @return array
93 102 */
94 103 public static function trigger_gateway( $action, $entry, $form ) {
95 104 $response = array(
@@ -97,17 +106,18 @@
97 106 'run_triggers' => false,
98 107 'show_errors' => true,
99 108 );
100 109 $atts = compact( 'action', 'entry', 'form' );
110 + $amount = self::prepare_amount( $action->post_content['amount'], $atts );
101 111
102 - $amount = self::prepare_amount( $action->post_content['amount'], $atts );
103 - if ( empty( $amount ) || $amount == 000 ) {
112 + // phpcs:ignore Universal.Operators.StrictComparisons
113 + if ( ! $amount || $amount == 000 ) {
104 114 $response['error'] = __( 'Please specify an amount for the payment', 'formidable' );
105 115 return $response;
106 116 }
107 117
108 118 if ( ! self::square_is_configured() ) {
109 - $response['error'] = __( 'There was a problem communicating with Square. Please try again.', 'formidable' );
119 + $response['error'] = __( 'Square still needs to be configured.', 'formidable' );
110 120 return $response;
111 121 }
112 122
113 123 $payment_args = compact( 'form', 'entry', 'action', 'amount' );
@@ -115,10 +125,9 @@
115 125 // Attempt to charge the customer's card.
116 126 if ( 'recurring' === $action->post_content['type'] ) {
117 127 $charge = self::trigger_recurring_payment( $payment_args );
118 128 } else {
119 - $charge = self::trigger_one_time_payment( $payment_args );
120 - $response['run_triggers'] = true;
129 + $charge = self::trigger_one_time_payment( $payment_args );
121 130 }
122 131
123 132 if ( $charge === true ) {
124 133 $response['success'] = true;
@@ -132,8 +141,9 @@
132 141 /**
133 142 * Trigger a one time payment.
134 143 *
135 144 * @param array $atts The arguments for the payment.
145 + *
136 146 * @return string|true string on error, true on success.
137 147 */
138 148 private static function trigger_one_time_payment( $atts ) {
139 149 if ( empty( $_POST['square-token'] ) || empty( $_POST['square-verification-token'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
@@ -174,8 +184,9 @@
174 184 /**
175 185 * Add a payment row for the payments table.
176 186 *
177 187 * @param array $atts The arguments for the payment.
188 + *
178 189 * @return int
179 190 */
180 191 private static function create_new_payment( $atts ) {
181 192 $atts['charge'] = (object) $atts['charge'];
@@ -186,15 +197,14 @@
186 197 'paysys' => 'square',
187 198 'item_id' => $atts['entry']->id,
188 199 'action_id' => $atts['action']->ID,
189 200 'receipt_id' => $atts['charge']->id,
190 - 'sub_id' => isset( $atts['charge']->sub_id ) ? $atts['charge']->sub_id : '',
201 + 'sub_id' => $atts['charge']->sub_id ?? '',
191 202 'test' => 'test' === FrmSquareLiteAppHelper::active_mode() ? 1 : 0,
192 203 );
193 204
194 205 $frm_payment = new FrmTransLitePayment();
195 - $payment_id = $frm_payment->create( $new_values );
196 - return $payment_id;
206 + return $frm_payment->create( $new_values );
197 207 }
198 208
199 209 /**
200 210 * Create a new Square subscription and a subscription and payment for the payments tables.
@@ -199,8 +209,9 @@
199 209 /**
200 210 * Create a new Square subscription and a subscription and payment for the payments tables.
201 211 *
202 212 * @param array $atts Includes 'customer', 'entry', 'action', 'amount'.
213 + *
203 214 * @return bool|string True on success, error message on failure
204 215 */
205 216 private static function trigger_recurring_payment( $atts ) {
206 217 if ( empty( $_POST['square-token'] ) || empty( $_POST['square-verification-token'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
@@ -250,8 +261,9 @@
250 261 );
251 262 }
252 263
253 264 $response = FrmSquareLiteConnectHelper::create_subscription( $info );
265 +
254 266 if ( false === $response ) {
255 267 return FrmSquareLiteConnectHelper::get_latest_error_from_square_api();
256 268 }
257 269
@@ -264,9 +276,9 @@
264 276 }
265 277
266 278 // Add subscription database row.
267 279 // We do not add a payment row at this time. This is handled with our webhook handling.
268 - $subscription_id = self::create_new_subscription( $response->id, $atts );
280 + self::create_new_subscription( $response->id, $atts );
269 281
270 282 return true;
271 283 }
272 284
@@ -274,8 +286,9 @@
274 286 * Prepare the description for a subscription.
275 287 *
276 288 * @param string $description
277 289 * @param array $atts
290 + *
278 291 * @return string
279 292 */
280 293 private static function prepare_subscription_description( $description, $atts ) {
281 294 $shortcode_atts = array(
@@ -290,8 +303,9 @@
290 303 * Create a new subscription and payment for the payments tables.
291 304 *
292 305 * @param string $subscription_id
293 306 * @param array $atts
307 + *
294 308 * @return int
295 309 */
296 310 private static function create_new_subscription( $subscription_id, $atts ) {
297 311 $repeat_cadence = $atts['action']->post_content['repeat_cadence'] ?? 'DAILY';
@@ -311,15 +325,15 @@
311 325 'test' => 'test' === FrmSquareLiteAppHelper::active_mode() ? 1 : 0,
312 326 );
313 327
314 328 $frm_payment = new FrmTransLiteSubscription();
315 - $payment_id = $frm_payment->create( $new_values );
316 329
317 - return $payment_id;
330 + return $frm_payment->create( $new_values );
318 331 }
319 332
320 333 /**
321 334 * @param string $repeat_cadence
335 + *
322 336 * @return int
323 337 */
324 338 private static function get_interval_count_from_repeat_cadence( $repeat_cadence ) {
325 339 switch ( $repeat_cadence ) {
@@ -349,8 +363,9 @@
349 363 }
350 364
351 365 /**
352 366 * @param string $repeat_cadence
367 + *
353 368 * @return string
354 369 */
355 370 private static function get_interval_from_repeat_cadence( $repeat_cadence ) {
356 371 switch ( $repeat_cadence ) {
@@ -387,31 +402,13 @@
387 402 return (bool) FrmSquareLiteConnectHelper::get_merchant_id();
388 403 }
389 404
390 405 /**
391 - * Replace an [email] shortcode with the current user email.
392 - *
393 - * @param string $email
394 - * @return string
395 - */
396 - private static function replace_email_shortcode( $email ) {
397 - if ( false === strpos( $email, '[email]' ) ) {
398 - return $email;
399 - }
400 -
401 - global $current_user;
402 - return str_replace(
403 - '[email]',
404 - ! empty( $current_user->user_email ) ? $current_user->user_email : '',
405 - $email
406 - );
407 - }
408 -
409 - /**
410 406 * Convert the amount from 10.00 to 1000.
411 407 *
412 408 * @param mixed $amount
413 409 * @param array $atts
410 + *
414 411 * @return string
415 412 */
416 413 public static function prepare_amount( $amount, $atts = array() ) {
417 414 $amount = parent::prepare_amount( $amount, $atts );
@@ -422,11 +419,13 @@
422 419 /**
423 420 * If this form submits with ajax, load the scripts on the first page.
424 421 *
425 422 * @param array $params
423 + *
426 424 * @return void
427 425 */
428 426 public static function maybe_load_scripts( $params ) {
427 + // phpcs:ignore Universal.Operators.StrictComparisons
429 428 if ( $params['form_id'] == $params['posted_form_id'] ) {
430 429 // This form has already been posted, so we aren't on the first page.
431 430 return;
432 431 }
@@ -431,8 +430,9 @@
431 430 return;
432 431 }
433 432
434 433 $form = FrmForm::getOne( $params['form_id'] );
434 +
435 435 if ( ! $form ) {
436 436 return;
437 437 }
438 438
@@ -441,20 +441,24 @@
441 441 'fi.form_id' => $form->id,
442 442 'type' => 'credit_card',
443 443 )
444 444 );
445 +
445 446 if ( ! $credit_card_field ) {
446 447 return;
447 448 }
448 449
449 450 $payment_actions = self::get_actions_before_submit( $form->id );
451 +
450 452 if ( ! $payment_actions ) {
451 453 return;
452 454 }
453 455
454 456 $found_gateway = false;
457 +
455 458 foreach ( $payment_actions as $action ) {
456 459 $gateways = $action->post_content['gateway'];
460 +
457 461 if ( in_array( 'square', (array) $gateways, true ) ) {
458 462 $found_gateway = true;
459 463 break;
460 464 }
@@ -470,8 +474,9 @@
470 474 /**
471 475 * Load front end JavaScript for a Stripe form.
472 476 *
473 477 * @param int $form_id
478 + *
474 479 * @return void
475 480 */
476 481 public static function load_scripts( $form_id ) {
477 482 if ( FrmAppHelper::is_admin_page( 'formidable-entries' ) ) {
@@ -488,10 +493,12 @@
488 493 }
489 494
490 495 $action_settings = self::prepare_settings_for_js( $form_id );
491 496 $found_gateway = false;
497 +
492 498 foreach ( $action_settings as $action ) {
493 499 $gateways = $action['gateways'];
500 +
494 501 if ( ! $gateways || in_array( 'square', (array) $gateways, true ) ) {
495 502 $found_gateway = true;
496 503 break;
497 504 }
@@ -519,9 +526,9 @@
519 526 FrmAppHelper::plugin_version(),
520 527 false
521 528 );
522 529
523 - $square_vars = array(
530 + $square_vars = array(
524 531 'formId' => $form_id,
525 532 'nonce' => wp_create_nonce( 'frm_square_ajax' ),
526 533 'ajax' => esc_url_raw( FrmAppHelper::get_ajax_url() ),
527 534 'settings' => $action_settings,
@@ -539,11 +546,13 @@
539 546 * @return string
540 547 */
541 548 private static function get_app_id() {
542 549 $mode = FrmSquareLiteAppHelper::active_mode();
550 +
543 551 if ( 'live' === $mode ) {
544 552 return 'sq0idp-eR4XI1xgNduJAXcBvjemTg';
545 553 }
554 +
546 555 return 'sandbox-sq0idb-MXl8ilzmhAgsHWKV9c6ycQ';
547 556 }
548 557
549 558 /**
@@ -556,8 +565,9 @@
556 565 }
557 566
558 567 /**
559 568 * @param int $form_id
569 + *
560 570 * @return array
561 571 */
562 572 private static function get_style( $form_id ) {
563 573 $settings = self::get_style_settings_for_form( $form_id );
@@ -567,9 +577,8 @@
567 577 'color' => $settings['text_color'],
568 578 'backgroundColor' => $settings['bg_color'],
569 579 'fontWeight' => $settings['field_weight'],
570 580 ),
571 - // How does input placeholder work??
572 581 'input::placeholder' => array(
573 582 'color' => $settings['text_color_disabled'],
574 583 ),
575 584 '.input-container' => array(
@@ -580,9 +589,9 @@
580 589 ),
581 590 );
582 591
583 592 if ( ! empty( $settings['font'] ) ) {
584 - $style['input']['fontFamily'] = $settings['font'];
593 + $style['input']['fontFamily'] = self::prepare_font_family_setting( $settings['font'] );
585 594 }
586 595
587 596 /**
588 597 * @since 6.22
@@ -594,13 +603,32 @@
594 603 return apply_filters( 'frm_square_style', $style, $settings, $form_id );
595 604 }
596 605
597 606 /**
607 + * Prepare the font family setting for the Stripe element.
608 + *
609 + * @since 6.26
610 + *
611 + * @param string $font
612 + *
613 + * @return string
614 + */
615 + private static function prepare_font_family_setting( $font ) {
616 + if ( ! str_contains( $font, ',' ) ) {
617 + return $font;
618 + }
619 +
620 + $fonts = explode( ',', $font );
621 + return trim( reset( $fonts ) );
622 + }
623 +
624 + /**
598 625 * Get the border radius for Stripe elements.
599 626 *
600 627 * @since 6.22
601 628 *
602 629 * @param array $settings
630 + *
603 631 * @return string
604 632 */
605 633 private static function get_border_radius( $settings ) {
606 634 if ( ! empty( $settings['field_shape_type'] ) ) {
@@ -620,8 +648,9 @@
620 648 *
621 649 * @since 6.22
622 650 *
623 651 * @param int $form_id
652 + *
624 653 * @return array
625 654 */
626 655 private static function get_style_settings_for_form( $form_id ) {
627 656 if ( ! $form_id ) {
@@ -628,8 +657,9 @@
628 657 return array();
629 658 }
630 659
631 660 $style = FrmStylesController::get_form_style( $form_id );
661 +
632 662 if ( ! $style ) {
633 663 return array();
634 664 }
635 665
@@ -634,8 +664,9 @@
634 664 }
635 665
636 666 $settings = FrmStylesHelper::get_settings_for_output( $style );
637 667 $disallowed = array( ';', ':', '!important' );
668 +
638 669 foreach ( $settings as $k => $s ) {
639 670 if ( is_string( $s ) ) {
640 671 $settings[ $k ] = str_replace( $disallowed, '', $s );
641 672 }
@@ -652,26 +683,19 @@
652 683 *
653 684 * @param array $errors
654 685 * @param stdClass $field
655 686 * @param array $values
687 + *
656 688 * @return array
657 689 */
658 690 public static function remove_cc_validation( $errors, $field, $values ) {
659 691 $has_processed = ! empty( $_POST['square-token'] ) && ! empty( $_POST['square-verification-token'] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
692 +
660 693 if ( ! $has_processed ) {
661 694 return $errors;
662 695 }
663 696
664 - $field_id = isset( $field->temp_id ) ? $field->temp_id : $field->id;
665 -
666 - if ( isset( $errors[ 'field' . $field_id . '-cc' ] ) ) {
667 - unset( $errors[ 'field' . $field_id . '-cc' ] );
668 - }
669 - if ( isset( $errors[ 'field' . $field_id ] ) ) {
670 - unset( $errors[ 'field' . $field_id ] );
671 - }
672 -
673 - return $errors;
697 + return FrmTransLiteActionsController::remove_cc_errors( $errors, $field );
674 698 }
675 699
676 700 /**
677 701 * @return void