Automattic / Co-Authors-Plus

Multiple bylines and Guest Authors for WordPress
https://wordpress.org/plugins/co-authors-plus/
GNU General Public License v2.0
291 stars 205 forks source link

If guest author has no email address, avatar from Gravatar is a 404 instead of the fallback image. #977

Closed pire closed 1 year ago

pire commented 1 year ago

This is the function where the magic happens.

function coauthors_get_avatar( $coauthor, $size = 32, $default = '', $alt = false, $class = null ) {
    global $coauthors_plus;

    if ( ! is_object( $coauthor ) ) {
        return '';
    }

    if ( isset( $coauthor->type ) && 'guest-author' == $coauthor->type ) {
        $guest_author_thumbnail = $coauthors_plus->guest_authors->get_guest_author_thumbnail( $coauthor, $size, $class );

        if ( $guest_author_thumbnail ) {
            return $guest_author_thumbnail;
        }
    }

    // Make sure we're dealing with an object for which we can retrieve an email
    if ( isset( $coauthor->user_email ) ) {
        return get_avatar( $coauthor->user_email, $size, $default, $alt, array( 'class' => $class ) );
    }

    // Nothing matched, an invalid object was passed.
    return '';
}

This bit checks if the user_email is set, but not if it's a blank string.

if ( isset( $coauthor->user_email ) )

So if it is a blank string no user_email is sent to get_avatar and a 404 image is returned instead of the default fallback.

I wonder if that is a new issue with how Gravatar works, since sending a string (not an email) is enough to get the fallback image back.

    // Make sure we're dealing with an object for which we can retrieve an email
    if ( isset( $coauthor->user_email ) ) {
        $email_to_send = $coauthor->user_email ? $coauthor->user_email : 'a';
        return get_avatar( $email_to_send, $size, $default, $alt, array( 'class' => $class ) );
    }
GaryJones commented 1 year ago

Looking at the format of Gravatar URLs, here's a URL for a populated avatar: http://2.gravatar.com/avatar/e70d4086e89c2e1e081870865be68485?s=64&d=mm&r=g

Here's a URL for an avatar with an empty string as the first parameter of get_avatar(): http://2.gravatar.com/avatar/?s=64&d=mm&r=g

I pulled this from a Guest Author with no email set, but it can also be grabbed via wp shell:

wp> get_avatar_url( '' );
=> string(43) "http://2.gravatar.com/avatar/?s=96&d=mm&r=g"
Screenshot 2023-08-19 at 23 17 30

As you can see the fallback when there's no hash is a 200 response and the usual grey image.

Screenshot 2023-08-19 at 23 20 27

Are you seeing something different?

pire commented 1 year ago

Hi @GaryJones,

I should have also included those image URLs to make it easier for you, sorry!

The URLS you shared are the same I got. They are working correctly now, so I am sure it was a bug on Gravatar's side which had not been yet reported!

Thanks for getting back to me and sorry for the unnecessary hassle.