Skip to content

Instantly share code, notes, and snippets.

@mboynes
Last active August 29, 2015 14:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mboynes/f6b4fe17992af759489e to your computer and use it in GitHub Desktop.
Save mboynes/f6b4fe17992af759489e to your computer and use it in GitHub Desktop.
Check a file of email addresses against the WP User database to see if they're in use or not. This is not part of a command; it can be added to any.
<?php
/**
* Check a file of email addresses against the user database to see if those
* email addresses are currently in use.
*
* ## OPTIONS
*
* <file>
* : The file of users to import.
*
* ## EXAMPLES
*
* wp COMMAND user_recon emails.csv
*
* Sample emails.csv file:
*
* email
* bobjones@domain.com
* janedoe@domain.org
*
* @synopsis <file>
*/
function user_recon( $args, $assoc_args ) {
$filename = $args[0];
if ( ! file_exists( $filename ) ) {
WP_CLI::warning( sprintf( "Missing file: %s", $filename ) );
}
$total = $exists = $missing = 0;
foreach ( new \WP_CLI\Iterators\CSV( $filename ) as $user ) {
$total++;
$email = sanitize_email( $user['email'] );
if ( empty( $email ) ) {
WP_CLI::warning( "`{$user['email']}` is not a valid email address." );
continue;
}
$existing_user = get_user_by( 'email', $email );
if ( $existing_user ) {
$exists++;
WP_CLI::line( "{$email} is currently in use by {$existing_user->user_login}" );
} else {
$missing++;
}
}
WP_CLI::success( "Process complete! Found {$exists} email addresses in use and {$missing} not in use across {$total} entries in {$filename}." );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment