Ticket #21195: 21195.12.diff
File 21195.12.diff, 24.9 KB (added by , 10 years ago) |
---|
-
src/wp-includes/link-template.php
3324 3324 echo $before, $link, $after; 3325 3325 } 3326 3326 } 3327 3328 /** 3329 * Retrieve the avatar URL 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 array $args { 3336 * Optional. Extra arguments to retrieve the avatar. 3337 * 3338 * @type int $size Height and width of the avatar in pixels. Default 96. 3339 * @type string $default URL for the default image or a default type. Accepts '404' (return 3340 * a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster), 3341 * 'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm', 3342 * or 'mysterman' (The Oyster Man), 'blank' (transparent GIF), or 3343 * 'gravatar_default' (the Gravatar logo). Default is the value of the 3344 * 'avatar_default' option, with a fallback of 'mystery'. 3345 * @type bool $force_default Whether to always show the default image, never the Gravatar. Default false. 3346 * @type string $rating What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are 3347 * judged in that order. Default is the value of the 'avatar_rating' option. 3348 * @type string $scheme URL scheme to use. See {@see set_url_scheme()} for accepted values. 3349 * Default null. 3350 * @type array $processed_args When the function returns, the value will be the processed/sanitized $args 3351 * plus a "found_avatar" guess. Pass as a reference. Default null. 3352 * } 3353 * 3354 * @return bool|string Avatar URL, or false on failure. 3355 */ 3356 function get_avatar_url( $id_or_email, &$args = null ) { 3357 $original_args = $args; 3358 3359 $args = wp_parse_args( $args, array( 3360 'size' => 96, 3361 'default' => get_option( 'avatar_default', 'mystery' ), 3362 'force_default' => false, 3363 'rating' => get_option( 'avatar_rating' ), 3364 'scheme' => null, 3365 'processed_args' => null, // if used, should be a reference 3366 ) ); 3367 3368 if ( is_numeric( $args['size'] ) ) { 3369 $args['size'] = absint( $args['size'] ); 3370 if ( ! $args['size'] ) { 3371 $args['size'] = 96; 3372 } 3373 } else { 3374 $args['size'] = 96; 3375 } 3376 3377 if ( empty( $args['default'] ) ) { 3378 $args['default'] = 'mystery'; 3379 } 3380 3381 switch ( $args['default'] ) { 3382 case 'mm' : 3383 case 'mystery' : 3384 case 'mysteryman' : 3385 $args['default'] = 'mm'; 3386 break; 3387 case 'gravatar_default' : 3388 $args['default'] = false; 3389 break; 3390 } 3391 3392 $args['force_default'] = (bool) $args['force_default']; 3393 3394 $args['rating'] = strtolower( $args['rating'] ); 3395 3396 $args['found_avatar'] = false; 3397 3398 // Just in case we're passed the array version of the data objects 3399 if ( is_array( $id_or_email ) ) { 3400 $id_or_email = (object) $id_or_email; 3401 } 3402 3403 /** 3404 * Filter whether to retrieve the avatar URL early. 3405 * 3406 * Passing a non-null value will effectively short-circuit {@see get_avatar_url()}, 3407 * passing the value through the 'get_avatar_url' filter and returning early. 3408 * 3409 * @since 4.2.0 3410 * 3411 * @param string $url URL for the user's avatar. Default null. 3412 * @param int|object|string $id_or_email A user ID, email address, or comment object. 3413 * @param array $args Arguments passed to get_avatar_url(), after processing. 3414 * @param array $original_args Original arguments passed to get_avatar_url(). 3415 */ 3416 $url = apply_filters( 'pre_get_avatar_url', null, $id_or_email, $args, $original_args ); 3417 3418 if ( ! is_null( $url ) ) { 3419 /** This filter is documented in src/wp-includes/link-template.php */ 3420 $return = apply_filters( 'get_avatar_url', $url, $id_or_email, $args, $original_args ); 3421 $args['processed_args'] = $args; 3422 unset( $args['processed_args']['processed_args'] ); 3423 return $return; 3424 } 3425 3426 $email_hash = ''; 3427 $user = $email = false; 3428 3429 // Process the user identifier. 3430 if ( is_numeric( $id_or_email ) ) { 3431 $user = get_user_by( 'id', absint( $id_or_email ) ); 3432 } elseif ( is_string( $id_or_email ) ) { 3433 if ( strpos( $id_or_email, '@md5.gravatar.com' ) ) { 3434 // md5 hash 3435 list( $email_hash ) = explode( '@', $id_or_email ); 3436 } else { 3437 // email address 3438 $email = $id_or_email; 3439 } 3440 } elseif ( is_object( $id_or_email ) ) { 3441 if ( isset( $id_or_email->comment_ID ) ) { 3442 // Comment Object 3443 3444 /** 3445 * Filter the list of allowed comment types for retrieving avatars. 3446 * 3447 * @since 3.0.0 3448 * 3449 * @param array $types An array of content types. Default only contains 'comment'. 3450 */ 3451 $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) ); 3452 if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) { 3453 $args['processed_args'] = $args; 3454 unset( $args['processed_args']['processed_args'] ); 3455 return false; 3456 } 3457 3458 if ( ! empty( $id_or_email->user_id ) ) { 3459 $user = get_user_by( 'id', (int) $id_or_email->user_id ); 3460 } 3461 if ( ( ! $user || is_wp_error( $user ) ) && ! empty( $id_or_email->comment_author_email ) ) { 3462 $email = $id_or_email->comment_author_email; 3463 } 3464 } elseif ( ! empty( $id_or_email->user_login ) ) { 3465 // User Object 3466 $user = $id_or_email; 3467 } elseif ( ! empty( $id_or_email->post_author ) ) { 3468 // Post Object 3469 $user = get_user_by( 'id', (int) $id_or_email->post_author ); 3470 } 3471 } 3472 3473 if ( ! $email_hash ) { 3474 if ( $user ) { 3475 $email = $user->user_email; 3476 } 3477 3478 if ( $email ) { 3479 $email_hash = md5( strtolower( trim( $email ) ) ); 3480 } 3481 } 3482 3483 if ( $email_hash ) { 3484 $args['found_avatar'] = true; 3485 $gravatar_server = hexdec( $email_hash[0] ) % 3; 3486 } else { 3487 $gravatar_server = rand( 0, 2 ); 3488 } 3489 3490 $url_args = array( 3491 's' => $args['size'], 3492 'd' => $args['default'], 3493 'f' => $args['force_default'] ? 'y' : false, 3494 'r' => $args['rating'], 3495 ); 3496 3497 $url = sprintf( 'http://%d.gravatar.com/avatar/%s', $gravatar_server, $email_hash ); 3498 3499 $url = add_query_arg( 3500 rawurlencode_deep( array_filter( $url_args ) ), 3501 set_url_scheme( $url, $args['scheme'] ) 3502 ); 3503 3504 /** 3505 * Filter the avatar to retrieve. 3506 * 3507 * @since 4.2.0 3508 * 3509 * @param string $url URL for the user's avatar. 3510 * @param int|object|string $id_or_email A user ID, email address, or comment object. 3511 * @param array $args Arguments passed to get_avatar_url(), after processing. 3512 * @param array $original_args Original arguments passed to get_avatar_url(). 3513 */ 3514 $return = apply_filters( 'get_avatar_url', $url, $id_or_email, $args, $original_args ); 3515 3516 $args['processed_args'] = $args; 3517 unset( $args['processed_args']['processed_args'] ); 3518 3519 // Don't return a broken URL if we couldn't find the email hash, and none of the filters returned a different URL. 3520 if ( ! $email_hash && $url === $return ) { 3521 return false; 3522 } 3523 3524 return $return; 3525 } -
src/wp-includes/pluggable.php
2082 2082 2083 2083 if ( !function_exists( 'get_avatar' ) ) : 2084 2084 /** 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. 2086 2086 * 2087 2087 * @since 2.5.0 2088 * @since 4.2.0 Optional $args parameter added. 2088 2089 * 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 <img> 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 <img> 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 */ 2116 function get_avatar( $id_or_email, $size = 96, $default = '', $alt = '', $args = null ) { 2117 $defaults = array( 2118 // get_avatar_url() 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 ); 2098 2128 2099 if ( false === $alt) 2100 $safe_alt = ''; 2101 else 2102 $safe_alt = esc_attr( $alt ); 2129 if ( empty( $args ) ) { 2130 $args = array(); 2131 } 2103 2132 2104 if ( !is_numeric($size) ) 2105 $size = '96'; 2133 $args['size'] = $size; 2134 $args['default'] = $default; 2135 $args['alt'] = $alt; 2106 2136 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; 2115 2138 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 ); 2126 2140 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 ); 2138 2158 } 2139 2159 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; 2146 2162 } 2147 2163 2148 if ( !empty($email) )2149 $email_hash = md5( strtolower( trim( $email ) ) );2164 $processed_args = null; 2165 $args['processed_args'] =& $processed_args; 2150 2166 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'; 2167 $url = get_avatar_url( $id_or_email, $args ); 2168 2169 if ( ! $url || is_wp_error( $url ) ) { 2170 return false; 2158 2171 } 2159 2172 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&s={$size}"; 2170 elseif ( strpos($default, 'http://') === 0 ) 2171 $default = add_query_arg( 's', $size, $default ); 2173 $class = array( 'avatar', 'avatar-' . (int) $processed_args['size'], 'photo' ); 2172 2174 2173 if ( !empty($email) ) { 2174 $out = "$host/avatar/"; 2175 $out .= $email_hash; 2176 $out .= '?s='.$size; 2177 $out .= '&d=' . urlencode( $default ); 2175 if ( ! $processed_args['found_avatar'] || $processed_args['force_default'] ) { 2176 $class[] = 'avatar-default'; 2177 } 2178 2178 2179 $rating = get_option('avatar_rating'); 2180 if ( !empty( $rating ) ) 2181 $out .= "&r={$rating}"; 2182 2183 $out = str_replace( '&', '&', 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}' />"; 2179 if ( $args['class'] ) { 2180 if ( is_array( $args['class'] ) ) { 2181 $class = array_merge( $class, $args['class'] ); 2182 } else { 2183 $class[] = $args['class']; 2184 } 2188 2185 } 2189 2186 2187 $avatar = sprintf( 2188 '<img alt="%s" src="%s" class="%s" height="%d" width="%d" />', 2189 esc_attr( $processed_args['alt'] ), 2190 esc_url( $url ), 2191 esc_attr( join( ' ', $class ) ), 2192 (int) $processed_args['size'], 2193 (int) $processed_args['size'] 2194 ); 2195 2190 2196 /** 2191 2197 * Filter the avatar to retrieve. 2192 2198 * 2193 2199 * @since 2.5.0 2200 * @since 4.2.0 $processed_args and $original_args parameters added 2194 2201 * 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. 2202 * @param string $avatar <img> tag for the user's avatar. 2203 * @param int|object|string $id_or_email A user ID, email address, or comment object. 2204 * @param int $size Square avatar width and height in pixels to retrieve. 2205 * @param string $alt Alternative text to use in the avatar image tag. 2206 * Default empty. 2207 * @param array $processed_args Arguments passed to get_avatar_url(), after processing. 2208 * @param array $original_args Original arguments passed to get_avatar_url(). 2200 2209 */ 2201 return apply_filters( 'get_avatar', $avatar, $id_or_email, $ size, $default, $alt);2210 return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $processed_args, $original_args ); 2202 2211 } 2203 2212 endif; 2204 2213 -
tests/phpunit/tests/avatar.php
1 <?php 2 3 /** 4 * Test avatar related functions 5 * 6 * @group avatar 7 */ 8 class 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_processed_args() { 117 $args = array( 's' => 100 ); 118 $url = get_avatar_url( 1, $args ); 119 $this->assertTrue( isset( $args['processed_args'] ) ); 120 $this->assertFalse( isset( $args['processed_args']['processed_args'] ) ); 121 } 122 123 /** 124 * @ticket 21195 125 */ 126 public function test_get_avatar_url_bad_id() { 127 $this->assertFalse( get_avatar_url( false ) ); 128 $this->assertFalse( get_avatar_url( 100000 ) ); 129 } 130 131 protected $fakeURL; 132 /** 133 * @ticket 21195 134 */ 135 public function test_pre_get_avatar_url_filter() { 136 $this->fakeURL = 'haha wat'; 137 138 add_filter( 'pre_get_avatar_url', array( $this, 'pre_get_avatar_url_filter' ), 10, 1 ); 139 $url = get_avatar_url( 1 ); 140 remove_filter( 'pre_get_avatar_url', array( $this, 'pre_get_avatar_url_filter' ), 10 ); 141 142 $this->assertEquals( $url, $this->fakeURL ); 143 } 144 public function pre_get_avatar_url_filter( $url ) { 145 return $this->fakeURL; 146 } 147 148 /** 149 * @ticket 21195 150 */ 151 public function test_get_avatar_url_filter() { 152 $this->fakeURL = 'omg lol'; 153 154 add_filter( 'get_avatar_url', array( $this, 'get_avatar_url_filter' ), 10, 1 ); 155 $url = get_avatar_url( 1 ); 156 remove_filter( 'get_avatar_url', array( $this, 'get_avatar_url_filter' ), 10 ); 157 158 $this->assertEquals( $url, $this->fakeURL ); 159 } 160 public function get_avatar_url_filter( $url ) { 161 return $this->fakeURL; 162 } 163 164 /** 165 * @ticket 21195 166 */ 167 public function test_get_avatar_comment_types_filter() { 168 $url = get_avatar_url( 1 ); 169 170 $post_id = $this->factory->post->create( array( 'post_author' => 1 ) ); 171 $comment_id = $this->factory->comment->create( array( 'comment_post_ID' => $post_id, 'user_id' => 1, 'comment_type' => 'pingback' ) ); 172 $comment = get_comment( $comment_id ); 173 174 $url2 = get_avatar_url( $comment ); 175 $this->assertFalse( $url2 ); 176 177 add_filter( 'get_avatar_comment_types', array( $this, 'get_avatar_comment_types_filter' ), 10, 1 ); 178 $url2 = get_avatar_url( $comment ); 179 remove_filter( 'get_avatar_comment_types', array( $this, 'get_avatar_comment_types_filter' ), 10 ); 180 181 $this->assertEquals( $url, $url2 ); 182 } 183 public function get_avatar_comment_types_filter( $comment_types ) { 184 $comment_types[] = 'pingback'; 185 return $comment_types; 186 } 187 188 public function test_get_avatar() { 189 $img = get_avatar( 1 ); 190 $this->assertEquals( preg_match( '|^<img alt="[^"]*" src="[^"]*" class="[^"]*" height="[^"]*" width="[^"]*" />$|', $img ), 1 ); 191 } 192 193 public function test_get_avatar_size() { 194 $size = '100'; 195 $img = get_avatar( 1, $size ); 196 $this->assertEquals( preg_match( '|^<img .*height="' . $size . '".*width="' . $size . '"|', $img ), 1 ); 197 } 198 199 public function test_get_avatar_alt() { 200 $alt = 'Mr Hyde'; 201 $img = get_avatar( 1, 96, '', $alt ); 202 $this->assertEquals( preg_match( '|^<img alt="' . $alt . '"|', $img ), 1 ); 203 } 204 205 public function test_get_avatar_class() { 206 $class = 'first'; 207 $img = get_avatar( 1, 96, '', '', array( 'class' => $class ) ); 208 $this->assertEquals( preg_match( '|^<img .*class="[^"]*' . $class . '[^"]*"|', $img ), 1 ); 209 } 210 211 public function test_get_avatar_default_class() { 212 $img = get_avatar( 1, 96, '', '', array( 'force_default' => true ) ); 213 $this->assertEquals( preg_match( '|^<img .*class="[^"]*avatar-default[^"]*"|', $img ), 1 ); 214 } 215 216 public function test_get_avatar_force_display() { 217 $old = get_option( 'show_avatars' ); 218 update_option( 'show_avatars', false ); 219 220 $this->assertFalse( get_avatar( 1 ) ); 221 222 $this->assertNotEmpty( get_avatar( 1, 96, '', '', array( 'force_display' => true ) ) ); 223 224 update_option( 'show_avatars', $old ); 225 } 226 227 228 protected $fakeIMG; 229 /** 230 * @ticket 21195 231 */ 232 public function test_pre_get_avatar_filter() { 233 $this->fakeIMG = 'YOU TOO?!'; 234 235 add_filter( 'pre_get_avatar', array( $this, 'pre_get_avatar_filter' ), 10, 1 ); 236 $img = get_avatar( 1 ); 237 remove_filter( 'pre_get_avatar', array( $this, 'pre_get_avatar_filter' ), 10 ); 238 239 $this->assertEquals( $img, $this->fakeIMG ); 240 } 241 public function pre_get_avatar_filter( $img ) { 242 return $this->fakeIMG; 243 } 244 245 /** 246 * @ticket 21195 247 */ 248 public function test_get_avatar_filter() { 249 $this->fakeURL = 'YA RLY'; 250 251 add_filter( 'get_avatar', array( $this, 'get_avatar_filter' ), 10, 1 ); 252 $img = get_avatar( 1 ); 253 remove_filter( 'get_avatar', array( $this, 'get_avatar_filter' ), 10 ); 254 255 $this->assertEquals( $img, $this->fakeURL ); 256 } 257 public function get_avatar_filter( $img ) { 258 return $this->fakeURL; 259 } 260 261 }