ld-payments-functions.php
13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
<?php
/**
* Functions related to payments
*
* @since 4.1.0
*
* @package LearnDash
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
const LEARNDASH_PRICE_TYPE_OPEN = 'open';
const LEARNDASH_PRICE_TYPE_CLOSED = 'closed';
const LEARNDASH_PRICE_TYPE_FREE = 'free';
const LEARNDASH_PRICE_TYPE_PAYNOW = 'paynow';
const LEARNDASH_PRICE_TYPE_SUBSCRIBE = 'subscribe';
require_once LEARNDASH_LMS_PLUGIN_DIR . 'includes/payments/class-learndash-payment-button.php';
/**
* Outputs the LearnDash global currency symbol.
*
* @since 4.1.0
*
* @return void
*/
function learndash_the_currency_symbol(): void {
echo wp_kses_post( learndash_get_currency_symbol() );
}
/**
* Gets the LearnDash global currency symbol.
*
* @since 4.1.0
* @since 4.2.0 Added $currency_code parameter.
*
* @param string $currency_code Optional. The country currency code (@since 4.2.0).
*
* @return string Currency symbol.
*/
function learndash_get_currency_symbol( string $currency_code = '' ): string {
$currency = ! empty( $currency_code ) ? $currency_code : learndash_get_currency_code();
if ( ! empty( $currency ) && class_exists( 'NumberFormatter' ) ) {
$number_format = new NumberFormatter(
get_locale() . '@currency=' . $currency,
NumberFormatter::CURRENCY
);
$currency = $number_format->getSymbol( NumberFormatter::CURRENCY_SYMBOL );
}
/**
* Filter the LearnDash global currency symbol.
*
* @since 4.1.0
*
* @param string $currency The currency symbol.
*/
return apply_filters( 'learndash_currency_symbol', $currency );
}
/**
* Gets the LearnDash global currency code.
*
* @since 4.1.0
*
* @return string Currency code.
*/
function learndash_get_currency_code(): string {
$currency = LearnDash_Settings_Section::get_section_setting(
'LearnDash_Settings_Section_Payments_Defaults',
'currency'
);
$currency = trim( $currency );
/**
* Filter the LearnDash global currency code.
*
* @since 4.1.0
*
* @param string $currency The currency code.
*/
return apply_filters( 'learndash_currency_code', $currency );
}
/**
* Gets the price formatted based on the LearnDash global currency configuration.
*
* @since 4.1.0
* @since 4.2.0 Added $currency_code parameter.
*
* @param mixed $price The price to format.
* @param string $currency_code Optional. The country currency code (@since 4.2.0).
*
* @return string Returns price formatted.
*/
function learndash_get_price_formatted( $price, string $currency_code = '' ): string {
// Empty prices should not be displayed.
if ( '' === $price ) {
return '';
}
$currency_code = ! empty( $currency_code ) ? $currency_code : learndash_get_currency_code();
// Price is shown as is if no currency set.
if ( empty( $currency_code ) ) {
return $price;
}
// Price is shown as is if non-numeric.
if ( ! is_numeric( $price ) ) {
return $price;
}
if ( class_exists( 'NumberFormatter' ) ) {
$number_format = new NumberFormatter(
get_locale() . '@currency=' . $currency_code,
NumberFormatter::CURRENCY
);
return $number_format->format(
floatval( $price )
);
}
$currency_symbol = learndash_get_currency_symbol( $currency_code );
return strlen( $currency_symbol ) > 1
? "$price $currency_symbol" // it's currency code: we should display at the end of the price.
: "$currency_symbol{$price}"; // show the currency symbol at the beginning of the price (en_US style).
}
/**
* Gets the price as float value.
*
* @since 4.1.1
*
* @param string $price The price to convert.
*
* @return float Returns price as float value.
*/
function learndash_get_price_as_float( string $price ): float {
if ( is_numeric( $price ) ) {
return floatval( $price );
}
// trying to convert it into a numeric string.
$dot_position = strpos( $price, '.' );
$comma_position = strpos( $price, ',' );
if ( false !== $dot_position && false !== $comma_position ) {
if ( $dot_position < $comma_position ) {
// dot is before comma. Comma is decimal separator.
$price = str_replace( '.', '', $price ); // remove dot.
$price = str_replace( ',', '.', $price ); // convert comma to dot.
} else {
// comma is before dot. Dot is decimal separator.
$price = str_replace( ',', '', $price ); // remove comma.
}
} elseif ( ! empty( $comma_position ) ) {
$number_before_comma = (int) mb_substr( $price, 0, $comma_position );
$digits_count_after_comma = mb_strlen( mb_substr( $price, $comma_position + 1 ) );
$price = str_replace(
',',
3 === $digits_count_after_comma && 0 !== $number_before_comma ? '' : '.',
$price
);
}
$price = preg_replace( '/[^0-9.]/', '', $price );
return floatval( $price );
}
/**
* Checks currency code is a zero decimal currency.
*
* @since 4.1.0
*
* @param string $currency Stripe currency ISO code.
*
* @return bool
*/
function learndash_is_zero_decimal_currency( string $currency = '' ): bool {
$zero_decimal_currencies = array(
'BIF',
'CLP',
'DJF',
'GNF',
'JPY',
'KMF',
'KRW',
'MGA',
'PYG',
'RWF',
'VND',
'VUV',
'XAF',
'XOF',
'XPF',
);
return in_array( strtoupper( $currency ), $zero_decimal_currencies, true );
}
/**
* Gets the course price.
*
* Return an array of price type, amount and cycle.
*
* @since 3.0.0
* @since 4.1.0 Optional $user_id param added.
* @since 4.5.0 Param $user_id is not nullable.
*
* @global WP_Post $post Global post object.
*
* @param int|WP_Post|null $course Course `WP_Post` object or post ID. Default to global $post.
* @param int $user_id User ID. Default to current user ID.
*
* @return array Course price details.
*/
function learndash_get_course_price( $course = null, int $user_id = 0 ): array {
if ( is_null( $course ) ) {
global $post;
$course = $post;
}
if ( is_numeric( $course ) ) {
$course = get_post( $course );
}
if ( ! is_a( $course, 'WP_Post' ) ) {
return array();
}
// Get the course price.
$meta = get_post_meta( $course->ID, '_sfwd-courses', true );
$pricing = array(
'type' => ! empty( $meta['sfwd-courses_course_price_type'] )
? $meta['sfwd-courses_course_price_type']
: LEARNDASH_DEFAULT_COURSE_PRICE_TYPE,
'price' => ! empty( $meta['sfwd-courses_course_price'] )
? $meta['sfwd-courses_course_price']
: '',
);
// Get the price a user had when was applying a coupon.
if ( 0 === $user_id ) {
$user_id = get_current_user_id();
}
if (
$user_id > 0 &&
learndash_get_price_as_float( strval( $pricing['price'] ) ) > 0 &&
learndash_post_has_attached_coupon( $course->ID, $user_id )
) {
$attached_coupon_data = learndash_get_attached_coupon_data( $course->ID, $user_id );
if ( ! empty( $attached_coupon_data ) ) {
$pricing['price'] = $attached_coupon_data->price;
}
}
// Add subscription data.
if ( LEARNDASH_PRICE_TYPE_SUBSCRIBE === $pricing['type'] ) {
$interval = intval( learndash_get_setting( $course->ID, 'course_price_billing_p3' ) );
$frequency = strval( learndash_get_setting( $course->ID, 'course_price_billing_t3' ) );
$repeats = intval( learndash_get_setting( $course->ID, 'course_no_of_cycles' ) );
$trial_interval = intval( learndash_get_setting( $course->ID, 'course_trial_duration_p1' ) );
$trial_frequency = strval( learndash_get_setting( $course->ID, 'course_trial_duration_t1' ) );
$pricing['interval'] = $interval;
$pricing['frequency'] = learndash_get_grammatical_number_label_for_interval( $interval, $frequency );
$pricing['frequency_raw'] = $frequency;
$pricing['repeats'] = $repeats;
$pricing['repeat_frequency'] = empty( $repeats ) ? '' : learndash_get_grammatical_number_label_for_interval( $repeats, $frequency );
$pricing['trial_price'] = strval( learndash_get_setting( $course->ID, 'course_trial_price' ) );
$pricing['trial_interval'] = $trial_interval;
$pricing['trial_frequency'] = empty( $trial_interval ) ? '' : learndash_get_grammatical_number_label_for_interval( $trial_interval, $trial_frequency );
$pricing['trial_frequency_raw'] = $trial_frequency;
}
/**
* Filters price details for a course.
*
* @since 3.0.0
*
* @param array $pricing Course price details.
*/
return apply_filters( 'learndash_get_course_price', $pricing );
}
/**
* Get group price
*
* Return an array of price type, amount and cycle
*
* @since 3.2.0
* @since 4.1.0 Optional $user_id param added.
* @since 4.5.0 Param $user_id is not nullable.
*
* @param int|WP_Post|null $group Group `WP_Post` object or post ID. Default to global $post.
* @param int $user_id User ID. Default to current user id.
*
* @return array price details.
*/
function learndash_get_group_price( $group = null, int $user_id = 0 ): array {
if ( is_null( $group ) ) {
global $post;
$group = $post;
}
if ( is_numeric( $group ) ) {
$group = get_post( $group );
}
if ( ! is_a( $group, 'WP_Post' ) ) {
return array();
}
// Get the group price.
$meta = get_post_meta( $group->ID, '_groups', true );
$pricing = array(
'type' => ! empty( $meta['groups_group_price_type'] )
? $meta['groups_group_price_type']
: LEARNDASH_DEFAULT_GROUP_PRICE_TYPE,
'price' => ! empty( $meta['groups_group_price'] )
? $meta['groups_group_price']
: '',
);
// Get the price a user had when was applying a coupon.
if ( 0 === $user_id ) {
$user_id = get_current_user_id();
}
if (
$user_id > 0 &&
learndash_get_price_as_float( strval( $pricing['price'] ) ) > 0 &&
learndash_post_has_attached_coupon( $group->ID, $user_id )
) {
$attached_coupon_data = learndash_get_attached_coupon_data( $group->ID, $user_id );
if ( ! empty( $attached_coupon_data ) ) {
$pricing['price'] = $attached_coupon_data->price;
}
}
// Add subscription data.
if ( LEARNDASH_PRICE_TYPE_SUBSCRIBE === $pricing['type'] ) {
$interval = intval( learndash_get_setting( $group->ID, 'group_price_billing_p3' ) );
$frequency = strval( learndash_get_setting( $group->ID, 'group_price_billing_t3' ) );
$repeats = intval( learndash_get_setting( $group->ID, 'post_no_of_cycles' ) );
$trial_interval = intval( learndash_get_setting( $group->ID, 'group_trial_duration_p1' ) );
$trial_frequency = strval( learndash_get_setting( $group->ID, 'group_trial_duration_t1' ) );
$pricing['interval'] = $interval;
$pricing['frequency'] = learndash_get_grammatical_number_label_for_interval( $interval, $frequency );
$pricing['frequency_raw'] = $frequency;
$pricing['repeats'] = $repeats;
$pricing['repeat_frequency'] = empty( $repeats ) ? '' : learndash_get_grammatical_number_label_for_interval( $repeats, $frequency );
$pricing['trial_price'] = strval( learndash_get_setting( $group->ID, 'group_trial_price' ) );
$pricing['trial_interval'] = $trial_interval;
$pricing['trial_frequency'] = empty( $trial_interval ) ? '' : learndash_get_grammatical_number_label_for_interval( $trial_interval, $trial_frequency );
$pricing['trial_frequency_raw'] = $trial_frequency;
}
/**
* Filter Group Price details.
*
* @since 3.2.0
*
* @param array $pricing Group Price Details array.
*/
return apply_filters( 'learndash_get_group_price', $pricing );
}
/**
* Get the singular or plural label for recurring payment intervals
*
* @since 3.6.0
* @since 4.5.0 $interval must be integer.
*
* @param int $interval Number of payment intervals.
* @param string $frequency A symbol for day, week, month or year.
* @param bool $short_version Whether to return the short version of the label.
*
* @return string
*/
function learndash_get_grammatical_number_label_for_interval( int $interval, string $frequency, bool $short_version = false ): string {
switch ( $frequency ) {
case ( 'D' ):
return _n( 'day', 'days', $interval, 'learndash' );
case ( 'W' ):
return $short_version
? _n( 'wk', 'wks', $interval, 'learndash' )
: _n( 'week', 'weeks', $interval, 'learndash' );
case ( 'M' ):
return $short_version
? _n( 'mo', 'mos', $interval, 'learndash' )
: _n( 'month', 'months', $interval, 'learndash' );
case ( 'Y' ):
return $short_version
? _n( 'yr', 'yrs', $interval, 'learndash' )
: _n( 'year', 'years', $interval, 'learndash' );
default:
return '';
}
}
/**
* Generates the LearnDash payment buttons output.
*
* @since 2.1.0
*
* @param int|WP_Post $post Post ID or `WP_Post` object.
*
* @return string The payment buttons HTML output.
*/
function learndash_payment_buttons( $post ): string {
$payment_button_generator = new Learndash_Payment_Button( $post );
/**
* Filters payment button HTML right before output. Fires in the end.
*
* @since 4.5.0
*
* @param string $button Payment button HTML markup. Can contain all types of buttons.
*
* @return string Payment button HTML markup.
*/
$button = (string) apply_filters( 'learndash_payment_button_markup', $payment_button_generator->map() );
/**
* Fires when the payment button is added.
*
* @since 4.5.0
*
* @param string $button Payment button HTML markup. Can contain all types of buttons.
*/
do_action( 'learndash_payment_button_added', $button );
return $button;
}
/**
* Output array of country currency code data.
*
* @since 4.4.0
*
* @return array
*/
function learndash_currency_codes_list(): array {
$currency_codes = array();
$currency_codes_array = array_map( 'str_getcsv', file( LEARNDASH_LMS_PLUGIN_DIR . 'assets/misc/payment-currencies.csv' ) );
// Remove CSV headers from array.
unset( $currency_codes_array[0] );
foreach ( $currency_codes_array as $code ) {
[
$country,
$currency,
$currency_code,
$numeric_code,
$minor_unit,
$withdrawal_date,
] = $code;
$currency_codes[] = array(
'currency_code' => $currency_code,
'option_label' => ucwords( mb_strtolower( $country ) ) . ' (' . learndash_get_currency_symbol( $currency_code ) . ') ',
'country' => $country,
'currency' => mb_strtoupper( $currency ),
);
}
/**
* Filters list of currency codes.
*
* @since 4.4.0
*
* @param array $currency_codes List of currency codes.
*/
return apply_filters( 'learndash_currency_code_list', $currency_codes );
}