Index: src/wp-includes/link-template.php
===================================================================
--- src/wp-includes/link-template.php (revision 25990)
+++ src/wp-includes/link-template.php (working copy)
@@ -2531,3 +2531,163 @@
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 3.8.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;
+
+ $url = apply_filters_ref_array( 'pre_get_avatar_url', array( null, $id_or_email, &$args, $original_args ) );
+ if ( !is_null( $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;
+ }
+
+ $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
+
+ // No avatar for pingbacks or trackbacks
+ $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;
+ }
+
+ $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', hexdec( $email_hash[0] ) % 3, $email_hash );
+
+ $url = add_query_arg(
+ rawurlencode_deep( array_filter( $url_args ) ),
+ set_url_scheme( $url, $args['scheme'] )
+ );
+
+ $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 25990)
+++ src/wp-includes/pluggable.php (working copy)
@@ -1612,101 +1612,120 @@
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
- * @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 tag for the user's avatar
-*/
-function get_avatar( $id_or_email, $size = '96', $default = '', $alt = false ) {
- if ( ! get_option('show_avatars') )
- return false;
+ * @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 tag for the user's avatar. False on failure.
+ */
+// Old Parameters: $id_or_email, $size = 96, $default = '', $alt = '' )
+function get_avatar( $id_or_email, $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,
+
+ 'alt' => '',
+ 'class' => null,
+ 'force_display' => false,
+ );
+
+ if ( is_scalar( $args ) ) {
+ $args = array(
+ 'size' => $args,
+ );
+
+ $num_args = func_num_args();
+ if ( $num_args > 4 ) {
+ $num_args = 4;
+ }
- if ( false === $alt)
- $safe_alt = '';
- else
- $safe_alt = esc_attr( $alt );
-
- if ( !is_numeric($size) )
- $size = '96';
-
- $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
- $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;
-
- if ( !empty($id_or_email->user_id) ) {
- $id = (int) $id_or_email->user_id;
- $user = get_userdata($id);
- if ( $user)
- $email = $user->user_email;
- } elseif ( !empty($id_or_email->comment_author_email) ) {
- $email = $id_or_email->comment_author_email;
+ switch ( $num_args ) {
+ // no breaks
+ case 4 :
+ $args['alt'] = func_get_arg( 3 );
+ case 3 :
+ $args['default'] = func_get_arg( 2 );
}
} else {
- $email = $id_or_email;
+ $args = (array) $args;
}
- if ( empty($default) ) {
- $avatar_default = get_option('avatar_default');
- if ( empty($avatar_default) )
- $default = 'mystery';
- else
- $default = $avatar_default;
+ $original_args = $args;
+
+ $args = wp_parse_args( $args, $defaults );
+
+ $avatar = apply_filters_ref_array( 'pre_get_avatar', array( null, $id_or_email, &$args, $original_args ) );
+ if ( !is_null( $avatar ) ) {
+ return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $args, $original_args );
}
- if ( !empty($email) )
- $email_hash = md5( strtolower( trim( $email ) ) );
+ if ( !$args['force_display'] && !get_option( 'show_avatars' ) ) {
+ return false;
+ }
- 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';
- }
-
- 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&s={$size}";
- elseif ( strpos($default, 'http://') === 0 )
- $default = add_query_arg( 's', $size, $default );
-
- if ( !empty($email) ) {
- $out = "$host/avatar/";
- $out .= $email_hash;
- $out .= '?s='.$size;
- $out .= '&d=' . urlencode( $default );
-
- $rating = get_option('avatar_rating');
- if ( !empty( $rating ) )
- $out .= "&r={$rating}";
+ $processed_args = null;
+ $args['processed_args'] =& $processed_args;
+ $url = get_avatar_url( $id_or_email, $args );
+ if ( !$url || is_wp_error( $url ) ) {
+ return false;
+ }
- $out = str_replace( '&', '&', esc_url( $out ) );
- $avatar = "";
- } else {
- $avatar = "";
+ $class = array( 'avatar', 'avatar-' . (int) $processed_args['size'], 'photo' );
+
+ if ( !$processed_args['found_avatar'] || $processed_args['force_default'] ) {
+ $class[] = ' avatar-default';
}
- return apply_filters('get_avatar', $avatar, $id_or_email, $size, $default, $alt);
+ if ( $args['class'] ) {
+ if ( is_array( $args['class'] ) ) {
+ $class = array_merge( $class, $args['class'] );
+ } else {
+ $class[] = $args['class'];
+ }
+ }
+
+ $avatar = sprintf(
+ '',
+ esc_attr( $processed_args['alt'] ),
+ esc_url( $url ),
+ esc_attr( join( ' ', $class ) ),
+ (int) $processed_args['size'],
+ (int) $processed_args['size']
+ );
+
+ return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $processed_args, $original_args );
}
endif;