1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules\Html\Partials; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Application; |
6
|
|
|
use GeminiLabs\SiteReviews\Contracts\PartialContract; |
7
|
|
|
use GeminiLabs\SiteReviews\Database\QueryBuilder; |
8
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Builder; |
9
|
|
|
|
10
|
|
|
class Pagination implements PartialContract |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
protected $args; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @return void|string |
19
|
|
|
*/ |
20
|
|
|
public function build( array $args = [] ) |
21
|
|
|
{ |
22
|
|
|
$this->args = $this->normalize( $args ); |
23
|
|
|
if( $this->args['total'] < 2 )return; |
24
|
|
|
$links = $this->buildLinksForDeprecatedThemes(); |
25
|
|
|
if( empty( $links )) { |
26
|
|
|
$links = $this->buildLinks(); |
27
|
|
|
} |
28
|
|
|
$links = apply_filters( 'site-reviews/reviews/navigation_links', $links, $this->args ); |
29
|
|
|
if( empty( $links ))return; |
30
|
|
|
return $this->buildTemplate( $links ); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
protected function buildLinks() |
37
|
|
|
{ |
38
|
|
|
$paginateArgs = [ |
39
|
|
|
'before_page_number' => '<span class="meta-nav screen-reader-text">'.__( 'Page', 'site-reviews' ).' </span>', |
40
|
|
|
'current' => $this->args['paged'], |
41
|
|
|
'format' => '?'.Application::PAGED_QUERY_VAR.'=%#%', |
42
|
|
|
'mid_size' => 1, |
43
|
|
|
'next_text' => __( 'Next →', 'site-reviews' ), |
44
|
|
|
'prev_text' => __( '← Previous', 'site-reviews' ), |
45
|
|
|
'total' => $this->args['total'], |
46
|
|
|
]; |
47
|
|
|
if( is_front_page() ) { |
48
|
|
|
unset( $paginateArgs['format'] ); |
49
|
|
|
} |
50
|
|
|
return paginate_links( $paginateArgs ); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return void|string |
55
|
|
|
*/ |
56
|
|
|
protected function buildLinksForDeprecatedThemes() |
57
|
|
|
{ |
58
|
|
|
$theme = wp_get_theme()->get( 'TextDomain' ); |
59
|
|
|
if( !in_array( $theme, ['twentyten','twentyeleven','twentytwelve','twentythirteen'] ))return; |
60
|
|
|
$links = ''; |
61
|
|
|
if( $this->args['paged'] > 1 ) { |
62
|
|
|
$links.= sprintf( '<div class="nav-previous"><a href="%s"><span class="meta-nav">←</span> %s</a></div>', |
63
|
|
|
$this->buildUrlForDeprecatedThemes(-1), |
64
|
|
|
__( 'Previous', 'site-reviews' ) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
if( $this->args['paged'] < $this->args['total'] ) { |
68
|
|
|
$links.= sprintf( '<div class="nav-next"><a href="%s">%s <span class="meta-nav">→</span></a></div>', |
69
|
|
|
$this->buildUrlForDeprecatedThemes(1), |
70
|
|
|
__( 'Next', 'site-reviews' ) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
return $links; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $links |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
protected function buildTemplate( $links ) |
81
|
|
|
{ |
82
|
|
|
$theme = wp_get_theme()->get( 'TextDomain' ); |
83
|
|
|
$class = 'navigation pagination'; |
84
|
|
|
$screenReaderTemplate = '<h2 class="screen-reader-text">%2$s</h2>'; |
85
|
|
|
$screenReaderText = __( 'Site Reviews navigation', 'site-reviews' ); |
86
|
|
|
$innerTemplate = $screenReaderTemplate.'<div class="nav-links">%3$s</div>'; |
87
|
|
|
if( in_array( $theme, ['twentyten', 'twentyeleven', 'twentytwelve'] )) { |
88
|
|
|
$innerTemplate = '%3$s'; |
89
|
|
|
} |
90
|
|
|
else if( $theme == 'twentyfourteen' ) { |
91
|
|
|
$class = str_replace( 'pagination', 'paging-navigation', $class ); |
92
|
|
|
$innerTemplate = $screenReaderTemplate.'<div class="pagination loop-pagination">%3$s</div>'; |
93
|
|
|
} |
94
|
|
|
$template = '<nav class="%1$s" role="navigation">'.$innerTemplate.'</nav>'; |
95
|
|
|
$template = apply_filters( 'navigation_markup_template', $template, $class ); |
96
|
|
|
$template = sprintf( $template, $class, $screenReaderText, $links ); |
97
|
|
|
return glsr( Builder::class )->div( $template.'<div class="glsr-loader"></div>', [ |
98
|
|
|
'class' => 'glsr-navigation', |
99
|
|
|
]) |
100
|
|
|
} |
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param int $pageIncrement |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
|
|
protected function buildUrlForDeprecatedThemes( $pageIncrement ) |
107
|
|
|
{ |
108
|
|
|
if( is_front_page() ) { |
109
|
|
|
return get_pagenum_link( $this->args['paged'] + $pageIncrement ); |
110
|
|
|
} |
111
|
|
|
return add_query_arg( Application::PAGED_QUERY_VAR, $this->args['paged'] + $pageIncrement, get_pagenum_link() ); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
protected function normalize( array $args ) |
118
|
|
|
{ |
119
|
|
|
return wp_parse_args( $args, [ |
120
|
|
|
'paged' => glsr( QueryBuilder::class )->getPaged(), |
121
|
|
|
'total' => 1, |
122
|
|
|
]); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|