= 1000000 ) { /* translators: %s: a number of millions, e.g. "1.2". */ return sprintf( __( '%sM', 'adpix-analytics' ), number_format_i18n( $value / 1000000, $abs >= 10000000 ? 0 : 1 ) ); } if ( $abs >= 1000 ) { /* translators: %s: a number of thousands, e.g. "1.2". */ return sprintf( __( '%sK', 'adpix-analytics' ), number_format_i18n( $value / 1000, $abs >= 10000 ? 0 : 1 ) ); } return number_format_i18n( $value, self::decimals( $value ) ); } /** * An amount of money. * * The currency is shown as the code the property reports in rather than a symbol, because the * property's currency is not necessarily the site's, and a "$" in front of a figure denominated * in something else is worse than no symbol at all. * * @param float $value Amount. * @param string $currency ISO 4217 code, may be empty. * @return string */ public static function money( float $value, string $currency ): string { $amount = number_format_i18n( $value, 2 ); if ( '' === $currency ) { return $amount; } /* translators: 1: formatted amount, 2: ISO currency code. */ return sprintf( _x( '%1$s %2$s', 'money with currency code', 'adpix-analytics' ), $amount, $currency ); } /** * A share of a total. * * @param float $value Zero to one hundred. * @return string */ public static function percent( float $value ): string { /* translators: %s: a percentage figure, without the sign. */ return sprintf( __( '%s%%', 'adpix-analytics' ), number_format_i18n( $value, $value > 0.0 && $value < 10.0 ? 1 : 0 ) ); } /** * A calendar day, short. * * @param string $ymd Date as YYYY-MM-DD. * @return string The localised day, or the input when it cannot be parsed. */ public static function day( string $ymd ): string { $stamp = strtotime( $ymd . ' 00:00:00 UTC' ); if ( false === $stamp ) { return $ymd; } // Formatted in UTC because the date is already the property's own calendar day: shifting it into // the site's timezone would move half the year's points onto the day before. return (string) wp_date( 'j M', $stamp, new \DateTimeZone( 'UTC' ) ); } /** * A calendar day, written out. * * @param string $ymd Date as YYYY-MM-DD. * @return string */ public static function date( string $ymd ): string { $stamp = strtotime( $ymd . ' 00:00:00 UTC' ); if ( false === $stamp ) { return $ymd; } return (string) wp_date( (string) get_option( 'date_format' ), $stamp, new \DateTimeZone( 'UTC' ) ); } /** * The readable name of a metric. * * @param string $metric Metric key as the API names it. * @return string The translated name, or the key when it is one this screen does not show. */ public static function metric( string $metric ): string { $names = array( 'totalUsers' => __( 'Visitors', 'adpix-analytics' ), 'users' => __( 'Visitors', 'adpix-analytics' ), 'activeUsers' => __( 'Active visitors', 'adpix-analytics' ), 'sessions' => __( 'Visits', 'adpix-analytics' ), 'pageviews' => __( 'Page views', 'adpix-analytics' ), 'eventCount' => __( 'Events', 'adpix-analytics' ), 'conversions' => __( 'Conversions', 'adpix-analytics' ), 'conversionValue' => __( 'Conversion value', 'adpix-analytics' ), 'revenue' => __( 'Revenue', 'adpix-analytics' ), ); return $names[ $metric ] ?? $metric; } /** * Whether a metric is money rather than a count. * * @param string $metric Metric key. * @return bool */ public static function is_money( string $metric ): bool { return in_array( $metric, array( 'conversionValue', 'revenue' ), true ); } /** * One metric value, formatted according to what it is. * * @param string $metric Metric key. * @param float $value Value. * @param string $currency Property currency. * @return string */ public static function value( string $metric, float $value, string $currency = '' ): string { return self::is_money( $metric ) ? self::money( $value, $currency ) : self::number( $value ); } /** * How many decimal places a value deserves. * * Counts are whole things — half a visit does not exist — so they are shown whole. A value that * genuinely has a fraction keeps two places rather than being rounded into looking exact. * * @param float $value Raw number. * @return int */ private static function decimals( float $value ): int { return abs( $value - round( $value ) ) < 0.005 ? 0 : 2; } }