AJAX responsiveness improvements from mdawaffe. fixes #3099
git-svn-id: http://svn.automattic.com/wordpress/trunk@4187 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
ad1924f837
commit
ce0c2a2f64
|
@ -5,7 +5,6 @@ require_once('admin-db.php');
|
|||
|
||||
define('DOING_AJAX', true);
|
||||
|
||||
|
||||
check_ajax_referer();
|
||||
if ( !is_user_logged_in() )
|
||||
die('-1');
|
||||
|
@ -13,19 +12,17 @@ if ( !is_user_logged_in() )
|
|||
function get_out_now() { exit; }
|
||||
add_action( 'shutdown', 'get_out_now', -1 );
|
||||
|
||||
function wp_ajax_echo_meta( $pid, $mid, $key, $value ) {
|
||||
function wp_ajax_meta_row( $pid, $mid, $key, $value ) {
|
||||
$value = wp_specialchars($value, true);
|
||||
$key_js = addslashes(wp_specialchars($key, 'double'));
|
||||
$key = wp_specialchars($key, true);
|
||||
$r = "<meta><id>$mid</id><postid>$pid</postid><newitem><![CDATA[<table><tbody>";
|
||||
$r .= "<tr id='meta-$mid'><td valign='top'>";
|
||||
$r .= "<input name='meta[$mid][key]' tabindex='6' onkeypress='return killSubmit(\"theList.ajaxUpdater('meta','meta-$mid');\",event);' type='text' size='20' value='$key' />";
|
||||
$r .= "</td><td><textarea name='meta[$mid][value]' tabindex='6' rows='2' cols='30'>$value</textarea></td><td align='center'>";
|
||||
$r .= "<input name='updatemeta' type='button' class='updatemeta' tabindex='6' value='Update' onclick='return theList.ajaxUpdater('meta','meta-$mid');' /><br />";
|
||||
$r .= "<input name='deletemeta[$mid]' type='submit' onclick=\"return deleteSomething( 'meta', $mid, '";
|
||||
$r .= sprintf(__("You are about to delete the "%s" custom field on this post.\\n"OK" to delete, "Cancel" to stop."), $key_js);
|
||||
$r .= "' );\" class='deletemeta' tabindex='6' value='Delete' />";
|
||||
$r .= "</td></tr></tbody></table>]]></newitem></meta>";
|
||||
$r .= "' );\" class='deletemeta' tabindex='6' value='Delete' /></td></tr>";
|
||||
return $r;
|
||||
}
|
||||
|
||||
|
@ -113,7 +110,7 @@ case 'add-category' : // On the Fly
|
|||
if ( !current_user_can( 'manage_categories' ) )
|
||||
die('-1');
|
||||
$names = explode(',', $_POST['newcat']);
|
||||
$r = "<?xml version='1.0' standalone='yes'?><ajaxresponse>";
|
||||
$x = new WP_Ajax_Response();
|
||||
foreach ( $names as $cat_name ) {
|
||||
$cat_name = trim($cat_name);
|
||||
if ( !$category_nicename = sanitize_title($cat_name) )
|
||||
|
@ -121,14 +118,13 @@ case 'add-category' : // On the Fly
|
|||
if ( !$cat_id = category_exists( $cat_name ) )
|
||||
$cat_id = wp_create_category( $cat_name );
|
||||
$cat_name = wp_specialchars(stripslashes($cat_name));
|
||||
$r .= "<category><id>$cat_id</id><newitem><![CDATA[";
|
||||
$r .= "<li id='category-$cat_id'><label for='in-category-$cat_id' class='selectit'>";
|
||||
$r .= "<input value='$cat_id' type='checkbox' checked='checked' name='post_category[]' id='in-category-$cat_id'/> $cat_name</label></li>";
|
||||
$r .= "]]></newitem></category>";
|
||||
$x->add( array(
|
||||
'what' => 'category',
|
||||
'id' => $cat_id,
|
||||
'data' => "<li id='category-$cat_id'><label for='in-category-$cat_id' class='selectit'><input value='$cat_id' type='checkbox' checked='checked' name='post_category[]' id='in-category-$cat_id'/> $cat_name</label></li>"
|
||||
) );
|
||||
}
|
||||
$r .= '</ajaxresponse>';
|
||||
header('Content-type: text/xml');
|
||||
die($r);
|
||||
$x->send();
|
||||
break;
|
||||
case 'add-cat' : // From Manage->Categories
|
||||
if ( !current_user_can( 'manage_categories' ) )
|
||||
|
@ -147,12 +143,13 @@ case 'add-cat' : // From Manage->Categories
|
|||
}
|
||||
$cat_full_name = wp_specialchars( $cat_full_name, 1 );
|
||||
|
||||
$r = "<?xml version='1.0' standalone='yes'?><ajaxresponse>";
|
||||
$r .= "<cat><id>$cat->cat_ID</id><name>$cat_full_name</name><newitem><![CDATA[<table><tbody>";
|
||||
$r .= _cat_row( $cat, $level, $cat_full_name );
|
||||
$r .= "</tbody></table>]]></newitem></cat></ajaxresponse>";
|
||||
header('Content-type: text/xml');
|
||||
die($r);
|
||||
$x = new WP_Ajax_Response( array(
|
||||
'what' => 'cat',
|
||||
'id' => $cat->cat_ID,
|
||||
'data' => _cat_row( $cat, $level, $cat_full_name ),
|
||||
'supplemental' => array('name' => $cat_full_name)
|
||||
) );
|
||||
$x->send();
|
||||
break;
|
||||
case 'add-meta' :
|
||||
if ( !current_user_can( 'edit_post', $id ) )
|
||||
|
@ -171,11 +168,13 @@ case 'add-meta' :
|
|||
$value = $meta->meta_value;
|
||||
$pid = (int) $meta->post_id;
|
||||
|
||||
$r = "<?xml version='1.0' standalone='yes'?><ajaxresponse>";
|
||||
$r .= wp_ajax_echo_meta( $pid, $mid, $key, $value );
|
||||
$r .= '</ajaxresponse>';
|
||||
header('Content-type: text/xml');
|
||||
die($r);
|
||||
$x = new WP_Ajax_Response( array(
|
||||
'what' => 'meta',
|
||||
'id' => $mid,
|
||||
'data' => wp_ajax_meta_row( $pid, $mid, $key, $value ),
|
||||
'supplemental' => array('postid' => $pid)
|
||||
) );
|
||||
$x->send();
|
||||
break;
|
||||
case 'update-meta' :
|
||||
$mid = (int) array_pop(array_keys($_POST['meta']));
|
||||
|
@ -185,33 +184,36 @@ case 'update-meta' :
|
|||
die('0');
|
||||
if ( !current_user_can( 'edit_post', $meta->post_id ) )
|
||||
die('-1');
|
||||
$r = "<?xml version='1.0' standalone='yes'?><ajaxresponse>";
|
||||
if ( $u = update_meta( $mid, $key, $value ) ) {
|
||||
$key = stripslashes($key);
|
||||
$value = stripslashes($value);
|
||||
$r .= wp_ajax_echo_meta( $meta->post_id, $mid, $key, $value );
|
||||
$x = new WP_Ajax_Response( array(
|
||||
'what' => 'meta',
|
||||
'id' => $mid,
|
||||
'data' => wp_ajax_meta_row( $meta->post_id, $mid, $key, $value ),
|
||||
'supplemental' => array('postid' => $meta->post_id)
|
||||
) );
|
||||
$x->send();
|
||||
}
|
||||
$r .= '</ajaxresponse>';
|
||||
header('Content-type: text/xml');
|
||||
die($r);
|
||||
die('0');
|
||||
break;
|
||||
case 'add-user' :
|
||||
if ( !current_user_can('edit_users') )
|
||||
die('-1');
|
||||
require_once(ABSPATH . WPINC . '/registration.php');
|
||||
$user_id = add_user();
|
||||
if ( is_wp_error( $user_id ) ) {
|
||||
foreach( $user_id->get_error_messages() as $message )
|
||||
echo "$message<br />";
|
||||
exit;
|
||||
} elseif ( !$user_id ) {
|
||||
if ( !$user_id = add_user() )
|
||||
die('0');
|
||||
elseif ( is_wp_error( $user_id ) ) {
|
||||
foreach( $user_id->get_error_messages() as $message )
|
||||
echo "<p>$message<p>";
|
||||
exit;
|
||||
}
|
||||
$r = "<?xml version='1.0' standalone='yes'?><ajaxresponse><user><id>$user_id</id><newitem><![CDATA[<table><tbody>";
|
||||
$r .= user_row( $user_id );
|
||||
$r .= "</tbody></table>]]></newitem></user></ajaxresponse>";
|
||||
header('Content-type: text/xml');
|
||||
die($r);
|
||||
$x = new WP_Ajax_Response( array(
|
||||
'what' => 'user',
|
||||
'id' => $user_id,
|
||||
'data' => user_row( $user_id )
|
||||
) );
|
||||
$x->send();
|
||||
break;
|
||||
case 'autosave' :
|
||||
$_POST['post_content'] = $_POST['content'];
|
||||
|
|
|
@ -5,31 +5,10 @@ cache_javascript_headers();
|
|||
addLoadEvent(function(){catList=new listMan('categorychecklist');catList.ajaxRespEl='jaxcat';catList.topAdder=1;catList.alt=0;catList.showLink=0;});
|
||||
addLoadEvent(newCatAddIn);
|
||||
function newCatAddIn() {
|
||||
if ( !document.getElementById('jaxcat') ) return false;
|
||||
var ajaxcat = document.createElement('span');
|
||||
ajaxcat.id = 'ajaxcat';
|
||||
|
||||
newcat = document.createElement('input');
|
||||
newcat.type = 'text';
|
||||
newcat.name = 'newcat';
|
||||
newcat.id = 'newcat';
|
||||
newcat.size = '16';
|
||||
newcat.setAttribute('autocomplete', 'off');
|
||||
newcat.onkeypress = function(e) { return killSubmit("catList.ajaxAdder('category','categorydiv');", e); };
|
||||
|
||||
var newcatSub = document.createElement('input');
|
||||
newcatSub.type = 'button';
|
||||
newcatSub.name = 'Button';
|
||||
newcatSub.id = 'catadd';
|
||||
newcatSub.value = '<?php _e('Add'); ?>';
|
||||
newcatSub.onclick = function() { catList.ajaxAdder('category', 'categorydiv'); };
|
||||
|
||||
ajaxcat.appendChild(newcat);
|
||||
ajaxcat.appendChild(newcatSub);
|
||||
document.getElementById('jaxcat').appendChild(ajaxcat);
|
||||
|
||||
howto = document.createElement('span');
|
||||
howto.innerHTML = "<?php _e('Separate multiple categories with commas.'); ?>";
|
||||
howto.id = 'howto';
|
||||
ajaxcat.appendChild(howto);
|
||||
var jaxcat = $('jaxcat');
|
||||
if ( !jaxcat )
|
||||
return false;
|
||||
jaxcat.update('<span id="ajaxcat"><input type="text" name="newcat" id="newcat" size="16" autocomplete="off"/><input type="button" name="Button" id="catadd" value="<?php _e('Add'); ?>"/><span id="howto"><?php _e('Separate multiple categories with commas.'); ?></span></span>');
|
||||
$('newcat').onkeypress = function(e) { return killSubmit("catList.ajaxAdder('category','jaxcat');", e); };
|
||||
$('catadd').onclick = function() { catList.ajaxAdder('category', 'jaxcat'); };
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
addLoadEvent(function() {
|
||||
if (!theList.theList) return false;
|
||||
document.forms.addcat.submit.onclick = function(e) {return killSubmit('theList.ajaxAdder("cat", "addcat");', e); };
|
||||
theList.addComplete = function(what, where, update) {
|
||||
var name = getNodeValue(theList.ajaxAdd.responseXML, 'name');
|
||||
var id = getNodeValue(theList.ajaxAdd.responseXML, 'id');
|
||||
theList.addComplete = function(what, where, update, transport) {
|
||||
var name = getNodeValue(transport.responseXML, 'name');
|
||||
var id = transport.responseXML.getElementsByTagName(what)[0].getAttribute('id');
|
||||
var options = document.forms['addcat'].category_parent.options;
|
||||
options[options.length] = new Option(name, id);
|
||||
};
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
function customFieldsOnComplete() {
|
||||
var pidEl = document.getElementById('post_ID');
|
||||
function customFieldsOnComplete( what, where, update, transport ) {
|
||||
var pidEl = $('post_ID');
|
||||
pidEl.name = 'post_ID';
|
||||
pidEl.value = getNodeValue(theList.ajaxAdd.responseXML, 'postid');
|
||||
var aEl = document.getElementById('hiddenaction')
|
||||
pidEl.value = getNodeValue(transport.responseXML, 'postid');
|
||||
var aEl = $('hiddenaction')
|
||||
if ( aEl.value == 'post' ) aEl.value = 'postajaxpost';
|
||||
}
|
||||
addLoadEvent(customFieldsAddIn);
|
||||
|
@ -21,6 +21,6 @@ function customFieldsAddIn() {
|
|||
}
|
||||
}
|
||||
|
||||
document.getElementById('metakeyinput').onkeypress = function(e) {return killSubmit('theList.inputData+="&id="+document.getElementById("post_ID").value;theList.ajaxAdder("meta", "newmeta");', e); };
|
||||
document.getElementById('updatemetasub').onclick = function(e) {return killSubmit('theList.inputData+="&id="+document.getElementById("post_ID").value;theList.ajaxAdder("meta", "newmeta");', e); };
|
||||
$('metakeyinput').onkeypress = function(e) {return killSubmit('theList.inputData+="&id="+$("post_ID").value;theList.ajaxAdder("meta", "newmeta");', e); };
|
||||
$('updatemetasub').onclick = function(e) {return killSubmit('theList.inputData+="&id="+$("post_ID").value;theList.ajaxAdder("meta", "newmeta");', e); };
|
||||
}
|
||||
|
|
|
@ -1,170 +0,0 @@
|
|||
<?php
|
||||
require_once('admin.php');
|
||||
cache_javascript_headers();
|
||||
$handler = get_option( 'siteurl' ) . '/wp-admin/admin-ajax.php';
|
||||
?>
|
||||
addLoadEvent(function(){theList=new listMan();});
|
||||
function deleteSomething(what,id,message,obj){if(!obj)obj=theList;if(!message)message="<?php printf(__('Are you sure you want to delete this %s?'),"'+what+'"); ?>";if(confirm(message))return obj.ajaxDelete(what,id);else return false;}
|
||||
function dimSomething(what,id,dimClass,obj){if(!obj)obj=theList;return obj.ajaxDimmer(what,id,dimClass);}
|
||||
|
||||
function WPAjax(file, responseEl){//class WPAjax extends sack
|
||||
this.getResponseElement=function(r){var p=document.getElementById(r+'-p');if(!p){p=document.createElement('span');p.id=r+'-p';document.getElementById(r).appendChild(p);}this.myResponseElement=p; }
|
||||
this.parseAjaxResponse=function(){
|
||||
if(isNaN(this.response)){this.myResponseElement.innerHTML='<div class="error"><p>'+this.response+'</p></div>';return false;}
|
||||
this.response=parseInt(this.response,10);
|
||||
if(-1==this.response){this.myResponseElement.innerHTML="<div class='error'><p><?php _e("You don't have permission to do that."); ?></p></div>";return false;}
|
||||
else if(0==this.response){this.myResponseElement.innerHTML="<div class='error'><p><?php _e("Something odd happened. Try refreshing the page? Either that or what you tried to change never existed in the first place."); ?></p></div>";return false;}
|
||||
return true;
|
||||
}
|
||||
this.parseAjaxResponseXML=function(){
|
||||
if(this.responseXML&&typeof this.responseXML=='object')return true;
|
||||
if(isNaN(this.response)){this.myResponseElement.innerHTML='<div class="error"><p>'+this.response+'</p></div>';return false;}
|
||||
var r=parseInt(this.response,10);
|
||||
if(-1==r){this.myResponseElement.innerHTML="<div class='error'><p><?php _e("You don't have permission to do that."); ?></p></div>";}
|
||||
else if(0==r){this.myResponseElement.innerHTML="<div class='error'><p><?php _e("Invalid Entry."); ?></p></div>";}
|
||||
return false;
|
||||
}
|
||||
this.init(file,responseEl);
|
||||
} WPAjax.prototype=new sack;
|
||||
WPAjax.prototype.init=function(f,r){
|
||||
this.encVar('cookie', document.cookie);
|
||||
this.requestFile=f?f:'<?php echo $handler; ?>';this.getResponseElement(r);this.method='POST';
|
||||
}
|
||||
|
||||
function listMan(theListId){
|
||||
this.theList=null;this.theListId=theListId;
|
||||
this.ajaxRespEl=null;this.ajaxHandler='<?php echo $handler; ?>';
|
||||
this.inputData='';this.clearInputs=new Array();this.showLink=1;
|
||||
this.topAdder=0;this.alt='alternate';this.recolorPos;this.reg_color='#FFFFFF';this.alt_color='#F1F1F1';
|
||||
this.addComplete=null;this.delComplete=null;this.dimComplete=null;
|
||||
var listType;var listItems;
|
||||
self.aTrap=0;
|
||||
|
||||
this.ajaxAdder=function(what,where,update){//for TR, server must wrap TR in TABLE TBODY. this.makeEl cleans it
|
||||
if(self.aTrap)return;self.aTrap=1;setTimeout('aTrap=0',300);
|
||||
this.ajaxAdd=new WPAjax(this.ajaxHandler,this.ajaxRespEl?this.ajaxRespEl:'ajax-response');
|
||||
if(this.ajaxAdd.failed)return true;
|
||||
this.grabInputs(where);
|
||||
var tempObj=this;
|
||||
this.ajaxAdd.onCompletion=function(){
|
||||
if(!this.parseAjaxResponseXML())return;
|
||||
var newItems=this.responseXML.getElementsByTagName(what);
|
||||
if(tempObj.topAdder)tempObj.recolorPos=0;
|
||||
if(newItems){for (c=0;c<newItems.length;c++){
|
||||
var id=getNodeValue(newItems[c],'id');
|
||||
var exists=document.getElementById(what+'-'+id);
|
||||
if(exists)tempObj.replaceListItem(exists.id,getNodeValue(newItems[c],'newitem'),newItems.length,update);
|
||||
else tempObj.addListItem(getNodeValue(newItems[c],'newitem'),newItems.length);
|
||||
}}
|
||||
tempObj.inputData='';
|
||||
if(tempObj.showLink){this.myResponseElement.innerHTML='<div id="jumplink" class="updated fade"><p><a href="#'+what+'-'+id+'"><?php _e('Jump to new item'); ?></a></p></div>';}
|
||||
else this.myResponseElement.innerHTML='';
|
||||
for(var i=0;i<tempObj.clearInputs.length;i++){try{var theI=document.getElementById(tempObj.clearInputs[i]);if(theI.tagName.match(/select/i))theI.selectedIndex=0;else theI.value='';}catch(e){}}
|
||||
if(tempObj.addComplete&&typeof tempObj.addComplete=='function')tempObj.addComplete(what,where,update);
|
||||
tempObj.recolorList(tempObj.recolorPos,1000);
|
||||
}
|
||||
this.ajaxAdd.runAJAX('action='+(update?'update-':'add-')+what+this.inputData);
|
||||
return false;
|
||||
}
|
||||
this.ajaxUpdater=function(what,where){return this.ajaxAdder(what,where,true);}
|
||||
this.ajaxDelete=function(what,id){
|
||||
if(self.aTrap)return;self.aTrap=1;setTimeout('aTrap=0',300);
|
||||
this.ajaxDel=new WPAjax(this.ajaxHandler,this.ajaxRespEl?this.ajaxRespEl:'ajax-response');
|
||||
if(this.ajaxDel.failed)return true;
|
||||
var tempObj=this;
|
||||
this.ajaxDel.onCompletion=function(){if(this.parseAjaxResponse()){tempObj.removeListItem(what.replace('-as-spam','')+'-'+id);this.myResponseElement.innerHTML='';if(tempObj.delComplete&&typeof tempObj.delComplete=='function')tempObj.delComplete(what,id);tempObj.recolorList(tempObj.recolorPos,1000)}};
|
||||
this.ajaxDel.runAJAX('action=delete-'+what+'&id='+id);
|
||||
return false;
|
||||
}
|
||||
this.ajaxDimmer=function(what,id,dimClass){
|
||||
if(self.aTrap)return;self.aTrap=1;setTimeout('aTrap=0',300);
|
||||
this.ajaxDim=new WPAjax(this.ajaxHandler,this.ajaxRespEl?this.ajaxRespEl:'ajax-response');
|
||||
if(this.ajaxDim.failed)return true;
|
||||
var tempObj=this;
|
||||
this.ajaxDim.onCompletion=function(){if(this.parseAjaxResponse()){tempObj.dimItem(what+'-'+id,dimClass);this.myResponseElement.innerHTML='';if(tempObj.dimComplete&&typeof tempObj.dimComplete=='function')tempObj.dimComplete(what,id,dimClass);}};
|
||||
this.ajaxDim.runAJAX('action=dim-'+what+'&id='+id);
|
||||
return false;
|
||||
}
|
||||
this.makeEl=function(h){var fakeItem=document.createElement('div');fakeItem.innerHTML=h;var r=fakeItem.firstChild;while(r.tagName.match(/(table|tbody)/i)){r=r.firstChild;}return r;}
|
||||
this.addListItem=function(h,tot){
|
||||
newItem=this.makeEl(h);
|
||||
if(this.topAdder){var firstItem=this.theList.getElementsByTagName('table'==listType?'tr':'li')[0];listItems.unshift(newItem.id);this.recolorPos++}
|
||||
else{listItems.push(newItem.id);this.recolorPos=listItems.length;}
|
||||
if(this.alt&&!((tot-this.recolorPos)%2))newItem.className+=' '+this.alt;
|
||||
if(firstItem)firstItem.parentNode.insertBefore(newItem,firstItem);
|
||||
else this.theList.appendChild(newItem);
|
||||
Fat.fade_element(newItem.id);
|
||||
}
|
||||
this.removeListItem=function(id,noFade){
|
||||
if(!noFade)Fat.fade_element(id,null,700,'#FF3333');
|
||||
var theItem=document.getElementById(id);
|
||||
if(!noFade){var func=encloseFunc(function(a){a.parentNode.removeChild(a);},theItem);setTimeout(func,705);}
|
||||
else{theItem.parentNode.removeChild(theItem);}
|
||||
var pos=this.getListPos(id);
|
||||
listItems.splice(pos,1);
|
||||
}
|
||||
this.replaceListItem=function(id,h,tot,update){
|
||||
if(!update){this.removeListItem(id,true);this.addListItem(h,tot);return;}
|
||||
var newItem=this.makeEl(h);
|
||||
var oldItem=document.getElementById(id);
|
||||
var pos=this.getListPos(oldItem.id,1);if(this.alt&&!(pos%2))newItem.className+=' '+this.alt;
|
||||
oldItem.parentNode.replaceChild(newItem,oldItem);
|
||||
Fat.fade_element(newItem.id);
|
||||
}
|
||||
this.dimItem=function(id,dimClass,noFade){
|
||||
var theItem=document.getElementById(id);
|
||||
if(theItem.className.match(dimClass)){if(!noFade)Fat.fade_element(id,null,700,null);theItem.className=theItem.className.replace(dimClass,'');}
|
||||
else{if(!noFade)Fat.fade_element(id,null,700,'#FF3333');theItem.className=theItem.className+' '+dimClass;}
|
||||
}
|
||||
this.grabInputs=function(elId){//text,password,hidden,textarea,select
|
||||
var theItem=document.getElementById(elId);
|
||||
var inputs=new Array();
|
||||
inputs.push(theItem.getElementsByTagName('input'),theItem.getElementsByTagName('textarea'),theItem.getElementsByTagName('select'));
|
||||
for(var a=0;a<inputs.length;a++){
|
||||
for(var i=0;i<inputs[a].length;i++){
|
||||
if('action'==inputs[a][i].name)continue;
|
||||
if('text'==inputs[a][i].type||'password'==inputs[a][i].type||'hidden'==inputs[a][i].type||inputs[a][i].tagName.match(/textarea/i)){
|
||||
this.inputData+='&'+inputs[a][i].name+'='+encodeURIComponent(inputs[a][i].value);if('hidden'!=inputs[a][i].type)this.clearInputs.push(inputs[a][i].id);
|
||||
}else if(inputs[a][i].tagName.match(/select/i)){
|
||||
this.inputData+='&'+inputs[a][i].name+'='+encodeURIComponent(inputs[a][i].options[inputs[a][i].selectedIndex].value);this.clearInputs.push(inputs[a][i].id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
this.getListPos=function(id,n){for(var i=0;i<listItems.length;i++){if(id==listItems[i]){var pos=i;break;}}if(!n){if(pos<this.recolorPos)this.recolorPos=pos;}return pos;}
|
||||
this.getListItems=function(){
|
||||
if(this.theList)return;
|
||||
listItems=new Array();
|
||||
if(this.theListId){this.theList=document.getElementById(this.theListId);if(!this.theList)return false;}
|
||||
else{this.theList=document.getElementById('the-list');if(this.theList)this.theListId='the-list';}
|
||||
if(this.theList){
|
||||
var items=this.theList.getElementsByTagName('tr');listType='table';
|
||||
if(!items[0]){items=this.theList.getElementsByTagName('li');listType='list';}
|
||||
for(var i=0;i<items.length;i++){listItems.push(items[i].id);}
|
||||
this.recolorPos=listItems.length;
|
||||
}
|
||||
}
|
||||
this.recolorList=function(pos,dur){
|
||||
if(!this.alt)return;if(!pos)pos=0;this.recolorPos=listItems.length;
|
||||
for(var i=pos;i<listItems.length;i++){var e=document.getElementById(listItems[i]);if(i%2)e.className=e.className.replace(this.alt,'fade-'+this.alt_color.slice(1));else e.className+=' '+this.alt+' fade-'+this.reg_color.slice(1);e.style.backgroundColor='';}
|
||||
Fat.fade_all(dur);
|
||||
var func=encloseFunc(function(l){for(var i=0;i<l.length;i++){var e=document.getElementById(l[i]);e.className=e.className.replace(/fade-[a-f0-9]{6}/i,'');}},listItems);
|
||||
setTimeout(func,dur+5);
|
||||
}
|
||||
this.getListItems();
|
||||
}
|
||||
//No submit unless code returns true.
|
||||
function killSubmit ( code, e ) {
|
||||
e = e ? e : window.event;
|
||||
if ( !e ) return;
|
||||
var t = e.target ? e.target : e.srcElement;
|
||||
if ( ( 'text' == t.type && e.keyCode == 13 ) || ( 'submit' == t.type && 'click' == e.type ) ) {
|
||||
if ( ( 'string' == typeof code && !eval(code) ) || 'function' == typeof code && !code() ) {
|
||||
if ( !eval(code) ) { e.returnValue = false; e.cancelBubble = true; return false; }
|
||||
}
|
||||
}
|
||||
}
|
||||
//Pretty func adapted from ALA http://www.alistapart.com/articles/gettingstartedwithajax
|
||||
function getNodeValue(tree,el){try { var r = tree.getElementsByTagName(el)[0].firstChild.nodeValue; } catch(err) { var r = null; } return r; }
|
||||
//Generic but lame JS closure
|
||||
function encloseFunc(f){var a=arguments[1];return function(){return f(a);}}
|
|
@ -490,12 +490,10 @@ foreach ( (array) $roleclass as $user_object ) {
|
|||
|
||||
<?php if ( is_wp_error( $add_user_errors ) ) : ?>
|
||||
<div class="error">
|
||||
<ul>
|
||||
<?php
|
||||
foreach ( $add_user_errors->get_error_messages() as $message )
|
||||
echo "$message<br />";
|
||||
echo "<p>$message<p>";
|
||||
?>
|
||||
</ul>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<div id="ajax-response"></div>
|
||||
|
|
|
@ -709,4 +709,67 @@ class Walker_CategoryDropdown extends Walker {
|
|||
}
|
||||
}
|
||||
|
||||
class WP_Ajax_Response {
|
||||
var $responses = array();
|
||||
|
||||
function WP_Ajax_Response( $args = '' ) {
|
||||
if ( !empty($args) )
|
||||
$this->add($args);
|
||||
}
|
||||
|
||||
// a WP_Error object can be passed in 'id' or 'data'
|
||||
function add( $args = '' ) {
|
||||
if ( is_array($args) )
|
||||
$r = &$args;
|
||||
else
|
||||
parse_str($args, $r);
|
||||
|
||||
$defaults = array('what' => 'object', 'action' => false, 'id' => '0', 'old_id' => false,
|
||||
'data' => '', 'supplemental' => array());
|
||||
|
||||
$r = array_merge($defaults, $r);
|
||||
extract($r);
|
||||
|
||||
if ( is_wp_error($id) ) {
|
||||
$data = $id;
|
||||
$id = 0;
|
||||
}
|
||||
|
||||
$response = '';
|
||||
if ( is_wp_error($data) )
|
||||
foreach ( $data->get_error_codes() as $code )
|
||||
$response .= "<wp_error code='$code'><![CDATA[" . $data->get_error_message($code) . "]]></wp_error>";
|
||||
else
|
||||
$response = "<response_data><![CDATA[$data]]></response_data>";
|
||||
|
||||
$s = '';
|
||||
if ( (array) $supplemental )
|
||||
foreach ( $supplemental as $k => $v )
|
||||
$s .= "<$k><![CDATA[$v]]></$k>";
|
||||
|
||||
if ( false === $action )
|
||||
$action = $_POST['action'];
|
||||
|
||||
$x = '';
|
||||
$x .= "<response action='$action_$id'>"; // The action attribute in the xml output is formatted like a nonce action
|
||||
$x .= "<$what id='$id'" . ( false !== $old_id ? "old_id='$old_id'>" : '>' );
|
||||
$x .= $response;
|
||||
$x .= $s;
|
||||
$x .= "</$what>";
|
||||
$x .= "</response>";
|
||||
|
||||
$this->responses[] = $x;
|
||||
return $x;
|
||||
}
|
||||
|
||||
function send() {
|
||||
header('Content-type: text/xml');
|
||||
echo "<?xml version='1.0' standalone='yes'?><wp_ajax>";
|
||||
foreach ( $this->responses as $response )
|
||||
echo $response;
|
||||
echo '</wp_ajax>';
|
||||
die();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
|
|
@ -0,0 +1,268 @@
|
|||
<?php
|
||||
@require_once('../../wp-config.php');
|
||||
cache_javascript_headers();
|
||||
$handler = get_option( 'siteurl' ) . '/wp-admin/admin-ajax.php';
|
||||
?>
|
||||
addLoadEvent(function(){theList=new listMan();});
|
||||
function deleteSomething(what,id,message,obj){if(!obj)obj=theList;if(!message)message="<?php printf(__('Are you sure you want to delete this %s?'),"'+what+'"); ?>";if(confirm(message))return obj.ajaxDelete(what,id);else return false;}
|
||||
function dimSomething(what,id,dimClass,obj){if(!obj)obj=theList;return obj.ajaxDimmer(what,id,dimClass);}
|
||||
|
||||
var listMan = Class.create();
|
||||
Object.extend(listMan.prototype, {
|
||||
ajaxRespEl: 'ajax-response',
|
||||
ajaxHandler: '<?php echo $handler; ?>',
|
||||
inputData: '',
|
||||
clearInputs: [],
|
||||
showLink: true,
|
||||
topAdder: false,
|
||||
alt: 'alternate',
|
||||
altOffset: 0,
|
||||
addComplete: null,
|
||||
delComplete: null,
|
||||
dimComplete: null,
|
||||
dataStore: null,
|
||||
formStore: null,
|
||||
|
||||
initialize: function(theListId) {
|
||||
this.theList = $(theListId ? theListId : 'the-list');
|
||||
if ( !this.theList )
|
||||
return false;
|
||||
this.theList.cleanWhitespace();
|
||||
},
|
||||
|
||||
// sends add-what and fields contained in where
|
||||
// recieves html with top element having an id like what-#
|
||||
ajaxAdder: function( what, where, update ) { // Do NOT wrap TR in TABLE TBODY
|
||||
var ajaxAdd = new WPAjax( this.ajaxHandler, this.ajaxRespEl );
|
||||
if ( ajaxAdd.notInitialized() )
|
||||
return true;
|
||||
ajaxAdd.options.parameters += '&action=' + ( update ? 'update-' : 'add-' ) + what + '&' + this.grabInputs( where, ajaxAdd ) + this.inputData;
|
||||
var tempObj=this;
|
||||
ajaxAdd.addOnComplete( function(transport) {
|
||||
var newItems = $A(transport.responseXML.getElementsByTagName(what));
|
||||
if ( newItems ) {
|
||||
newItems.each( function(i) {
|
||||
var id = i.getAttribute('id');
|
||||
var exists = $(what+'-'+id);
|
||||
if ( exists )
|
||||
tempObj.replaceListItem( exists, getNodeValue(i,'response_data'), update );
|
||||
else
|
||||
tempObj.addListItem( getNodeValue(i, 'response_data') );
|
||||
if ( tempObj.showLink )
|
||||
tempObj.showLink = id;
|
||||
});
|
||||
}
|
||||
ajaxAdd.myResponseElement.update(tempObj.showLink ? ( "<div id='jumplink' class='updated fade'><p><a href='#" + what + '-' + tempObj.showLink + "'><?php _e('Jump to new item'); ?></a></p></div>" ) : '');
|
||||
if ( tempObj.addComplete && typeof tempObj.addComplete == 'function' )
|
||||
tempObj.addComplete( what, where, update, transport );
|
||||
tempObj.recolorList();
|
||||
ajaxAdd.restoreInputs = null;
|
||||
});
|
||||
ajaxAdd.addOnWPError( function(transport) { tempObj.restoreForm(ajaxAdd.restoreInputs); });
|
||||
ajaxAdd.request(ajaxAdd.url);
|
||||
this.clear();
|
||||
return false;
|
||||
},
|
||||
|
||||
// sends update-what and fields contained in where
|
||||
// recieves html with top element having an id like what-#
|
||||
ajaxUpdater: function( what, where ) { return this.ajaxAdder( what, where, true ); },
|
||||
|
||||
// sends delete-what and id#
|
||||
ajaxDelete: function( what, id ) {
|
||||
var ajaxDel = new WPAjax( this.ajaxHandler, this.ajaxRespEl );
|
||||
if( ajaxDel.notInitialized() )
|
||||
return true;
|
||||
var tempObj = this;
|
||||
var action = 'delete-' + what + '&id=' + id;
|
||||
var idName = what.replace('-as-spam','') + '-' + id;
|
||||
ajaxDel.addOnComplete( function(transport) {
|
||||
ajaxDel.myResponseElement.update('');
|
||||
tempObj.destore(action);
|
||||
if( tempObj.delComplete && typeof tempObj.delComplete == 'function' )
|
||||
tempObj.delComplete( what, id, transport );
|
||||
});
|
||||
ajaxDel.addOnWPError( function(transport) { tempObj.restore(action, true); });
|
||||
ajaxDel.options.parameters += '&action=' + action + this.inputData;
|
||||
ajaxDel.request(ajaxDel.url);
|
||||
this.store(action, idName);
|
||||
tempObj.removeListItem( idName );
|
||||
return false;
|
||||
},
|
||||
|
||||
// Toggles class nomes
|
||||
// sends dim-what and id#
|
||||
ajaxDimmer: function( what, id, dimClass ) {
|
||||
ajaxDim = new WPAjax( this.ajaxHandler, this.ajaxRespEl );
|
||||
if ( ajaxDim.notInitialized() )
|
||||
return true;
|
||||
var tempObj = this;
|
||||
var action = 'dim-' + what + '&id=' + id;
|
||||
var idName = what + '-' + id;
|
||||
ajaxDim.addOnComplete( function(transport) {
|
||||
ajaxDim.myResponseElement.update('');
|
||||
tempObj.destore(action);
|
||||
if ( tempObj.dimComplete && typeof tempObj.dimComplete == 'function' )
|
||||
tempObj.dimComplete( what, id, dimClass, transport );
|
||||
});
|
||||
ajaxDim.addOnWPError( function(transport) { tempObj.restore(action, true); });
|
||||
ajaxDim.options.parameters += '&action=' + action + this.inputData;
|
||||
ajaxDim.request(ajaxDim.url);
|
||||
this.store(action, idName);
|
||||
this.dimItem( idName, dimClass );
|
||||
return false;
|
||||
},
|
||||
|
||||
addListItem: function( h ) {
|
||||
new Insertion[this.topAdder ? 'Top' : 'Bottom'](this.theList,h);
|
||||
this.theList.cleanWhitespace();
|
||||
var id = this.topAdder ? this.theList.firstChild.id : this.theList.lastChild.id;
|
||||
if ( this.alt )
|
||||
if ( this.theList.childNodes.length % 2 )
|
||||
$(id).addClassName(this.alt);
|
||||
Fat.fade_element(id);
|
||||
},
|
||||
|
||||
// only hides the element sa it can be put back again if necessary
|
||||
removeListItem: function( id, noFade ) {
|
||||
id = $(id);
|
||||
if ( !noFade ) {
|
||||
Fat.fade_element(id.id,null,700,'#FF3333');
|
||||
var tempObj = this;
|
||||
var func = function() { id.hide(); tempObj.recolorList(); }
|
||||
setTimeout(func, 705);
|
||||
} else {
|
||||
id.hide();
|
||||
this.recolorList();
|
||||
}
|
||||
},
|
||||
|
||||
replaceListItem: function( id, h, update ) {
|
||||
id = $(id);
|
||||
if ( !update ) {
|
||||
id.remove();
|
||||
this.addListItem( h );
|
||||
return;
|
||||
}
|
||||
id.replace(h);
|
||||
Fat.fade_element(id.id);
|
||||
},
|
||||
|
||||
// toggles class
|
||||
dimItem: function( id, dimClass, noFade ) {
|
||||
id = $(id);
|
||||
if ( id.hasClassName(dimClass) ) {
|
||||
if ( !noFade )
|
||||
Fat.fade_element(id.id,null,700,null);
|
||||
id.removeClassName(dimClass);
|
||||
} else {
|
||||
if ( !noFade )
|
||||
Fat.fade_element(id.id,null,700,'#FF3333');
|
||||
id.addClassName(dimClass);
|
||||
}
|
||||
},
|
||||
|
||||
// store an element in case we need it later
|
||||
store: function(action, id) {
|
||||
if ( !this.dataStore )
|
||||
this.dataStore = $H();
|
||||
this.dataStore[action] = $(id).cloneNode(true);
|
||||
},
|
||||
|
||||
// delete from store
|
||||
destore: function(action) { delete(this.dataStore[action]); },
|
||||
|
||||
// restore element from store into existing (possibly hidden) element of same id
|
||||
restore: function(action, error) {
|
||||
var id = this.dataStore[action].id;
|
||||
this.theList.replaceChild(this.dataStore[action], $(id));
|
||||
delete(this.dataStore[action]);
|
||||
if ( error ) {
|
||||
func = function() { $(id).setStyle( { 'background-color': '#FF3333' } ); }
|
||||
func(); setTimeout(func, 705); // Hit it twice in case it's still fading.
|
||||
}
|
||||
},
|
||||
|
||||
// Like Form.serialize, but excludes action and sets up clearInputs
|
||||
grabInputs: function( where, ajaxObj ) {
|
||||
if ( ajaxObj )
|
||||
ajaxObj.restoreInputs = [];
|
||||
var elements = Form.getElements($(where));
|
||||
var queryComponents = new Array();
|
||||
for (var i = 0; i < elements.length; i++) {
|
||||
if ( 'action' == elements[i].name )
|
||||
continue;
|
||||
if ( 'hidden' != elements[i].type && 'submit' != elements[i].type && 'button' != elements[i].type ) {
|
||||
this.clearInputs.push(elements[i]);
|
||||
if ( ajaxObj )
|
||||
ajaxObj.restoreInputs.push([elements[i], elements[i].value]);
|
||||
}
|
||||
var queryComponent = Form.Element.serialize(elements[i]);
|
||||
if (queryComponent) {
|
||||
queryComponents.push(queryComponent);
|
||||
}
|
||||
}
|
||||
return queryComponents.join('&');
|
||||
},
|
||||
|
||||
// form.reset() can only do whole forms. This can do subsections.
|
||||
clear: function() {
|
||||
this.clearInputs.each( function(i) {
|
||||
i = $(i);
|
||||
if ( 'textarea' == i.tagName.toLowerCase() )
|
||||
i.value = '';
|
||||
else
|
||||
switch ( i.type.toLowerCase() ) {
|
||||
case 'password': case 'text':
|
||||
i.value = '';
|
||||
break;
|
||||
case 'checkbox': case 'radio':
|
||||
i.checked = false;
|
||||
break;
|
||||
case 'select': case 'select-one':
|
||||
i.selectedIndex = null;
|
||||
break;
|
||||
case 'select-multiple':
|
||||
for (var o = 0; o < i.length; o++) i.options[o].selected = false;
|
||||
break;
|
||||
}
|
||||
});
|
||||
this.clearInputs = [];
|
||||
},
|
||||
|
||||
restoreForm: function(elements) {
|
||||
elements.each( function(i) {
|
||||
i[0].value = i[1];
|
||||
});
|
||||
},
|
||||
|
||||
recolorList: function() {
|
||||
if ( !this.alt )
|
||||
return;
|
||||
var alt = this.alt;
|
||||
var offset = this.altOffset;
|
||||
var listItems = $A(this.theList.childNodes).findAll( function(i) { return i.visible() } );
|
||||
listItems.each( function(i,n) {
|
||||
if ( ( n + offset ) % 2 )
|
||||
i.removeClassName(alt);
|
||||
else
|
||||
i.addClassName(alt);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
//No submit unless code returns true.
|
||||
function killSubmit ( code, e ) {
|
||||
e = e ? e : window.event;
|
||||
if ( !e ) return;
|
||||
var t = e.target ? e.target : e.srcElement;
|
||||
if ( ( 'text' == t.type && e.keyCode == 13 ) || ( 'submit' == t.type && 'click' == e.type ) ) {
|
||||
if ( ( 'string' == typeof code && !eval(code) ) || ( 'function' == typeof code && !code() ) ) {
|
||||
e.returnValue = false; e.cancelBubble = true; return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
//Pretty func adapted from ALA http://www.alistapart.com/articles/gettingstartedwithajax
|
||||
function getNodeValue(tree,el){try { var r = tree.getElementsByTagName(el)[0].firstChild.nodeValue; } catch(err) { var r = null; } return r; }
|
||||
//Generic but lame JS closure
|
||||
function encloseFunc(f){var a=arguments[1];return function(){return f(a);}}
|
|
@ -0,0 +1,93 @@
|
|||
<?php @require_once('../../wp-config.php'); cache_javascript_headers(); ?>
|
||||
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;
|
||||
this.getResponseElement(responseEl);
|
||||
},
|
||||
addArg: function(key, value) {
|
||||
var a = $H(this.options.parameters.parseQuery());
|
||||
a[encodeURIComponent(key)] = encodeURIComponent(value);
|
||||
this.options.parameters = a.map(function(pair) {
|
||||
return pair.join('=');
|
||||
}).join('&');
|
||||
},
|
||||
getResponseElement: function(r) {
|
||||
var p = $(r + '-p');
|
||||
if ( !p ) {
|
||||
new Insertion.Bottom(r, "<span id='" + r + "-p'></span>");
|
||||
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' ) {
|
||||
var err = this.transport.responseXML.getElementsByTagName('wp_error');
|
||||
if ( err[0] ) {
|
||||
var msg = $A(err).inject( '', function(a, b) { return a + '<p>' + b.firstChild.nodeValue + '</p>'; } );
|
||||
this.myResponseElement.update('<div class="error">' + msg + '</div>');
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
var r = this.transport.responseText;
|
||||
if ( isNaN(r) ) {
|
||||
this.myResponseElement.update('<div class="error"><p>' + r + '</p></div>');
|
||||
return false;
|
||||
}
|
||||
var r = parseInt(r,10);
|
||||
if ( -1 == r ) {
|
||||
this.myResponseElement.update("<div class='error'><p><?php _e("You don't have permission to do that."); ?></p></div>");
|
||||
return false;
|
||||
} else if ( 0 == r ) {
|
||||
this.myResponseElement.update("<div class='error'><p><?php _e("Something strange happened. Try refreshing the page."); ?></p></div>");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
},
|
||||
addOnComplete: function(f) {
|
||||
if ( 'function' == typeof f ) { var of = this.WPComplete; this.WPComplete = function(t) { if ( of ) of(t); f(t); } }
|
||||
},
|
||||
addOnWPError: function(f) {
|
||||
if ( 'function' == typeof f ) { var of = this.WPError; this.WPError = function(t) { if ( of ) of(t); f(t); } }
|
||||
},
|
||||
notInitialized: function() {
|
||||
return this.transport ? false : true;
|
||||
}
|
||||
});
|
||||
|
||||
Ajax.activeSendCount = 0;
|
||||
Ajax.Responders.register( {
|
||||
onCreate: function() {
|
||||
Ajax.activeSendCount++;
|
||||
if ( 1 != Ajax.activeSendCount )
|
||||
return;
|
||||
wpBeforeUnload = window.onbeforeunload;
|
||||
window.onbeforeunload = function() {
|
||||
return "<?php _e("Slow down, I'm still sending your data!"); ?>";
|
||||
}
|
||||
},
|
||||
onLoading: function() { // Can switch to onLoaded if we lose data
|
||||
Ajax.activeSendCount--;
|
||||
if ( 0 != Ajax.activeSendCount )
|
||||
return;
|
||||
window.onbeforeunload = wpBeforeUnload;
|
||||
}
|
||||
});
|
|
@ -19,9 +19,11 @@ class WP_Scripts {
|
|||
$this->add( 'wp_tiny_mce', '/wp-includes/js/tinymce/tiny_mce_config.php', array('tiny_mce'), '04162006' );
|
||||
$this->add( 'prototype', '/wp-includes/js/prototype.js', false, '1.5.0');
|
||||
$this->add( 'autosave', '/wp-includes/js/autosave.js.php', array('prototype', 'sack'), '4183');
|
||||
$this->add( 'wp-ajax', '/wp-includes/js/wp-ajax-js.php', array('prototype'), '4187');
|
||||
$this->add( 'listman', '/wp-includes/js/list-manipulation-js.php', array('wp-ajax', 'fat'), rand());
|
||||
if ( is_admin() ) {
|
||||
$this->add( 'dbx-admin-key', '/wp-admin/dbx-admin-key-js.php', array('dbx'), '3651' );
|
||||
$this->add( 'listman', '/wp-admin/list-manipulation-js.php', array('sack', 'fat'), '4042' ); // Make changeset # the correct one
|
||||
$this->add( 'listman-old', '/wp-admin/list-manipulation-js.php', array('sack', 'fat'), '4042' ); // Make changeset # the correct one
|
||||
$this->add( 'ajaxcat', '/wp-admin/cat-js.php', array('listman'), '3684' );
|
||||
$this->add( 'admin-categories', '/wp-admin/categories.js', array('listman'), '3684' );
|
||||
$this->add( 'admin-custom-fields', '/wp-admin/custom-fields.js', array('listman'), '3733' );
|
||||
|
|
Loading…
Reference in New Issue