Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ node_modules/
vendor/
*.zip
*.tar.gz
composer.lock
42 changes: 42 additions & 0 deletions features/user-import-csv.feature
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,45 @@ Feature: Import users from CSV
| display_name | roles |
| Bob Jones | contributor |
| Bill Jones | administrator,author |

Scenario: Importing users from STDIN
Given a WP install
And a users.csv file:
"""
user_login,user_email,display_name,role
bobjones,bobjones@example.com,Bob Jones,contributor
newuser1,newuser1@example.com,New User,author
admin,admin@example.com,Existing User,administrator
"""

When I try `wp user import-csv -`
Then STDERR should be:
"""
Error: Unable to read content from STDIN.
"""

When I run `cat users.csv | wp user import-csv -`
Then STDOUT should be:
"""
Success: bobjones created.
Success: newuser1 created.
Success: admin updated.
"""
And an email should not be sent

When I run `wp user list --format=count`
Then STDOUT should be:
"""
3
"""

When I run `wp user list --format=json`
Then STDOUT should be JSON containing:
"""
[{
"user_login":"admin",
"display_name":"Existing User",
"user_email":"admin@example.com",
"roles":"administrator"
}]
"""
31 changes: 28 additions & 3 deletions src/User_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ public function list_caps( $args, $assoc_args ) {
* ## OPTIONS
*
* <file>
* : The local or remote CSV file of users to import.
* : The local or remote CSV file of users to import. If '-', then reads from STDIN.
*
* [--send-email]
* : Send an email to new users with their account details.
Expand Down Expand Up @@ -876,15 +876,40 @@ public function import_csv( $args, $assoc_args ) {
if ( in_array( $response_code[0], array( 4, 5 ) ) ) {
WP_CLI::error( "Couldn't access remote CSV file (HTTP {$response_code} response)." );
}
} else if ( ! file_exists( $filename ) ) {
} elseif ( '-' === $filename ) {
if ( ! WP_CLI\Entity\Utils::has_stdin() ) {
\WP_CLI::error( "Unable to read content from STDIN." );
}
} elseif ( ! file_exists( $filename ) ) {
WP_CLI::error( sprintf( "Missing file: %s", $filename ) );
}

// Don't send core's emails during the creation / update process
add_filter( 'send_password_change_email', '__return_false' );
add_filter( 'send_email_change_email', '__return_false' );

foreach ( new \WP_CLI\Iterators\CSV( $filename ) as $i => $new_user ) {
if ( '-' === $filename && WP_CLI\Entity\Utils::has_stdin() ) {
$file_object = new NoRewindIterator( new SplFileObject( "php://stdin" ) );
$file_object->setFlags( SplFileObject::READ_CSV );
$csv_data = array();
$indexes = array();
foreach ( $file_object as $line ) {
if ( empty( $line[0] ) ) {
continue;
} elseif ( empty( $indexes ) ) {
$indexes = $line;
continue;
}
foreach ( $indexes as $n => $key ) {
$data[ $key ] = $line[ $n ];
}
$csv_data[] = $data;
}
} else {
$csv_data = new \WP_CLI\Iterators\CSV( $filename );
}

foreach ( $csv_data as $i => $new_user ) {
$defaults = array(
'role' => get_option('default_role'),
'user_pass' => wp_generate_password(),
Expand Down