1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Shortcodes; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Contracts\ShortcodeContract; |
6
|
|
|
use GeminiLabs\SiteReviews\Helper; |
7
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Partial; |
8
|
|
|
use GeminiLabs\SiteReviews\Modules\Rating; |
9
|
|
|
use ReflectionClass; |
10
|
|
|
|
11
|
|
|
abstract class Shortcode implements ShortcodeContract |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $partialName; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $shortcodeName; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string|array $atts |
25
|
|
|
* @param string $type |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
|
|
public function build($atts, array $args = [], $type = 'shortcode') |
29
|
|
|
{ |
30
|
|
|
$this->partialName = $this->getShortcodePartialName(); |
31
|
|
|
$this->shortcodeName = $this->getShortcodeName(); |
32
|
|
|
$args = $this->normalizeArgs($args, $type); |
33
|
|
|
$atts = $this->normalizeAtts($atts, $type); |
34
|
|
|
$partial = glsr(Partial::class)->build($this->partialName, $atts); |
35
|
|
|
$title = !empty($atts['title']) |
36
|
|
|
? $args['before_title'].$atts['title'].$args['after_title'] |
37
|
|
|
: ''; |
38
|
|
|
$debug = sprintf('<glsr-%1$s hidden data-atts=\'%2$s\'></glsr-%1$s>', $type, $atts['json']); |
39
|
|
|
return $args['before_widget'].$title.$partial.$debug.$args['after_widget']; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string|array $atts |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
public function buildShortcode($atts = []) |
47
|
|
|
{ |
48
|
|
|
return $this->build($atts); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
public function getDefaults($atts) |
55
|
|
|
{ |
56
|
|
|
return glsr($this->getShortcodeDefaultsClassName())->restrict(wp_parse_args($atts)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
1 |
|
public function getHideOptions() |
63
|
|
|
{ |
64
|
1 |
|
$options = $this->hideOptions(); |
65
|
1 |
|
return apply_filters('site-reviews/shortcode/hide-options', $options, $this->shortcodeName); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function getShortcodeClassName($replace = '', $search = 'Shortcode') |
72
|
|
|
{ |
73
|
|
|
return str_replace($search, $replace, (new ReflectionClass($this))->getShortName()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function getShortcodeDefaultsClassName() |
80
|
|
|
{ |
81
|
|
|
return glsr(Helper::class)->buildClassName( |
82
|
|
|
$this->getShortcodeClassName('Defaults'), |
83
|
|
|
'Defaults' |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function getShortcodeName() |
91
|
|
|
{ |
92
|
|
|
return glsr(Helper::class)->snakeCase($this->getShortcodeClassName()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function getShortcodePartialName() |
99
|
|
|
{ |
100
|
|
|
return glsr(Helper::class)->dashCase($this->getShortcodeClassName()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param array|string $args |
105
|
|
|
* @param string $type |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
public function normalizeArgs($args, $type = 'shortcode') |
109
|
|
|
{ |
110
|
|
|
$args = wp_parse_args($args, [ |
111
|
|
|
'before_widget' => '<div class="glsr-'.$type.' '.$type.'-'.$this->partialName.'">', |
112
|
|
|
'after_widget' => '</div>', |
113
|
|
|
'before_title' => '<h3 class="glsr-'.$type.'-title">', |
114
|
|
|
'after_title' => '</h3>', |
115
|
|
|
]); |
116
|
|
|
return apply_filters('site-reviews/shortcode/args', $args, $type, $this->partialName); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param array|string $atts |
121
|
|
|
* @param string $type |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
|
|
public function normalizeAtts($atts, $type = 'shortcode') |
125
|
|
|
{ |
126
|
|
|
$atts = apply_filters('site-reviews/shortcode/atts', $atts, $type, $this->partialName); |
127
|
|
|
$atts = $this->getDefaults($atts); |
128
|
|
|
array_walk($atts, function (&$value, $key) { |
129
|
|
|
$methodName = glsr(Helper::class)->buildMethodName($key, 'normalize'); |
130
|
|
|
if (!method_exists($this, $methodName)) { |
131
|
|
|
return; |
132
|
|
|
} |
133
|
|
|
$value = $this->$methodName($value); |
134
|
|
|
}); |
135
|
|
|
return $atts; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
|
|
abstract protected function hideOptions(); |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param string $postId |
145
|
|
|
* @return int|string |
146
|
|
|
*/ |
147
|
|
|
protected function normalizeAssignedTo($postId) |
148
|
|
|
{ |
149
|
|
|
if ('parent_id' == $postId) { |
150
|
|
|
$postId = intval(wp_get_post_parent_id(intval(get_the_ID()))); |
151
|
|
|
} elseif ('post_id' == $postId) { |
152
|
|
|
$postId = intval(get_the_ID()); |
153
|
|
|
} |
154
|
|
|
return $postId; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param string $postId |
159
|
|
|
* @return int|string |
160
|
|
|
*/ |
161
|
|
|
protected function normalizeAssignTo($postId) |
162
|
|
|
{ |
163
|
|
|
return $this->normalizeAssignedTo($postId); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param string|array $hide |
168
|
|
|
* @return array |
169
|
|
|
*/ |
170
|
|
|
protected function normalizeHide($hide) |
171
|
|
|
{ |
172
|
|
|
if (is_string($hide)) { |
173
|
|
|
$hide = explode(',', $hide); |
174
|
|
|
} |
175
|
|
|
$hideKeys = array_keys($this->getHideOptions()); |
176
|
|
|
return array_filter(array_map('trim', $hide), function ($value) use ($hideKeys) { |
177
|
|
|
return in_array($value, $hideKeys); |
178
|
|
|
}); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param string $id |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
|
|
protected function normalizeId($id) |
186
|
|
|
{ |
187
|
|
|
return sanitize_title($id); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param string $labels |
192
|
|
|
* @return array |
193
|
|
|
*/ |
194
|
|
|
protected function normalizeLabels($labels) |
195
|
|
|
{ |
196
|
|
|
$defaults = [ |
197
|
|
|
__('Excellent', 'site-reviews'), |
198
|
|
|
__('Very good', 'site-reviews'), |
199
|
|
|
__('Average', 'site-reviews'), |
200
|
|
|
__('Poor', 'site-reviews'), |
201
|
|
|
__('Terrible', 'site-reviews'), |
202
|
|
|
]; |
203
|
|
|
$maxRating = glsr()->constant('MAX_RATING', Rating::class); |
204
|
|
|
$defaults = array_pad(array_slice($defaults, 0, $maxRating), $maxRating, ''); |
205
|
|
|
$labels = array_map('trim', explode(',', $labels)); |
206
|
|
|
foreach ($defaults as $i => $label) { |
207
|
|
|
if (empty($labels[$i])) { |
208
|
|
|
continue; |
209
|
|
|
} |
210
|
|
|
$defaults[$i] = $labels[$i]; |
211
|
|
|
} |
212
|
|
|
return array_combine(range($maxRating, 1), $defaults); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @param string $schema |
217
|
|
|
* @return bool |
218
|
|
|
*/ |
219
|
|
|
protected function normalizeSchema($schema) |
220
|
|
|
{ |
221
|
|
|
return wp_validate_boolean($schema); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param string $text |
226
|
|
|
* @return string |
227
|
|
|
*/ |
228
|
|
|
protected function normalizeText($text) |
229
|
|
|
{ |
230
|
|
|
return trim($text); |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|