1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The Carbon Pagination base class. |
4
|
|
|
* Contains and manages all of the pagination settings. |
5
|
|
|
* Abstract, can be extended by all specific pagination types. |
6
|
|
|
* |
7
|
|
|
* @abstract |
8
|
|
|
*/ |
9
|
|
|
abstract class Carbon_Pagination { |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var array |
13
|
|
|
* |
14
|
|
|
* Optional. Can be used if you want to loop through IDs instead of consecutive pages. |
15
|
|
|
* If not defined, falls back to an array of all pages from 1 to $total_pages. |
16
|
|
|
*/ |
17
|
|
|
protected $pages = array(); |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var int The total number of pages. |
21
|
|
|
*/ |
22
|
|
|
protected $total_pages = 1; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var int The current page number. |
26
|
|
|
*/ |
27
|
|
|
protected $current_page = 1; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var bool Whether the previous page link should be displayed. |
31
|
|
|
*/ |
32
|
|
|
protected $enable_prev = true; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var bool Whether the next page link should be displayed. |
36
|
|
|
*/ |
37
|
|
|
protected $enable_next = true; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var bool Whether the first page link should be displayed. |
41
|
|
|
*/ |
42
|
|
|
protected $enable_first = false; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var bool Whether the last page link should be displayed. |
46
|
|
|
*/ |
47
|
|
|
protected $enable_last = false; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var bool Whether the page number links should be displayed. |
51
|
|
|
*/ |
52
|
|
|
protected $enable_numbers = false; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var bool Whether the current page text ("Page X of Y") should be displayed. |
56
|
|
|
*/ |
57
|
|
|
protected $enable_current_page_text = false; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var int |
61
|
|
|
* |
62
|
|
|
* How much page number links should be displayed (on each side of the current page item). |
63
|
|
|
* Using 0 means only the current page item will be displayed. |
64
|
|
|
* Using -1 means no limit (all page number links will be displayed). |
65
|
|
|
*/ |
66
|
|
|
protected $number_limit = -1; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var int |
70
|
|
|
* |
71
|
|
|
* How much larger page number links should be displayed. |
72
|
|
|
* Larger page numbers can be: 10, 20, 30, etc. |
73
|
|
|
* Using 0 means none (no larger page number links will be displayed). |
74
|
|
|
*/ |
75
|
|
|
protected $large_page_number_limit = 0; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var int |
79
|
|
|
* |
80
|
|
|
* The interval between larger page number links. |
81
|
|
|
* If set to 5, larger page numbers will be 5, 10, 15, 20, etc. |
82
|
|
|
*/ |
83
|
|
|
protected $large_page_number_interval = 10; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @var string The class name of the pagination collection object. |
87
|
|
|
*/ |
88
|
|
|
protected $collection = 'Carbon_Pagination_Collection'; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @var string The class name of the pagination renderer object. |
92
|
|
|
*/ |
93
|
|
|
protected $renderer = 'Carbon_Pagination_Renderer'; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @var array |
97
|
|
|
* |
98
|
|
|
* The default argument values. Can be declared in the inheriting classes. |
99
|
|
|
* Will override the default configuration options in Carbon_Pagination::__construct |
100
|
|
|
* but can be overriden by the $args parameter. |
101
|
|
|
*/ |
102
|
|
|
public $default_args = array(); |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Constructor. |
106
|
|
|
* Creates and configures a new pagination with the provided settings. |
107
|
|
|
* |
108
|
|
|
* @param array $args Configuration options to modify the pagination settings. |
109
|
|
|
*/ |
110
|
5 |
|
public function __construct( $args = array() ) { |
111
|
|
|
// allow default options to be filtered |
112
|
5 |
|
$defaults = apply_filters( 'carbon_pagination_default_options', $this->default_args, $this ); |
113
|
|
|
|
114
|
|
|
// parse configuration options |
115
|
5 |
|
$args = wp_parse_args( $args, $defaults ); |
116
|
|
|
|
117
|
|
|
// set configuration options & constraints |
118
|
5 |
|
$this->set( $args ); |
119
|
5 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Bulk set certain configuration options & constraints. |
123
|
|
|
* |
124
|
|
|
* @param array $args Configuration options |
125
|
|
|
*/ |
126
|
3 |
|
public function set( $args ) { |
127
|
3 |
|
foreach ( $args as $arg_name => $arg_value ) { |
128
|
3 |
|
$method = 'set_' . $arg_name; |
129
|
3 |
|
if ( method_exists( $this, $method ) ) { |
130
|
2 |
|
call_user_func( array( $this, $method ), $arg_value ); |
131
|
2 |
|
} |
132
|
3 |
|
} |
133
|
3 |
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Retrieve the pages array. |
137
|
|
|
* |
138
|
|
|
* @return array $pages The pages array. |
139
|
|
|
*/ |
140
|
7 |
|
public function get_pages() { |
141
|
7 |
|
return $this->pages; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Modify the pages array. |
146
|
|
|
* Array keys are intentionally reset. |
147
|
|
|
* |
148
|
|
|
* @param array $pages The new pages array. |
149
|
|
|
*/ |
150
|
3 |
|
public function set_pages( $pages = array() ) { |
151
|
3 |
|
if ( ! is_array( $pages ) ) { |
152
|
1 |
|
$pages = array( $pages ); |
153
|
1 |
|
} |
154
|
|
|
|
155
|
3 |
|
$this->pages = array_values( $pages ); |
156
|
3 |
|
$this->total_pages = count( $pages ); |
157
|
3 |
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Retrieve the current page number. |
161
|
|
|
* |
162
|
|
|
* @return int $current_page The current page number. |
163
|
|
|
*/ |
164
|
3 |
|
public function get_current_page() { |
165
|
3 |
|
return $this->current_page; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Modify the current page number, affecting $total_pages as well. |
170
|
|
|
* |
171
|
|
|
* @param int $current_page The new current page number. |
172
|
|
|
*/ |
173
|
5 |
|
public function set_current_page( $current_page = 1 ) { |
174
|
5 |
|
$current_page = intval( $current_page ); |
175
|
5 |
|
if ( $current_page < 1 ) { |
176
|
3 |
|
$current_page = 1; |
177
|
3 |
|
} |
178
|
|
|
|
179
|
5 |
|
$total_pages = $this->get_total_pages(); |
180
|
5 |
|
if ( $current_page > $total_pages ) { |
181
|
1 |
|
$current_page = $total_pages; |
182
|
1 |
|
} |
183
|
|
|
|
184
|
5 |
|
$this->current_page = $current_page; |
185
|
5 |
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Retrieve the total number of pages. |
189
|
|
|
* |
190
|
|
|
* @return int $total_pages The total number of pages. |
191
|
|
|
*/ |
192
|
7 |
|
public function get_total_pages() { |
193
|
7 |
|
return $this->total_pages; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Modify the total number of pages, affecting $pages as well. |
198
|
|
|
* |
199
|
|
|
* @param int $total_pages The new total number of pages. |
200
|
|
|
*/ |
201
|
4 |
|
public function set_total_pages( $total_pages ) { |
202
|
4 |
|
$total_pages = intval( $total_pages ); |
203
|
4 |
|
if ( $total_pages < 1 ) { |
204
|
3 |
|
$total_pages = 1; |
205
|
3 |
|
} |
206
|
|
|
|
207
|
4 |
|
$this->total_pages = $total_pages; |
208
|
4 |
|
$this->pages = range( 1, $total_pages ); |
209
|
4 |
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Whether the previous page link should be displayed. |
213
|
|
|
* |
214
|
|
|
* @return bool $enable_prev Whether the previous page link should be displayed. |
215
|
|
|
*/ |
216
|
1 |
|
public function get_enable_prev() { |
217
|
1 |
|
return $this->enable_prev; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Change whether the previous page link should be displayed. |
222
|
|
|
* |
223
|
|
|
* @param bool $enable_prev Whether the previous page link should be displayed. |
224
|
|
|
*/ |
225
|
1 |
|
public function set_enable_prev( $enable_prev ) { |
226
|
1 |
|
$this->enable_prev = (bool) $enable_prev; |
227
|
1 |
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Whether the next page link should be displayed. |
231
|
|
|
* |
232
|
|
|
* @return bool $enable_next Whether the next page link should be displayed. |
233
|
|
|
*/ |
234
|
1 |
|
public function get_enable_next() { |
235
|
1 |
|
return $this->enable_next; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Change whether the next page link should be displayed. |
240
|
|
|
* |
241
|
|
|
* @param bool $enable_next Whether the next page link should be displayed. |
242
|
|
|
*/ |
243
|
1 |
|
public function set_enable_next( $enable_next ) { |
244
|
1 |
|
$this->enable_next = (bool) $enable_next; |
245
|
1 |
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Whether the first page link should be displayed. |
249
|
|
|
* |
250
|
|
|
* @return bool $enable_first Whether the first page link should be displayed. |
251
|
|
|
*/ |
252
|
1 |
|
public function get_enable_first() { |
253
|
1 |
|
return $this->enable_first; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Change whether the first page link should be displayed. |
258
|
|
|
* |
259
|
|
|
* @param bool $enable_first Whether the first page link should be displayed. |
260
|
|
|
*/ |
261
|
1 |
|
public function set_enable_first( $enable_first ) { |
262
|
1 |
|
$this->enable_first = (bool) $enable_first; |
263
|
1 |
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Whether the last page link should be displayed. |
267
|
|
|
* |
268
|
|
|
* @return bool $enable_last Whether the last page link should be displayed. |
269
|
|
|
*/ |
270
|
1 |
|
public function get_enable_last() { |
271
|
1 |
|
return $this->enable_last; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Change whether the last page link should be displayed. |
276
|
|
|
* |
277
|
|
|
* @param bool $enable_last Whether the last page link should be displayed. |
278
|
|
|
*/ |
279
|
1 |
|
public function set_enable_last( $enable_last ) { |
280
|
1 |
|
$this->enable_last = (bool) $enable_last; |
281
|
1 |
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Whether the page number links should be displayed. |
285
|
|
|
* |
286
|
|
|
* @return bool $enable_numbers Whether the page number links should be displayed. |
287
|
|
|
*/ |
288
|
1 |
|
public function get_enable_numbers() { |
289
|
1 |
|
return $this->enable_numbers; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Change whether the page number links should be displayed. |
294
|
|
|
* |
295
|
|
|
* @param bool $enable_numbers Whether the page number links should be displayed. |
296
|
|
|
*/ |
297
|
1 |
|
public function set_enable_numbers( $enable_numbers ) { |
298
|
1 |
|
$this->enable_numbers = (bool) $enable_numbers; |
299
|
1 |
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Whether the current page text should be displayed. |
303
|
|
|
* |
304
|
|
|
* @return bool $enable_current_page_text Whether the current page text should be displayed. |
305
|
|
|
*/ |
306
|
1 |
|
public function get_enable_current_page_text() { |
307
|
1 |
|
return $this->enable_current_page_text; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Change whether the current page text should be displayed. |
312
|
|
|
* |
313
|
|
|
* @param bool $enable_current_page_text Whether the current page text should be displayed. |
314
|
|
|
*/ |
315
|
1 |
|
public function set_enable_current_page_text( $enable_current_page_text ) { |
316
|
1 |
|
$this->enable_current_page_text = (bool) $enable_current_page_text; |
317
|
1 |
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Retrieve the page number links limit. |
321
|
|
|
* |
322
|
|
|
* @return int $number_limit The page number links limit. |
323
|
|
|
*/ |
324
|
4 |
|
public function get_number_limit() { |
325
|
4 |
|
return $this->number_limit; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Modify the page number links limit. |
330
|
|
|
* |
331
|
|
|
* @param int $number_limit The new page number links limit. |
332
|
|
|
*/ |
333
|
4 |
|
public function set_number_limit( $number_limit ) { |
334
|
4 |
|
$this->number_limit = intval( $number_limit ); |
335
|
4 |
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* Retrieve the large page number links limit. |
339
|
|
|
* |
340
|
|
|
* @return int $large_page_number_limit The large page number links limit. |
341
|
|
|
*/ |
342
|
4 |
|
public function get_large_page_number_limit() { |
343
|
4 |
|
return $this->large_page_number_limit; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Modify the large page number links limit. |
348
|
|
|
* |
349
|
|
|
* @param int $large_page_number_limit The new large page number links limit. |
350
|
|
|
*/ |
351
|
4 |
|
public function set_large_page_number_limit( $large_page_number_limit ) { |
352
|
4 |
|
$this->large_page_number_limit = absint( $large_page_number_limit ); |
353
|
4 |
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Retrieve the large page number links interval. |
357
|
|
|
* |
358
|
|
|
* @return int $large_page_number_interval The large page number links interval. |
359
|
|
|
*/ |
360
|
4 |
|
public function get_large_page_number_interval() { |
361
|
4 |
|
return $this->large_page_number_interval; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Modify the large page number links interval. |
366
|
|
|
* |
367
|
|
|
* @param int $large_page_number_interval The new large page number links interval. |
368
|
|
|
*/ |
369
|
4 |
|
public function set_large_page_number_interval( $large_page_number_interval ) { |
370
|
4 |
|
$this->large_page_number_interval = absint( $large_page_number_interval ); |
371
|
4 |
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* Retrieve the collection object class name. |
375
|
|
|
* |
376
|
|
|
* @return string $collection The collection object class name. |
377
|
|
|
*/ |
378
|
1 |
|
public function get_collection() { |
379
|
1 |
|
return $this->collection; |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Modify the collection object class name. |
384
|
|
|
* |
385
|
|
|
* @param string $collection The new collection object class name. |
386
|
|
|
*/ |
387
|
1 |
|
public function set_collection( $collection ) { |
388
|
1 |
|
$this->collection = $collection; |
389
|
1 |
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* Retrieve the renderer object class name. |
393
|
|
|
* |
394
|
|
|
* @return string $renderer The renderer object class name. |
395
|
|
|
*/ |
396
|
1 |
|
public function get_renderer() { |
397
|
1 |
|
return $this->renderer; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* Modify the renderer object class name. |
402
|
|
|
* |
403
|
|
|
* @param string $renderer The new renderer object class name. |
404
|
|
|
*/ |
405
|
1 |
|
public function set_renderer( $renderer ) { |
406
|
1 |
|
$this->renderer = $renderer; |
407
|
1 |
|
} |
408
|
|
|
|
409
|
|
|
/** |
410
|
|
|
* Get the URL to a certain page. |
411
|
|
|
* |
412
|
|
|
* @abstract |
413
|
|
|
* @param int $page_number The page number. |
414
|
|
|
* @param string $old_url Optional. The URL to add the page number to. |
415
|
|
|
* @return string $url The URL to the page number. |
416
|
|
|
*/ |
417
|
|
|
abstract public function get_page_url( $page_number, $old_url = '' ); |
418
|
|
|
|
419
|
|
|
} |