ShortCodes.php
4.43 KB
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
<?php
class ShortCodes extends Singleton {
private static $registered = Array();
private static $private = Array();
public static function make() {
add_actions('ShortCodes_Actions');
}
public static function registerClass($class) {
if (!class_exists($class)) {
throw new Exception("$class does not exist");
}
$ref = new ReflectionClass($class);
$methods = $ref->getMethods(ReflectionMethod::IS_STATIC);
foreach ($methods as $method) {
self::add($method->name, Array($class, $method->name));
}
}
public static function add($code, $callback) {
$fn = (function_exists('wp_add_shortcode') ? 'wp_add_shortcode' : 'add_shortcode');
call_user_func($fn, $code, $callback);
if (is_admin()) {
self::$registered[$code] = Array('code' => $code, 'title' => $code, 'params' => Array(), 'uses_content' => 0);
if (is_array($callback)) {
$ref = new ReflectionMethod($callback[0], $callback[1]);
} else {
$ref = new ReflectionFunction($callback);
}
$api = $ref->getDocComment();
$api = explode("\n", $api);
array_shift($api);
array_pop($api);
foreach ($api as $key => &$comment) {
$comment = trim($comment, ' *');
if (substr($comment, 0, 1) == '@') {
$tag = trim(substr($comment, 1, strpos($comment, ' ')));
if (empty($tag)) {
$tag = trim($comment, '@');
}
if (method_exists(__CLASS__, 'parseTag_' . $tag)) {
call_user_func_array(Array(__CLASS__, 'parseTag_' . $tag), Array($code, $comment));
}
}
}
}
}
public static function parseTag_display($code, $string) {
$string = trim(str_replace('@display', '', $string));
self::$registered[$code]['title'] = $string;
}
public static function parseTag_param($code, $string) {
$regex = '.*?@param {((?:[a-z][a-z]+))(\\(.*?\\))?} (.*?) (.*?)$'; // Awww yeah!
if ($num = preg_match_all("/" . $regex . "/is", $string, $matches)) {
self::$registered[$code]['params'][] = Array('name' => $matches[3][0], 'type' => $matches[1][0], 'options' => explode(',', trim($matches[2][0], ')(')), 'desc' => $matches[4][0]);
}
}
public static function parseTag_private($code, $string) {
self::$private[$code] = 1;
}
public static function uses_content($code) {
self::$registered[$code]['uses_content'] = 1;
}
public static function getRegistered() {
$return = self::$registered;
foreach (self::$private as $key => $one) {
unset($return[$key]);
}
return $return;
}
public static function drawMetaBox() {
?>
<label for="TzShortCodeList">Tag:</label>
<select id="TzShortCodeList" name="TzShortCodeList">
<?php
$options = ShortCodes::getRegistered();
ksort($options);
foreach ($options as $tag => $data) {
echo '<option value="' . $tag . '">' . $data['title'] . '</option>';
}
?>
</select>
<p class="submit">
<input type="button" id="TzInsertSC" value="<?php _e('Insert into post'); ?>" />
</p>
<?php
}
}
class ShortCodes_Actions {
public static function admin_menu() {
add_meta_box('TzShortCodes', 'Code Helper', Array('ShortCodes', 'drawMetaBox'), 'post', 'normal');
add_meta_box('TzShortCodes', 'Code Helper', Array('ShortCodes', 'drawMetaBox'), 'page', 'normal');
}
public static function admin_print_scripts() {
if ($GLOBALS['editing']) {
_enqueue_script('shortcoder', plugins_url('shortcoder.js', __FILE__), Array('jquery'));
echo "<script type=\"text/javascript\">\n/* <![CDATA[ */\n";
echo 'var TzRegisteredShortCodes = ' . json_encode(ShortCodes::getRegistered());
echo "\n/* ]]> */</script>\n";
}
}
}
/**
* @deprecated
*/
function add_shortcodes($class) {
call_user_func(Array('ShortCodes', 'registerClass'), $class);
}
if (function_exists('rename_function')) {
rename_function('add_shortcode', 'wp_add_shortcode');
function add_shortcode() {
$args = func_get_args();
call_user_func_array(Array('ShortCodes', 'add'), $args);
}
}
ShortCodes::make();
?>