Plugin Directory

Changeset 681718


Ignore:
Timestamp:
03/14/2013 10:22:53 AM (13 years ago)
Author:
sorich87
Message:

Allow importing fields with 0 value\nAdded option to update existing users by username or email

Location:
import-users-from-csv/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • import-users-from-csv/trunk/examples/import.csv

    r477250 r681718  
    11"user_login","user_email","user_pass","first_name","last_name","display_name","role","custom_usermeta_1","custom_usermeta_2"
    22"johndoe","john.doe@localhost.localdomain",,"John","Doe","John Doe","administrator","Test","Test"
    3 "janedoe","jane.doe@localhost.localdomain.com","test","Jane","Doe","Jane Doe","contributor",,
     3"janedoe","jane.doe@localhost.localdomain.com","test","Jane","Doe","Jane Doe","contributor","0",
  • import-users-from-csv/trunk/import-users-from-csv.php

    r507550 r681718  
    55/*
    66Plugin Name: Import Users from CSV
    7 Plugin URI: http://pubpoet.com/plugins/
     7Plugin URI: http://wordpress.org/extend/plugins/import-users-from-csv/
    88Description: Import Users data and metadata from a csv file.
    9 Version: 0.5.1
    10 Author: PubPoet
    11 Author URI: http://pubpoet.com/
     9Version: 0.6
     10Author: Ulrich Sossou
     11Author URI: http://ulrichsossou.com/
    1212License: GPL2
    1313Text Domain: import-users-from-csv
     
    7979                $filename              = $_FILES['users_csv']['tmp_name'];
    8080                $password_nag          = isset( $_POST['password_nag'] ) ? $_POST['password_nag'] : false;
     81                $users_update          = isset( $_POST['users_update'] ) ? $_POST['users_update'] : false;
    8182                $new_user_notification = isset( $_POST['new_user_notification'] ) ? $_POST['new_user_notification'] : false;
    8283
    83                 $results = self::import_csv( $filename, $password_nag, $new_user_notification );
     84                $results = self::import_csv( $filename, array(
     85                    'password_nag' => $password_nag,
     86                    'new_user_notification' => $new_user_notification,
     87                    'users_update' => $users_update
     88                ) );
    8489
    8590                // No users imported?
     
    154159        <table class="form-table">
    155160            <tr valign="top">
    156                 <th scope="row"><label for"users_csv"><?php _e( 'CSV file' , 'import-users-from-csv'); ?></label></th>
    157                 <td><input type="file" id="users_csv" name="users_csv" value="" class="all-options" /></td>
     161                <th scope="row"><label for="users_csv"><?php _e( 'CSV file' , 'import-users-from-csv'); ?></label></th>
     162                <td>
     163                    <input type="file" id="users_csv" name="users_csv" value="" class="all-options" /><br />
     164                    <span class="description"><?php echo sprintf( __( 'You may want to see <a href="%s">the example of the CSV file</a>.' , 'import-users-from-csv'), plugin_dir_url(__FILE__).'examples/import.csv'); ?></span>
     165                </td>
    158166            </tr>
    159167            <tr valign="top">
     
    163171                    <label for="new_user_notification">
    164172                        <input id="new_user_notification" name="new_user_notification" type="checkbox" value="1" />
    165                         Send to new users
     173                        <?php _e('Send to new users', 'import-users-from-csv') ?>
    166174                    </label>
    167175                </fieldset></td>
     
    173181                    <label for="password_nag">
    174182                        <input id="password_nag" name="password_nag" type="checkbox" value="1" />
    175                         Show password nag on new users signon
     183                        <?php _e('Show password nag on new users signon', 'import-users-from-csv') ?>
     184                    </label>
     185                </fieldset></td>
     186            </tr>
     187            <tr valign="top">
     188                <th scope="row"><?php _e( 'Users update' , 'import-users-from-csv'); ?></th>
     189                <td><fieldset>
     190                    <legend class="screen-reader-text"><span><?php _e( 'Users update' , 'import-users-from-csv' ); ?></span></legend>
     191                    <label for="users_update">
     192                        <input id="users_update" name="users_update" type="checkbox" value="1" />
     193                        <?php _e( 'Update user when a username or email exists', 'import-users-from-csv' ) ;?>
    176194                    </label>
    177195                </fieldset></td>
     
    179197        </table>
    180198        <p class="submit">
    181             <input type="submit" class="button-primary" value="<?php _e( 'Import' , 'import-users-from-csv'); ?>" />
     199            <input type="submit" class="button-primary" value="<?php _e( 'Import' , 'import-users-from-csv'); ?>" />
    182200        </p>
    183201    </form>
     
    190208     * @since 0.5
    191209     */
    192     public static function import_csv( $filename, $password_nag = false, $new_user_notification = false ) {
     210    public static function import_csv( $filename, $args ) {
    193211        $errors = $user_ids = array();
     212
     213        $defaults = array(
     214            'password_nag' => false,
     215            'new_user_notification' => false,
     216            'users_update' => false
     217        );
     218        extract( wp_parse_args( $args, $defaults ) );
    194219
    195220        // User data fields list used to differentiate with user meta
     
    236261                $column = trim( $column );
    237262
    238                 if ( empty( $column ) )
    239                     continue;
    240 
    241263                if ( in_array( $column_name, $userdata_fields ) ) {
    242264                    $userdata[$column_name] = $column;
     
    257279            do_action( 'is_iu_pre_user_import', $userdata, $usermeta );
    258280
    259             // Are we updating an old user or creating a new one?
     281            $user = $user_id = false;
     282
     283            if ( isset( $userdata['ID'] ) )
     284                $user = get_user_by( 'ID', $userdata['ID'] );
     285
     286            if ( ! $user && $users_update ) {
     287                if ( isset( $userdata['user_login'] ) )
     288                    $user = get_user_by( 'login', $userdata['user_login'] );
     289
     290                if ( ! $user && isset( $userdata['user_email'] ) )
     291                    $user = get_user_by( 'email', $userdata['user_email'] );
     292            }
     293
    260294            $update = false;
    261             $user_id = 0;
    262             if ( ! empty( $userdata['ID'] ) ) {
     295            if ( $user ) {
     296                $userdata['ID'] = $user->ID;
    263297                $update = true;
    264                 $user_id = $userdata['ID'];
    265298            }
    266299
     
    269302                $userdata['user_pass'] = wp_generate_password( 12, false );
    270303
    271             // Insert or update... at last! If only user ID was provided, we don't need to do anything at all. :)
    272             if ( array( 'ID' => $user_id ) == $userdata )
    273                 $user_id = get_userdata( $user_id )->ID; // To check if the user id exists
    274             else if ( $update )
     304            if ( $update )
    275305                $user_id = wp_update_user( $userdata );
    276306            else
  • import-users-from-csv/trunk/readme.txt

    r507550 r681718  
    33Tags: user, users, csv, batch, import, importer, admin
    44Requires at least: 3.1
    5 Tested up to: 3.4
    6 Stable tag: 0.5.1
     5Tested up to: 3.6
     6Stable tag: 0.6
    77
    88Import users from a CSV file into WordPress
     
    2929For feature request and bug reports, [please use the forums](http://wordpress.org/tags/import-users-from-csv?forum_id=10#postform).
    3030Code contributions are welcome [on Github](https://github.com/sorich87/Import-Users-from-CSV).
    31 
    32 = Tutorials =
    33 [How to Update Existing Users with "Import Users from CSV"](http://pubpoet.com/2011/05/28/update-users-import-users-csv/)
    3431
    3532== Installation ==
     
    7269You can try importing that file and look at the result.
    7370
    74 = How to Update Existing Users? =
    75 
    76 See [How to Update Existing Users with "Import Users from CSV"](http://pubpoet.com/2011/05/28/update-users-import-users-csv/)
    77 
    7871== Screenshots ==
    7972
     
    8174
    8275== Changelog ==
     76
     77= 0.6 =
     78* Fixed bug where importing fields with "0" value doesn't work
     79* Added option to update existing users by username or email
    8380
    8481= 0.5.1 =
Note: See TracChangeset for help on using the changeset viewer.