Passed
Push — hotfix/fix-counts ( 673622...5fa6b5 )
by Paul
05:31
created

ReviewsHtml::offsetGet()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 8
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 11
ccs 0
cts 11
cp 0
crap 20
rs 10
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\Html;
4
5
use ArrayObject;
6
use GeminiLabs\SiteReviews\Database\OptionManager;
7
use GeminiLabs\SiteReviews\Modules\Html\Partial;
8
use GeminiLabs\SiteReviews\Modules\Html\Template;
9
use GeminiLabs\SiteReviews\Reviews;
10
11
class ReviewsHtml extends ArrayObject
12
{
13
	/**
14
	 * @var array
15
	 */
16
	public $args;
17
18
	/**
19
	 * @var int
20
	 */
21
	public $max_num_pages;
22
23
	/**
24
	 * @var string
25
	 */
26
	public $pagination;
27
28
	/**
29
	 * @var array
30
	 */
31
	public $reviews;
32
33
	public function __construct( array $reviews, $maxPageCount, array $args )
34
	{
35
		$this->args = $args;
36
		$this->max_num_pages = $maxPageCount;
37
		$this->reviews = $reviews;
38
		$this->pagination = $this->buildPagination();
39
		parent::__construct( $reviews, ArrayObject::STD_PROP_LIST|ArrayObject::ARRAY_AS_PROPS );
40
	}
41
42
	/**
43
	 * @return string
44
	 */
45
	public function __toString()
46
	{
47
		return glsr( Template::class )->build( 'templates/reviews', [
48
			'args' => $this->args,
49
			'context' => [
50
				'assigned_to' => $this->args['assigned_to'],
51
				'category' => $this->args['category'],
52
				'class' => $this->getClass(),
53
				'id' => $this->args['id'],
54
				'pagination' => $this->getPagination(),
55
				'reviews' => $this->getReviews(),
56
			],
57
		]);
58
	}
59
60
	/**
61
	 * @return string
62
	 */
63
	public function getPagination()
64
	{
65
		return wp_validate_boolean( $this->args['pagination'] )
66
			? $this->pagination
67
			: '';
68
	}
69
70
	/**
71
	 * @return string
72
	 */
73
	public function getReviews()
74
	{
75
		$html = empty( $this->reviews )
76
			? $this->getReviewsFallback()
77
			: implode( PHP_EOL, $this->reviews );
78
		$wrapper = '<div class="glsr-reviews">%s</div>';
79
		$wrapper = apply_filters( 'site-reviews/reviews/reviews-wrapper', $wrapper );
80
		return sprintf( $wrapper, $html );
81
	}
82
83
	/**
84
	 * @param mixed $key
85
	 * @return mixed
86
	 */
87
	public function offsetGet( $key ) {
88
		if( $key == 'navigation' ) {
89
			glsr()->deprecated[] = 'The $reviewsHtml->navigation property has been been deprecated. Please use the $reviewsHtml->pagination property instead.';
90
			return $this->pagination;
91
		}
92
		if( property_exists( $this, $key )) {
93
			return $this->$key;
94
		}
95
		return array_key_exists( $key, $this->reviews )
96
			? $this->reviews[$key]
97
			: null;
98
	}
99
100
	/**
101
	 * @return string
102
	 */
103
	protected function buildPagination()
104
	{
105
		$html = glsr( Partial::class )->build( 'pagination', [
106
			'baseUrl' => glsr_get( $this->args, 'pagedUrl' ),
107
			'current' => glsr_get( $this->args, 'paged' ),
108
			'total' => $this->max_num_pages,
109
		]);
110
		$html.= sprintf( '<glsr-pagination hidden data-atts=\'%s\'></glsr-pagination>', $this->args['json'] );
111
		$wrapper = '<div class="glsr-pagination">%s</div>';
112
		$wrapper = apply_filters( 'site-reviews/reviews/pagination-wrapper', $wrapper );
113
		return sprintf( $wrapper, $html );
114
	}
115
116
	/**
117
	 * @return string
118
	 */
119
	protected function getClass()
120
	{
121
		$defaults = [
122
			'glsr-default',
123
		];
124
		if( $this->args['pagination'] == 'ajax' ) {
125
			$defaults[] = 'glsr-ajax-pagination';
126
		}
127
		$classes = explode( ' ', $this->args['class'] );
128
		$classes = array_unique( array_merge( $defaults, array_filter( $classes )));
129
		return implode( ' ', $classes );
130
	}
131
132
	/**
133
	 * @return string
134
	 */
135
	protected function getReviewsFallback()
136
	{
137
		if( empty( $this->args['fallback'] ) && glsr( OptionManager::class )->getBool( 'settings.reviews.fallback' )) {
138
			$this->args['fallback'] = __( 'There are no reviews yet. Be the first one to write one.', 'site-reviews' );
139
		}
140
		$fallback = '<p class="glsr-no-margins">'.$this->args['fallback'].'</p>';
141
		return apply_filters( 'site-reviews/reviews/fallback', $fallback, $this->args );
142
	}
143
}
144