Index: src/wp-includes/link-template.php
===================================================================
--- src/wp-includes/link-template.php	(revision 29824)
+++ src/wp-includes/link-template.php	(working copy)
@@ -3139,3 +3139,195 @@
 		echo $before, $link, $after;
 	}
 }
+
+/**
+ * Retrieve the avatar URL for a user, email address, MD5 hash, comment, or post.
+ *
+ * @uses apply_filters() 'pre_get_avatar_url' to bypass
+ * @uses apply_filters() 'get_avatar_url' filters the result
+ * @uses apply_filters() 'get_avatar_comment_types' filters the comment types for which we can calculate an avatar
+ *
+ * @since 4.1.0
+ * @param mixed $id_or_email The Gravatar to retrieve {
+ *     @type int    {user_id}                   : Use the email address of the corresponding user
+ *     @type string "{hash}@md5.gravatar.com"   : Use the hash directly
+ *     @type string "{email}"
+ *     @type object {User row or WP_User object}: Use the user's email
+ *     @type object {Post row or WP_Post object}: Use the post_author's email
+ *     @type object {Comment row}               : Use the comment's user_id or comment_author_email
+ * }
+ * @param array $args Extra options to apply to the avatar {
+ *     @type int    $size           Size of the avatar image
+ *     @type string $default        URL for the default image or a default type {
+ *             404                    : Return a 404 instead of a default image
+ *             retro                  : 8bit
+ *             monsterid              : Monster
+ *             wavatar                : cartoon face
+ *             identicon              : the "quilt"
+ *             mystery, mm, mysteryman: The Oyster Man
+ *             blank                  : A transparent GIF
+ *             gravatar_default       : Gravatar Logo
+ *     }
+ *     @type bool   $force_default  Always show the default image, never the Gravatar
+ *     @type string $rating         display avatars up to the given rating: G < PG < R < X.
+ *     @type string $scheme         @see set_url_scheme()
+ *     @type array  $processed_args Pass as reference.  When the function returns, the value will be the processed/sanitized $args plus a "found_avatar" guess.
+ * }
+ *
+ * @return bool|string URL false on failure
+ */
+function get_avatar_url( $id_or_email, $args = null ) {
+	$original_args = $args;
+
+	$args = wp_parse_args( $args, array(
+		'size'           => 96,
+		'default'        => get_option( 'avatar_default', 'mystery' ),
+		'force_default'  => false,
+		'rating'         => get_option( 'avatar_rating' ),
+		'scheme'         => null,
+		'processed_args' => null, // if used, should be a reference
+	) );
+
+	if ( is_numeric( $args['size'] ) ) {
+		$args['size'] = absint( $args['size'] );
+		if ( ! $args['size'] ) {
+			$args['size'] = 96;
+		}
+	} else {
+		$args['size'] = 96;
+	}
+
+	if ( empty( $args['default'] ) ) {
+		$args['default'] = 'mystery';
+	}
+
+	switch ( $args['default'] ) {
+		case 'mm' :
+		case 'mystery' :
+		case 'mysteryman' :
+			$args['default'] = 'mm';
+			break;
+		case 'gravatar_default' :
+			$args['default'] = false;
+			break;
+	}
+
+	$args['force_default'] = (bool) $args['force_default'];
+
+	$args['rating'] = strtolower( $args['rating'] );
+
+	$args['found_avatar'] = false;
+
+	/**
+	 * Filter the avatar to retrieve early.
+	 *
+	 * If this filter doesn't return null, the return value will be used as the avatar URL.
+	 *
+	 * @since 4.1.0
+	 *
+	 * @param string            $url           URL for the user's avatar. Default null.
+	 * @param int|object|string $id_or_email   A user ID, email address, or comment object.
+	 * @param array             $args          Arguments passed to get_avatar_url(), after processing.
+	 * @param array             $original_args Original arguments passed to get_avatar_url().
+	 */
+	$url = apply_filters_ref_array( 'pre_get_avatar_url', array( null, $id_or_email, &$args, $original_args ) );
+	if ( ! is_null( $url ) ) {
+		/** This filter is documented in src/wp-includes/link-template.php */
+		$return = apply_filters_ref_array( 'get_avatar_url', array( $url, $id_or_email, &$args, $original_args ) );
+		$args['processed_args'] = $args;
+		unset( $args['processed_args']['processed_args'] );
+		return $return;
+	}
+
+	$email_hash = '';
+	$user = $email = false;
+
+	if ( is_numeric( $id_or_email ) ) {
+		$user = get_user_by( 'id', absint( $id_or_email ) );
+	} elseif ( is_string( $id_or_email ) ) {
+		if ( strpos( $id_or_email, '@md5.gravatar.com' ) ) {
+			// md5 hash
+			list( $email_hash ) = explode( '@', $id_or_email );
+		} else {
+			// email address
+			$email = $id_or_email;
+		}
+	} elseif ( is_object( $id_or_email ) ) {
+		if ( isset( $id_or_email->comment_ID ) ) {
+			// Comment Object
+
+			/**
+			 * Filter the list of allowed comment types for retrieving avatars.
+			 *
+			 * @since 3.0.0
+			 *
+			 * @param array $types An array of content types. Default only contains 'comment'.
+			 */
+			$allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
+			if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) {
+				$args['processed_args'] = $args;
+				unset( $args['processed_args']['processed_args'] );
+				return false;
+			}
+
+			if ( ! empty( $id_or_email->user_id ) ) {
+				$user = get_user_by( 'id', (int) $id_or_email->user_id );
+			}
+			if ( ( ! $user || is_wp_error( $user ) ) && ! empty( $id_or_email->comment_author_email ) ) {
+				$email = $id_or_email->comment_author_email;
+			}
+		} elseif ( ! empty( $id_or_email->user_login ) ) {
+			// User Object
+			$user = $id_or_email;
+		} elseif ( ! empty( $id_or_email->post_author ) ) {
+			// Post Object
+			$user = get_user_by( 'id', (int) $id_or_email->post_author );
+		}
+	}
+
+	if ( !$email_hash ) {
+		if ( $user ) {
+			$email = $user->user_email;
+		}
+
+		if ( $email ) {
+			$email_hash = md5( strtolower( trim( $email ) ) );
+		}
+	}
+
+	if ( $email_hash ) {
+		$args['found_avatar'] = true;
+		$gravatar_server = hexdec( $email_hash[0] ) % 3;
+	} else {
+		$gravatar_server = rand( 0, 2 );
+	}
+
+	$url_args = array(
+		's' => $args['size'],
+		'd' => $args['default'],
+		'f' => $args['force_default'] ? 'y' : false,
+		'r' => $args['rating'],
+	);
+
+	$url = sprintf( 'http://%d.gravatar.com/avatar/%s', $gravatar_server, $email_hash );
+
+	$url = add_query_arg(
+		rawurlencode_deep( array_filter( $url_args ) ),
+		set_url_scheme( $url, $args['scheme'] )
+	);
+
+	/**
+	 * Filter the avatar to retrieve.
+	 *
+	 * @since 4.1.0
+	 *
+	 * @param string            $url           URL for the user's avatar.
+	 * @param int|object|string $id_or_email   A user ID, email address, or comment object.
+	 * @param array             $args          Arguments passed to get_avatar_url(), after processing.
+	 * @param array             $original_args Original arguments passed to get_avatar_url().
+	 */
+	$return = apply_filters_ref_array( 'get_avatar_url', array( $url, $id_or_email, &$args, $original_args ) );
+	$args['processed_args'] = $args;
+	unset( $args['processed_args']['processed_args'] );
+	return $return;
+}
Index: src/wp-includes/pluggable.php
===================================================================
--- src/wp-includes/pluggable.php	(revision 29824)
+++ src/wp-includes/pluggable.php	(working copy)
@@ -2086,123 +2086,134 @@
 
 if ( !function_exists( 'get_avatar' ) ) :
 /**
- * Retrieve the avatar for a user who provided a user ID or email address.
+ * Retrieve the avatar img tag for a user, email address, MD5 hash, comment, or post.
  *
+ * @uses apply_filters() 'pre_get_avatar' to bypass
+ * @uses apply_filters() 'get_avatar' filters the result
+ *
  * @since 2.5.0
+ * @param mixed  $id_or_email The Gravatar to retrieve {
+ *     @type int    {user_id}                   : Use the email address of the corresponding user
+ *     @type string "{hash}@md5.gravatar.com"   : Use the hash directly
+ *     @type string "{email}"
+ *     @type object {User row or WP_User object}: Use the user's email
+ *     @type object {Post row or WP_Post object}: Use the post_author's email
+ *     @type object {Comment row}               : Use the comment's user_id or comment_author_email
+ * }
+ * @param int    $size        Size of the avatar image
+ * @param string $default     URL for the default image or a default type {
+ *     404                    : Return a 404 instead of a default image
+ *     retro                  : 8bit
+ *     monsterid              : Monster
+ *     wavatar                : cartoon face
+ *     identicon              : the "quilt"
+ *     mystery, mm, mysteryman: The Oyster Man
+ *     blank                  : A transparent GIF
+ *     gravatar_default       : Gravatar Logo
+ * }
+ * @param string $alt         Alternative text to use in image tag. Defaults to blank
+ * @param array  $args        Extra options to apply to the avatar (since 4.1.0) {
+ *     @type bool   $force_default Always show the default image, never the Gravatar
+ *     @type string $rating        display avatars up to the given rating: G < PG < R < X.
+ *     @type string $scheme        @see set_url_scheme()
+ *     @type mixed  $class         array or string of additional classes to add to the img element
+ *     @type bool   $force_display Always show the avatar - ignore the show_avatars option
+ * }
  *
- * @param int|string|object $id_or_email A user ID,  email address, or comment object
- * @param int $size Size of the avatar image
- * @param string $default URL to a default image to use if no avatar is available
- * @param string $alt Alternative text to use in image tag. Defaults to blank
- * @return string <img> tag for the user's avatar
-*/
-function get_avatar( $id_or_email, $size = '96', $default = '', $alt = false ) {
-	if ( ! get_option('show_avatars') )
-		return false;
+ * @return bool|string <img> tag for the user's avatar.  False on failure.
+ */
+function get_avatar( $id_or_email, $size = 96, $default = '', $alt = '', $args = null ) {
+	$defaults = array(
+		// get_avatar_url() args
+		'size'          => 96,
+		'default'       => get_option( 'avatar_default', 'mystery' ),
+		'force_default' => false,
+		'rating'        => get_option( 'avatar_rating' ),
+		'scheme'        => null,
 
-	if ( false === $alt)
-		$safe_alt = '';
-	else
-		$safe_alt = esc_attr( $alt );
+		'alt'           => '',
+		'class'         => null,
+		'force_display' => false,
+	);
 
-	if ( !is_numeric($size) )
-		$size = '96';
+	if ( empty( $args ) ) {
+		$args = array();
+	}
 
-	$email = '';
-	if ( is_numeric($id_or_email) ) {
-		$id = (int) $id_or_email;
-		$user = get_userdata($id);
-		if ( $user )
-			$email = $user->user_email;
-	} elseif ( is_object($id_or_email) ) {
-		// No avatar for pingbacks or trackbacks
+	$args['size']    = $size;
+	$args['default'] = $default;
+	$args['alt']     = $alt;
 
-		/**
-		 * Filter the list of allowed comment types for retrieving avatars.
-		 *
-		 * @since 3.0.0
-		 *
-		 * @param array $types An array of content types. Default only contains 'comment'.
-		 */
-		$allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
-		if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) )
-			return false;
+	$original_args = $args;
 
-		if ( ! empty( $id_or_email->user_id ) ) {
-			$id = (int) $id_or_email->user_id;
-			$user = get_userdata($id);
-			if ( $user )
-				$email = $user->user_email;
-		}
+	$args = wp_parse_args( $args, $defaults );
 
-		if ( ! $email && ! empty( $id_or_email->comment_author_email ) )
-			$email = $id_or_email->comment_author_email;
-	} else {
-		$email = $id_or_email;
+	/**
+	 * Filter the avatar to retrieve early.
+	 *
+	 * If this filter doesn't return null, the return value will be used as the avatar URL.
+	 *
+	 * @since 4.1.0
+	 *
+	 * @param string            $avatar        HTML for the user's avatar. Default null.
+	 * @param int|object|string $id_or_email   A user ID, email address, or comment object.
+	 * @param array             $args          Arguments passed to get_avatar_url(), after processing.
+	 * @param array             $original_args Original arguments passed to get_avatar_url().
+	 */
+	$avatar = apply_filters_ref_array( 'pre_get_avatar', array( null, $id_or_email, &$args, $original_args ) );
+	if ( ! is_null( $avatar ) ) {
+		/** This filter is documented in src/wp-include/pluggable.php */
+		return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $args, $original_args );
 	}
 
-	if ( empty($default) ) {
-		$avatar_default = get_option('avatar_default');
-		if ( empty($avatar_default) )
-			$default = 'mystery';
-		else
-			$default = $avatar_default;
+	if ( !$args['force_display'] && ! get_option( 'show_avatars' ) ) {
+		return false;
 	}
 
-	if ( !empty($email) )
-		$email_hash = md5( strtolower( trim( $email ) ) );
-
-	if ( is_ssl() ) {
-		$host = 'https://secure.gravatar.com';
-	} else {
-		if ( !empty($email) )
-			$host = sprintf( "http://%d.gravatar.com", ( hexdec( $email_hash[0] ) % 2 ) );
-		else
-			$host = 'http://0.gravatar.com';
+	$processed_args = null;
+	$args['processed_args'] =& $processed_args;
+	$url = get_avatar_url( $id_or_email, $args );
+	if ( ! $url || is_wp_error( $url ) ) {
+        return false;
 	}
 
-	if ( 'mystery' == $default )
-		$default = "$host/avatar/ad516503a11cd5ca435acc9bb6523536?s={$size}"; // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com')
-	elseif ( 'blank' == $default )
-		$default = $email ? 'blank' : includes_url( 'images/blank.gif' );
-	elseif ( !empty($email) && 'gravatar_default' == $default )
-		$default = '';
-	elseif ( 'gravatar_default' == $default )
-		$default = "$host/avatar/?s={$size}";
-	elseif ( empty($email) )
-		$default = "$host/avatar/?d=$default&amp;s={$size}";
-	elseif ( strpos($default, 'http://') === 0 )
-		$default = add_query_arg( 's', $size, $default );
+	$class = array( 'avatar', 'avatar-' . (int) $processed_args['size'], 'photo' );
 
-	if ( !empty($email) ) {
-		$out = "$host/avatar/";
-		$out .= $email_hash;
-		$out .= '?s='.$size;
-		$out .= '&amp;d=' . urlencode( $default );
+	if ( ! $processed_args['found_avatar'] || $processed_args['force_default'] ) {
+        $class[] = ' avatar-default';
+	}
 
-		$rating = get_option('avatar_rating');
-		if ( !empty( $rating ) )
-			$out .= "&amp;r={$rating}";
-
-		$out = str_replace( '&#038;', '&amp;', esc_url( $out ) );
-		$avatar = "<img alt='{$safe_alt}' src='{$out}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
-	} else {
-		$out = esc_url( $default );
-		$avatar = "<img alt='{$safe_alt}' src='{$out}' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />";
+	if ( $args['class'] ) {
+		if ( is_array( $args['class'] ) ) {
+			$class = array_merge( $class, $args['class'] );
+		} else {
+			$class[] = $args['class'];
+		}
 	}
 
+	$avatar = sprintf(
+		'<img alt="%s" src="%s" class="%s" height="%d" width="%d" />',
+		esc_attr( $processed_args['alt'] ),
+		esc_url( $url ),
+		esc_attr( join( ' ', $class ) ),
+		(int) $processed_args['size'],
+		(int) $processed_args['size']
+	);
+
 	/**
 	 * Filter the avatar to retrieve.
 	 *
 	 * @since 2.5.0
 	 *
-	 * @param string            $avatar      Image tag for the user's avatar.
-	 * @param int|object|string $id_or_email A user ID, email address, or comment object.
-	 * @param int               $size        Square avatar width and height in pixels to retrieve.
-	 * @param string            $alt         Alternative text to use in the avatar image tag.
-	 *                                       Default empty.
+	 * @param string            $avatar         Image tag for the user's avatar.
+	 * @param int|object|string $id_or_email    A user ID, email address, or comment object.
+	 * @param int               $size           Square avatar width and height in pixels to retrieve.
+	 * @param string            $alt            Alternative text to use in the avatar image tag.
+	 *                                          Default empty.
+	 * @param array             $processed_args Arguments passed to get_avatar_url(), after processing. (since 4.1.0)
+	 * @param array             $original_args  Original arguments passed to get_avatar_url(). (since 4.1.0)
 	 */
-	return apply_filters( 'get_avatar', $avatar, $id_or_email, $size, $default, $alt );
+	return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $processed_args, $original_args );
 }
 endif;
 
