[wp-trac] [WordPress Trac] #41544: $id_or_email parameter in get_avatar filter needs to be more concrete

WordPress Trac noreply at wordpress.org
Thu Aug 3 12:17:43 UTC 2017


#41544: $id_or_email parameter in get_avatar filter needs to be more concrete
----------------------------+------------------------------
 Reporter:  dikiy_forester  |       Owner:
     Type:  enhancement     |      Status:  new
 Priority:  normal          |   Milestone:  Awaiting Review
Component:  Users           |     Version:
 Severity:  normal          |  Resolution:
 Keywords:                  |     Focuses:
----------------------------+------------------------------

Comment (by dikiy_forester):

 > Given the nature / signature of this function I think the "concrete"
 user (or not) should be left to the plugin or theme to decide, and hence,
 the filter parameters are ok.

 Can't agree that theme or plugin should duplicate the code from
 https://core.trac.wordpress.org/browser/tags/4.8.1/src/wp-includes/link-
 template.php?rev=41211#L3923 just to been able to process the user
 identifier (or determine that there is no logged in user):
 {{{#!php
 <?php
         if ( is_object( $id_or_email ) && isset( $id_or_email->comment_ID
 ) ) {
                 $id_or_email = get_comment( $id_or_email );
         }

         // Process the user identifier.
         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 ( $id_or_email instanceof WP_User ) {
                 // User Object
                 $user = $id_or_email;
         } elseif ( $id_or_email instanceof WP_Post ) {
                 // Post Object
                 $user = get_user_by( 'id', (int) $id_or_email->post_author
 );
         } elseif ( $id_or_email instanceof WP_Comment ) {
                 /**
                  * Filters 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['url'] = false;
                         /** This filter is documented in wp-includes/link-
 template.php */
                         return apply_filters( 'get_avatar_data', $args,
 $id_or_email );
                 }

                 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;
                 }
         }

 }}}

 At least this block of code might be extracted from the
 {{{get_avatar_data()}}} to a separate function.

 As of original ticket topic, I still think that parameter
 {{{$id_or_email}}} needs to be concretized and contain the user id if
 available OR gravatar hash otherwise. So, developer can determine if it's
 a numeric value - it's user ID, otherwise it's a hash.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/41544#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list