-
Notifications
You must be signed in to change notification settings - Fork 2
/
Shortcode.php
338 lines (240 loc) · 8.22 KB
/
Shortcode.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
namespace Shortcode;
/**
* Simple Shortcode Parser
*
* @copyright Copyright (c) 2018 SquareFlower Websolutions
* @license BSD License
* @author Lukas Rydygel <hallo@squareflower.de>
* @version 0.3.2
*/
abstract class Shortcode
{
/**
* Collection of registered shortcodes
*
* @var array
*/
protected static $shortcodes = [];
/**
* Register a callback function for a shortcode
* Default attributes can be pased
*
* Existing shortcodes can't be overwritten, they must be unregistered first
*
* @param string $tag
* @param mixed $callback
* @param array $attr
* @return boolean
*/
public static function register($tag, $callback, array $attr = [])
{
// check if the shortcode exists
if (!array_key_exists($tag, self::$shortcodes)) {
// register the shortcode
self::$shortcodes[$tag] = ['callback' => $callback, 'attr' => $attr];
return true;
}
return false;
}
/**
* Removes a registered shortcode
*
* @param string $tag
* @return boolean
*/
public static function unregister($tag)
{
// check if the shortcode exists
if (array_key_exists($tag, self::$shortcodes)) {
// remove the shortcode
unset(self::$shortcodes[$tag]);
return true;
}
return false;
}
/**
* Process all shortcodes inside a string
*
* You may also allow only certain shortcodes. If no shortcodes were specified, all registered shortcodes will be used.
*
* @param string $content
* @param array $allow
* @return type
*/
public static function process($content, array $allow = [])
{
// return if content is empty
if (empty($content)) {
return $content;
}
// return if no shortcodes exist
if (empty(self::$shortcodes)) {
return $content;
}
// get all allowed shortcodes
$allowedShortcodes = self::getAllowedShortcodes($allow);
// return if no shortcodes are allowed
if (empty($allowedShortcodes)) {
return $content;
}
// execute the shortcodes
return self::execute($allowedShortcodes, $content);
}
/**
* Gets all allowed shortcodes
*
* @param array $allow
* @return array
*/
protected static function getAllowedShortcodes(array $allow)
{
// return all if no shortcodes were specified
if (empty($allow)) {
return self::$shortcodes;
}
// intersect by the registered tags
return array_intersect_key(self::$shortcodes, array_flip($allow, $allow));
}
/**
* Executes the content with a set of given shortcodes
*
* @param array $shortcodes
* @param string $content
* @return string
*/
protected static function execute($shortcodes, $content)
{
$offset = 0;
foreach ($shortcodes as $tag => $data) {
// Regular expression for opening and closing tags
$open = "/\[{$tag}(\s(.*?))?\]/i";
$close = "/\[\/{$tag}\]/i";
$size = strlen($tag)+3;
if (preg_match_all($open, $content, $openMatches, PREG_OFFSET_CAPTURE) && preg_match_all($close, $content, $closeMatches, PREG_OFFSET_CAPTURE)) {
// Find matching tags
$matches = self::findMatchingTags($openMatches[0], $closeMatches[0]);
// Walk through matching tags
foreach ($matches as $match) {
// Calculate the start and endpoint of the part
$start = $match[0];
$end = $match[1] - $start + $size + $offset;
// Get the part from the content
$part = substr($content, $start, $end);
$length = strlen($part);
// Replace the part with the parsed results from the shortcode
$replace = self::parseTagInPart($tag, $part);
$content = substr_replace($content, $replace, $start, $end);
// Correct the offset
$offset += strlen($replace) - $length;
}
}
}
return self::parseEmptyTags($shortcodes, $content);
}
protected static function findMatchingTags($openMatches, $closeMatches)
{
$tags = [];
foreach (array_reverse($openMatches) as $i => $openMatch) {
$diff = null;
foreach ($closeMatches as $j => $closeMatch) {
if ($openMatch[1] < $closeMatch[1]) {
$d = $closeMatch[1] - $openMatch[1];
if (!isset($diff) || $d < $diff) {
$diff = $d;
$tags[$i] = [$openMatch[1], $closeMatch[1]];
unset($openMatches[$i]);
unset($closeMatches[$j]);
}
}
}
}
return $tags;
}
protected static function parseEmptyTags($shortcodes, $content)
{
// get the regular expression for the given shortcodes
$regexp = self::createRegExp($shortcodes);
// run a replace callback for the regular expression
return preg_replace_callback($regexp, function($matches) {
// default values
$attr = [];
$content = '';
// check for the number of matches
switch (count($matches)) {
// shortcode without params
case 2:
list(, $tag) = $matches;
break;
// shortcode with attr
case 4:
list(, $tag, , $attr) = $matches;
break;
}
return self::parseInternal($tag, $attr);
}, $content);
}
/**
* Parse a single shortcode in a given part of the content
*
* @param string $tag
* @param string $part
* @return string
*/
protected static function parseTagInPart($tag, $part)
{
$regexp = "/\[{$tag}(\s(.*?))?\](.*)\[\/{$tag}\]/is";
return preg_replace_callback($regexp, function($matches) use($tag) {
// default values
$attr = [];
$content = '';
// check for the number of matches
switch (count($matches)) {
// shortcode with attr, no content
case 3:
list(, , $attr) = $matches;
break;
// shortcodes with attr and content
case 4:
list(, , $attr, $content) = $matches;
break;
}
return self::parseInternal($tag, $attr, $content);
}, $part);
}
protected static function parseInternal($tag, $attr, $content = '')
{
$content = trim($content);
// if no params were found, they don't need to be parsed
if (!is_array($attr)) {
$attr = self::parseAttributes($attr);
}
$attr = array_replace($attr, self::$shortcodes[$tag]['attr']);
// run the callback with params and content
return call_user_func_array(self::$shortcodes[$tag]['callback'], [$attr, $content]);
}
/**
* Parses the attributes by using XML
*
* @param string $attr
* @return array
*/
protected static function parseAttributes($attr)
{
$xml = (array) new \SimpleXMLElement("<element $attr />");
return array_key_exists('@attributes', $xml) ? $xml['@attributes'] : [];
}
/**
* Created one regular expression for all given shortcodes.
*
* Using one regular expression instead looping through the shortcodes increased the speed.
*
* @param array $shortcodes
* @return string
*/
protected static function createRegExp($shortcodes)
{
$tags = implode('|', array_keys($shortcodes));
return "/\[({$tags})(\s(.*?))?\/?\]/is";
}
}