|
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
|
|
|
'percentage' => $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 glsr( Builder::class )->span( $backgroundPercent, [ |
|
82
|
|
|
'class' => 'glsr-bar-background', |
|
83
|
|
|
]); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param string $label |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function buildPercentageLabel( $label ) |
|
91
|
|
|
{ |
|
92
|
|
|
return glsr( Builder::class )->span( $label, [ |
|
93
|
|
|
'class' => 'glsr-bar-label', |
|
94
|
|
|
]); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param string $percent |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function buildPercentagePercent( $percent ) |
|
102
|
|
|
{ |
|
103
|
|
|
return glsr( Builder::class )->span( $percent, [ |
|
104
|
|
|
'class' => 'glsr-bar-percent', |
|
105
|
|
|
]); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return void|string |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function buildRating() |
|
112
|
|
|
{ |
|
113
|
|
|
if( $this->isHidden( 'rating' ))return; |
|
114
|
|
|
return $this->wrap( 'rating', '<span>'.$this->rating.'</span>' ); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @return void|string |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function buildStars() |
|
121
|
|
|
{ |
|
122
|
|
|
if( $this->isHidden( 'stars' ))return; |
|
123
|
|
|
$stars = glsr( Partial::class )->build( 'star-rating', [ |
|
124
|
|
|
// 'number' => count( $this->reviews ), |
|
|
|
|
|
|
125
|
|
|
'rating' => $this->rating, |
|
126
|
|
|
]); |
|
127
|
|
|
return $this->wrap( 'stars', $stars ); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @return void|string |
|
132
|
|
|
*/ |
|
133
|
|
|
protected function buildText() |
|
134
|
|
|
{ |
|
135
|
|
|
if( $this->isHidden( 'summary' ))return; |
|
136
|
|
|
$count = count( $this->reviews ); |
|
137
|
|
|
if( empty( $this->args['text'] )) { |
|
138
|
|
|
$this->args['text'] = _nx( |
|
139
|
|
|
'{rating} out of {max} stars (based on %d review)', |
|
140
|
|
|
'{rating} out of {max} stars (based on %d reviews)', |
|
141
|
|
|
$count, |
|
142
|
|
|
'Do not translate {rating} and {max}, they are template tags.', |
|
143
|
|
|
'site-reviews' |
|
144
|
|
|
); |
|
145
|
|
|
} |
|
146
|
|
|
$summary = str_replace( ['{rating}','{max}'], [$this->rating, Rating::MAX_RATING], $this->args['text'] ); |
|
147
|
|
|
$summary = str_replace( ['%s','%d'], $count, $summary ); |
|
148
|
|
|
return $this->wrap( 'text', '<span>'.$summary.'</span>' ); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @return void |
|
153
|
|
|
*/ |
|
154
|
|
|
protected function generateSchema() |
|
155
|
|
|
{ |
|
156
|
|
|
if( !wp_validate_boolean( $this->args['schema'] ))return; |
|
157
|
|
|
glsr( Schema::class )->store( glsr( Schema::class )->buildSummary( $this->args )); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @return string |
|
162
|
|
|
*/ |
|
163
|
|
|
protected function getClass() |
|
164
|
|
|
{ |
|
165
|
|
|
$style = apply_filters( 'site-reviews/style', 'glsr-style' ); |
|
166
|
|
|
return trim( 'glsr-summary '.$style.' '.$this->args['class'] ); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @param string $key |
|
171
|
|
|
* @return bool |
|
172
|
|
|
*/ |
|
173
|
|
|
protected function isHidden( $key ) |
|
174
|
|
|
{ |
|
175
|
|
|
return in_array( $key, $this->args['hide'] ); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @param string $key |
|
180
|
|
|
* @param string $value |
|
181
|
|
|
* @return string |
|
182
|
|
|
*/ |
|
183
|
|
|
protected function wrap( $key, $value ) |
|
184
|
|
|
{ |
|
185
|
|
|
return glsr( Builder::class )->div( $value, [ |
|
186
|
|
|
'class' => 'glsr-summary-'.$key, |
|
187
|
|
|
]); |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.