diff --git a/inc/fragment.php b/inc/fragment.php index 2af299a..3d8128d 100644 --- a/inc/fragment.php +++ b/inc/fragment.php @@ -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 ) ) ), @@ -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 diff --git a/js/collections/users.js b/js/collections/users.js index 88dbbeb..e2f6eec 100644 --- a/js/collections/users.js +++ b/js/collections/users.js @@ -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; }, @@ -138,4 +144,3 @@ o2.Collections.Users = ( function( $, Backbone ) { } ); } )( jQuery, Backbone ); -