AJAXify user addition. Props mdawaffe. fixes #2624

git-svn-id: http://svn.automattic.com/wordpress/trunk@3677 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2006-04-02 00:31:26 +00:00
parent 3d2347a655
commit cebb0a3a15
9 changed files with 142 additions and 91 deletions

View File

@ -209,6 +209,25 @@ case 'update-meta' :
header('Content-type: text/xml'); header('Content-type: text/xml');
die($r); die($r);
break; break;
case 'add-user' :
if ( !current_user_can('edit_users') )
die('-1');
require_once( ABSPATH . WPINC . '/registration-functions.php');
$user_id = add_user();
if ( is_wp_error( $user_id ) ) {
foreach( $user_id->get_error_codes() as $code)
foreach( $user_id->get_error_messages($code) as $message )
echo "$message<br />";
exit;
} elseif ( !$user_id ) {
die('0');
}
$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);
break;
default : default :
die('0'); die('0');
break; break;

View File

@ -361,15 +361,38 @@ function get_category_to_edit($id) {
return $category; return $category;
} }
function wp_dropdown_roles( $default = false ) {
global $wp_roles;
$r = '';
foreach($wp_roles->role_names as $role => $name)
if ( $default == $role ) // Make default first in list
$p = "\n\t<option selected='selected' value='$role'>$name</option>";
else
$r .= "\n\t<option value='$role'>$name</option>";
echo $p . $r;
}
// Creates a new user from the "Users" form using $_POST information. // Creates a new user from the "Users" form using $_POST information.
function add_user() { function add_user() {
return edit_user(); if ( func_num_args() ) { // The hackiest hack that ever did hack
global $current_user, $wp_roles;
$user_id = func_get_arg(0);
if (isset ($_POST['role'])) {
if($user_id != $current_user->id || $wp_roles->role_objects[$_POST['role']]->has_cap('edit_users')) {
$user = new WP_User($user_id);
$user->set_role($_POST['role']);
}
}
} else {
add_action('user_register', 'add_user'); // See above
return edit_user();
}
} }
function edit_user($user_id = 0) { function edit_user($user_id = 0) {
global $current_user, $wp_roles, $wpdb; global $current_user, $wp_roles, $wpdb;
if ($user_id != 0) { if ($user_id != 0) {
$update = true; $update = true;
$user->ID = $user_id; $user->ID = $user_id;
@ -417,49 +440,49 @@ function edit_user($user_id = 0) {
if (isset ($_POST['yim'])) if (isset ($_POST['yim']))
$user->yim = wp_specialchars(trim($_POST['yim'])); $user->yim = wp_specialchars(trim($_POST['yim']));
$errors = array (); $errors = new WP_Error();
/* checking that username has been typed */ /* checking that username has been typed */
if ($user->user_login == '') if ($user->user_login == '')
$errors['user_login'] = __('<strong>ERROR</strong>: Please enter a username.'); $errors->add('user_login', __('<strong>ERROR</strong>: Please enter a username.'));
/* checking the password has been typed twice */ /* checking the password has been typed twice */
do_action('check_passwords', array ($user->user_login, & $pass1, & $pass2)); do_action('check_passwords', array ($user->user_login, & $pass1, & $pass2));
if (!$update) { if (!$update) {
if ($pass1 == '' || $pass2 == '') if ($pass1 == '' || $pass2 == '')
$errors['pass'] = __('<strong>ERROR</strong>: Please enter your password twice.'); $errors->add('pass', __('<strong>ERROR</strong>: Please enter your password twice.'));
} else { } else {
if ((empty ($pass1) && !empty ($pass2)) || (empty ($pass2) && !empty ($pass1))) if ((empty ($pass1) && !empty ($pass2)) || (empty ($pass2) && !empty ($pass1)))
$errors['pass'] = __("<strong>ERROR</strong>: you typed your new password only once."); $errors->add('pass', __("<strong>ERROR</strong>: you typed your new password only once."));
} }
/* Check for "\" in password */ /* Check for "\" in password */
if( strpos( " ".$pass1, "\\" ) ) if( strpos( " ".$pass1, "\\" ) )
$errors['pass'] = __('<strong>ERROR</strong>: Passwords may not contain the character "\\".'); $errors->add('pass', __('<strong>ERROR</strong>: Passwords may not contain the character "\\".'));
/* checking the password has been typed twice the same */ /* checking the password has been typed twice the same */
if ($pass1 != $pass2) if ($pass1 != $pass2)
$errors['pass'] = __('<strong>ERROR</strong>: Please type the same password in the two password fields.'); $errors->add('pass', __('<strong>ERROR</strong>: Please type the same password in the two password fields.'));
if (!empty ($pass1)) if (!empty ($pass1))
$user->user_pass = $pass1; $user->user_pass = $pass1;
if ( !validate_username($user->user_login) ) if ( !validate_username($user->user_login) )
$errors['user_login'] = __('<strong>ERROR</strong>: This username is invalid. Please enter a valid username.'); $errors->add('user_login', __('<strong>ERROR</strong>: This username is invalid. Please enter a valid username.'));
if (!$update && username_exists($user->user_login)) if (!$update && username_exists($user->user_login))
$errors['user_login'] = __('<strong>ERROR</strong>: This username is already registered, please choose another one.'); $errors->add('user_login', __('<strong>ERROR</strong>: This username is already registered, please choose another one.'));
/* checking e-mail address */ /* checking e-mail address */
if (empty ($user->user_email)) { if (empty ($user->user_email)) {
$errors['user_email'] = __("<strong>ERROR</strong>: please type an e-mail address"); $errors->add('user_email', __("<strong>ERROR</strong>: please type an e-mail address"));
} else } else
if (!is_email($user->user_email)) { if (!is_email($user->user_email)) {
$errors['user_email'] = __("<strong>ERROR</strong>: the email address isn't correct"); $errors->add('user_email', __("<strong>ERROR</strong>: the email address isn't correct"));
} }
if (count($errors) != 0) if ( $errors->get_error_codes() )
return $errors; return $errors;
if ($update) { if ($update) {
@ -468,8 +491,7 @@ function edit_user($user_id = 0) {
$user_id = wp_insert_user(get_object_vars($user)); $user_id = wp_insert_user(get_object_vars($user));
wp_new_user_notification($user_id); wp_new_user_notification($user_id);
} }
return $user_id;
return $errors;
} }
@ -692,6 +714,33 @@ function page_rows($parent = 0, $level = 0, $pages = 0, $hierarchy = true) {
} }
} }
function user_row( $user_object, $style = '' ) {
if ( !(is_object($user_object) && is_a($user_object, 'WP_User')) )
$user_object = new WP_User( (int) $user_object );
$email = $user_object->user_email;
$url = $user_object->user_url;
$short_url = str_replace('http://', '', $url);
$short_url = str_replace('www.', '', $short_url);
if ('/' == substr($short_url, -1))
$short_url = substr($short_url, 0, -1);
if (strlen($short_url) > 35)
$short_url = substr($short_url, 0, 32).'...';
$numposts = get_usernumposts($user_object->ID);
if (0 < $numposts) $numposts = "<a href='edit.php?author=$user_object->ID' title='" . __('View posts') . "'>$numposts</a>";
$r = "<tr id='user-$user_object->ID'$style>
<td><input type='checkbox' name='users[]' id='user_{$user_object->ID}' value='{$user_object->ID}' /> <label for='user_{$user_object->ID}'>{$user_object->ID}</label></td>
<td><label for='user_{$user_object->ID}'><strong>$user_object->user_login</strong></label></td>
<td><label for='user_{$user_object->ID}'>$user_object->first_name $user_object->last_name</label></td>
<td><a href='mailto:$email' title='" . sprintf(__('e-mail: %s'), $email) . "'>$email</a></td>
<td><a href='$url' title='website: $url'>$short_url</a></td>";
$r .= "\n\t\t<td align='right'>$numposts</td>";
$r .= "\n\t\t<td>";
if (current_user_can('edit_users'))
$r .= "<a href='user-edit.php?user_id=$user_object->ID' class='edit'>".__('Edit')."</a>";
$r .= "</td>\n\t</tr>";
return $r;
}
function wp_dropdown_cats($currentcat = 0, $currentparent = 0, $parent = 0, $level = 0, $categories = 0) { function wp_dropdown_cats($currentcat = 0, $currentparent = 0, $parent = 0, $level = 0, $categories = 0) {
global $wpdb, $bgcolor; global $wpdb, $bgcolor;
if (!$categories) { if (!$categories) {

View File

@ -40,6 +40,9 @@ function addLoadEvent(func) {if ( typeof wpOnload!='function'){wpOnload=func;}el
<?php if ( 'categories.php' == $pagenow && 'edit' != $action ) { ?> <?php if ( 'categories.php' == $pagenow && 'edit' != $action ) { ?>
<script type="text/javascript" src="categories.js"></script> <script type="text/javascript" src="categories.js"></script>
<?php } ?> <?php } ?>
<?php if ( $users_js ) { ?>
<script type="text/javascript" src="users.js"></script>
<?php } ?>
<?php if ( $dbx_js ) { ?> <?php if ( $dbx_js ) { ?>
<script type="text/javascript" src="../wp-includes/js/dbx.js"></script> <script type="text/javascript" src="../wp-includes/js/dbx.js"></script>
<script type="text/javascript"> <script type="text/javascript">

View File

@ -40,7 +40,7 @@ for ($i=0; $i<count($wpvarstoreset); $i += 1) {
} }
} }
$xfn_js = $sack_js = $list_js = $cat_js = $dbx_js = $pmeta_js = $editing = false; $xfn_js = $sack_js = $list_js = $cat_js = $users_js = $dbx_js = $pmeta_js = $editing = false;
require(ABSPATH . '/wp-admin/menu.php'); require(ABSPATH . '/wp-admin/menu.php');

View File

@ -9,7 +9,7 @@ function dimSomething(what,id,dimClass){return theList.ajaxDimmer(what,id,dimCla
function WPAjax(file, responseEl){//class WPAjax extends sack 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+'ajax-response-p';document.getElementById(r).appendChild(p);}this.myResponseElement=p; } this.getResponseElement=function(r){var p=document.getElementById(r+'-p');if(!p){p=document.createElement('span');p.id=r+'ajax-response-p';document.getElementById(r).appendChild(p);}this.myResponseElement=p; }
this.parseAjaxResponse=function(){ this.parseAjaxResponse=function(){
if(isNaN(this.response)){this.myResponseElement.innerHTML="<?php _e('Error: '); ?>"+this.response;return false;} if(isNaN(this.response)){this.myResponseElement.innerHTML='<div class="error">'+this.response+'</div>';return false;}
this.response=parseInt(this.response,10); this.response=parseInt(this.response,10);
if(-1==this.response){this.myResponseElement.innerHTML="<?php _e("You don't have permission to do that."); ?>";return false;} if(-1==this.response){this.myResponseElement.innerHTML="<?php _e("You don't have permission to do that."); ?>";return false;}
else if(0==this.response){this.myResponseElement.innerHTML="<?php _e("Something odd happened. Try refreshing the page? Either that or what you tried to change never existed in the first place."); ?>";return false;} else if(0==this.response){this.myResponseElement.innerHTML="<?php _e("Something odd happened. Try refreshing the page? Either that or what you tried to change never existed in the first place."); ?>";return false;}
@ -17,7 +17,7 @@ function WPAjax(file, responseEl){//class WPAjax extends sack
} }
this.parseAjaxResponseXML=function(){ this.parseAjaxResponseXML=function(){
if(this.responseXML&&typeof this.responseXML=='object')return true; if(this.responseXML&&typeof this.responseXML=='object')return true;
if(isNaN(this.response)){this.myResponseElement.innerHTML="<?php _e('Error: '); ?>"+this.response;return false;} if(isNaN(this.response)){this.myResponseElement.innerHTML='<div class="error">'+this.response+'</div>';return false;}
var r=parseInt(this.response,10); var r=parseInt(this.response,10);
if(-1==r){this.myResponseElement.innerHTML="<?php _e("You don't have permission to do that."); ?>";} if(-1==r){this.myResponseElement.innerHTML="<?php _e("You don't have permission to do that."); ?>";}
else if(0==r){this.myResponseElement.innerHTML="<?php _e("Invalid Entry."); ?>";} else if(0==r){this.myResponseElement.innerHTML="<?php _e("Invalid Entry."); ?>";}
@ -153,7 +153,7 @@ function listMan(theListId){
this.getListItems(); this.getListItems();
} }
//No submit unless eval(code) returns true. //No submit unless eval(code) returns true.
function killSubmit(code,e){if(!e){if(window.event)e=window.event;else return;}var t=e.target?e.target:e.srcElement;if(('text'==t.type&&e.keyCode==13)||('submit'==t.type&&'click'==e.type)){if(!eval(code));e.returnValue=false;e.cancelBubble=true;return false;}} function killSubmit(code,e){if(!e){if(window.event)e=window.event;else return;}var t=e.target?e.target:e.srcElement;if(('text'==t.type&&e.keyCode==13)||('submit'==t.type&&'click'==e.type)){if(!eval(code)){e.returnValue=false;e.cancelBubble=true;return false;}}}
//Pretty func from ALA http://www.alistapart.com/articles/gettingstartedwithajax //Pretty func from ALA http://www.alistapart.com/articles/gettingstartedwithajax
function getNodeValue(tree,el){return tree.getElementsByTagName(el)[0].firstChild.nodeValue;} function getNodeValue(tree,el){return tree.getElementsByTagName(el)[0].firstChild.nodeValue;}
//Generic but lame JS closure //Generic but lame JS closure

View File

@ -54,12 +54,7 @@ include('./admin-header.php');
<tr valign="top"> <tr valign="top">
<th scope="row"><?php _e('New User Default Role:') ?></th> <th scope="row"><?php _e('New User Default Role:') ?></th>
<td><label for="default_role"> <td><label for="default_role">
<select name="default_role" id="default_role"><?php <select name="default_role" id="default_role"><?php wp_dropdown_roles( get_settings('default_role') ); ?></select></label>
foreach($wp_roles->role_names as $role => $name) {
$selected = (get_settings('default_role') == $role) ? 'selected="selected"' : '';
echo "<option {$selected} value=\"{$role}\">{$name}</option>";
}
?></select></label>
</td> </td>
</tr> </tr>
</table> </table>
@ -111,4 +106,4 @@ endfor;
</div> </div>
<?php include('./admin-footer.php') ?> <?php include('./admin-footer.php') ?>

View File

@ -9,10 +9,10 @@ if ( !$_POST )
$errors = edit_user($user_ID); $errors = edit_user($user_ID);
if (count($errors) != 0) { if ( is_wp_error( $errors ) ) {
foreach ($errors as $id => $error) { foreach( $errors->get_error_codes() as $code)
echo $error . '<br/>'; foreach( $errors->get_error_messages($code) as $message )
} echo "$message<br />";
exit; exit;
} }
@ -30,4 +30,4 @@ else
wp_redirect( $to ); wp_redirect( $to );
exit; exit;
?> ?>

View File

@ -34,14 +34,12 @@ case 'update':
check_admin_referer(); check_admin_referer();
$errors = array();
if (!current_user_can('edit_users')) if (!current_user_can('edit_users'))
$errors['head'] = __('You do not have permission to edit this user.'); $errors = new WP_Error('head', __('You do not have permission to edit this user.'));
else else
$errors = edit_user($user_id); $errors = edit_user($user_id);
if(count($errors) == 0) { if( !is_wp_error( $errors ) ) {
header("Location: user-edit.php?user_id=$user_id&updated=true"); header("Location: user-edit.php?user_id=$user_id&updated=true");
exit; exit;
} }
@ -51,7 +49,9 @@ include ('admin-header.php');
$profileuser = new WP_User($user_id); $profileuser = new WP_User($user_id);
if (!current_user_can('edit_users')) $errors['head'] = __('You do not have permission to edit this user.'); if (!current_user_can('edit_users'))
if ( !is_wp_error( $errors ) )
$errors = new WP_Error('head', __('You do not have permission to edit this user.'));
?> ?>
<?php if ( isset($_GET['updated']) ) : ?> <?php if ( isset($_GET['updated']) ) : ?>
@ -59,11 +59,13 @@ if (!current_user_can('edit_users')) $errors['head'] = __('You do not have permi
<p><strong><?php _e('User updated.') ?></strong></p> <p><strong><?php _e('User updated.') ?></strong></p>
</div> </div>
<?php endif; ?> <?php endif; ?>
<?php if ( count($errors) != 0 ) : ?> <?php if ( is_wp_error( $errors ) ) : ?>
<div class="error"> <div class="error">
<ul> <ul>
<?php <?php
foreach($errors as $error) echo "<li>$error</li>"; foreach( $errors->get_error_codes() as $code)
foreach( $errors->get_error_messages($code) as $message )
echo "<li>$message</li>";
?> ?>
</ul> </ul>
</div> </div>

View File

@ -79,7 +79,7 @@ case 'delete':
} }
if ( !current_user_can('edit_users') ) if ( !current_user_can('edit_users') )
$error['edit_users'] = __('You can&#8217;t delete users.'); $error = new WP_Error('edit_users', __('You can&#8217;t delete users.'));
$userids = $_POST['users']; $userids = $_POST['users'];
@ -133,15 +133,19 @@ break;
case 'adduser': case 'adduser':
check_admin_referer(); check_admin_referer();
$errors = add_user(); $user_id = add_user();
if ( is_wp_error( $user_id ) )
if(count($errors) == 0) { $errors = $user_id;
else {
header('Location: users.php?update=add'); header('Location: users.php?update=add');
die(); die();
} }
default: default:
$list_js = true;
$users_js = true;
include ('admin-header.php'); include ('admin-header.php');
$userids = $wpdb->get_col("SELECT ID FROM $wpdb->users;"); $userids = $wpdb->get_col("SELECT ID FROM $wpdb->users;");
@ -187,11 +191,13 @@ default:
break; break;
} }
endif; endif;
if ( isset($errors) ) : ?> if ( is_wp_error( $errors ) ) : ?>
<div class="error"> <div class="error">
<ul> <ul>
<?php <?php
foreach($errors as $error) echo "<li>$error</li>"; foreach( $errors->get_error_codes() as $code)
foreach( $errors->get_error_messages($code) as $message )
echo "<li>$message</li>";
?> ?>
</ul> </ul>
</div> </div>
@ -209,51 +215,27 @@ default:
?> ?>
<tr> <tr>
<th colspan="8" align="left"> <th colspan="8" align="left"><h3><?php echo $wp_roles->role_names[$role]; ?></h3></th>
<h3><?php echo $wp_roles->role_names[$role]; ?></h3>
</th></tr>
<tr>
<th><?php _e('ID') ?></th>
<th><?php _e('Username') ?></th>
<th><?php _e('Name') ?></th>
<th><?php _e('E-mail') ?></th>
<th><?php _e('Website') ?></th>
<th><?php _e('Posts') ?></th>
<th>&nbsp;</th>
</tr> </tr>
<?php <tr>
<th><?php _e('ID') ?></th>
<th><?php _e('Username') ?></th>
<th><?php _e('Name') ?></th>
<th><?php _e('E-mail') ?></th>
<th><?php _e('Website') ?></th>
<th><?php _e('Posts') ?></th>
<th>&nbsp;</th>
</tr>
<tbody id="role-<?php echo $role; ?>"><?php
$style = ''; $style = '';
foreach ($roleclass as $user_object) { foreach ($roleclass as $user_object) {
$email = $user_object->user_email; $style = (' class="alternate"' == $style) ? '' : ' class="alternate"';
$url = $user_object->user_url; echo "\n\t" . user_row( $user_object, $style );
$short_url = str_replace('http://', '', $url);
$short_url = str_replace('www.', '', $short_url);
if ('/' == substr($short_url, -1))
$short_url = substr($short_url, 0, -1);
if (strlen($short_url) > 35)
$short_url = substr($short_url, 0, 32).'...';
$style = ('class="alternate"' == $style) ? '' : 'class="alternate"';
$numposts = get_usernumposts($user_object->ID);
if (0 < $numposts) $numposts = "<a href='edit.php?author=$user_object->ID' title='" . __('View posts') . "'>$numposts</a>";
echo "
<tr $style>
<td><input type='checkbox' name='users[]' id='user_{$user_object->ID}' value='{$user_object->ID}' /> <label for='user_{$user_object->ID}'>{$user_object->ID}</label></td>
<td><label for='user_{$user_object->ID}'><strong>$user_object->user_login</strong></label></td>
<td><label for='user_{$user_object->ID}'>$user_object->first_name $user_object->last_name</label></td>
<td><a href='mailto:$email' title='" . sprintf(__('e-mail: %s'), $email) . "'>$email</a></td>
<td><a href='$url' title='website: $url'>$short_url</a></td>";
echo "<td align='right'>$numposts</td>";
echo '<td>';
if (current_user_can('edit_users'))
echo "<a href='user-edit.php?user_id=$user_object->ID' class='edit'>".__('Edit')."</a>";
echo '</td>';
echo '</tr>';
} }
?> ?>
</tbody>
<?php <?php
} }
?> ?>
@ -261,16 +243,12 @@ default:
<h2><?php _e('Update Users'); ?></h2> <h2><?php _e('Update Users'); ?></h2>
<?php
$role_select = '<select name="new_role">';
foreach($wp_roles->role_names as $role => $name) {
$role_select .= "<option value=\"{$role}\">{$name}</option>";
}
$role_select .= '</select>';
?>
<ul style="list-style:none;"> <ul style="list-style:none;">
<li><input type="radio" name="action" id="action0" value="delete" /> <label for="action0"><?php _e('Delete checked users.'); ?></label></li> <li><input type="radio" name="action" id="action0" value="delete" /> <label for="action0"><?php _e('Delete checked users.'); ?></label></li>
<li><input type="radio" name="action" id="action1" value="promote" /> <?php echo '<label for="action1">'.__('Set the Role of checked users to:')."</label> $role_select"; ?></li> <li>
<input type="radio" name="action" id="action1" value="promote" /> <label for="action1"><?php _e('Set the Role of checked users to:'); ?></label>
<select name="new_role"><?php wp_dropdown_roles(); ?></select>
</li>
</ul> </ul>
<p class="submit"><input type="submit" value="<?php _e('Update &raquo;'); ?>" /></p> <p class="submit"><input type="submit" value="<?php _e('Update &raquo;'); ?>" /></p>
</div> </div>
@ -313,11 +291,16 @@ if ( $show_password_fields ) :
<input name="pass2" type="password" id="pass2" /></td> <input name="pass2" type="password" id="pass2" /></td>
</tr> </tr>
<?php endif; ?> <?php endif; ?>
<tr>
<th scope="row"><?php _e('Role'); ?></th>
<td><select name="role" id="role"><?php wp_dropdown_roles( get_settings('default_role') ); ?></select></td>
</tr>
</table> </table>
<p class="submit"> <p class="submit">
<input name="adduser" type="submit" id="adduser" value="<?php _e('Add User &raquo;') ?>" /> <input name="adduser" type="submit" id="addusersub" value="<?php _e('Add User &raquo;') ?>" />
</p> </p>
</form> </form>
<div id="ajax-response"></div>
</div> </div>
<?php <?php