setAttribute( \NumberFormatter::FRACTION_DIGITS, Money::decimals( $currency ) ); $text = $formatter->formatCurrency( Money::to_major( $minor, $currency ), $currency ); if ( false !== $text ) { return $text; } } $symbols = array( 'USD' => '$', 'EUR' => '€', 'GBP' => '£', 'JPY' => '¥', 'INR' => '₹', ); $amount = number_format_i18n( Money::to_major( $minor, $currency ), Money::decimals( $currency ) ); return isset( $symbols[ $currency ] ) ? $symbols[ $currency ] . $amount : $currency . ' ' . $amount; } /** * A moment in a time zone, e.g. "June 1, 2030 at 9:30 am". * * @param int $timestamp Unix time. * @param DateTimeZone|null $tz Zone; the site's when null. */ public static function when( int $timestamp, ?DateTimeZone $tz = null ): string { $tz = $tz ?? Settings::timezone(); /* translators: 1: date; 2: time. */ return sprintf( __( '%1$s at %2$s', 'schedulereed' ), self::date( $timestamp, $tz ), self::time( $timestamp, $tz ) ); } /** * A date in the site's date format. * * @param int $timestamp Unix time. * @param DateTimeZone|null $tz Zone. */ public static function date( int $timestamp, ?DateTimeZone $tz = null ): string { return (string) wp_date( (string) get_option( 'date_format' ), $timestamp, $tz ?? Settings::timezone() ); } /** * A time in the site's time format. * * @param int $timestamp Unix time. * @param DateTimeZone|null $tz Zone. */ public static function time( int $timestamp, ?DateTimeZone $tz = null ): string { return (string) wp_date( (string) get_option( 'time_format' ), $timestamp, $tz ?? Settings::timezone() ); } /** * A length of time, e.g. "1 hour 30 minutes". * * @param int $minutes Minutes. */ public static function duration( int $minutes ): string { $days = intdiv( $minutes, 1440 ); $hours = intdiv( $minutes % 1440, 60 ); $mins = $minutes % 60; $parts = array(); if ( $days > 0 ) { /* translators: %d: days. */ $parts[] = sprintf( _n( '%d day', '%d days', $days, 'schedulereed' ), $days ); } if ( $hours > 0 ) { /* translators: %d: hours. */ $parts[] = sprintf( _n( '%d hour', '%d hours', $hours, 'schedulereed' ), $hours ); } if ( $mins > 0 || array() === $parts ) { /* translators: %d: minutes. */ $parts[] = sprintf( _n( '%d minute', '%d minutes', $mins, 'schedulereed' ), $mins ); } return implode( ' ', $parts ); } /** * The booking reference customers see, e.g. "K7Q2M9XA". * * @param array $booking Booking. */ public static function reference( array $booking ): string { return strtoupper( (string) $booking['uid'] ); } /** * The customer's own page for a booking: view, pay, move, cancel. * * @param array $booking Booking with uid and token. */ public static function manage_url( array $booking ): string { return add_query_arg( array( 'schedulereed_booking' => $booking['uid'], 'key' => $booking['token'], ), Settings::portal_url() ); } /** * Cancellation and change terms as sentences. With a start time the * deadlines are dates, and those already past are left out. * * @param CancellationPolicy|null $policy Refund terms; null = staff decide. * @param ChangeRules|null $changes Self-service rules. * @param int $start Booking start, or 0 for settings screens. * @return string[] */ public static function policy( ?CancellationPolicy $policy, ?ChangeRules $changes = null, int $start = 0 ): array { $lines = array(); $now = Clock::now(); if ( null !== $policy ) { $steps = array(); foreach ( $policy->steps() as $step ) { if ( 0 === $start ) { $steps[] = 100 === $step['percent'] /* translators: %s: length of time, e.g. "24 hours". */ ? sprintf( __( 'Full refund if cancelled at least %s before.', 'schedulereed' ), self::duration( $step['hours'] * 60 ) ) /* translators: 1: percentage; 2: length of time. */ : sprintf( __( '%1$d%% refund if cancelled at least %2$s before.', 'schedulereed' ), $step['percent'], self::duration( $step['hours'] * 60 ) ); continue; } $deadline = $start - $step['hours'] * HOUR_IN_SECONDS; if ( $deadline < $now ) { continue; } $steps[] = 100 === $step['percent'] /* translators: %s: date and time. */ ? sprintf( __( 'Free cancellation until %s.', 'schedulereed' ), self::when( $deadline ) ) /* translators: 1: percentage; 2: date and time. */ : sprintf( __( '%1$d%% refund if you cancel by %2$s.', 'schedulereed' ), $step['percent'], self::when( $deadline ) ); } if ( array() === $steps ) { $steps[] = $policy->is_non_refundable() ? __( 'Non-refundable.', 'schedulereed' ) : __( 'No refund if you cancel now.', 'schedulereed' ); } else { $steps[] = __( 'No refund after that.', 'schedulereed' ); } $lines = $steps; } if ( null !== $changes && $changes->reschedule_hours > 0 && $changes->max_reschedules > 0 ) { /* translators: %s: length of time. */ $lines[] = sprintf( __( 'You can move your booking online up to %s before it starts.', 'schedulereed' ), self::duration( $changes->reschedule_hours * 60 ) ); } return $lines; } /** * The refund terms copied onto a booking, as sentences. * * @param array $booking Booking. * @return string[] */ public static function booking_policy( array $booking ): array { try { $policy = CancellationPolicy::from_array( $booking['policy']['cancellation'] ?? null ); } catch ( \InvalidArgumentException ) { $policy = null; } return self::policy( $policy, null, (int) $booking['start'] ); } }