|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Shortcodes; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Database\RatingManager; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Helper; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Helpers\Cast; |
|
8
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Template; |
|
9
|
|
|
use GeminiLabs\SiteReviews\Modules\Schema; |
|
10
|
|
|
|
|
11
|
|
|
class SiteReviewsSummaryShortcode extends Shortcode |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var array |
|
15
|
|
|
*/ |
|
16
|
|
|
public $args; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $ratings; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* {@inheritdoc} |
|
25
|
|
|
*/ |
|
26
|
|
|
public function buildTemplate(array $args = []) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->args = $args; |
|
29
|
|
|
$this->ratings = glsr(RatingManager::class)->ratings($args); |
|
30
|
|
|
if ($this->isEmpty()) { |
|
31
|
|
|
return; |
|
32
|
|
|
} |
|
33
|
|
|
$this->generateSchema(); |
|
34
|
|
|
return glsr(Template::class)->build('templates/reviews-summary', [ |
|
35
|
|
|
'args' => $this->args, |
|
36
|
|
|
'context' => [ |
|
37
|
|
|
'class' => $this->getClasses(), |
|
38
|
|
|
'id' => '', // @deprecated in v5.0 |
|
39
|
|
|
'percentages' => $this->buildTemplateTag('percentages'), |
|
40
|
|
|
'rating' => $this->buildTemplateTag('rating'), |
|
41
|
|
|
'stars' => $this->buildTemplateTag('stars'), |
|
42
|
|
|
'text' => $this->buildTemplateTag('text'), |
|
43
|
|
|
], |
|
44
|
|
|
]); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $tag |
|
49
|
|
|
* @return string |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function buildTemplateTag($tag) |
|
52
|
|
|
{ |
|
53
|
|
|
$args = $this->args; |
|
54
|
|
|
$classname = implode('-', ['summary', $tag, 'tag']); |
|
55
|
|
|
$className = Helper::buildClassName($classname, 'Modules\Html\Tags'); |
|
56
|
|
|
$field = class_exists($className) |
|
57
|
|
|
? glsr($className, compact('tag', 'args'))->handleFor('summary', null, $this->ratings) |
|
58
|
|
|
: null; |
|
59
|
|
|
return glsr()->filterString('summary/build/'.$tag, $field, $this->ratings, $this); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return void |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function generateSchema() |
|
66
|
|
|
{ |
|
67
|
|
|
if (Cast::toBool($this->args['schema'])) { |
|
68
|
|
|
glsr(Schema::class)->store( |
|
69
|
|
|
glsr(Schema::class)->buildSummary($this->args, $this->ratings) |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function getClasses() |
|
78
|
|
|
{ |
|
79
|
|
|
return trim('glsr-summary '.$this->args['class']); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return array |
|
84
|
|
|
*/ |
|
85
|
7 |
|
protected function hideOptions() |
|
86
|
|
|
{ |
|
87
|
|
|
return [ |
|
88
|
7 |
|
'rating' => _x('Hide the rating', 'admin-text', 'site-reviews'), |
|
89
|
7 |
|
'stars' => _x('Hide the stars', 'admin-text', 'site-reviews'), |
|
90
|
7 |
|
'summary' => _x('Hide the summary', 'admin-text', 'site-reviews'), |
|
91
|
7 |
|
'bars' => _x('Hide the percentage bars', 'admin-text', 'site-reviews'), |
|
92
|
7 |
|
'if_empty' => _x('Hide if no reviews are found', 'admin-text', 'site-reviews'), |
|
93
|
|
|
]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return bool |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function isEmpty() |
|
100
|
|
|
{ |
|
101
|
|
|
return !array_sum($this->ratings) && in_array('if_empty', $this->args['hide']); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|