1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules\Html\Partials; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Contracts\PartialContract; |
6
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Template; |
7
|
|
|
use GeminiLabs\SiteReviews\Modules\Rating; |
8
|
|
|
|
9
|
|
|
class StarRating implements PartialContract |
10
|
|
|
{ |
11
|
|
|
protected $count; |
12
|
|
|
protected $prefix; |
13
|
|
|
protected $rating; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* {@inheritdoc} |
17
|
|
|
*/ |
18
|
6 |
|
public function build(array $args = []) |
19
|
|
|
{ |
20
|
6 |
|
$this->setProperties($args); |
21
|
6 |
|
$maxRating = glsr()->constant('MAX_RATING', Rating::class); |
22
|
6 |
|
$fullStars = intval(floor($this->rating)); |
23
|
6 |
|
$halfStars = intval(ceil($this->rating - $fullStars)); |
24
|
6 |
|
$emptyStars = max(0, $maxRating - $fullStars - $halfStars); |
25
|
6 |
|
$title = $this->count > 0 |
26
|
|
|
? __('Rated <strong>%s</strong> out of %s based on %s ratings', 'site-reviews') |
27
|
6 |
|
: __('Rated <strong>%s</strong> out of %s', 'site-reviews'); |
28
|
6 |
|
return glsr(Template::class)->build('templates/rating/stars', [ |
29
|
|
|
'context' => [ |
30
|
6 |
|
'empty_stars' => $this->getTemplate('empty-star', $emptyStars), |
31
|
6 |
|
'full_stars' => $this->getTemplate('full-star', $fullStars), |
32
|
6 |
|
'half_stars' => $this->getTemplate('half-star', $halfStars), |
33
|
6 |
|
'prefix' => $this->prefix, |
34
|
6 |
|
'title' => sprintf($title, $this->rating, $maxRating, $this->count), |
35
|
|
|
], |
36
|
|
|
]); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $templateName |
41
|
|
|
* @param int $timesRepeated |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
6 |
|
protected function getTemplate($templateName, $timesRepeated) |
45
|
|
|
{ |
46
|
6 |
|
$template = glsr(Template::class)->build('templates/rating/'.$templateName, [ |
47
|
|
|
'context' => [ |
48
|
6 |
|
'prefix' => $this->prefix, |
49
|
|
|
], |
50
|
|
|
]); |
51
|
6 |
|
return str_repeat($template, $timesRepeated); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
6 |
|
protected function setProperties(array $args) |
58
|
|
|
{ |
59
|
6 |
|
$args = wp_parse_args($args, [ |
60
|
6 |
|
'count' => 0, |
61
|
6 |
|
'prefix' => glsr()->isAdmin() ? '' : 'glsr-', |
62
|
6 |
|
'rating' => 0, |
63
|
|
|
]); |
64
|
6 |
|
$this->count = (int) $args['count']; |
65
|
6 |
|
$this->prefix = $args['prefix']; |
66
|
6 |
|
$this->rating = (float) sprintf('%g', $args['rating']); // remove unnecessary trailing zeros |
67
|
6 |
|
} |
68
|
|
|
} |
69
|
|
|
|