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 | stripe/controllers/FrmTransLiteActionsController.php +126 -42 6.25 → trunk View file →
@@ -16,8 +16,9 @@
16 16 /**
17 17 * Register payment action type.
18 18 *
19 19 * @param array $actions
20 + *
20 21 * @return array
21 22 */
22 23 public static function register_actions( $actions ) {
23 24 $actions['payment'] = 'FrmTransLiteAction';
@@ -50,8 +51,9 @@
50 51 /**
51 52 * Add event types for actions so an email can trigger on a successful payment.
52 53 *
53 54 * @param array $triggers
55 + *
54 56 * @return array
55 57 */
56 58 public static function add_payment_trigger( $triggers ) {
57 59 $triggers['payment-success'] = __( 'Successful Payment', 'formidable' );
@@ -64,8 +66,9 @@
64 66 }
65 67
66 68 /**
67 69 * @param array $options
70 + *
68 71 * @return array
69 72 */
70 73 public static function add_trigger_to_action( $options ) {
71 74 $options['event'][] = 'payment-success';
@@ -80,8 +83,9 @@
80 83 /**
81 84 * @param WP_Post $action
82 85 * @param stdClass $entry
83 86 * @param mixed $form
87 + *
84 88 * @return void
85 89 */
86 90 public static function trigger_action( $action, $entry, $form ) {
87 91 self::prepare_description( $action, compact( 'entry', 'form' ) );
@@ -86,13 +90,15 @@
86 90 public static function trigger_action( $action, $entry, $form ) {
87 91 self::prepare_description( $action, compact( 'entry', 'form' ) );
88 92
89 93 $gateway = self::get_gateway_for_action( $action );
94 +
90 95 if ( ! $gateway ) {
91 96 return;
92 97 }
93 98
94 99 $class_name = FrmTransLiteAppHelper::get_setting_for_gateway( $gateway, 'class' );
100 +
95 101 if ( ! $class_name ) {
96 102 return;
97 103 }
98 104
@@ -99,9 +105,9 @@
99 105 $class_name = 'Frm' . $class_name . 'ActionsController';
100 106 $response = $class_name::trigger_gateway( $action, $entry, $form );
101 107
102 108 if ( ! $response['success'] && $response['show_errors'] ) {
103 - // the payment failed
109 + // The payment failed
104 110 self::show_failed_message( compact( 'action', 'entry', 'form', 'response' ) );
105 111 }
106 112 }
107 113
@@ -106,8 +112,9 @@
106 112 }
107 113
108 114 /**
109 115 * @param WP_Post $action
116 + *
110 117 * @return array|string
111 118 */
112 119 private static function get_gateway_for_action( $action ) {
113 120 return $action->post_content['gateway'] ?? 'stripe';
@@ -116,8 +123,9 @@
116 123 /**
117 124 * @since 6.10
118 125 *
119 126 * @param array $args
127 + *
120 128 * @return void
121 129 */
122 130 private static function show_failed_message( $args ) {
123 131 global $frm_vars;
@@ -135,8 +143,9 @@
135 143 /**
136 144 * @since 6.10
137 145 *
138 146 * @param stdClass $form
147 + *
139 148 * @return stdClass
140 149 */
141 150 public static function include_form_with_success( $form ) {
142 151 $form->options['show_form'] = 1;
@@ -148,21 +157,21 @@
148 157 */
149 158 public static function replace_success_message() {
150 159 global $frm_vars;
151 160 $message = $frm_vars['frm_trans']['error'] ?? '';
152 - if ( empty( $message ) ) {
161 +
162 + if ( ! $message ) {
153 163 $message = __( 'There was an error processing your payment.', 'formidable' );
154 164 }
155 165
156 - $message = '<div class="frm_error_style">' . $message . '</div>';
166 + return '<div class="frm_error_style">' . $message . '</div>';
167 + }
157 168
158 - return $message;
159 - }
160 -
161 169 /**
162 170 * @param WP_Post $action
163 171 * @param stdClass $entry
164 172 * @param mixed $form
173 + *
165 174 * @return array
166 175 */
167 176 public static function trigger_gateway( $action, $entry, $form ) {
168 177 // This function must be overridden in a subclass.
@@ -183,8 +192,9 @@
183 192 /**
184 193 * @since 6.5, introduced in v1.12 of the Payments submodule.
185 194 *
186 195 * @param object $sub
196 + *
187 197 * @return void
188 198 */
189 199 public static function trigger_subscription_status_change( $sub ) {
190 200 $frm_payment = new FrmTransLitePayment();
@@ -201,8 +211,9 @@
201 211 }
202 212
203 213 /**
204 214 * @param array $atts
215 + *
205 216 * @return void
206 217 */
207 218 public static function trigger_payment_status_change( $atts ) {
208 219 $entry_id = isset( $atts['entry'] ) ? $atts['entry']->id : $atts['payment']->item_id;
@@ -241,8 +252,9 @@
241 252 * Maybe trigger payment-success or payment-failed event after payment so actions (like emails) can run.
242 253 *
243 254 * @param object $payment
244 255 * @param array $atts
256 + *
245 257 * @return void
246 258 */
247 259 public static function trigger_actions_after_payment( $payment, $atts = array() ) {
248 260 if ( 'pending' === $payment->status ) {
@@ -249,20 +261,16 @@
249 261 // 3D Secure has a delayed payment status, so avoid sending a payment failed email for a pending payment.
250 262 return;
251 263 }
252 264
253 - $entry = FrmEntry::getOne( $payment->item_id );
265 + $entry = FrmEntry::getOne( $payment->item_id );
266 + $trigger_event = isset( $atts['trigger'] ) ? 'payment-' . $atts['trigger'] : 'payment-' . $payment->status;
267 + $allowed_triggers = array_keys( self::add_payment_trigger( array() ) );
254 268
255 - if ( isset( $atts['trigger'] ) ) {
256 - $trigger_event = 'payment-' . $atts['trigger'];
257 - } else {
258 - $trigger_event = 'payment-' . $payment->status;
259 - }
260 -
261 - $allowed_triggers = array_keys( self::add_payment_trigger( array() ) );
262 269 if ( ! in_array( $trigger_event, $allowed_triggers, true ) ) {
263 270 $trigger_event = $payment->status === 'complete' ? 'payment-success' : 'payment-failed';
264 271 }
272 +
265 273 FrmFormActionsController::trigger_actions( $trigger_event, $entry->form_id, $entry->id );
266 274 }
267 275
268 276 /**
@@ -269,17 +277,21 @@
269 277 * Filter fields in description.
270 278 *
271 279 * @param WP_Post $action
272 280 * @param array $atts
281 + *
273 282 * @return void
274 283 */
275 284 public static function prepare_description( &$action, $atts ) {
276 285 $description = $action->post_content['description'];
277 - if ( ! empty( $description ) ) {
278 - $atts['value'] = $description;
279 - $description = FrmTransLiteAppHelper::process_shortcodes( $atts );
280 - $action->post_content['description'] = $description;
286 +
287 + if ( ! $description ) {
288 + return;
281 289 }
290 +
291 + $atts['value'] = $description;
292 + $description = FrmTransLiteAppHelper::process_shortcodes( $atts );
293 + $action->post_content['description'] = $description;
282 294 }
283 295
284 296 /**
285 297 * Convert the amount into 10.00.
@@ -285,8 +297,9 @@
285 297 * Convert the amount into 10.00.
286 298 *
287 299 * @param mixed $amount
288 300 * @param array $atts
301 + *
289 302 * @return string
290 303 */
291 304 public static function prepare_amount( $amount, $atts = array() ) {
292 305 if ( isset( $atts['form'] ) ) {
@@ -293,16 +306,16 @@
293 306 $atts['value'] = $amount;
294 307 $amount = FrmTransLiteAppHelper::process_shortcodes( $atts );
295 308 }
296 309
297 - if ( is_string( $amount ) && strlen( $amount ) >= 2 && $amount[0] === '[' && substr( $amount, -1 ) === ']' ) {
310 + if ( is_string( $amount ) && strlen( $amount ) >= 2 && $amount[0] === '[' && str_ends_with( $amount, ']' ) ) {
298 311 // Make sure we don't use a field id as the amount.
299 312 $amount = 0;
300 313 }
301 314
302 315 $currency = self::get_currency_for_action( $atts );
316 + $total = 0;
303 317
304 - $total = 0;
305 318 foreach ( (array) $amount as $a ) {
306 319 $this_amount = self::get_amount_from_string( $a );
307 320 self::maybe_use_decimal( $this_amount, $currency );
308 321 self::normalize_number( $this_amount, $currency );
@@ -317,12 +330,14 @@
317 330 /**
318 331 * Get currency to use when preparing amount.
319 332 *
320 333 * @param array $atts
334 + *
321 335 * @return array
322 336 */
323 337 public static function get_currency_for_action( $atts ) {
324 338 $currency = 'usd';
339 +
325 340 if ( isset( $atts['form'] ) ) {
326 341 $currency = $atts['action']->post_content['currency'];
327 342 } elseif ( isset( $atts['currency'] ) ) {
328 343 $currency = $atts['currency'];
@@ -339,15 +354,15 @@
339 354 private static function get_amount_from_string( $amount ) {
340 355 $amount = html_entity_decode( $amount );
341 356 $amount = trim( $amount );
342 357 preg_match_all( '/[0-9,.]*\.?\,?[0-9]+/', $amount, $matches );
343 - $amount = $matches ? end( $matches[0] ) : 0;
344 - return $amount;
358 + return $matches ? end( $matches[0] ) : 0;
345 359 }
346 360
347 361 /**
348 362 * @param string $amount
349 363 * @param array $currency
364 + *
350 365 * @return void
351 366 */
352 367 private static function maybe_use_decimal( &$amount, $currency ) {
353 368 if ( $currency['thousand_separator'] !== '.' ) {
@@ -354,8 +369,9 @@
354 369 return;
355 370 }
356 371
357 372 $amount_parts = explode( '.', $amount );
373 +
358 374 if ( 2 !== count( $amount_parts ) ) {
359 375 return;
360 376 }
361 377
@@ -369,8 +385,9 @@
369 385
370 386 /**
371 387 * @param string $amount
372 388 * @param array $currency
389 + *
373 390 * @return void
374 391 */
375 392 private static function normalize_number( &$amount, $currency ) {
376 393 $amount = str_replace( $currency['thousand_separator'], '', $amount );
@@ -381,18 +398,21 @@
381 398 /**
382 399 * These settings are included in frm_stripe_vars.settings global JavaScript object on Stripe forms.
383 400 *
384 401 * @param int $form_id
402 + *
385 403 * @return array
386 404 */
387 405 public static function prepare_settings_for_js( $form_id ) {
388 406 $payment_actions = self::get_actions_for_form( $form_id );
389 407 $action_settings = array();
408 +
390 409 foreach ( $payment_actions as $payment_action ) {
391 410 $settings_for_action = array(
392 411 'id' => $payment_action->ID,
393 412 'first_name' => $payment_action->post_content['billing_first_name'],
394 413 'last_name' => $payment_action->post_content['billing_last_name'],
414 + 'address' => $payment_action->post_content['billing_address'] ?? '',
395 415 'gateways' => $payment_action->post_content['gateway'],
396 416 'fields' => self::get_fields_for_price( $payment_action ),
397 417 'one' => $payment_action->post_content['type'],
398 418 'email' => $payment_action->post_content['email'],
@@ -415,8 +435,9 @@
415 435 *
416 436 * @since 6.5, introduced in v2.0 of the Payments submodule.
417 437 *
418 438 * @param WP_Post $action
439 + *
419 440 * @return array|int
420 441 */
421 442 private static function get_fields_for_price( $action ) {
422 443 $amount = $action->post_content['amount'];
@@ -427,8 +448,9 @@
427 448 /**
428 449 * Get all published payment actions.
429 450 *
430 451 * @param int|string $form_id
452 + *
431 453 * @return array
432 454 */
433 455 public static function get_actions_for_form( $form_id ) {
434 456 $action_status = array(
@@ -434,12 +456,10 @@
434 456 $action_status = array(
435 457 'post_status' => 'publish',
436 458 );
437 459 $payment_actions = FrmFormAction::get_action_for_form( $form_id, 'payment', $action_status );
438 - if ( ! $payment_actions ) {
439 - $payment_actions = array();
440 - }
441 - return $payment_actions;
460 +
461 + return $payment_actions ? $payment_actions : array();
442 462 }
443 463
444 464 /**
445 465 * Make sure a gateway field is hidden on the front end.
@@ -445,8 +465,9 @@
445 465 * Make sure a gateway field is hidden on the front end.
446 466 *
447 467 * @param array $values
448 468 * @param stdClass $field
469 + *
449 470 * @return array
450 471 */
451 472 public static function hide_gateway_field_on_front_end( $values, $field ) {
452 473 if ( $field->type !== 'gateway' ) {
@@ -452,8 +473,14 @@
452 473 if ( $field->type !== 'gateway' ) {
453 474 return $values;
454 475 }
455 476
477 + if ( FrmAppHelper::is_form_builder_page() ) {
478 + // The hooks this uses can get called in the form builder and settings pages.
479 + // But we do not need the script in this case.
480 + return $values;
481 + }
482 +
456 483 // This is also called from the frm_enqueue_form_scripts hook.
457 484 // With this here, the value of frm_stripe_vars.settings[0].fields is -1
458 485 // This is because the amount value is processed and a shortcode is not found in '000'.
459 486 FrmStrpLiteActionsController::load_scripts( (int) $field->form_id );
@@ -469,14 +496,17 @@
469 496 * @since 6.5.1
470 497 *
471 498 * @param array $values
472 499 * @param stdClass $field
500 + *
473 501 * @return array
474 502 */
475 503 public static function fill_entry_from_previous( $values, $field ) {
476 504 global $frm_vars;
477 505 $previous_entry = $frm_vars['frm_trans']['pay_entry'] ?? false;
478 - if ( empty( $previous_entry ) || $previous_entry->form_id != $field->form_id ) {
506 +
507 + // phpcs:ignore Universal.Operators.StrictComparisons
508 + if ( ! $previous_entry || $previous_entry->form_id != $field->form_id ) {
479 509 return $values;
480 510 }
481 511
482 512 if ( is_array( $previous_entry->metas ) && isset( $previous_entry->metas[ $field->id ] ) ) {
@@ -483,10 +513,9 @@
483 513 $values['value'] = $previous_entry->metas[ $field->id ];
484 514 }
485 515
486 516 $frm_vars['trans_filled'] = true;
487 -
488 - $previous_entry_id = $previous_entry->id;
517 + $previous_entry_id = $previous_entry->id;
489 518 self::destroy_entry_later( $previous_entry_id );
490 519
491 520 return $values;
492 521 }
@@ -498,8 +527,9 @@
498 527 *
499 528 * @since 6.5.1
500 529 *
501 530 * @param int|string $entry_id
531 + *
502 532 * @return void
503 533 */
504 534 private static function destroy_entry_later( $entry_id ) {
505 535 if ( in_array( (int) $entry_id, self::$entry_ids_to_destroy_later, true ) ) {
@@ -512,8 +542,9 @@
512 542 * Destroy an entry and remove this action so it only tries to destroy the entry once.
513 543 *
514 544 * @param int|string $entry_id
515 545 * @param Closure $destroy_callback
546 + *
516 547 * @return void
517 548 */
518 549 function () use ( $entry_id, &$destroy_callback ) {
519 550 FrmEntry::destroy( $entry_id );
@@ -531,20 +562,17 @@
531 562 * @since 6.22
532 563 *
533 564 * @param array $settings
534 565 * @param array $action
566 + *
535 567 * @return array
536 568 */
537 569 public static function before_save_settings( $settings, $action ) {
538 - $settings['gateway'] = ! empty( $settings['gateway'] ) ? (array) $settings['gateway'] : array( 'stripe' );
570 + $settings['gateway'] = ! empty( $settings['gateway'] ) ? (array) $settings['gateway'] : array( 'stripe' );
539 571
540 - if ( in_array( 'square', $settings['gateway'] ) ) {
541 - $currency = FrmSquareLiteConnectHelper::get_merchant_currency();
542 - if ( false !== $currency ) {
543 - $settings['currency'] = strtolower( $currency );
544 - } else {
545 - $settings['currency'] = 'usd';
546 - }
572 + if ( in_array( 'square', $settings['gateway'], true ) ) {
573 + $currency = FrmSquareLiteConnectHelper::get_merchant_currency();
574 + $settings['currency'] = false !== $currency ? strtolower( $currency ) : 'usd';
547 575 } else {
548 576 $settings['currency'] = strtolower( $settings['currency'] );
549 577 }
550 578
@@ -557,16 +585,24 @@
557 585 'type' => 'credit_card',
558 586 'form_id' => $form_id,
559 587 )
560 588 );
589 +
561 590 if ( ! $credit_card_field_id ) {
562 591 $credit_card_field_id = self::add_a_credit_card_field( $form_id );
563 592 }
593 +
564 594 if ( $credit_card_field_id ) {
565 595 $settings['credit_card'] = $credit_card_field_id;
566 596 }
567 597 }
568 598
599 + if ( ! in_array( 'stripe', $settings['gateway'], true ) ) {
600 + // We only need a gateway field for Stripe add-on compatibility,
601 + // so unless Stripe is selected, we can return early.
602 + return $settings;
603 + }
604 +
569 605 $gateway_field_id = FrmDb::get_var(
570 606 'frm_fields',
571 607 array(
572 608 'type' => 'gateway',
@@ -572,12 +608,13 @@
572 608 'type' => 'gateway',
573 609 'form_id' => $form_id,
574 610 )
575 611 );
612 +
576 613 if ( ! $gateway_field_id ) {
577 614 self::add_a_gateway_field( $form_id );
578 615 }
579 -
616 +
580 617 return $settings;
581 618 }
582 619
583 620 /**
@@ -583,8 +620,9 @@
583 620 /**
584 621 * A credit card field is added automatically if missing before a Stripe action is updated.
585 622 *
586 623 * @param int $form_id
624 + *
587 625 * @return false|int
588 626 */
589 627 protected static function add_a_credit_card_field( $form_id ) {
590 628 return self::add_a_field( $form_id, 'credit_card', __( 'Payment', 'formidable' ) );
@@ -594,8 +632,9 @@
594 632 * A gateway field is added automatically for compatibility with the Stripe add on.
595 633 * The gateway field is not important for the Stripe Lite implementation.
596 634 *
597 635 * @param int $form_id
636 + *
598 637 * @return false|int
599 638 */
600 639 protected static function add_a_gateway_field( $form_id ) {
601 640 return self::add_a_field( $form_id, 'gateway', __( 'Payment Method', 'formidable' ) );
@@ -604,13 +643,58 @@
604 643 /**
605 644 * @param int $form_id
606 645 * @param string $field_type
607 646 * @param string $field_name
647 + *
608 648 * @return false|int
609 649 */
610 650 protected static function add_a_field( $form_id, $field_type, $field_name ) {
611 - $new_values = FrmFieldsHelper::setup_new_vars( $field_type, $form_id );
612 - $new_values['name'] = $field_name;
613 - $field_id = FrmField::create( $new_values );
614 - return $field_id;
651 + $new_values = FrmFieldsHelper::setup_new_vars( $field_type, $form_id );
652 + $new_values['name'] = $field_name;
653 + $new_values['field_order'] = self::get_field_order_before_submit( $form_id, $new_values['field_order'] );
654 + return FrmField::create( $new_values );
655 + }
656 +
657 + /**
658 + * When auto-injecting a field, ensure it is placed before the submit button.
659 + *
660 + * @since 6.29
661 + *
662 + * @param int $form_id
663 + * @param int $field_order
664 + *
665 + * @return int
666 + */
667 + private static function get_field_order_before_submit( $form_id, $field_order ) {
668 + $submit_field = FrmSubmitHelper::get_submit_field( $form_id );
669 +
670 + if ( ! $submit_field || $field_order < (int) $submit_field->field_order ) {
671 + return $field_order;
672 + }
673 +
674 + $submit_order = (int) $submit_field->field_order;
675 + FrmField::update( $submit_field->id, array( 'field_order' => $submit_order + 1 ) );
676 + return $submit_order;
677 + }
678 +
679 + /**
680 + * Remove credit card validation errors.
681 + *
682 + * @param array $errors
683 + * @param stdClass $field
684 + *
685 + * @return array
686 + */
687 + public static function remove_cc_errors( $errors, $field ) {
688 + $field_id = $field->temp_id ?? $field->id;
689 +
690 + if ( isset( $errors[ 'field' . $field_id . '-cc' ] ) ) {
691 + unset( $errors[ 'field' . $field_id . '-cc' ] );
692 + }
693 +
694 + if ( isset( $errors[ 'field' . $field_id ] ) ) {
695 + unset( $errors[ 'field' . $field_id ] );
696 + }
697 +
698 + return $errors;
615 699 }
616 700 }