| @@ -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/'; |
| @@ -79,9 +74,9 @@ | ||
| 79 | 74 | * |
| 80 | 75 | * @return string |
| 81 | 76 | */ |
| 82 | 77 | public static function get_payment_status( $payment ) { |
| 83 | - if ( $payment->status ) { | |
| 78 | + if ( ! empty( $payment->status ) ) { | |
| 84 | 79 | return $payment->status; |
| 85 | 80 | } |
| 86 | 81 | // PayPal fallback. |
| 87 | 82 | return ! empty( $payment->completed ) ? 'complete' : 'pending'; |
| @@ -158,10 +153,9 @@ | ||
| 158 | 153 | * @return mixed |
| 159 | 154 | */ |
| 160 | 155 | public static function get_action_setting( $option, $atts ) { |
| 161 | 156 | $settings = self::get_action_settings( $atts ); |
| 162 | - $value = $settings[ $option ] ?? ''; | |
| 163 | - return $value; | |
| 157 | + return $settings[ $option ] ?? ''; | |
| 164 | 158 | } |
| 165 | 159 | |
| 166 | 160 | /** |
| 167 | 161 | * @param array $atts |
| @@ -180,13 +174,9 @@ | ||
| 180 | 174 | } |
| 181 | 175 | |
| 182 | 176 | $form_action = FrmTransLiteAction::get_single_action_type( $atts['payment']['action_id'], 'payment' ); |
| 183 | 177 | |
| 184 | - if ( ! $form_action ) { | |
| 185 | - return array(); | |
| 186 | - } | |
| 187 | - | |
| 188 | - return $form_action->post_content; | |
| 178 | + return $form_action ? $form_action->post_content : array(); | |
| 189 | 179 | } |
| 190 | 180 | |
| 191 | 181 | /** |
| 192 | 182 | * Allow entry values, default values, and other shortcodes |
| @@ -197,9 +187,9 @@ | ||
| 197 | 187 | */ |
| 198 | 188 | public static function process_shortcodes( $atts ) { |
| 199 | 189 | $value = $atts['value']; |
| 200 | 190 | |
| 201 | - if ( strpos( $value, '[' ) === false ) { | |
| 191 | + if ( ! str_contains( $value, '[' ) ) { | |
| 202 | 192 | return $value; |
| 203 | 193 | } |
| 204 | 194 | |
| 205 | 195 | if ( is_callable( 'FrmProFieldsHelper::replace_non_standard_formidable_shortcodes' ) ) { |
| @@ -213,10 +203,9 @@ | ||
| 213 | 203 | |
| 214 | 204 | $value = apply_filters( 'frm_content', $value, $atts['form'], $atts['entry'] ); |
| 215 | 205 | } |
| 216 | 206 | |
| 217 | - $value = do_shortcode( $value ); | |
| 218 | - return $value; | |
| 207 | + return do_shortcode( $value ); | |
| 219 | 208 | } |
| 220 | 209 | |
| 221 | 210 | /** |
| 222 | 211 | * @param object $sub |
| @@ -226,14 +215,13 @@ | ||
| 226 | 215 | public static function format_billing_cycle( $sub ) { |
| 227 | 216 | $amount = self::formatted_amount( $sub ); |
| 228 | 217 | $interval = self::get_repeat_label_from_value( $sub->time_interval, $sub->interval_count ); |
| 229 | 218 | |
| 230 | - if ( $sub->interval_count == 1 ) { | |
| 231 | - $amount = $amount . '/' . $interval; | |
| 232 | - } else { | |
| 233 | - $amount = $amount . ' every ' . $sub->interval_count . ' ' . $interval; | |
| 219 | + if ( (int) $sub->interval_count === 1 ) { | |
| 220 | + return $amount . '/' . $interval; | |
| 234 | 221 | } |
| 235 | - return $amount; | |
| 222 | + | |
| 223 | + return $amount . ' every ' . $sub->interval_count . ' ' . $interval; | |
| 236 | 224 | } |
| 237 | 225 | |
| 238 | 226 | /** |
| 239 | 227 | * @return array |
| @@ -272,13 +260,9 @@ | ||
| 272 | 260 | * @return string |
| 273 | 261 | */ |
| 274 | 262 | public static function get_repeat_label_from_value( $value, $number ) { |
| 275 | 263 | $times = self::get_plural_repeat_times( $number ); |
| 276 | - | |
| 277 | - if ( isset( $times[ $value ] ) ) { | |
| 278 | - $value = $times[ $value ]; | |
| 279 | - } | |
| 280 | - return $value; | |
| 264 | + return $times[ $value ] ?? $value; | |
| 281 | 265 | } |
| 282 | 266 | |
| 283 | 267 | /** |
| 284 | 268 | * @param array|float|int|object $payment |
| @@ -348,21 +332,16 @@ | ||
| 348 | 332 | /** |
| 349 | 333 | * @return string |
| 350 | 334 | */ |
| 351 | 335 | public static function get_date_format() { |
| 352 | - $date_format = 'm/d/Y'; | |
| 336 | + if ( ! class_exists( 'FrmProAppHelper' ) ) { | |
| 337 | + return get_option( 'date_format' ); | |
| 338 | + } | |
| 353 | 339 | |
| 354 | - if ( class_exists( 'FrmProAppHelper' ) ) { | |
| 355 | - $frmpro_settings = FrmProAppHelper::get_settings(); | |
| 340 | + $date_format = 'm/d/Y'; | |
| 341 | + $frmpro_settings = FrmProAppHelper::get_settings(); | |
| 356 | 342 | |
| 357 | - if ( $frmpro_settings ) { | |
| 358 | - $date_format = $frmpro_settings->date_format; | |
| 359 | - } | |
| 360 | - } else { | |
| 361 | - $date_format = get_option( 'date_format' ); | |
| 362 | - } | |
| 363 | - | |
| 364 | - return $date_format; | |
| 343 | + return $frmpro_settings ? $frmpro_settings->date_format : $date_format; | |
| 365 | 344 | } |
| 366 | 345 | |
| 367 | 346 | /** |
| 368 | 347 | * @param string $date |
| @@ -370,9 +349,9 @@ | ||
| 370 | 349 | * |
| 371 | 350 | * @return string |
| 372 | 351 | */ |
| 373 | 352 | public static function format_the_date( $date, $format = '' ) { |
| 374 | - if ( empty( $format ) ) { | |
| 353 | + if ( ! $format ) { | |
| 375 | 354 | $format = self::get_date_format(); |
| 376 | 355 | } |
| 377 | 356 | return date_i18n( $format, strtotime( $date ) ); |
| 378 | 357 | } |
| @@ -382,14 +361,22 @@ | ||
| 382 | 361 | * |
| 383 | 362 | * @return int |
| 384 | 363 | */ |
| 385 | 364 | public static function get_user_id_for_current_payment() { |
| 386 | - $user_id = 0; | |
| 365 | + $user_id = is_user_logged_in() ? get_current_user_id() : 0; | |
| 387 | 366 | |
| 388 | - if ( is_user_logged_in() ) { | |
| 389 | - $user_id = get_current_user_id(); | |
| 390 | - } | |
| 391 | - return $user_id; | |
| 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 ); | |
| 392 | 379 | } |
| 393 | 380 | |
| 394 | 381 | /** |
| 395 | 382 | * @param int $user_id |
| @@ -396,18 +383,17 @@ | ||
| 396 | 383 | * |
| 397 | 384 | * @return string |
| 398 | 385 | */ |
| 399 | 386 | public static function get_user_link( $user_id ) { |
| 400 | - $user_link = esc_html__( 'Guest', 'formidable' ); | |
| 401 | - | |
| 402 | 387 | if ( $user_id ) { |
| 403 | 388 | $user = get_userdata( $user_id ); |
| 404 | 389 | |
| 405 | 390 | if ( $user ) { |
| 406 | - $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>'; | |
| 407 | 392 | } |
| 408 | 393 | } |
| 409 | - return $user_link; | |
| 394 | + | |
| 395 | + return esc_html__( 'Guest', 'formidable' ); | |
| 410 | 396 | } |
| 411 | 397 | |
| 412 | 398 | /** |
| 413 | 399 | * @param mixed $value |
| @@ -415,17 +401,22 @@ | ||
| 415 | 401 | * |
| 416 | 402 | * @return void |
| 417 | 403 | */ |
| 418 | 404 | public static function show_in_table( $value, $label ) { |
| 419 | - if ( ! empty( $value ) ) { ?> | |
| 420 | - <tr> | |
| 421 | - <th scope="row"><?php echo esc_html( $label ); ?>:</th> | |
| 422 | - <td> | |
| 423 | - <?php echo esc_html( $value ); ?> | |
| 424 | - </td> | |
| 425 | - </tr> | |
| 426 | - <?php | |
| 405 | + if ( ! $value ) { | |
| 406 | + return; | |
| 427 | 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 | |
| 428 | 419 | } |
| 429 | 420 | |
| 430 | 421 | /** |
| 431 | 422 | * Echo a link that includes a data-deleteconfirm attribute. |
| @@ -478,9 +469,9 @@ | ||
| 478 | 469 | |
| 479 | 470 | $currency = FrmCurrencyHelper::get_currency( $action->post_content['currency'] ); |
| 480 | 471 | |
| 481 | 472 | if ( ! empty( $currency['decimals'] ) ) { |
| 482 | - $amount = number_format( $amount / 100, 2, '.', '' ); | |
| 473 | + return number_format( $amount / 100, 2, '.', '' ); | |
| 483 | 474 | } |
| 484 | 475 | |
| 485 | 476 | return $amount; |
| 486 | 477 | } |
| @@ -485,36 +476,8 @@ | ||
| 485 | 476 | return $amount; |
| 486 | 477 | } |
| 487 | 478 | |
| 488 | 479 | /** |
| 489 | - * @return bool | |
| 490 | - */ | |
| 491 | - public static function should_fallback_to_paypal() { | |
| 492 | - if ( isset( self::$should_fallback_to_paypal ) ) { | |
| 493 | - return self::$should_fallback_to_paypal; | |
| 494 | - } | |
| 495 | - | |
| 496 | - if ( ! class_exists( 'FrmPaymentsController' ) || ! isset( FrmPaymentsController::$db_opt_name ) ) { | |
| 497 | - self::$should_fallback_to_paypal = false; | |
| 498 | - return false; | |
| 499 | - } | |
| 500 | - | |
| 501 | - $db = new FrmTransLiteDb(); | |
| 502 | - $option = get_option( $db->db_opt_name ); | |
| 503 | - | |
| 504 | - if ( false !== $option ) { | |
| 505 | - // Don't fallback to PayPal if Stripe migrations have run. | |
| 506 | - self::$should_fallback_to_paypal = false; | |
| 507 | - return false; | |
| 508 | - } | |
| 509 | - | |
| 510 | - $option = get_option( FrmPaymentsController::$db_opt_name ); | |
| 511 | - self::$should_fallback_to_paypal = false !== $option; | |
| 512 | - | |
| 513 | - return self::$should_fallback_to_paypal; | |
| 514 | - } | |
| 515 | - | |
| 516 | - /** | |
| 517 | 480 | * Get a human readable translated 'Test' or 'Live' string if the column value is defined. |
| 518 | 481 | * Old payments will just output an empty string. |
| 519 | 482 | * |
| 520 | 483 | * @since 6.6 |
| @@ -554,10 +517,9 @@ | ||
| 554 | 517 | /** |
| 555 | 518 | * @return array |
| 556 | 519 | */ |
| 557 | 520 | public static function get_gateways() { |
| 558 | - $gateways = apply_filters( 'frm_payment_gateways', array() ); | |
| 559 | - return $gateways; | |
| 521 | + return apply_filters( 'frm_payment_gateways', array() ); | |
| 560 | 522 | } |
| 561 | 523 | |
| 562 | 524 | /** |
| 563 | 525 | * @param array|string $gateway |
| @@ -573,10 +535,11 @@ | ||
| 573 | 535 | $gateway = reset( $gateway ); |
| 574 | 536 | } |
| 575 | 537 | |
| 576 | 538 | if ( isset( $gateways[ $gateway ] ) ) { |
| 577 | - $value = $gateways[ $gateway ][ $setting ]; | |
| 539 | + return $gateways[ $gateway ][ $setting ]; | |
| 578 | 540 | } |
| 541 | + | |
| 579 | 542 | return $value; |
| 580 | 543 | } |
| 581 | 544 | |
| 582 | 545 | /** |
| @@ -604,8 +567,9 @@ | ||
| 604 | 567 | $selected = ''; |
| 605 | 568 | } |
| 606 | 569 | |
| 607 | 570 | $currencies = FrmCurrencyHelper::get_currencies(); |
| 571 | + // phpcs:disable Generic.WhiteSpace.ScopeIndent | |
| 608 | 572 | ?> |
| 609 | 573 | <select <?php FrmAppHelper::array_to_html_params( $select_attrs, true ); ?>> |
| 610 | 574 | <?php |
| 611 | 575 | if ( in_array( 'square', $gateways, true ) ) { |
| @@ -631,6 +595,81 @@ | ||
| 631 | 595 | } |
| 632 | 596 | ?> |
| 633 | 597 | </select> |
| 634 | 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 ); | |
| 635 | 674 | } |
| 636 | 675 | } |