Skip to content

Adds local avatar support #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions inc/fragment.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ public static function get_model_from_userdata( $user_data ) {
'displayName' => $user_data->display_name,
'firstName' => $user_data->user_firstname,
'lastName' => $user_data->user_lastname,
'localAvatar' => self::get_local_avatar_url( $user_data->ID ),
'url' => get_author_posts_url( $user_data->ID ),
'urlTitle' => sprintf( __( 'Posts by %1$s (%2$s)', 'o2' ), $user_data->display_name, '@' . $user_data->user_nicename ),
'hash' => md5( strtolower( trim( $user_data->user_email ) ) ),
Expand All @@ -486,6 +487,25 @@ public static function get_model_from_userdata( $user_data ) {
return $bootstrap_model;
}

/**
* This function is used to get the source attribute for a local avatar.
*
* @see filter get_avatar_url in wp-includes/link-template.php
* @see filter get_avatar_data in wp-includes/link-template.php
*
* @param mixed $user_id (Required) Accepts a user_id, gravatar md5 hash, user email, WP_User object, WP_Post object, or WP_Comment object.
* @return string|boolean $source Returns the source attribute of the avatar. Or false if not a local avatar.
*/
public static function get_local_avatar_url( $user_id ) {
$avatar_url = get_avatar_url( $user_id, array( 'size' => 48 ) );

if ( false !== strpos( $source, 'gravatar.com' ) ) {
return false;
}

return $avatar_url;
}

/**
* get_post_user_properties returns the userLogin for the author and
* bootstraps the rest of the user info
Expand Down
15 changes: 10 additions & 5 deletions js/collections/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,16 @@ o2.Collections.Users = ( function( $, Backbone ) {

var userAttributes = _.clone( user.attributes );

// Add the avatar info
var defaultAvatar = ( 'undefined' !== typeof o2.options.defaultAvatar ) ? o2.options.defaultAvatar : 'identicon';
userAttributes.avatar = 'https://gravatar.com/avatar/' + userAttributes.hash + '?d=' + defaultAvatar;
userAttributes.avatarSize = avatarSize;
// Add the avatar info. If there is a local avatar in use, override user gravatar.
if ( false !== userAttributes.localAvatar ) {
userAttributes.avatar = userAttributes.localAvatar;
userAttributes.avatarSize = avatarSize;
} else {
// Add the avatar info
var defaultAvatar = ( 'undefined' !== typeof o2.options.defaultAvatar ) ? o2.options.defaultAvatar : 'identicon';
userAttributes.avatar = 'https://gravatar.com/avatar/' + userAttributes.hash + '?d=' + defaultAvatar;
userAttributes.avatarSize = avatarSize;
}

return userAttributes;
},
Expand Down Expand Up @@ -138,4 +144,3 @@ o2.Collections.Users = ( function( $, Backbone ) {

} );
} )( jQuery, Backbone );