PluginProbe ʕ •ᴥ•ʔ
AlphaListing / 4.5.0
AlphaListing v4.5.0
4.5.0 trunk 4.3.4 4.3.5 4.3.6 4.3.7 4.4.0
alphalisting / src / Numbers.php
alphalisting / src Last commit date
Shortcode 2 weeks ago Alphabet.php 2 weeks ago Extension.php 2 weeks ago Grouping.php 2 weeks ago GutenBlock.php 2 weeks ago Indices.php 2 weeks ago Numbers.php 2 weeks ago Query.php 2 weeks ago Shortcode.php 2 weeks ago Singleton.php 2 weeks ago Strings.php 2 weeks ago
Numbers.php
112 lines
1 <?php
2 /**
3 * AlphaListing numbers functionality
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 * Adds and maintains filters providing number functionality to the alphabet
18 *
19 * @since 2.0.0
20 */
21 class Numbers {
22 /**
23 * Where to place the numbers.
24 *
25 * @var string
26 */
27 protected $position = 'hide';
28
29 /**
30 * Whether to group the numbers in a single entry.
31 *
32 * @var boolean
33 */
34 protected $group = false;
35
36 /**
37 * Add filters to append or prepend numbers to the alphabet with optional grouping
38 *
39 * @since 2.0.0
40 * @param string $position Can be either "before" or "after" indicating where to place the numbers respective to the alphabet.
41 * @param bool $group Whether to group the numbers into a single heading or individually.
42 */
43 public function __construct( string $position = 'hide', bool $group = false ) {
44 if ( 'before' === $position || 'after' === $position ) {
45 $this->position = $position;
46 $this->group = $group;
47 add_filter( 'alphalisting-alphabet', array( $this, 'add_to_alphabet' ) );
48 add_filter( 'the-a-z-letter-title', array( $this, 'title' ) );
49 }
50 }
51
52 /**
53 * Remove the numbers filters we added previously
54 *
55 * @since 2.0.0
56 * @return void
57 */
58 public function teardown() {
59 remove_filter( 'alphalisting-alphabet', array( $this, 'add_to_alphabet' ) );
60 remove_filter( 'the-a-z-letter-title', array( $this, 'title' ) );
61 }
62
63 /**
64 * Add numbers to the alphabet
65 *
66 * @since 2.0.0
67 * @param string $alphabet The alphabet to add numbers into.
68 * @return string The alphabet with numbers either prepended or appended
69 */
70 public function add_to_alphabet( string $alphabet ): string {
71 if ( true === $this->group ) {
72 $numbers = '0123456789';
73 } else {
74 $numbers = '0,1,2,3,4,5,6,7,8,9';
75 }
76
77 if ( 'before' === $this->position ) {
78 return join( ',', array( $numbers, $alphabet ) );
79 } else {
80 return join( ',', array( $alphabet, $numbers ) );
81 }
82 }
83
84 /**
85 * Override the group title for grouped numbers
86 *
87 * @since 2.0.0
88 * @param string $letter The original title of the group.
89 * @return string The new title for the group
90 */
91 public function title( string $letter ): string {
92 if ( '0' === $letter && true === $this->group ) {
93 return '0-9';
94 }
95 return $letter;
96 }
97 }
98
99 /**
100 * Sets the AlphaListing to include numbers.
101 *
102 * @since 1.7.0
103 * @since 1.8.0 Add $group parameter and functionality to group numbers into a single collection.
104 *
105 * @param string $position set to before to place the numbers first. Any other value will place them last.
106 * @param bool $group group the numbers in a single collection rather than individually.
107 * @return Numbers the numbers extension instance object
108 */
109 function add_a_z_numbers( string $position = 'after', bool $group = false ): Numbers {
110 return new Numbers( $position, $group );
111 }
112