Hi it’s a bit complicated due to the fact I allow people to add new accounts without submitting the page (so I just copy some HTML, and append the nodes to the HTML).
The cross in the corner of the account to delete one account just calls some JS that updates a counter of how many accounts we are supposed to have in a hidden field (account_delete_counter) so if the page is then submitted I know we should only have X accounts and I just loop up to that number.
I do supply a unique nonce and a flag the JS code is
deleteaccount : function(c,n){
ShowDebug("IN delete account c = " + c + " n = " + n);
// if account was added on the fly then it hasnt been saved so we can just remove it again
if(confirm("' . __('Are your sure you want to remove this Twitter account?','strictlytweetbot') . '")){
ShowDebug("remove div = hx_"+c);
if(document.getElementById("hx_"+c)){
var el = document.getElementById("AccountWrapper"+c);
ShowDebug("el = " + el.id + " typeof = " + typeof(el));
el.parentNode.removeChild(el);
document.getElementById("account_delete_counter").value = document.getElementById("account_delete_counter").value+c+",";
ShowDebug("gone");
}else{
ShowDebug("location.href=" + location.href);
document.forms[0].action=TwitterAccount.CleanURL(location.href) + "&_ajax_nonce=' . $nonce . '&del=1&key="+encodeURIComponent(n);
document.forms[0].submit();
}
}
},
CleanURL : function(url){
if(url!=""){
return url.replace(/&_ajax_nonce=\S+?&del=1&key=[^& ]+$/,"");
}
}
}
function ShowDebug(m){
if(typeof(window.console)!="undefined"){
console.log(m);
}
}
If the form is submitted and there is a del=1 flag in the querystring in my submit action I run this code.
if ( !$_POST['cmdSubmit'] && $_GET['del'] == "1"){
$key = $_GET['key'] ;
// check nonce - not AJAX but so what
check_ajax_referer('strictly-tweetbot-nonce');
$this->DeleteAccount($key);
}
And the DelteAccount function passes in the key to the function which removes all details of the account from the global array of all twitter accounts I have.
/**
* deletes an account
*
* @param string $account
* @return bool
*/
protected function DeleteAccount($key){
if(!empty($key)){
// get the account from the key - don't really need this now I have put code in to prevent duplicate account names from occurring
// added however there may be future reasons so I'll continue to use a surrogate key
$account = $this->GetAccountFromKey($key);
// update each array then re-save
// create array to store results
unset($this->accounts[$account]);
unset($this->account_names[$account]);
unset($this->access_token_secrets[$account]);
unset($this->access_tokens[$account]);
unset($this->verified[$account]);
unset($this->defaulttags[$account]);
unset($this->formats[$account]);
unset($this->active[$account]);
unset($this->tagtypes[$account]);
unset($this->contentanalysis[$account]);
unset($this->contentanalysistype[$account]);
unset($this->saved_keys[$account]);
unset($this->extra_querystring[$account]);
unset($this->ignoreterms[$account]);
unset($this->textshrink[$account]);
unset($this->tweetshrink[$account]);
$strictlytweet_options = array(
"accounts" => $this->accounts,
"account_names" => $this->account_names,
"access_token_secrets" => $this->access_token_secrets,
"access_tokens" => $this->access_tokens,
"verified" => $this->verified,
"defaulttags" => $this->defaulttags,
"formats" => $this->formats,
"active" => $this->active,
"tagtypes" => $this->tagtypes,
"bitlyAPIkey" => $this->bitlyAPIkey,
"bitlyAPIusername" => $this->bitlyAPIusername,
"bitlyAPI" => $this->bitlyAPI,
"contentanalysis" => $this->contentanalysis,
"contentanalysistype" => $this->contentanalysistype,
"saved_keys" => $this->saved_keys,
"extra_querystring" => $this->extra_querystring,
"ignoreterms" => $this->ignoreterms,
"textshrink" => $this->textshrink,
"tweetshrink" => $this->tweetshrink
);
// save our data to the wordpress database
update_option('strictlytweetbot_options', $strictlytweet_options);
}
}
I basically just have a big array of all the parts I need and the name of the account is the key to the array (hashtags, verification, text etc)
Hope this makes sense!