Passed
Push — master ( 57dde7...525778 )
by Paul
04:33
created

Pagination::normalize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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