-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Pagi.php
90 lines (77 loc) · 1.72 KB
/
Pagi.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
<?php
namespace Log1x\Pagi;
use Illuminate\Support\Arr;
class Pagi
{
/**
* The current WP_Query.
*
* @var \WP_Query
*/
protected $query;
/**
* Collection of pagination items.
*
* @var \Illuminate\Support\Collection
*/
protected $items;
/**
* The current page.
*
* @var int
*/
protected $currentPage = 1;
/**
* Items shown per page.
*
* @var int
*/
protected $perPage = 1;
/**
* Prepare the WordPress pagination.
*
* @return void
*/
protected function prepare()
{
if (! isset($this->query)) {
$this->query = collect(
Arr::get($GLOBALS, 'wp_query')->query_vars ?? []
)->filter();
$this->items = collect()->range(1, Arr::get($GLOBALS, 'wp_query')->found_posts);
}
if ($this->query->isEmpty()) {
return;
}
$this->perPage = $this->query->get('posts_per_page', 1);
$this->currentPage = max(1, absint(get_query_var('paged')));
}
/**
* Build the WordPress pagination.
*
* @return \Illuminate\Pagination\LengthAwarePaginator
*/
public function build()
{
$this->prepare();
return new LengthAwarePaginator(
$this->items,
$this->items->count(),
$this->perPage,
$this->currentPage
);
}
/**
* Set the WordPress query.
*
* @param \WP_Query
* @return void
*/
public function setQuery($query)
{
$this->items = collect()->range(1, $query->found_posts);
$this->query = collect(
$query->query_vars ?? []
)->filter();
}
}