var WPAjax = Class.create(); Object.extend(WPAjax.prototype, Ajax.Request.prototype); Object.extend(WPAjax.prototype, { WPComplete: false, // onComplete function WPError: false, // onWPError function initialize: function(url, responseEl) { var tempObj = this; this.transport = Ajax.getTransport(); if ( !this.transport ) return false; this.setOptions( { parameters: 'cookie=' + encodeURIComponent(document.cookie), onComplete: function(transport) { // transport = XMLHttpRequest object if ( tempObj.parseAjaxResponse() ) { if ( 'function' == typeof tempObj.WPComplete ) tempObj.WPComplete(transport); } else if ( 'function' == typeof tempObj.WPError ) // if response corresponds to an error (bad data, say, not 404) tempObj.WPError(transport); } }); this.url = url ? url : '/wp-admin/admin-ajax.php'; this.getResponseElement(responseEl); }, addArg: function(key, value) { var a = []; a[encodeURIComponent(key)] = encodeURIComponent(value); this.options.parameters = $H(this.options.parameters).merge($H(a)); }, getResponseElement: function(r) { var p = $(r + '-p'); if ( !p ) { new Insertion.Bottom(r, ""); var p = $(r + '-p'); } this.myResponseElement = p; }, parseAjaxResponse: function() { // 1 = good, 0 = strange (bad data?), -1 = you lack permission if ( this.transport.responseXML && typeof this.transport.responseXML == 'object' && ( this.transport.responseXML.xml || 'undefined' == typeof this.transport.responseXML.xml ) ) { var err = this.transport.responseXML.getElementsByTagName('wp_error'); if ( err[0] ) { var msg = $A(err).inject( '', function(a, b) { return a + '
' + b.firstChild.nodeValue + '
'; } ); Element.update(this.myResponseElement,'' + r + '