|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Shortcodes; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Arguments; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Contracts\ShortcodeContract; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Database\PostManager; |
|
8
|
|
|
use GeminiLabs\SiteReviews\Database\TaxonomyManager; |
|
9
|
|
|
use GeminiLabs\SiteReviews\Database\UserManager; |
|
10
|
|
|
use GeminiLabs\SiteReviews\Helper; |
|
11
|
|
|
use GeminiLabs\SiteReviews\Helpers\Cast; |
|
12
|
|
|
use GeminiLabs\SiteReviews\Helpers\Str; |
|
13
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Builder; |
|
14
|
|
|
use GeminiLabs\SiteReviews\Modules\Rating; |
|
15
|
|
|
use GeminiLabs\SiteReviews\Modules\Style; |
|
16
|
|
|
use ReflectionClass; |
|
17
|
|
|
|
|
18
|
|
|
abstract class Shortcode implements ShortcodeContract |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
public $args; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
public $dataAttributes; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
public $shortcode; |
|
34
|
|
|
|
|
35
|
7 |
|
public function __construct() |
|
36
|
|
|
{ |
|
37
|
7 |
|
$this->shortcode = Str::snakeCase($this->getShortClassName()); |
|
38
|
7 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function __get($parameter) |
|
41
|
|
|
{ |
|
42
|
|
|
// @compat provides backwards compatibility with unsupported add-ons |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string|array $atts |
|
47
|
|
|
* @param string $type |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
|
|
public function build($atts, array $args = [], $type = 'shortcode') |
|
51
|
|
|
{ |
|
52
|
|
|
$args = $this->normalizeArgs($args, $type); |
|
53
|
|
|
$atts = $this->normalizeAtts($atts, $type); |
|
54
|
|
|
$template = $this->buildTemplate($atts->toArray()); |
|
55
|
|
|
if (!empty($atts->title)) { |
|
56
|
|
|
$title = $args->before_title.$atts->title.$args->after_title; |
|
57
|
|
|
$atts->title = $title; |
|
58
|
|
|
} |
|
59
|
|
|
$attributes = wp_parse_args($this->dataAttributes, [ |
|
60
|
|
|
'class' => 'glsr glsr-'.glsr(Style::class)->get(), |
|
61
|
|
|
'id' => $atts->id, |
|
62
|
|
|
'text' => $template, |
|
63
|
|
|
]); |
|
64
|
|
|
$html = glsr(Builder::class)->div($attributes); |
|
65
|
|
|
return $args->before_widget.$atts->title.$html.$args->after_widget; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
*/ |
|
71
|
|
|
public function buildBlock($atts = []) |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->build($atts, [], 'block'); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritdoc} |
|
78
|
|
|
*/ |
|
79
|
|
|
public function buildShortcode($atts = []) |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->build($atts, [], 'shortcode'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return string|void |
|
86
|
|
|
*/ |
|
87
|
|
|
public function buildTemplate(array $args = []) |
|
88
|
|
|
{ |
|
89
|
|
|
return; // @todo make this abstract in v6.0 |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return array |
|
94
|
|
|
*/ |
|
95
|
7 |
|
public function getHideOptions() |
|
96
|
|
|
{ |
|
97
|
7 |
|
$options = $this->hideOptions(); |
|
98
|
7 |
|
return glsr()->filterArray('shortcode/hide-options', $options, $this->shortcode, $this); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return string |
|
103
|
|
|
*/ |
|
104
|
7 |
|
public function getShortClassName($replace = '', $search = 'Shortcode') |
|
105
|
|
|
{ |
|
106
|
7 |
|
return str_replace($search, $replace, (new ReflectionClass($this))->getShortName()); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getShortcodeDefaultsClassName() |
|
113
|
|
|
{ |
|
114
|
|
|
$classname = str_replace('Shortcodes\\', 'Defaults\\', get_class($this)); |
|
115
|
|
|
return str_replace('Shortcode', 'Defaults', $classname); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param array|string $args |
|
120
|
|
|
* @param string $type |
|
121
|
|
|
* @return Arguments |
|
122
|
|
|
*/ |
|
123
|
|
|
public function normalizeArgs($args, $type = 'shortcode') |
|
124
|
|
|
{ |
|
125
|
|
|
$args = wp_parse_args($args, [ |
|
126
|
|
|
'before_widget' => '', |
|
127
|
|
|
'after_widget' => '', |
|
128
|
|
|
'before_title' => '<h2 class="glsr-title">', |
|
129
|
|
|
'after_title' => '</h2>', |
|
130
|
|
|
]); |
|
131
|
|
|
$args = glsr()->filterArray('shortcode/args', $args, $type, $this->shortcode); |
|
132
|
|
|
return glsr()->args($args); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param array|string $atts |
|
137
|
|
|
* @param string $type |
|
138
|
|
|
* @return Arguments |
|
139
|
|
|
*/ |
|
140
|
|
|
public function normalizeAtts($atts, $type = 'shortcode') |
|
141
|
|
|
{ |
|
142
|
|
|
$atts = wp_parse_args($atts); |
|
143
|
|
|
$atts = glsr()->filterArray('shortcode/atts', $atts, $type, $this->shortcode); |
|
144
|
|
|
$atts = glsr($this->getShortcodeDefaultsClassName())->unguardedRestrict($atts); |
|
145
|
|
|
$atts = glsr()->args($atts); |
|
146
|
|
|
foreach ($atts as $key => &$value) { |
|
147
|
|
|
$method = Helper::buildMethodName($key, 'normalize'); |
|
148
|
|
|
if (method_exists($this, $method)) { |
|
149
|
|
|
$value = call_user_func([$this, $method], $value, $atts); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
$this->setDataAttributes($atts, $type); |
|
153
|
|
|
return $atts; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @return array |
|
158
|
|
|
*/ |
|
159
|
|
|
abstract protected function hideOptions(); |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @param string $postIds |
|
163
|
|
|
* @return string |
|
164
|
|
|
*/ |
|
165
|
|
|
protected function normalizeAssignedPosts($postIds, Arguments $atts) |
|
166
|
|
|
{ |
|
167
|
|
|
return implode(',', glsr(PostManager::class)->normalizeIds($postIds)); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @param string $termIds |
|
172
|
|
|
* @return string |
|
173
|
|
|
*/ |
|
174
|
|
|
protected function normalizeAssignedTerms($termIds) |
|
175
|
|
|
{ |
|
176
|
|
|
return implode(',', glsr(TaxonomyManager::class)->normalizeIds($termIds)); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param string $userIds |
|
181
|
|
|
* @return string |
|
182
|
|
|
*/ |
|
183
|
|
|
protected function normalizeAssignedUsers($userIds) |
|
184
|
|
|
{ |
|
185
|
|
|
return implode(',', glsr(UserManager::class)->normalizeIds($userIds)); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @param string|array $hide |
|
190
|
|
|
* @return array |
|
191
|
|
|
*/ |
|
192
|
|
|
protected function normalizeHide($hide) |
|
193
|
|
|
{ |
|
194
|
|
|
$hideKeys = array_keys($this->getHideOptions()); |
|
195
|
|
|
return array_filter(Cast::toArray($hide), function ($value) use ($hideKeys) { |
|
196
|
|
|
return in_array($value, $hideKeys); |
|
197
|
|
|
}); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @param string $labels |
|
202
|
|
|
* @return array |
|
203
|
|
|
*/ |
|
204
|
|
|
protected function normalizeLabels($labels) |
|
205
|
|
|
{ |
|
206
|
|
|
$defaults = [ |
|
207
|
|
|
__('Excellent', 'site-reviews'), |
|
208
|
|
|
__('Very good', 'site-reviews'), |
|
209
|
|
|
__('Average', 'site-reviews'), |
|
210
|
|
|
__('Poor', 'site-reviews'), |
|
211
|
|
|
__('Terrible', 'site-reviews'), |
|
212
|
|
|
]; |
|
213
|
|
|
$maxRating = (int) glsr()->constant('MAX_RATING', Rating::class); |
|
214
|
|
|
$defaults = array_pad(array_slice($defaults, 0, $maxRating), $maxRating, ''); |
|
215
|
|
|
$labels = array_map('trim', explode(',', $labels)); |
|
216
|
|
|
foreach ($defaults as $i => $label) { |
|
217
|
|
|
if (!empty($labels[$i])) { |
|
218
|
|
|
$defaults[$i] = $labels[$i]; |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
return array_combine(range($maxRating, 1), $defaults); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* @param string $type |
|
226
|
|
|
* @return void |
|
227
|
|
|
*/ |
|
228
|
|
|
protected function setDataAttributes(Arguments $atts, $type) |
|
229
|
|
|
{ |
|
230
|
|
|
$this->dataAttributes = wp_parse_args( |
|
231
|
|
|
glsr($this->getShortcodeDefaultsClassName())->dataAttributes($atts->toArray()), |
|
232
|
|
|
["data-{$type}" => ''] |
|
233
|
|
|
); |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|