Jetpack::get_avatar_url PHP Method

get_avatar_url() public static method

Centralize the function here until it gets added to core.
public static get_avatar_url ( integer | string | object $id_or_email, integer $size = 96, string $default = '', boolean $force_display = false ) : array
$id_or_email integer | string | object A user ID, email address, or comment object
$size integer Size of the avatar image
$default string URL to a default image to use if no avatar is available
$force_display boolean Whether to force it to return an avatar even if show_avatars is disabled
return array First element is the URL, second is the class.
    public static function get_avatar_url($id_or_email, $size = 96, $default = '', $force_display = false)
    {
        // Don't bother adding the __return_true filter if it's already there.
        $has_filter = has_filter('pre_option_show_avatars', '__return_true');
        if ($force_display && !$has_filter) {
            add_filter('pre_option_show_avatars', '__return_true');
        }
        $avatar = get_avatar($id_or_email, $size, $default);
        if ($force_display && !$has_filter) {
            remove_filter('pre_option_show_avatars', '__return_true');
        }
        // If no data, fail out.
        if (is_wp_error($avatar) || !$avatar) {
            return array(null, null);
        }
        // Pull out the URL.  If it's not there, fail out.
        if (!preg_match('/src=["\']([^"\']+)["\']/', $avatar, $url_matches)) {
            return array(null, null);
        }
        $url = wp_specialchars_decode($url_matches[1], ENT_QUOTES);
        // Pull out the class, but it's not a big deal if it's missing.
        $class = '';
        if (preg_match('/class=["\']([^"\']+)["\']/', $avatar, $class_matches)) {
            $class = wp_specialchars_decode($class_matches[1], ENT_QUOTES);
        }
        return array($url, $class);
    }

Usage Example

/**
 * Gather data about the current user.
 *
 * @since 4.1.0
 *
 * @return array
 */
function jetpack_current_user_data()
{
    global $current_user;
    $is_master_user = $current_user->ID == Jetpack_Options::get_option('master_user');
    $dotcom_data = Jetpack::get_connected_user_data();
    // Add connected user gravatar to the returned dotcom_data
    $avatar_data = Jetpack::get_avatar_url($dotcom_data['email']);
    $dotcom_data['avatar'] = $avatar_data[0];
    $current_user_data = array('isConnected' => Jetpack::is_user_connected($current_user->ID), 'isMaster' => $is_master_user, 'username' => $current_user->user_login, 'wpcomUser' => $dotcom_data, 'gravatar' => get_avatar($current_user->ID, 40), 'permissions' => array('admin_page' => current_user_can('jetpack_admin_page'), 'connect' => current_user_can('jetpack_connect'), 'disconnect' => current_user_can('jetpack_disconnect'), 'manage_modules' => current_user_can('jetpack_manage_modules'), 'network_admin' => current_user_can('jetpack_network_admin_page'), 'network_sites_page' => current_user_can('jetpack_network_sites_page'), 'edit_posts' => current_user_can('edit_posts'), 'manage_options' => current_user_can('manage_options'), 'view_stats' => current_user_can('view_stats'), 'manage_plugins' => current_user_can('install_plugins') && current_user_can('activate_plugins') && current_user_can('update_plugins') && current_user_can('delete_plugins')));
    return $current_user_data;
}
Jetpack