-
-
Notifications
You must be signed in to change notification settings - Fork 506
/
Pagination.php
269 lines (236 loc) · 8.89 KB
/
Pagination.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
namespace Timber;
use WP_Query;
/**
* Class Pagination
*
* @api
*/
class Pagination
{
public $current;
public $total;
public $pages;
public $next;
public $prev;
/**
* Pagination constructor.
*
* @api
*
* @param array $prefs
* @param WP_Query|null $wp_query
*/
public function __construct($prefs = [], $wp_query = null)
{
$this->init($prefs, $wp_query);
}
/**
* Get pagination.
*
* @api
* @param array $prefs
* @return array mixed
*/
public static function get_pagination($prefs = [])
{
$pagination = new self($prefs);
$pagination = \get_object_vars($pagination);
return $pagination;
}
protected function init($prefs = [], $wp_query = null)
{
if (!$wp_query) {
global $wp_query;
}
// use the current page from the provided query if available; else fall back to the global
$paged = $wp_query->query_vars['paged'] ?? \get_query_var('paged');
global $wp_rewrite;
$args = [];
// calculate the total number of pages based on found posts and posts per page
$ppp = $wp_query->query_vars['posts_per_page'] ?? 10;
$args['total'] = \ceil($wp_query->found_posts / $ppp);
if ($wp_rewrite->using_permalinks()) {
$url = \explode('?', \get_pagenum_link(0, false));
if (isset($url[1])) {
$query = [];
\wp_parse_str($url[1], $query);
$args['add_args'] = $query;
}
$args['format'] = $wp_rewrite->pagination_base . '/%#%';
$args['base'] = \trailingslashit($url[0]) . '%_%';
} else {
$big = 999999999;
$pagination_link = \get_pagenum_link($big, false);
$args['base'] = \str_replace('paged=' . $big, '', $pagination_link);
$args['format'] = '?paged=%#%';
}
$args['type'] = 'array';
$args['current'] = \max(1, $paged);
$args['mid_size'] = \max(9 - $args['current'], 3);
if (\is_int($prefs)) {
$args['mid_size'] = $prefs - 2;
} else {
$args = \array_merge($args, $prefs);
}
$this->current = $args['current'];
$this->total = $args['total'];
$this->pages = self::paginate_links($args);
if ($this->total <= \count($this->pages)) {
// decrement current so that it matches up with the 0 based index used by the pages array
$current = $this->current - 1;
} else {
// $data['current'] can't be used b/c there are more than 10 pages and we are condensing with dots
foreach ($this->pages as $key => $page) {
if (!empty($page['current'])) {
$current = $key;
break;
}
}
}
// set next and prev using pages array generated by paginate links
if (isset($current) && isset($this->pages[$current + 1]) && isset($this->pages[$current + 1]['link'])) {
$this->next = [
'link' => $this->pages[$current + 1]['link'],
'class' => 'page-numbers next',
];
}
if (isset($current) && isset($this->pages[$current - 1]) && isset($this->pages[$current - 1]['link'])) {
$this->prev = [
'link' => $this->pages[$current - 1]['link'],
'class' => 'page-numbers prev',
];
}
if ($current < 2) {
$this->prev = '';
}
if ($this->total === (float) 0) {
$this->next = '';
}
}
/**
*
*
* @param array $args
* @return array
*/
public static function paginate_links($args = [])
{
$defaults = [
'base' => '%_%',
// http://example.com/all_posts.php%_% : %_% is replaced by format (below)
'format' => '?page=%#%',
// ?page=%#% : %#% is replaced by the page number
'total' => 1,
'current' => 0,
'show_all' => false,
'prev_next' => false,
'prev_text' => \__('« Previous'),
'next_text' => \__('Next »'),
'start_size' => -1,
'end_size' => 1,
'mid_size' => 2,
'type' => 'array',
'add_args' => [],
// array of query args to add
'add_fragment' => '',
];
$args = \wp_parse_args($args, $defaults);
$args = self::sanitize_args($args);
// Who knows what else people pass in $args
$args['total'] = \intval((int) $args['total']);
if ($args['total'] < 2) {
return [];
}
$args['current'] = (int) $args['current'];
$args['end_size'] = 0 <= (int) $args['end_size'] ? (int) $args['end_size'] : 1; // Out of bounds? Make it the default.
$args['start_size'] = 0 <= (int) $args['start_size'] ? (int) $args['start_size'] : $args['end_size']; // Default to end_size for backwards compat
$args['mid_size'] = 0 <= (int) $args['mid_size'] ? (int) $args['mid_size'] : 2;
$args['add_args'] = \is_array($args['add_args']) ? $args['add_args'] : false;
$page_links = [];
$dots = true;
for ($n = 1; $n <= $args['total']; $n++) {
$n_display = \number_format_i18n($n);
if ($n == $args['current']) {
$page_links[] = [
'class' => 'page-number page-numbers current',
'title' => $n_display,
'text' => $n_display,
'name' => $n_display,
'current' => true,
];
$dots = true;
} else {
if (
$args['show_all']
|| (
$n <= (int) $args['start_size']
|| (
$args['current']
&& $n >= (int) $args['current'] - (int) $args['mid_size']
&& $n <= (int) $args['current'] + (int) $args['mid_size']
)
|| $n > (int) $args['total'] - (int) $args['end_size']
)
) {
$link = \str_replace('%_%', 1 == $n ? '' : $args['format'], $args['base']);
$link = \str_replace('%#%', $n, $link);
// we first follow the user trailing slash configuration
$link = URLHelper::user_trailingslashit($link);
// then we add all required querystring parameters
if ($args['add_args']) {
$link = \add_query_arg($args['add_args'], $link);
}
// last, we add fragment if needed
$link .= $args['add_fragment'];
$link = \apply_filters('paginate_links', $link);
$page_links[] = [
'class' => 'page-number page-numbers',
'link' => \esc_url($link),
'title' => $n_display,
'name' => $n_display,
'current' => $args['current'] == $n,
];
$dots = true;
} elseif ($dots && !$args['show_all']) {
$page_links[] = [
'class' => 'dots',
'title' => \__('…'),
];
$dots = false;
}
}
}
return $page_links;
}
protected static function sanitize_url_params($add_args)
{
foreach ($add_args as $key => $value) {
$add_args[$key] = \urlencode_deep($value);
}
return $add_args;
}
protected static function sanitize_args($args)
{
$format_args = [];
$format = \explode('?', \str_replace('%_%', $args['format'], $args['base']));
$format_query = isset($format[1]) ? $format[1] : '';
\wp_parse_str($format_query, $format_args);
// Remove the format argument from the array of query arguments, to avoid overwriting custom format.
foreach ($format_args as $format_arg => $format_arg_value) {
unset($args['add_args'][\urlencode_deep($format_arg)]);
}
$url_parts = \explode('?', $args['base']);
if (isset($url_parts[1])) {
// Find the query args of the requested URL.
$url_query_args = [];
\wp_parse_str($url_parts[1], $url_query_args);
$args['add_args'] = \array_merge($args['add_args'], \urlencode_deep($url_query_args));
$args['base'] = $url_parts[0] . '%_%';
}
if (isset($args['add_args'])) {
$args['add_args'] = self::sanitize_url_params($args['add_args']);
}
return $args;
}
}