PluginProbe ʕ •ᴥ•ʔ
AlphaListing / 4.3.4
AlphaListing v4.3.4
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 1 year ago legacy 1 year ago Alphabet.php 1 year ago Extension.php 1 year ago Grouping.php 1 year ago GutenBlock.php 1 year ago Indices.php 1 year ago Numbers.php 1 year ago Query.php 1 year ago Shortcode.php 1 year ago Singleton.php 1 year ago Strings.php 1 year ago
Numbers.php
116 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 = alphalisting_is_truthy( $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 ( 'hide' === $this->position ) {
72 return $alphabet;
73 }
74
75 if ( true === $this->group ) {
76 $numbers = '0123456789';
77 } else {
78 $numbers = '0,1,2,3,4,5,6,7,8,9';
79 }
80
81 if ( 'before' === $this->position ) {
82 return join( ',', array( $numbers, $alphabet ) );
83 } else {
84 return join( ',', array( $alphabet, $numbers ) );
85 }
86 }
87
88 /**
89 * Override the group title for grouped numbers
90 *
91 * @since 2.0.0
92 * @param string $letter The original title of the group.
93 * @return string The new title for the group
94 */
95 public function title( string $letter ): string {
96 if ( '0' === $letter && true === $this->group ) {
97 return '0-9';
98 }
99 return $letter;
100 }
101 }
102
103 /**
104 * Sets the AlphaListing to include numbers.
105 *
106 * @since 1.7.0
107 * @since 1.8.0 Add $group parameter and functionality to group numbers into a single collection.
108 *
109 * @param string $position set to before to place the numbers first. Any other value will place them last.
110 * @param bool $group group the numbers in a single collection rather than individually.
111 * @return Numbers the numbers extension instance object
112 */
113 function add_a_z_numbers( string $position = 'after', bool $group = false ): Numbers {
114 return new Numbers( $position, $group );
115 }
116