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/helpers/FrmTransLiteAppHelper.php +301 -77 6.5.3 → trunk View file →
@@ -5,13 +5,8 @@
5 5
6 6 class FrmTransLiteAppHelper {
7 7
8 8 /**
9 - * @var bool|null
10 - */
11 - private static $should_fallback_to_paypal;
12 -
13 - /**
14 9 * @return string
15 10 */
16 11 public static function plugin_path() {
17 12 return FrmAppHelper::plugin_path() . '/stripe/';
@@ -43,8 +38,9 @@
43 38 */
44 39 public static function payments_table_exists() {
45 40 $db = new FrmTransLiteDb();
46 41 $option = get_option( $db->db_opt_name );
42 +
47 43 if ( false !== $option ) {
48 44 return true;
49 45 }
50 46
@@ -49,8 +45,9 @@
49 45 }
50 46
51 47 if ( class_exists( 'FrmPaymentsController' ) && isset( FrmPaymentsController::$db_opt_name ) ) {
52 48 $option = get_option( FrmPaymentsController::$db_opt_name );
49 +
53 50 if ( false !== $option ) {
54 51 return true;
55 52 }
56 53 }
@@ -61,13 +58,14 @@
61 58 /**
62 59 * Get a payment status label.
63 60 *
64 61 * @param string $status The lowercase payment status value.
62 + *
65 63 * @return string
66 64 */
67 65 public static function show_status( $status ) {
68 66 $statuses = array_merge( self::get_payment_statuses(), self::get_subscription_statuses() );
69 - return isset( $statuses[ $status ] ) ? $statuses[ $status ] : $status;
67 + return $statuses[ $status ] ?? $status;
70 68 }
71 69
72 70 /**
73 71 * Get Payment status from a payment with support for PayPal backward compatibility.
@@ -72,12 +70,13 @@
72 70 /**
73 71 * Get Payment status from a payment with support for PayPal backward compatibility.
74 72 *
75 73 * @param stdClass $payment
74 + *
76 75 * @return string
77 76 */
78 77 public static function get_payment_status( $payment ) {
79 - if ( $payment->status ) {
78 + if ( ! empty( $payment->status ) ) {
80 79 return $payment->status;
81 80 }
82 81 // PayPal fallback.
83 82 return ! empty( $payment->completed ) ? 'complete' : 'pending';
@@ -116,8 +115,9 @@
116 115 * This is called when processing events in the Stripe add on.
117 116 *
118 117 * @param array $payment_values
119 118 * @param string $message
119 + *
120 120 * @return void
121 121 */
122 122 public static function add_note_to_payment( &$payment_values, $message = '' ) {
123 123 if ( ! $message ) {
@@ -126,14 +126,15 @@
126 126 __( 'Payment %s', 'formidable' ),
127 127 $payment_values['status']
128 128 );
129 129 }
130 - $payment_values['meta_value'] = isset( $payment_values['meta_value'] ) ? $payment_values['meta_value'] : array();
130 + $payment_values['meta_value'] = $payment_values['meta_value'] ?? array();
131 131 $payment_values['meta_value'] = self::add_meta_to_payment( $payment_values['meta_value'], $message );
132 132 }
133 133
134 134 /**
135 - * @param string $note
135 + * @param array|string $meta_value
136 + * @param string $note
136 137 *
137 138 * @return array
138 139 */
139 140 public static function add_meta_to_payment( $meta_value, $note ) {
@@ -146,18 +147,21 @@
146 147 }
147 148
148 149 /**
149 150 * @param string $option
150 - * @param array $atts
151 + * @param array $atts
152 + *
153 + * @return mixed
151 154 */
152 155 public static function get_action_setting( $option, $atts ) {
153 156 $settings = self::get_action_settings( $atts );
154 - $value = isset( $settings[ $option ] ) ? $settings[ $option ] : '';
155 - return $value;
157 + return $settings[ $option ] ?? '';
156 158 }
157 159
158 160 /**
159 161 * @param array $atts
162 + *
163 + * @return array
160 164 */
161 165 public static function get_action_settings( $atts ) {
162 166 if ( ! isset( $atts['payment'] ) ) {
163 167 return array();
@@ -163,29 +167,29 @@
163 167 return array();
164 168 }
165 169
166 170 $atts['payment'] = (array) $atts['payment'];
171 +
167 172 if ( empty( $atts['payment']['action_id'] ) ) {
168 173 return array();
169 174 }
170 175
171 176 $form_action = FrmTransLiteAction::get_single_action_type( $atts['payment']['action_id'], 'payment' );
172 - if ( ! $form_action ) {
173 - return array();
174 - }
175 177
176 - return $form_action->post_content;
178 + return $form_action ? $form_action->post_content : array();
177 179 }
178 180
179 181 /**
180 182 * Allow entry values, default values, and other shortcodes
181 183 *
182 - * @param array $atts - Includes value (required), form, entry
183 - * @return string|int
184 + * @param array $atts Includes value (required), form, entry.
185 + *
186 + * @return int|string
184 187 */
185 188 public static function process_shortcodes( $atts ) {
186 189 $value = $atts['value'];
187 - if ( strpos( $value, '[' ) === false ) {
190 +
191 + if ( ! str_contains( $value, '[' ) ) {
188 192 return $value;
189 193 }
190 194
191 195 if ( is_callable( 'FrmProFieldsHelper::replace_non_standard_formidable_shortcodes' ) ) {
@@ -195,28 +199,29 @@
195 199 if ( ! empty( $atts['entry'] ) ) {
196 200 if ( ! isset( $atts['form'] ) ) {
197 201 $atts['form'] = FrmForm::getOne( $atts['entry']->form_id );
198 202 }
203 +
199 204 $value = apply_filters( 'frm_content', $value, $atts['form'], $atts['entry'] );
200 205 }
201 206
202 - $value = do_shortcode( $value );
203 - return $value;
207 + return do_shortcode( $value );
204 208 }
205 209
206 210 /**
207 211 * @param object $sub
212 + *
208 213 * @return string
209 214 */
210 215 public static function format_billing_cycle( $sub ) {
211 216 $amount = self::formatted_amount( $sub );
212 217 $interval = self::get_repeat_label_from_value( $sub->time_interval, $sub->interval_count );
213 - if ( $sub->interval_count == 1 ) {
214 - $amount = $amount . '/' . $interval;
215 - } else {
216 - $amount = $amount . ' every ' . $sub->interval_count . ' ' . $interval;
218 +
219 + if ( (int) $sub->interval_count === 1 ) {
220 + return $amount . '/' . $interval;
217 221 }
218 - return $amount;
222 +
223 + return $amount . ' every ' . $sub->interval_count . ' ' . $interval;
219 224 }
220 225
221 226 /**
222 227 * @return array
@@ -233,8 +238,9 @@
233 238 /**
234 239 * @since 6.5, introduced in v1.16 of the Payments submodule.
235 240 *
236 241 * @param int $number
242 + *
237 243 * @return array
238 244 */
239 245 private static function get_plural_repeat_times( $number ) {
240 246 return array(
@@ -248,19 +254,22 @@
248 254 /**
249 255 * @since 6.5, introduced in v1.16 of the Payments submodule.
250 256 *
251 257 * @param string $value
252 - * @param int $number
258 + * @param int $number
259 + *
253 260 * @return string
254 261 */
255 262 public static function get_repeat_label_from_value( $value, $number ) {
256 263 $times = self::get_plural_repeat_times( $number );
257 - if ( isset( $times[ $value ] ) ) {
258 - $value = $times[ $value ];
259 - }
260 - return $value;
264 + return $times[ $value ] ?? $value;
261 265 }
262 266
267 + /**
268 + * @param array|float|int|object $payment
269 + *
270 + * @return string
271 + */
263 272 public static function formatted_amount( $payment ) {
264 273 $currency = '';
265 274 $amount = $payment;
266 275
@@ -281,10 +290,37 @@
281 290 return $amount;
282 291 }
283 292
284 293 /**
294 + * Gets amount and currency from payment object or amount.
295 + *
296 + * @since 6.7
297 + *
298 + * @param array|float|object|string $payment Payment object, payment array or amount.
299 + *
300 + * @return array Return the array with the first element is the amount, the second one is the currency value.
301 + */
302 + public static function get_amount_and_currency_from_payment( $payment ) {
303 + $currency = '';
304 + $amount = $payment;
305 +
306 + if ( is_object( $payment ) || is_array( $payment ) ) {
307 + $payment = (array) $payment;
308 + $amount = $payment['amount'];
309 + $currency = self::get_action_setting( 'currency', array( 'payment' => $payment ) );
310 + }
311 +
312 + if ( ! $currency ) {
313 + $currency = 'usd';
314 + }
315 +
316 + return array( $amount, $currency );
317 + }
318 +
319 + /**
285 320 * @param array $currency
286 321 * @param float $amount
322 + *
287 323 * @return void
288 324 */
289 325 public static function format_amount_for_currency( $currency, &$amount ) {
290 326 $amount = number_format( $amount, $currency['decimals'], $currency['decimal_separator'], $currency['thousand_separator'] );
@@ -296,28 +332,26 @@
296 332 /**
297 333 * @return string
298 334 */
299 335 public static function get_date_format() {
300 - $date_format = 'm/d/Y';
301 - if ( class_exists( 'FrmProAppHelper' ) ) {
302 - $frmpro_settings = FrmProAppHelper::get_settings();
303 - if ( $frmpro_settings ) {
304 - $date_format = $frmpro_settings->date_format;
305 - }
306 - } else {
307 - $date_format = get_option( 'date_format' );
336 + if ( ! class_exists( 'FrmProAppHelper' ) ) {
337 + return get_option( 'date_format' );
308 338 }
309 339
310 - return $date_format;
340 + $date_format = 'm/d/Y';
341 + $frmpro_settings = FrmProAppHelper::get_settings();
342 +
343 + return $frmpro_settings ? $frmpro_settings->date_format : $date_format;
311 344 }
312 345
313 346 /**
314 347 * @param string $date
315 348 * @param string $format
349 + *
316 350 * @return string
317 351 */
318 352 public static function format_the_date( $date, $format = '' ) {
319 - if ( empty( $format ) ) {
353 + if ( ! $format ) {
320 354 $format = self::get_date_format();
321 355 }
322 356 return date_i18n( $format, strtotime( $date ) );
323 357 }
@@ -327,45 +361,62 @@
327 361 *
328 362 * @return int
329 363 */
330 364 public static function get_user_id_for_current_payment() {
331 - $user_id = 0;
332 - if ( is_user_logged_in() ) {
333 - $user_id = get_current_user_id();
334 - }
335 - return $user_id;
365 + $user_id = is_user_logged_in() ? get_current_user_id() : 0;
366 +
367 + /**
368 + * Set the user who owns the payment being made.
369 + * This makes it possible for an add on to claim the payment for a user who isn't logged in yet,
370 + * for example when the same submission registers them.
371 + *
372 + * @since 6.35
373 + *
374 + * @param int $user_id The logged in user, or 0 for a guest.
375 + */
376 + $user_id = (int) apply_filters( 'frm_payment_user_id', $user_id );
377 +
378 + return max( 0, $user_id );
336 379 }
337 380
338 381 /**
339 382 * @param int $user_id
383 + *
340 384 * @return string
341 385 */
342 386 public static function get_user_link( $user_id ) {
343 - $user_link = esc_html__( 'Guest', 'formidable' );
344 387 if ( $user_id ) {
345 388 $user = get_userdata( $user_id );
389 +
346 390 if ( $user ) {
347 - $user_link = '<a href="' . esc_url( admin_url( 'user-edit.php?user_id=' . $user_id ) ) . '">' . esc_html( $user->display_name ) . '</a>';
391 + return '<a href="' . esc_url( admin_url( 'user-edit.php?user_id=' . $user_id ) ) . '">' . esc_html( $user->display_name ) . '</a>';
348 392 }
349 393 }
350 - return $user_link;
394 +
395 + return esc_html__( 'Guest', 'formidable' );
351 396 }
352 397
353 398 /**
354 399 * @param mixed $value
355 400 * @param string $label
401 + *
356 402 * @return void
357 403 */
358 404 public static function show_in_table( $value, $label ) {
359 - if ( ! empty( $value ) ) { ?>
360 - <tr>
361 - <th scope="row"><?php echo esc_html( $label ); ?>:</th>
362 - <td>
363 - <?php echo esc_html( $value ); ?>
364 - </td>
365 - </tr>
366 - <?php
405 + if ( ! $value ) {
406 + return;
367 407 }
408 +
409 + // phpcs:disable Generic.WhiteSpace.ScopeIndent
410 + ?>
411 + <tr>
412 + <th scope="row"><?php echo esc_html( $label ); ?>:</th>
413 + <td>
414 + <?php echo esc_html( $value ); ?>
415 + </td>
416 + </tr>
417 + <?php
418 + // phpcs:enable Generic.WhiteSpace.ScopeIndent
368 419 }
369 420
370 421 /**
371 422 * Echo a link that includes a data-deleteconfirm attribute.
@@ -373,14 +424,15 @@
373 424 *
374 425 * @since 6.5
375 426 *
376 427 * @param string $link
428 + *
377 429 * @return void
378 430 */
379 431 public static function echo_confirmation_link( $link ) {
380 - $filter = __CLASS__ . '::allow_deleteconfirm_data_attribute';
432 + $filter = self::class . '::allow_deleteconfirm_data_attribute';
381 433 add_filter( 'frm_striphtml_allowed_tags', $filter );
382 - echo FrmAppHelper::kses( $link, array( 'a' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
434 + FrmAppHelper::kses_echo( $link, array( 'a' ) );
383 435 remove_filter( 'frm_striphtml_allowed_tags', $filter );
384 436 }
385 437
386 438 /**
@@ -389,8 +441,9 @@
389 441 *
390 442 * @since 6.5
391 443 *
392 444 * @param array $allowed
445 + *
393 446 * @return array
394 447 */
395 448 public static function allow_deleteconfirm_data_attribute( $allowed ) {
396 449 $allowed['a']['data-deleteconfirm'] = true;
@@ -403,9 +456,9 @@
403 456 * Formats non zero-decimal currencies.
404 457 *
405 458 * @since 6.5
406 459 *
407 - * @param string|int $amount
460 + * @param int|string $amount
408 461 * @param WP_Post $action
409 462 *
410 463 * @return string
411 464 */
@@ -414,10 +467,11 @@
414 467 return $amount;
415 468 }
416 469
417 470 $currency = FrmCurrencyHelper::get_currency( $action->post_content['currency'] );
471 +
418 472 if ( ! empty( $currency['decimals'] ) ) {
419 - $amount = number_format( ( $amount / 100 ), 2, '.', '' );
473 + return number_format( $amount / 100, 2, '.', '' );
420 474 }
421 475
422 476 return $amount;
423 477 }
@@ -422,30 +476,200 @@
422 476 return $amount;
423 477 }
424 478
425 479 /**
480 + * Get a human readable translated 'Test' or 'Live' string if the column value is defined.
481 + * Old payments will just output an empty string.
482 + *
483 + * @since 6.6
484 + *
485 + * @param stdClass $payment
486 + *
487 + * @return string
488 + */
489 + public static function get_test_mode_display_string( $payment ) {
490 + if ( ! isset( $payment->test ) ) {
491 + return '';
492 + }
493 + return $payment->test ? __( 'Test', 'formidable' ) : __( 'Live', 'formidable' );
494 + }
495 +
496 + /**
497 + * Returns the count of completed payments.
498 + *
499 + * @since 6.11
500 + *
501 + * @param array $payments
502 + *
503 + * @return int
504 + */
505 + public static function count_completed_payments( $payments ) {
506 + $count = 0;
507 +
508 + foreach ( $payments as $payment ) {
509 + if ( $payment->status === 'complete' ) {
510 + $count++;
511 + }
512 + }
513 +
514 + return $count;
515 + }
516 +
517 + /**
518 + * @return array
519 + */
520 + public static function get_gateways() {
521 + return apply_filters( 'frm_payment_gateways', array() );
522 + }
523 +
524 + /**
525 + * @param array|string $gateway
526 + * @param string $setting
527 + *
528 + * @return mixed
529 + */
530 + public static function get_setting_for_gateway( $gateway, $setting ) {
531 + $gateways = self::get_gateways();
532 + $value = '';
533 +
534 + if ( is_array( $gateway ) ) {
535 + $gateway = reset( $gateway );
536 + }
537 +
538 + if ( isset( $gateways[ $gateway ] ) ) {
539 + return $gateways[ $gateway ][ $setting ];
540 + }
541 +
542 + return $value;
543 + }
544 +
545 + /**
546 + * Show the currency dropdown for a Payment action.
547 + * When Square is selected, this dropdown is disabled and will always use "Use Square Merchant Currency".
548 + *
549 + * @since 6.22
550 + *
551 + * @param string $id
552 + * @param string $name
553 + * @param array $action_settings
554 + *
555 + * @return void
556 + */
557 + public static function show_currency_dropdown( $id, $name, $action_settings ) {
558 + $selected = $action_settings['currency'];
559 + $gateways = (array) $action_settings['gateway'];
560 + $select_attrs = array(
561 + 'id' => $id,
562 + 'name' => $name,
563 + );
564 +
565 + if ( in_array( 'square', $gateways, true ) ) {
566 + $select_attrs['disabled'] = 'disabled';
567 + $selected = '';
568 + }
569 +
570 + $currencies = FrmCurrencyHelper::get_currencies();
571 + // phpcs:disable Generic.WhiteSpace.ScopeIndent
572 + ?>
573 + <select <?php FrmAppHelper::array_to_html_params( $select_attrs, true ); ?>>
574 + <?php
575 + if ( in_array( 'square', $gateways, true ) ) {
576 + $option_params = array(
577 + 'class' => 'square-currency',
578 + 'selected' => 'selected',
579 + 'value' => 'square',
580 + );
581 + ?>
582 + <option <?php FrmAppHelper::array_to_html_params( $option_params, true ); ?>><?php esc_html_e( 'Use Square Merchant Currency', 'formidable' ); ?></option>
583 + <?php
584 + }
585 +
586 + foreach ( $currencies as $code => $currency ) {
587 + FrmHtmlHelper::echo_dropdown_option(
588 + $currency['name'] . ' (' . strtoupper( $code ) . ')',
589 + $selected === strtolower( $code ),
590 + array(
591 + 'value' => strtolower( $code ),
592 + )
593 + );
594 + unset( $currency, $code );
595 + }
596 + ?>
597 + </select>
598 + <?php
599 + // phpcs:enable Generic.WhiteSpace.ScopeIndent
600 + }
601 +
602 + /**
603 + * @since 6.27
604 + *
426 605 * @return bool
427 606 */
607 + public static function payments_submodule_or_paypal_is_active() {
608 + return class_exists( 'FrmTransAppController' ) || class_exists( 'FrmPaymentsController' );
609 + }
610 +
611 + /**
612 + * @deprecated 6.27
613 + *
614 + * @return bool
615 + */
428 616 public static function should_fallback_to_paypal() {
429 - if ( isset( self::$should_fallback_to_paypal ) ) {
430 - return self::$should_fallback_to_paypal;
431 - }
617 + _deprecated_function( __METHOD__, '6.27' );
618 + return false;
619 + }
432 620
433 - if ( ! class_exists( 'FrmPaymentsController' ) || ! isset( FrmPaymentsController::$db_opt_name ) ) {
434 - self::$should_fallback_to_paypal = false;
435 - return false;
436 - }
621 + /**
622 + * Render the gateway icon buttons for the payment action settings.
623 + *
624 + * @param array $gateways
625 + * @param WP_Post $form_action
626 + * @param FrmFormAction $action_control
627 + *
628 + * @return void
629 + */
630 + public static function show_gateway_buttons( $gateways, $form_action, $action_control ) {
631 + $gateway_order = array( 'stripe', 'square', 'paypal' );
632 + $gateways = self::sort_gateways( $gateways, $gateway_order );
437 633
438 - $db = new FrmTransLiteDb();
439 - $option = get_option( $db->db_opt_name );
440 - if ( false !== $option ) {
441 - // Don't fallback to PayPal if Stripe migrations have run.
442 - self::$should_fallback_to_paypal = false;
443 - return false;
634 + include self::plugin_path() . '/views/action-settings/gateway-buttons.php';
635 + }
636 +
637 + /**
638 + * Sort gateways by a predefined order.
639 + * Unlisted gateways are appended at the end.
640 + *
641 + * @param array $gateways
642 + * @param array $order Gateway keys in desired order.
643 + *
644 + * @return array
645 + */
646 + private static function sort_gateways( $gateways, $order ) {
647 + $sorted = array();
648 +
649 + foreach ( $order as $key ) {
650 + if ( isset( $gateways[ $key ] ) ) {
651 + $sorted[ $key ] = $gateways[ $key ];
652 + }
444 653 }
445 654
446 - $option = get_option( FrmPaymentsController::$db_opt_name );
447 - self::$should_fallback_to_paypal = false !== $option;
655 + return $sorted + $gateways;
656 + }
448 657
449 - return self::$should_fallback_to_paypal;
658 + /**
659 + * @since 6.32.1
660 + *
661 + * @param string $gateway 'stripe', 'square', or 'paypal'.
662 + * @param string $mode 'test' or 'live'.
663 + *
664 + * @return void
665 + */
666 + public static function trigger_gateway_disconnected_hook( $gateway, $mode ) {
667 + /**
668 + * @since 6.32.1
669 + *
670 + * @param string $gateway 'stripe', 'square', or 'paypal'.
671 + * @param string $mode 'test' or 'live'.
672 + */
673 + do_action( 'frm_disconnected_gateway', $gateway, $mode );
450 674 }
451 675 }