forked from CodyReichert/materializer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortcodes.php
80 lines (69 loc) · 2.09 KB
/
shortcodes.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
<?php
/*
Materializer - Material Design components for WordPress
Copyright (c) Cody Reichert - 2015
*/
/* Code starts here */
class MaterializerShortcodes {
public static function spliceColor($color) {
return $color;
}
/**
* Parse $content for shortcodes matching $tag, collect them
* and return them as an array.
*/
public static function get_stripped_shortcodes($content, $tag) {
$pattern = MaterializerShortcodes::single_shortcode_regexp($tag);
$matches = array();
if(!has_shortcode($content, $tag)) {
return false;
}
preg_match_all("/$pattern/s", $content, $matches);
return $matches;
}
/**
* Parse $content and strip all $tag shortcodes, returning
* the content with only shortcodes matchin $tag removed.
*/
public static function strip_shortcode($content, $tag) {
if(false === strpos($content, $tag)) {
return $content;
}
$pattern = MaterializerShortcodes::single_shortcode_regexp($tag);
return preg_replace_callback("/$pattern/s", 'strip_shortcode_tag', $content);
}
/**
* Given $tag, return a regexp to match shortcodes with name $tag.
*/
public static function single_shortcode_regexp($tag) {
return
'\\['
. '(\\[?)'
. "($tag)"
. '(?![\\w-])'
. '('
. '[^\\]\\/]*'
. '(?:'
. '\\/(?!\\])'
. '[^\\]\\/]*'
. ')*?'
. ')'
. '(?:'
. '(\\/)'
. '\\]'
. '|'
. '\\]'
. '(?:'
. '('
. '[^\\[]*+'
. '(?:'
. '\\[(?!\\/\\2\\])'
. '[^\\[]*+'
. ')*+'
. ')'
. '\\[\\/\\2\\]'
. ')?'
. ')'
. '(\\]?)';
}
}