Make WordPress Core

Ticket #21195: 21195.15.diff

File 21195.15.diff, 39.5 KB (added by pento, 10 years ago)
  • src/wp-admin/includes/class-wp-ms-users-list-table.php

     
    186186                                        break;
    187187
    188188                                        case 'username':
    189                                                 $avatar = get_avatar( $user->user_email, 32 );
     189                                                $avatar = wp_get_avatar( $user->user_email, 32 );
    190190                                                $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user->ID ) ) );
    191191
    192192                                                echo "<td $attributes>"; ?>
  • src/wp-admin/includes/class-wp-posts-list-table.php

     
    682682
    683683                                if ( $can_edit_post && $post->post_status != 'trash' ) {
    684684                                        if ( $lock_holder ) {
    685                                                 $locked_avatar = get_avatar( $lock_holder->ID, 18 );
     685                                                $locked_avatar = wp_get_avatar( $lock_holder->ID, 18 );
    686686                                                $locked_text = esc_html( sprintf( __( '%s is currently editing' ), $lock_holder->display_name ) );
    687687                                        } else {
    688688                                                $locked_avatar = $locked_text = '';
  • src/wp-admin/includes/class-wp-users-list-table.php

     
    393393                        $edit = '<strong>' . $user_object->user_login . '</strong>';
    394394                }
    395395                $role_name = isset( $wp_roles->role_names[$role] ) ? translate_user_role( $wp_roles->role_names[$role] ) : __( 'None' );
    396                 $avatar = get_avatar( $user_object->ID, 32 );
     396                $avatar = wp_get_avatar( $user_object->ID, 32 );
    397397
    398398                $r = "<tr id='user-$user_object->ID'$style>";
    399399
  • src/wp-admin/includes/comment.php

     
    163163 */
    164164function floated_admin_avatar( $name ) {
    165165        global $comment;
    166         $avatar = get_avatar( $comment, 32, 'mystery' );
     166        $avatar = wp_get_avatar( $comment, 32, 'mystery' );
    167167        return "$avatar $name";
    168168}
    169169
  • src/wp-admin/includes/dashboard.php

     
    589589
    590590                <div id="comment-<?php echo $comment->comment_ID; ?>" <?php comment_class( array( 'comment-item', wp_get_comment_status($comment->comment_ID) ) ); ?>>
    591591
    592                         <?php echo get_avatar( $comment, 50, 'mystery' ); ?>
     592                        <?php echo wp_get_avatar( $comment, 50, 'mystery' ); ?>
    593593
    594594                        <?php if ( !$comment->comment_type || 'comment' == $comment->comment_type ) : ?>
    595595
  • src/wp-admin/includes/misc.php

     
    694694                        if ( ( $user_id = wp_check_post_lock( $post_id ) ) && ( $user = get_userdata( $user_id ) ) && current_user_can( 'edit_post', $post_id ) ) {
    695695                                $send = array( 'text' => sprintf( __( '%s is currently editing' ), $user->display_name ) );
    696696
    697                                 if ( ( $avatar = get_avatar( $user->ID, 18 ) ) && preg_match( "|src='([^']+)'|", $avatar, $matches ) )
     697                                if ( ( $avatar = wp_get_avatar( $user->ID, 18 ) ) && preg_match( "|src='([^']+)'|", $avatar, $matches ) )
    698698                                        $send['avatar_src'] = $matches[1];
    699699
    700700                                $checked[$key] = $send;
     
    730730                                'text' => sprintf( __( '%s has taken over and is currently editing.' ), $user->display_name )
    731731                        );
    732732
    733                         if ( $avatar = get_avatar( $user->ID, 64 ) ) {
     733                        if ( $avatar = wp_get_avatar( $user->ID, 64 ) ) {
    734734                                if ( preg_match( "|src='([^']+)'|", $avatar, $matches ) )
    735735                                        $error['avatar_src'] = $matches[1];
    736736                        }
  • src/wp-admin/includes/post.php

     
    14621462
    14631463                ?>
    14641464                <div class="post-locked-message">
    1465                 <div class="post-locked-avatar"><?php echo get_avatar( $user->ID, 64 ); ?></div>
     1465                <div class="post-locked-avatar"><?php echo wp_get_avatar( $user->ID, 64 ); ?></div>
    14661466                <p class="currently-editing wp-tab-first" tabindex="0">
    14671467                <?php
    14681468                        _e( 'This content is currently locked.' );
  • src/wp-admin/includes/revision.php

     
    180180                if ( ! isset( $authors[ $revision->post_author ] ) ) {
    181181                        $authors[ $revision->post_author ] = array(
    182182                                'id' => (int) $revision->post_author,
    183                                 'avatar' => $show_avatars ? get_avatar( $revision->post_author, 32 ) : '',
     183                                'avatar' => $show_avatars ? wp_get_avatar( $revision->post_author, 32 ) : '',
    184184                                'name' => get_the_author_meta( 'display_name', $revision->post_author ),
    185185                        );
    186186                }
  • src/wp-admin/options-discussion.php

     
    243243        $selected = ($default == $default_key) ? 'checked="checked" ' : '';
    244244        $avatar_list .= "\n\t<label><input type='radio' name='avatar_default' id='avatar_{$default_key}' value='" . esc_attr($default_key) . "' {$selected}/> ";
    245245
    246         $avatar = get_avatar( $user_email, $size, $default_key );
     246        $avatar = wp_get_avatar( $user_email, $size, $default_key );
    247247        $avatar_list .= preg_replace("/src='(.+?)'/", "src='\$1&amp;forcedefault=1'", $avatar);
    248248
    249249        $avatar_list .= ' ' . $default_name . '</label>';
  • src/wp-includes/admin-bar.php

     
    187187        if ( ! $user_id )
    188188                return;
    189189
    190         $avatar = get_avatar( $user_id, 26 );
     190        $avatar = wp_get_avatar( $user_id, 26 );
    191191        $howdy  = sprintf( __('Howdy, %1$s'), $current_user->display_name );
    192192        $class  = empty( $avatar ) ? '' : 'with-avatar';
    193193
     
    223223                'id'     => 'user-actions',
    224224        ) );
    225225
    226         $user_info  = get_avatar( $user_id, 64 );
     226        $user_info  = wp_get_avatar( $user_id, 64 );
    227227        $user_info .= "<span class='display-name'>{$current_user->display_name}</span>";
    228228
    229229        if ( $current_user->display_name !== $current_user->user_login )
  • src/wp-includes/comment-template.php

     
    18871887                <div id="div-comment-<?php comment_ID(); ?>" class="comment-body">
    18881888                <?php endif; ?>
    18891889                <div class="comment-author vcard">
    1890                         <?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
     1890                        <?php if ( 0 != $args['avatar_size'] ) echo wp_get_avatar( $comment, $args['avatar_size'] ); ?>
    18911891                        <?php printf( __( '<cite class="fn">%s</cite> <span class="says">says:</span>' ), get_comment_author_link() ); ?>
    18921892                </div>
    18931893                <?php if ( '0' == $comment->comment_approved ) : ?>
     
    19391939                        <article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
    19401940                                <footer class="comment-meta">
    19411941                                        <div class="comment-author vcard">
    1942                                                 <?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, $args['avatar_size'] ); ?>
     1942                                                <?php if ( 0 != $args['avatar_size'] ) echo wp_get_avatar( $comment, $args['avatar_size'] ); ?>
    19431943                                                <?php printf( __( '%s <span class="says">says:</span>' ), sprintf( '<b class="fn">%s</b>', get_comment_author_link() ) ); ?>
    19441944                                        </div><!-- .comment-author -->
    19451945
  • src/wp-includes/link-template.php

     
    33243324                echo $before, $link, $after;
    33253325        }
    33263326}
     3327
     3328/**
     3329 * Retrieve the avatar `<img>` tag for a user, email address, MD5 hash, comment, or post.
     3330 *
     3331 * @since 4.2.0
     3332 *
     3333 * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
     3334 *                           user email, WP_User object, WP_Post object, or comment object.
     3335 * @param int    $size       Optional. Height and width of the avatar in pixels. Default 96.
     3336 * @param string $default    Optional. URL for the default image or a default type. Accepts '404'
     3337 *                           (return a 404 instead of a default image), 'retro' (8bit), 'monsterid'
     3338 *                           (monster), 'wavatar' (cartoon face), 'indenticon' (the "quilt"),
     3339 *                           'mystery', 'mm', or 'mysterman' (The Oyster Man), 'blank' (transparent GIF),
     3340 *                           or 'gravatar_default' (the Gravatar logo). Default is the value of the
     3341 *                           'avatar_default' option, with a fallback of 'mystery'.
     3342 * @param string $alt        Optional. Alternative text to use in &lt;img&gt; tag. Default empty.
     3343 * @param array  $args       {
     3344 *     Optional. Extra arguments to retrieve the avatar.
     3345 *
     3346 *     @type bool         $force_default Whether to always show the default image, never the Gravatar. Default false.
     3347 *     @type string       $rating        What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
     3348 *                                       judged in that order. Default is the value of the 'avatar_rating' option.
     3349 *     @type string       $scheme        URL scheme to use. See {@see set_url_scheme()} for accepted values.
     3350 *                                       Default null.
     3351 *     @type array|string $class         Array or string of additional classes to add to the &lt;img&gt; element.
     3352 *                                       Default null.
     3353 *     @type bool         $force_display Whether to always show the avatar - ignores the show_avatars option.
     3354 *                                       Default false.
     3355 * }
     3356 *
     3357 * @return false|string `<img>` tag for the user's avatar. False on failure.
     3358 */
     3359function wp_get_avatar( $id_or_email, $size = 96, $default = '', $alt = '', $args = null ) {
     3360        $defaults = array(
     3361                // wp_get_avatar_data() args.
     3362                'size'          => 96,
     3363                'default'       => get_option( 'avatar_default', 'mystery' ),
     3364                'force_default' => false,
     3365                'rating'        => get_option( 'avatar_rating' ),
     3366                'scheme'        => null,
     3367                'alt'           => '',
     3368                'class'         => null,
     3369                'force_display' => false,
     3370        );
     3371
     3372        if ( empty( $args ) ) {
     3373                $args = array();
     3374        }
     3375
     3376        $args['size']    = $size;
     3377        $args['default'] = $default;
     3378        $args['alt']     = $alt;
     3379
     3380        $args = wp_parse_args( $args, $defaults );
     3381
     3382        /**
     3383         * Filter whether to retrieve the avatar URL early.
     3384         *
     3385         * Passing a non-null value will effectively short-circuit {@see wp_get_avatar()},
     3386         * passing the value through the 'pre_get_avatar' filter and returning early.
     3387         *
     3388         * @since 4.2.0
     3389         *
     3390         * @param string            $avatar        HTML for the user's avatar. Default null.
     3391         * @param int|object|string $id_or_email   A user ID, email address, or comment object.
     3392         * @param array             $args          Arguments passed to wp_get_avatar_url(), after processing.
     3393         */
     3394        $avatar = apply_filters( 'pre_get_avatar', null, $id_or_email, $args );
     3395        if ( ! is_null( $avatar ) ) {
     3396                /** This filter is documented in src/wp-include/pluggable.php */
     3397                return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $args );
     3398        }
     3399
     3400        if ( ! $args['force_display'] && ! get_option( 'show_avatars' ) ) {
     3401                return false;
     3402        }
     3403
     3404        $args = wp_get_avatar_data( $id_or_email, $args );
     3405
     3406        $url = $args['url'];
     3407
     3408        if ( ! $url || is_wp_error( $url ) ) {
     3409        return false;
     3410        }
     3411
     3412        $class = array( 'avatar', 'avatar-' . (int) $args['size'], 'photo' );
     3413
     3414        if ( ! $args['found_avatar'] || $args['force_default'] ) {
     3415        $class[] = 'avatar-default';
     3416        }
     3417
     3418        if ( $args['class'] ) {
     3419                if ( is_array( $args['class'] ) ) {
     3420                        $class = array_merge( $class, $args['class'] );
     3421                } else {
     3422                        $class[] = $args['class'];
     3423                }
     3424        }
     3425
     3426        $avatar = sprintf(
     3427                '<img alt="%s" src="%s" class="%s" height="%d" width="%d" />',
     3428                esc_attr( $args['alt'] ),
     3429                esc_url( $url ),
     3430                esc_attr( join( ' ', $class ) ),
     3431                (int) $args['size'],
     3432                (int) $args['size']
     3433        );
     3434
     3435        /**
     3436         * Filter the avatar to retrieve.
     3437         *
     3438         * @since 2.5.0
     3439         * @since 4.2.0 $args parameter added
     3440         *
     3441         * @param string            $avatar         &lt;img&gt; tag for the user's avatar.
     3442         * @param int|object|string $id_or_email    A user ID, email address, or comment object.
     3443         * @param int               $size           Square avatar width and height in pixels to retrieve.
     3444         * @param string            $alt            Alternative text to use in the avatar image tag.
     3445         *                                          Default empty.
     3446         * @param array             $args           Arguments passed to wp_get_avatar_data(), after processing.
     3447         */
     3448        return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $args );
     3449}
     3450
     3451/**
     3452 * Retrieve the avatar URL.
     3453 *
     3454 * @since 4.2.0
     3455 *
     3456 * @param mixed $id_or_email The Gravatar to retrieve a URL for. Accepts a user_id, gravatar md5 hash,
     3457 *                           user email, WP_User object, WP_Post object, or comment object.
     3458 * @param array $args        {
     3459 *     Optional. Arguments to return instead of the default arguments.
     3460 *
     3461 *     @type int    $size           Height and width of the avatar in pixels. Default 96.
     3462 *     @type string $default        URL for the default image or a default type. Accepts '404' (return
     3463 *                                  a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),
     3464 *                                  'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',
     3465 *                                  or 'mysterman' (The Oyster Man), 'blank' (transparent GIF), or
     3466 *                                  'gravatar_default' (the Gravatar logo). Default is the value of the
     3467 *                                  'avatar_default' option, with a fallback of 'mystery'.
     3468 *     @type bool   $force_default  Whether to always show the default image, never the Gravatar. Default false.
     3469 *     @type string $rating         What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
     3470 *                                  judged in that order. Default is the value of the 'avatar_rating' option.
     3471 *     @type string $scheme         URL scheme to use. See {@see set_url_scheme()} for accepted values.
     3472 *                                  Default null.
     3473 *     @type array  $processed_args When the function returns, the value will be the processed/sanitized $args
     3474 *                                  plus a "found_avatar" guess. Pass as a reference. Default null.
     3475 * }
     3476 *
     3477 * @return false|string The URL of the avatar we found, or false if we couldn't find an avatar.
     3478 */
     3479function wp_get_avatar_url( $id_or_email, $args = null ) {
     3480        $args = wp_get_avatar_data( $id_or_email, $args );
     3481        return $args['url'];
     3482}
     3483
     3484/**
     3485 * Retrieve default data about the avatar.
     3486 *
     3487 * @since 4.2.0
     3488 *
     3489 * @param mixed $id_or_email The Gravatar to check the data against. Accepts a user_id, gravatar md5 hash,
     3490 *                           user email, WP_User object, WP_Post object, or comment object.
     3491 * @param array $args        {
     3492 *     Optional. Arguments to return instead of the default arguments.
     3493 *
     3494 *     @type int    $size           Height and width of the avatar in pixels. Default 96.
     3495 *     @type string $default        URL for the default image or a default type. Accepts '404' (return
     3496 *                                  a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),
     3497 *                                  'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',
     3498 *                                  or 'mysterman' (The Oyster Man), 'blank' (transparent GIF), or
     3499 *                                  'gravatar_default' (the Gravatar logo). Default is the value of the
     3500 *                                  'avatar_default' option, with a fallback of 'mystery'.
     3501 *     @type bool   $force_default  Whether to always show the default image, never the Gravatar. Default false.
     3502 *     @type string $rating         What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
     3503 *                                  judged in that order. Default is the value of the 'avatar_rating' option.
     3504 *     @type string $scheme         URL scheme to use. See {@see set_url_scheme()} for accepted values.
     3505 *                                  Default null.
     3506 *     @type array  $processed_args When the function returns, the value will be the processed/sanitized $args
     3507 *                                  plus a "found_avatar" guess. Pass as a reference. Default null.
     3508 * }
     3509 *
     3510 * @return array $processed_args {
     3511 *     Along with the arguments passed in $args, this will contain a couple of extra arguments.
     3512 *
     3513 *     @type bool         $found_avatar True if we were able to find an avatar for this user,
     3514 *                                      false or not set if we couldn't.
     3515 *     @type false|string $url          The URL of the avatar we found, or false if we couldn't find an avatar.
     3516 * }
     3517 */
     3518function wp_get_avatar_data( $id_or_email, $args = null ) {
     3519        $args = wp_parse_args( $args, array(
     3520                'size'           => 96,
     3521                'default'        => get_option( 'avatar_default', 'mystery' ),
     3522                'force_default'  => false,
     3523                'rating'         => get_option( 'avatar_rating' ),
     3524                'scheme'         => null,
     3525                'processed_args' => null, // if used, should be a reference
     3526        ) );
     3527
     3528        if ( is_numeric( $args['size'] ) ) {
     3529                $args['size'] = absint( $args['size'] );
     3530                if ( ! $args['size'] ) {
     3531                        $args['size'] = 96;
     3532                }
     3533        } else {
     3534                $args['size'] = 96;
     3535        }
     3536
     3537        if ( empty( $args['default'] ) ) {
     3538                $args['default'] = 'mystery';
     3539        }
     3540
     3541        switch ( $args['default'] ) {
     3542                case 'mm' :
     3543                case 'mystery' :
     3544                case 'mysteryman' :
     3545                        $args['default'] = 'mm';
     3546                        break;
     3547                case 'gravatar_default' :
     3548                        $args['default'] = false;
     3549                        break;
     3550        }
     3551
     3552        $args['force_default'] = (bool) $args['force_default'];
     3553
     3554        $args['rating'] = strtolower( $args['rating'] );
     3555
     3556        $args['found_avatar'] = false;
     3557
     3558        /**
     3559         * Filter whether to retrieve the avatar URL early.
     3560         *
     3561         * Passing a non-null value in the 'url' member of the return array will
     3562         * effectively short circuit {@see wp_get_avatar_data()}, passing the value
     3563         * through the 'get_avatar_data' filter and returning early.
     3564         *
     3565         * @since 4.2.0
     3566         *
     3567         * @param array             $args          Arguments passed to wp_get_avatar_data(), after processing.
     3568         * @param int|object|string $id_or_email   A user ID, email address, or comment object.
     3569         */
     3570        $args = apply_filters( 'pre_get_avatar_data', $args, $id_or_email );
     3571
     3572        if ( isset( $args['url'] ) && ! is_null( $args['url'] ) ) {
     3573                /** This filter is documented in src/wp-includes/link-template.php */
     3574                return apply_filters( 'get_avatar_data', $args, $id_or_email );
     3575        }
     3576
     3577        $email_hash = '';
     3578        $user = $email = false;
     3579
     3580        // Process the user identifier.
     3581        if ( is_numeric( $id_or_email ) ) {
     3582                $user = get_user_by( 'id', absint( $id_or_email ) );
     3583        } elseif ( is_string( $id_or_email ) ) {
     3584                if ( strpos( $id_or_email, '@md5.gravatar.com' ) ) {
     3585                        // md5 hash
     3586                        list( $email_hash ) = explode( '@', $id_or_email );
     3587                } else {
     3588                        // email address
     3589                        $email = $id_or_email;
     3590                }
     3591        } elseif ( $id_or_email instanceof WP_User ) {
     3592                // User Object
     3593                $user = $id_or_email;
     3594        } elseif ( $id_or_email instanceof WP_Post ) {
     3595                // Post Object
     3596                $user = get_user_by( 'id', (int) $id_or_email->post_author );
     3597        } elseif ( is_object( $id_or_email ) && isset( $id_or_email->comment_ID ) ) {
     3598                // Comment Object
     3599
     3600                /**
     3601                 * Filter the list of allowed comment types for retrieving avatars.
     3602                 *
     3603                 * @since 3.0.0
     3604                 *
     3605                 * @param array $types An array of content types. Default only contains 'comment'.
     3606                 */
     3607                $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
     3608                if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) {
     3609                        $args['url'] = false;
     3610                        /** This filter is documented in src/wp-includes/link-template.php */
     3611                        return apply_filters( 'get_avatar_data', $args, $id_or_email );
     3612                }
     3613
     3614                if ( ! empty( $id_or_email->user_id ) ) {
     3615                        $user = get_user_by( 'id', (int) $id_or_email->user_id );
     3616                }
     3617                if ( ( ! $user || is_wp_error( $user ) ) && ! empty( $id_or_email->comment_author_email ) ) {
     3618                        $email = $id_or_email->comment_author_email;
     3619                }
     3620        }
     3621
     3622        if ( ! $email_hash ) {
     3623                if ( $user ) {
     3624                        $email = $user->user_email;
     3625                }
     3626
     3627                if ( $email ) {
     3628                        $email_hash = md5( strtolower( trim( $email ) ) );
     3629                }
     3630        }
     3631
     3632        if ( $email_hash ) {
     3633                $args['found_avatar'] = true;
     3634                $gravatar_server = hexdec( $email_hash[0] ) % 3;
     3635        } else {
     3636                $gravatar_server = rand( 0, 2 );
     3637        }
     3638
     3639        $url_args = array(
     3640                's' => $args['size'],
     3641                'd' => $args['default'],
     3642                'f' => $args['force_default'] ? 'y' : false,
     3643                'r' => $args['rating'],
     3644        );
     3645
     3646        $url = sprintf( 'http://%d.gravatar.com/avatar/%s', $gravatar_server, $email_hash );
     3647
     3648        $url = add_query_arg(
     3649                rawurlencode_deep( array_filter( $url_args ) ),
     3650                set_url_scheme( $url, $args['scheme'] )
     3651        );
     3652
     3653        /**
     3654         * Filter the avatar URL.
     3655         *
     3656         * @since 4.2.0
     3657         *
     3658         * @param string            $url           The URL of the avatar.
     3659         * @param int|object|string $id_or_email   A user ID, email address, or comment object.
     3660         * @param array             $args          Arguments passed to wp_get_avatar_data(), after processing.
     3661         */
     3662        $args['url'] = apply_filters( 'get_avatar_url', $url, $id_or_email, $args );
     3663
     3664        /**
     3665         * Filter the avatar data.
     3666         *
     3667         * @since 4.2.0
     3668         *
     3669         * @param array             $args          Arguments passed to wp_get_avatar_data(), after processing.
     3670         * @param int|object|string $id_or_email   A user ID, email address, or comment object.
     3671         */
     3672        $args = apply_filters( 'get_avatar_data', $args, $id_or_email );
     3673
     3674        // Don't return a broken URL if we couldn't find the email hash, and none of the filters returned a different URL.
     3675        if ( ! $email_hash && $url === $args['url'] ) {
     3676                $args['url'] = false;
     3677        }
     3678
     3679        return $args;
     3680}
  • src/wp-includes/pluggable-deprecated.php

     
    189189                        _deprecated_function( __CLASS__ . '::' . $name, '3.5', 'the Atom Publishing Protocol plugin' );
    190190                }
    191191        }
    192 }
    193  No newline at end of file
     192}
     193
     194if ( !function_exists( 'get_avatar' ) ) :
     195/**
     196 * Retrieve the avatar for a user who provided a user ID or email address.
     197 *
     198 * @since 2.5.0
     199 * @deprecated 4.2.0
     200 *
     201 * @param int|string|object $id_or_email A user ID,  email address, or comment object
     202 * @param int $size Size of the avatar image
     203 * @param string $default URL to a default image to use if no avatar is available
     204 * @param string $alt Alternative text to use in image tag. Defaults to blank
     205 * @return false|string `<img>` tag for the user's avatar.
     206*/
     207function get_avatar( $id_or_email, $size = '96', $default = '', $alt = false ) {
     208        _deprecated_function( __FUNCTION__, '4.2', 'wp_get_avatar()' );
     209        if ( ! get_option('show_avatars') )
     210                return false;
     211
     212        if ( false === $alt)
     213                $safe_alt = '';
     214        else
     215                $safe_alt = esc_attr( $alt );
     216
     217        if ( !is_numeric($size) )
     218                $size = '96';
     219
     220        $email = '';
     221        if ( is_numeric($id_or_email) ) {
     222                $id = (int) $id_or_email;
     223                $user = get_userdata($id);
     224                if ( $user )
     225                        $email = $user->user_email;
     226        } elseif ( is_object($id_or_email) ) {
     227                // No avatar for pingbacks or trackbacks
     228
     229                /**
     230                 * Filter the list of allowed comment types for retrieving avatars.
     231                 *
     232                 * @since 3.0.0
     233                 *
     234                 * @param array $types An array of content types. Default only contains 'comment'.
     235                 */
     236                $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
     237                if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) )
     238                        return false;
     239
     240                if ( ! empty( $id_or_email->user_id ) ) {
     241                        $id = (int) $id_or_email->user_id;
     242                        $user = get_userdata($id);
     243                        if ( $user )
     244                                $email = $user->user_email;
     245                }
     246
     247                if ( ! $email && ! empty( $id_or_email->comment_author_email ) )
     248                        $email = $id_or_email->comment_author_email;
     249        } else {
     250                $email = $id_or_email;
     251        }
     252
     253        if ( empty($default) ) {
     254                $avatar_default = get_option('avatar_default');
     255                if ( empty($avatar_default) )
     256                        $default = 'mystery';
     257                else
     258                        $default = $avatar_default;
     259        }
     260
     261        if ( !empty($email) )
     262                $email_hash = md5( strtolower( trim( $email ) ) );
     263
     264        if ( is_ssl() ) {
     265                $host = 'https://secure.gravatar.com';
     266        } else {
     267                if ( !empty($email) )
     268                        $host = sprintf( "http://%d.gravatar.com", ( hexdec( $email_hash[0] ) % 2 ) );
     269                else
     270                        $host = 'http://0.gravatar.com';
     271        }
     272
     273        if ( 'mystery' == $default )
     274                $default = "$host/avatar/ad516503a11cd5ca435acc9bb6523536?s={$size}"; // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com')
     275        elseif ( 'blank' == $default )
     276                $default = $email ? 'blank' : includes_url( 'images/blank.gif' );
     277        elseif ( !empty($email) && 'gravatar_default' == $default )
     278                $default = '';
     279        elseif ( 'gravatar_default' == $default )
     280                $default = "$host/avatar/?s={$size}";
     281        elseif ( empty($email) )
     282                $default = "$host/avatar/?d=$default&amp;s={$size}";
     283        elseif ( strpos($default, 'http://') === 0 )
     284                $default = add_query_arg( 's', $size, $default );
     285
     286        if ( !empty($email) ) {
     287                $out = "$host/avatar/";
     288                $out .= $email_hash;
     289                $out .= '?s='.$size;
     290                $out .= '&amp;d=' . urlencode( $default );
     291
     292                $rating = get_option('avatar_rating');
     293                if ( !empty( $rating ) )
     294                        $out .= "&amp;r={$rating}";
     295
     296                $out = str_replace( '&#038;', '&amp;', esc_url( $out ) );
     297                $avatar = "<img alt='{$safe_alt}' src='{$out}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
     298        } else {
     299                $out = esc_url( $default );
     300                $avatar = "<img alt='{$safe_alt}' src='{$out}' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />";
     301        }
     302
     303        /**
     304         * Filter the avatar to retrieve.
     305         *
     306         * @since 2.5.0
     307         *
     308         * @param string            $avatar      Image tag for the user's avatar.
     309         * @param int|object|string $id_or_email A user ID, email address, or comment object.
     310         * @param int               $size        Square avatar width and height in pixels to retrieve.
     311         * @param string            $alt         Alternative text to use in the avatar image tag.
     312         *                                       Default empty.
     313         */
     314        return apply_filters( 'get_avatar', $avatar, $id_or_email, $size, $default, $alt );
     315}
     316else:
     317        _deprecated_function( 'get_avatar', '4.2', 'wp_get_avatar()' );
     318endif;
     319 No newline at end of file
  • src/wp-includes/pluggable.php

     
    20802080}
    20812081endif;
    20822082
    2083 if ( !function_exists( 'get_avatar' ) ) :
    2084 /**
    2085  * Retrieve the avatar for a user who provided a user ID or email address.
    2086  *
    2087  * @since 2.5.0
    2088  *
    2089  * @param int|string|object $id_or_email A user ID,  email address, or comment object
    2090  * @param int $size Size of the avatar image
    2091  * @param string $default URL to a default image to use if no avatar is available
    2092  * @param string $alt Alternative text to use in image tag. Defaults to blank
    2093  * @return false|string `<img>` tag for the user's avatar.
    2094 */
    2095 function get_avatar( $id_or_email, $size = '96', $default = '', $alt = false ) {
    2096         if ( ! get_option('show_avatars') )
    2097                 return false;
    2098 
    2099         if ( false === $alt)
    2100                 $safe_alt = '';
    2101         else
    2102                 $safe_alt = esc_attr( $alt );
    2103 
    2104         if ( !is_numeric($size) )
    2105                 $size = '96';
    2106 
    2107         $email = '';
    2108         if ( is_numeric($id_or_email) ) {
    2109                 $id = (int) $id_or_email;
    2110                 $user = get_userdata($id);
    2111                 if ( $user )
    2112                         $email = $user->user_email;
    2113         } elseif ( is_object($id_or_email) ) {
    2114                 // No avatar for pingbacks or trackbacks
    2115 
    2116                 /**
    2117                  * Filter the list of allowed comment types for retrieving avatars.
    2118                  *
    2119                  * @since 3.0.0
    2120                  *
    2121                  * @param array $types An array of content types. Default only contains 'comment'.
    2122                  */
    2123                 $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
    2124                 if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) )
    2125                         return false;
    2126 
    2127                 if ( ! empty( $id_or_email->user_id ) ) {
    2128                         $id = (int) $id_or_email->user_id;
    2129                         $user = get_userdata($id);
    2130                         if ( $user )
    2131                                 $email = $user->user_email;
    2132                 }
    2133 
    2134                 if ( ! $email && ! empty( $id_or_email->comment_author_email ) )
    2135                         $email = $id_or_email->comment_author_email;
    2136         } else {
    2137                 $email = $id_or_email;
    2138         }
    2139 
    2140         if ( empty($default) ) {
    2141                 $avatar_default = get_option('avatar_default');
    2142                 if ( empty($avatar_default) )
    2143                         $default = 'mystery';
    2144                 else
    2145                         $default = $avatar_default;
    2146         }
    2147 
    2148         if ( !empty($email) )
    2149                 $email_hash = md5( strtolower( trim( $email ) ) );
    2150 
    2151         if ( is_ssl() ) {
    2152                 $host = 'https://secure.gravatar.com';
    2153         } else {
    2154                 if ( !empty($email) )
    2155                         $host = sprintf( "http://%d.gravatar.com", ( hexdec( $email_hash[0] ) % 2 ) );
    2156                 else
    2157                         $host = 'http://0.gravatar.com';
    2158         }
    2159 
    2160         if ( 'mystery' == $default )
    2161                 $default = "$host/avatar/ad516503a11cd5ca435acc9bb6523536?s={$size}"; // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com')
    2162         elseif ( 'blank' == $default )
    2163                 $default = $email ? 'blank' : includes_url( 'images/blank.gif' );
    2164         elseif ( !empty($email) && 'gravatar_default' == $default )
    2165                 $default = '';
    2166         elseif ( 'gravatar_default' == $default )
    2167                 $default = "$host/avatar/?s={$size}";
    2168         elseif ( empty($email) )
    2169                 $default = "$host/avatar/?d=$default&amp;s={$size}";
    2170         elseif ( strpos($default, 'http://') === 0 )
    2171                 $default = add_query_arg( 's', $size, $default );
    2172 
    2173         if ( !empty($email) ) {
    2174                 $out = "$host/avatar/";
    2175                 $out .= $email_hash;
    2176                 $out .= '?s='.$size;
    2177                 $out .= '&amp;d=' . urlencode( $default );
    2178 
    2179                 $rating = get_option('avatar_rating');
    2180                 if ( !empty( $rating ) )
    2181                         $out .= "&amp;r={$rating}";
    2182 
    2183                 $out = str_replace( '&#038;', '&amp;', esc_url( $out ) );
    2184                 $avatar = "<img alt='{$safe_alt}' src='{$out}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
    2185         } else {
    2186                 $out = esc_url( $default );
    2187                 $avatar = "<img alt='{$safe_alt}' src='{$out}' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />";
    2188         }
    2189 
    2190         /**
    2191          * Filter the avatar to retrieve.
    2192          *
    2193          * @since 2.5.0
    2194          *
    2195          * @param string            $avatar      Image tag for the user's avatar.
    2196          * @param int|object|string $id_or_email A user ID, email address, or comment object.
    2197          * @param int               $size        Square avatar width and height in pixels to retrieve.
    2198          * @param string            $alt         Alternative text to use in the avatar image tag.
    2199          *                                       Default empty.
    2200          */
    2201         return apply_filters( 'get_avatar', $avatar, $id_or_email, $size, $default, $alt );
    2202 }
    2203 endif;
    2204 
    22052083if ( !function_exists( 'wp_text_diff' ) ) :
    22062084/**
    22072085 * Displays a human readable HTML representation of the difference between two strings.
  • src/wp-includes/post-template.php

     
    16951695        /* translators: revision date format, see http://php.net/date */
    16961696        $datef = _x( 'j F, Y @ G:i:s', 'revision date format');
    16971697
    1698         $gravatar = get_avatar( $revision->post_author, 24 );
     1698        $gravatar = wp_get_avatar( $revision->post_author, 24 );
    16991699
    17001700        $date = date_i18n( $datef, strtotime( $revision->post_modified ) );
    17011701        if ( $link && current_user_can( 'edit_post', $revision->ID ) && $link = get_edit_post_link( $revision->ID ) )
  • tests/phpunit/tests/avatar.php

     
     1<?php
     2
     3/**
     4 * Test avatar related functions
     5 *
     6 * @group avatar
     7 */
     8class Tests_Avatar extends WP_UnitTestCase {
     9        /**
     10         * @ticket 21195
     11         */
     12        public function test_get_avatar_url_gravatar_url() {
     13                $url = wp_get_avatar_url( 1 );
     14                $this->assertEquals( preg_match( '|^http?://[0-9]+.gravatar.com/avatar/[0-9a-f]{32}\?|', $url ), 1 );
     15        }
     16
     17        /**
     18         * @ticket 21195
     19         */
     20        public function test_get_avatar_url_size() {
     21                $url = wp_get_avatar_url( 1 );
     22                $this->assertEquals( preg_match( '|\?.*s=96|', $url ), 1 );
     23
     24                $args = array( 'size' => 100 );
     25                $url = wp_get_avatar_url( 1, $args );
     26                $this->assertEquals( preg_match( '|\?.*s=100|', $url ), 1 );
     27        }
     28
     29        /**
     30         * @ticket 21195
     31         */
     32        public function test_get_avatar_url_default() {
     33                $url = wp_get_avatar_url( 1 );
     34                $this->assertEquals( preg_match( '|\?.*d=mm|', $url ), 1 );
     35
     36                $args = array( 'default' => 'wavatar' );
     37                $url = wp_get_avatar_url( 1, $args );
     38                $this->assertEquals( preg_match( '|\?.*d=wavatar|', $url ), 1 );
     39
     40                $this->assertEquals( preg_match( '|\?.*f=y|', $url ), 0 );
     41                $args = array( 'force_default' => true );
     42                $url = wp_get_avatar_url( 1, $args );
     43                $this->assertEquals( preg_match( '|\?.*f=y|', $url ), 1 );
     44        }
     45
     46        /**
     47         * @ticket 21195
     48         */
     49        public function test_get_avatar_url_rating() {
     50                $url = wp_get_avatar_url( 1 );
     51                $this->assertEquals( preg_match( '|\?.*r=g|', $url ), 1 );
     52
     53                $args = array( 'rating' => 'M' );
     54                $url = wp_get_avatar_url( 1, $args );
     55                $this->assertEquals( preg_match( '|\?.*r=m|', $url ), 1 );
     56        }
     57
     58        /**
     59         * @ticket 21195
     60         */
     61        public function test_get_avatar_url_scheme() {
     62                $url = wp_get_avatar_url( 1 );
     63                $this->assertEquals( preg_match( '|^http://|', $url ), 1 );
     64
     65                $args = array( 'scheme' => 'https' );
     66                $url = wp_get_avatar_url( 1, $args );
     67                $this->assertEquals( preg_match( '|^https://|', $url ), 1 );
     68
     69                $args = array( 'scheme' => 'lolcat' );
     70                $url = wp_get_avatar_url( 1, $args );
     71                $this->assertEquals( preg_match( '|^lolcat://|', $url ), 0 );
     72        }
     73
     74        /**
     75         * @ticket 21195
     76         */
     77        public function test_get_avatar_url_user() {
     78                $url = wp_get_avatar_url( 1 );
     79
     80                $url2 = wp_get_avatar_url( WP_TESTS_EMAIL );
     81                $this->assertEquals( $url, $url2 );
     82
     83                $url2 = wp_get_avatar_url( md5( WP_TESTS_EMAIL ) . '@md5.gravatar.com' );
     84                $this->assertEquals( $url, $url2 );
     85
     86                $user = get_user_by( 'id', 1 );
     87                $url2 = wp_get_avatar_url( $user );
     88                $this->assertEquals( $url, $url2 );
     89
     90                $post_id = $this->factory->post->create( array( 'post_author' => 1 ) );
     91                $post = get_post( $post_id );
     92                $url2 = wp_get_avatar_url( $post );
     93                $this->assertEquals( $url, $url2 );
     94
     95                $comment_id = $this->factory->comment->create( array( 'comment_post_ID' => $post_id, 'user_id' => 1 ) );
     96                $comment = get_comment( $comment_id );
     97                $url2 = wp_get_avatar_url( $comment );
     98                $this->assertEquals( $url, $url2 );
     99        }
     100
     101        /**
     102         * @ticket 21195
     103         */
     104        public function test_get_avatar_url_bad_id() {
     105                $this->assertFalse( wp_get_avatar_url( false ) );
     106                $this->assertFalse( wp_get_avatar_url( 100000 ) );
     107        }
     108
     109        protected $fakeURL;
     110        /**
     111         * @ticket 21195
     112         */
     113        public function test_pre_get_avatar_url_filter() {
     114                $this->fakeURL = 'haha wat';
     115
     116                add_filter( 'pre_get_avatar_data', array( $this, 'pre_get_avatar_url_filter' ), 10, 1 );
     117                $url = wp_get_avatar_url( 1 );
     118                remove_filter( 'pre_get_avatar_data', array( $this, 'pre_get_avatar_url_filter' ), 10 );
     119
     120                $this->assertEquals( $url, $this->fakeURL );
     121        }
     122        public function pre_get_avatar_url_filter( $args ) {
     123                $args['url'] = $this->fakeURL;
     124                return $args;
     125        }
     126
     127        /**
     128         * @ticket 21195
     129         */
     130        public function test_get_avatar_url_filter() {
     131                $this->fakeURL = 'omg lol';
     132
     133                add_filter( 'get_avatar_url', array( $this, 'get_avatar_url_filter' ), 10, 1 );
     134                $url = wp_get_avatar_url( 1 );
     135                remove_filter( 'get_avatar_url', array( $this, 'get_avatar_url_filter' ), 10 );
     136
     137                $this->assertEquals( $url, $this->fakeURL );
     138        }
     139        public function get_avatar_url_filter( $url ) {
     140                return $this->fakeURL;
     141        }
     142
     143        /**
     144         * @ticket 21195
     145         */
     146        public function test_get_avatar_comment_types_filter() {
     147                $url = wp_get_avatar_url( 1 );
     148
     149                $post_id = $this->factory->post->create( array( 'post_author' => 1 ) );
     150                $comment_id = $this->factory->comment->create( array( 'comment_post_ID' => $post_id, 'user_id' => 1, 'comment_type' => 'pingback' ) );
     151                $comment = get_comment( $comment_id );
     152
     153                $url2 = wp_get_avatar_url( $comment );
     154                $this->assertFalse( $url2 );
     155
     156                add_filter( 'get_avatar_comment_types', array( $this, 'get_avatar_comment_types_filter' ), 10, 1 );
     157                $url2 = wp_get_avatar_url( $comment );
     158                remove_filter( 'get_avatar_comment_types', array( $this, 'get_avatar_comment_types_filter' ), 10 );
     159
     160                $this->assertEquals( $url, $url2 );
     161        }
     162        public function get_avatar_comment_types_filter( $comment_types ) {
     163                $comment_types[] = 'pingback';
     164                return $comment_types;
     165        }
     166
     167        public function test_get_avatar() {
     168                $img = wp_get_avatar( 1 );
     169                $this->assertEquals( preg_match( '|^<img alt="[^"]*" src="[^"]*" class="[^"]*" height="[^"]*" width="[^"]*" />$|', $img ), 1 );
     170        }
     171
     172        public function test_get_avatar_size() {
     173                $size = '100';
     174                $img = wp_get_avatar( 1, $size );
     175                $this->assertEquals( preg_match( '|^<img .*height="' . $size . '".*width="' . $size . '"|', $img ), 1 );
     176        }
     177
     178        public function test_get_avatar_alt() {
     179                $alt = 'Mr Hyde';
     180                $img = wp_get_avatar( 1, 96, '', $alt );
     181                $this->assertEquals( preg_match( '|^<img alt="' . $alt . '"|', $img ), 1 );
     182        }
     183
     184        public function test_get_avatar_class() {
     185                $class = 'first';
     186                $img = wp_get_avatar( 1, 96, '', '', array( 'class' => $class ) );
     187                $this->assertEquals( preg_match( '|^<img .*class="[^"]*' . $class . '[^"]*"|', $img ), 1 );
     188        }
     189
     190        public function test_get_avatar_default_class() {
     191                $img = wp_get_avatar( 1, 96, '', '', array( 'force_default' => true ) );
     192                $this->assertEquals( preg_match( '|^<img .*class="[^"]*avatar-default[^"]*"|', $img ), 1 );
     193        }
     194
     195        public function test_get_avatar_force_display() {
     196                $old = get_option( 'show_avatars' );
     197                update_option( 'show_avatars', false );
     198
     199                $this->assertFalse( wp_get_avatar( 1 ) );
     200
     201                $this->assertNotEmpty( wp_get_avatar( 1, 96, '', '', array( 'force_display' => true ) ) );
     202
     203                update_option( 'show_avatars', $old );
     204        }
     205
     206
     207        protected $fakeIMG;
     208        /**
     209         * @ticket 21195
     210         */
     211        public function test_pre_get_avatar_filter() {
     212                $this->fakeIMG = 'YOU TOO?!';
     213
     214                add_filter( 'pre_get_avatar', array( $this, 'pre_get_avatar_filter' ), 10, 1 );
     215                $img = wp_get_avatar( 1 );
     216                remove_filter( 'pre_get_avatar', array( $this, 'pre_get_avatar_filter' ), 10 );
     217
     218                $this->assertEquals( $img, $this->fakeIMG );
     219        }
     220        public function pre_get_avatar_filter( $img ) {
     221                return $this->fakeIMG;
     222        }
     223
     224        /**
     225         * @ticket 21195
     226         */
     227        public function test_get_avatar_filter() {
     228                $this->fakeURL = 'YA RLY';
     229
     230                add_filter( 'get_avatar', array( $this, 'get_avatar_filter' ), 10, 1 );
     231                $img = wp_get_avatar( 1 );
     232                remove_filter( 'get_avatar', array( $this, 'get_avatar_filter' ), 10 );
     233
     234                $this->assertEquals( $img, $this->fakeURL );
     235        }
     236        public function get_avatar_filter( $img ) {
     237                return $this->fakeURL;
     238        }
     239
     240}