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\Rating; |
9
|
|
|
use GeminiLabs\SiteReviews\Modules\Schema; |
10
|
|
|
|
11
|
|
|
class SiteReviewsSummary |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
protected $args; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var float |
20
|
|
|
*/ |
21
|
|
|
protected $rating; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var object |
25
|
|
|
*/ |
26
|
|
|
protected $reviews; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return void|string |
30
|
|
|
*/ |
31
|
|
|
public function build( $name, array $args = [] ) |
32
|
|
|
{ |
33
|
|
|
$this->args = $args; |
34
|
|
|
$this->reviews = glsr( Database::class )->getReviews( $args ); |
35
|
|
|
if( $this->isHidden() )return; |
36
|
|
|
$this->rating = glsr( Rating::class )->getAverage( $this->reviews->results ); |
37
|
|
|
$this->buildSchema(); |
38
|
|
|
return glsr( Builder::class )->div( $this->buildSummary().$this->buildPercentageBars(), [ |
39
|
|
|
'class' => 'glsr-summary-wrap '.$args['class'], |
40
|
|
|
'id' => $args['id'], |
41
|
|
|
]); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param int $index |
46
|
|
|
* @param string $percent |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
protected function buildPercentageBar( $index, $percent ) |
50
|
|
|
{ |
51
|
|
|
$build = glsr( Builder::class ); |
52
|
|
|
$label = $build->span( $this->args['labels'][$index], [ |
53
|
|
|
'class' => 'glsr-bar-label', |
54
|
|
|
]); |
55
|
|
|
$barBackground = $build->span([ |
56
|
|
|
'class' => 'glsr-bar-percent', |
57
|
|
|
'style' => 'width:'.$percent, |
58
|
|
|
]); |
59
|
|
|
$barPercent = $build->span( $barBackground, [ |
60
|
|
|
'class' => 'glsr-bar-background', |
61
|
|
|
]); |
62
|
|
|
$percent = $build->span( $percent, [ |
63
|
|
|
'class' => 'glsr-bar-count', |
64
|
|
|
]); |
65
|
|
|
return $build->div( $label.$barPercent.$percent, [ |
66
|
|
|
'class' => 'glsr-percentage-bar', |
67
|
|
|
]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return void|string |
72
|
|
|
*/ |
73
|
|
|
protected function buildPercentageBars() |
74
|
|
|
{ |
75
|
|
|
if( in_array( 'bars', $this->args['hide'] ))return; |
76
|
|
|
$percentages = preg_filter( '/$/', '%', glsr( Rating::class )->getPercentages( $this->reviews->results )); |
77
|
|
|
$range = range( Rating::MAX_RATING, 1 ); |
78
|
|
|
$bars = array_reduce( $range, function( $carry, $index ) use( $percentages ) { |
79
|
|
|
return $carry.$this->buildPercentageBar( intval( $index ), $percentages[$index] ); |
80
|
|
|
}); |
81
|
|
|
return glsr( Builder::class )->div( $bars, [ |
82
|
|
|
'class' => 'glsr-percentage-bars', |
83
|
|
|
]); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
|
|
protected function buildSchema() |
90
|
|
|
{ |
91
|
|
|
if( !$this->args['schema'] )return; |
92
|
|
|
$schema = glsr( Schema::class ); |
93
|
|
|
$schema->store( $schema->buildSummary( $this->args )); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return void|string |
98
|
|
|
*/ |
99
|
|
|
protected function buildSummary() |
100
|
|
|
{ |
101
|
|
|
$summary = $this->buildSummaryRating().$this->buildSummaryStars().$this->buildSummaryText(); |
102
|
|
|
if( empty( $summary ))return; |
103
|
|
|
return glsr( Builder::class )->div( $summary, [ |
104
|
|
|
'class' => 'glsr-summary', |
105
|
|
|
]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return void|string |
110
|
|
|
*/ |
111
|
|
|
protected function buildSummaryRating() |
112
|
|
|
{ |
113
|
|
|
if( in_array( 'rating', $this->args['hide'] ))return; |
114
|
|
|
return glsr( Builder::class )->span( $this->rating, [ |
115
|
|
|
'class' => 'glsr-summary-rating', |
116
|
|
|
]); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return void|string |
121
|
|
|
*/ |
122
|
|
|
protected function buildSummaryStars() |
123
|
|
|
{ |
124
|
|
|
if( in_array( 'stars', $this->args['hide'] ))return; |
125
|
|
|
$stars = glsr( Html::class )->buildPartial( 'star-rating', [ |
126
|
|
|
'rating' => $this->rating, |
127
|
|
|
]); |
128
|
|
|
return glsr( Builder::class )->span( $stars, [ |
129
|
|
|
'class' => 'glsr-summary-stars', |
130
|
|
|
]); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return void|string |
135
|
|
|
*/ |
136
|
|
|
protected function buildSummaryText() |
137
|
|
|
{ |
138
|
|
|
if( in_array( 'summary', $this->args['hide'] ))return; |
139
|
|
|
$count = count( $this->reviews->results ); |
140
|
|
|
if( empty( $this->args['text'] )) { |
141
|
|
|
$this->args['text'] = _nx( |
142
|
|
|
'{rating} out of {max} stars (based on %d review)', |
143
|
|
|
'{rating} out of {max} stars (based on %d reviews)', |
144
|
|
|
$count, |
145
|
|
|
'Do not translate {rating} and {max}, they are template tags.', |
146
|
|
|
'site-reviews' |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
$summary = str_replace( |
150
|
|
|
['{rating}','{max}'], [$this->rating, Rating::MAX_RATING], $this->args['text'] |
151
|
|
|
); |
152
|
|
|
return str_replace( ['%s','%d'], $count, $summary ); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return bool |
157
|
|
|
*/ |
158
|
|
|
protected function isHidden() |
159
|
|
|
{ |
160
|
|
|
return empty( $this->reviews->results ) && in_array( 'if_empty', $this->args['hide'] ); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|