Ticket #21195: 21195.10.diff
File 21195.10.diff, 24.9 KB (added by , 10 years ago) |
---|
-
src/wp-includes/link-template.php
3139 3139 echo $before, $link, $after; 3140 3140 } 3141 3141 } 3142 3143 /** 3144 * Retrieve the avatar URL for a user, email address, MD5 hash, comment, or post. 3145 * 3146 * @since 4.1.0 3147 * 3148 * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash, 3149 * user email, WP_User object, WP_Post object, or comment object. 3150 * @param array $args { 3151 * Optional. Extra arguments to retrieve the avatar. 3152 * 3153 * @type int $size Height and width of the avatar in pixels. Default 96. 3154 * @type string $default URL for the default image or a default type. Accepts '404' (return 3155 * a 404 instead of a default image), 'retro' (8bit), 'monsterid' (monster), 3156 * 'wavatar' (cartoon face), 'indenticon' (the "quilt"), 'mystery', 'mm', 3157 * or 'mysterman' (The Oyster Man), 'blank' (transparent GIF), or 3158 * 'gravatar_default' (the Gravatar logo). Default is the value of the 3159 * 'avatar_default' option, with a fallback of 'mystery'. 3160 * @type bool $force_default Whether to always show the default image, never the Gravatar. Default false. 3161 * @type string $rating What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are 3162 * judged in that order. Default is the value of the 'avatar_rating' option. 3163 * @type string $scheme URL scheme to use. See {@see set_url_scheme()} for accepted values. 3164 * Default null. 3165 * @type array $processed_args When the function returns, the value will be the processed/sanitized $args 3166 * plus a "found_avatar" guess. Pass as a reference. Default null. 3167 * } 3168 * 3169 * @return bool|string Avatar URL, or false on failure. 3170 */ 3171 function get_avatar_url( $id_or_email, &$args = null ) { 3172 $original_args = $args; 3173 3174 $args = wp_parse_args( $args, array( 3175 'size' => 96, 3176 'default' => get_option( 'avatar_default', 'mystery' ), 3177 'force_default' => false, 3178 'rating' => get_option( 'avatar_rating' ), 3179 'scheme' => null, 3180 'processed_args' => null, // if used, should be a reference 3181 ) ); 3182 3183 if ( is_numeric( $args['size'] ) ) { 3184 $args['size'] = absint( $args['size'] ); 3185 if ( ! $args['size'] ) { 3186 $args['size'] = 96; 3187 } 3188 } else { 3189 $args['size'] = 96; 3190 } 3191 3192 if ( empty( $args['default'] ) ) { 3193 $args['default'] = 'mystery'; 3194 } 3195 3196 switch ( $args['default'] ) { 3197 case 'mm' : 3198 case 'mystery' : 3199 case 'mysteryman' : 3200 $args['default'] = 'mm'; 3201 break; 3202 case 'gravatar_default' : 3203 $args['default'] = false; 3204 break; 3205 } 3206 3207 $args['force_default'] = (bool) $args['force_default']; 3208 3209 $args['rating'] = strtolower( $args['rating'] ); 3210 3211 $args['found_avatar'] = false; 3212 3213 // Just in case we're passed the array version of the data objects 3214 if ( is_array( $id_or_email ) ) { 3215 $id_or_email = (object) $id_or_email; 3216 } 3217 3218 /** 3219 * Filter whether to retrieve the avatar URL early. 3220 * 3221 * Passing a non-null value will effectively short-circuit {@see get_avatar_url()}, 3222 * passing the value through the 'get_avatar_url' filter and returning early. 3223 * 3224 * @since 4.1.0 3225 * 3226 * @param string $url URL for the user's avatar. Default null. 3227 * @param int|object|string $id_or_email A user ID, email address, or comment object. 3228 * @param array $args Arguments passed to get_avatar_url(), after processing. 3229 * @param array $original_args Original arguments passed to get_avatar_url(). 3230 */ 3231 $url = apply_filters_ref_array( 'pre_get_avatar_url', array( null, $id_or_email, &$args, $original_args ) ); 3232 3233 if ( ! is_null( $url ) ) { 3234 /** This filter is documented in src/wp-includes/link-template.php */ 3235 $return = apply_filters_ref_array( 'get_avatar_url', array( $url, $id_or_email, &$args, $original_args ) ); 3236 $args['processed_args'] = $args; 3237 unset( $args['processed_args']['processed_args'] ); 3238 return $return; 3239 } 3240 3241 $email_hash = ''; 3242 $user = $email = false; 3243 3244 // Process the user identifier. 3245 if ( is_numeric( $id_or_email ) ) { 3246 $user = get_user_by( 'id', absint( $id_or_email ) ); 3247 } elseif ( is_string( $id_or_email ) ) { 3248 if ( strpos( $id_or_email, '@md5.gravatar.com' ) ) { 3249 // md5 hash 3250 list( $email_hash ) = explode( '@', $id_or_email ); 3251 } else { 3252 // email address 3253 $email = $id_or_email; 3254 } 3255 } elseif ( is_object( $id_or_email ) ) { 3256 if ( isset( $id_or_email->comment_ID ) ) { 3257 // Comment Object 3258 3259 /** 3260 * Filter the list of allowed comment types for retrieving avatars. 3261 * 3262 * @since 3.0.0 3263 * 3264 * @param array $types An array of content types. Default only contains 'comment'. 3265 */ 3266 $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) ); 3267 if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) { 3268 $args['processed_args'] = $args; 3269 unset( $args['processed_args']['processed_args'] ); 3270 return false; 3271 } 3272 3273 if ( ! empty( $id_or_email->user_id ) ) { 3274 $user = get_user_by( 'id', (int) $id_or_email->user_id ); 3275 } 3276 if ( ( ! $user || is_wp_error( $user ) ) && ! empty( $id_or_email->comment_author_email ) ) { 3277 $email = $id_or_email->comment_author_email; 3278 } 3279 } elseif ( ! empty( $id_or_email->user_login ) ) { 3280 // User Object 3281 $user = $id_or_email; 3282 } elseif ( ! empty( $id_or_email->post_author ) ) { 3283 // Post Object 3284 $user = get_user_by( 'id', (int) $id_or_email->post_author ); 3285 } 3286 } 3287 3288 if ( ! $email_hash ) { 3289 if ( $user ) { 3290 $email = $user->user_email; 3291 } 3292 3293 if ( $email ) { 3294 $email_hash = md5( strtolower( trim( $email ) ) ); 3295 } 3296 } 3297 3298 if ( $email_hash ) { 3299 $args['found_avatar'] = true; 3300 $gravatar_server = hexdec( $email_hash[0] ) % 3; 3301 } else { 3302 $gravatar_server = rand( 0, 2 ); 3303 } 3304 3305 $url_args = array( 3306 's' => $args['size'], 3307 'd' => $args['default'], 3308 'f' => $args['force_default'] ? 'y' : false, 3309 'r' => $args['rating'], 3310 ); 3311 3312 $url = sprintf( 'http://%d.gravatar.com/avatar/%s', $gravatar_server, $email_hash ); 3313 3314 $url = add_query_arg( 3315 rawurlencode_deep( array_filter( $url_args ) ), 3316 set_url_scheme( $url, $args['scheme'] ) 3317 ); 3318 3319 /** 3320 * Filter the avatar to retrieve. 3321 * 3322 * @since 4.1.0 3323 * 3324 * @param string $url URL for the user's avatar. 3325 * @param int|object|string $id_or_email A user ID, email address, or comment object. 3326 * @param array $args Arguments passed to get_avatar_url(), after processing. 3327 * @param array $original_args Original arguments passed to get_avatar_url(). 3328 */ 3329 $return = apply_filters_ref_array( 'get_avatar_url', array( $url, $id_or_email, &$args, $original_args ) ); 3330 3331 $args['processed_args'] = $args; 3332 unset( $args['processed_args']['processed_args'] ); 3333 3334 // Don't return a broken URL if we couldn't find the email hash, and none of the filters returned a different URL. 3335 if ( ! $email_hash && $url === $return ) { 3336 return false; 3337 } 3338 3339 return $return; 3340 } -
src/wp-includes/pluggable.php
2086 2086 2087 2087 if ( !function_exists( 'get_avatar' ) ) : 2088 2088 /** 2089 * Retrieve the avatar for a user who provided a user ID or email address.2089 * Retrieve the avatar <img> tag for a user, email address, MD5 hash, comment, or post. 2090 2090 * 2091 2091 * @since 2.5.0 2092 * @since 4.1.0 Optional $args parameter added. 2092 2093 * 2093 * @param int|string|object $id_or_email A user ID, email address, or comment object 2094 * @param int $size Size of the avatar image 2095 * @param string $default URL to a default image to use if no avatar is available 2096 * @param string $alt Alternative text to use in image tag. Defaults to blank 2097 * @return string <img> tag for the user's avatar 2098 */ 2099 function get_avatar( $id_or_email, $size = '96', $default = '', $alt = false ) { 2100 if ( ! get_option('show_avatars') ) 2101 return false; 2094 * @param mixed $id_or_email The Gravatar to retrieve. Accepts a user_id, gravatar md5 hash, 2095 * user email, WP_User object, WP_Post object, or comment object. 2096 * @param int $size Optional. Height and width of the avatar in pixels. Default 96. 2097 * @param string $default Optional. URL for the default image or a default type. Accepts '404' 2098 * (return a 404 instead of a default image), 'retro' (8bit), 'monsterid' 2099 * (monster), 'wavatar' (cartoon face), 'indenticon' (the "quilt"), 2100 * 'mystery', 'mm', or 'mysterman' (The Oyster Man), 'blank' (transparent GIF), 2101 * or 'gravatar_default' (the Gravatar logo). Default is the value of the 2102 * 'avatar_default' option, with a fallback of 'mystery'. 2103 * @param string $alt Optional. Alternative text to use in <img> tag. Default empty. 2104 * @param array $args { 2105 * Optional. Extra arguments to retrieve the avatar. 2106 * 2107 * @type bool $force_default Whether to always show the default image, never the Gravatar. Default false. 2108 * @type string $rating What rating to display avatars up to. Accepts 'G', 'PG', 'R', 'X', and are 2109 * judged in that order. Default is the value of the 'avatar_rating' option. 2110 * @type string $scheme URL scheme to use. See {@see set_url_scheme()} for accepted values. 2111 * Default null. 2112 * @type array|string $class Array or string of additional classes to add to the <img> element. 2113 * Default null. 2114 * @type bool $force_display Whether to always show the avatar - ignores the show_avatars option. 2115 * Default false. 2116 * } 2117 * 2118 * @return bool|string <img> tag for the user's avatar. False on failure. 2119 */ 2120 function get_avatar( $id_or_email, $size = 96, $default = '', $alt = '', $args = null ) { 2121 $defaults = array( 2122 // get_avatar_url() args. 2123 'size' => 96, 2124 'default' => get_option( 'avatar_default', 'mystery' ), 2125 'force_default' => false, 2126 'rating' => get_option( 'avatar_rating' ), 2127 'scheme' => null, 2128 'alt' => '', 2129 'class' => null, 2130 'force_display' => false, 2131 ); 2102 2132 2103 if ( false === $alt) 2104 $safe_alt = ''; 2105 else 2106 $safe_alt = esc_attr( $alt ); 2133 if ( empty( $args ) ) { 2134 $args = array(); 2135 } 2107 2136 2108 if ( !is_numeric($size) ) 2109 $size = '96'; 2137 $args['size'] = $size; 2138 $args['default'] = $default; 2139 $args['alt'] = $alt; 2110 2140 2111 $email = ''; 2112 if ( is_numeric($id_or_email) ) { 2113 $id = (int) $id_or_email; 2114 $user = get_userdata($id); 2115 if ( $user ) 2116 $email = $user->user_email; 2117 } elseif ( is_object($id_or_email) ) { 2118 // No avatar for pingbacks or trackbacks 2141 $original_args = $args; 2119 2142 2120 /** 2121 * Filter the list of allowed comment types for retrieving avatars. 2122 * 2123 * @since 3.0.0 2124 * 2125 * @param array $types An array of content types. Default only contains 'comment'. 2126 */ 2127 $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) ); 2128 if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) 2129 return false; 2143 $args = wp_parse_args( $args, $defaults ); 2130 2144 2131 if ( ! empty( $id_or_email->user_id ) ) { 2132 $id = (int) $id_or_email->user_id; 2133 $user = get_userdata($id); 2134 if ( $user ) 2135 $email = $user->user_email; 2136 } 2137 2138 if ( ! $email && ! empty( $id_or_email->comment_author_email ) ) 2139 $email = $id_or_email->comment_author_email; 2140 } else { 2141 $email = $id_or_email; 2145 /** 2146 * Filter whether to retrieve the avatar URL early. 2147 * 2148 * Passing a non-null value will effectively short-circuit {@see get_avatar()}, 2149 * passing the value through the 'pre_get_avatar' filter and returning early. 2150 * 2151 * @since 4.1.0 2152 * 2153 * @param string $avatar HTML for the user's avatar. Default null. 2154 * @param int|object|string $id_or_email A user ID, email address, or comment object. 2155 * @param array $args Arguments passed to get_avatar_url(), after processing. 2156 * @param array $original_args Original arguments passed to get_avatar_url(). 2157 */ 2158 $avatar = apply_filters_ref_array( 'pre_get_avatar', array( null, $id_or_email, &$args, $original_args ) ); 2159 if ( ! is_null( $avatar ) ) { 2160 /** This filter is documented in src/wp-include/pluggable.php */ 2161 return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $args, $original_args ); 2142 2162 } 2143 2163 2144 if ( empty($default) ) { 2145 $avatar_default = get_option('avatar_default'); 2146 if ( empty($avatar_default) ) 2147 $default = 'mystery'; 2148 else 2149 $default = $avatar_default; 2164 if ( ! $args['force_display'] && ! get_option( 'show_avatars' ) ) { 2165 return false; 2150 2166 } 2151 2167 2152 if ( !empty($email) )2153 $email_hash = md5( strtolower( trim( $email ) ) );2168 $processed_args = null; 2169 $args['processed_args'] =& $processed_args; 2154 2170 2155 if ( is_ssl() ) { 2156 $host = 'https://secure.gravatar.com'; 2157 } else { 2158 if ( !empty($email) ) 2159 $host = sprintf( "http://%d.gravatar.com", ( hexdec( $email_hash[0] ) % 2 ) ); 2160 else 2161 $host = 'http://0.gravatar.com'; 2171 $url = get_avatar_url( $id_or_email, $args ); 2172 2173 if ( ! $url || is_wp_error( $url ) ) { 2174 return false; 2162 2175 } 2163 2176 2164 if ( 'mystery' == $default ) 2165 $default = "$host/avatar/ad516503a11cd5ca435acc9bb6523536?s={$size}"; // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com') 2166 elseif ( 'blank' == $default ) 2167 $default = $email ? 'blank' : includes_url( 'images/blank.gif' ); 2168 elseif ( !empty($email) && 'gravatar_default' == $default ) 2169 $default = ''; 2170 elseif ( 'gravatar_default' == $default ) 2171 $default = "$host/avatar/?s={$size}"; 2172 elseif ( empty($email) ) 2173 $default = "$host/avatar/?d=$default&s={$size}"; 2174 elseif ( strpos($default, 'http://') === 0 ) 2175 $default = add_query_arg( 's', $size, $default ); 2177 $class = array( 'avatar', 'avatar-' . (int) $processed_args['size'], 'photo' ); 2176 2178 2177 if ( !empty($email) ) { 2178 $out = "$host/avatar/"; 2179 $out .= $email_hash; 2180 $out .= '?s='.$size; 2181 $out .= '&d=' . urlencode( $default ); 2179 if ( ! $processed_args['found_avatar'] || $processed_args['force_default'] ) { 2180 $class[] = 'avatar-default'; 2181 } 2182 2182 2183 $rating = get_option('avatar_rating'); 2184 if ( !empty( $rating ) ) 2185 $out .= "&r={$rating}"; 2186 2187 $out = str_replace( '&', '&', esc_url( $out ) ); 2188 $avatar = "<img alt='{$safe_alt}' src='{$out}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />"; 2189 } else { 2190 $out = esc_url( $default ); 2191 $avatar = "<img alt='{$safe_alt}' src='{$out}' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />"; 2183 if ( $args['class'] ) { 2184 if ( is_array( $args['class'] ) ) { 2185 $class = array_merge( $class, $args['class'] ); 2186 } else { 2187 $class[] = $args['class']; 2188 } 2192 2189 } 2193 2190 2191 $avatar = sprintf( 2192 '<img alt="%s" src="%s" class="%s" height="%d" width="%d" />', 2193 esc_attr( $processed_args['alt'] ), 2194 esc_url( $url ), 2195 esc_attr( join( ' ', $class ) ), 2196 (int) $processed_args['size'], 2197 (int) $processed_args['size'] 2198 ); 2199 2194 2200 /** 2195 2201 * Filter the avatar to retrieve. 2196 2202 * 2197 2203 * @since 2.5.0 2204 * @since 4.1.0 $processed_args and $original_args parameters added 2198 2205 * 2199 * @param string $avatar Image tag for the user's avatar. 2200 * @param int|object|string $id_or_email A user ID, email address, or comment object. 2201 * @param int $size Square avatar width and height in pixels to retrieve. 2202 * @param string $alt Alternative text to use in the avatar image tag. 2203 * Default empty. 2206 * @param string $avatar <img> tag for the user's avatar. 2207 * @param int|object|string $id_or_email A user ID, email address, or comment object. 2208 * @param int $size Square avatar width and height in pixels to retrieve. 2209 * @param string $alt Alternative text to use in the avatar image tag. 2210 * Default empty. 2211 * @param array $processed_args Arguments passed to get_avatar_url(), after processing. 2212 * @param array $original_args Original arguments passed to get_avatar_url(). 2204 2213 */ 2205 return apply_filters( 'get_avatar', $avatar, $id_or_email, $ size, $default, $alt);2214 return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $processed_args, $original_args ); 2206 2215 } 2207 2216 endif; 2208 2217 -
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->fakeIMG ); 256 } 257 public function get_avatar_filter( $img ) { 258 return $this->fakeIMG; 259 } 260 261 }