1
|
|
|
<?php |
2
|
|
|
namespace Thunder\Shortcode\Parser; |
3
|
|
|
|
4
|
|
|
use Thunder\Shortcode\HandlerContainer\HandlerContainer; |
5
|
|
|
use Thunder\Shortcode\Shortcode\ParsedShortcode; |
6
|
|
|
use Thunder\Shortcode\Shortcode\Shortcode; |
7
|
|
|
use Thunder\Shortcode\Utility\RegexBuilderUtility; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* IMPORTANT NOTE: USE THIS PARSER **ONLY** IF YOU WANT THE FULL COMPATIBILITY |
11
|
|
|
* WITH WORDPRESS AND REPORT BUGS **ONLY** IF ITS BEHAVIOR IS DIFFERENT. |
12
|
|
|
* |
13
|
|
|
* This is a direct port of WordPress' shortcode parser with code copied from |
14
|
|
|
* its latest release (4.3.1 at the moment) adjusted to conform to this library. |
15
|
|
|
* Main regex was copied from get_shortcode_regex(), changed to handle all |
16
|
|
|
* shortcode names and folded into single string ($shortcodeRegex property), |
17
|
|
|
* method parseParameters() is a copy of function shortcode_parse_atts(). Code |
18
|
|
|
* was only structurally refactored for better readability. Read the comment |
19
|
|
|
* at the bottom of ParserTest::provideShortcodes() to understand the |
20
|
|
|
* limitations of this parser. |
21
|
|
|
* |
22
|
|
|
* @see https://core.trac.wordpress.org/browser/tags/4.3.1/src/wp-includes/shortcodes.php#L239 |
23
|
|
|
* @see https://core.trac.wordpress.org/browser/tags/4.3.1/src/wp-includes/shortcodes.php#L448 |
24
|
|
|
* |
25
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
final class WordpressParser implements ParserInterface |
28
|
|
|
{ |
29
|
|
|
/** @var string */ |
30
|
|
|
private static $shortcodeRegex = '/\\[(\\[?)(<NAMES>)(?![\\w-])([^\\]\\/]*(?:\\/(?!\\])[^\\]\\/]*)*?)(?:(\\/)\\]|\\](?:([^\\[]*+(?:\\[(?!\\/\\2\\])[^\\[]*+)*+)\\[\\/\\2\\])?)(\\]?)/s'; |
31
|
|
|
/** @var string */ |
32
|
|
|
private static $argumentsRegex = '/([\w-]+)\s*=\s*"([^"]*)"(?:\s|$)|([\w-]+)\s*=\s*\'([^\']*)\'(?:\s|$)|([\w-]+)\s*=\s*([^\s\'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|(\S+)(?:\s|$)/'; |
33
|
|
|
|
34
|
|
|
/** @var string[] */ |
35
|
|
|
private $names = array(); |
36
|
|
|
|
37
|
2 |
|
public function __construct() |
38
|
|
|
{ |
39
|
2 |
|
} |
40
|
|
|
|
41
|
|
|
/** @return self */ |
42
|
1 |
|
public static function createFromHandlers(HandlerContainer $handlers) |
43
|
|
|
{ |
44
|
1 |
|
return static::createFromNames($handlers->getNames()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string[] $names |
49
|
|
|
* |
50
|
|
|
* @return self |
51
|
|
|
*/ |
52
|
2 |
|
public static function createFromNames(array $names) |
53
|
|
|
{ |
54
|
2 |
|
foreach($names as $name) { |
55
|
|
|
/** @psalm-suppress DocblockTypeContradiction, RedundantConditionGivenDocblockType */ |
56
|
2 |
|
if(false === is_string($name)) { |
57
|
1 |
|
throw new \InvalidArgumentException('Shortcode name must be a string!'); |
58
|
|
|
} |
59
|
2 |
|
} |
60
|
|
|
|
61
|
1 |
|
$self = new self(); |
62
|
1 |
|
$self->names = $names; |
63
|
|
|
|
64
|
1 |
|
return $self; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $text |
69
|
|
|
* |
70
|
|
|
* @return ParsedShortcode[] |
71
|
|
|
*/ |
72
|
39 |
|
public function parse($text) |
73
|
|
|
{ |
74
|
39 |
|
$names = $this->names |
75
|
39 |
|
? implode('|', array_map('preg_quote', $this->names)) |
76
|
39 |
|
: RegexBuilderUtility::buildNameRegex(); |
77
|
39 |
|
$regex = str_replace('<NAMES>', $names, static::$shortcodeRegex); |
|
|
|
|
78
|
39 |
|
preg_match_all($regex, $text, $matches, PREG_OFFSET_CAPTURE); |
79
|
|
|
|
80
|
39 |
|
$shortcodes = array(); |
81
|
39 |
|
$count = count($matches[0]); |
82
|
39 |
|
for($i = 0; $i < $count; $i++) { |
83
|
33 |
|
$name = $matches[2][$i][0]; |
84
|
33 |
|
$parameters = static::parseParameters($matches[3][$i][0]); |
|
|
|
|
85
|
33 |
|
$content = $matches[5][$i][1] !== -1 ? $matches[5][$i][0] : null; |
|
|
|
|
86
|
33 |
|
$match = $matches[0][$i][0]; |
87
|
33 |
|
$offset = mb_strlen(substr($text, 0, $matches[0][$i][1]), 'utf-8'); |
88
|
|
|
|
89
|
33 |
|
$shortcode = new Shortcode($name, $parameters, $content, null); |
90
|
33 |
|
$shortcodes[] = new ParsedShortcode($shortcode, $match, $offset); |
91
|
33 |
|
} |
92
|
|
|
|
93
|
39 |
|
return $shortcodes; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $text |
98
|
|
|
* |
99
|
|
|
* @psalm-return array<string,string|null> |
100
|
|
|
*/ |
101
|
33 |
|
private static function parseParameters($text) |
102
|
|
|
{ |
103
|
33 |
|
$text = preg_replace('/[\x{00a0}\x{200b}]+/u', ' ', $text); |
104
|
|
|
|
105
|
33 |
|
if(!preg_match_all(static::$argumentsRegex, $text, $matches, PREG_SET_ORDER)) { |
|
|
|
|
106
|
19 |
|
return ltrim($text) ? array(ltrim($text) => null) : array(); |
107
|
|
|
} |
108
|
|
|
|
109
|
17 |
|
$parameters = array(); |
110
|
17 |
|
foreach($matches as $match) { |
111
|
17 |
|
if(!empty($match[1])) { |
112
|
11 |
|
$parameters[strtolower($match[1])] = stripcslashes($match[2]); |
113
|
17 |
|
} elseif(!empty($match[3])) { |
114
|
1 |
|
$parameters[strtolower($match[3])] = stripcslashes($match[4]); |
115
|
12 |
|
} elseif(!empty($match[5])) { |
116
|
8 |
|
$parameters[strtolower($match[5])] = stripcslashes($match[6]); |
117
|
12 |
|
} elseif(isset($match[7]) && $match[7] !== '') { |
118
|
1 |
|
$parameters[stripcslashes($match[7])] = null; |
119
|
6 |
|
} elseif(isset($match[8])) { |
120
|
6 |
|
$parameters[stripcslashes($match[8])] = null; |
121
|
6 |
|
} |
122
|
17 |
|
} |
123
|
|
|
|
124
|
17 |
|
foreach($parameters as $key => $value) { |
125
|
|
|
// NOTE: the `?: ''` fallback is the only change from the way WordPress parses shortcodes to satisfy Psalm's PossiblyNullArgument |
126
|
17 |
|
$value = $value ?: ''; |
127
|
17 |
|
if(false !== strpos($value, '<') && 1 !== preg_match('/^[^<]*+(?:<[^>]*+>[^<]*+)*+$/', $value)) { |
128
|
1 |
|
$parameters[$key] = ''; |
129
|
1 |
|
} |
130
|
17 |
|
} |
131
|
|
|
|
132
|
17 |
|
return $parameters; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
Late static binding only has effect in subclasses. A
final
class cannot be extended anymore so late static binding cannot occurr. Consider replacingstatic::
withself::
.To learn more about late static binding, please refer to the PHP core documentation.