|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules\Html\Partials; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Contracts\PartialContract; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Database\OptionManager; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Database\QueryBuilder; |
|
8
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
|
9
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Template; |
|
10
|
|
|
use GeminiLabs\SiteReviews\Modules\Style; |
|
11
|
|
|
|
|
12
|
|
|
class Pagination implements PartialContract |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $args; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @return string |
|
21
|
|
|
*/ |
|
22
|
|
|
public function build(array $args = []) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->args = $this->normalize($args); |
|
25
|
|
|
if ($this->args['total'] < 2) { |
|
26
|
|
|
return ''; |
|
27
|
|
|
} |
|
28
|
|
|
return glsr(Template::class)->build('templates/pagination', [ |
|
29
|
|
|
'context' => [ |
|
30
|
|
|
'links' => apply_filters('site-reviews/paginate_links', $this->buildLinks(), $this->args), |
|
31
|
|
|
'loader' => '<div class="glsr-loader"></div>', |
|
32
|
|
|
'screen_reader_text' => __('Site Reviews navigation', 'site-reviews'), |
|
33
|
|
|
], |
|
34
|
|
|
]); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected function buildFauxLinks() |
|
41
|
|
|
{ |
|
42
|
|
|
$links = paginate_links(wp_parse_args(['type' => 'array'], $this->args)); |
|
43
|
|
|
$pattern = '/(href=["\'])([^"\']*?)(["\'])/i'; |
|
44
|
|
|
foreach ($links as &$link) { |
|
45
|
|
|
if (!preg_match($pattern, $link, $matches)) { |
|
46
|
|
|
continue; |
|
47
|
|
|
} |
|
48
|
|
|
parse_str(parse_url(Arr::get($matches, 2), PHP_URL_QUERY), $urlQuery); |
|
49
|
|
|
$page = (int) Arr::get($urlQuery, glsr()->constant('PAGED_QUERY_VAR'), 1); |
|
50
|
|
|
$replacement = sprintf('data-page="%d" href="#"', $page); |
|
51
|
|
|
$link = str_replace(Arr::get($matches, 0), $replacement, $link); |
|
52
|
|
|
} |
|
53
|
|
|
return implode("\n", $links); |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function buildLinks() |
|
60
|
|
|
{ |
|
61
|
|
|
return glsr(OptionManager::class)->getBool('settings.reviews.pagination.url_parameter') |
|
|
|
|
|
|
62
|
|
|
? paginate_links($this->args) |
|
63
|
|
|
: $this->buildFauxLinks(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return array |
|
68
|
|
|
*/ |
|
69
|
|
|
protected function normalize(array $args) |
|
70
|
|
|
{ |
|
71
|
|
|
if ($baseUrl = Arr::get($args, 'baseUrl')) { |
|
72
|
|
|
$args['base'] = $baseUrl.'%_%'; |
|
73
|
|
|
} |
|
74
|
|
|
$args = wp_parse_args(array_filter($args), [ |
|
75
|
|
|
'current' => glsr(QueryBuilder::class)->getPaged(), |
|
76
|
|
|
'total' => 1, |
|
77
|
|
|
]); |
|
78
|
|
|
$args = glsr(Style::class)->paginationArgs($args); |
|
79
|
|
|
if ('array' == $args['type']) { |
|
80
|
|
|
$args['type'] = 'plain'; |
|
81
|
|
|
} |
|
82
|
|
|
return $args; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|