|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* The Carbon Pagination item collection class. |
|
4
|
|
|
* Contains and manages the pagination items. |
|
5
|
|
|
* Can generate the items based on the pagination settings. |
|
6
|
|
|
*/ |
|
7
|
|
|
class Carbon_Pagination_Collection { |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @var array |
|
11
|
|
|
* |
|
12
|
|
|
* Pagination items. |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $items = array(); |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var Carbon_Pagination_HTML |
|
18
|
|
|
* |
|
19
|
|
|
* The pagination object. |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $pagination; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Constructor. |
|
25
|
|
|
* Creates and configures a new pagination collection for the provided pagination. |
|
26
|
|
|
* |
|
27
|
|
|
* @param Carbon_Pagination_HTML $pagination Pagination object. |
|
28
|
|
|
* @param bool $autogenerate Whether to automatically generate pagination items. |
|
29
|
|
|
*/ |
|
30
|
5 |
|
public function __construct( Carbon_Pagination_HTML $pagination, $autogenerate = true ) { |
|
31
|
5 |
|
$this->set_pagination( $pagination ); |
|
32
|
|
|
|
|
33
|
|
|
// whether to auto generate pagination items |
|
34
|
5 |
|
$autogenerate = apply_filters( 'carbon_pagination_autogenerate_collection_items', $autogenerate, $this ); |
|
35
|
5 |
|
if ( $autogenerate ) { |
|
36
|
3 |
|
$this->generate(); |
|
37
|
3 |
|
} |
|
38
|
5 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Generate the pagination items and wrappers. |
|
42
|
|
|
*/ |
|
43
|
4 |
|
public function generate() { |
|
44
|
4 |
|
$this->generate_items(); |
|
45
|
4 |
|
$this->generate_wrappers(); |
|
46
|
|
|
|
|
47
|
|
|
// allow developers to modify the collection items after it was generated |
|
48
|
4 |
|
do_action( 'carbon_pagination_collection_after_generate', $this ); |
|
49
|
4 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Retrieve the item prototypes for the collection. |
|
53
|
|
|
* These represent the classes of all items that will be generated and their |
|
54
|
|
|
* corresponding pagination methods that determine their availability. |
|
55
|
|
|
*/ |
|
56
|
2 |
|
public function get_item_prototypes() { |
|
57
|
|
|
// condition method => item class |
|
58
|
|
|
$item_classes = array( |
|
59
|
2 |
|
'get_enable_current_page_text' => 'Carbon_Pagination_Item_Current_Page_Text', |
|
60
|
2 |
|
'get_enable_first' => 'Carbon_Pagination_Item_First_Page', |
|
61
|
2 |
|
'get_enable_prev' => 'Carbon_Pagination_Item_Previous_Page', |
|
62
|
2 |
|
'get_enable_numbers' => 'Carbon_Pagination_Item_Number_Links', |
|
63
|
2 |
|
'get_enable_next' => 'Carbon_Pagination_Item_Next_Page', |
|
64
|
2 |
|
'get_enable_last' => 'Carbon_Pagination_Item_Last_Page', |
|
65
|
2 |
|
); |
|
66
|
|
|
|
|
67
|
|
|
// allow default methods => items to be filtered |
|
68
|
2 |
|
return apply_filters( 'carbon_pagination_default_collection_items', $item_classes, $this ); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Generate the pagination items. |
|
73
|
|
|
*/ |
|
74
|
5 |
|
public function generate_items() { |
|
75
|
5 |
|
$pagination = $this->get_pagination(); |
|
76
|
5 |
|
$items = array(); |
|
77
|
|
|
|
|
78
|
|
|
// allow default methods => items to be filtered |
|
79
|
5 |
|
$item_classes = $this->get_item_prototypes(); |
|
80
|
|
|
|
|
81
|
|
|
// if item is enabled, generate it |
|
82
|
5 |
|
foreach ( $item_classes as $method => $classname ) { |
|
83
|
5 |
|
if ( call_user_func( array( $pagination, $method ) ) ) { |
|
84
|
4 |
|
$items[] = new $classname($this); |
|
85
|
4 |
|
} |
|
86
|
5 |
|
} |
|
87
|
|
|
|
|
88
|
5 |
|
$this->set_items( $items ); |
|
89
|
5 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Generate the pagination wrappers. |
|
93
|
|
|
*/ |
|
94
|
2 |
|
public function generate_wrappers() { |
|
95
|
2 |
|
$pagination = $this->get_pagination(); |
|
96
|
2 |
|
$items = $this->get_items(); |
|
97
|
|
|
|
|
98
|
2 |
|
if ( ! empty( $items ) ) { |
|
99
|
|
|
// insert wrapper before the items |
|
100
|
1 |
|
$wrapper_before = new Carbon_Pagination_Item_HTML( $this ); |
|
101
|
1 |
|
$wrapper_before->set_html( $pagination->get_wrapper_before() ); |
|
102
|
1 |
|
$this->insert_item_at( $wrapper_before, 0 ); |
|
103
|
|
|
|
|
104
|
|
|
// insert wrapper after the items |
|
105
|
1 |
|
$wrapper_after = new Carbon_Pagination_Item_HTML( $this ); |
|
106
|
1 |
|
$wrapper_after->set_html( $pagination->get_wrapper_after() ); |
|
107
|
1 |
|
$this->insert_item_at( $wrapper_after, count( $items ) + 1 ); |
|
108
|
1 |
|
} |
|
109
|
2 |
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Retrieve the pagination object. |
|
113
|
|
|
* |
|
114
|
|
|
* @return Carbon_Pagination_HTML $pagination The pagination object. |
|
115
|
|
|
*/ |
|
116
|
1 |
|
public function get_pagination() { |
|
117
|
1 |
|
return $this->pagination; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Modify the pagination object. |
|
122
|
|
|
* |
|
123
|
|
|
* @param Carbon_Pagination_HTML $pagination The new pagination object. |
|
124
|
|
|
*/ |
|
125
|
1 |
|
public function set_pagination( Carbon_Pagination_HTML $pagination ) { |
|
126
|
1 |
|
$this->pagination = $pagination; |
|
127
|
1 |
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Retrieve the pagination items in the collection. |
|
131
|
|
|
* |
|
132
|
|
|
* @return array $items The pagination items, contained in the collection. |
|
133
|
|
|
*/ |
|
134
|
1 |
|
public function get_items() { |
|
135
|
1 |
|
return $this->items; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Modify the pagination items in the collection. |
|
140
|
|
|
* |
|
141
|
|
|
* @param array $items The new set of pagination items. |
|
142
|
|
|
*/ |
|
143
|
1 |
|
public function set_items( $items = array() ) { |
|
144
|
1 |
|
$this->items = $items; |
|
145
|
1 |
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Add item(s) to the collection. |
|
149
|
|
|
* If $new_items is not an array, it will be treated as one item. |
|
150
|
|
|
* If $new_items is an array, it will be treated as a set of items. |
|
151
|
|
|
* |
|
152
|
|
|
* @param mixed $new_items The set of pagination items to add. |
|
153
|
|
|
*/ |
|
154
|
4 |
|
public function add_items( $new_items = array() ) { |
|
155
|
4 |
|
if ( ! is_array( $new_items ) ) { |
|
156
|
1 |
|
$new_items = array( $new_items ); |
|
157
|
1 |
|
} else { |
|
158
|
3 |
|
$new_items = array_values( $new_items ); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
4 |
|
$items = $this->get_items(); |
|
162
|
4 |
|
$items = array_merge( $items, $new_items ); |
|
163
|
|
|
|
|
164
|
4 |
|
$this->set_items( $items ); |
|
165
|
4 |
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Insert item(s) at a specified index in the collection. |
|
169
|
|
|
* If the $item is an array, it will be treated as a set of items. |
|
170
|
|
|
* If the $item is not an array, it will be treated as a single item. |
|
171
|
|
|
* |
|
172
|
|
|
* @param mixed $item The item(s) to insert. |
|
173
|
|
|
* @param int $index The index to insert the item at. |
|
174
|
|
|
*/ |
|
175
|
6 |
|
public function insert_item_at( $item, $index ) { |
|
176
|
6 |
|
$items = $this->get_items(); |
|
177
|
6 |
|
if ( ! is_array( $item ) ) { |
|
178
|
6 |
|
$item = array( $item ); |
|
179
|
6 |
|
} |
|
180
|
|
|
|
|
181
|
6 |
|
$before = array_slice( $items, 0, $index ); |
|
182
|
6 |
|
$after = array_slice( $items, $index ); |
|
183
|
6 |
|
$new_items = array_merge( $before, $item, $after ); |
|
184
|
|
|
|
|
185
|
6 |
|
$this->set_items( $new_items ); |
|
186
|
6 |
|
} |
|
187
|
|
|
|
|
188
|
|
|
} |