position = $position; $this->group = $group; add_filter( 'alphalisting-alphabet', array( $this, 'add_to_alphabet' ) ); add_filter( 'alphalisting_sorting_alphabet', array( $this, 'ungroup_for_sorting' ) ); add_filter( 'the-a-z-letter-title', array( $this, 'title' ) ); } } /** * Remove the numbers filters we added previously * * @since 2.0.0 * @return void */ public function teardown() { remove_filter( 'alphalisting-alphabet', array( $this, 'add_to_alphabet' ) ); remove_filter( 'alphalisting_sorting_alphabet', array( $this, 'ungroup_for_sorting' ) ); remove_filter( 'the-a-z-letter-title', array( $this, 'title' ) ); } /** * Add numbers to the alphabet * * @since 2.0.0 * @param string $alphabet The alphabet to add numbers into. * @return string The alphabet with numbers either prepended or appended */ public function add_to_alphabet( string $alphabet ): string { if ( true === $this->group ) { $numbers = '0123456789'; } else { $numbers = '0,1,2,3,4,5,6,7,8,9'; } if ( 'before' === $this->position ) { return join( ',', array( $numbers, $alphabet ) ); } else { return join( ',', array( $alphabet, $numbers ) ); } } /** * Split the grouped numbers apart again so items under the shared `0-9` * heading order by their real first digit. * * @since 4.5.1 * @param string $alphabet The alphabet used for ordering. * @return string The alphabet with the number group split into single digits. */ public function ungroup_for_sorting( string $alphabet ): string { if ( true !== $this->group ) { return $alphabet; } $parts = array_map( function( string $part ): string { return '0123456789' === trim( $part ) ? '0,1,2,3,4,5,6,7,8,9' : $part; }, explode( ',', $alphabet ) ); return join( ',', $parts ); } /** * Override the group title for grouped numbers * * @since 2.0.0 * @param string $letter The original title of the group. * @return string The new title for the group */ public function title( string $letter ): string { if ( '0' === $letter && true === $this->group ) { return '0-9'; } return $letter; } } /** * Sets the AlphaListing to include numbers. * * @since 1.7.0 * @since 1.8.0 Add $group parameter and functionality to group numbers into a single collection. * * @param string $position set to before to place the numbers first. Any other value will place them last. * @param bool $group group the numbers in a single collection rather than individually. * @return Numbers the numbers extension instance object */ function add_a_z_numbers( string $position = 'after', bool $group = false ): Numbers { return new Numbers( $position, $group ); }