Installer
3 years ago
AdminNotices.php
2 months ago
AjaxHandler.php
1 year ago
Autologin.php
9 months ago
BlockRegistrations.php
10 months ago
BuddyPressBbPress.php
3 years ago
DisableConcurrentLogins.php
2 years ago
EditUserProfile.php
5 months ago
ExtensionManager.php
1 month ago
FileUploader.php
3 months ago
FormPreviewHandler.php
5 months ago
FormRepository.php
1 year ago
FormShortcodeDefaults.php
1 month ago
GDPR.php
3 years ago
Geolocation.php
3 years ago
GlobalSiteAccess.php
3 years ago
ImageUploader.php
1 year ago
LoginAuth.php
1 year ago
Miscellaneous.php
3 years ago
ModifyRedirectDefaultLinks.php
2 hours ago
PPRESS_Session.php
1 year ago
PROFILEPRESS_sql.php
1 year ago
PasswordReset.php
1 year ago
ProfileUrlRewrite.php
4 years ago
RegistrationAuth.php
1 week ago
SendEmail.php
3 years ago
ShortcodeThemeFactory.php
5 years ago
UserAvatar.php
1 year ago
UserSignupLocationListingPage.php
3 years ago
UsernameEmailRestrictLogin.php
4 years ago
WPProfileFieldParserTrait.php
1 year ago
WelcomeEmailAfterSignup.php
1 year ago
default-email-template.php
1 year ago
index.php
3 years ago
UserAvatar.php
265 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Classes; |
| 4 | |
| 5 | class UserAvatar |
| 6 | { |
| 7 | public function __construct() |
| 8 | { |
| 9 | add_filter('pre_get_avatar_data', function ($args, $id_or_email) { |
| 10 | |
| 11 | if (ppress_var($args, 'force_default') === true || ppress_var($args, 'ppress_skip') === true) return $args; |
| 12 | |
| 13 | $size = apply_filters('ppress_user_avatar_image_size', ppress_var($args, 'size'), $id_or_email); |
| 14 | |
| 15 | $original = ppress_var($args, 'ppress-full') === true; |
| 16 | |
| 17 | $avatar_url = self::get_avatar_complete_url($id_or_email, $size, $original); |
| 18 | |
| 19 | if ( ! empty($avatar_url)) { |
| 20 | $args['url'] = $avatar_url; |
| 21 | $args['found_avatar'] = true; |
| 22 | } |
| 23 | |
| 24 | return $args; |
| 25 | |
| 26 | }, PHP_INT_MAX - 1, 2); |
| 27 | |
| 28 | add_filter('get_avatar', function ($avatar, $id_or_email, $size, $default, $alt, $args) { |
| 29 | |
| 30 | if (ppress_var($args, 'force_default') !== true && ppress_var($args, 'ppress_skip') !== true) { |
| 31 | if (self::user_has_pp_avatar($id_or_email)) { |
| 32 | $class = []; |
| 33 | if (isset($args['class'])) { |
| 34 | if (is_array($args['class'])) { |
| 35 | $class = array_merge($class, $args['class']); |
| 36 | } else { |
| 37 | $class[] = $args['class']; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | $class = esc_attr(implode(' ', $class)); |
| 42 | |
| 43 | $original = ppress_var($args, 'ppress-full') === true; |
| 44 | |
| 45 | $avatar = self::get_avatar_img($id_or_email, $size, $alt, $class, '', $original); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | return $avatar; |
| 50 | |
| 51 | }, PHP_INT_MAX - 1, 6); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Get user profile picture. Falls back to default avatar or gravatar set in settings. |
| 56 | * |
| 57 | * @param string|int $id_or_email |
| 58 | * @param int $size |
| 59 | * @param bool $original |
| 60 | * |
| 61 | * @return false|string |
| 62 | */ |
| 63 | public static function get_avatar_complete_url($id_or_email, $size = '', $original = false) |
| 64 | { |
| 65 | $url = false; |
| 66 | |
| 67 | if ( ! empty($size)) $size = absint($size); |
| 68 | |
| 69 | if (self::user_has_pp_avatar($id_or_email)) { |
| 70 | |
| 71 | $url = self::get_pp_avatar_url($id_or_email, $size, $original); |
| 72 | |
| 73 | } else { |
| 74 | |
| 75 | /** WP User Avatar Adapter STARTS */ |
| 76 | global $wpua_disable_gravatar, $wpua_functions; |
| 77 | |
| 78 | // First checking custom avatar. |
| 79 | if ($wpua_disable_gravatar == '1') { |
| 80 | if (is_object($wpua_functions) && method_exists($wpua_functions, 'wpua_get_default_avatar_url')) { |
| 81 | $url = $wpua_functions->wpua_get_default_avatar_url($size); |
| 82 | } |
| 83 | } else { |
| 84 | |
| 85 | $has_valid_url = false; |
| 86 | |
| 87 | if (is_object($wpua_functions) && method_exists($wpua_functions, 'wpua_has_gravatar')) { |
| 88 | $has_valid_url = $wpua_functions->wpua_has_gravatar($id_or_email); |
| 89 | } elseif (class_exists('\WP_User_Avatar_Functions')) { |
| 90 | |
| 91 | $obj = new \WP_User_Avatar_Functions(); |
| 92 | if (method_exists($obj, 'wpua_has_gravatar')) { |
| 93 | $has_valid_url = $obj->wpua_has_gravatar($id_or_email); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | if ( ! $has_valid_url) { |
| 98 | |
| 99 | $wpua_size = ! empty($size) ? $size : 96; |
| 100 | |
| 101 | if (is_object($wpua_functions) && method_exists($wpua_functions, 'wpua_get_default_avatar_url')) { |
| 102 | $url = $wpua_functions->wpua_get_default_avatar_url($wpua_size); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | /** WP User Avatar Adapter ENDS */ |
| 107 | } |
| 108 | |
| 109 | return apply_filters('ppress_user_avatar_image_url', $url, $id_or_email, $size); |
| 110 | } |
| 111 | |
| 112 | public static function user_has_pp_avatar($id_or_email) |
| 113 | { |
| 114 | $user_id = self::get_avatar_user_id($id_or_email); |
| 115 | |
| 116 | $avatar_url = get_user_meta($user_id, 'pp_profile_avatar', true); |
| 117 | |
| 118 | if ( ! empty($avatar_url) && is_string($avatar_url)) return true; |
| 119 | |
| 120 | /** WP User Avatar Adapter STARTS */ |
| 121 | global $wpdb, $blog_id, $avatar_default, $wpua_avatar_default; |
| 122 | |
| 123 | $attachment_id = get_user_meta($user_id, $wpdb->get_blog_prefix($blog_id) . 'user_avatar', true); |
| 124 | // Check if avatar is same as default avatar or on excluded list |
| 125 | if ( ! empty($attachment_id) && ($avatar_default != 'wp_user_avatar' || $attachment_id != $wpua_avatar_default) && wp_attachment_is_image($attachment_id)) { |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | /** WP User Avatar Adapter ENDS */ |
| 130 | |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * @param $id_or_email |
| 136 | * |
| 137 | * @param string $size |
| 138 | * |
| 139 | * @return string |
| 140 | */ |
| 141 | public static function get_pp_avatar_url($id_or_email, $size = '', $original = false) |
| 142 | { |
| 143 | $user_id = self::get_avatar_user_id($id_or_email); |
| 144 | |
| 145 | $avatar_url = get_user_meta($user_id, 'pp_profile_avatar', true); |
| 146 | |
| 147 | if ( ! empty($avatar_url) && is_string($avatar_url)) { |
| 148 | |
| 149 | return PPRESS_AVATAR_UPLOAD_URL . "$avatar_url"; |
| 150 | } |
| 151 | |
| 152 | /** WP User Avatar Adapter STARTS */ |
| 153 | global $wpdb, $blog_id; |
| 154 | |
| 155 | $attachment_id = get_the_author_meta($wpdb->get_blog_prefix($blog_id) . 'user_avatar', $user_id); |
| 156 | |
| 157 | if ( ! empty($attachment_id) && wp_attachment_is_image($attachment_id)) { |
| 158 | |
| 159 | if ( ! empty($size) && ! $original) { |
| 160 | |
| 161 | $size = is_numeric($size) ? [$size, $size] : $size; |
| 162 | |
| 163 | return ppress_var(wp_get_attachment_image_src($attachment_id, $size), 0); |
| 164 | } |
| 165 | |
| 166 | return wp_get_attachment_url($attachment_id); |
| 167 | } |
| 168 | |
| 169 | /** WP User Avatar Adapter ENDS */ |
| 170 | |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * HTML image for the user profile |
| 176 | * |
| 177 | * @param $id_or_email |
| 178 | * @param string $size |
| 179 | * @param string $alt |
| 180 | * @param string $class |
| 181 | * @param string $css_id |
| 182 | * @param bool $original |
| 183 | * |
| 184 | * @return mixed |
| 185 | */ |
| 186 | public static function get_avatar_img($id_or_email, $size = '96', $alt = '', $class = '', $css_id = '', $original = true) |
| 187 | { |
| 188 | $alt = esc_attr($alt); |
| 189 | |
| 190 | if ( ! is_numeric($size)) $size = '96'; |
| 191 | |
| 192 | if ( ! empty($css_id)) $css_id = " id='$css_id'"; |
| 193 | |
| 194 | $size = apply_filters('ppress_user_avatar_image_size', absint($size), $id_or_email); |
| 195 | |
| 196 | if ($original === true) $size = 800; |
| 197 | |
| 198 | $avatar_url = get_avatar_url($id_or_email, ['size' => $size, 'ppress-full' => $original]); |
| 199 | |
| 200 | $alt_attr = apply_filters('ppress_avatar_img_alt', ! empty($alt) ? " alt=\"{$alt}\"" : '', $id_or_email); |
| 201 | |
| 202 | return "<img data-del=\"avatar\"" . $alt_attr . " src='{$avatar_url}' class='avatar pp-user-avatar avatar-{$size} photo {$class}' height='{$size}' width='{$size}'$css_id/>"; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Culled from get_avatar_data() |
| 207 | * |
| 208 | * @param mixed $id_or_email |
| 209 | * |
| 210 | * @return bool|int |
| 211 | */ |
| 212 | public static function get_avatar_user_id($id_or_email) |
| 213 | { |
| 214 | if (is_object($id_or_email) && isset($id_or_email->comment_ID)) { |
| 215 | |
| 216 | $email = get_comment($id_or_email)->comment_author_email; |
| 217 | |
| 218 | return ppress_var_obj(get_user_by('email', $email), 'ID'); |
| 219 | } |
| 220 | |
| 221 | if (is_numeric($id_or_email)) { |
| 222 | return ppress_var_obj(get_user_by('id', absint($id_or_email)), 'ID'); |
| 223 | } |
| 224 | |
| 225 | if (is_string($id_or_email)) { |
| 226 | |
| 227 | if ( ! strpos($id_or_email, '@md5.gravatar.com')) { |
| 228 | // email address |
| 229 | return ppress_var_obj(get_user_by('email', $id_or_email), 'ID'); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | if ($id_or_email instanceof \WP_User) { |
| 234 | return $id_or_email->ID; |
| 235 | } |
| 236 | |
| 237 | if ($id_or_email instanceof \WP_Post) { |
| 238 | return get_user_by('id', (int)$id_or_email->post_author)->ID; |
| 239 | } |
| 240 | |
| 241 | if ($id_or_email instanceof \WP_Comment) { |
| 242 | |
| 243 | if ( ! is_avatar_comment_type(get_comment_type($id_or_email))) return false; |
| 244 | |
| 245 | if ( ! empty($id_or_email->user_id)) return $id_or_email->user_id; |
| 246 | |
| 247 | if ( ! empty($id_or_email->comment_author_email)) { |
| 248 | return get_user_by('email', $id_or_email->comment_author_email)->ID; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | public static function get_instance() |
| 256 | { |
| 257 | static $instance = null; |
| 258 | |
| 259 | if (is_null($instance)) { |
| 260 | $instance = new self(); |
| 261 | } |
| 262 | |
| 263 | return $instance; |
| 264 | } |
| 265 | } |