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(Style::class)->styleClasses(), |
61
|
|
|
'id' => $atts->id, |
62
|
|
|
'text' => $template, |
63
|
|
|
]); |
64
|
|
|
$attributes = glsr()->filterArray('shortcode/'.$this->shortcode.'/attributes', $attributes, $this); |
65
|
|
|
$html = glsr(Builder::class)->div($attributes); |
66
|
|
|
return $args->before_widget.$atts->title.$html.$args->after_widget; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function buildBlock($atts = []) |
73
|
|
|
{ |
74
|
|
|
return $this->build($atts, [], 'block'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
|
|
public function buildShortcode($atts = []) |
81
|
|
|
{ |
82
|
|
|
return $this->build($atts, [], 'shortcode'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return string|void |
87
|
|
|
*/ |
88
|
|
|
public function buildTemplate(array $args = []) |
89
|
|
|
{ |
90
|
|
|
return; // @todo make this abstract in v6.0 |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
7 |
|
public function getHideOptions() |
97
|
|
|
{ |
98
|
7 |
|
$options = $this->hideOptions(); |
99
|
7 |
|
return glsr()->filterArray('shortcode/hide-options', $options, $this->shortcode, $this); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
7 |
|
public function getShortClassName($replace = '', $search = 'Shortcode') |
106
|
|
|
{ |
107
|
7 |
|
return str_replace($search, $replace, (new ReflectionClass($this))->getShortName()); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
public function getShortcodeDefaultsClassName() |
114
|
|
|
{ |
115
|
|
|
$classname = str_replace('Shortcodes\\', 'Defaults\\', get_class($this)); |
116
|
|
|
return str_replace('Shortcode', 'Defaults', $classname); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param array|string $args |
121
|
|
|
* @param string $type |
122
|
|
|
* @return Arguments |
123
|
|
|
*/ |
124
|
|
|
public function normalizeArgs($args, $type = 'shortcode') |
125
|
|
|
{ |
126
|
|
|
$args = wp_parse_args($args, [ |
127
|
|
|
'before_widget' => '', |
128
|
|
|
'after_widget' => '', |
129
|
|
|
'before_title' => '<h2 class="glsr-title">', |
130
|
|
|
'after_title' => '</h2>', |
131
|
|
|
]); |
132
|
|
|
$args = glsr()->filterArray('shortcode/args', $args, $type, $this->shortcode); |
133
|
|
|
return glsr()->args($args); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param array|string $atts |
138
|
|
|
* @param string $type |
139
|
|
|
* @return Arguments |
140
|
|
|
*/ |
141
|
|
|
public function normalizeAtts($atts, $type = 'shortcode') |
142
|
|
|
{ |
143
|
|
|
$atts = wp_parse_args($atts); |
144
|
|
|
$atts = glsr()->filterArray('shortcode/atts', $atts, $type, $this->shortcode); |
145
|
|
|
$atts = glsr($this->getShortcodeDefaultsClassName())->unguardedRestrict($atts); |
146
|
|
|
$atts = glsr()->args($atts); |
147
|
|
|
foreach ($atts as $key => &$value) { |
148
|
|
|
$method = Helper::buildMethodName($key, 'normalize'); |
149
|
|
|
if (method_exists($this, $method)) { |
150
|
|
|
$value = call_user_func([$this, $method], $value, $atts); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
$this->setDataAttributes($atts, $type); |
154
|
|
|
return $atts; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return array |
159
|
|
|
*/ |
160
|
|
|
abstract protected function hideOptions(); |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param string $postIds |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
|
|
protected function normalizeAssignedPosts($postIds, Arguments $atts) |
167
|
|
|
{ |
168
|
|
|
return implode(',', glsr(PostManager::class)->normalizeIds($postIds)); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param string $termIds |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
|
|
protected function normalizeAssignedTerms($termIds) |
176
|
|
|
{ |
177
|
|
|
return implode(',', glsr(TaxonomyManager::class)->normalizeIds($termIds)); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param string $userIds |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
|
|
protected function normalizeAssignedUsers($userIds) |
185
|
|
|
{ |
186
|
|
|
return implode(',', glsr(UserManager::class)->normalizeIds($userIds)); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param string|array $hide |
191
|
|
|
* @return array |
192
|
|
|
*/ |
193
|
|
|
protected function normalizeHide($hide) |
194
|
|
|
{ |
195
|
|
|
$hideKeys = array_keys($this->getHideOptions()); |
196
|
|
|
return array_filter(Cast::toArray($hide), function ($value) use ($hideKeys) { |
197
|
|
|
return in_array($value, $hideKeys); |
198
|
|
|
}); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param string $labels |
203
|
|
|
* @return array |
204
|
|
|
*/ |
205
|
|
|
protected function normalizeLabels($labels) |
206
|
|
|
{ |
207
|
|
|
$defaults = [ |
208
|
|
|
__('Excellent', 'site-reviews'), |
209
|
|
|
__('Very good', 'site-reviews'), |
210
|
|
|
__('Average', 'site-reviews'), |
211
|
|
|
__('Poor', 'site-reviews'), |
212
|
|
|
__('Terrible', 'site-reviews'), |
213
|
|
|
]; |
214
|
|
|
$maxRating = (int) glsr()->constant('MAX_RATING', Rating::class); |
215
|
|
|
$defaults = array_pad(array_slice($defaults, 0, $maxRating), $maxRating, ''); |
216
|
|
|
$labels = array_map('trim', explode(',', $labels)); |
217
|
|
|
foreach ($defaults as $i => $label) { |
218
|
|
|
if (!empty($labels[$i])) { |
219
|
|
|
$defaults[$i] = $labels[$i]; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
return array_combine(range($maxRating, 1), $defaults); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param string $type |
227
|
|
|
* @return void |
228
|
|
|
*/ |
229
|
|
|
protected function setDataAttributes(Arguments $atts, $type) |
230
|
|
|
{ |
231
|
|
|
$this->dataAttributes = wp_parse_args( |
232
|
|
|
glsr($this->getShortcodeDefaultsClassName())->dataAttributes($atts->toArray()), |
233
|
|
|
["data-{$type}" => ''] |
234
|
|
|
); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|