Shortcode
9 months ago
Alphabet.php
9 months ago
Extension.php
9 months ago
Grouping.php
9 months ago
GutenBlock.php
9 months ago
Indices.php
9 months ago
Numbers.php
9 months ago
Query.php
9 months ago
Shortcode.php
9 months ago
Singleton.php
9 months ago
Strings.php
9 months ago
Grouping.php
146 lines
| 1 | <?php |
| 2 | /** |
| 3 | * AlphaListing Alphabet grouping system |
| 4 | * |
| 5 | * @package alphalisting |
| 6 | */ |
| 7 | |
| 8 | declare(strict_types=1); |
| 9 | |
| 10 | namespace eslin87\AlphaListing; |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit; |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * AlphaListing Alphabet grouping system class |
| 18 | * |
| 19 | * @since 2.0.0 |
| 20 | */ |
| 21 | class Grouping { |
| 22 | /** |
| 23 | * The configured grouping count |
| 24 | * |
| 25 | * @since 2.0.0 |
| 26 | * @var int |
| 27 | */ |
| 28 | private $grouping; |
| 29 | |
| 30 | /** |
| 31 | * The populated headings for the listing |
| 32 | * |
| 33 | * @since 2.0.0 |
| 34 | * @var array<string,array> |
| 35 | */ |
| 36 | private $headings; |
| 37 | |
| 38 | /** |
| 39 | * Add filters to group the alphabet letters |
| 40 | * |
| 41 | * @since 2.0.0 |
| 42 | * @param int $grouping The number of letters in each group. |
| 43 | */ |
| 44 | public function __construct( int $grouping ) { |
| 45 | $this->grouping = $grouping; |
| 46 | |
| 47 | if ( 1 < $grouping ) { |
| 48 | add_filter( 'alphalisting-alphabet', array( $this, 'alphabet_filter' ), 2 ); |
| 49 | add_filter( 'the-a-z-letter-title', array( $this, 'heading' ), 5 ); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Remove the filters grouping the alphabet letters |
| 55 | * |
| 56 | * @since 2.0.0 |
| 57 | * @return void |
| 58 | */ |
| 59 | public function teardown() { |
| 60 | remove_filter( 'alphalisting-alphabet', array( $this, 'alphabet_filter' ), 2 ); |
| 61 | remove_filter( 'the-a-z-letter-title', array( $this, 'heading' ), 5 ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Override the alphabet with grouped letters |
| 66 | * |
| 67 | * @since 2.0.0 |
| 68 | * @param string $alphabet The alphabet to override. |
| 69 | * @return string the new grouped alphabet. |
| 70 | */ |
| 71 | public function alphabet_filter( string $alphabet ): string { |
| 72 | $headings = array(); |
| 73 | $letters = explode( ',', $alphabet ); |
| 74 | $letters = array_map( 'trim', $letters ); |
| 75 | |
| 76 | $i = 0; |
| 77 | $j = 0; |
| 78 | |
| 79 | $grouping = $this->grouping; |
| 80 | |
| 81 | $groups = array_reduce( |
| 82 | $letters, |
| 83 | /** |
| 84 | * Closure to reduce the groups array and populate the headings array |
| 85 | * |
| 86 | * @param array<int,string> $carry |
| 87 | * @param string $letter |
| 88 | * @return array<int,string> |
| 89 | */ |
| 90 | function( array $carry, string $letter ) use ( $grouping, &$headings, &$i, &$j ) { |
| 91 | if ( isset( $carry[ $j ] ) ) { |
| 92 | $carry[ $j ] = $carry[ $j ] . $letter; |
| 93 | } else { |
| 94 | $carry[ $j ] = $letter; |
| 95 | } |
| 96 | $headings[ $j ][] = Strings::maybe_mb_substr( $letter, 0, 1 ); |
| 97 | |
| 98 | if ( $i + 1 === $grouping ) { |
| 99 | $i = 0; |
| 100 | $j++; |
| 101 | } else { |
| 102 | $i++; |
| 103 | } |
| 104 | |
| 105 | return $carry; |
| 106 | }, |
| 107 | array() |
| 108 | ); |
| 109 | |
| 110 | $this->headings = array_reduce( |
| 111 | $headings, |
| 112 | /** |
| 113 | * Closure to reduce the headings array |
| 114 | * |
| 115 | * @param array<string,string> $carry |
| 116 | * @param string $heading |
| 117 | * @return array<string,string> |
| 118 | */ |
| 119 | function( array $carry, array $heading ): array { |
| 120 | $carry[ Strings::maybe_mb_substr( $heading[0], 0, 1 ) ] = $heading; |
| 121 | return $carry; |
| 122 | }, |
| 123 | array() |
| 124 | ); |
| 125 | |
| 126 | return join( ',', $groups ); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Override the title of each group |
| 131 | * |
| 132 | * @since 2.0.0 |
| 133 | * @param string $title The original title of the group. |
| 134 | * @return string The new title for the group. |
| 135 | */ |
| 136 | public function heading( string $title ): string { |
| 137 | if ( isset( $this->headings[ $title ] ) && is_array( $this->headings[ $title ] ) ) { |
| 138 | $first = $this->headings[ $title ][0]; |
| 139 | $last = $this->headings[ $title ][ count( $this->headings[ $title ] ) - 1 ]; |
| 140 | return $first . '-' . $last; |
| 141 | } |
| 142 | |
| 143 | return $title; |
| 144 | } |
| 145 | } |
| 146 |