number_format['decimal_point'] ) ? $wp_locale->number_format['decimal_point'] : '.'; } if ( '' === $thousands_sep ) { $thousands_sep = isset( $wp_locale->number_format['thousands_sep'] ) ? $wp_locale->number_format['thousands_sep'] : ','; } $number = (float) $value; if ( empty( $attr['numberCompact'] ) ) { return number_format( $number, $decimals, $decimal_sep, $thousands_sep ); } return self::format_compact( $number, $decimals, $decimal_sep, $thousands_sep ); } /** * Format a date, date time, or time field value with the block's date format attributes. * * Mirrors acf_format_date(): the stored value is parsed with strtotime() and * printed with date_i18n(), so the wall-clock value is shown as stored and * month/day names follow the site language. * * @param mixed $value Raw stored value (Ymd, Y-m-d H:i:s, or H:i:s). * @param string $type Field type. * @param array $attr Block attributes. * @return string|null Formatted value, or null to keep the field's own format. */ public static function format_date( $value, $type, $attr ) { $mode = isset( $attr['dateFormatMode'] ) ? (string) $attr['dateFormatMode'] : 'field'; if ( 'field' === $mode || '' === $mode || ! $value || ( ! is_string( $value ) && ! is_int( $value ) ) ) { return null; } if ( is_numeric( $value ) && 8 !== strlen( (string) $value ) ) { // Legacy unix timestamp, as acf_format_date() accepts. $timestamp = (int) $value; } elseif ( 'time_picker' === $type ) { // A time has no date; use today's date in the site timezone, not UTC. $today = gmdate( 'Y-m-d', current_time( 'timestamp' ) ); // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested $timestamp = strtotime( $today . ' ' . (string) $value ); } else { $timestamp = strtotime( (string) $value ); } if ( false === $timestamp ) { return null; } if ( 'relative' === $mode ) { return 'time_picker' === $type ? null : self::format_relative( $timestamp, $type ); } $format = ''; if ( 'site' === $mode ) { $format = self::site_format( $type ); } elseif ( 'preset' === $mode ) { $format = isset( $attr['datePreset'] ) ? (string) $attr['datePreset'] : ''; } elseif ( 'custom' === $mode ) { $format = isset( $attr['dateCustomFormat'] ) ? trim( (string) $attr['dateCustomFormat'] ) : ''; } if ( '' === $format ) { return null; } return date_i18n( $format, $timestamp ); } /** * The site's own date/time format for a field type. * * @param string $type Field type. * @return string */ public static function site_format( $type ) { if ( 'time_picker' === $type ) { return get_option( 'time_format' ); } if ( 'date_time_picker' === $type ) { return get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); } return get_option( 'date_format' ); } /** * Relative form of a date ("Today", "3 days ago", "In 2 hours"). * * Both sides are wall-clock timestamps (strtotime() of the stored value and * current_time( 'timestamp' )), so the comparison follows the site timezone. * * @param int $timestamp Wall-clock timestamp of the value. * @param string $type Field type: date_picker compares calendar days. * @return string */ private static function format_relative( $timestamp, $type ) { $now = current_time( 'timestamp' ); // phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested if ( 'date_picker' === $type ) { $today = strtotime( gmdate( 'Y-m-d', $now ) ); $day = strtotime( gmdate( 'Y-m-d', $timestamp ) ); $days = (int) round( ( $day - $today ) / DAY_IN_SECONDS ); if ( 0 === $days ) { return __( 'Today', 'acf-field-blocks' ); } elseif ( -1 === $days ) { return __( 'Yesterday', 'acf-field-blocks' ); } elseif ( 1 === $days ) { return __( 'Tomorrow', 'acf-field-blocks' ); } $timestamp = $day; $now = $today; } $span = self::time_span( abs( $timestamp - $now ) ); /* translators: %s: time span, e.g. "3 days" */ return $timestamp < $now ? sprintf( __( '%s ago', 'acf-field-blocks' ), $span ) : sprintf( __( 'In %s', 'acf-field-blocks' ), $span ); } /** * Human readable time span, same steps as human_time_diff(). * * @param int $diff Seconds. * @return string */ private static function time_span( $diff ) { if ( $diff < MINUTE_IN_SECONDS ) { $n = max( 1, (int) $diff ); /* translators: %s: number of seconds */ return sprintf( _n( '%s second', '%s seconds', $n, 'acf-field-blocks' ), $n ); } elseif ( $diff < HOUR_IN_SECONDS ) { $n = max( 1, (int) round( $diff / MINUTE_IN_SECONDS ) ); /* translators: %s: number of minutes */ return sprintf( _n( '%s minute', '%s minutes', $n, 'acf-field-blocks' ), $n ); } elseif ( $diff < DAY_IN_SECONDS ) { $n = max( 1, (int) round( $diff / HOUR_IN_SECONDS ) ); /* translators: %s: number of hours */ return sprintf( _n( '%s hour', '%s hours', $n, 'acf-field-blocks' ), $n ); } elseif ( $diff < WEEK_IN_SECONDS ) { $n = max( 1, (int) round( $diff / DAY_IN_SECONDS ) ); /* translators: %s: number of days */ return sprintf( _n( '%s day', '%s days', $n, 'acf-field-blocks' ), $n ); } elseif ( $diff < MONTH_IN_SECONDS ) { $n = max( 1, (int) round( $diff / WEEK_IN_SECONDS ) ); /* translators: %s: number of weeks */ return sprintf( _n( '%s week', '%s weeks', $n, 'acf-field-blocks' ), $n ); } elseif ( $diff < YEAR_IN_SECONDS ) { $n = max( 1, (int) round( $diff / MONTH_IN_SECONDS ) ); /* translators: %s: number of months */ return sprintf( _n( '%s month', '%s months', $n, 'acf-field-blocks' ), $n ); } $n = max( 1, (int) round( $diff / YEAR_IN_SECONDS ) ); /* translators: %s: number of years */ return sprintf( _n( '%s year', '%s years', $n, 'acf-field-blocks' ), $n ); } /** * Format a number in compact form (1.2K, 3.5M, ...). * * @param float $number Number. * @param int $decimals Decimals kept on the scaled value. * @param string $decimal_sep Decimal separator. * @param string $thousands_sep Thousands separator. * @return string */ private static function format_compact( $number, $decimals, $decimal_sep, $thousands_sep ) { $units = array( '' => 1, 'K' => 1e3, 'M' => 1e6, 'B' => 1e9, 'T' => 1e12, ); $labels = array( '' => '', 'K' => _x( 'K', 'compact number suffix for thousands, e.g. 1.2K', 'acf-field-blocks' ), 'M' => _x( 'M', 'compact number suffix for millions, e.g. 3.5M', 'acf-field-blocks' ), 'B' => _x( 'B', 'compact number suffix for billions, e.g. 2B', 'acf-field-blocks' ), 'T' => _x( 'T', 'compact number suffix for trillions, e.g. 4T', 'acf-field-blocks' ), ); $negative = $number < 0; $absolute = abs( $number ); $keys = array_keys( $units ); $index = 0; // Pick the largest unit the number reaches. foreach ( $keys as $i => $unit ) { if ( $absolute >= $units[ $unit ] ) { $index = $i; } } $scaled = round( $absolute / $units[ $keys[ $index ] ], $decimals ); // Rounding can reach the next unit (999.95K -> 1000K), move up instead. while ( $scaled >= 1000 && $index < count( $keys ) - 1 ) { ++$index; $scaled = round( $absolute / $units[ $keys[ $index ] ], $decimals ); } // Plain numbers below 1000 keep the regular format. if ( 0 === $index ) { return number_format( $number, $decimals, $decimal_sep, $thousands_sep ); } $plain = number_format( $scaled, $decimals, '.', '' ); if ( false !== strpos( $plain, '.' ) ) { $plain = rtrim( rtrim( $plain, '0' ), '.' ); } $parts = explode( '.', $plain ); $integer = number_format( (float) $parts[0], 0, '', $thousands_sep ); $result = isset( $parts[1] ) ? $integer . $decimal_sep . $parts[1] : $integer; return ( $negative ? '-' : '' ) . $result . $labels[ $keys[ $index ] ]; } }