2073 | | * Retrieve the avatar for a user who provided a user ID or email address. |
2074 | | * |
2075 | | * @since 2.5.0 |
2076 | | * |
2077 | | * @param int|string|object $id_or_email A user ID, email address, or comment object |
2078 | | * @param int $size Size of the avatar image |
2079 | | * @param string $default URL to a default image to use if no avatar is available |
2080 | | * @param string $alt Alternative text to use in image tag. Defaults to blank |
2081 | | * @return string <img> tag for the user's avatar |
2082 | | */ |
2083 | | function get_avatar( $id_or_email, $size = '96', $default = '', $alt = false ) { |
2084 | | if ( ! get_option('show_avatars') ) |
2085 | | return false; |
2086 | | |
2087 | | if ( false === $alt) |
2088 | | $safe_alt = ''; |
2089 | | else |
2090 | | $safe_alt = esc_attr( $alt ); |
2091 | | |
2092 | | if ( !is_numeric($size) ) |
2093 | | $size = '96'; |
2094 | | |
2095 | | $email = ''; |
2096 | | if ( is_numeric($id_or_email) ) { |
2097 | | $id = (int) $id_or_email; |
2098 | | $user = get_userdata($id); |
2099 | | if ( $user ) |
2100 | | $email = $user->user_email; |
2101 | | } elseif ( is_object($id_or_email) ) { |
2102 | | // No avatar for pingbacks or trackbacks |
2103 | | |
2104 | | /** |
2105 | | * Filter the list of allowed comment types for retrieving avatars. |
2106 | | * |
2107 | | * @since 3.0.0 |
2108 | | * |
2109 | | * @param array $types An array of content types. Default only contains 'comment'. |
2110 | | */ |
2111 | | $allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) ); |
2112 | | if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) ) |
2113 | | return false; |
2114 | | |
2115 | | if ( ! empty( $id_or_email->user_id ) ) { |
2116 | | $id = (int) $id_or_email->user_id; |
2117 | | $user = get_userdata($id); |
2118 | | if ( $user ) |
2119 | | $email = $user->user_email; |
2120 | | } |
2121 | | |
2122 | | if ( ! $email && ! empty( $id_or_email->comment_author_email ) ) |
2123 | | $email = $id_or_email->comment_author_email; |
2124 | | } else { |
2125 | | $email = $id_or_email; |
2126 | | } |
2127 | | |
2128 | | if ( empty($default) ) { |
2129 | | $avatar_default = get_option('avatar_default'); |
2130 | | if ( empty($avatar_default) ) |
2131 | | $default = 'mystery'; |
2132 | | else |
2133 | | $default = $avatar_default; |
2134 | | } |
2135 | | |
2136 | | if ( !empty($email) ) |
2137 | | $email_hash = md5( strtolower( trim( $email ) ) ); |
2138 | | |
2139 | | if ( is_ssl() ) { |
2140 | | $host = 'https://secure.gravatar.com'; |
2141 | | } else { |
2142 | | if ( !empty($email) ) |
2143 | | $host = sprintf( "http://%d.gravatar.com", ( hexdec( $email_hash[0] ) % 2 ) ); |
2144 | | else |
2145 | | $host = 'http://0.gravatar.com'; |
2146 | | } |
2147 | | |
2148 | | if ( 'mystery' == $default ) |
2149 | | $default = "$host/avatar/ad516503a11cd5ca435acc9bb6523536?s={$size}"; // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com') |
2150 | | elseif ( 'blank' == $default ) |
2151 | | $default = $email ? 'blank' : includes_url( 'images/blank.gif' ); |
2152 | | elseif ( !empty($email) && 'gravatar_default' == $default ) |
2153 | | $default = ''; |
2154 | | elseif ( 'gravatar_default' == $default ) |
2155 | | $default = "$host/avatar/?s={$size}"; |
2156 | | elseif ( empty($email) ) |
2157 | | $default = "$host/avatar/?d=$default&s={$size}"; |
2158 | | elseif ( strpos($default, 'http://') === 0 ) |
2159 | | $default = add_query_arg( 's', $size, $default ); |
2160 | | |
2161 | | if ( !empty($email) ) { |
2162 | | $out = "$host/avatar/"; |
2163 | | $out .= $email_hash; |
2164 | | $out .= '?s='.$size; |
2165 | | $out .= '&d=' . urlencode( $default ); |
2166 | | |
2167 | | $rating = get_option('avatar_rating'); |
2168 | | if ( !empty( $rating ) ) |
2169 | | $out .= "&r={$rating}"; |
2170 | | |
2171 | | $out = str_replace( '&', '&', esc_url( $out ) ); |
2172 | | $avatar = "<img alt='{$safe_alt}' src='{$out}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />"; |
2173 | | } else { |
2174 | | $avatar = "<img alt='{$safe_alt}' src='{$default}' class='avatar avatar-{$size} photo avatar-default' height='{$size}' width='{$size}' />"; |
2175 | | } |
2176 | | |
2177 | | /** |
2178 | | * Filter the avatar to retrieve. |
2179 | | * |
2180 | | * @since 2.5.0 |
2181 | | * |
2182 | | * @param string $avatar Image tag for the user's avatar. |
2183 | | * @param int|object|string $id_or_email A user ID, email address, or comment object. |
2184 | | * @param int $size Square avatar width and height in pixels to retrieve. |
2185 | | * @param string $alt Alternative text to use in the avatar image tag. |
2186 | | * Default empty. |
2187 | | */ |
2188 | | return apply_filters( 'get_avatar', $avatar, $id_or_email, $size, $default, $alt ); |
| 2073 | * Retrieve the avatar img tag for a user, email address, MD5 hash, comment, or post. |
| 2074 | * |
| 2075 | * @uses apply_filters() 'pre_get_avatar' to bypass |
| 2076 | * @uses apply_filters() 'get_avatar' filters the result |
| 2077 | * |
| 2078 | * @since 2.5 |
| 2079 | * @param mixed $id_or_email The Gravatar to retrieve { |
| 2080 | * @type int {user_id} : Use the email address of the corresponding user |
| 2081 | * @type string "{hash}@md5.gravatar.com" : Use the hash directly |
| 2082 | * @type string "{email}" |
| 2083 | * @type object {User row or WP_User object}: Use the user's email |
| 2084 | * @type object {Post row or WP_Post object}: Use the post_author's email |
| 2085 | * @type object {Comment row} : Use the comment's user_id or comment_author_email |
| 2086 | * } |
| 2087 | * @param int $size Size of the avatar image |
| 2088 | * @param string $default URL for the default image or a default type { |
| 2089 | * 404 : Return a 404 instead of a default image |
| 2090 | * retro : 8bit |
| 2091 | * monsterid : Monster |
| 2092 | * wavatar : cartoon face |
| 2093 | * identicon : the "quilt" |
| 2094 | * mystery, mm, mysteryman: The Oyster Man |
| 2095 | * blank : A transparent GIF |
| 2096 | * gravatar_default : Gravatar Logo |
| 2097 | * } |
| 2098 | * @param string $alt Alternative text to use in image tag. Defaults to blank |
| 2099 | * @param array $args Extra options to apply to the avatar { |
| 2100 | * @type bool $force_default Always show the default image, never the Gravatar |
| 2101 | * @type string $rating display avatars up to the given rating: G < PG < R < X. |
| 2102 | * @type string $scheme @see set_url_scheme() |
| 2103 | * @type mixed $class array or string of additional classes to add to the img element |
| 2104 | * @type bool $force_display Always show the avatar - ignore the show_avatars option |
| 2105 | * } |
| 2106 | * |
| 2107 | * @return bool|string <img> tag for the user's avatar. False on failure. |
| 2108 | */ |
| 2109 | function get_avatar( $id_or_email, $size = 96, $default = '', $alt = '', $args = null ) { |
| 2110 | $defaults = array( |
| 2111 | // get_avatar_url() args |
| 2112 | 'size' => 96, |
| 2113 | 'default' => get_option( 'avatar_default', 'mystery' ), |
| 2114 | 'force_default' => false, |
| 2115 | 'rating' => get_option( 'avatar_rating' ), |
| 2116 | 'scheme' => null, |
| 2117 | |
| 2118 | 'alt' => '', |
| 2119 | 'class' => null, |
| 2120 | 'force_display' => false, |
| 2121 | ); |
| 2122 | |
| 2123 | if ( empty( $args ) ) |
| 2124 | $args = array(); |
| 2125 | |
| 2126 | $args['size'] = $size; |
| 2127 | $args['default'] = $default; |
| 2128 | $args['alt'] = $alt; |
| 2129 | |
| 2130 | $original_args = $args; |
| 2131 | |
| 2132 | $args = wp_parse_args( $args, $defaults ); |
| 2133 | |
| 2134 | $avatar = apply_filters_ref_array( 'pre_get_avatar', array( null, $id_or_email, &$args, $original_args ) ); |
| 2135 | if ( !is_null( $avatar ) ) { |
| 2136 | return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $args, $original_args ); |
| 2137 | } |
| 2138 | |
| 2139 | if ( !$args['force_display'] && !get_option( 'show_avatars' ) ) { |
| 2140 | return false; |
| 2141 | } |
| 2142 | |
| 2143 | $processed_args = null; |
| 2144 | $args['processed_args'] =& $processed_args; |
| 2145 | $url = get_avatar_url( $id_or_email, $args ); |
| 2146 | if ( !$url || is_wp_error( $url ) ) { |
| 2147 | return false; |
| 2148 | } |
| 2149 | |
| 2150 | $class = array( 'avatar', 'avatar-' . (int) $processed_args['size'], 'photo' ); |
| 2151 | |
| 2152 | if ( !$processed_args['found_avatar'] || $processed_args['force_default'] ) { |
| 2153 | $class[] = ' avatar-default'; |
| 2154 | } |
| 2155 | |
| 2156 | if ( $args['class'] ) { |
| 2157 | if ( is_array( $args['class'] ) ) { |
| 2158 | $class = array_merge( $class, $args['class'] ); |
| 2159 | } else { |
| 2160 | $class[] = $args['class']; |
| 2161 | } |
| 2162 | } |
| 2163 | |
| 2164 | $avatar = sprintf( |
| 2165 | '<img alt="%s" src="%s" class="%s" height="%d" width="%d" />', |
| 2166 | esc_attr( $processed_args['alt'] ), |
| 2167 | esc_url( $url ), |
| 2168 | esc_attr( join( ' ', $class ) ), |
| 2169 | (int) $processed_args['size'], |
| 2170 | (int) $processed_args['size'] |
| 2171 | ); |
| 2172 | |
| 2173 | return apply_filters( 'get_avatar', $avatar, $id_or_email, $args['size'], $args['default'], $args['alt'], $processed_args, $original_args ); |