|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules\Html\Partials; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Database; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Modules\Html; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Builder; |
|
8
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Template; |
|
9
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Partial; |
|
10
|
|
|
use GeminiLabs\SiteReviews\Modules\Rating; |
|
11
|
|
|
use GeminiLabs\SiteReviews\Modules\Schema; |
|
12
|
|
|
|
|
13
|
|
|
class SiteReviewsSummary |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $args; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var float |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $rating; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var object |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $reviews; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return void|string |
|
32
|
|
|
*/ |
|
33
|
|
|
public function build( array $args = [] ) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->args = $args; |
|
36
|
|
|
$this->reviews = glsr( Database::class )->getReviews( $args )->results; |
|
37
|
|
|
if( empty( $this->reviews ) && $this->isHidden( 'if_empty' ))return; |
|
38
|
|
|
$this->rating = glsr( Rating::class )->getAverage( $this->reviews ); |
|
39
|
|
|
$this->generateSchema(); |
|
40
|
|
|
return glsr( Template::class )->build( 'templates/reviews-summary', [ |
|
41
|
|
|
'context' => [ |
|
42
|
|
|
'class' => $this->getClass(), |
|
43
|
|
|
'id' => $this->args['id'], |
|
44
|
|
|
'percentages' => $this->buildPercentage(), |
|
45
|
|
|
'rating' => $this->buildRating(), |
|
46
|
|
|
'stars' => $this->buildStars(), |
|
47
|
|
|
'text' => $this->buildText(), |
|
48
|
|
|
], |
|
49
|
|
|
]); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return void|string |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function buildPercentage() |
|
56
|
|
|
{ |
|
57
|
|
|
if( $this->isHidden( 'bars' ))return; |
|
58
|
|
|
$range = range( Rating::MAX_RATING, 1 ); |
|
59
|
|
|
$percentages = preg_filter( '/$/', '%', glsr( Rating::class )->getPercentages( $this->reviews )); |
|
60
|
|
|
$bars = array_reduce( $range, function( $carry, $level ) use( $percentages ) { |
|
61
|
|
|
$label = $this->buildPercentageLabel( $this->args['labels'][$level] ); |
|
62
|
|
|
$background = $this->buildPercentageBackground( $percentages[$level] ); |
|
63
|
|
|
$percent = $this->buildPercentagePercent( $percentages[$level] ); |
|
64
|
|
|
return $carry.glsr( Builder::class )->div( $label.$background.$percent, [ |
|
65
|
|
|
'class' => 'glsr-bar', |
|
66
|
|
|
]); |
|
67
|
|
|
}); |
|
68
|
|
|
return $this->wrap( 'percentage', $bars ); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param string $percent |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function buildPercentageBackground( $percent ) |
|
76
|
|
|
{ |
|
77
|
|
|
$backgroundPercent = glsr( Builder::class )->span([ |
|
78
|
|
|
'class' => 'glsr-bar-background-percent', |
|
79
|
|
|
'style' => 'width:'.$percent, |
|
80
|
|
|
]); |
|
81
|
|
|
return '<span class="glsr-bar-background">'.$backgroundPercent.'</span>'; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param string $label |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function buildPercentageLabel( $label ) |
|
89
|
|
|
{ |
|
90
|
|
|
return '<span class="glsr-bar-label">'.$label.'</span>'; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param string $percent |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function buildPercentagePercent( $percent ) |
|
98
|
|
|
{ |
|
99
|
|
|
return '<span class="glsr-bar-percent">'.$percent.'</span>'; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return void|string |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function buildRating() |
|
106
|
|
|
{ |
|
107
|
|
|
if( $this->isHidden( 'rating' ))return; |
|
108
|
|
|
return $this->wrap( 'rating', '<span>'.$this->rating.'</span>' ); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return void|string |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function buildStars() |
|
115
|
|
|
{ |
|
116
|
|
|
if( $this->isHidden( 'stars' ))return; |
|
117
|
|
|
$stars = glsr( Partial::class )->build( 'star-rating', [ |
|
118
|
|
|
'rating' => $this->rating, |
|
119
|
|
|
]); |
|
120
|
|
|
return $this->wrap( 'stars', $stars ); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @return void|string |
|
125
|
|
|
*/ |
|
126
|
|
|
protected function buildText() |
|
127
|
|
|
{ |
|
128
|
|
|
if( $this->isHidden( 'summary' ))return; |
|
129
|
|
|
$count = count( $this->reviews ); |
|
130
|
|
|
if( empty( $this->args['text'] )) { |
|
131
|
|
|
$this->args['text'] = _nx( |
|
132
|
|
|
'{rating} out of {max} stars (based on %d review)', |
|
133
|
|
|
'{rating} out of {max} stars (based on %d reviews)', |
|
134
|
|
|
$count, |
|
135
|
|
|
'Do not translate {rating} and {max}, they are template tags.', |
|
136
|
|
|
'site-reviews' |
|
137
|
|
|
); |
|
138
|
|
|
} |
|
139
|
|
|
$summary = str_replace( ['{rating}','{max}'], [$this->rating, Rating::MAX_RATING], $this->args['text'] ); |
|
140
|
|
|
$summary = str_replace( ['%s','%d'], $count, $summary ); |
|
141
|
|
|
return $this->wrap( 'text', '<span>'.$summary.'</span>' ); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @return void |
|
146
|
|
|
*/ |
|
147
|
|
|
protected function generateSchema() |
|
148
|
|
|
{ |
|
149
|
|
|
if( !wp_validate_boolean( $this->args['schema'] ))return; |
|
150
|
|
|
glsr( Schema::class )->store( |
|
151
|
|
|
glsr( Schema::class )->buildSummary( $this->args ) |
|
152
|
|
|
); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @return string |
|
157
|
|
|
*/ |
|
158
|
|
|
protected function getClass() |
|
159
|
|
|
{ |
|
160
|
|
|
$style = apply_filters( 'site-reviews/style', 'glsr-style' ); |
|
161
|
|
|
return trim( 'glsr-summary '.$style.' '.$this->args['class'] ); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param string $key |
|
166
|
|
|
* @return bool |
|
167
|
|
|
*/ |
|
168
|
|
|
protected function isHidden( $key ) |
|
169
|
|
|
{ |
|
170
|
|
|
return in_array( $key, $this->args['hide'] ); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @param string $key |
|
175
|
|
|
* @param string $value |
|
176
|
|
|
* @return string |
|
177
|
|
|
*/ |
|
178
|
|
|
protected function wrap( $key, $value ) |
|
179
|
|
|
{ |
|
180
|
|
|
return glsr( Builder::class )->div( $value, [ |
|
181
|
|
|
'class' => 'glsr-summary-'.$key, |
|
182
|
|
|
]); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|