|
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
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* IMPORTANT NOTE: USE THIS PARSER **ONLY** IF YOU WANT THE FULL COMPATIBILITY |
|
10
|
|
|
* WITH WORDPRESS AND REPORT BUGS **ONLY** IF ITS BEHAVIOR IS DIFFERENT. |
|
11
|
|
|
* |
|
12
|
|
|
* This is a direct port of WordPress' shortcode parser with code copied from |
|
13
|
|
|
* its latest release (4.3.1 at the moment) adjusted to conform to this library. |
|
14
|
|
|
* Main regex was copied from get_shortcode_regex(), changed to handle all |
|
15
|
|
|
* shortcode names and folded into single string ($shortcodeRegex property), |
|
16
|
|
|
* method parseParameters() is a copy of function shortcode_parse_atts(). Code |
|
17
|
|
|
* was only structurally refactored for better readability. Read the comment |
|
18
|
|
|
* at the bottom of ParserTest::provideShortcodes() to understand the |
|
19
|
|
|
* limitations of this parser. |
|
20
|
|
|
* |
|
21
|
|
|
* @see https://core.trac.wordpress.org/browser/tags/4.3.1/src/wp-includes/shortcodes.php#L239 |
|
22
|
|
|
* @see https://core.trac.wordpress.org/browser/tags/4.3.1/src/wp-includes/shortcodes.php#L448 |
|
23
|
|
|
* |
|
24
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
final class WordpressParser implements ParserInterface |
|
27
|
|
|
{ |
|
28
|
|
|
private static $shortcodeRegex = '/\\[(\\[?)(<NAMES>)(?![\\w-])([^\\]\\/]*(?:\\/(?!\\])[^\\]\\/]*)*?)(?:(\\/)\\]|\\](?:([^\\[]*+(?:\\[(?!\\/\\2\\])[^\\[]*+)*+)\\[\\/\\2\\])?)(\\]?)/s'; |
|
29
|
|
|
private static $argumentsRegex = '/([\w-]+)\s*=\s*"([^"]*)"(?:\s|$)|([\w-]+)\s*=\s*\'([^\']*)\'(?:\s|$)|([\w-]+)\s*=\s*([^\s\'"]+)(?:\s|$)|"([^"]*)"(?:\s|$)|(\S+)(?:\s|$)/'; |
|
30
|
|
|
|
|
31
|
|
|
/** @var HandlerContainer */ |
|
32
|
|
|
private $handlers; |
|
33
|
|
|
|
|
34
|
2 |
|
public function __construct(HandlerContainer $handlers = null) |
|
35
|
|
|
{ |
|
36
|
2 |
|
$this->handlers = $handlers; |
|
37
|
2 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $text |
|
41
|
|
|
* |
|
42
|
|
|
* @return ParsedShortcode[] |
|
43
|
|
|
*/ |
|
44
|
32 |
|
public function parse($text) |
|
45
|
|
|
{ |
|
46
|
32 |
|
$names = $this->handlers ? implode('|', $this->handlers->getNames()) : '[a-zA-Z-]+'; |
|
47
|
32 |
|
$regex = str_replace('<NAMES>', $names, static::$shortcodeRegex); |
|
|
|
|
|
|
48
|
32 |
|
preg_match_all($regex, $text, $matches, PREG_OFFSET_CAPTURE); |
|
49
|
|
|
|
|
50
|
32 |
|
$shortcodes = array(); |
|
51
|
32 |
|
$count = count($matches[0]); |
|
52
|
32 |
|
for($i = 0; $i < $count; $i++) { |
|
53
|
25 |
|
$name = $matches[2][$i][0]; |
|
54
|
25 |
|
$parameters = static::parseParameters($matches[3][$i][0]); |
|
|
|
|
|
|
55
|
25 |
|
$content = $matches[5][$i][0] ?: null; |
|
56
|
25 |
|
$match = $matches[0][$i][0]; |
|
57
|
25 |
|
$offset = mb_strlen(substr($text, 0, $matches[0][$i][1]), 'utf-8'); |
|
58
|
|
|
|
|
59
|
25 |
|
$shortcode = new Shortcode($name, $parameters, $content, null); |
|
60
|
25 |
|
$shortcodes[] = new ParsedShortcode($shortcode, $match, $offset); |
|
61
|
25 |
|
} |
|
62
|
|
|
|
|
63
|
32 |
|
return $shortcodes; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
25 |
|
private static function parseParameters($text) |
|
67
|
|
|
{ |
|
68
|
25 |
|
$text = preg_replace('/[\x{00a0}\x{200b}]+/u', ' ', $text); |
|
69
|
|
|
|
|
70
|
25 |
|
if(!preg_match_all(static::$argumentsRegex, $text, $matches, PREG_SET_ORDER)) { |
|
|
|
|
|
|
71
|
12 |
|
return ltrim($text) ? array(ltrim($text) => null) : array(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
14 |
|
$parameters = array(); |
|
75
|
14 |
|
foreach($matches as $match) { |
|
76
|
14 |
|
if(!empty($match[1])) { |
|
77
|
11 |
|
$parameters[strtolower($match[1])] = stripcslashes($match[2]); |
|
78
|
14 |
|
} elseif(!empty($match[3])) { |
|
79
|
1 |
|
$parameters[strtolower($match[3])] = stripcslashes($match[4]); |
|
80
|
9 |
|
} elseif(!empty($match[5])) { |
|
81
|
5 |
|
$parameters[strtolower($match[5])] = stripcslashes($match[6]); |
|
82
|
9 |
|
} elseif(isset($match[7]) && strlen($match[7])) { |
|
83
|
1 |
|
$parameters[stripcslashes($match[7])] = null; |
|
84
|
5 |
|
} elseif(isset($match[8])) { |
|
85
|
4 |
|
$parameters[stripcslashes($match[8])] = null; |
|
86
|
4 |
|
} |
|
87
|
14 |
|
} |
|
88
|
|
|
|
|
89
|
14 |
|
foreach($parameters as $key => $value) { |
|
90
|
14 |
|
if(false !== strpos($value, '<') && 1 !== preg_match('/^[^<]*+(?:<[^>]*+>[^<]*+)*+$/', $value)) { |
|
91
|
1 |
|
$parameters[$key] = ''; |
|
92
|
1 |
|
} |
|
93
|
14 |
|
} |
|
94
|
|
|
|
|
95
|
14 |
|
return $parameters; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
Late static binding only has effect in subclasses. A
finalclass 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.