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