From 768e4807ad2d05f53b7cbc845be5d22507b9ece9 Mon Sep 17 00:00:00 2001 From: BE-Webdesign Date: Sun, 14 Feb 2016 20:21:26 -0500 Subject: [PATCH 1/2] Adds local avatar support This was tested using https://wordpress.org/plugins/wp-user-avatar/, which has 200,000 active installs. This will handle local avatars that are using get_avatar() for functionality. Solves #14, for most cases. There are likely edge cases where this may not work, but for the most part, I imagine this will add support for local avatars. --- inc/fragment.php | 35 +++++++++++++++++++++++++++++++++++ js/collections/users.js | 14 +++++++++----- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/inc/fragment.php b/inc/fragment.php index 2af299a..a334bf7 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,40 @@ 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. That can be overriden in get_avatar(). + * + * @TODO This function needs work to handle more edge cases and implementations of local avatars ( currently will only work with functions hooked into get_avatar() ). + * @see filter get_avatar in wp-includes/ + * + * @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 ) { + // 48 seems to be the default size used in o2. + $avatar = get_avatar( $user_id, 48 ); + + // Create a domdocument. + $dom = new DOMDocument(); + $dom->loadHTML( $avatar ); + + // Find the image. + $img = $dom->getElementsByTagName( 'img' ); + $img = $img->item( 0 ); + + if ( $img->hasAttribute( 'src' ) ) { + // Get avatar's source attribute. + $source = $img->getAttribute( 'src' ); + // If it is a gravatar return false so that backbone can use the hardcoded https:// version in the users collection. + if ( false !== strpos( $source, 'gravatar.com' ) ) { + return false; + } + } else { + return false; + } + return $source; + } + /** * 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 a902837..d0c728e 100644 --- a/js/collections/users.js +++ b/js/collections/users.js @@ -65,10 +65,15 @@ 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 { + 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 +143,3 @@ o2.Collections.Users = ( function( $, Backbone ) { } ); } )( jQuery, Backbone ); - From 22c9171ec917c6fc4af15c18c84e7c6c74d72554 Mon Sep 17 00:00:00 2001 From: BE-Webdesign Date: Thu, 31 Mar 2016 20:15:36 -0400 Subject: [PATCH 2/2] Use only get_avatar_url. Changed to use only get_avatar_url(). This is the proper function to use and agreement was reached for not adding backwards compatibility for get_avatar hook. --- inc/fragment.php | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/inc/fragment.php b/inc/fragment.php index a334bf7..3d8128d 100644 --- a/inc/fragment.php +++ b/inc/fragment.php @@ -488,37 +488,22 @@ public static function get_model_from_userdata( $user_data ) { } /** - * This function is used to get the source attribute for a local avatar. That can be overriden in get_avatar(). + * This function is used to get the source attribute for a local avatar. * - * @TODO This function needs work to handle more edge cases and implementations of local avatars ( currently will only work with functions hooked into get_avatar() ). - * @see filter get_avatar in wp-includes/ + * @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 ) { - // 48 seems to be the default size used in o2. - $avatar = get_avatar( $user_id, 48 ); - - // Create a domdocument. - $dom = new DOMDocument(); - $dom->loadHTML( $avatar ); - - // Find the image. - $img = $dom->getElementsByTagName( 'img' ); - $img = $img->item( 0 ); - - if ( $img->hasAttribute( 'src' ) ) { - // Get avatar's source attribute. - $source = $img->getAttribute( 'src' ); - // If it is a gravatar return false so that backbone can use the hardcoded https:// version in the users collection. - if ( false !== strpos( $source, 'gravatar.com' ) ) { - return false; - } - } else { + $avatar_url = get_avatar_url( $user_id, array( 'size' => 48 ) ); + + if ( false !== strpos( $source, 'gravatar.com' ) ) { return false; } - return $source; + + return $avatar_url; } /**