api-routes.php
5 years ago
avatar.php
4 years ago
counter-settings.php
5 years ago
legacy.php
5 years ago
login-settings.php
3 years ago
providers.php
3 years ago
route.php
4 years ago
settings.php
5 years ago
share-settings.php
4 years ago
share.php
3 years ago
avatar.php
442 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WP_Social\App; |
| 4 | |
| 5 | use WP_Social\Traits\Singleton; |
| 6 | |
| 7 | class Avatar { |
| 8 | |
| 9 | use Singleton; |
| 10 | |
| 11 | |
| 12 | public function init() { |
| 13 | |
| 14 | add_filter('get_avatar', [$this, 'xs_social_get_avatar'], 10, 5); |
| 15 | } |
| 16 | |
| 17 | |
| 18 | /** |
| 19 | * |
| 20 | * @since 1.3.8 |
| 21 | * |
| 22 | * @param $type |
| 23 | * |
| 24 | * @return string |
| 25 | */ |
| 26 | private function get_user_cache_key($type) { |
| 27 | |
| 28 | return '_wps__user_cache__' . $type; |
| 29 | } |
| 30 | |
| 31 | |
| 32 | public function xs_social_get_avatar($avatar, $id_or_email, $size = 96, $default = '', $alt = '') { |
| 33 | |
| 34 | if(is_numeric($id_or_email)) { |
| 35 | |
| 36 | $pic_url = get_user_meta($id_or_email, 'xs_social_profile_image', true); |
| 37 | |
| 38 | if(!empty($pic_url)) { |
| 39 | |
| 40 | return '<img alt="' . $alt . '" src="' . $pic_url . '" class="avatar avatar-' . $size . ' photo" height="' . $size . '" width="' . $size . '" />'; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | return $avatar; |
| 45 | } |
| 46 | |
| 47 | |
| 48 | /** |
| 49 | * |
| 50 | * @since 1.3.8 |
| 51 | * |
| 52 | * @param $profile |
| 53 | * @param $type |
| 54 | * |
| 55 | * @return string |
| 56 | */ |
| 57 | public function get_nice_name($profile, $type) { |
| 58 | |
| 59 | switch($type) { |
| 60 | |
| 61 | case 'Twitter': |
| 62 | case 'twitter': #fall through |
| 63 | case 'Facebook': |
| 64 | case 'facebook': #fall through |
| 65 | case 'LinkedIn': |
| 66 | case 'linkedIn': |
| 67 | case 'Google': |
| 68 | case 'google': |
| 69 | |
| 70 | $ret = $profile->firstName; |
| 71 | break; |
| 72 | |
| 73 | case 'GitHub': |
| 74 | case 'gitHub': |
| 75 | |
| 76 | $ret = empty($profile->firstName) ? ($this->get_display_name($profile, $type)) : $profile->firstName; |
| 77 | break; |
| 78 | |
| 79 | default: |
| 80 | |
| 81 | $ret = empty($profile->firstName) ? 'Nice name' : $profile->firstName; |
| 82 | } |
| 83 | |
| 84 | return $ret; |
| 85 | } |
| 86 | |
| 87 | |
| 88 | public function get_first_name($profile, $type) { |
| 89 | |
| 90 | switch($type) { |
| 91 | |
| 92 | case 'Twitter': |
| 93 | case 'twitter': #fall through |
| 94 | case 'Facebook': |
| 95 | case 'facebook': #fall through |
| 96 | case 'LinkedIn': |
| 97 | case 'linkedIn': #fall through |
| 98 | case 'Google': |
| 99 | case 'google': #fall through |
| 100 | case 'GitHub': |
| 101 | case 'gitHub': |
| 102 | |
| 103 | $ret = empty($profile->firstName) ? '' : $profile->firstName; |
| 104 | break; |
| 105 | |
| 106 | default: |
| 107 | |
| 108 | $ret = empty($profile->firstName) ? '' : $profile->firstName; |
| 109 | } |
| 110 | |
| 111 | return $ret; |
| 112 | } |
| 113 | |
| 114 | |
| 115 | /** |
| 116 | * |
| 117 | * @since 1.3.8 |
| 118 | * |
| 119 | * @param $profile |
| 120 | * @param $type |
| 121 | * |
| 122 | * @return string |
| 123 | */ |
| 124 | public function get_last_name($profile, $type) { |
| 125 | |
| 126 | switch($type) { |
| 127 | |
| 128 | case 'Twitter': |
| 129 | case 'twitter': #fall through |
| 130 | case 'Facebook': |
| 131 | case 'facebook': #fall through |
| 132 | case 'LinkedIn': |
| 133 | case 'linkedIn': #fall through |
| 134 | case 'Google': |
| 135 | case 'google': #fall through |
| 136 | case 'GitHub': |
| 137 | case 'gitHub': |
| 138 | |
| 139 | $ret = empty($profile->lastName) ? '' : $profile->lastName; |
| 140 | break; |
| 141 | |
| 142 | default: |
| 143 | |
| 144 | $ret = empty($profile->lastName) ? '' : $profile->lastName; |
| 145 | } |
| 146 | |
| 147 | return $ret; |
| 148 | } |
| 149 | |
| 150 | |
| 151 | /** |
| 152 | * |
| 153 | * @since 1.3.8 |
| 154 | * |
| 155 | * @param $profile |
| 156 | * @param $type |
| 157 | * |
| 158 | * @return string |
| 159 | */ |
| 160 | public function get_display_name($profile, $type) { |
| 161 | |
| 162 | switch($type) { |
| 163 | |
| 164 | case 'Twitter': |
| 165 | case 'twitter': #fall through |
| 166 | case 'Facebook': |
| 167 | case 'facebook': #fall through |
| 168 | case 'LinkedIn': |
| 169 | case 'linkedIn': #fall through |
| 170 | case 'Google': |
| 171 | case 'google': #fall through |
| 172 | case 'GitHub': |
| 173 | case 'gitHub': |
| 174 | |
| 175 | $ret = $profile->displayName; |
| 176 | break; |
| 177 | |
| 178 | default: |
| 179 | |
| 180 | $ret = empty($profile->displayName) ? $this->get_nice_name($profile, $type) : $profile->displayName; |
| 181 | } |
| 182 | |
| 183 | return $ret; |
| 184 | } |
| 185 | |
| 186 | |
| 187 | /** |
| 188 | * |
| 189 | * @since 1.3.8 |
| 190 | * |
| 191 | * @param $profile |
| 192 | * @param $type |
| 193 | * |
| 194 | * @return string |
| 195 | */ |
| 196 | protected function get_email($profile, $type) { |
| 197 | |
| 198 | if(!empty($profile->email) || !empty($profile->emailVerified)) { |
| 199 | |
| 200 | return $profile->email; |
| 201 | } |
| 202 | |
| 203 | if(!empty($profile->emailVerified)) { |
| 204 | |
| 205 | return $profile->emailVerified; |
| 206 | } |
| 207 | |
| 208 | |
| 209 | return $profile->identifier . '_not_exist@' . $type . '.com'; |
| 210 | } |
| 211 | |
| 212 | |
| 213 | public function get_user_id_by_social_key($user_name) { |
| 214 | |
| 215 | get_user_meta(); |
| 216 | } |
| 217 | |
| 218 | |
| 219 | /** |
| 220 | * |
| 221 | * @since 1.3.8 |
| 222 | * |
| 223 | * @param $profile |
| 224 | * @param $type |
| 225 | * |
| 226 | * @return string |
| 227 | */ |
| 228 | public function get_username($profile, $type) { |
| 229 | |
| 230 | switch($type) { |
| 231 | |
| 232 | case 'Twitter': |
| 233 | case 'twitter': |
| 234 | |
| 235 | $ret = 'twt' . $profile->identifier; |
| 236 | break; |
| 237 | |
| 238 | case 'Facebook': |
| 239 | case 'facebook': |
| 240 | |
| 241 | $ret = 'fb' . $profile->identifier; |
| 242 | break; |
| 243 | |
| 244 | case 'LinkedIn': |
| 245 | case 'linkedIn': |
| 246 | |
| 247 | $ret = 'Ln' . $profile->identifier; |
| 248 | break; |
| 249 | |
| 250 | case 'Google': |
| 251 | case 'google': |
| 252 | |
| 253 | $ret = 'G' . $profile->identifier; |
| 254 | break; |
| 255 | |
| 256 | case 'GitHub': |
| 257 | case 'gitHub': |
| 258 | |
| 259 | $ret = 'Gt' . $profile->identifier; |
| 260 | break; |
| 261 | |
| 262 | default: |
| 263 | |
| 264 | $ret = empty($profile->identifier) ? strtolower($type) . '_' . $profile->email : strtolower($type) . '_' . $profile->identifier; |
| 265 | } |
| 266 | |
| 267 | return $ret; |
| 268 | } |
| 269 | |
| 270 | |
| 271 | public function make_wp_username($profile, $type) { |
| 272 | |
| 273 | |
| 274 | } |
| 275 | |
| 276 | |
| 277 | /** |
| 278 | * |
| 279 | * @since 1.3.8 |
| 280 | * |
| 281 | * @param $user_key |
| 282 | * @param $type |
| 283 | * |
| 284 | * @return array |
| 285 | */ |
| 286 | public function get_linked_user($user_key, $type) { |
| 287 | |
| 288 | $cache = get_option($this->get_user_cache_key($type)); |
| 289 | |
| 290 | return empty($cache[$user_key]) ? [] : $cache[$user_key]; |
| 291 | } |
| 292 | |
| 293 | |
| 294 | /** |
| 295 | * |
| 296 | * @since 1.3.8 |
| 297 | * |
| 298 | * @param $user_key |
| 299 | * @param $type |
| 300 | * @param array $info |
| 301 | * |
| 302 | * @return mixed |
| 303 | */ |
| 304 | public function update_linked_user($user_key, $type, $info = []) { |
| 305 | |
| 306 | $option_key = $this->get_user_cache_key($type); |
| 307 | |
| 308 | $cache = get_option($option_key); |
| 309 | |
| 310 | $cache[$user_key]['id'] = $info['id']; |
| 311 | $cache[$user_key]['username'] = $info['usr'];; |
| 312 | |
| 313 | return update_option($option_key, $cache); |
| 314 | } |
| 315 | |
| 316 | |
| 317 | /** |
| 318 | * |
| 319 | * @since 1.3.8 |
| 320 | * |
| 321 | * @param $profile |
| 322 | * @param $type |
| 323 | * |
| 324 | * @return string |
| 325 | */ |
| 326 | public function get_available_username($profile, $type) { |
| 327 | |
| 328 | $usr = $this->get_sanitized_username($profile, $type); |
| 329 | |
| 330 | $user_id = username_exists($usr); |
| 331 | |
| 332 | if($user_id == false) { |
| 333 | return $usr; |
| 334 | } |
| 335 | |
| 336 | $counter = 1; |
| 337 | $usr = $usr . $counter; |
| 338 | |
| 339 | while(username_exists($usr) !== false) { |
| 340 | $counter++; |
| 341 | $usr = $usr . $counter; |
| 342 | } |
| 343 | |
| 344 | return $usr; |
| 345 | } |
| 346 | |
| 347 | |
| 348 | /** |
| 349 | * |
| 350 | * @since 1.3.8 |
| 351 | * |
| 352 | * @param $profile |
| 353 | * @param $type |
| 354 | * |
| 355 | * @return string |
| 356 | */ |
| 357 | protected function get_sanitized_username($profile, $type) { |
| 358 | |
| 359 | $user_nm = $this->get_first_name($profile, $type) . $this->get_last_name($profile, $type); |
| 360 | |
| 361 | if(empty($user_nm)) { |
| 362 | |
| 363 | //this is a fallback checking |
| 364 | //if for any reason user first name and last name is not comming we will make the id as out username |
| 365 | return $this->get_username($profile, $type); |
| 366 | } |
| 367 | |
| 368 | $username = strtolower($user_nm); |
| 369 | $username = preg_replace('/\s+/', '', $username); |
| 370 | |
| 371 | $sanitized = sanitize_user($username); |
| 372 | |
| 373 | if(empty($sanitized)) { |
| 374 | |
| 375 | return $this->get_username($profile, $type); |
| 376 | } |
| 377 | |
| 378 | if(!validate_username($sanitized)) { |
| 379 | |
| 380 | return $this->get_username($profile, $type); |
| 381 | } |
| 382 | |
| 383 | return $sanitized; |
| 384 | } |
| 385 | |
| 386 | |
| 387 | /** |
| 388 | * |
| 389 | * @since 1.3.8 |
| 390 | * |
| 391 | * @param $profile |
| 392 | * @param $type |
| 393 | * |
| 394 | * @return mixed |
| 395 | */ |
| 396 | public function get_avatar_url($profile, $type) { |
| 397 | |
| 398 | switch($type) { |
| 399 | |
| 400 | case 'Twitter': |
| 401 | case 'twitter': #fall through |
| 402 | case 'Facebook': |
| 403 | case 'facebook': #fall through |
| 404 | case 'LinkedIn': |
| 405 | case 'linkedIn': #fall through |
| 406 | case 'Google': |
| 407 | case 'google': #fall through |
| 408 | case 'GitHub': |
| 409 | case 'gitHub': |
| 410 | |
| 411 | $ret = $profile->photoURL; |
| 412 | break; |
| 413 | |
| 414 | default: |
| 415 | |
| 416 | $ret = $profile->photoURL; // todo - later we will put the mystry man url from wordpress |
| 417 | } |
| 418 | |
| 419 | return $ret; |
| 420 | } |
| 421 | |
| 422 | |
| 423 | /** |
| 424 | * Only for testing purpose |
| 425 | * |
| 426 | * @since 1.3.8 |
| 427 | * |
| 428 | * @param $type |
| 429 | * @param array $info |
| 430 | * |
| 431 | * @return mixed |
| 432 | */ |
| 433 | public function clear_it($type, $info = []) { |
| 434 | |
| 435 | $option_key = $this->get_user_cache_key($type); |
| 436 | $cache = []; |
| 437 | |
| 438 | return update_option($option_key, $cache); |
| 439 | } |
| 440 | |
| 441 | } |
| 442 |