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