WordPress.org

Make WordPress Core


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

AJAXify user addition. Props mdawaffe. fixes #2624

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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 
Note: See TracChangeset for help on using the changeset viewer.