ShortCodes.php
4.94 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
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
<?php
namespace Tz\WordPress\Tools\ShortCodes;
use Tz\WordPress\Tools;
use ReflectionClass, ReflectionMethod, ReflectionFunction;
use Exception, Closure;
call_user_func(function() {
Tools\add_actions(__NAMESPACE__ . '\Actions');
Tools\add_filters(__NAMESPACE__ . '\Filters');
if (function_exists('rename_function')) {
rename_function('add_shortcode', 'wp_add_shortcode');
function add_shortcode() {
$args = func_get_args();
call_user_func_array('add', $args);
}
}
});
function add_shortcodes($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) {
add($method->name, Array($class, $method->name));
}
}
function getFileContents($file) {
if (!is_file($file)) {
throw new Exception("$file not found");
}
ob_start();
require($file);
$parsed_contents = ob_get_contents();
ob_end_clean();
return $parsed_contents;
}
function buffer(Closure $cb) {
ob_start();
$cb();
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
function dump($var) {
return '<pre>' . buffer(function() use ($var) { print_r($var); }) . '</pre>';
}
function registerClass($class) {
call_user_func(__NAMESPACE__ . '\add_shortcodes', $class);
}
function add($code, $callback) {
$fn = (function_exists('wp_add_shortcode') ? 'wp_add_shortcode' : 'add_shortcode');
call_user_func($fn, $code, $callback);
if (is_admin()) { // I may want to remove this condition...
Vars::$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 (function_exists('parseTag_' . $tag)) {
call_user_func_array('parseTag_' . $tag, Array($code, $comment));
}
}
}
}
}
function parseTag_display($code, $string) {
$string = trim(str_replace('@display', '', $string));
Vars::$registered[$code]['title'] = $string;
}
function parseTag_param($code, $string) {
$regex = '.*?@param {((?:[a-z][a-z]+))(\\(.*?\\))?} (.*?) (.*?)$'; // Awww yeah!
if ($num = preg_match_all("/" . $regex . "/is", $string, $matches)) {
Vars::$registered[$code]['params'][] = Array('name' => $matches[3][0], 'type' => $matches[1][0], 'options' => explode(',', trim($matches[2][0], ')(')), 'desc' => $matches[4][0]);
}
}
function parseTag_private($code, $string) {
Vars::$private[$code] = 1;
}
function uses_content($code) {
Vars::$registered[$code]['uses_content'] = 1;
}
function getRegistered() {
$return = Vars::$registered;
foreach (Vars::$private as $key => $one) {
unset($return[$key]);
}
return $return;
}
function drawMetaBox() {
?>
<label for="TzShortCodeList">Tag:</label>
<select id="TzShortCodeList" name="TzShortCodeList">
<?php
$options = 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 Actions {
public static function admin_menu() {
add_meta_box('TzShortCodes', 'Code Helper', __NAMESPACE__ . '\drawMetaBox', 'post', 'normal');
add_meta_box('TzShortCodes', 'Code Helper', __NAMESPACE__ . '\drawMetaBox', 'page', 'normal');
}
public static function admin_print_scripts() {
if ($GLOBALS['editing']) {
_enqueue_script('shortcoder', Tools\url('shortcoder.js', __FILE__), Array('jquery'));
echo "<script type=\"text/javascript\">\n/* <![CDATA[ */\n";
echo 'var TzRegisteredShortCodes = ' . json_encode(getRegistered());
echo "\n/* ]]> */</script>\n";
}
}
}
class Filters {
public static function the_title($content) {
return is_admin() ? $content : do_shortcode($content);
}
public static function wp_title($content) {
return is_admin() ? $content : do_shortcode(htmlspecialchars_decode($content));
}
}
class Vars {
public static $registered = Array();
public static $private = Array();
}
?>