WordPress.org

Make WordPress Core

Changeset 3677


Ignore:
Timestamp:
04/02/06 00:31:26 (9 years ago)
Author:
ryan
Message:

AJAXify user addition. Props mdawaffe. fixes #2624

Location:
trunk/wp-admin
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/admin-ajax.php

    r3660 r3677  
    210210    die($r); 
    211211    break; 
     212case 'add-user' : 
     213    if ( !current_user_can('edit_users') ) 
     214        die('-1'); 
     215    require_once( ABSPATH . WPINC . '/registration-functions.php'); 
     216    $user_id = add_user(); 
     217    if ( is_wp_error( $user_id ) ) { 
     218        foreach( $user_id->get_error_codes() as $code) 
     219                        foreach( $user_id->get_error_messages($code) as $message ) 
     220                                 echo "$message<br />"; 
     221    exit; 
     222    } elseif ( !$user_id ) { 
     223        die('0'); 
     224    } 
     225    $r  = "<?xml version='1.0' standalone='yes'?><ajaxresponse><user><id>$user_id</id><newitem><![CDATA[<table><tbody>"; 
     226    $r .= user_row( $user_id ); 
     227    $r .= "</tbody></table>]]></newitem></user></ajaxresponse>"; 
     228    header('Content-type: text/xml'); 
     229    die($r); 
     230    break; 
    212231default : 
    213232    die('0'); 
  • trunk/wp-admin/admin-functions.php

    r3676 r3677  
    362362} 
    363363 
     364function wp_dropdown_roles( $default = false ) { 
     365    global $wp_roles; 
     366    $r = ''; 
     367    foreach($wp_roles->role_names as $role => $name) 
     368        if ( $default == $role ) // Make default first in list 
     369            $p = "\n\t<option selected='selected' value='$role'>$name</option>"; 
     370        else 
     371            $r .= "\n\t<option value='$role'>$name</option>"; 
     372    echo $p . $r; 
     373} 
     374 
     375 
    364376// Creates a new user from the "Users" form using $_POST information. 
    365377 
    366378function add_user() { 
    367     return edit_user(); 
     379    if ( func_num_args() ) { // The hackiest hack that ever did hack 
     380        global $current_user, $wp_roles; 
     381        $user_id = func_get_arg(0); 
     382        if (isset ($_POST['role'])) { 
     383            if($user_id != $current_user->id || $wp_roles->role_objects[$_POST['role']]->has_cap('edit_users')) { 
     384                $user = new WP_User($user_id); 
     385                $user->set_role($_POST['role']); 
     386            } 
     387        } 
     388    } else { 
     389        add_action('user_register', 'add_user'); // See above 
     390        return edit_user(); 
     391    } 
    368392} 
    369393 
    370394function edit_user($user_id = 0) { 
    371395    global $current_user, $wp_roles, $wpdb; 
    372  
    373396    if ($user_id != 0) { 
    374397        $update = true; 
     
    418441        $user->yim = wp_specialchars(trim($_POST['yim'])); 
    419442 
    420     $errors = array (); 
     443    $errors = new WP_Error(); 
    421444 
    422445    /* checking that username has been typed */ 
    423446    if ($user->user_login == '') 
    424         $errors['user_login'] = __('<strong>ERROR</strong>: Please enter a username.'); 
     447        $errors->add('user_login', __('<strong>ERROR</strong>: Please enter a username.')); 
    425448 
    426449    /* checking the password has been typed twice */ 
     
    429452    if (!$update) { 
    430453        if ($pass1 == '' || $pass2 == '') 
    431             $errors['pass'] = __('<strong>ERROR</strong>: Please enter your password twice.'); 
     454            $errors->add('pass', __('<strong>ERROR</strong>: Please enter your password twice.')); 
    432455    } else { 
    433456        if ((empty ($pass1) && !empty ($pass2)) || (empty ($pass2) && !empty ($pass1))) 
    434             $errors['pass'] = __("<strong>ERROR</strong>: you typed your new password only once."); 
     457            $errors->add('pass', __("<strong>ERROR</strong>: you typed your new password only once.")); 
    435458    } 
    436459 
    437460    /* Check for "\" in password */ 
    438461    if( strpos( " ".$pass1, "\\" ) ) 
    439         $errors['pass'] = __('<strong>ERROR</strong>: Passwords may not contain the character "\\".'); 
     462        $errors->add('pass', __('<strong>ERROR</strong>: Passwords may not contain the character "\\".')); 
    440463 
    441464    /* checking the password has been typed twice the same */ 
    442465    if ($pass1 != $pass2) 
    443         $errors['pass'] = __('<strong>ERROR</strong>: Please type the same password in the two password fields.'); 
     466        $errors->add('pass', __('<strong>ERROR</strong>: Please type the same password in the two password fields.')); 
    444467 
    445468    if (!empty ($pass1)) 
     
    447470 
    448471    if ( !validate_username($user->user_login) ) 
    449         $errors['user_login'] = __('<strong>ERROR</strong>: This username is invalid.  Please enter a valid username.'); 
     472        $errors->add('user_login', __('<strong>ERROR</strong>: This username is invalid.  Please enter a valid username.')); 
    450473 
    451474    if (!$update && username_exists($user->user_login)) 
    452         $errors['user_login'] = __('<strong>ERROR</strong>: This username is already registered, please choose another one.'); 
     475        $errors->add('user_login', __('<strong>ERROR</strong>: This username is already registered, please choose another one.')); 
    453476 
    454477    /* checking e-mail address */ 
    455478    if (empty ($user->user_email)) { 
    456         $errors['user_email'] = __("<strong>ERROR</strong>: please type an e-mail address"); 
     479        $errors->add('user_email', __("<strong>ERROR</strong>: please type an e-mail address")); 
    457480    } else 
    458481        if (!is_email($user->user_email)) { 
    459             $errors['user_email'] = __("<strong>ERROR</strong>: the email address isn't correct"); 
    460         } 
    461  
    462     if (count($errors) != 0) 
     482            $errors->add('user_email', __("<strong>ERROR</strong>: the email address isn't correct")); 
     483        } 
     484 
     485    if ( $errors->get_error_codes() ) 
    463486        return $errors; 
    464487 
     
    469492        wp_new_user_notification($user_id); 
    470493    } 
    471  
    472     return $errors; 
     494    return $user_id; 
    473495} 
    474496 
     
    691713        if ( $hierarchy) page_rows($id, $level + 1, $pages); 
    692714    } 
     715} 
     716 
     717function user_row( $user_object, $style = '' ) { 
     718    if ( !(is_object($user_object) && is_a($user_object, 'WP_User')) ) 
     719        $user_object = new WP_User( (int) $user_object ); 
     720    $email = $user_object->user_email; 
     721    $url = $user_object->user_url; 
     722    $short_url = str_replace('http://', '', $url); 
     723    $short_url = str_replace('www.', '', $short_url); 
     724    if ('/' == substr($short_url, -1)) 
     725        $short_url = substr($short_url, 0, -1); 
     726    if (strlen($short_url) > 35) 
     727        $short_url =  substr($short_url, 0, 32).'...'; 
     728    $numposts = get_usernumposts($user_object->ID); 
     729    if (0 < $numposts) $numposts = "<a href='edit.php?author=$user_object->ID' title='" . __('View posts') . "'>$numposts</a>"; 
     730    $r = "<tr id='user-$user_object->ID'$style> 
     731        <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> 
     732        <td><label for='user_{$user_object->ID}'><strong>$user_object->user_login</strong></label></td> 
     733        <td><label for='user_{$user_object->ID}'>$user_object->first_name $user_object->last_name</label></td> 
     734        <td><a href='mailto:$email' title='" . sprintf(__('e-mail: %s'), $email) . "'>$email</a></td> 
     735        <td><a href='$url' title='website: $url'>$short_url</a></td>"; 
     736    $r .= "\n\t\t<td align='right'>$numposts</td>"; 
     737    $r .= "\n\t\t<td>"; 
     738    if (current_user_can('edit_users')) 
     739        $r .= "<a href='user-edit.php?user_id=$user_object->ID' class='edit'>".__('Edit')."</a>"; 
     740    $r .= "</td>\n\t</tr>"; 
     741    return $r; 
    693742} 
    694743 
  • trunk/wp-admin/admin-header.php

    r3664 r3677  
    4141<script type="text/javascript" src="categories.js"></script> 
    4242<?php } ?> 
     43<?php if ( $users_js ) { ?> 
     44<script type="text/javascript" src="users.js"></script> 
     45<?php } ?> 
    4346<?php if ( $dbx_js ) { ?> 
    4447<script type="text/javascript" src="../wp-includes/js/dbx.js"></script> 
  • trunk/wp-admin/admin.php

    r3660 r3677  
    4141} 
    4242 
    43 $xfn_js = $sack_js = $list_js = $cat_js = $dbx_js = $pmeta_js = $editing = false; 
     43$xfn_js = $sack_js = $list_js = $cat_js = $users_js = $dbx_js = $pmeta_js = $editing = false; 
    4444 
    4545require(ABSPATH . '/wp-admin/menu.php'); 
  • trunk/wp-admin/list-manipulation-js.php

    r3669 r3677  
    1010    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; } 
    1111    this.parseAjaxResponse=function(){ 
    12         if(isNaN(this.response)){this.myResponseElement.innerHTML="<?php _e('Error: '); ?>"+this.response;return false;} 
     12        if(isNaN(this.response)){this.myResponseElement.innerHTML='<div class="error">'+this.response+'</div>';return false;} 
    1313        this.response=parseInt(this.response,10); 
    1414        if(-1==this.response){this.myResponseElement.innerHTML="<?php _e("You don't have permission to do that."); ?>";return false;} 
     
    1818    this.parseAjaxResponseXML=function(){ 
    1919        if(this.responseXML&&typeof this.responseXML=='object')return true; 
    20         if(isNaN(this.response)){this.myResponseElement.innerHTML="<?php _e('Error: '); ?>"+this.response;return false;} 
     20        if(isNaN(this.response)){this.myResponseElement.innerHTML='<div class="error">'+this.response+'</div>';return false;} 
    2121        var r=parseInt(this.response,10); 
    2222        if(-1==r){this.myResponseElement.innerHTML="<?php _e("You don't have permission to do that."); ?>";} 
     
    154154} 
    155155//No submit unless eval(code) returns true. 
    156 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;}} 
     156function 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;}}} 
    157157//Pretty func from ALA http://www.alistapart.com/articles/gettingstartedwithajax 
    158158function getNodeValue(tree,el){return tree.getElementsByTagName(el)[0].firstChild.nodeValue;} 
  • trunk/wp-admin/options-general.php

    r3676 r3677  
    5555<th scope="row"><?php _e('New User Default Role:') ?></th>  
    5656<td><label for="default_role">  
    57 <select name="default_role" id="default_role"><?php  
    58 foreach($wp_roles->role_names as $role => $name) { 
    59     $selected = (get_settings('default_role') == $role) ? 'selected="selected"' : ''; 
    60     echo "<option {$selected} value=\"{$role}\">{$name}</option>"; 
    61 } 
    62 ?></select></label> 
     57<select name="default_role" id="default_role"><?php wp_dropdown_roles( get_settings('default_role') ); ?></select></label> 
    6358</td>  
    6459</tr>  
  • trunk/wp-admin/profile-update.php

    r3112 r3677  
    1010$errors = edit_user($user_ID); 
    1111 
    12 if (count($errors) != 0) { 
    13     foreach ($errors as $id => $error) { 
    14         echo $error . '<br/>'; 
    15     } 
     12if ( is_wp_error( $errors ) ) { 
     13    foreach( $errors->get_error_codes() as $code) 
     14        foreach( $errors->get_error_messages($code) as $message ) 
     15            echo "$message<br />"; 
    1616    exit; 
    1717} 
  • trunk/wp-admin/user-edit.php

    r3615 r3677  
    3535check_admin_referer(); 
    3636 
    37 $errors = array(); 
    38  
    3937if (!current_user_can('edit_users')) 
    40     $errors['head'] = __('You do not have permission to edit this user.'); 
     38    $errors = new WP_Error('head', __('You do not have permission to edit this user.')); 
    4139else 
    4240    $errors = edit_user($user_id); 
    4341 
    44 if(count($errors) == 0) { 
     42if( !is_wp_error( $errors ) ) { 
    4543    header("Location: user-edit.php?user_id=$user_id&updated=true"); 
    4644    exit; 
     
    5250$profileuser = new WP_User($user_id); 
    5351 
    54 if (!current_user_can('edit_users')) $errors['head'] = __('You do not have permission to edit this user.'); 
     52if (!current_user_can('edit_users')) 
     53    if ( !is_wp_error( $errors ) ) 
     54        $errors = new WP_Error('head', __('You do not have permission to edit this user.')); 
    5555?> 
    5656 
     
    6060</div> 
    6161<?php endif; ?> 
    62 <?php if ( count($errors) != 0 ) : ?> 
     62<?php if ( is_wp_error( $errors ) ) : ?> 
    6363<div class="error"> 
    6464    <ul> 
    6565    <?php 
    66     foreach($errors as $error) echo "<li>$error</li>"; 
     66    foreach( $errors->get_error_codes() as $code) 
     67        foreach( $errors->get_error_messages($code) as $message ) 
     68            echo "<li>$message</li>"; 
    6769    ?> 
    6870    </ul> 
  • trunk/wp-admin/users.php

    r3541 r3677  
    8080 
    8181    if ( !current_user_can('edit_users') ) 
    82         $error['edit_users'] = __('You can&#8217;t delete users.'); 
     82        $error = new WP_Error('edit_users', __('You can&#8217;t delete users.')); 
    8383 
    8484    $userids = $_POST['users']; 
     
    134134    check_admin_referer(); 
    135135 
    136     $errors = add_user(); 
    137  
    138     if(count($errors) == 0) { 
     136    $user_id = add_user(); 
     137    if ( is_wp_error( $user_id ) ) 
     138        $errors = $user_id; 
     139    else { 
    139140        header('Location: users.php?update=add'); 
    140141        die(); 
     
    142143 
    143144default: 
     145 
     146    $list_js = true; 
     147    $users_js = true; 
    144148 
    145149    include ('admin-header.php'); 
     
    188192        } 
    189193    endif;  
    190     if ( isset($errors) ) : ?> 
     194    if ( is_wp_error( $errors ) ) : ?> 
    191195    <div class="error"> 
    192196        <ul> 
    193197        <?php 
    194         foreach($errors as $error) echo "<li>$error</li>"; 
     198        foreach( $errors->get_error_codes() as $code) 
     199            foreach( $errors->get_error_messages($code) as $message ) 
     200                 echo "<li>$message</li>"; 
    195201        ?> 
    196202        </ul> 
     
    210216 
    211217    <tr> 
    212     <th colspan="8" align="left"> 
    213   <h3><?php echo $wp_roles->role_names[$role]; ?></h3> 
    214   </th></tr> 
    215  
     218        <th colspan="8" align="left"><h3><?php echo $wp_roles->role_names[$role]; ?></h3></th> 
     219    </tr> 
    216220    <tr> 
    217     <th><?php _e('ID') ?></th> 
    218     <th><?php _e('Username') ?></th> 
    219     <th><?php _e('Name') ?></th> 
    220     <th><?php _e('E-mail') ?></th> 
    221     <th><?php _e('Website') ?></th> 
    222     <th><?php _e('Posts') ?></th> 
    223     <th>&nbsp;</th> 
     221        <th><?php _e('ID') ?></th> 
     222        <th><?php _e('Username') ?></th> 
     223        <th><?php _e('Name') ?></th> 
     224        <th><?php _e('E-mail') ?></th> 
     225        <th><?php _e('Website') ?></th> 
     226        <th><?php _e('Posts') ?></th> 
     227        <th>&nbsp;</th> 
    224228    </tr> 
    225     <?php 
     229    <tbody id="role-<?php echo $role; ?>"><?php 
    226230    $style = ''; 
    227231    foreach ($roleclass as $user_object) { 
    228         $email = $user_object->user_email; 
    229         $url = $user_object->user_url; 
    230         $short_url = str_replace('http://', '', $url); 
    231         $short_url = str_replace('www.', '', $short_url); 
    232         if ('/' == substr($short_url, -1)) 
    233             $short_url = substr($short_url, 0, -1); 
    234         if (strlen($short_url) > 35) 
    235         $short_url =  substr($short_url, 0, 32).'...'; 
    236         $style = ('class="alternate"' == $style) ? '' : 'class="alternate"'; 
    237         $numposts = get_usernumposts($user_object->ID); 
    238         if (0 < $numposts) $numposts = "<a href='edit.php?author=$user_object->ID' title='" . __('View posts') . "'>$numposts</a>"; 
    239         echo " 
    240 <tr $style> 
    241     <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> 
    242     <td><label for='user_{$user_object->ID}'><strong>$user_object->user_login</strong></label></td> 
    243     <td><label for='user_{$user_object->ID}'>$user_object->first_name $user_object->last_name</label></td> 
    244     <td><a href='mailto:$email' title='" . sprintf(__('e-mail: %s'), $email) . "'>$email</a></td> 
    245     <td><a href='$url' title='website: $url'>$short_url</a></td>"; 
    246     echo "<td align='right'>$numposts</td>"; 
    247     echo '<td>'; 
    248     if (current_user_can('edit_users')) 
    249         echo "<a href='user-edit.php?user_id=$user_object->ID' class='edit'>".__('Edit')."</a>"; 
    250     echo '</td>'; 
    251     echo '</tr>'; 
     232        $style = (' class="alternate"' == $style) ? '' : ' class="alternate"'; 
     233        echo "\n\t" . user_row( $user_object, $style ); 
    252234    } 
    253235 
    254236    ?> 
    255237 
    256  
     238    </tbody> 
    257239<?php 
    258240    } 
     
    262244 
    263245    <h2><?php _e('Update Users'); ?></h2> 
    264 <?php 
    265 $role_select = '<select name="new_role">'; 
    266 foreach($wp_roles->role_names as $role => $name) { 
    267     $role_select .= "<option value=\"{$role}\">{$name}</option>"; 
    268 } 
    269 $role_select .= '</select>'; 
    270 ?>   
    271246  <ul style="list-style:none;"> 
    272247    <li><input type="radio" name="action" id="action0" value="delete" /> <label for="action0"><?php _e('Delete checked users.'); ?></label></li> 
    273     <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> 
     248    <li> 
     249        <input type="radio" name="action" id="action1" value="promote" /> <label for="action1"><?php _e('Set the Role of checked users to:'); ?></label> 
     250        <select name="new_role"><?php wp_dropdown_roles(); ?></select> 
     251    </li> 
    274252  </ul> 
    275253    <p class="submit"><input type="submit" value="<?php _e('Update &raquo;'); ?>" /></p> 
     
    314292    </tr> 
    315293<?php endif; ?> 
     294    <tr> 
     295      <th scope="row"><?php _e('Role'); ?></th> 
     296      <td><select name="role" id="role"><?php wp_dropdown_roles( get_settings('default_role') ); ?></select></td> 
     297    </tr> 
    316298  </table> 
    317299  <p class="submit"> 
    318     <input name="adduser" type="submit" id="adduser" value="<?php _e('Add User &raquo;') ?>" /> 
     300    <input name="adduser" type="submit" id="addusersub" value="<?php _e('Add User &raquo;') ?>" /> 
    319301  </p> 
    320302  </form> 
     303<div id="ajax-response"></div> 
    321304</div> 
    322305    <?php 
Note: See TracChangeset for help on using the changeset viewer.