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 +339 -56 6.5 → trunk View file →
@@ -26,16 +26,46 @@
26 26 return basename( self::plugin_path() );
27 27 }
28 28
29 29 /**
30 + * Check if the payments table has been created.
31 + * This includes either the frm_trans_db_version option (used in Stripe Lite and the Payments submodule) or frm_pay_db_version option (from the PayPal add on).
32 + *
33 + * @since 6.5
34 + * @since 6.5.1 A check for the PayPal add on option
35 + * @since 6.5.1 This function was renamed and moved from FrmStrpLiteAppController::payments_are_installed and made public.
36 + *
37 + * @return bool
38 + */
39 + public static function payments_table_exists() {
40 + $db = new FrmTransLiteDb();
41 + $option = get_option( $db->db_opt_name );
42 +
43 + if ( false !== $option ) {
44 + return true;
45 + }
46 +
47 + if ( class_exists( 'FrmPaymentsController' ) && isset( FrmPaymentsController::$db_opt_name ) ) {
48 + $option = get_option( FrmPaymentsController::$db_opt_name );
49 +
50 + if ( false !== $option ) {
51 + return true;
52 + }
53 + }
54 +
55 + return false;
56 + }
57 +
58 + /**
30 59 * Get a payment status label.
31 60 *
32 61 * @param string $status The lowercase payment status value.
62 + *
33 63 * @return string
34 64 */
35 65 public static function show_status( $status ) {
36 66 $statuses = array_merge( self::get_payment_statuses(), self::get_subscription_statuses() );
37 - return isset( $statuses[ $status ] ) ? $statuses[ $status ] : $status;
67 + return $statuses[ $status ] ?? $status;
38 68 }
39 69
40 70 /**
41 71 * Get Payment status from a payment with support for PayPal backward compatibility.
@@ -40,12 +70,13 @@
40 70 /**
41 71 * Get Payment status from a payment with support for PayPal backward compatibility.
42 72 *
43 73 * @param stdClass $payment
74 + *
44 75 * @return string
45 76 */
46 77 public static function get_payment_status( $payment ) {
47 - if ( $payment->status ) {
78 + if ( ! empty( $payment->status ) ) {
48 79 return $payment->status;
49 80 }
50 81 // PayPal fallback.
51 82 return ! empty( $payment->completed ) ? 'complete' : 'pending';
@@ -84,8 +115,9 @@
84 115 * This is called when processing events in the Stripe add on.
85 116 *
86 117 * @param array $payment_values
87 118 * @param string $message
119 + *
88 120 * @return void
89 121 */
90 122 public static function add_note_to_payment( &$payment_values, $message = '' ) {
91 123 if ( ! $message ) {
@@ -94,14 +126,15 @@
94 126 __( 'Payment %s', 'formidable' ),
95 127 $payment_values['status']
96 128 );
97 129 }
98 - $payment_values['meta_value'] = isset( $payment_values['meta_value'] ) ? $payment_values['meta_value'] : array();
130 + $payment_values['meta_value'] = $payment_values['meta_value'] ?? array();
99 131 $payment_values['meta_value'] = self::add_meta_to_payment( $payment_values['meta_value'], $message );
100 132 }
101 133
102 134 /**
103 - * @param string $note
135 + * @param array|string $meta_value
136 + * @param string $note
104 137 *
105 138 * @return array
106 139 */
107 140 public static function add_meta_to_payment( $meta_value, $note ) {
@@ -114,18 +147,21 @@
114 147 }
115 148
116 149 /**
117 150 * @param string $option
118 - * @param array $atts
151 + * @param array $atts
152 + *
153 + * @return mixed
119 154 */
120 155 public static function get_action_setting( $option, $atts ) {
121 156 $settings = self::get_action_settings( $atts );
122 - $value = isset( $settings[ $option ] ) ? $settings[ $option ] : '';
123 - return $value;
157 + return $settings[ $option ] ?? '';
124 158 }
125 159
126 160 /**
127 161 * @param array $atts
162 + *
163 + * @return array
128 164 */
129 165 public static function get_action_settings( $atts ) {
130 166 if ( ! isset( $atts['payment'] ) ) {
131 167 return array();
@@ -131,29 +167,29 @@
131 167 return array();
132 168 }
133 169
134 170 $atts['payment'] = (array) $atts['payment'];
171 +
135 172 if ( empty( $atts['payment']['action_id'] ) ) {
136 173 return array();
137 174 }
138 175
139 176 $form_action = FrmTransLiteAction::get_single_action_type( $atts['payment']['action_id'], 'payment' );
140 - if ( ! $form_action ) {
141 - return array();
142 - }
143 177
144 - return $form_action->post_content;
178 + return $form_action ? $form_action->post_content : array();
145 179 }
146 180
147 181 /**
148 182 * Allow entry values, default values, and other shortcodes
149 183 *
150 - * @param array $atts - Includes value (required), form, entry
151 - * @return string|int
184 + * @param array $atts Includes value (required), form, entry.
185 + *
186 + * @return int|string
152 187 */
153 188 public static function process_shortcodes( $atts ) {
154 189 $value = $atts['value'];
155 - if ( strpos( $value, '[' ) === false ) {
190 +
191 + if ( ! str_contains( $value, '[' ) ) {
156 192 return $value;
157 193 }
158 194
159 195 if ( is_callable( 'FrmProFieldsHelper::replace_non_standard_formidable_shortcodes' ) ) {
@@ -163,28 +199,29 @@
163 199 if ( ! empty( $atts['entry'] ) ) {
164 200 if ( ! isset( $atts['form'] ) ) {
165 201 $atts['form'] = FrmForm::getOne( $atts['entry']->form_id );
166 202 }
203 +
167 204 $value = apply_filters( 'frm_content', $value, $atts['form'], $atts['entry'] );
168 205 }
169 206
170 - $value = do_shortcode( $value );
171 - return $value;
207 + return do_shortcode( $value );
172 208 }
173 209
174 210 /**
175 211 * @param object $sub
212 + *
176 213 * @return string
177 214 */
178 215 public static function format_billing_cycle( $sub ) {
179 216 $amount = self::formatted_amount( $sub );
180 217 $interval = self::get_repeat_label_from_value( $sub->time_interval, $sub->interval_count );
181 - if ( $sub->interval_count == 1 ) {
182 - $amount = $amount . '/' . $interval;
183 - } else {
184 - $amount = $amount . ' every ' . $sub->interval_count . ' ' . $interval;
218 +
219 + if ( (int) $sub->interval_count === 1 ) {
220 + return $amount . '/' . $interval;
185 221 }
186 - return $amount;
222 +
223 + return $amount . ' every ' . $sub->interval_count . ' ' . $interval;
187 224 }
188 225
189 226 /**
190 227 * @return array
@@ -201,8 +238,9 @@
201 238 /**
202 239 * @since 6.5, introduced in v1.16 of the Payments submodule.
203 240 *
204 241 * @param int $number
242 + *
205 243 * @return array
206 244 */
207 245 private static function get_plural_repeat_times( $number ) {
208 246 return array(
@@ -216,19 +254,22 @@
216 254 /**
217 255 * @since 6.5, introduced in v1.16 of the Payments submodule.
218 256 *
219 257 * @param string $value
220 - * @param int $number
258 + * @param int $number
259 + *
221 260 * @return string
222 261 */
223 262 public static function get_repeat_label_from_value( $value, $number ) {
224 263 $times = self::get_plural_repeat_times( $number );
225 - if ( isset( $times[ $value ] ) ) {
226 - $value = $times[ $value ];
227 - }
228 - return $value;
264 + return $times[ $value ] ?? $value;
229 265 }
230 266
267 + /**
268 + * @param array|float|int|object $payment
269 + *
270 + * @return string
271 + */
231 272 public static function formatted_amount( $payment ) {
232 273 $currency = '';
233 274 $amount = $payment;
234 275
@@ -249,10 +290,37 @@
249 290 return $amount;
250 291 }
251 292
252 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 + /**
253 320 * @param array $currency
254 321 * @param float $amount
322 + *
255 323 * @return void
256 324 */
257 325 public static function format_amount_for_currency( $currency, &$amount ) {
258 326 $amount = number_format( $amount, $currency['decimals'], $currency['decimal_separator'], $currency['thousand_separator'] );
@@ -264,28 +332,26 @@
264 332 /**
265 333 * @return string
266 334 */
267 335 public static function get_date_format() {
268 - $date_format = 'm/d/Y';
269 - if ( class_exists( 'FrmProAppHelper' ) ) {
270 - $frmpro_settings = FrmProAppHelper::get_settings();
271 - if ( $frmpro_settings ) {
272 - $date_format = $frmpro_settings->date_format;
273 - }
274 - } else {
275 - $date_format = get_option( 'date_format' );
336 + if ( ! class_exists( 'FrmProAppHelper' ) ) {
337 + return get_option( 'date_format' );
276 338 }
277 339
278 - 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;
279 344 }
280 345
281 346 /**
282 347 * @param string $date
283 348 * @param string $format
349 + *
284 350 * @return string
285 351 */
286 352 public static function format_the_date( $date, $format = '' ) {
287 - if ( empty( $format ) ) {
353 + if ( ! $format ) {
288 354 $format = self::get_date_format();
289 355 }
290 356 return date_i18n( $format, strtotime( $date ) );
291 357 }
@@ -295,45 +361,62 @@
295 361 *
296 362 * @return int
297 363 */
298 364 public static function get_user_id_for_current_payment() {
299 - $user_id = 0;
300 - if ( is_user_logged_in() ) {
301 - $user_id = get_current_user_id();
302 - }
303 - 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 );
304 379 }
305 380
306 381 /**
307 382 * @param int $user_id
383 + *
308 384 * @return string
309 385 */
310 386 public static function get_user_link( $user_id ) {
311 - $user_link = esc_html__( 'Guest', 'formidable' );
312 387 if ( $user_id ) {
313 388 $user = get_userdata( $user_id );
389 +
314 390 if ( $user ) {
315 - $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>';
316 392 }
317 393 }
318 - return $user_link;
394 +
395 + return esc_html__( 'Guest', 'formidable' );
319 396 }
320 397
321 398 /**
322 399 * @param mixed $value
323 400 * @param string $label
401 + *
324 402 * @return void
325 403 */
326 404 public static function show_in_table( $value, $label ) {
327 - if ( ! empty( $value ) ) { ?>
328 - <tr>
329 - <th scope="row"><?php echo esc_html( $label ); ?>:</th>
330 - <td>
331 - <?php echo esc_html( $value ); ?>
332 - </td>
333 - </tr>
334 - <?php
405 + if ( ! $value ) {
406 + return;
335 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
336 419 }
337 420
338 421 /**
339 422 * Echo a link that includes a data-deleteconfirm attribute.
@@ -341,14 +424,15 @@
341 424 *
342 425 * @since 6.5
343 426 *
344 427 * @param string $link
428 + *
345 429 * @return void
346 430 */
347 431 public static function echo_confirmation_link( $link ) {
348 - $filter = __CLASS__ . '::allow_deleteconfirm_data_attribute';
432 + $filter = self::class . '::allow_deleteconfirm_data_attribute';
349 433 add_filter( 'frm_striphtml_allowed_tags', $filter );
350 - echo FrmAppHelper::kses( $link, array( 'a' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
434 + FrmAppHelper::kses_echo( $link, array( 'a' ) );
351 435 remove_filter( 'frm_striphtml_allowed_tags', $filter );
352 436 }
353 437
354 438 /**
@@ -357,8 +441,9 @@
357 441 *
358 442 * @since 6.5
359 443 *
360 444 * @param array $allowed
445 + *
361 446 * @return array
362 447 */
363 448 public static function allow_deleteconfirm_data_attribute( $allowed ) {
364 449 $allowed['a']['data-deleteconfirm'] = true;
@@ -371,9 +456,9 @@
371 456 * Formats non zero-decimal currencies.
372 457 *
373 458 * @since 6.5
374 459 *
375 - * @param string|int $amount
460 + * @param int|string $amount
376 461 * @param WP_Post $action
377 462 *
378 463 * @return string
379 464 */
@@ -382,11 +467,209 @@
382 467 return $amount;
383 468 }
384 469
385 470 $currency = FrmCurrencyHelper::get_currency( $action->post_content['currency'] );
471 +
386 472 if ( ! empty( $currency['decimals'] ) ) {
387 - $amount = number_format( ( $amount / 100 ), 2, '.', '' );
473 + return number_format( $amount / 100, 2, '.', '' );
388 474 }
389 475
390 476 return $amount;
477 + }
478 +
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 + *
605 + * @return bool
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 + */
616 + public static function should_fallback_to_paypal() {
617 + _deprecated_function( __METHOD__, '6.27' );
618 + return false;
619 + }
620 +
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 );
633 +
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 + }
653 + }
654 +
655 + return $sorted + $gateways;
656 + }
657 +
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 );
391 674 }
392 675 }