From 0492ad749d10a2412c00b878fb0b39f37c98b57e Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sun, 14 Jul 2019 14:34:02 +0000 Subject: [PATCH] REST API: Allow `rest_get_avatar_urls()` to accept full user, post, or comment objects, rather than just an email address, to provide better flexibility for alternative avatar data. Since the function uses `get_avatar_url()` internally, which already supports it, this should not have any backward compatibility concerns. Props donmhico, dshanske, pputzer, joehoyle, TimothyBlynJacobs. Fixes #40030. git-svn-id: https://develop.svn.wordpress.org/trunk@45632 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/rest-api.php | 9 +++++---- .../endpoints/class-wp-rest-comments-controller.php | 2 +- .../endpoints/class-wp-rest-users-controller.php | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php index 716b425e57..31223c6f30 100644 --- a/src/wp-includes/rest-api.php +++ b/src/wp-includes/rest-api.php @@ -1085,21 +1085,22 @@ function rest_is_boolean( $maybe_bool ) { } /** - * Retrieves the avatar urls in various sizes based on a given email address. + * Retrieves the avatar urls in various sizes. * * @since 4.7.0 * * @see get_avatar_url() * - * @param string $email Email address. + * @param mixed $id_or_email The Gravatar to retrieve a URL for. Accepts a user_id, gravatar md5 hash, + * user email, WP_User object, WP_Post object, or WP_Comment object. * @return array $urls Gravatar url for each size. */ -function rest_get_avatar_urls( $email ) { +function rest_get_avatar_urls( $id_or_email ) { $avatar_sizes = rest_get_avatar_sizes(); $urls = array(); foreach ( $avatar_sizes as $size ) { - $urls[ $size ] = get_avatar_url( $email, array( 'size' => $size ) ); + $urls[ $size ] = get_avatar_url( $id_or_email, array( 'size' => $size ) ); } return $urls; diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index 8fafc20e80..b730844fde 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php @@ -949,7 +949,7 @@ class WP_REST_Comments_Controller extends WP_REST_Controller { } if ( in_array( 'author_avatar_urls', $fields, true ) ) { - $data['author_avatar_urls'] = rest_get_avatar_urls( $comment->comment_author_email ); + $data['author_avatar_urls'] = rest_get_avatar_urls( $comment ); } if ( in_array( 'meta', $fields, true ) ) { diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php index 7553678456..bd09b70b32 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php @@ -938,7 +938,7 @@ class WP_REST_Users_Controller extends WP_REST_Controller { } if ( in_array( 'avatar_urls', $fields, true ) ) { - $data['avatar_urls'] = rest_get_avatar_urls( $user->user_email ); + $data['avatar_urls'] = rest_get_avatar_urls( $user ); } if ( in_array( 'meta', $fields, true ) ) {