Skip to content

Commit

Permalink
Merge pull request #100 from wp-cli/patch-96
Browse files Browse the repository at this point in the history
`wp user import-csv` to be able to import CSV from STDIN
  • Loading branch information
danielbachhuber authored Oct 7, 2017
2 parents 50f907c + 4048954 commit dfedb1e
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
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,[email protected],Bob Jones,contributor
newuser1,[email protected],New User,author
admin,[email protected],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":"[email protected]",
"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

0 comments on commit dfedb1e

Please sign in to comment.