ld-payments-functions.php 13.1 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
<?php
/**
 * Functions related to payments
 *
 * @since 4.1.0
 *
 * @package LearnDash
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

const LEARNDASH_PRICE_TYPE_PAYNOW    = 'paynow';
const LEARNDASH_PRICE_TYPE_SUBSCRIBE = 'subscribe';

/**
 * Outputs the LearnDash global currency symbol.
 *
 * @since 4.1.0
 */
function learndash_the_currency_symbol() {
	echo wp_kses_post( learndash_get_currency_symbol() );
}

/**
 * Gets the LearnDash global currency symbol.
 *
 * @since 4.1.0
 * @since 4.2.0 Add $currency_code parameter
 *
 * @param string $currency_code Optional. The country currency code (@since 4.2.0).
 *
 * @return string Returns currency symbol.
 */
function learndash_get_currency_symbol( $currency_code = '' ) {
	if ( ! empty( $currency_code ) ) {
		$currency = $currency_code;
	} else {
		$currency = learndash_get_currency_code();
	}

	if ( ! empty( $currency ) && class_exists( 'NumberFormatter' ) ) {
		$locale        = get_locale();
		$number_format = new NumberFormatter( $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 Returns currency code.
 */
function learndash_get_currency_code() {
	$currency = LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Section_Payments_Defaults', 'currency' );

	/**
	 * Filter the LearnDash global currency code.
	 *
	 * @since 4.1.0
	 *
	 * @param string $currency The currency code.
	 */
	return apply_filters( 'learndash_currency_code', trim( $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, $currency_code = '' ) {
	if ( ! empty( $currency_code ) ) {
		$currency_code = $currency_code;
	} else {
		$currency_code = learndash_get_currency_code();
	}

	if ( '' === $price ) {
		return ''; // empty prices should not be displayed.
	}

	if ( empty( $currency_code ) ) {
		return $price; // no currency set.
	}

	if ( ! is_numeric( $price ) ) {
		return $price; // non-numeric price is shown as is.
	}

	if ( class_exists( 'NumberFormatter' ) ) {
		$locale          = get_locale();
		$number_format   = new NumberFormatter( $locale . '@currency=' . $currency_code, NumberFormatter::CURRENCY );
		$price_formatted = $number_format->format( floatval( $price ) );
	} else {
		$currency_symbol = learndash_get_currency_symbol( $currency_code );
		if ( strlen( $currency_symbol ) > 1 ) {
			// it's currency code: we should display at the end of the price.
			$price_formatted = "$price $currency_symbol";
		} else {
			// show the currency symbol at the beginning of the price (en_US style).
			$price_formatted = "$currency_symbol{$price}";
		}
	}

	return $price_formatted;
}


/**
 * 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 );
}

/**
 * Maps the payment button label.
 *
 * @since 4.2.0
 *
 * @param WP_Post|int|null $post Post or Post ID.
 *
 * @return string
 */
function learndash_get_payment_button_label( $post ): string {
	if ( empty( $post ) ) {
		return '';
	}

	if ( is_int( $post ) ) {
		$post = get_post( $post );

		if ( is_null( $post ) ) {
			return '';
		}
	}

	$button_label = '';

	if ( class_exists( 'LearnDash_Custom_Label' ) ) {
		if ( learndash_is_course_post( $post ) ) {
			$button_label = LearnDash_Custom_Label::get_label( 'button_take_this_course' );
		} elseif ( learndash_is_group_post( $post ) ) {
			$button_label = LearnDash_Custom_Label::get_label( 'button_take_this_group' );
		}
	} else {
		if ( learndash_is_course_post( $post ) ) {
			$button_label = __( 'Take This Course', 'learndash' );
		} elseif ( learndash_is_group_post( $post ) ) {
			$button_label = __( 'Enroll in Group', 'learndash' );
		}
	}

	return $button_label;
}

/**
 * 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.
 *
 * @param int|object|null $course  Course `WP_Post` object or post ID. Default to global $post.
 * @param int|null        $user_id User ID. Default to current user id.
 *
 * @return array Course price details.
 * @global WP_Post $post Global post object.
 */
function learndash_get_course_price( $course = null, ?int $user_id = null ): array {
	if ( 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 );

	$course_price = 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 ( is_null( $user_id ) || 0 === $user_id ) {
		$user_id = get_current_user_id();
	}

	if ( $user_id > 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 ) ) {
			$course_price['price'] = $attached_coupon_data['price'];
		}
	}

	// Add subscription data.
	if ( 'subscribe' === $course_price['type'] ) {
		$interval        = learndash_get_setting( $course->ID, 'course_price_billing_p3' );
		$frequency       = learndash_get_setting( $course->ID, 'course_price_billing_t3' );
		$repeats         = learndash_get_setting( $course->ID, 'course_no_of_cycles' );
		$trial_interval  = learndash_get_setting( $course->ID, 'course_trial_duration_p1' );
		$trial_frequency = learndash_get_setting( $course->ID, 'course_trial_duration_t1' );

		$course_price['interval']  = $interval;
		$course_price['frequency'] = learndash_get_grammatical_number_label_for_interval( $interval, $frequency );

		if ( ! empty( $repeats ) ) {
			$course_price['repeats']          = $repeats;
			$course_price['repeat_frequency'] = learndash_get_grammatical_number_label_for_interval( $repeats, $frequency );
		}

		$course_price['trial_price'] = ! empty( learndash_get_setting( $course->ID, 'course_trial_price' ) ) ? learndash_get_setting( $course->ID, 'course_trial_price' ) : '';

		if ( ! empty( $trial_interval ) ) {
			$course_price['trial_interval']  = $trial_interval;
			$course_price['trial_frequency'] = learndash_get_grammatical_number_label_for_interval( $trial_interval, $trial_frequency );
		}
	}

	/**
	 * Filters price details for a course.
	 *
	 * @since 3.0.0
	 *
	 * @param array $course_price Course price details.
	 */
	return apply_filters( 'learndash_get_course_price', $course_price );
}

/**
 * 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.
 *
 * @param int|object|null $group   Course `WP_Post` object or post ID. Default to global $post.
 * @param int|null        $user_id User ID. Default to current user id.
 *
 * @return array price details.
 */
function learndash_get_group_price( $group = null, ?int $user_id = null ): array {
	if ( 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 );

	$group_price = 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 ( is_null( $user_id ) || 0 === $user_id ) {
		$user_id = get_current_user_id();
	}

	if ( $user_id > 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 ) ) {
			$group_price['price'] = $attached_coupon_data['price'];
		}
	}

	// Add subscription data.

	if ( 'subscribe' === $group_price['type'] ) {
		$frequency       = learndash_get_setting( $group->ID, 'group_price_billing_t3' );
		$interval        = learndash_get_setting( $group->ID, 'group_price_billing_p3' );
		$repeats         = learndash_get_setting( $group->ID, 'post_no_of_cycles' );
		$trial_interval  = learndash_get_setting( $group->ID, 'group_trial_duration_p1' );
		$trial_frequency = learndash_get_setting( $group->ID, 'group_trial_duration_t1' );

		$group_price['interval']  = $interval;
		$group_price['frequency'] = learndash_get_grammatical_number_label_for_interval( $interval, $frequency );

		if ( ! empty( $repeats ) ) {
			$group_price['repeats']          = $repeats;
			$group_price['repeat_frequency'] = learndash_get_grammatical_number_label_for_interval( $repeats, $frequency );
		}

		$group_price['trial_price'] = ! empty( learndash_get_setting( $group->ID, 'group_trial_price' ) ) ? learndash_get_setting( $group->ID, 'group_trial_price' ) : '';

		if ( ! empty( $trial_interval ) ) {
			$group_price['trial_interval']  = $trial_interval;
			$group_price['trial_frequency'] = learndash_get_grammatical_number_label_for_interval( $trial_interval, $trial_frequency );
		}
	}

	/**
	 * Filter Group Price details.
	 *
	 * @since 3.2.0
	 *
	 * @param array $group_price Group Price Details array.
	 */
	return apply_filters( 'learndash_get_group_price', $group_price );
}

/**
 * Get the singular or plural label for recurring payment intervals
 *
 * @since 3.6.0
 *
 * @param string $interval  Number of payment intervals.
 * @param string $frequency PayPal symbol for day, week, month or year.
 *
 * @return string
 */
function learndash_get_grammatical_number_label_for_interval( $interval, $frequency ) {
	$interval = intval( $interval );
	switch ( $frequency ) {
		case ( 'D' ):
			return _n( 'day', 'days', $interval, 'learndash' );

		case ( 'W' ):
			return _n( 'week', 'weeks', $interval, 'learndash' );

		case ( 'M' ):
			return _n( 'month', 'months', $interval, 'learndash' );

		case ( 'Y' ):
			return _n( 'year', 'years', $interval, 'learndash' );

		default:
			return '';
	}
}

/**
 * Return currency list.
 *
 * @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 ) {
		list(
			$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 );
}