142c134e by Chris Boden

Added ShortCodes components, updated wp_funs for password reset

1 parent 6dbd4043
1 <?php
2 class ShortCodes extends Singleton {
3 private static $registered = Array();
4 private static $private = Array();
5
6 public static function make() {
7 add_actions('ShortCodes_Actions');
8 }
9
10 public static function registerClass($class) {
11 if (!class_exists($class)) {
12 throw new Exception("$class does not exist");
13 }
14
15 $ref = new ReflectionClass($class);
16 $methods = $ref->getMethods(ReflectionMethod::IS_STATIC);
17 foreach ($methods as $method) {
18 self::add($method->name, Array($class, $method->name));
19 }
20 }
21
22 public static function add($code, $callback) {
23 $fn = (function_exists('wp_add_shortcode') ? 'wp_add_shortcode' : 'add_shortcode');
24 call_user_func($fn, $code, $callback);
25
26 if (is_admin()) {
27 self::$registered[$code] = Array('code' => $code, 'title' => $code, 'params' => Array(), 'uses_content' => 0);
28
29 if (is_array($callback)) {
30 $ref = new ReflectionMethod($callback[0], $callback[1]);
31 } else {
32 $ref = new ReflectionFunction($callback);
33 }
34 $api = $ref->getDocComment();
35 $api = explode("\n", $api);
36 array_shift($api);
37 array_pop($api);
38
39 foreach ($api as $key => &$comment) {
40 $comment = trim($comment, ' *');
41
42 if (substr($comment, 0, 1) == '@') {
43 $tag = trim(substr($comment, 1, strpos($comment, ' ')));
44 if (empty($tag)) {
45 $tag = trim($comment, '@');
46 }
47
48 if (method_exists(__CLASS__, 'parseTag_' . $tag)) {
49 call_user_func_array(Array(__CLASS__, 'parseTag_' . $tag), Array($code, $comment));
50 }
51 }
52 }
53 }
54 }
55
56 public static function parseTag_display($code, $string) {
57 $string = trim(str_replace('@display', '', $string));
58 self::$registered[$code]['title'] = $string;
59 }
60
61 public static function parseTag_param($code, $string) {
62 $regex = '.*?@param {((?:[a-z][a-z]+))(\\(.*?\\))?} (.*?) (.*?)$'; // Awww yeah!
63 if ($num = preg_match_all("/" . $regex . "/is", $string, $matches)) {
64 self::$registered[$code]['params'][] = Array('name' => $matches[3][0], 'type' => $matches[1][0], 'options' => explode(',', trim($matches[2][0], ')(')), 'desc' => $matches[4][0]);
65 }
66 }
67
68 public static function parseTag_private($code, $string) {
69 self::$private[$code] = 1;
70 }
71
72 public static function uses_content($code) {
73 self::$registered[$code]['uses_content'] = 1;
74 }
75
76 public static function getRegistered() {
77 $return = self::$registered;
78 foreach (self::$private as $key => $one) {
79 unset($return[$key]);
80 }
81
82 return $return;
83 }
84
85 public static function drawMetaBox() {
86 ?>
87 <label for="TzShortCodeList">Tag:</label>
88 <select id="TzShortCodeList" name="TzShortCodeList">
89 <?php
90 $options = ShortCodes::getRegistered();
91 ksort($options);
92 foreach ($options as $tag => $data) {
93 echo '<option value="' . $tag . '">' . $data['title'] . '</option>';
94 }
95 ?>
96 </select>
97
98 <p class="submit">
99 <input type="button" id="TzInsertSC" value="<?php _e('Insert into post'); ?>" />
100 </p>
101 <?php
102 }
103 }
104
105 class ShortCodes_Actions {
106 public static function admin_menu() {
107 add_meta_box('TzShortCodes', 'Code Helper', Array('ShortCodes', 'drawMetaBox'), 'post', 'normal');
108 add_meta_box('TzShortCodes', 'Code Helper', Array('ShortCodes', 'drawMetaBox'), 'page', 'normal');
109 }
110
111 public static function admin_print_scripts() {
112 if ($GLOBALS['editing']) {
113 _enqueue_script('shortcoder', plugins_url('shortcoder.js', __FILE__), Array('jquery'));
114
115 echo "<script type=\"text/javascript\">\n/* <![CDATA[ */\n";
116 echo 'var TzRegisteredShortCodes = ' . json_encode(ShortCodes::getRegistered());
117 echo "\n/* ]]> */</script>\n";
118 }
119 }
120 }
121
122 /**
123 * @deprecated
124 */
125 function add_shortcodes($class) {
126 call_user_func(Array('ShortCodes', 'registerClass'), $class);
127 }
128
129
130 if (function_exists('rename_function')) {
131 rename_function('add_shortcode', 'wp_add_shortcode');
132
133 function add_shortcode() {
134 $args = func_get_args();
135 call_user_func_array(Array('ShortCodes', 'add'), $args);
136 }
137 }
138
139 ShortCodes::make();
140 ?>
...\ No newline at end of file ...\ No newline at end of file
1 var ShortCoder = function() {
2 var dLookup = TzRegisteredShortCodes;
3 var $SCList;
4
5 var api = {
6 insert: function() {
7 var strKey = $SCList[0][$SCList[0].selectedIndex].value;
8 var strCode = dLookup[strKey]['code'];
9
10 console.log(dLookup[strCode]);
11
12 send_to_editor('[' + strCode + ']');
13 }
14
15 , getCode: function() {
16 var strKey = $SCList[0][$SCList[0].selectedIndex].value;
17 return dLookup[strKey]['code'];
18
19 // I almost certainly can just return strKey...
20 }
21 }
22
23 var init = function() {
24 jQuery('#TzInsertSC').click(api.insert);
25 $SCList = jQuery('#TzShortCodeList').change(tagChange);
26 }
27
28 var generateCode = function() {
29 // if uses_content, send a close tag
30 }
31
32 var tagChange = function($o) {
33
34 }
35
36 /*
37 var create = {
38 string: function() {
39
40 }
41
42 , boolean: function() {
43
44 }
45
46 , enum: function() {
47
48 }
49 }
50 */
51
52 jQuery(document).ready(init);
53 return api;
54 }();
55
56 /*
57 onclick="return wpYourPluginAdmin.sendToEditor(this.form);"
58
59 sendToEditor : function(f) {
60 var collection = jQuery(f).find("input[id^=wpYourPluginName]:not(input:checkbox),input[id^=wpYourPluginName]:checkbox:checked");
61 var $this = this;
62 collection.each(function () {
63 var name = this.name.substring(13, this.name.length-1);
64 $this['options'][name] = this.value;
65 });
66 send_to_editor(this.generateShortCode());
67 return false;
68 }
69
70
71 */
...\ No newline at end of file ...\ No newline at end of file
...@@ -16,13 +16,14 @@ class TzTools { ...@@ -16,13 +16,14 @@ class TzTools {
16 public static function load() { 16 public static function load() {
17 spl_autoload_register(Array(__CLASS__, 'autoloader')); 17 spl_autoload_register(Array(__CLASS__, 'autoloader'));
18 18
19 require_once(dirname(__FILE__) . '/wp_functions.php'); 19 require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'wp_functions.php');
20 20
21 _register_script('addEvent', plugins_url('addEvent.js', __FILE__)); 21 _register_script('addEvent', plugins_url('addEvent.js', __FILE__));
22 _register_script('xmlhttpHandler', plugins_url('xmlhttpHandler.js', __FILE__)); 22 _register_script('xmlhttpHandler', plugins_url('xmlhttpHandler.js', __FILE__));
23 _register_script('fireEvent', plugins_url('fireEvent.js', __FILE__)); 23 _register_script('fireEvent', plugins_url('fireEvent.js', __FILE__));
24 24
25 add_action('widgets_init', Array('MenuWidget', 'init')); 25 add_action('widgets_init', Array('MenuWidget', 'init'));
26 self::import('ShortCodes');
26 } 27 }
27 28
28 public static function import($com) { 29 public static function import($com) {
...@@ -53,18 +54,6 @@ function add_actions($class) { ...@@ -53,18 +54,6 @@ function add_actions($class) {
53 } 54 }
54 } 55 }
55 56
56 function add_shortcodes($class) {
57 if (!class_exists($class)) {
58 throw new Exception("$class does not exist");
59 }
60
61 $ref = new ReflectionClass($class);
62 $methods = $ref->getMethods(ReflectionMethod::IS_STATIC);
63 foreach ($methods as $method) {
64 add_shortcode($method->name, Array($class, $method->name));
65 }
66 }
67
68 function add_filters($class) { 57 function add_filters($class) {
69 if (!class_exists($class)) { 58 if (!class_exists($class)) {
70 throw new Exception("$class does not exist"); 59 throw new Exception("$class does not exist");
...@@ -89,25 +78,6 @@ function get_custom_data($name, $post_id = false) { ...@@ -89,25 +78,6 @@ function get_custom_data($name, $post_id = false) {
89 } 78 }
90 79
91 return $raw_data; 80 return $raw_data;
92
93
94
95 /* @deprecated
96 if (is_array($raw_data)) {
97 return $raw_data;
98 }
99
100
101
102 return $raw_data
103
104 /* @deprecated
105 if (null === $maybe_data = json_decode($raw_data, true)) {
106 return $raw_data;
107 } else {
108 return $maybe_data;
109 }
110 */
111 } 81 }
112 82
113 function _custom_attachment($post_id, $custom_name) { 83 function _custom_attachment($post_id, $custom_name) {
...@@ -122,19 +92,6 @@ function _custom_attachment($post_id, $custom_name) { ...@@ -122,19 +92,6 @@ function _custom_attachment($post_id, $custom_name) {
122 function _custom_page($post_id, $custom_name) { 92 function _custom_page($post_id, $custom_name) {
123 $custom = get_post_meta($post_id, $custom_name); 93 $custom = get_post_meta($post_id, $custom_name);
124 return array_shift($custom); 94 return array_shift($custom);
125
126 // @deprecated
127 static $custom = Array();
128
129 if (!isset($custom[$post_id])) {
130 $custom[$post_id] = get_post_custom($post_id);
131
132 if (!isset($custom[$post_id][$custom_name])) {
133 $custom[$post_id][$custom_name] = Array('');
134 }
135 }
136
137 return array_shift($custom[$post_id][$custom_name]);
138 } 95 }
139 96
140 function _custom_post() { 97 function _custom_post() {
......
...@@ -138,4 +138,15 @@ function _insert_user() { ...@@ -138,4 +138,15 @@ function _insert_user() {
138 $params = func_get_args(); 138 $params = func_get_args();
139 return call_user_func_array('wp' . __FUNCTION__, $params); 139 return call_user_func_array('wp' . __FUNCTION__, $params);
140 } 140 }
141
142 function _generate_password() {
143 $params = func_get_args();
144 return call_user_func_array('wp' . __FUNCTION__, $params);
145 }
146
147 function _update_user() {
148 require_once(ABSPATH . WPINC . DIRECTORY_SEPARATOR . 'registration.php');
149 $params = func_get_args();
150 return call_user_func_array('wp' . __FUNCTION__, $params);
151 }
141 ?> 152 ?>
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
3 * @author Chris Boden 3 * @author Chris Boden
4 * @version 1.0b 4 * @version 1.0b
5 * @todo Figure out if/why blnLoading is needed 5 * @todo Figure out if/why blnLoading is needed
6 I don't think it's being used properly; should be used in conjunction
7 with a buffer if call() is called multiple times before a return
6 * @todo Some of the private variables need to be changable maybe just make all public 8 * @todo Some of the private variables need to be changable maybe just make all public
7 * @todo More testing 9 * @todo More testing
8 * @todo Make distinction between data error and exception 10 * @todo Make distinction between data error and exception
...@@ -18,9 +20,7 @@ ...@@ -18,9 +20,7 @@
18 */ 20 */
19 var xmlhttpHandler = function(strURL) { 21 var xmlhttpHandler = function(strURL) {
20 var xhrHandle = new XMLHttpRequest(); 22 var xhrHandle = new XMLHttpRequest();
21 var strMethod = 'POST'; 23
22 var blnAsync = true;
23 var strServer = strURL;
24 var headerLabel = 'Content-Type'; 24 var headerLabel = 'Content-Type';
25 var headerValue = 'application/x-www-form-urlencoded'; 25 var headerValue = 'application/x-www-form-urlencoded';
26 26
...@@ -28,47 +28,18 @@ var xmlhttpHandler = function(strURL) { ...@@ -28,47 +28,18 @@ var xmlhttpHandler = function(strURL) {
28 var fnCallHandle; 28 var fnCallHandle;
29 var fnCallError; 29 var fnCallError;
30 30
31 var receiver = function(rsc) { 31 var api = {
32 if (xhrHandle.readyState != 4 || !blnLoading) { 32 url: strURL
33 return; 33 , method: 'POST'
34 } 34 , async: true
35 blnLoading = false;
36
37 try {
38 var objResult = eval('(' + xhrHandle.responseText + ')');
39 35
40 if (objResult['status'] != 0) {
41 fnCallError(objResult['data']);
42 return false;
43 }
44 } catch (e) {
45 fnCallError({message: xhrHandle.responseText});
46 return false;
47 }
48
49 if (typeof fnCallHandle == 'function') {
50 fnCallHandle(objResult.data);
51 }
52 }
53
54 var catcher = function(o) {
55 var msg = 'Unable to complete your request: "' + o.message + '"';
56
57 if (console && console.warn) {
58 console.warn(msg);
59 } else {
60 alert(msg);
61 }
62 }
63
64 return {
65 /** 36 /**
66 * Make a POST or GET call to the server 37 * Make a POST or GET call to the server
67 * @param {function} fnHandle The function to call after the server returns 38 * @param {function} fnHandle The function to call after the server returns
68 * @param {Object} oData the data object to send to the server 39 * @param {Object} oData the data object to send to the server
69 * @param {function} fnError The function to call if an error occurs (optional) 40 * @param {function} fnError The function to call if an error occurs (optional)
70 */ 41 */
71 call: function(fnHandle, oData, fnError) { 42 , call: function(fnHandle, oData, fnError) {
72 var sData = this.stringify(oData); 43 var sData = this.stringify(oData);
73 44
74 fnCallHandle = fnHandle; 45 fnCallHandle = fnHandle;
...@@ -76,7 +47,7 @@ var xmlhttpHandler = function(strURL) { ...@@ -76,7 +47,7 @@ var xmlhttpHandler = function(strURL) {
76 47
77 blnLoading = true; 48 blnLoading = true;
78 49
79 xhrHandle.open(strMethod, strServer, blnAsync); 50 xhrHandle.open(api.method, api.url, api.async);
80 xhrHandle.setRequestHeader(headerLabel, headerValue); 51 xhrHandle.setRequestHeader(headerLabel, headerValue);
81 xhrHandle.onreadystatechange = receiver; 52 xhrHandle.onreadystatechange = receiver;
82 xhrHandle.send(sData); 53 xhrHandle.send(sData);
...@@ -101,4 +72,39 @@ var xmlhttpHandler = function(strURL) { ...@@ -101,4 +72,39 @@ var xmlhttpHandler = function(strURL) {
101 return str.substr(1, str.length); 72 return str.substr(1, str.length);
102 } 73 }
103 } 74 }
104 } 75
76 var receiver = function(rsc) {
77 if (xhrHandle.readyState != 4 || !blnLoading) {
78 return;
79 }
80 blnLoading = false;
81
82 try {
83 var objResult = eval('(' + xhrHandle.responseText + ')');
84
85 if (objResult['status'] != 0) {
86 fnCallError(objResult['data']);
87 return false;
88 }
89 } catch (e) {
90 fnCallError({message: xhrHandle.responseText});
91 return false;
92 }
93
94 if (typeof fnCallHandle == 'function') {
95 fnCallHandle(objResult.data);
96 }
97 }
98
99 var catcher = function(o) {
100 var msg = 'Unable to complete your request:\n\n' + o.message;
101
102 if (console && console.warn) {
103 console.warn(msg);
104 } else {
105 alert(msg);
106 }
107 }
108
109 return api;
110 }
...\ No newline at end of file ...\ No newline at end of file
......