PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.16.1
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.16.1
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/FrmStrpLiteActionsController.php +88 -168 6.33 → 6.16.1 View file →
@@ -10,31 +10,8 @@
10 10 */
11 11 private static $customer;
12 12
13 13 /**
14 - * @since 6.22
15 - *
16 - * @param string $callback
17 - * @param array|false|object $field
18 - *
19 - * @return string
20 - */
21 - public static function maybe_show_card( $callback, $field = false ) {
22 - if ( false === $field ) {
23 - // Pro isn't up to date.
24 - // Fallback to Stripe Lite if we do not know the form.
25 - // This way we do not display the default credit card field
26 - // when Pro is not up to version 6.21.
27 - return self::class . '::show_card';
28 - }
29 -
30 - $form_id = is_object( $field ) ? $field->form_id : $field['form_id'];
31 - $actions = self::get_actions_before_submit( $form_id );
32 -
33 - return $actions ? self::class . '::show_card' : $callback;
34 - }
35 -
36 - /**
37 14 * Override the credit card field HTML if there is a Stripe action.
38 15 *
39 16 * @since 6.5, introduced in v2.0 of the Stripe add on.
40 17 *
@@ -40,9 +17,8 @@
40 17 *
41 18 * @param array $field
42 19 * @param string $field_name
43 20 * @param array $atts
44 - *
45 21 * @return void
46 22 */
47 23 public static function show_card( $field, $field_name, $atts ) {
48 24 $actions = self::get_actions_before_submit( $field['form_id'] );
@@ -62,23 +38,19 @@
62 38 *
63 39 * @since 6.5, introduced in v2.0 of the Stripe add on.
64 40 *
65 41 * @param int|string $form_id
66 - *
67 42 * @return array
68 43 */
69 44 public static function get_actions_before_submit( $form_id ) {
70 45 $payment_actions = self::get_actions_for_form( $form_id );
71 -
72 46 foreach ( $payment_actions as $k => $payment_action ) {
73 47 $gateway = $payment_action->post_content['gateway'];
74 48 $is_stripe = $gateway === 'stripe' || ( is_array( $gateway ) && in_array( 'stripe', $gateway, true ) );
75 -
76 49 if ( ! $is_stripe || empty( $payment_action->post_content['amount'] ) ) {
77 50 unset( $payment_actions[ $k ] );
78 51 }
79 52 }
80 -
81 53 return $payment_actions;
82 54 }
83 55
84 56 /**
@@ -88,9 +60,8 @@
88 60 *
89 61 * @since 6.5, introduced in v3.0 of the Stripe add on.
90 62 *
91 63 * @param int|string $form_id
92 - *
93 64 * @return false|WP_Post
94 65 */
95 66 public static function get_stripe_link_action( $form_id ) {
96 67 $actions = self::get_actions_before_submit( $form_id );
@@ -103,10 +74,9 @@
103 74 * It is also called when Stripe link is active but the Stripe payment is triggered instead with JavaScript.
104 75 *
105 76 * @param WP_Post $action
106 77 * @param stdClass $entry
107 - * @param mixed $form
108 - *
78 + * @param stdClass $form
109 79 * @return array
110 80 */
111 81 public static function trigger_gateway( $action, $entry, $form ) {
112 82 $response = array(
@@ -114,23 +84,21 @@
114 84 'run_triggers' => false,
115 85 'show_errors' => true,
116 86 );
117 87 $atts = compact( 'action', 'entry', 'form' );
118 - $amount = self::prepare_amount( $action->post_content['amount'], $atts );
119 88
120 - // phpcs:ignore Universal.Operators.StrictComparisons
121 - if ( ! $amount || $amount == 000 ) {
89 + $amount = self::prepare_amount( $action->post_content['amount'], $atts );
90 + if ( empty( $amount ) || $amount == 000 ) {
122 91 $response['error'] = __( 'Please specify an amount for the payment', 'formidable' );
123 92 return $response;
124 93 }
125 94
126 95 if ( ! self::stripe_is_configured() ) {
127 - $response['error'] = __( 'Stripe still needs to be configured.', 'formidable' );
96 + $response['error'] = __( 'There was a problem communicating with Stripe. Please try again.', 'formidable' );
128 97 return $response;
129 98 }
130 99
131 100 $customer = self::set_customer_with_token( $atts );
132 -
133 101 if ( ! is_object( $customer ) ) {
134 102 $response['error'] = $customer;
135 103 return $response;
136 104 }
@@ -136,13 +104,9 @@
136 104 }
137 105
138 106 $one_time_payment_args = compact( 'customer', 'form', 'entry', 'action', 'amount' );
139 107
140 - if ( ! FrmStrpLiteLinkController::create_pending_stripe_link_payment( $one_time_payment_args ) ) {
141 - $response['error'] = __( 'There was something wrong with the payment data.', 'formidable' );
142 - return $response;
143 - }
144 -
108 + FrmStrpLiteLinkController::create_pending_stripe_link_payment( $one_time_payment_args );
145 109 $response['show_errors'] = false;
146 110 return $response;
147 111 }
148 112
@@ -158,9 +122,8 @@
158 122 /**
159 123 * Set a customer object to $_POST['customer'] to use later.
160 124 *
161 125 * @param array $atts
162 - *
163 126 * @return object|string
164 127 */
165 128 private static function set_customer_with_token( $atts ) {
166 129 if ( isset( self::$customer ) ) {
@@ -189,13 +152,12 @@
189 152 /**
190 153 * Replace an [email] shortcode with the current user email.
191 154 *
192 155 * @param string $email
193 - *
194 156 * @return string
195 157 */
196 158 private static function replace_email_shortcode( $email ) {
197 - if ( ! str_contains( $email, '[email]' ) ) {
159 + if ( false === strpos( $email, '[email]' ) ) {
198 160 return $email;
199 161 }
200 162
201 163 global $current_user;
@@ -212,9 +174,8 @@
212 174 * @since 6.5, introduced in v2.02 of the Stripe add on.
213 175 *
214 176 * @param array $atts
215 177 * @param array $payment_info
216 - *
217 178 * @return void
218 179 */
219 180 private static function add_customer_name( $atts, &$payment_info ) {
220 181 if ( empty( $atts['action']->post_content['billing_first_name'] ) ) {
@@ -221,9 +182,8 @@
221 182 return;
222 183 }
223 184
224 185 $name = '[' . $atts['action']->post_content['billing_first_name'] . ' show="first"]';
225 -
226 186 if ( ! empty( $atts['action']->post_content['billing_last_name'] ) ) {
227 187 $name .= ' [' . $atts['action']->post_content['billing_last_name'] . ' show="last"]';
228 188 }
229 189
@@ -235,20 +195,17 @@
235 195 *
236 196 * @since 6.5, introduced in v1.16 of the Stripe add on.
237 197 *
238 198 * @param array $atts Includes 'customer', 'entry', 'action', 'amount'.
239 - *
240 199 * @return int The timestamp when the trial should end. 0 for no trial
241 200 */
242 201 public static function get_trial_end_time( $atts ) {
243 202 $settings = $atts['action']->post_content;
244 -
245 203 if ( empty( $settings['trial_interval_count'] ) ) {
246 204 return 0;
247 205 }
248 206
249 207 $trial = $settings['trial_interval_count'];
250 -
251 208 if ( ! is_numeric( $trial ) ) {
252 209 $trial = FrmTransLiteAppHelper::process_shortcodes(
253 210 array(
254 211 'value' => $trial,
@@ -268,9 +225,8 @@
268 225 * Convert the amount from 10.00 to 1000.
269 226 *
270 227 * @param mixed $amount
271 228 * @param array $atts
272 - *
273 229 * @return string
274 230 */
275 231 public static function prepare_amount( $amount, $atts = array() ) {
276 232 $amount = parent::prepare_amount( $amount, $atts );
@@ -281,21 +237,14 @@
281 237 /**
282 238 * Add defaults for additional Stripe action options.
283 239 *
284 240 * @param array $defaults
285 - *
286 241 * @return array
287 242 */
288 243 public static function add_action_defaults( $defaults ) {
289 - // Stripe action options.
290 244 $defaults['plan_id'] = '';
291 245 $defaults['capture'] = '';
292 246 $defaults['stripe_link'] = '';
293 -
294 - // PayPal action options.
295 - $defaults['product_name'] = '';
296 - $defaults['pay_later'] = '';
297 -
298 247 return $defaults;
299 248 }
300 249
301 250 /**
@@ -301,9 +250,8 @@
301 250 /**
302 251 * Print additional options for Stripe action settings.
303 252 *
304 253 * @param array $atts
305 - *
306 254 * @return void
307 255 */
308 256 public static function add_action_options( $atts ) {
309 257 $form_action = $atts['form_action'];
@@ -315,35 +263,86 @@
315 263 * Filter Stripe action on save.
316 264 *
317 265 * @param array $settings
318 266 * @param array $action
319 - *
320 267 * @return array
321 268 */
322 269 public static function before_save_settings( $settings, $action ) {
323 270 $settings['currency'] = strtolower( $settings['currency'] );
324 271
325 - // Gateway is a radio button but it should always be an array in the database for
326 - // compatibility with the payments submodule where it is a checkbox (when Authorize.Net is active).
327 - $settings['gateway'] = ! empty( $settings['gateway'] ) ? (array) $settings['gateway'] : array( 'stripe' );
272 + // In Lite Stripe link is always used.
273 + $settings['stripe_link'] = 1;
274 + $settings = self::create_plans( $settings );
275 + $form_id = absint( $action['menu_order'] );
328 276
329 - $is_stripe = in_array( 'stripe', $settings['gateway'], true );
277 + if ( empty( $settings['credit_card'] ) ) {
278 + $credit_card_field_id = FrmDb::get_var(
279 + 'frm_fields',
280 + array(
281 + 'type' => 'credit_card',
282 + 'form_id' => $form_id,
283 + )
284 + );
285 + if ( ! $credit_card_field_id ) {
286 + $credit_card_field_id = self::add_a_credit_card_field( $form_id );
287 + }
288 + if ( $credit_card_field_id ) {
289 + $settings['credit_card'] = $credit_card_field_id;
290 + }
291 + }
330 292
331 - if ( ! $is_stripe ) {
332 - return $settings;
293 + $gateway_field_id = FrmDb::get_var(
294 + 'frm_fields',
295 + array(
296 + 'type' => 'gateway',
297 + 'form_id' => $form_id,
298 + )
299 + );
300 + if ( ! $gateway_field_id ) {
301 + self::add_a_gateway_field( $form_id );
333 302 }
334 303
335 - // In Lite Stripe link is always used.
336 - $settings['stripe_link'] = 1;
304 + return $settings;
305 + }
337 306
338 - return self::create_plans( $settings );
307 + /**
308 + * @param int $form_id
309 + * @param string $field_type
310 + * @param string $field_name
311 + * @return false|int
312 + */
313 + private static function add_a_field( $form_id, $field_type, $field_name ) {
314 + $new_values = FrmFieldsHelper::setup_new_vars( $field_type, $form_id );
315 + $new_values['name'] = $field_name;
316 + $field_id = FrmField::create( $new_values );
317 + return $field_id;
339 318 }
340 319
341 320 /**
321 + * A credit card field is added automatically if missing before a Stripe action is updated.
322 + *
323 + * @param int $form_id
324 + * @return false|int
325 + */
326 + private static function add_a_credit_card_field( $form_id ) {
327 + return self::add_a_field( $form_id, 'credit_card', __( 'Payment', 'formidable' ) );
328 + }
329 +
330 + /**
331 + * A gateway field is added automatically for compatibility with the Stripe add on.
332 + * The gateway field is not important for the Stripe Lite implementation.
333 + *
334 + * @param int $form_id
335 + * @return false|int
336 + */
337 + private static function add_a_gateway_field( $form_id ) {
338 + return self::add_a_field( $form_id, 'gateway', __( 'Payment Method', 'formidable' ) );
339 + }
340 +
341 + /**
342 342 * Create any required Stripe plans, used for subscriptions.
343 343 *
344 344 * @param array $settings
345 - *
346 345 * @return array
347 346 */
348 347 public static function create_plans( $settings ) {
349 348 if ( $settings['type'] !== 'recurring' || strpos( $settings['amount'], ']' ) ) {
@@ -352,10 +351,8 @@
352 351 return $settings;
353 352 }
354 353
355 354 $plan_opts = FrmStrpLiteSubscriptionHelper::prepare_plan_options( $settings );
356 -
357 - // phpcs:ignore Universal.Operators.StrictComparisons
358 355 if ( $plan_opts['id'] != $settings['plan_id'] ) {
359 356 $settings['plan_id'] = FrmStrpLiteSubscriptionHelper::maybe_create_plan( $plan_opts );
360 357 }
361 358
@@ -365,14 +362,14 @@
365 362 /**
366 363 * Include settings in the plan description in order to make sure the correct plan is used.
367 364 *
368 365 * @param array $settings
369 - *
370 366 * @return string
371 367 */
372 368 public static function create_plan_id( $settings ) {
373 369 $amount = self::prepare_amount( $settings['amount'], $settings );
374 - return sanitize_title_with_dashes( $settings['description'] ) . '_' . $amount . '_' . $settings['interval_count'] . $settings['interval'] . '_' . $settings['currency'];
370 + $id = sanitize_title_with_dashes( $settings['description'] ) . '_' . $amount . '_' . $settings['interval_count'] . $settings['interval'] . '_' . $settings['currency'];
371 + return $id;
375 372 }
376 373
377 374 /**
378 375 * If this form submits with ajax, load the scripts on the first page.
@@ -377,13 +374,11 @@
377 374 /**
378 375 * If this form submits with ajax, load the scripts on the first page.
379 376 *
380 377 * @param array $params
381 - *
382 378 * @return void
383 379 */
384 380 public static function maybe_load_scripts( $params ) {
385 - // phpcs:ignore Universal.Operators.StrictComparisons
386 381 if ( $params['form_id'] == $params['posted_form_id'] ) {
387 382 // This form has already been posted, so we aren't on the first page.
388 383 return;
389 384 }
@@ -388,9 +383,8 @@
388 383 return;
389 384 }
390 385
391 386 $form = FrmForm::getOne( $params['form_id'] );
392 -
393 387 if ( ! $form ) {
394 388 return;
395 389 }
396 390
@@ -399,9 +393,8 @@
399 393 'fi.form_id' => $form->id,
400 394 'type' => 'credit_card',
401 395 )
402 396 );
403 -
404 397 if ( ! $credit_card_field ) {
405 398 return;
406 399 }
407 400
@@ -411,9 +404,8 @@
411 404 /**
412 405 * Load front end JavaScript for a Stripe form.
413 406 *
414 407 * @param int $form_id
415 - *
416 408 * @return void
417 409 */
418 410 public static function load_scripts( $form_id ) {
419 411 if ( FrmAppHelper::is_admin_page( 'formidable-entries' ) ) {
@@ -424,9 +416,8 @@
424 416 return;
425 417 }
426 418
427 419 $stripe_connect_is_setup = FrmStrpLiteConnectHelper::stripe_connect_is_setup();
428 -
429 420 if ( ! $stripe_connect_is_setup ) {
430 421 return;
431 422 }
432 423
@@ -456,9 +447,8 @@
456 447 if ( ! $suffix && ! is_readable( FrmStrpLiteAppHelper::plugin_path() . 'js/frmstrp.js' ) ) {
457 448 // The unminified file is not included in releases so force the minified script.
458 449 $suffix = '.min';
459 450 }
460 -
461 451 $script_url = FrmStrpLiteAppHelper::plugin_url() . 'js/frmstrp' . $suffix . '.js';
462 452 }
463 453
464 454 if ( class_exists( 'FrmProStrpLiteController' ) && ( ! $suffix || ! FrmProAppController::has_combo_js_file() ) ) {
@@ -464,28 +454,8 @@
464 454 if ( class_exists( 'FrmProStrpLiteController' ) && ( ! $suffix || ! FrmProAppController::has_combo_js_file() ) ) {
465 455 $dependencies[] = 'formidablepro';
466 456 }
467 457
468 - $action_settings = self::prepare_settings_for_js( $form_id );
469 - $found_gateway = false;
470 -
471 - foreach ( $action_settings as &$action ) {
472 - $gateways = $action['gateways'];
473 -
474 - if ( ! $gateways || in_array( 'stripe', (array) $gateways, true ) ) {
475 - $found_gateway = true;
476 - }
477 -
478 - if ( ! empty( $action['layout'] ) && ! in_array( $action['layout'], array( 'accordion', 'tabs' ), true ) ) {
479 - $action['layout'] = '';
480 - }
481 - }
482 - unset( $action );
483 -
484 - if ( ! $found_gateway ) {
485 - return;
486 - }
487 -
488 458 wp_enqueue_script(
489 459 'formidable-stripe',
490 460 $script_url,
491 461 $dependencies,
@@ -492,10 +462,11 @@
492 462 FrmAppHelper::plugin_version(),
493 463 false
494 464 );
495 465
496 - $style_settings = self::get_style_settings_for_form( $form_id );
497 - $stripe_vars = array(
466 + $action_settings = self::prepare_settings_for_js( $form_id );
467 + $style_settings = self::get_style_settings_for_form( $form_id );
468 + $stripe_vars = array(
498 469 'publishable_key' => $publishable,
499 470 'form_id' => $form_id,
500 471 'nonce' => wp_create_nonce( 'frm_strp_ajax' ),
501 472 'ajax' => esc_url_raw( FrmAppHelper::get_ajax_url() ),
@@ -514,9 +485,8 @@
514 485 *
515 486 * @since 6.5, introduced in v3.0 of the Stripe add on.
516 487 *
517 488 * @param mixed $form_id
518 - *
519 489 * @return array
520 490 */
521 491 private static function get_style_settings_for_form( $form_id ) {
522 492 if ( ! $form_id ) {
@@ -523,9 +493,8 @@
523 493 return array();
524 494 }
525 495
526 496 $style = FrmStylesController::get_form_style( $form_id );
527 -
528 497 if ( ! $style ) {
529 498 return array();
530 499 }
531 500
@@ -530,9 +499,8 @@
530 499 }
531 500
532 501 $settings = FrmStylesHelper::get_settings_for_output( $style );
533 502 $disallowed = array( ';', ':', '!important' );
534 -
535 503 foreach ( $settings as $k => $s ) {
536 504 if ( is_string( $s ) ) {
537 505 $settings[ $k ] = str_replace( $disallowed, '', $s );
538 506 }
@@ -549,24 +517,20 @@
549 517 *
550 518 * @since 6.5, introduced in v3.0 of the Stripe add on.
551 519 *
552 520 * @param array $settings
553 - *
554 521 * @return array
555 522 */
556 523 private static function get_appearance_rules( $settings ) {
557 - $rules = array(
524 + return array(
558 525 '.Input' => array(
559 - 'fontFamily' => $settings['font'],
560 526 'color' => $settings['text_color'],
561 527 'backgroundColor' => $settings['bg_color'],
562 528 'padding' => $settings['field_pad'],
563 529 'lineHeight' => 1.3,
564 530 'borderColor' => $settings['border_color'],
565 - 'borderWidth' => self::get_border_width( $settings ),
531 + 'borderWidth' => $settings['field_border_width'],
566 532 'borderStyle' => $settings['field_border_style'],
567 - 'borderRadius' => self::get_border_radius( $settings ),
568 - 'fontWeight' => $settings['field_weight'],
569 533 ),
570 534 '.Input::placeholder' => array(
571 535 'color' => $settings['text_color_disabled'],
572 536 ),
@@ -573,73 +537,22 @@
573 537 '.Input:focus' => array(
574 538 'backgroundColor' => $settings['bg_color_active'],
575 539 ),
576 540 '.Label' => array(
577 - 'color' => $settings['label_color'],
578 - 'fontSize' => $settings['font_size'],
579 - 'fontWeight' => $settings['weight'],
580 - 'padding' => $settings['label_padding'],
581 - 'marginBottom' => 0,
541 + 'color' => $settings['label_color'],
542 + 'fontSize' => $settings['font_size'],
543 + 'fontWeight' => $settings['weight'],
582 544 ),
583 545 '.Error' => array(
584 546 'color' => $settings['border_color_error'],
585 547 ),
586 548 );
587 -
588 - /*
589 - * Filters the appearance rules for Stripe elements.
590 - *
591 - * @since 6.21
592 - *
593 - * @param array $rules
594 - * @param array $settings
595 - */
596 - return apply_filters( 'frm_stripe_appearance_rules', $rules, $settings );
597 549 }
598 550
599 551 /**
600 - * Get the border width for Stripe elements.
601 - *
602 - * @since 6.21
603 - *
604 - * @param array $settings
605 - *
606 - * @return string
607 - */
608 - private static function get_border_width( $settings ) {
609 - if ( ! empty( $settings['field_shape_type'] ) && 'underline' === $settings['field_shape_type'] ) {
610 - return '0 0 ' . $settings['field_border_width'] . ' 0';
611 - }
612 - return $settings['field_border_width'];
613 - }
614 -
615 - /**
616 - * Get the border radius for Stripe elements.
617 - *
618 - * @since 6.21
619 - *
620 - * @param array $settings
621 - *
622 - * @return string
623 - */
624 - private static function get_border_radius( $settings ) {
625 - if ( ! empty( $settings['field_shape_type'] ) ) {
626 - switch ( $settings['field_shape_type'] ) {
627 - case 'underline':
628 - case 'regular':
629 - return '0px';
630 - case 'circle':
631 - return '30px';
632 - }
633 - }
634 - return $settings['border_radius'];
635 - }
636 -
637 - /**
638 552 * Get the language to use for Stripe elements.
639 553 *
640 554 * @since 6.5, introduced in v2.0 of the Stripe add on.
641 - *
642 555 * @return string
643 556 */
644 557 private static function get_locale() {
645 558 $allowed = array( 'ar', 'da', 'de', 'en', 'es', 'fi', 'fr', 'he', 'it', 'ja', 'nl', 'no', 'pl', 'ru', 'sv', 'zh' );
@@ -657,17 +570,24 @@
657 570 *
658 571 * @param array $errors
659 572 * @param stdClass $field
660 573 * @param array $values
661 - *
662 574 * @return array
663 575 */
664 576 public static function remove_cc_validation( $errors, $field, $values ) {
665 577 $has_processed = isset( $_POST[ 'frmintent' . $field->form_id ] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
666 -
667 578 if ( ! $has_processed ) {
668 579 return $errors;
669 580 }
670 581
671 - return FrmTransLiteActionsController::remove_cc_errors( $errors, $field );
582 + $field_id = isset( $field->temp_id ) ? $field->temp_id : $field->id;
583 +
584 + if ( isset( $errors[ 'field' . $field_id . '-cc' ] ) ) {
585 + unset( $errors[ 'field' . $field_id . '-cc' ] );
586 + }
587 + if ( isset( $errors[ 'field' . $field_id ] ) ) {
588 + unset( $errors[ 'field' . $field_id ] );
589 + }
590 +
591 + return $errors;
672 592 }
673 593 }