Adding current code to repository
0 parents
Showing
5 changed files
with
320 additions
and
0 deletions
Ajaxdata.php
0 → 100644
| 1 | <?php | ||
| 2 | /** | ||
| 3 | * Handles data preparation for XMLHTTP responses to xmlhttpHandler script | ||
| 4 | * | ||
| 5 | * @author Chris Boden | ||
| 6 | * @version 0.3 | ||
| 7 | */ | ||
| 8 | class Ajaxdata { | ||
| 9 | private $_data = Array(); | ||
| 10 | private static $exception = false; | ||
| 11 | |||
| 12 | /** | ||
| 13 | * @private | ||
| 14 | */ | ||
| 15 | final public function __destruct() { | ||
| 16 | if (self::$exception) { | ||
| 17 | return false; | ||
| 18 | } | ||
| 19 | |||
| 20 | $output = Array(); | ||
| 21 | $output['status'] = 0; | ||
| 22 | $output['data'] = $this->_data; | ||
| 23 | |||
| 24 | echo json_encode($output); | ||
| 25 | } | ||
| 26 | |||
| 27 | /** | ||
| 28 | * @private | ||
| 29 | */ | ||
| 30 | final public function __set($var, $val) { | ||
| 31 | // is this going to work for arrays? | ||
| 32 | $this->_data[$var] = $val; | ||
| 33 | } | ||
| 34 | |||
| 35 | /** | ||
| 36 | * @private | ||
| 37 | */ | ||
| 38 | final public function &__get($var) { | ||
| 39 | if (isset($this->_data[$var])) { | ||
| 40 | return $this->_data[$var]; | ||
| 41 | } else { | ||
| 42 | return ''; | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | /** | ||
| 47 | * @private | ||
| 48 | * possibly obsolete | ||
| 49 | */ | ||
| 50 | final public static function ExceptionThrown() { | ||
| 51 | self::$exception = true; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | /** | ||
| 56 | * Only 1 is declared at a time | ||
| 57 | * This exception is generated when doing an XHR | ||
| 58 | * | ||
| 59 | * @author Chris Boden | ||
| 60 | * @version 0.2 | ||
| 61 | */ | ||
| 62 | class JSONException extends Exception { | ||
| 63 | /** | ||
| 64 | * @private | ||
| 65 | */ | ||
| 66 | public function __construct($message=NULL, $code=0) { | ||
| 67 | parent::__construct($message, $code); | ||
| 68 | // die instead? | ||
| 69 | Ajaxdata::ExceptionThrown(); | ||
| 70 | } | ||
| 71 | |||
| 72 | /** | ||
| 73 | * @private | ||
| 74 | */ | ||
| 75 | public function __destruct() { | ||
| 76 | $output = Array(); | ||
| 77 | $output['status'] = 1; | ||
| 78 | $output['data'] = Array('message' => $this->getMessage()); | ||
| 79 | |||
| 80 | echo json_encode($output); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | ?> |
WP_Option.php
0 → 100644
| 1 | <?php | ||
| 2 | class WP_Option implements ArrayAccess, Countable { | ||
| 3 | private $_ns; | ||
| 4 | private $_data = Array(); | ||
| 5 | |||
| 6 | public function __construct($ns) { | ||
| 7 | $this->_ns = $ns; | ||
| 8 | $this->_data = get_option($ns); | ||
| 9 | } | ||
| 10 | |||
| 11 | public function offsetExists($var) { | ||
| 12 | return (isset($this->_data[$var]) ? true : false); | ||
| 13 | } | ||
| 14 | |||
| 15 | public function offsetGet($var) { | ||
| 16 | return ($this->offsetExists($var) ? $this->_data[$var] : ''); | ||
| 17 | } | ||
| 18 | |||
| 19 | public function offsetSet($var, $val) { | ||
| 20 | $this->_data[$var] = $val; | ||
| 21 | $this->save(); | ||
| 22 | } | ||
| 23 | |||
| 24 | public function offsetUnset($var) { | ||
| 25 | unset($this->_data[$var]); | ||
| 26 | $this->save(); | ||
| 27 | } | ||
| 28 | |||
| 29 | public function count() { | ||
| 30 | return count($this->_data); | ||
| 31 | } | ||
| 32 | |||
| 33 | public function save() { | ||
| 34 | update_option($this->_ns, $this->_data); | ||
| 35 | } | ||
| 36 | |||
| 37 | public function multiSet(Array $data) { | ||
| 38 | array_merge($this->_data, $data); | ||
| 39 | $this->save(); | ||
| 40 | } | ||
| 41 | } | ||
| 42 | ?> | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
tz-tools.php
0 → 100644
| 1 | <?php | ||
| 2 | /* | ||
| 3 | Plugin Name: Tenzing Tools | ||
| 4 | Version: 0.1 | ||
| 5 | Description: Various classes to help out with stuff | ||
| 6 | Author: Tenzing | ||
| 7 | */ | ||
| 8 | |||
| 9 | if (phpversion() < '5.2.2') { | ||
| 10 | die('PHP version 5.2.2 or greater is required'); | ||
| 11 | } | ||
| 12 | |||
| 13 | new TzTools(); | ||
| 14 | |||
| 15 | class TzTools { | ||
| 16 | public function __construct() { | ||
| 17 | set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__)); | ||
| 18 | spl_autoload_register(Array('TzTools', 'loader')); | ||
| 19 | |||
| 20 | require_once(dirname(__FILE__) . '/wp_functions.php'); | ||
| 21 | |||
| 22 | add_action('wp_print_scripts', Array('TzTools', 'registerScripts')); | ||
| 23 | } | ||
| 24 | |||
| 25 | public static function registerScripts() { | ||
| 26 | _register_script('xmlhttpHandler', plugins_url('xmlhttpHandler.js', __FILE__)); | ||
| 27 | } | ||
| 28 | |||
| 29 | public static function loader($class) { | ||
| 30 | $file = dirname(__FILE__) . '/' . $class . '.php'; | ||
| 31 | if (is_file($file)) { | ||
| 32 | require($file); | ||
| 33 | } | ||
| 34 | } | ||
| 35 | } | ||
| 36 | ?> | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
wp_functions.php
0 → 100644
| 1 | <?php | ||
| 2 | /** | ||
| 3 | * WordPress strongly advises against using functions that start with "wp_" | ||
| 4 | * as they are reserved for the core of WordPress and can change with any | ||
| 5 | * new release without providing backwards compatability. | ||
| 6 | * Any time a wp_ function needs to be called it is added here without the "wp" | ||
| 7 | * which calls the actual function. This way if WordPress changes a function | ||
| 8 | * the call only needs to be adjusted here, not the x# of times used else where | ||
| 9 | */ | ||
| 10 | |||
| 11 | function _register_script() { | ||
| 12 | $params = func_get_args(); | ||
| 13 | return call_user_func_array('wp_register_script', $params); | ||
| 14 | } | ||
| 15 | |||
| 16 | function _localize_script() { | ||
| 17 | $params = func_get_args(); | ||
| 18 | return call_user_func_array('wp_localize_script', $params); | ||
| 19 | } | ||
| 20 | |||
| 21 | function _enqueue_script() { | ||
| 22 | $params = func_get_args(); | ||
| 23 | return call_user_func_array('wp_enqueue_script', $params); | ||
| 24 | } | ||
| 25 | |||
| 26 | function _enqueue_style() { | ||
| 27 | $params = func_get_args(); | ||
| 28 | return call_user_func_array('wp_enqueue_style', $params); | ||
| 29 | } | ||
| 30 | |||
| 31 | function _die() { | ||
| 32 | $params = func_get_args(); | ||
| 33 | return call_user_func_array('wp_die', $params); | ||
| 34 | } | ||
| 35 | |||
| 36 | function _get_current_user() { | ||
| 37 | $params = func_get_args(); | ||
| 38 | return call_user_func_array('wp_get_current_user', $params); | ||
| 39 | } | ||
| 40 | |||
| 41 | function _insert_post() { | ||
| 42 | $params = func_get_args(); | ||
| 43 | return call_user_func_array('wp_insert_post', $params); | ||
| 44 | } | ||
| 45 | |||
| 46 | function _new_comment($commentdata) { | ||
| 47 | $params = func_get_args(); | ||
| 48 | return call_user_func_array('wp_new_comment', $params); | ||
| 49 | } | ||
| 50 | |||
| 51 | function _nonce_field() { | ||
| 52 | $params = func_get_args(); | ||
| 53 | return call_user_func_array('wp_nonce_field', $params); | ||
| 54 | } | ||
| 55 | ?> | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
xmlhttpHandler.js
0 → 100644
| 1 | /** | ||
| 2 | * An easy class to make Ajax requests with the xmlhttp object | ||
| 3 | * @author Chris Boden | ||
| 4 | * @version 1.0b | ||
| 5 | * @todo Figure out if/why blnLoading is needed | ||
| 6 | * @todo Some of the private variables need to be changable maybe just make all public | ||
| 7 | * @todo More testing | ||
| 8 | * @todo Make distinction between data error and exception | ||
| 9 | */ | ||
| 10 | |||
| 11 | /*@cc_on @if (@_win32 && @_jscript_version >= 5) if (!window.XMLHttpRequest) | ||
| 12 | window.XMLHttpRequest = function() { return new ActiveXObject('Microsoft.XMLHTTP') } | ||
| 13 | @end @*/ | ||
| 14 | |||
| 15 | /** | ||
| 16 | * @constructor | ||
| 17 | * @param {String} strURL This is the URL to pass requests to | ||
| 18 | */ | ||
| 19 | var xmlhttpHandler = function(strURL) { | ||
| 20 | var xhrHandle = new XMLHttpRequest(); | ||
| 21 | var strMethod = 'POST'; | ||
| 22 | var blnAsync = true; | ||
| 23 | var strServer = strURL; | ||
| 24 | var headerLabel = 'Content-Type'; | ||
| 25 | var headerValue = 'application/x-www-form-urlencoded'; | ||
| 26 | |||
| 27 | var blnLoading = false; | ||
| 28 | var fnCallHandle; | ||
| 29 | var fnCallError; | ||
| 30 | |||
| 31 | var receiver = function(rsc) { | ||
| 32 | if (xhrHandle.readyState != 4 || !blnLoading) { | ||
| 33 | return; | ||
| 34 | } | ||
| 35 | blnLoading = false; | ||
| 36 | |||
| 37 | try { | ||
| 38 | var objResult = eval('(' + xhrHandle.responseText + ')'); | ||
| 39 | |||
| 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 | /** | ||
| 66 | * Make a POST or GET call to the server | ||
| 67 | * @param {function} fnHandle The function to call after the server returns | ||
| 68 | * @param {Object} oData the data object to send to the server | ||
| 69 | * @param {function} fnError The function to call if an error occurs (optional) | ||
| 70 | */ | ||
| 71 | call: function(fnHandle, oData, fnError) { | ||
| 72 | var sData = this.stringify(oData); | ||
| 73 | |||
| 74 | fnCallHandle = fnHandle; | ||
| 75 | fnCallError = (fnError && typeof fnError == 'function' ? fnError : catcher); | ||
| 76 | |||
| 77 | blnLoading = true; | ||
| 78 | |||
| 79 | xhrHandle.open(strMethod, strServer, blnAsync); | ||
| 80 | xhrHandle.setRequestHeader(headerLabel, headerValue); | ||
| 81 | xhrHandle.onreadystatechange = receiver; | ||
| 82 | xhrHandle.send(sData); | ||
| 83 | } | ||
| 84 | |||
| 85 | /** | ||
| 86 | * Turns a Javascript data object into a string for the server to read | ||
| 87 | * @param {Object} obj The object to stringify | ||
| 88 | * @param {Boolean} child If there is further nesting | ||
| 89 | * @returns The string to send to the server | ||
| 90 | * @type String | ||
| 91 | */ | ||
| 92 | , stringify: function(obj, child) { | ||
| 93 | var str = ''; | ||
| 94 | for (var strKey in obj) { | ||
| 95 | if (typeof obj[strKey] == 'object') { | ||
| 96 | str += '&' + this.stringify(obj[strKey], (child ? child + '[' + strKey + ']' : strKey)); | ||
| 97 | } else { | ||
| 98 | str += '&' + (child ? child + '[' : '') + strKey + (child ? ']' : '') + '=' + encodeURIComponent(obj[strKey]); | ||
| 99 | } | ||
| 100 | } | ||
| 101 | return str.substr(1, str.length); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | } |
-
Please register or sign in to post a comment