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
formidable / stripe / helpers / FrmTransLiteAppHelper.php

FrmTransLiteAppHelper.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More trunk, at stripe/helpers/FrmTransLiteAppHelper.php

676 lines 16.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 die( 'You are not allowed to call this page directly.' );
4 }
5
6 class FrmTransLiteAppHelper {
7
8 /**
9 * @return string
10 */
11 public static function plugin_path() {
12 return FrmAppHelper::plugin_path() . '/stripe/';
13 }
14
15 /**
16 * @return string
17 */
18 public static function plugin_url() {
19 return FrmAppHelper::plugin_url() . '/stripe/';
20 }
21
22 /**
23 * @return string
24 */
25 public static function plugin_folder() {
26 return basename( self::plugin_path() );
27 }
28
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 /**
59 * Get a payment status label.
60 *
61 * @param string $status The lowercase payment status value.
62 *
63 * @return string
64 */
65 public static function show_status( $status ) {
66 $statuses = array_merge( self::get_payment_statuses(), self::get_subscription_statuses() );
67 return $statuses[ $status ] ?? $status;
68 }
69
70 /**
71 * Get Payment status from a payment with support for PayPal backward compatibility.
72 *
73 * @param stdClass $payment
74 *
75 * @return string
76 */
77 public static function get_payment_status( $payment ) {
78 if ( ! empty( $payment->status ) ) {
79 return $payment->status;
80 }
81 // PayPal fallback.
82 return ! empty( $payment->completed ) ? 'complete' : 'pending';
83 }
84
85 /**
86 * @return string[]
87 */
88 public static function get_payment_statuses() {
89 return array(
90 'authorized' => __( 'Authorized', 'formidable' ),
91 'pending' => __( 'Pending', 'formidable' ),
92 'complete' => __( 'Completed', 'formidable' ),
93 'failed' => __( 'Failed', 'formidable' ),
94 'refunded' => __( 'Refunded', 'formidable' ),
95 'canceled' => __( 'Canceled', 'formidable' ),
96 'processing' => __( 'Processing', 'formidable' ),
97 );
98 }
99
100 /**
101 * @return string[]
102 */
103 public static function get_subscription_statuses() {
104 return array(
105 'pending' => __( 'Pending', 'formidable' ),
106 'active' => __( 'Active', 'formidable' ),
107 'future_cancel' => __( 'Canceled', 'formidable' ),
108 'canceled' => __( 'Canceled', 'formidable' ),
109 'void' => __( 'Void', 'formidable' ),
110 );
111 }
112
113 /**
114 * Add a note to payment data that will get saved to the payment meta.
115 * This is called when processing events in the Stripe add on.
116 *
117 * @param array $payment_values
118 * @param string $message
119 *
120 * @return void
121 */
122 public static function add_note_to_payment( &$payment_values, $message = '' ) {
123 if ( ! $message ) {
124 $message = sprintf(
125 // translators: %s: Payment status.
126 __( 'Payment %s', 'formidable' ),
127 $payment_values['status']
128 );
129 }
130 $payment_values['meta_value'] = $payment_values['meta_value'] ?? array();
131 $payment_values['meta_value'] = self::add_meta_to_payment( $payment_values['meta_value'], $message );
132 }
133
134 /**
135 * @param array|string $meta_value
136 * @param string $note
137 *
138 * @return array
139 */
140 public static function add_meta_to_payment( $meta_value, $note ) {
141 $meta_value = (array) maybe_unserialize( $meta_value );
142 $meta_value[] = array(
143 'message' => $note,
144 'date' => gmdate( 'Y-m-d H:i:s' ),
145 );
146 return $meta_value;
147 }
148
149 /**
150 * @param string $option
151 * @param array $atts
152 *
153 * @return mixed
154 */
155 public static function get_action_setting( $option, $atts ) {
156 $settings = self::get_action_settings( $atts );
157 return $settings[ $option ] ?? '';
158 }
159
160 /**
161 * @param array $atts
162 *
163 * @return array
164 */
165 public static function get_action_settings( $atts ) {
166 if ( ! isset( $atts['payment'] ) ) {
167 return array();
168 }
169
170 $atts['payment'] = (array) $atts['payment'];
171
172 if ( empty( $atts['payment']['action_id'] ) ) {
173 return array();
174 }
175
176 $form_action = FrmTransLiteAction::get_single_action_type( $atts['payment']['action_id'], 'payment' );
177
178 return $form_action ? $form_action->post_content : array();
179 }
180
181 /**
182 * Allow entry values, default values, and other shortcodes
183 *
184 * @param array $atts Includes value (required), form, entry.
185 *
186 * @return int|string
187 */
188 public static function process_shortcodes( $atts ) {
189 $value = $atts['value'];
190
191 if ( ! str_contains( $value, '[' ) ) {
192 return $value;
193 }
194
195 if ( is_callable( 'FrmProFieldsHelper::replace_non_standard_formidable_shortcodes' ) ) {
196 FrmProFieldsHelper::replace_non_standard_formidable_shortcodes( array(), $value );
197 }
198
199 if ( ! empty( $atts['entry'] ) ) {
200 if ( ! isset( $atts['form'] ) ) {
201 $atts['form'] = FrmForm::getOne( $atts['entry']->form_id );
202 }
203
204 $value = apply_filters( 'frm_content', $value, $atts['form'], $atts['entry'] );
205 }
206
207 return do_shortcode( $value );
208 }
209
210 /**
211 * @param object $sub
212 *
213 * @return string
214 */
215 public static function format_billing_cycle( $sub ) {
216 $amount = self::formatted_amount( $sub );
217 $interval = self::get_repeat_label_from_value( $sub->time_interval, $sub->interval_count );
218
219 if ( (int) $sub->interval_count === 1 ) {
220 return $amount . '/' . $interval;
221 }
222
223 return $amount . ' every ' . $sub->interval_count . ' ' . $interval;
224 }
225
226 /**
227 * @return array
228 */
229 public static function get_repeat_times() {
230 return array(
231 'day' => __( 'day(s)', 'formidable' ),
232 'week' => __( 'week(s)', 'formidable' ),
233 'month' => __( 'month(s)', 'formidable' ),
234 'year' => __( 'year(s)', 'formidable' ),
235 );
236 }
237
238 /**
239 * @since 6.5, introduced in v1.16 of the Payments submodule.
240 *
241 * @param int $number
242 *
243 * @return array
244 */
245 private static function get_plural_repeat_times( $number ) {
246 return array(
247 'day' => _n( 'day', 'days', $number, 'formidable' ),
248 'week' => _n( 'week', 'weeks', $number, 'formidable' ),
249 'month' => _n( 'month', 'months', $number, 'formidable' ),
250 'year' => _n( 'year', 'years', $number, 'formidable' ),
251 );
252 }
253
254 /**
255 * @since 6.5, introduced in v1.16 of the Payments submodule.
256 *
257 * @param string $value
258 * @param int $number
259 *
260 * @return string
261 */
262 public static function get_repeat_label_from_value( $value, $number ) {
263 $times = self::get_plural_repeat_times( $number );
264 return $times[ $value ] ?? $value;
265 }
266
267 /**
268 * @param array|float|int|object $payment
269 *
270 * @return string
271 */
272 public static function formatted_amount( $payment ) {
273 $currency = '';
274 $amount = $payment;
275
276 if ( is_object( $payment ) || is_array( $payment ) ) {
277 $payment = (array) $payment;
278 $amount = $payment['amount'];
279 $currency = self::get_action_setting( 'currency', array( 'payment' => $payment ) );
280 }
281
282 if ( ! $currency ) {
283 $currency = 'usd';
284 }
285
286 $currency = FrmCurrencyHelper::get_currency( $currency );
287
288 self::format_amount_for_currency( $currency, $amount );
289
290 return $amount;
291 }
292
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 /**
320 * @param array $currency
321 * @param float $amount
322 *
323 * @return void
324 */
325 public static function format_amount_for_currency( $currency, &$amount ) {
326 $amount = number_format( $amount, $currency['decimals'], $currency['decimal_separator'], $currency['thousand_separator'] );
327 $left_symbol = $currency['symbol_left'] . $currency['symbol_padding'];
328 $right_symbol = $currency['symbol_padding'] . $currency['symbol_right'];
329 $amount = $left_symbol . $amount . $right_symbol;
330 }
331
332 /**
333 * @return string
334 */
335 public static function get_date_format() {
336 if ( ! class_exists( 'FrmProAppHelper' ) ) {
337 return get_option( 'date_format' );
338 }
339
340 $date_format = 'm/d/Y';
341 $frmpro_settings = FrmProAppHelper::get_settings();
342
343 return $frmpro_settings ? $frmpro_settings->date_format : $date_format;
344 }
345
346 /**
347 * @param string $date
348 * @param string $format
349 *
350 * @return string
351 */
352 public static function format_the_date( $date, $format = '' ) {
353 if ( ! $format ) {
354 $format = self::get_date_format();
355 }
356 return date_i18n( $format, strtotime( $date ) );
357 }
358
359 /**
360 * Set a user id for current payment if a user is logged in.
361 *
362 * @return int
363 */
364 public static function get_user_id_for_current_payment() {
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 );
379 }
380
381 /**
382 * @param int $user_id
383 *
384 * @return string
385 */
386 public static function get_user_link( $user_id ) {
387 if ( $user_id ) {
388 $user = get_userdata( $user_id );
389
390 if ( $user ) {
391 return '<a href="' . esc_url( admin_url( 'user-edit.php?user_id=' . $user_id ) ) . '">' . esc_html( $user->display_name ) . '</a>';
392 }
393 }
394
395 return esc_html__( 'Guest', 'formidable' );
396 }
397
398 /**
399 * @param mixed $value
400 * @param string $label
401 *
402 * @return void
403 */
404 public static function show_in_table( $value, $label ) {
405 if ( ! $value ) {
406 return;
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
419 }
420
421 /**
422 * Echo a link that includes a data-deleteconfirm attribute.
423 * This includes refund links and links to cancel a subscription.
424 *
425 * @since 6.5
426 *
427 * @param string $link
428 *
429 * @return void
430 */
431 public static function echo_confirmation_link( $link ) {
432 $filter = self::class . '::allow_deleteconfirm_data_attribute';
433 add_filter( 'frm_striphtml_allowed_tags', $filter );
434 FrmAppHelper::kses_echo( $link, array( 'a' ) );
435 remove_filter( 'frm_striphtml_allowed_tags', $filter );
436 }
437
438 /**
439 * Allow the data-deleteconfirm attribute for confirmation links.
440 * The attribute is used for the confirmation message.
441 *
442 * @since 6.5
443 *
444 * @param array $allowed
445 *
446 * @return array
447 */
448 public static function allow_deleteconfirm_data_attribute( $allowed ) {
449 $allowed['a']['data-deleteconfirm'] = true;
450 $allowed['a']['data-frmverify'] = true;
451 $allowed['a']['data-frmverify-btn'] = true;
452 return $allowed;
453 }
454
455 /**
456 * Formats non zero-decimal currencies.
457 *
458 * @since 6.5
459 *
460 * @param int|string $amount
461 * @param WP_Post $action
462 *
463 * @return string
464 */
465 public static function get_formatted_amount_for_currency( $amount, $action ) {
466 if ( ! isset( $action->post_content['currency'] ) ) {
467 return $amount;
468 }
469
470 $currency = FrmCurrencyHelper::get_currency( $action->post_content['currency'] );
471
472 if ( ! empty( $currency['decimals'] ) ) {
473 return number_format( $amount / 100, 2, '.', '' );
474 }
475
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 );
674 }
675 }
676