In `get_avatar_data()` and `get_avatar()`, allow `height` and `width` to be specified separately (both default to `size`). Also allow arbitrary attributes on the `<img>` via the `extra_attr` arg.

Props miqrogroove.
See #31469.

Built from https://develop.svn.wordpress.org/trunk@31561


git-svn-id: http://core.svn.wordpress.org/trunk@31542 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Scott Taylor 2015-02-26 21:17:24 +00:00
parent b95d99d1c1
commit e899c370a4
3 changed files with 44 additions and 6 deletions

View File

@ -3422,7 +3422,9 @@ function get_avatar_url( $id_or_email, $args = null ) {
* @param array $args {
* Optional. Arguments to return instead of the default arguments.
*
* @type int $size Height and width of the avatar in pixels. Default 96.
* @type int $size Height and width of the avatar image file in pixels. Default 96.
* @type int $height Display height of the avatar in pixels. Defaults to $size.
* @type int $width Display width of the avatar in pixels. Defaults to $size.
* @type string $default URL for the default image or a default type. Accepts '404' (return
* a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),
* 'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',
@ -3436,6 +3438,7 @@ function get_avatar_url( $id_or_email, $args = null ) {
* Default null.
* @type array $processed_args When the function returns, the value will be the processed/sanitized $args
* plus a "found_avatar" guess. Pass as a reference. Default null.
* @type string $extra_attr HTML attribute to insert in the IMG element. Has no default and is not sanitized.
* }
*
* @return array $processed_args {
@ -3449,11 +3452,14 @@ function get_avatar_url( $id_or_email, $args = null ) {
function get_avatar_data( $id_or_email, $args = null ) {
$args = wp_parse_args( $args, array(
'size' => 96,
'height' => null,
'width' => null,
'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
'extra_attr' => '',
) );
if ( is_numeric( $args['size'] ) ) {
@ -3465,6 +3471,24 @@ function get_avatar_data( $id_or_email, $args = null ) {
$args['size'] = 96;
}
if ( is_numeric( $args['height'] ) ) {
$args['height'] = absint( $args['height'] );
if ( ! $args['height'] ) {
$args['height'] = $args['size'];
}
} else {
$args['height'] = $args['size'];
}
if ( is_numeric( $args['width'] ) ) {
$args['width'] = absint( $args['width'] );
if ( ! $args['width'] ) {
$args['width'] = $args['size'];
}
} else {
$args['width'] = $args['size'];
}
if ( empty( $args['default'] ) ) {
$args['default'] = get_option( 'avatar_default', 'mystery' );
}

View File

@ -2101,7 +2101,7 @@ if ( !function_exists( 'get_avatar' ) ) :
*
* @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
* user email, WP_User object, WP_Post object, or comment object.
* @param int $size Optional. Height and width of the avatar in pixels. Default 96.
* @param int $size Optional. Height and width of the avatar image file in pixels. Default 96.
* @param string $default Optional. URL for the default image or a default type. Accepts '404'
* (return a 404 instead of a default image), 'retro' (8bit), 'monsterid'
* (monster), 'wavatar' (cartoon face), 'indenticon' (the "quilt"),
@ -2112,6 +2112,8 @@ if ( !function_exists( 'get_avatar' ) ) :
* @param array $args {
* Optional. Extra arguments to retrieve the avatar.
*
* @type int $height Display height of the avatar in pixels. Defaults to $size.
* @type int $width Display width of the avatar in pixels. Defaults to $size.
* @type bool $force_default Whether to always show the default image, never the Gravatar. Default false.
* @type string $rating What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
* judged in that order. Default is the value of the 'avatar_rating' option.
@ -2121,6 +2123,7 @@ if ( !function_exists( 'get_avatar' ) ) :
* Default null.
* @type bool $force_display Whether to always show the avatar - ignores the show_avatars option.
* Default false.
* @type string $extra_attr HTML attribute to insert in the IMG element. Has no default and is not sanitized.
* }
*
* @return false|string `<img>` tag for the user's avatar. False on failure.
@ -2129,6 +2132,8 @@ function get_avatar( $id_or_email, $size = 96, $default = '', $alt = '', $args =
$defaults = array(
// get_avatar_data() args.
'size' => 96,
'height' => null,
'width' => null,
'default' => get_option( 'avatar_default', 'mystery' ),
'force_default' => false,
'rating' => get_option( 'avatar_rating' ),
@ -2136,6 +2141,7 @@ function get_avatar( $id_or_email, $size = 96, $default = '', $alt = '', $args =
'alt' => '',
'class' => null,
'force_display' => false,
'extra_attr' => '',
);
if ( empty( $args ) ) {
@ -2148,6 +2154,13 @@ function get_avatar( $id_or_email, $size = 96, $default = '', $alt = '', $args =
$args = wp_parse_args( $args, $defaults );
if ( empty( $args['height'] ) ) {
$args['height'] = $args['size'];
}
if ( empty( $args['width'] ) ) {
$args['width'] = $args['size'];
}
/**
* Filter whether to retrieve the avatar URL early.
*
@ -2193,12 +2206,13 @@ function get_avatar( $id_or_email, $size = 96, $default = '', $alt = '', $args =
}
$avatar = sprintf(
"<img alt='%s' src='%s' class='%s' height='%d' width='%d' />",
"<img alt='%s' src='%s' class='%s' height='%d' width='%d' %s/>",
esc_attr( $args['alt'] ),
esc_url( $url ),
esc_attr( join( ' ', $class ) ),
(int) $args['size'],
(int) $args['size']
(int) $args['height'],
(int) $args['width'],
$args['extra_attr']
);
/**

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.2-alpha-31560';
$wp_version = '4.2-alpha-31561';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.