diff --git a/src/Block.php b/src/Block.php index c08aad0..91c061b 100644 --- a/src/Block.php +++ b/src/Block.php @@ -14,6 +14,48 @@ public function __construct( array $attributes ) { $this->attributes = $attributes; } + /** + * Format a count for compact badge/pill display (GitHub-style). + * + * Uses locale-aware grouping under 1,000, one decimal k/M between 1,000 and 99,999, + * and whole k/M from 100,000 upward so wide counts fit small pills. + */ + protected function format_count( int $count ): string { + if ( $count >= 1_000_000 ) { + if ( $count >= 100_000_000 ) { + return (string) (int) round( $count / 1_000_000 ) . 'M'; + } + + return $this->format_compact_unit( $count, 1_000_000, 'M' ); + } + + if ( $count >= 100_000 ) { + return (string) (int) round( $count / 1_000 ) . 'k'; + } + + if ( $count >= 1_000 ) { + return $this->format_compact_unit( $count, 1_000, 'k' ); + } + + return number_format_i18n( $count ); + } + + /** + * @param int $count Raw count value. + * @param int $unit Divisor for the compact unit (1_000 or 1_000_000). + * @param string $suffix Compact suffix (`k` or `M`). + */ + protected function format_compact_unit( int $count, int $unit, string $suffix ): string { + $value = round( $count / $unit, 1 ); + $formatted = number_format( $value, 1, '.', '' ); + + if ( str_ends_with( $formatted, '.0' ) ) { + $formatted = substr( $formatted, 0, -2 ); + } + + return $formatted . $suffix; + } + protected function fetchData( string $url, string $keySuffix = '' ) { $key = "blocks_for_github_$keySuffix"; $data = get_transient( $key ); @@ -318,11 +360,11 @@ class="bfg-top-repo-pill bfg-top-repo-pill--purple">forks ); ?> + ); ?>format_count( (int) $repo->forks ) ); ?> stargazers_count ); ?> + ); ?>format_count( (int) $repo->stargazers_count ) ); ?>