Last active
August 29, 2015 14:01
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 protected] | |
* [email protected] | |
* | |
* @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