Last active
April 8, 2016 12:03
-
-
Save higiacomo/e4c484a1a271c8eb33a2 to your computer and use it in GitHub Desktop.
List last 100 registered WordPress / BuddyPress users without a gravatar or uploaded avatar
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
/** | |
* | |
* List last 100 registered users without a gravatar or uploaded avatar | |
* | |
*/ | |
add_action( 'admin_menu', 'wk_list_members_without_profile_picture' ); | |
function wk_list_members_without_profile_picture(){ | |
add_menu_page( 'Members w/o picture', 'Members w/o picture', 'manage_options', 'wb-without-avatar', 'wb_without_avatar' ); | |
} | |
function wb_without_avatar(){ | |
echo '<h1>Last 100 registered users without a profile picture</h1>'; | |
$args = array( | |
'orderby' => 'registered', | |
'order' => 'DESC', | |
'number' => '100', | |
); | |
$users = get_users( $args ); | |
// we'll create a list of comma separated emails as well | |
$email_list = array(); | |
echo '<table>'; | |
foreach ($users as $user) { | |
if ( wb_has_avatar($user->ID)) continue; | |
if ( wb_has_gravatar($user->user_email)) continue; | |
// if there's no avatar and no gravatar create a row with email, date of registration and link to profile page | |
echo '<tr>'; | |
printf('<td>%s</td>', $user->user_email); | |
printf('<td>%s</td>', $user->user_registered); | |
printf('<td><a href="%s">profilo (%s)</a></td>', get_admin_url().'user-edit.php?user_id='.$user->ID ,$user->ID); | |
echo '</tr>'; | |
$emails_bcc[] = $user->user_email; | |
} | |
echo '</table>'; | |
echo '<h2>Email friendly format</h2>'; | |
foreach ($emails_bcc as $email) printf('%s;<br>', $email); | |
} | |
// returns true if the uploaded avatar is different than the default one | |
function wb_has_avatar( $user_id ) { | |
$retval = false; | |
if ( bp_core_fetch_avatar( array( 'item_id' => $user_id, 'no_grav' => true, 'html' => false ) ) != bp_core_avatar_default( 'local' ) ) | |
$retval = true; | |
return $retval; | |
} | |
// returns true if there's a gravatar associated with this email | |
function wk_has_gravatar( $user_email ) { | |
$hashkey = md5(strtolower(trim($email))); | |
$uri = 'http://www.gravatar.com/avatar/' . $hashkey . '?d=404'; | |
$data = wp_cache_get($hashkey); | |
if (false === $data) { | |
$response = wp_remote_head($uri); | |
if( is_wp_error($response) ) $data = 'not200'; | |
else $data = $response['response']['code']; | |
// cache the response | |
wp_cache_set($hashkey, $data, $group = '', $expire = 60*5); | |
} | |
if ($data == '200') return true; | |
else return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment