Make WordPress Core

Ticket #21195: 21195.13.diff

File 21195.13.diff, 27.1 KB (added by pento, 10 years ago)
  • src/wp-includes/link-template.php

     
    33243324                echo $before, $link, $after;
    33253325        }
    33263326}
     3327
     3328
     3329/**
     3330 * Retrieve the avatar URL.
     3331 *
     3332 * @since 4.2.0
     3333 *
     3334 * @param mixed $id_or_email The Gravatar to retrieve a URL for. Accepts a user_id, gravatar md5 hash,
     3335 *                           user email, WP_User object, WP_Post object, or comment object.
     3336 * @param array $args        {
     3337 *     Optional. Arguments to return instead of the default arguments.
     3338 *
     3339 *     @type int    $size           Height and width of the avatar in pixels. Default 96.
     3340 *     @type string $default        URL for the default image or a default type. Accepts '404' (return
     3341 *                                  a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),
     3342 *                                  'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',
     3343 *                                  or 'mysterman' (The Oyster Man), 'blank' (transparent GIF), or
     3344 *                                  'gravatar_default' (the Gravatar logo). Default is the value of the
     3345 *                                  'avatar_default' option, with a fallback of 'mystery'.
     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  $processed_args When the function returns, the value will be the processed/sanitized $args
     3352 *                                  plus a "found_avatar" guess. Pass as a reference. Default null.
     3353 * }
     3354 *
     3355 * @return false|string The URL of the avatar we found, or false if we couldn't find an avatar.
     3356 */
     3357function get_avatar_url( $id_or_email, $args = null ) {
     3358        $original_args = $args;
     3359
     3360        $args = get_avatar_data( $id_or_email, $args );
     3361
     3362        /**
     3363         * Filter the avatar URL.
     3364         *
     3365         * @since 4.2.0
     3366         *
     3367         * @param string            $url           The URL of the avatar.
     3368         * @param int|object|string $id_or_email   A user ID, email address, or comment object.
     3369         * @param array             $args          Arguments passed to get_avatar_data(), after processing.
     3370         * @param array             $original_args Original arguments passed to get_avatar_data().
     3371         */
     3372        return apply_filters( 'get_avatar_url', $args['url'], $id_or_email, $args, $original_args );
     3373}
     3374
     3375/**
     3376 * Retrieve default data about the avatar.
     3377 *
     3378 * @since 4.2.0
     3379 *
     3380 * @param mixed $id_or_email The Gravatar to check the data against. Accepts a user_id, gravatar md5 hash,
     3381 *                           user email, WP_User object, WP_Post object, or comment object.
     3382 * @param array $args        {
     3383 *     Optional. Arguments to return instead of the default arguments.
     3384 *
     3385 *     @type int    $size           Height and width of the avatar in pixels. Default 96.
     3386 *     @type string $default        URL for the default image or a default type. Accepts '404' (return
     3387 *                                  a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster),
     3388 *                                  'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm',
     3389 *                                  or 'mysterman' (The Oyster Man), 'blank' (transparent GIF), or
     3390 *                                  'gravatar_default' (the Gravatar logo). Default is the value of the
     3391 *                                  'avatar_default' option, with a fallback of 'mystery'.
     3392 *     @type bool   $force_default  Whether to always show the default image, never the Gravatar. Default false.
     3393 *     @type string $rating         What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
     3394 *                                  judged in that order. Default is the value of the 'avatar_rating' option.
     3395 *     @type string $scheme         URL scheme to use. See {@see set_url_scheme()} for accepted values.
     3396 *                                  Default null.
     3397 *     @type array  $processed_args When the function returns, the value will be the processed/sanitized $args
     3398 *                                  plus a "found_avatar" guess. Pass as a reference. Default null.
     3399 * }
     3400 *
     3401 * @return array $processed_args {
     3402 *     Along with the arguments passed in $args, this will contain a couple of extra arguments.
     3403 *
     3404 *     @type bool         $found_avatar True if we were able to find an avatar for this user,
     3405 *                                      false or not set if we couldn't.
     3406 *     @type false|string $url          The URL of the avatar we found, or false if we couldn't find an avatar.
     3407 * }
     3408 */
     3409function get_avatar_data( $id_or_email, $args = null ) {
     3410        $original_args = $args;
     3411
     3412        $args = wp_parse_args( $args, array(
     3413                'size'           => 96,
     3414                'default'        => get_option( 'avatar_default', 'mystery' ),
     3415                'force_default'  => false,
     3416                'rating'         => get_option( 'avatar_rating' ),
     3417                'scheme'         => null,
     3418                'processed_args' => null, // if used, should be a reference
     3419        ) );
     3420
     3421        if ( is_numeric( $args['size'] ) ) {
     3422                $args['size'] = absint( $args['size'] );
     3423                if ( ! $args['size'] ) {
     3424                        $args['size'] = 96;
     3425                }
     3426        } else {
     3427                $args['size'] = 96;
     3428        }
     3429
     3430        if ( empty( $args['default'] ) ) {
     3431                $args['default'] = 'mystery';
     3432        }
     3433
     3434        switch ( $args['default'] ) {
     3435                case 'mm' :
     3436                case 'mystery' :
     3437                case 'mysteryman' :
     3438                        $args['default'] = 'mm';
     3439                        break;
     3440                case 'gravatar_default' :
     3441                        $args['default'] = false;
     3442                        break;
     3443        }
     3444
     3445        $args['force_default'] = (bool) $args['force_default'];
     3446
     3447        $args['rating'] = strtolower( $args['rating'] );
     3448
     3449        $args['found_avatar'] = false;
     3450
     3451        // Just in case we're passed the array version of the data objects
     3452        if ( is_array( $id_or_email ) ) {
     3453                $id_or_email = (object) $id_or_email;
     3454        }
     3455
     3456        /**
     3457         * Filter whether to retrieve the avatar URL early.
     3458         *
     3459         * Passing a non-null value in the 'url' member of the return array will
     3460         * effectively short circuit {@see get_avatar_data()}, passing the value
     3461         * through the 'get_avatar_data' filter and returning early.
     3462         *
     3463         * @since 4.2.0
     3464         *
     3465         * @param array             $args          Arguments passed to get_avatar_data(), after processing.
     3466         * @param int|object|string $id_or_email   A user ID, email address, or comment object.
     3467         * @param array             $original_args Original arguments passed to get_avatar_data().
     3468         */
     3469        $args = apply_filters( 'pre_get_avatar_data', $args, $id_or_email, $original_args );
     3470
     3471        if ( isset( $args['url'] ) && ! is_null( $args['url'] ) ) {
     3472                /** This filter is documented in src/wp-includes/link-template.php */
     3473                return apply_filters( 'get_avatar_data', $args, $id_or_email, $original_args );
     3474        }
     3475
     3476        $email_hash = '';
     3477        $user = $email = false;
     3478
     3479        // Process the user identifier.
     3480        if ( is_numeric( $id_or_email ) ) {
     3481                $user = get_user_by( 'id', absint( $id_or_email ) );
     3482        } elseif ( is_string( $id_or_email ) ) {
     3483                if ( strpos( $id_or_email, '@md5.gravatar.com' ) ) {
     3484                        // md5 hash
     3485                        list( $email_hash ) = explode( '@', $id_or_email );
     3486                } else {
     3487                        // email address
     3488                        $email = $id_or_email;
     3489                }
     3490        } elseif ( is_object( $id_or_email ) ) {
     3491                if ( isset( $id_or_email->comment_ID ) ) {
     3492                        // Comment Object
     3493
     3494                        /**
     3495                         * Filter the list of allowed comment types for retrieving avatars.
     3496                         *
     3497                         * @since 3.0.0
     3498                         *
     3499                         * @param array $types An array of content types. Default only contains 'comment'.
     3500                         */
     3501                        $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
     3502                        if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) {
     3503                                $args['url'] = false;
     3504                                /** This filter is documented in src/wp-includes/link-template.php */
     3505                                return apply_filters( 'get_avatar_data', $args, $id_or_email, $original_args );
     3506                        }
     3507
     3508                        if ( ! empty( $id_or_email->user_id ) ) {
     3509                                $user = get_user_by( 'id', (int) $id_or_email->user_id );
     3510                        }
     3511                        if ( ( ! $user || is_wp_error( $user ) ) && ! empty( $id_or_email->comment_author_email ) ) {
     3512                                $email = $id_or_email->comment_author_email;
     3513                        }
     3514                } elseif ( ! empty( $id_or_email->user_login ) ) {
     3515                        // User Object
     3516                        $user = $id_or_email;
     3517                } elseif ( ! empty( $id_or_email->post_author ) ) {
     3518                        // Post Object
     3519                        $user = get_user_by( 'id', (int) $id_or_email->post_author );
     3520                }
     3521        }
     3522
     3523        if ( ! $email_hash ) {
     3524                if ( $user ) {
     3525                        $email = $user->user_email;
     3526                }
     3527
     3528                if ( $email ) {
     3529                        $email_hash = md5( strtolower( trim( $email ) ) );
     3530                }
     3531        }
     3532
     3533        if ( $email_hash ) {
     3534                $args['found_avatar'] = true;
     3535                $gravatar_server = hexdec( $email_hash[0] ) % 3;
     3536        } else {
     3537                $gravatar_server = rand( 0, 2 );
     3538        }
     3539
     3540        $url_args = array(
     3541                's' => $args['size'],
     3542                'd' => $args['default'],
     3543                'f' => $args['force_default'] ? 'y' : false,
     3544                'r' => $args['rating'],
     3545        );
     3546
     3547        $url = sprintf( 'http://%d.gravatar.com/avatar/%s', $gravatar_server, $email_hash );
     3548
     3549        $url = add_query_arg(
     3550                rawurlencode_deep( array_filter( $url_args ) ),
     3551                set_url_scheme( $url, $args['scheme'] )
     3552        );
     3553
     3554        $args['url'] = $url;
     3555
     3556        /**
     3557         * Filter the avatar data.
     3558         *
     3559         * @since 4.2.0
     3560         *
     3561         * @param array             $args          Arguments passed to get_avatar_data(), after processing.
     3562         * @param int|object|string $id_or_email   A user ID, email address, or comment object.
     3563         * @param array             $original_args Original arguments passed to get_avatar_data().
     3564         */
     3565        $args = apply_filters( 'get_avatar_data', $args, $id_or_email, $original_args );
     3566
     3567        // Don't return a broken URL if we couldn't find the email hash, and none of the filters returned a different URL.
     3568        if ( ! $email_hash && $url === $args['url'] ) {
     3569                $args['url'] = false;
     3570        }
     3571
     3572        return $args;
     3573}
  • src/wp-includes/pluggable.php

     
    20822082
    20832083if ( !function_exists( 'get_avatar' ) ) :
    20842084/**
    2085  * Retrieve the avatar for a user who provided a user ID or email address.
     2085 * Retrieve the avatar `<img>` tag for a user, email address, MD5 hash, comment, or post.
    20862086 *
    20872087 * @since 2.5.0
     2088 * @since 4.2.0 Optional $args parameter added.
    20882089 *
    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;
     2090 * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash,
     2091 *                           user email, WP_User object, WP_Post object, or comment object.
     2092 * @param int    $size       Optional. Height and width of the avatar in pixels. Default 96.
     2093 * @param string $default    Optional. URL for the default image or a default type. Accepts '404'
     2094 *                           (return a 404 instead of a default image), 'retro' (8bit), 'monsterid'
     2095 *                           (monster), 'wavatar' (cartoon face), 'indenticon' (the "quilt"),
     2096 *                           'mystery', 'mm', or 'mysterman' (The Oyster Man), 'blank' (transparent GIF),
     2097 *                           or 'gravatar_default' (the Gravatar logo). Default is the value of the
     2098 *                           'avatar_default' option, with a fallback of 'mystery'.
     2099 * @param string $alt        Optional. Alternative text to use in &lt;img&gt; tag. Default empty.
     2100 * @param array  $args       {
     2101 *     Optional. Extra arguments to retrieve the avatar.
     2102 *
     2103 *     @type bool         $force_default Whether to always show the default image, never the Gravatar. Default false.
     2104 *     @type string       $rating        What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are
     2105 *                                       judged in that order. Default is the value of the 'avatar_rating' option.
     2106 *     @type string       $scheme        URL scheme to use. See {@see set_url_scheme()} for accepted values.
     2107 *                                       Default null.
     2108 *     @type array|string $class         Array or string of additional classes to add to the &lt;img&gt; element.
     2109 *                                       Default null.
     2110 *     @type bool         $force_display Whether to always show the avatar - ignores the show_avatars option.
     2111 *                                       Default false.
     2112 * }
     2113 *
     2114 * @return false|string `<img>` tag for the user's avatar. False on failure.
     2115 */
     2116function get_avatar( $id_or_email, $size = 96, $default = '', $alt = '', $args = null ) {
     2117        $defaults = array(
     2118                // get_avatar_data() args.
     2119                'size'          => 96,
     2120                'default'       => get_option( 'avatar_default', 'mystery' ),
     2121                'force_default' => false,
     2122                'rating'        => get_option( 'avatar_rating' ),
     2123                'scheme'        => null,
     2124                'alt'           => '',
     2125                'class'         => null,
     2126                'force_display' => false,
     2127        );
    20982128
    2099         if ( false === $alt)
    2100                 $safe_alt = '';
    2101         else
    2102                 $safe_alt = esc_attr( $alt );
     2129        if ( empty( $args ) ) {
     2130                $args = array();
     2131        }
    21032132
    2104         if ( !is_numeric($size) )
    2105                 $size = '96';
     2133        $args['size']    = $size;
     2134        $args['default'] = $default;
     2135        $args['alt']     = $alt;
    21062136
    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
     2137        $original_args = $args;
    21152138
    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;
     2139        $args = wp_parse_args( $args, $defaults );
    21262140
    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;
     2141        /**
     2142         * Filter whether to retrieve the avatar URL early.
     2143         *
     2144         * Passing a non-null value will effectively short-circuit {@see get_avatar()},
     2145         * passing the value through the 'pre_get_avatar' filter and returning early.
     2146         *
     2147         * @since 4.2.0
     2148         *
     2149         * @param string            $avatar        HTML for the user's avatar. Default null.
     2150         * @param int|object|string $id_or_email   A user ID, email address, or comment object.
     2151         * @param array             $args          Arguments passed to get_avatar_url(), after processing.
     2152         * @param array             $original_args Original arguments passed to get_avatar_url().
     2153         */
     2154        $avatar = apply_filters( 'pre_get_avatar', null, $id_or_email, $args, $original_args );
     2155        if ( ! is_null( $avatar ) ) {
     2156                /** This filter is documented in src/wp-include/pluggable.php */
     2157                return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $args, $original_args );
    21382158        }
    21392159
    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;
     2160        if ( ! $args['force_display'] && ! get_option( 'show_avatars' ) ) {
     2161                return false;
    21462162        }
    21472163
    2148         if ( !empty($email) )
    2149                 $email_hash = md5( strtolower( trim( $email ) ) );
     2164        $args = get_avatar_data( $id_or_email, $args );
    21502165
    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';
     2166        $url = $args['url'];
     2167
     2168        if ( ! $url || is_wp_error( $url ) ) {
     2169        return false;
    21582170        }
    21592171
    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        $class = array( 'avatar', 'avatar-' . (int) $args['size'], 'photo' );
    21722173
    2173         if ( !empty($email) ) {
    2174                 $out = "$host/avatar/";
    2175                 $out .= $email_hash;
    2176                 $out .= '?s='.$size;
    2177                 $out .= '&amp;d=' . urlencode( $default );
     2174        if ( ! $args['found_avatar'] || $args['force_default'] ) {
     2175        $class[] = 'avatar-default';
     2176        }
    21782177
    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}' />";
     2178        if ( $args['class'] ) {
     2179                if ( is_array( $args['class'] ) ) {
     2180                        $class = array_merge( $class, $args['class'] );
     2181                } else {
     2182                        $class[] = $args['class'];
     2183                }
    21882184        }
    21892185
     2186        $avatar = sprintf(
     2187                '<img alt="%s" src="%s" class="%s" height="%d" width="%d" />',
     2188                esc_attr( $args['alt'] ),
     2189                esc_url( $url ),
     2190                esc_attr( join( ' ', $class ) ),
     2191                (int) $args['size'],
     2192                (int) $args['size']
     2193        );
     2194
    21902195        /**
    21912196         * Filter the avatar to retrieve.
    21922197         *
    21932198         * @since 2.5.0
     2199         * @since 4.2.0 $args and $original_args parameters added
    21942200         *
    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.
     2201         * @param string            $avatar         &lt;img&gt; tag for the user's avatar.
     2202         * @param int|object|string $id_or_email    A user ID, email address, or comment object.
     2203         * @param int               $size           Square avatar width and height in pixels to retrieve.
     2204         * @param string            $alt            Alternative text to use in the avatar image tag.
     2205         *                                          Default empty.
     2206         * @param array             $args           Arguments passed to get_avatar_data(), after processing.
     2207         * @param array             $original_args  Original arguments passed to get_avatar_data().
    22002208         */
    2201         return apply_filters( 'get_avatar', $avatar, $id_or_email, $size, $default, $alt );
     2209        return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $args, $original_args );
    22022210}
    22032211endif;
    22042212
  • 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 = 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 = get_avatar_url( 1 );
     22                $this->assertEquals( preg_match( '|\?.*s=96|', $url ), 1 );
     23
     24                $args = array( 'size' => 100 );
     25                $url = 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 = get_avatar_url( 1 );
     34                $this->assertEquals( preg_match( '|\?.*d=mm|', $url ), 1 );
     35
     36                $args = array( 'default' => 'wavatar' );
     37                $url = 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 = 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 = get_avatar_url( 1 );
     51                $this->assertEquals( preg_match( '|\?.*r=g|', $url ), 1 );
     52
     53                $args = array( 'rating' => 'M' );
     54                $url = 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 = get_avatar_url( 1 );
     63                $this->assertEquals( preg_match( '|^http://|', $url ), 1 );
     64
     65                $args = array( 'scheme' => 'https' );
     66                $url = get_avatar_url( 1, $args );
     67                $this->assertEquals( preg_match( '|^https://|', $url ), 1 );
     68
     69                $args = array( 'scheme' => 'lolcat' );
     70                $url = 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 = get_avatar_url( 1 );
     79
     80                $url2 = get_avatar_url( WP_TESTS_EMAIL );
     81                $this->assertEquals( $url, $url2 );
     82
     83                $url2 = 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 = 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 = 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 = get_avatar_url( $comment );
     98                $this->assertEquals( $url, $url2 );
     99        }
     100
     101        /**
     102         * @ticket 21195
     103         */
     104        public function test_get_avatar_url_array() {
     105                $url = get_avatar_url( 1 );
     106
     107                $post_id = $this->factory->post->create( array( 'post_author' => 1 ) );
     108                $post = get_post( $post_id, ARRAY_A );
     109                $url2 = get_avatar_url( $post );
     110                $this->assertEquals( $url, $url2 );
     111        }
     112
     113        /**
     114         * @ticket 21195
     115         */
     116        public function test_get_avatar_url_bad_id() {
     117                $this->assertFalse( get_avatar_url( false ) );
     118                $this->assertFalse( get_avatar_url( 100000 ) );
     119        }
     120
     121        protected $fakeURL;
     122        /**
     123         * @ticket 21195
     124         */
     125        public function test_pre_get_avatar_url_filter() {
     126                $this->fakeURL = 'haha wat';
     127
     128                add_filter( 'pre_get_avatar_data', array( $this, 'pre_get_avatar_url_filter' ), 10, 1 );
     129                $url = get_avatar_url( 1 );
     130                remove_filter( 'pre_get_avatar_data', array( $this, 'pre_get_avatar_url_filter' ), 10 );
     131
     132                $this->assertEquals( $url, $this->fakeURL );
     133        }
     134        public function pre_get_avatar_url_filter( $args ) {
     135                $args['url'] = $this->fakeURL;
     136                return $args;
     137        }
     138
     139        /**
     140         * @ticket 21195
     141         */
     142        public function test_get_avatar_url_filter() {
     143                $this->fakeURL = 'omg lol';
     144
     145                add_filter( 'get_avatar_url', array( $this, 'get_avatar_url_filter' ), 10, 1 );
     146                $url = get_avatar_url( 1 );
     147                remove_filter( 'get_avatar_url', array( $this, 'get_avatar_url_filter' ), 10 );
     148
     149                $this->assertEquals( $url, $this->fakeURL );
     150        }
     151        public function get_avatar_url_filter( $url ) {
     152                return $this->fakeURL;
     153        }
     154
     155        /**
     156         * @ticket 21195
     157         */
     158        public function test_get_avatar_comment_types_filter() {
     159                $url = get_avatar_url( 1 );
     160
     161                $post_id = $this->factory->post->create( array( 'post_author' => 1 ) );
     162                $comment_id = $this->factory->comment->create( array( 'comment_post_ID' => $post_id, 'user_id' => 1, 'comment_type' => 'pingback' ) );
     163                $comment = get_comment( $comment_id );
     164
     165                $url2 = get_avatar_url( $comment );
     166                $this->assertFalse( $url2 );
     167
     168                add_filter( 'get_avatar_comment_types', array( $this, 'get_avatar_comment_types_filter' ), 10, 1 );
     169                $url2 = get_avatar_url( $comment );
     170                remove_filter( 'get_avatar_comment_types', array( $this, 'get_avatar_comment_types_filter' ), 10 );
     171
     172                $this->assertEquals( $url, $url2 );
     173        }
     174        public function get_avatar_comment_types_filter( $comment_types ) {
     175                $comment_types[] = 'pingback';
     176                return $comment_types;
     177        }
     178
     179        public function test_get_avatar() {
     180                $img = get_avatar( 1 );
     181                $this->assertEquals( preg_match( '|^<img alt="[^"]*" src="[^"]*" class="[^"]*" height="[^"]*" width="[^"]*" />$|', $img ), 1 );
     182        }
     183
     184        public function test_get_avatar_size() {
     185                $size = '100';
     186                $img = get_avatar( 1, $size );
     187                $this->assertEquals( preg_match( '|^<img .*height="' . $size . '".*width="' . $size . '"|', $img ), 1 );
     188        }
     189
     190        public function test_get_avatar_alt() {
     191                $alt = 'Mr Hyde';
     192                $img = get_avatar( 1, 96, '', $alt );
     193                $this->assertEquals( preg_match( '|^<img alt="' . $alt . '"|', $img ), 1 );
     194        }
     195
     196        public function test_get_avatar_class() {
     197                $class = 'first';
     198                $img = get_avatar( 1, 96, '', '', array( 'class' => $class ) );
     199                $this->assertEquals( preg_match( '|^<img .*class="[^"]*' . $class . '[^"]*"|', $img ), 1 );
     200        }
     201
     202        public function test_get_avatar_default_class() {
     203                $img = get_avatar( 1, 96, '', '', array( 'force_default' => true ) );
     204                $this->assertEquals( preg_match( '|^<img .*class="[^"]*avatar-default[^"]*"|', $img ), 1 );
     205        }
     206
     207        public function test_get_avatar_force_display() {
     208                $old = get_option( 'show_avatars' );
     209                update_option( 'show_avatars', false );
     210
     211                $this->assertFalse( get_avatar( 1 ) );
     212
     213                $this->assertNotEmpty( get_avatar( 1, 96, '', '', array( 'force_display' => true ) ) );
     214
     215                update_option( 'show_avatars', $old );
     216        }
     217
     218
     219        protected $fakeIMG;
     220        /**
     221         * @ticket 21195
     222         */
     223        public function test_pre_get_avatar_filter() {
     224                $this->fakeIMG = 'YOU TOO?!';
     225
     226                add_filter( 'pre_get_avatar', array( $this, 'pre_get_avatar_filter' ), 10, 1 );
     227                $img = get_avatar( 1 );
     228                remove_filter( 'pre_get_avatar', array( $this, 'pre_get_avatar_filter' ), 10 );
     229
     230                $this->assertEquals( $img, $this->fakeIMG );
     231        }
     232        public function pre_get_avatar_filter( $img ) {
     233                return $this->fakeIMG;
     234        }
     235
     236        /**
     237         * @ticket 21195
     238         */
     239        public function test_get_avatar_filter() {
     240                $this->fakeURL = 'YA RLY';
     241
     242                add_filter( 'get_avatar', array( $this, 'get_avatar_filter' ), 10, 1 );
     243                $img = get_avatar( 1 );
     244                remove_filter( 'get_avatar', array( $this, 'get_avatar_filter' ), 10 );
     245
     246                $this->assertEquals( $img, $this->fakeURL );
     247        }
     248        public function get_avatar_filter( $img ) {
     249                return $this->fakeURL;
     250        }
     251
     252}