Passed
Push — master ( c2a16e...9283b2 )
by Paul
17:15 queued 07:52
created

Pagination::buildLoadMoreButton()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 7
c 0
b 0
f 0
dl 0
loc 9
ccs 0
cts 9
cp 0
rs 10
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Html\Partials;
4
5
use GeminiLabs\SiteReviews\Contracts\PartialContract;
6
use GeminiLabs\SiteReviews\Helper;
7
use GeminiLabs\SiteReviews\Helpers\Arr;
8
use GeminiLabs\SiteReviews\Modules\Html\Template;
9
use GeminiLabs\SiteReviews\Modules\Style;
10
11
class Pagination implements PartialContract
12
{
13
    /**
14
     * @var array
15
     */
16
    protected $args;
17
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function build(array $args = [])
22
    {
23
        $this->args = $this->normalize($args);
24
        if ($this->args['total'] > 1) { // total pages
25
            return 'loadmore' === $this->args['type']
26
                ? $this->buildLoadMoreButton()
27
                : $this->buildPagination();
28
        }
29
    }
30
31
    /**
32
     * @return string|void
33
     */
34
    protected function buildLoadMoreButton()
35
    {
36
        if ($this->args['total'] > $this->args['current']) {
37
            return glsr(Template::class)->build('templates/load-more-button', [
38
                'context' => [
39
                    'loading_text' => __('Loading, please wait...', 'site-reviews'),
40
                    'page' => $this->args['current'] + 1,
41
                    'screen_reader_text' => _x('Load more reviews', 'screen reader text', 'site-reviews'),
42
                    'text' => __('Load more', 'site-reviews'),
43
                ],
44
            ]);
45
        }
46
    }
47
48
    /**
49
     * @return string
50
     */
51
    protected function buildPagination()
52
    {
53
        return glsr(Template::class)->build('templates/pagination', [
54
            'context' => [
55
                'links' => glsr()->filterString('paginate_links', $this->paginatedLinks(), $this->args),
56
                'loader' => '<div class="glsr-loader"><div class="glsr-spinner"></div></div>',
57
                'screen_reader_text' => _x('Site Reviews navigation', 'screen reader text', 'site-reviews'),
58
            ],
59
        ]);
60
    }
61
62
    /**
63
     * @return string
64
     */
65
    protected function paginatedLinks()
66
    {
67
        $links = (array) paginate_links(wp_parse_args(['type' => 'array'], $this->args));
68
        $pattern = '/href=["\']([^"\']*?)["\']/i';
69
        foreach ($links as &$link) {
70
            if (preg_match($pattern, $link, $matches)) {
71
                $hrefTag = Arr::get($matches, 0);
72
                $hrefUrl = Arr::get($matches, 1);
73
                $page = Helper::getPageNumber($hrefUrl);
74
                $replacement = sprintf('%s data-page="%d"', $hrefTag, $page);
75
                $link = str_replace($hrefTag, $replacement, $link);
76
            }
77
        }
78
        return implode("\n", $links);
79
    }
80
81
    /**
82
     * @return array
83
     */
84
    protected function normalize(array $args)
85
    {
86
        if ($baseUrl = Arr::get($args, 'baseUrl')) {
87
            $args['base'] = $baseUrl.'%_%';
88
        }
89
        $args = wp_parse_args(array_filter($args), [
90
            'current' => Helper::getPageNumber(),
91
            'total' => 1,
92
        ]);
93
        return glsr(Style::class)->paginationArgs($args);
94
    }
95
}
96