Passed
Push — master ( 974b46...adbe27 )
by Paul
04:40
created

Pagination::buildLinks()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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