| 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 |
if ( false !== $option ) { |
| 43 |
return true; |
| 44 |
} |
| 45 |
|
| 46 |
if ( class_exists( 'FrmPaymentsController' ) && isset( FrmPaymentsController::$db_opt_name ) ) { |
| 47 |
$option = get_option( FrmPaymentsController::$db_opt_name ); |
| 48 |
if ( false !== $option ) { |
| 49 |
return true; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Get a payment status label. |
| 58 |
* |
| 59 |
* @param string $status The lowercase payment status value. |
| 60 |
* @return string |
| 61 |
*/ |
| 62 |
public static function show_status( $status ) { |
| 63 |
$statuses = array_merge( self::get_payment_statuses(), self::get_subscription_statuses() ); |
| 64 |
return isset( $statuses[ $status ] ) ? $statuses[ $status ] : $status; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Get Payment status from a payment with support for PayPal backward compatibility. |
| 69 |
* |
| 70 |
* @param stdClass $payment |
| 71 |
* @return string |
| 72 |
*/ |
| 73 |
public static function get_payment_status( $payment ) { |
| 74 |
if ( $payment->status ) { |
| 75 |
return $payment->status; |
| 76 |
} |
| 77 |
// PayPal fallback. |
| 78 |
return ! empty( $payment->completed ) ? 'complete' : 'pending'; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* @return string[] |
| 83 |
*/ |
| 84 |
public static function get_payment_statuses() { |
| 85 |
return array( |
| 86 |
'authorized' => __( 'Authorized', 'formidable' ), |
| 87 |
'pending' => __( 'Pending', 'formidable' ), |
| 88 |
'complete' => __( 'Completed', 'formidable' ), |
| 89 |
'failed' => __( 'Failed', 'formidable' ), |
| 90 |
'refunded' => __( 'Refunded', 'formidable' ), |
| 91 |
'canceled' => __( 'Canceled', 'formidable' ), |
| 92 |
'processing' => __( 'Processing', 'formidable' ), |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* @return string[] |
| 98 |
*/ |
| 99 |
public static function get_subscription_statuses() { |
| 100 |
return array( |
| 101 |
'pending' => __( 'Pending', 'formidable' ), |
| 102 |
'active' => __( 'Active', 'formidable' ), |
| 103 |
'future_cancel' => __( 'Canceled', 'formidable' ), |
| 104 |
'canceled' => __( 'Canceled', 'formidable' ), |
| 105 |
'void' => __( 'Void', 'formidable' ), |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Add a note to payment data that will get saved to the payment meta. |
| 111 |
* This is called when processing events in the Stripe add on. |
| 112 |
* |
| 113 |
* @param array $payment_values |
| 114 |
* @param string $message |
| 115 |
* @return void |
| 116 |
*/ |
| 117 |
public static function add_note_to_payment( &$payment_values, $message = '' ) { |
| 118 |
if ( ! $message ) { |
| 119 |
$message = sprintf( |
| 120 |
// translators: %s: Payment status. |
| 121 |
__( 'Payment %s', 'formidable' ), |
| 122 |
$payment_values['status'] |
| 123 |
); |
| 124 |
} |
| 125 |
$payment_values['meta_value'] = isset( $payment_values['meta_value'] ) ? $payment_values['meta_value'] : array(); |
| 126 |
$payment_values['meta_value'] = self::add_meta_to_payment( $payment_values['meta_value'], $message ); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* @param string $note |
| 131 |
* |
| 132 |
* @return array |
| 133 |
*/ |
| 134 |
public static function add_meta_to_payment( $meta_value, $note ) { |
| 135 |
$meta_value = (array) maybe_unserialize( $meta_value ); |
| 136 |
$meta_value[] = array( |
| 137 |
'message' => $note, |
| 138 |
'date' => gmdate( 'Y-m-d H:i:s' ), |
| 139 |
); |
| 140 |
return $meta_value; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* @param string $option |
| 145 |
* @param array $atts |
| 146 |
*/ |
| 147 |
public static function get_action_setting( $option, $atts ) { |
| 148 |
$settings = self::get_action_settings( $atts ); |
| 149 |
$value = isset( $settings[ $option ] ) ? $settings[ $option ] : ''; |
| 150 |
return $value; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* @param array $atts |
| 155 |
*/ |
| 156 |
public static function get_action_settings( $atts ) { |
| 157 |
if ( ! isset( $atts['payment'] ) ) { |
| 158 |
return array(); |
| 159 |
} |
| 160 |
|
| 161 |
$atts['payment'] = (array) $atts['payment']; |
| 162 |
if ( empty( $atts['payment']['action_id'] ) ) { |
| 163 |
return array(); |
| 164 |
} |
| 165 |
|
| 166 |
$form_action = FrmTransLiteAction::get_single_action_type( $atts['payment']['action_id'], 'payment' ); |
| 167 |
if ( ! $form_action ) { |
| 168 |
return array(); |
| 169 |
} |
| 170 |
|
| 171 |
return $form_action->post_content; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Allow entry values, default values, and other shortcodes |
| 176 |
* |
| 177 |
* @param array $atts - Includes value (required), form, entry |
| 178 |
* @return string|int |
| 179 |
*/ |
| 180 |
public static function process_shortcodes( $atts ) { |
| 181 |
$value = $atts['value']; |
| 182 |
if ( strpos( $value, '[' ) === false ) { |
| 183 |
return $value; |
| 184 |
} |
| 185 |
|
| 186 |
if ( is_callable( 'FrmProFieldsHelper::replace_non_standard_formidable_shortcodes' ) ) { |
| 187 |
FrmProFieldsHelper::replace_non_standard_formidable_shortcodes( array(), $value ); |
| 188 |
} |
| 189 |
|
| 190 |
if ( ! empty( $atts['entry'] ) ) { |
| 191 |
if ( ! isset( $atts['form'] ) ) { |
| 192 |
$atts['form'] = FrmForm::getOne( $atts['entry']->form_id ); |
| 193 |
} |
| 194 |
$value = apply_filters( 'frm_content', $value, $atts['form'], $atts['entry'] ); |
| 195 |
} |
| 196 |
|
| 197 |
$value = do_shortcode( $value ); |
| 198 |
return $value; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* @param object $sub |
| 203 |
* @return string |
| 204 |
*/ |
| 205 |
public static function format_billing_cycle( $sub ) { |
| 206 |
$amount = self::formatted_amount( $sub ); |
| 207 |
$interval = self::get_repeat_label_from_value( $sub->time_interval, $sub->interval_count ); |
| 208 |
if ( $sub->interval_count == 1 ) { |
| 209 |
$amount = $amount . '/' . $interval; |
| 210 |
} else { |
| 211 |
$amount = $amount . ' every ' . $sub->interval_count . ' ' . $interval; |
| 212 |
} |
| 213 |
return $amount; |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* @return array |
| 218 |
*/ |
| 219 |
public static function get_repeat_times() { |
| 220 |
return array( |
| 221 |
'day' => __( 'day(s)', 'formidable' ), |
| 222 |
'week' => __( 'week(s)', 'formidable' ), |
| 223 |
'month' => __( 'month(s)', 'formidable' ), |
| 224 |
'year' => __( 'year(s)', 'formidable' ), |
| 225 |
); |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* @since 6.5, introduced in v1.16 of the Payments submodule. |
| 230 |
* |
| 231 |
* @param int $number |
| 232 |
* @return array |
| 233 |
*/ |
| 234 |
private static function get_plural_repeat_times( $number ) { |
| 235 |
return array( |
| 236 |
'day' => _n( 'day', 'days', $number, 'formidable' ), |
| 237 |
'week' => _n( 'week', 'weeks', $number, 'formidable' ), |
| 238 |
'month' => _n( 'month', 'months', $number, 'formidable' ), |
| 239 |
'year' => _n( 'year', 'years', $number, 'formidable' ), |
| 240 |
); |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* @since 6.5, introduced in v1.16 of the Payments submodule. |
| 245 |
* |
| 246 |
* @param string $value |
| 247 |
* @param int $number |
| 248 |
* @return string |
| 249 |
*/ |
| 250 |
public static function get_repeat_label_from_value( $value, $number ) { |
| 251 |
$times = self::get_plural_repeat_times( $number ); |
| 252 |
if ( isset( $times[ $value ] ) ) { |
| 253 |
$value = $times[ $value ]; |
| 254 |
} |
| 255 |
return $value; |
| 256 |
} |
| 257 |
|
| 258 |
public static function formatted_amount( $payment ) { |
| 259 |
$currency = ''; |
| 260 |
$amount = $payment; |
| 261 |
|
| 262 |
if ( is_object( $payment ) || is_array( $payment ) ) { |
| 263 |
$payment = (array) $payment; |
| 264 |
$amount = $payment['amount']; |
| 265 |
$currency = self::get_action_setting( 'currency', array( 'payment' => $payment ) ); |
| 266 |
} |
| 267 |
|
| 268 |
if ( ! $currency ) { |
| 269 |
$currency = 'usd'; |
| 270 |
} |
| 271 |
|
| 272 |
$currency = FrmCurrencyHelper::get_currency( $currency ); |
| 273 |
|
| 274 |
self::format_amount_for_currency( $currency, $amount ); |
| 275 |
|
| 276 |
return $amount; |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* @param array $currency |
| 281 |
* @param float $amount |
| 282 |
* @return void |
| 283 |
*/ |
| 284 |
public static function format_amount_for_currency( $currency, &$amount ) { |
| 285 |
$amount = number_format( $amount, $currency['decimals'], $currency['decimal_separator'], $currency['thousand_separator'] ); |
| 286 |
$left_symbol = $currency['symbol_left'] . $currency['symbol_padding']; |
| 287 |
$right_symbol = $currency['symbol_padding'] . $currency['symbol_right']; |
| 288 |
$amount = $left_symbol . $amount . $right_symbol; |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* @return string |
| 293 |
*/ |
| 294 |
public static function get_date_format() { |
| 295 |
$date_format = 'm/d/Y'; |
| 296 |
if ( class_exists( 'FrmProAppHelper' ) ) { |
| 297 |
$frmpro_settings = FrmProAppHelper::get_settings(); |
| 298 |
if ( $frmpro_settings ) { |
| 299 |
$date_format = $frmpro_settings->date_format; |
| 300 |
} |
| 301 |
} else { |
| 302 |
$date_format = get_option( 'date_format' ); |
| 303 |
} |
| 304 |
|
| 305 |
return $date_format; |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* @param string $date |
| 310 |
* @param string $format |
| 311 |
* @return string |
| 312 |
*/ |
| 313 |
public static function format_the_date( $date, $format = '' ) { |
| 314 |
if ( empty( $format ) ) { |
| 315 |
$format = self::get_date_format(); |
| 316 |
} |
| 317 |
return date_i18n( $format, strtotime( $date ) ); |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Set a user id for current payment if a user is logged in. |
| 322 |
* |
| 323 |
* @return int |
| 324 |
*/ |
| 325 |
public static function get_user_id_for_current_payment() { |
| 326 |
$user_id = 0; |
| 327 |
if ( is_user_logged_in() ) { |
| 328 |
$user_id = get_current_user_id(); |
| 329 |
} |
| 330 |
return $user_id; |
| 331 |
} |
| 332 |
|
| 333 |
/** |
| 334 |
* @param int $user_id |
| 335 |
* @return string |
| 336 |
*/ |
| 337 |
public static function get_user_link( $user_id ) { |
| 338 |
$user_link = esc_html__( 'Guest', 'formidable' ); |
| 339 |
if ( $user_id ) { |
| 340 |
$user = get_userdata( $user_id ); |
| 341 |
if ( $user ) { |
| 342 |
$user_link = '<a href="' . esc_url( admin_url( 'user-edit.php?user_id=' . $user_id ) ) . '">' . esc_html( $user->display_name ) . '</a>'; |
| 343 |
} |
| 344 |
} |
| 345 |
return $user_link; |
| 346 |
} |
| 347 |
|
| 348 |
/** |
| 349 |
* @param mixed $value |
| 350 |
* @param string $label |
| 351 |
* @return void |
| 352 |
*/ |
| 353 |
public static function show_in_table( $value, $label ) { |
| 354 |
if ( ! empty( $value ) ) { ?> |
| 355 |
<tr> |
| 356 |
<th scope="row"><?php echo esc_html( $label ); ?>:</th> |
| 357 |
<td> |
| 358 |
<?php echo esc_html( $value ); ?> |
| 359 |
</td> |
| 360 |
</tr> |
| 361 |
<?php |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
/** |
| 366 |
* Echo a link that includes a data-deleteconfirm attribute. |
| 367 |
* This includes refund links and links to cancel a subscription. |
| 368 |
* |
| 369 |
* @since 6.5 |
| 370 |
* |
| 371 |
* @param string $link |
| 372 |
* @return void |
| 373 |
*/ |
| 374 |
public static function echo_confirmation_link( $link ) { |
| 375 |
$filter = __CLASS__ . '::allow_deleteconfirm_data_attribute'; |
| 376 |
add_filter( 'frm_striphtml_allowed_tags', $filter ); |
| 377 |
echo FrmAppHelper::kses( $link, array( 'a' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 378 |
remove_filter( 'frm_striphtml_allowed_tags', $filter ); |
| 379 |
} |
| 380 |
|
| 381 |
/** |
| 382 |
* Allow the data-deleteconfirm attribute for confirmation links. |
| 383 |
* The attribute is used for the confirmation message. |
| 384 |
* |
| 385 |
* @since 6.5 |
| 386 |
* |
| 387 |
* @param array $allowed |
| 388 |
* @return array |
| 389 |
*/ |
| 390 |
public static function allow_deleteconfirm_data_attribute( $allowed ) { |
| 391 |
$allowed['a']['data-deleteconfirm'] = true; |
| 392 |
$allowed['a']['data-frmverify'] = true; |
| 393 |
$allowed['a']['data-frmverify-btn'] = true; |
| 394 |
return $allowed; |
| 395 |
} |
| 396 |
|
| 397 |
/** |
| 398 |
* Formats non zero-decimal currencies. |
| 399 |
* |
| 400 |
* @since 6.5 |
| 401 |
* |
| 402 |
* @param string|int $amount |
| 403 |
* @param WP_Post $action |
| 404 |
* |
| 405 |
* @return string |
| 406 |
*/ |
| 407 |
public static function get_formatted_amount_for_currency( $amount, $action ) { |
| 408 |
if ( ! isset( $action->post_content['currency'] ) ) { |
| 409 |
return $amount; |
| 410 |
} |
| 411 |
|
| 412 |
$currency = FrmCurrencyHelper::get_currency( $action->post_content['currency'] ); |
| 413 |
if ( ! empty( $currency['decimals'] ) ) { |
| 414 |
$amount = number_format( ( $amount / 100 ), 2, '.', '' ); |
| 415 |
} |
| 416 |
|
| 417 |
return $amount; |
| 418 |
} |
| 419 |
} |
| 420 |
|