PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.33
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.33
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 6.33, at stripe/helpers/FrmTransLiteAppHelper.php

663 lines 16.0 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 return is_user_logged_in() ? get_current_user_id() : 0;
366 }
367
368 /**
369 * @param int $user_id
370 *
371 * @return string
372 */
373 public static function get_user_link( $user_id ) {
374 if ( $user_id ) {
375 $user = get_userdata( $user_id );
376
377 if ( $user ) {
378 return '<a href="' . esc_url( admin_url( 'user-edit.php?user_id=' . $user_id ) ) . '">' . esc_html( $user->display_name ) . '</a>';
379 }
380 }
381
382 return esc_html__( 'Guest', 'formidable' );
383 }
384
385 /**
386 * @param mixed $value
387 * @param string $label
388 *
389 * @return void
390 */
391 public static function show_in_table( $value, $label ) {
392 if ( ! $value ) {
393 return;
394 }
395
396 // phpcs:disable Generic.WhiteSpace.ScopeIndent
397 ?>
398 <tr>
399 <th scope="row"><?php echo esc_html( $label ); ?>:</th>
400 <td>
401 <?php echo esc_html( $value ); ?>
402 </td>
403 </tr>
404 <?php
405 // phpcs:enable Generic.WhiteSpace.ScopeIndent
406 }
407
408 /**
409 * Echo a link that includes a data-deleteconfirm attribute.
410 * This includes refund links and links to cancel a subscription.
411 *
412 * @since 6.5
413 *
414 * @param string $link
415 *
416 * @return void
417 */
418 public static function echo_confirmation_link( $link ) {
419 $filter = self::class . '::allow_deleteconfirm_data_attribute';
420 add_filter( 'frm_striphtml_allowed_tags', $filter );
421 FrmAppHelper::kses_echo( $link, array( 'a' ) );
422 remove_filter( 'frm_striphtml_allowed_tags', $filter );
423 }
424
425 /**
426 * Allow the data-deleteconfirm attribute for confirmation links.
427 * The attribute is used for the confirmation message.
428 *
429 * @since 6.5
430 *
431 * @param array $allowed
432 *
433 * @return array
434 */
435 public static function allow_deleteconfirm_data_attribute( $allowed ) {
436 $allowed['a']['data-deleteconfirm'] = true;
437 $allowed['a']['data-frmverify'] = true;
438 $allowed['a']['data-frmverify-btn'] = true;
439 return $allowed;
440 }
441
442 /**
443 * Formats non zero-decimal currencies.
444 *
445 * @since 6.5
446 *
447 * @param int|string $amount
448 * @param WP_Post $action
449 *
450 * @return string
451 */
452 public static function get_formatted_amount_for_currency( $amount, $action ) {
453 if ( ! isset( $action->post_content['currency'] ) ) {
454 return $amount;
455 }
456
457 $currency = FrmCurrencyHelper::get_currency( $action->post_content['currency'] );
458
459 if ( ! empty( $currency['decimals'] ) ) {
460 return number_format( $amount / 100, 2, '.', '' );
461 }
462
463 return $amount;
464 }
465
466 /**
467 * Get a human readable translated 'Test' or 'Live' string if the column value is defined.
468 * Old payments will just output an empty string.
469 *
470 * @since 6.6
471 *
472 * @param stdClass $payment
473 *
474 * @return string
475 */
476 public static function get_test_mode_display_string( $payment ) {
477 if ( ! isset( $payment->test ) ) {
478 return '';
479 }
480 return $payment->test ? __( 'Test', 'formidable' ) : __( 'Live', 'formidable' );
481 }
482
483 /**
484 * Returns the count of completed payments.
485 *
486 * @since 6.11
487 *
488 * @param array $payments
489 *
490 * @return int
491 */
492 public static function count_completed_payments( $payments ) {
493 $count = 0;
494
495 foreach ( $payments as $payment ) {
496 if ( $payment->status === 'complete' ) {
497 $count++;
498 }
499 }
500
501 return $count;
502 }
503
504 /**
505 * @return array
506 */
507 public static function get_gateways() {
508 return apply_filters( 'frm_payment_gateways', array() );
509 }
510
511 /**
512 * @param array|string $gateway
513 * @param string $setting
514 *
515 * @return mixed
516 */
517 public static function get_setting_for_gateway( $gateway, $setting ) {
518 $gateways = self::get_gateways();
519 $value = '';
520
521 if ( is_array( $gateway ) ) {
522 $gateway = reset( $gateway );
523 }
524
525 if ( isset( $gateways[ $gateway ] ) ) {
526 return $gateways[ $gateway ][ $setting ];
527 }
528
529 return $value;
530 }
531
532 /**
533 * Show the currency dropdown for a Payment action.
534 * When Square is selected, this dropdown is disabled and will always use "Use Square Merchant Currency".
535 *
536 * @since 6.22
537 *
538 * @param string $id
539 * @param string $name
540 * @param array $action_settings
541 *
542 * @return void
543 */
544 public static function show_currency_dropdown( $id, $name, $action_settings ) {
545 $selected = $action_settings['currency'];
546 $gateways = (array) $action_settings['gateway'];
547 $select_attrs = array(
548 'id' => $id,
549 'name' => $name,
550 );
551
552 if ( in_array( 'square', $gateways, true ) ) {
553 $select_attrs['disabled'] = 'disabled';
554 $selected = '';
555 }
556
557 $currencies = FrmCurrencyHelper::get_currencies();
558 // phpcs:disable Generic.WhiteSpace.ScopeIndent
559 ?>
560 <select <?php FrmAppHelper::array_to_html_params( $select_attrs, true ); ?>>
561 <?php
562 if ( in_array( 'square', $gateways, true ) ) {
563 $option_params = array(
564 'class' => 'square-currency',
565 'selected' => 'selected',
566 'value' => 'square',
567 );
568 ?>
569 <option <?php FrmAppHelper::array_to_html_params( $option_params, true ); ?>><?php esc_html_e( 'Use Square Merchant Currency', 'formidable' ); ?></option>
570 <?php
571 }
572
573 foreach ( $currencies as $code => $currency ) {
574 FrmHtmlHelper::echo_dropdown_option(
575 $currency['name'] . ' (' . strtoupper( $code ) . ')',
576 $selected === strtolower( $code ),
577 array(
578 'value' => strtolower( $code ),
579 )
580 );
581 unset( $currency, $code );
582 }
583 ?>
584 </select>
585 <?php
586 // phpcs:enable Generic.WhiteSpace.ScopeIndent
587 }
588
589 /**
590 * @since 6.27
591 *
592 * @return bool
593 */
594 public static function payments_submodule_or_paypal_is_active() {
595 return class_exists( 'FrmTransAppController' ) || class_exists( 'FrmPaymentsController' );
596 }
597
598 /**
599 * @deprecated 6.27
600 *
601 * @return bool
602 */
603 public static function should_fallback_to_paypal() {
604 _deprecated_function( __METHOD__, '6.27' );
605 return false;
606 }
607
608 /**
609 * Render the gateway icon buttons for the payment action settings.
610 *
611 * @param array $gateways
612 * @param WP_Post $form_action
613 * @param FrmFormAction $action_control
614 *
615 * @return void
616 */
617 public static function show_gateway_buttons( $gateways, $form_action, $action_control ) {
618 $gateway_order = array( 'stripe', 'square', 'paypal' );
619 $gateways = self::sort_gateways( $gateways, $gateway_order );
620
621 include self::plugin_path() . '/views/action-settings/gateway-buttons.php';
622 }
623
624 /**
625 * Sort gateways by a predefined order.
626 * Unlisted gateways are appended at the end.
627 *
628 * @param array $gateways
629 * @param array $order Gateway keys in desired order.
630 *
631 * @return array
632 */
633 private static function sort_gateways( $gateways, $order ) {
634 $sorted = array();
635
636 foreach ( $order as $key ) {
637 if ( isset( $gateways[ $key ] ) ) {
638 $sorted[ $key ] = $gateways[ $key ];
639 }
640 }
641
642 return $sorted + $gateways;
643 }
644
645 /**
646 * @since 6.32.1
647 *
648 * @param string $gateway 'stripe', 'square', or 'paypal'.
649 * @param string $mode 'test' or 'live'.
650 *
651 * @return void
652 */
653 public static function trigger_gateway_disconnected_hook( $gateway, $mode ) {
654 /**
655 * @since 6.32.1
656 *
657 * @param string $gateway 'stripe', 'square', or 'paypal'.
658 * @param string $mode 'test' or 'live'.
659 */
660 do_action( 'frm_disconnected_gateway', $gateway, $mode );
661 }
662 }
663