Ticket #21195: 21195.8.diff
File 21195.8.diff, 16.3 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 * @uses apply_filters() 'pre_get_avatar_url' to bypass 3147 * @uses apply_filters() 'get_avatar_url' filters the result 3148 * @uses apply_filters() 'get_avatar_comment_types' filters the comment types for which we can calculate an avatar 3149 * 3150 * @since 4.1.0 3151 * @param mixed $id_or_email The Gravatar to retrieve { 3152 * @type int {user_id} : Use the email address of the corresponding user 3153 * @type string "{hash}@md5.gravatar.com" : Use the hash directly 3154 * @type string "{email}" 3155 * @type object {User row or WP_User object}: Use the user's email 3156 * @type object {Post row or WP_Post object}: Use the post_author's email 3157 * @type object {Comment row} : Use the comment's user_id or comment_author_email 3158 * } 3159 * @param array $args Extra options to apply to the avatar { 3160 * @type int $size Size of the avatar image 3161 * @type string $default URL for the default image or a default type { 3162 * 404 : Return a 404 instead of a default image 3163 * retro : 8bit 3164 * monsterid : Monster 3165 * wavatar : cartoon face 3166 * identicon : the "quilt" 3167 * mystery, mm, mysteryman: The Oyster Man 3168 * blank : A transparent GIF 3169 * gravatar_default : Gravatar Logo 3170 * } 3171 * @type bool $force_default Always show the default image, never the Gravatar 3172 * @type string $rating display avatars up to the given rating: G < PG < R < X. 3173 * @type string $scheme @see set_url_scheme() 3174 * @type array $processed_args Pass as reference. When the function returns, the value will be the processed/sanitized $args plus a "found_avatar" guess. 3175 * } 3176 * 3177 * @return bool|string URL false on failure 3178 */ 3179 function get_avatar_url( $id_or_email, $args = null ) { 3180 $original_args = $args; 3181 3182 $args = wp_parse_args( $args, array( 3183 'size' => 96, 3184 'default' => get_option( 'avatar_default', 'mystery' ), 3185 'force_default' => false, 3186 'rating' => get_option( 'avatar_rating' ), 3187 'scheme' => null, 3188 'processed_args' => null, // if used, should be a reference 3189 ) ); 3190 3191 if ( is_numeric( $args['size'] ) ) { 3192 $args['size'] = absint( $args['size'] ); 3193 if ( ! $args['size'] ) { 3194 $args['size'] = 96; 3195 } 3196 } else { 3197 $args['size'] = 96; 3198 } 3199 3200 if ( empty( $args['default'] ) ) { 3201 $args['default'] = 'mystery'; 3202 } 3203 3204 switch ( $args['default'] ) { 3205 case 'mm' : 3206 case 'mystery' : 3207 case 'mysteryman' : 3208 $args['default'] = 'mm'; 3209 break; 3210 case 'gravatar_default' : 3211 $args['default'] = false; 3212 break; 3213 } 3214 3215 $args['force_default'] = (bool) $args['force_default']; 3216 3217 $args['rating'] = strtolower( $args['rating'] ); 3218 3219 $args['found_avatar'] = false; 3220 3221 /** 3222 * Filter the avatar to retrieve early. 3223 * 3224 * If this filter doesn't return null, the return value will be used as the avatar URL. 3225 * 3226 * @since 4.1.0 3227 * 3228 * @param string $url URL for the user's avatar. Default null. 3229 * @param int|object|string $id_or_email A user ID, email address, or comment object. 3230 * @param array $args Arguments passed to get_avatar_url(), after processing. 3231 * @param array $original_args Original arguments passed to get_avatar_url(). 3232 */ 3233 $url = apply_filters_ref_array( 'pre_get_avatar_url', array( null, $id_or_email, &$args, $original_args ) ); 3234 if ( ! is_null( $url ) ) { 3235 /** This filter is documented in src/wp-includes/link-template.php */ 3236 $return = apply_filters_ref_array( 'get_avatar_url', array( $url, $id_or_email, &$args, $original_args ) ); 3237 $args['processed_args'] = $args; 3238 unset( $args['processed_args']['processed_args'] ); 3239 return $return; 3240 } 3241 3242 $email_hash = ''; 3243 $user = $email = false; 3244 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 $args['processed_args'] = $args; 3331 unset( $args['processed_args']['processed_args'] ); 3332 return $return; 3333 } -
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 * @uses apply_filters() 'pre_get_avatar' to bypass 2092 * @uses apply_filters() 'get_avatar' filters the result 2093 * 2091 2094 * @since 2.5.0 2095 * @param mixed $id_or_email The Gravatar to retrieve { 2096 * @type int {user_id} : Use the email address of the corresponding user 2097 * @type string "{hash}@md5.gravatar.com" : Use the hash directly 2098 * @type string "{email}" 2099 * @type object {User row or WP_User object}: Use the user's email 2100 * @type object {Post row or WP_Post object}: Use the post_author's email 2101 * @type object {Comment row} : Use the comment's user_id or comment_author_email 2102 * } 2103 * @param int $size Size of the avatar image 2104 * @param string $default URL for the default image or a default type { 2105 * 404 : Return a 404 instead of a default image 2106 * retro : 8bit 2107 * monsterid : Monster 2108 * wavatar : cartoon face 2109 * identicon : the "quilt" 2110 * mystery, mm, mysteryman: The Oyster Man 2111 * blank : A transparent GIF 2112 * gravatar_default : Gravatar Logo 2113 * } 2114 * @param string $alt Alternative text to use in image tag. Defaults to blank 2115 * @param array $args Extra options to apply to the avatar (since 4.1.0) { 2116 * @type bool $force_default Always show the default image, never the Gravatar 2117 * @type string $rating display avatars up to the given rating: G < PG < R < X. 2118 * @type string $scheme @see set_url_scheme() 2119 * @type mixed $class array or string of additional classes to add to the img element 2120 * @type bool $force_display Always show the avatar - ignore the show_avatars option 2121 * } 2092 2122 * 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; 2123 * @return bool|string <img> tag for the user's avatar. False on failure. 2124 */ 2125 function get_avatar( $id_or_email, $size = 96, $default = '', $alt = '', $args = null ) { 2126 $defaults = array( 2127 // get_avatar_url() args 2128 'size' => 96, 2129 'default' => get_option( 'avatar_default', 'mystery' ), 2130 'force_default' => false, 2131 'rating' => get_option( 'avatar_rating' ), 2132 'scheme' => null, 2102 2133 2103 if ( false === $alt)2104 $safe_alt = '';2105 else2106 $safe_alt = esc_attr( $alt);2134 'alt' => '', 2135 'class' => null, 2136 'force_display' => false, 2137 ); 2107 2138 2108 if ( !is_numeric($size) ) 2109 $size = '96'; 2139 if ( empty( $args ) ) { 2140 $args = array(); 2141 } 2110 2142 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 2143 $args['size'] = $size; 2144 $args['default'] = $default; 2145 $args['alt'] = $alt; 2119 2146 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; 2147 $original_args = $args; 2130 2148 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 } 2149 $args = wp_parse_args( $args, $defaults ); 2137 2150 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; 2151 /** 2152 * Filter the avatar to retrieve early. 2153 * 2154 * If this filter doesn't return null, the return value will be used as the avatar URL. 2155 * 2156 * @since 4.1.0 2157 * 2158 * @param string $avatar HTML for the user's avatar. Default null. 2159 * @param int|object|string $id_or_email A user ID, email address, or comment object. 2160 * @param array $args Arguments passed to get_avatar_url(), after processing. 2161 * @param array $original_args Original arguments passed to get_avatar_url(). 2162 */ 2163 $avatar = apply_filters_ref_array( 'pre_get_avatar', array( null, $id_or_email, &$args, $original_args ) ); 2164 if ( ! is_null( $avatar ) ) { 2165 /** This filter is documented in src/wp-include/pluggable.php */ 2166 return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $args, $original_args ); 2142 2167 } 2143 2168 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; 2169 if ( !$args['force_display'] && ! get_option( 'show_avatars' ) ) { 2170 return false; 2150 2171 } 2151 2172 2152 if ( !empty($email) ) 2153 $email_hash = md5( strtolower( trim( $email ) ) ); 2154 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'; 2173 $processed_args = null; 2174 $args['processed_args'] =& $processed_args; 2175 $url = get_avatar_url( $id_or_email, $args ); 2176 if ( ! $url || is_wp_error( $url ) ) { 2177 return false; 2162 2178 } 2163 2179 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 ); 2180 $class = array( 'avatar', 'avatar-' . (int) $processed_args['size'], 'photo' ); 2176 2181 2177 if ( !empty($email) ) { 2178 $out = "$host/avatar/"; 2179 $out .= $email_hash; 2180 $out .= '?s='.$size; 2181 $out .= '&d=' . urlencode( $default ); 2182 if ( ! $processed_args['found_avatar'] || $processed_args['force_default'] ) { 2183 $class[] = ' avatar-default'; 2184 } 2182 2185 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}' />"; 2186 if ( $args['class'] ) { 2187 if ( is_array( $args['class'] ) ) { 2188 $class = array_merge( $class, $args['class'] ); 2189 } else { 2190 $class[] = $args['class']; 2191 } 2192 2192 } 2193 2193 2194 $avatar = sprintf( 2195 '<img alt="%s" src="%s" class="%s" height="%d" width="%d" />', 2196 esc_attr( $processed_args['alt'] ), 2197 esc_url( $url ), 2198 esc_attr( join( ' ', $class ) ), 2199 (int) $processed_args['size'], 2200 (int) $processed_args['size'] 2201 ); 2202 2194 2203 /** 2195 2204 * Filter the avatar to retrieve. 2196 2205 * 2197 2206 * @since 2.5.0 2198 2207 * 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. 2208 * @param string $avatar Image tag for the user's avatar. 2209 * @param int|object|string $id_or_email A user ID, email address, or comment object. 2210 * @param int $size Square avatar width and height in pixels to retrieve. 2211 * @param string $alt Alternative text to use in the avatar image tag. 2212 * Default empty. 2213 * @param array $processed_args Arguments passed to get_avatar_url(), after processing. (since 4.1.0) 2214 * @param array $original_args Original arguments passed to get_avatar_url(). (since 4.1.0) 2204 2215 */ 2205 return apply_filters( 'get_avatar', $avatar, $id_or_email, $ size, $default, $alt);2216 return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $processed_args, $original_args ); 2206 2217 } 2207 2218 endif; 2208 2219