elementor
3 years ago
admin-create-shortcode.php
7 years ago
admin-create-user.php
1 week ago
admin-custom-function.php
1 week ago
admin-rest-api.php
1 week ago
admin-settings.php
2 years ago
admin-social-button.php
1 week ago
admin-social-enque-script.php
5 years ago
counter-widget.php
1 week ago
counter.php
1 week ago
custom-function.php
5 years ago
login-widget.php
3 years ago
login.php
5 years ago
share-widget.php
1 week ago
share.php
1 week ago
admin-create-user.php
872 lines
| 1 | <?php |
| 2 | |
| 3 | use WP_Social\App\Settings; |
| 4 | use WP_Social\App\Login_Settings; |
| 5 | use WP_Social\Lib\Login\Line_App; |
| 6 | |
| 7 | defined('ABSPATH') || exit; |
| 8 | |
| 9 | /** |
| 10 | * session stat for current redirect URL after login from social. |
| 11 | * |
| 12 | * @since : 1.0 |
| 13 | */ |
| 14 | session_start(); |
| 15 | |
| 16 | |
| 17 | if(isset($_GET['XScurrentPage']) and strlen(sanitize_url($_GET['XScurrentPage'])) > 2) { |
| 18 | |
| 19 | $_SESSION['xs_social_login_ref_url'] = sanitize_url($_GET['XScurrentPage']); |
| 20 | $_SESSION['xs_social']['login_ref_url'] = sanitize_url($_GET['XScurrentPage']); |
| 21 | } |
| 22 | |
| 23 | //wordpress default redirect_to param |
| 24 | if(!empty($_GET['redirect_to'])) { |
| 25 | |
| 26 | $_SESSION['xs_social']['redirect_to'] = urldecode(sanitize_url($_GET['redirect_to'])); |
| 27 | } |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * Variable Name: $currentURL |
| 32 | * Variable Details: get Current URL from session data after login by social |
| 33 | * |
| 34 | * @since : 1.0 |
| 35 | */ |
| 36 | $currentURL = isset($_SESSION['xs_social_login_ref_url']) ? sanitize_url($_SESSION['xs_social_login_ref_url']) : get_home_url(); |
| 37 | |
| 38 | /** |
| 39 | * Wp Function: is_user_logged_in(); |
| 40 | * Function Details: check user login. If user is login after redirect current URL by $currentURL |
| 41 | * |
| 42 | * @since : 1.0 |
| 43 | */ |
| 44 | |
| 45 | if(is_user_logged_in()) { |
| 46 | if(wp_redirect($currentURL)) { |
| 47 | exit; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Variable Name : $xs_config |
| 53 | * Variable Type : Array |
| 54 | * |
| 55 | * @since : 1.0 |
| 56 | */ |
| 57 | $xs_config = []; |
| 58 | |
| 59 | if(!empty($typeSocial)) { |
| 60 | if($typeSocial == 'lineapp' && !empty($code)){ |
| 61 | create_line_app_user($code, $typeSocial); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Variable Name : $provider_data |
| 66 | * Variable Type : Array |
| 67 | * @return : array() $provider_data . Get array from socail provider data "" |
| 68 | * |
| 69 | * @since : 1.0 |
| 70 | */ |
| 71 | $provider_data = Settings::get_login_settings_data(); |
| 72 | |
| 73 | /** |
| 74 | * Variable Name : $callBackUrl |
| 75 | * Variable Type : String |
| 76 | * Variable Details : Create dynamic callback URL for all social services. |
| 77 | * |
| 78 | * @since : 1.0 |
| 79 | */ |
| 80 | $callBackUrl = get_home_url() . '/wp-json/wslu-social-login/type/' . $typeSocial; |
| 81 | |
| 82 | /** |
| 83 | * Variable Name : $serviceType |
| 84 | * Variable Type : Array |
| 85 | * @return : array(). Get array from custom function page "admin-custom-function.php" |
| 86 | * |
| 87 | * @since : 1.0 |
| 88 | */ |
| 89 | $serviceType = \WP_Social\App\Providers::get_core_providers_login(); |
| 90 | |
| 91 | /** |
| 92 | * check array key from $serviceType by social type . For Example: facebook |
| 93 | * |
| 94 | * @since : 1.0 |
| 95 | */ |
| 96 | if(array_key_exists($typeSocial, $serviceType)) { |
| 97 | $socialType = $serviceType[$typeSocial]; |
| 98 | |
| 99 | if($typeSocial == 'linkedin') { |
| 100 | $socialType = 'LinkedInOpenID'; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * API configration for Facebook, Twitter, Linkedin, Dribble, Pinterest, Wordpress, Instagram, GitHub, Vkontakte and Reddit |
| 106 | * |
| 107 | * @since : 1.0 |
| 108 | */ |
| 109 | |
| 110 | /** |
| 111 | * Set callback URL in array "$xs_config" for configration API |
| 112 | * |
| 113 | * @since : 1.0 |
| 114 | */ |
| 115 | $xs_config['callback'] = $callBackUrl; |
| 116 | |
| 117 | /** |
| 118 | * Create array for API Providers for all service using foreach by variable "$serviceType" |
| 119 | * |
| 120 | * @since : 1.0 |
| 121 | */ |
| 122 | foreach($serviceType as $serviceKey => $serviceValue) : |
| 123 | $idData = 'id'; |
| 124 | if($serviceKey == 'twitter') { |
| 125 | $idData = 'key'; |
| 126 | } |
| 127 | |
| 128 | $config_para = [ |
| 129 | 'enabled' => true, |
| 130 | 'keys' => [ |
| 131 | $idData => isset($provider_data[$serviceKey]['id']) ? $provider_data[$serviceKey]['id'] : '', |
| 132 | 'secret' => isset($provider_data[$serviceKey]['secret']) ? $provider_data[$serviceKey]['secret'] : '', |
| 133 | ], |
| 134 | ]; |
| 135 | |
| 136 | if( $serviceKey == 'linkedin' ) { |
| 137 | $config_para['scope'] = 'openid profile email'; // optional //Linkedin openid connect scope |
| 138 | } |
| 139 | |
| 140 | if( $serviceKey == 'linkedin' ) { |
| 141 | $xs_config['providers']['LinkedInOpenID'] = $config_para; |
| 142 | }else{ |
| 143 | $xs_config['providers'][$serviceValue] = $config_para; |
| 144 | } |
| 145 | |
| 146 | endforeach; |
| 147 | } |
| 148 | |
| 149 | |
| 150 | /** |
| 151 | * Config API |
| 152 | * |
| 153 | * @since : 1.0 |
| 154 | */ |
| 155 | $code = isset($_GET['code']) ? sanitize_text_field($_GET['code']) : ''; |
| 156 | |
| 157 | if(strlen($socialType) > 0) { |
| 158 | |
| 159 | try { |
| 160 | $hybridauth = new Hybridauth\Hybridauth($xs_config); |
| 161 | |
| 162 | $adapter = $hybridauth->authenticate($socialType); |
| 163 | |
| 164 | $isConnected = $adapter->isConnected(); |
| 165 | |
| 166 | if($isConnected) : |
| 167 | $getProfile = $adapter->getUserProfile(); |
| 168 | |
| 169 | if(is_object($getProfile) && !empty($getProfile)) { |
| 170 | |
| 171 | $user_email = isset($getProfile->email) ? $getProfile->email : ''; |
| 172 | $emailVerified = !empty($getProfile->emailVerified); |
| 173 | if ($emailVerified || empty($user_email)) { |
| 174 | |
| 175 | /** |
| 176 | * Variable Name : $setting_data |
| 177 | * Variable Type : Array |
| 178 | * @return : array() $setting_data . Get array from social global setting data "xs_global_setting_data" |
| 179 | * |
| 180 | * @since : 1.0 |
| 181 | */ |
| 182 | $setting_data = get_option(\WP_Social\Keys::OK_GLOBAL_SETTINGS); |
| 183 | |
| 184 | /** |
| 185 | * Resolve it before resetting the session |
| 186 | */ |
| 187 | $final_redirect = resolve_redirect_url($_SESSION, $setting_data); |
| 188 | |
| 189 | |
| 190 | /** |
| 191 | * Now cleaning the session |
| 192 | * |
| 193 | */ |
| 194 | xs_login_session_handaler(); |
| 195 | |
| 196 | $avatar_obj = \WP_Social\App\Avatar::instance(); |
| 197 | |
| 198 | $first_name = $avatar_obj->get_nice_name($getProfile, $socialType); |
| 199 | $last_name = $avatar_obj->get_last_name($getProfile, $socialType); |
| 200 | $s_user_key = $avatar_obj->get_username($getProfile, $socialType); |
| 201 | $display_nm = $avatar_obj->get_display_name($getProfile, $socialType); |
| 202 | $user_email = $getProfile->email; |
| 203 | $description = $getProfile->description; |
| 204 | |
| 205 | $user_info = $avatar_obj->get_linked_user($s_user_key, $socialType); |
| 206 | |
| 207 | $user = false; |
| 208 | if (isset($user_info['username'])) { |
| 209 | $user = get_user_by('login', $user_info['username']); |
| 210 | } |
| 211 | |
| 212 | if ( !$user) { |
| 213 | |
| 214 | /** |
| 215 | * This is a registration process |
| 216 | * this user never? had registered with us |
| 217 | * |
| 218 | * Lets check if user's social profile email is existed in our system |
| 219 | * If it does we just let him log in |
| 220 | */ |
| 221 | $user_id = email_exists($user_email); |
| 222 | |
| 223 | if($user_id) { |
| 224 | |
| 225 | $user_nameD = xs_login_get_user_data_email($user_email, 'user_login'); |
| 226 | |
| 227 | $avatar_obj->update_linked_user($s_user_key, $socialType, ['id' => $user_id, 'usr' => $user_nameD]); |
| 228 | |
| 229 | xs_user_login($user_nameD, $final_redirect); |
| 230 | |
| 231 | die(esc_html__('Most unlikely error occurred in your case.', 'wp-social')); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * It turns out this user does not used his email in our system |
| 236 | * So lets make him a new username for login |
| 237 | * |
| 238 | */ |
| 239 | $user_nm = $avatar_obj->get_available_username($getProfile, $socialType); |
| 240 | |
| 241 | /** |
| 242 | * Grabbing the default role settings for new user |
| 243 | * Though it is working with wp_insert_user still adding this as per Ataur bhai |
| 244 | * |
| 245 | */ |
| 246 | $default_role = get_option('default_role', ''); |
| 247 | |
| 248 | $insertData = []; |
| 249 | $insertData['first_name'] = $first_name; |
| 250 | $insertData['last_name'] = $last_name; |
| 251 | $insertData['user_nicename'] = $user_nm; |
| 252 | $insertData['user_email'] = $user_email; |
| 253 | $insertData['display_name'] = $display_nm; |
| 254 | $insertData['description'] = $description; |
| 255 | |
| 256 | /** |
| 257 | * User does not exists with prepared username or |
| 258 | * email from social site in our system |
| 259 | * |
| 260 | * Save the image from social site a attachment |
| 261 | * lets make a random password |
| 262 | * now create a new user |
| 263 | * |
| 264 | */ |
| 265 | $password = wp_generate_password(); |
| 266 | $insertData['user_login'] = $user_nm; |
| 267 | $insertData['user_pass'] = $password; |
| 268 | $insertData['role'] = $default_role; |
| 269 | |
| 270 | /** |
| 271 | * Make the avatar url |
| 272 | * and save the image as attachment |
| 273 | */ |
| 274 | $avatar_url = $avatar_obj->get_avatar_url($getProfile, $socialType); |
| 275 | if(get_option('wp_social_login_sync_image_too') == 'yes' && !empty($avatar_url)) { |
| 276 | $attach = save_image_from_url_as_attachment($avatar_url); |
| 277 | }else{ |
| 278 | $attach['error'] = true; |
| 279 | } |
| 280 | |
| 281 | //$checkUser = xs_login_create_user($insertData); |
| 282 | $checkUser = xs_social_create_user($insertData); |
| 283 | |
| 284 | if($checkUser > 0) { |
| 285 | /** |
| 286 | * User created successful |
| 287 | * Update user meta |
| 288 | * Notify admin a new user has been created |
| 289 | * Notify user? [AR: a customer asked!] |
| 290 | * |
| 291 | */ |
| 292 | |
| 293 | if(empty($attach['error'])) { |
| 294 | |
| 295 | update_user_meta($checkUser, 'xs_social_register_by', $socialType); |
| 296 | update_user_meta($checkUser, 'xs_social_profile_image', $attach['url']); |
| 297 | update_user_meta($checkUser, 'xs_social_profile_image_id', $attach['attachment_id']); |
| 298 | } else { |
| 299 | update_user_meta($checkUser, 'xs_social_profile_image', ''); |
| 300 | update_user_meta($checkUser, 'xs_social_profile_image_error_log', $socialType . '::' . $attach['error']); |
| 301 | } |
| 302 | |
| 303 | $avatar_obj->update_linked_user($s_user_key, $socialType, ['id' => $checkUser, 'usr' => $user_nm]); |
| 304 | |
| 305 | /** |
| 306 | * As we have created the user with a random password and they are registering with social credential |
| 307 | * so there is no use of change of password |
| 308 | */ |
| 309 | update_user_meta($checkUser, 'xs_password_changed', 'yes'); |
| 310 | |
| 311 | notify_new_user_to_user($insertData); |
| 312 | |
| 313 | $wp_social_login_settings = get_option('xs_global_setting_data'); |
| 314 | |
| 315 | if (isset($wp_social_login_settings['email_new_registered_user']['enable']) && $wp_social_login_settings['email_new_registered_user']['enable'] == 1) { |
| 316 | notify_new_user_to_admin($checkUser, $socialType); |
| 317 | } |
| 318 | |
| 319 | xs_user_login($user_nm, $final_redirect); |
| 320 | |
| 321 | die(esc_html__('Most most unlikely error occurred in your case. user registration done but login failed!!', 'wp-social')); |
| 322 | } |
| 323 | |
| 324 | die(esc_html__('New user creation failed!', 'wp-social')); |
| 325 | |
| 326 | } else { |
| 327 | |
| 328 | $id = $user->data->ID; |
| 329 | |
| 330 | update_user_meta($id, 'xs_social_register_by', $socialType); |
| 331 | |
| 332 | if(get_option('wp_social_login_sync') == 'yes') { |
| 333 | |
| 334 | |
| 335 | update_user_meta($id, 'first_name', $first_name); |
| 336 | update_user_meta($id, 'last_name', $last_name); |
| 337 | update_user_meta($id, 'display_name', $display_nm); |
| 338 | update_user_meta($id, 'description', $description); |
| 339 | |
| 340 | |
| 341 | if(get_option('wp_social_login_sync_image_too') == 'yes') { |
| 342 | $avatar_url = $avatar_obj->get_avatar_url($getProfile, $socialType); |
| 343 | $current_avatar_url = get_user_meta($id, 'xs_social_profile_image', true); |
| 344 | |
| 345 | $attach = []; |
| 346 | |
| 347 | if(!empty($current_avatar_url) && !empty($avatar_url) && $current_avatar_url != $avatar_url) { |
| 348 | |
| 349 | $attach = save_image_from_url_as_attachment($avatar_url); |
| 350 | } |
| 351 | |
| 352 | if(!empty($attach) && empty($attach['error'])) { |
| 353 | |
| 354 | $existing_attachment_id = get_user_meta($id, 'xs_social_profile_image_id', true); |
| 355 | |
| 356 | if (!empty($existing_attachment_id)) { |
| 357 | wp_delete_attachment($existing_attachment_id, true); |
| 358 | } |
| 359 | update_user_meta($id, 'xs_social_profile_image', $attach['url']); |
| 360 | update_user_meta($id, 'xs_social_profile_image_id', $attach['attachment_id']); |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Proceeding to login |
| 367 | * |
| 368 | */ |
| 369 | |
| 370 | $user_name = $user_info['username']; |
| 371 | |
| 372 | xs_user_login($user_name, $final_redirect); |
| 373 | |
| 374 | die(esc_html__('Most unlikely error occurred in your case.', 'wp-social')); |
| 375 | } |
| 376 | } else { |
| 377 | // Email not verified or not provided |
| 378 | die(esc_html__('Email address is not verified or not provided by the social provider.', 'wp-social')); |
| 379 | } |
| 380 | |
| 381 | } else { |
| 382 | die(esc_html__('System Error for Callback!', 'wp-social')); |
| 383 | } |
| 384 | |
| 385 | endif; |
| 386 | |
| 387 | $adapter->disconnect(); |
| 388 | |
| 389 | } catch(\Exception $e) { |
| 390 | echo esc_html__('Oops, we ran into an issue!', 'wp-social') . esc_html($e->getMessage()); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | |
| 395 | /** |
| 396 | * Function Name : xs_login_create_user(); |
| 397 | * Function Details : create new user from socail login and check enable wp new create new users. |
| 398 | * |
| 399 | * @params : array() $userdata. For user information |
| 400 | * |
| 401 | * @return : int() if success then user id else 0 |
| 402 | * |
| 403 | * @since : 1.0 |
| 404 | */ |
| 405 | |
| 406 | function xs_login_create_user($userdata) { |
| 407 | |
| 408 | // todo - permission checking removed for registering user : consult with CTO |
| 409 | |
| 410 | $user_id = wp_insert_user($userdata); |
| 411 | |
| 412 | if(!is_wp_error($user_id)) { |
| 413 | |
| 414 | update_user_meta($user_id, 'xs_password_changed', 'no'); |
| 415 | |
| 416 | return $user_id; |
| 417 | } |
| 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | |
| 423 | add_action('init', 'xs_login_create_user'); |
| 424 | |
| 425 | /** |
| 426 | * Function Name : xs_login_get_user_data(); |
| 427 | * Function Details : Get user information when user already exists into database |
| 428 | * |
| 429 | * @params : String() $loginName. User login name |
| 430 | * |
| 431 | * @return : String() User information by set filed from database table. |
| 432 | * |
| 433 | * @since : 1.0 |
| 434 | */ |
| 435 | function xs_login_get_user_data($loginName, $getFiled = 'user_login') { |
| 436 | $users = get_user_by('login', $loginName); |
| 437 | if(empty($users)) { |
| 438 | return ''; |
| 439 | } |
| 440 | |
| 441 | return $users->data->$getFiled; |
| 442 | } |
| 443 | |
| 444 | |
| 445 | /** |
| 446 | * |
| 447 | * @since 1.3.8 |
| 448 | * |
| 449 | * @param $loginName |
| 450 | * @param string $field |
| 451 | * |
| 452 | * @return string |
| 453 | */ |
| 454 | function xs_login_get_user_field($loginName, $field = 'user_login') { |
| 455 | |
| 456 | $users = get_user_by('login', $loginName); |
| 457 | |
| 458 | if(empty($users)) { |
| 459 | return ''; |
| 460 | } |
| 461 | |
| 462 | return $users->data->$field; |
| 463 | } |
| 464 | |
| 465 | |
| 466 | add_action('init', 'xs_login_get_user_data'); |
| 467 | |
| 468 | /** |
| 469 | * Function Name : xs_login_get_user_data_email(); |
| 470 | * Function Details : Get user information when email already exists into database |
| 471 | * |
| 472 | * @params : String() $email. User login name |
| 473 | * |
| 474 | * @return : String() User information by set filed from database table. |
| 475 | * |
| 476 | * @since : 1.0 |
| 477 | */ |
| 478 | function xs_login_get_user_data_email($email, $getFiled = 'user_login') { |
| 479 | $users = get_user_by('email', $email); |
| 480 | if(empty($users)) { |
| 481 | return ''; |
| 482 | } |
| 483 | |
| 484 | return $users->data->$getFiled; |
| 485 | } |
| 486 | |
| 487 | |
| 488 | add_action('init', 'xs_login_get_user_data'); |
| 489 | /** |
| 490 | * Function Name : xs_user_login(); |
| 491 | * Function Details : User login function by wp_signon(); |
| 492 | * |
| 493 | * @params : String() $user_name. User login name |
| 494 | * @params : String() $password. User password |
| 495 | * |
| 496 | * @return : True | False |
| 497 | * |
| 498 | * @since : 1.0 |
| 499 | */ |
| 500 | function xs_user_login($user_name, $redirect_to = '') { |
| 501 | if(strlen($user_name) == 0) { |
| 502 | die(esc_html__('User name is empty!', 'wp-social')); |
| 503 | } |
| 504 | |
| 505 | $username = $user_name; |
| 506 | $user = get_user_by('login', $username); |
| 507 | |
| 508 | |
| 509 | if(!is_wp_error($user)) { |
| 510 | wp_clear_auth_cookie(); |
| 511 | wp_set_current_user($user->ID); |
| 512 | wp_set_auth_cookie($user->ID); |
| 513 | |
| 514 | $redirect_to = empty($redirect_to) ? user_admin_url() : $redirect_to; |
| 515 | wp_safe_redirect($redirect_to); |
| 516 | exit(); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | |
| 521 | add_action('init', 'xs_user_login'); |
| 522 | |
| 523 | |
| 524 | /** |
| 525 | * Get file extension fro a image that is rendering from a php url |
| 526 | * |
| 527 | * @since 1.0.0 |
| 528 | * |
| 529 | * @param $url |
| 530 | * |
| 531 | * @return array|string |
| 532 | */ |
| 533 | function get_file_ext_from_url($url) { |
| 534 | |
| 535 | $extension = ''; |
| 536 | $headers = wp_get_http_headers($url); |
| 537 | $mime_type = $headers['content-type']; |
| 538 | |
| 539 | foreach(wp_get_mime_types() as $ext => $mime) { |
| 540 | if($mime == $mime_type) { |
| 541 | |
| 542 | $extension = explode('|', $ext); |
| 543 | |
| 544 | return $extension[0]; |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | return $extension; |
| 549 | } |
| 550 | |
| 551 | |
| 552 | /** |
| 553 | * Save a image php url as a attachment of post |
| 554 | * |
| 555 | * @since 1.0.0 |
| 556 | * |
| 557 | * @param $url |
| 558 | * @param string $unique_name - name with extension |
| 559 | * @param int $post_id - default 0 |
| 560 | * |
| 561 | * @return array |
| 562 | */ |
| 563 | function save_image_from_url_as_attachment($url, $unique_name = '', $post_id = 0) { |
| 564 | |
| 565 | require_once(ABSPATH . "wp-admin" . '/includes/image.php'); |
| 566 | require_once(ABSPATH . "wp-admin" . '/includes/file.php'); |
| 567 | require_once(ABSPATH . "wp-admin" . '/includes/media.php'); |
| 568 | |
| 569 | $ext = get_file_ext_from_url($url); |
| 570 | $tmp = download_url($url); |
| 571 | |
| 572 | $name = empty($unique_name) ? '__tmp_' . time() . '.' . $ext : $unique_name; |
| 573 | |
| 574 | $file_array = array( |
| 575 | 'name' => $name, |
| 576 | 'tmp_name' => $tmp, |
| 577 | ); |
| 578 | |
| 579 | |
| 580 | /** |
| 581 | * Check for download errors |
| 582 | * if there are error unlink the temp file name |
| 583 | */ |
| 584 | if(is_wp_error($tmp)) { |
| 585 | @unlink($file_array['tmp_name']); |
| 586 | |
| 587 | return [ |
| 588 | 'error' => $tmp->get_error_message(), |
| 589 | ]; |
| 590 | } |
| 591 | |
| 592 | /** |
| 593 | * now we can actually use media_handle_sideload |
| 594 | * we pass it the file array of the file to handle |
| 595 | * and the post id of the post to attach it to |
| 596 | * $post_id can be set to '0' to not attach it to any particular post |
| 597 | */ |
| 598 | $id = media_handle_sideload($file_array, $post_id); |
| 599 | |
| 600 | /** |
| 601 | * We don't want to pass something to $id |
| 602 | * if there were upload errors. |
| 603 | * So this checks for errors |
| 604 | */ |
| 605 | if(is_wp_error($id)) { |
| 606 | @unlink($file_array['tmp_name']); |
| 607 | |
| 608 | return [ |
| 609 | 'error' => $id->get_error_message(), |
| 610 | ]; |
| 611 | } |
| 612 | |
| 613 | /** |
| 614 | * No we can get the url of the sideloaded file |
| 615 | * $value now contains the file url in WordPress |
| 616 | * $id is the attachment id |
| 617 | */ |
| 618 | $value = wp_get_attachment_url($id); |
| 619 | |
| 620 | |
| 621 | return [ |
| 622 | 'url' => $value, |
| 623 | 'attachment_id' => $id, |
| 624 | ]; |
| 625 | } |
| 626 | |
| 627 | |
| 628 | function xs_login_session_handaler() { |
| 629 | |
| 630 | session_unset(); |
| 631 | |
| 632 | // do we ever need the below? |
| 633 | if(isset($_SESSION['xs_social_login_ref_url'])) { |
| 634 | unset($_SESSION['xs_social_login_ref_url']); |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | |
| 639 | /** |
| 640 | * |
| 641 | * @since 1.3.7 |
| 642 | * |
| 643 | * @param $user_info |
| 644 | * |
| 645 | * @return int |
| 646 | */ |
| 647 | function xs_social_create_user($user_info) { |
| 648 | |
| 649 | do_action('wslu_social/before_create_user'); |
| 650 | |
| 651 | /* |
| 652 | * todo - ask Ataur bhai - do we allow insert user without permission with social? it make sense to do so |
| 653 | * |
| 654 | */ |
| 655 | $getPermissionRegisterWP = get_option('users_can_register', 0); |
| 656 | |
| 657 | if($getPermissionRegisterWP == 0) { |
| 658 | |
| 659 | // return 0; |
| 660 | } |
| 661 | |
| 662 | $user_id = wp_insert_user($user_info); |
| 663 | |
| 664 | if(is_wp_error($user_id)) { |
| 665 | |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | |
| 670 | return $user_id; |
| 671 | } |
| 672 | |
| 673 | |
| 674 | /** |
| 675 | * Checking the parameter and settings to find the correct redirect url |
| 676 | * |
| 677 | * @since 1.3.8 |
| 678 | * |
| 679 | * @param $session |
| 680 | * @param $setting |
| 681 | * |
| 682 | * @return string |
| 683 | */ |
| 684 | function resolve_redirect_url($session, $setting) { |
| 685 | |
| 686 | /** |
| 687 | * First priority to wordpress default redirect_to param |
| 688 | * Second priority to custom login settings url (including 'same page' option) |
| 689 | * Third priority to XScurrentPage param |
| 690 | * And lastly site home page |
| 691 | * |
| 692 | */ |
| 693 | |
| 694 | if(!empty($session['xs_social']['redirect_to'])) { |
| 695 | |
| 696 | $final_redirect = $session['xs_social']['redirect_to']; |
| 697 | |
| 698 | } elseif(!empty($setting['custom_login_url']['enable']) && !empty($setting['custom_login_url']['data'])) { |
| 699 | |
| 700 | // Custom login redirect is enabled |
| 701 | if($setting['custom_login_url']['data'] == 'same_page') { |
| 702 | // Same page option - redirect to referrer or fallback based on origin |
| 703 | if(!empty($session['xs_social']['login_ref_url'])) { |
| 704 | $final_redirect = $session['xs_social']['login_ref_url']; |
| 705 | } else { |
| 706 | // Check if user came from wp-login or admin area |
| 707 | $referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : ''; |
| 708 | if(strpos($referer, 'wp-login') !== false || strpos($referer, 'wp-admin') !== false) { |
| 709 | $final_redirect = user_admin_url(); |
| 710 | } else { |
| 711 | $final_redirect = get_home_url(); |
| 712 | } |
| 713 | } |
| 714 | } else { |
| 715 | // Specific page URL selected |
| 716 | $final_redirect = $setting['custom_login_url']['data']; |
| 717 | } |
| 718 | |
| 719 | } elseif(!empty($session['xs_social']['login_ref_url'])) { |
| 720 | |
| 721 | $final_redirect = $session['xs_social']['login_ref_url']; |
| 722 | |
| 723 | } else { |
| 724 | |
| 725 | $final_redirect = user_admin_url(); |
| 726 | } |
| 727 | |
| 728 | return $final_redirect; |
| 729 | } |
| 730 | |
| 731 | |
| 732 | /** |
| 733 | * |
| 734 | * @since 1.3.8 |
| 735 | * |
| 736 | */ |
| 737 | function clear_social_session_data() { |
| 738 | |
| 739 | if(!empty($_SESSION['xs_social'])) { |
| 740 | |
| 741 | unset($_SESSION['xs_social']); |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | |
| 746 | /** |
| 747 | * |
| 748 | * @since 1.3.7 |
| 749 | * |
| 750 | * @param $user_id |
| 751 | * |
| 752 | * @return mixed |
| 753 | */ |
| 754 | function notify_new_user_to_admin($user_id, $social_type) { |
| 755 | |
| 756 | wp_new_user_notification($user_id, null, 'both'); |
| 757 | |
| 758 | return true; |
| 759 | } |
| 760 | |
| 761 | |
| 762 | /** |
| 763 | * |
| 764 | * @since 1.3.7 |
| 765 | * |
| 766 | * |
| 767 | * @param array $info_array |
| 768 | * |
| 769 | * @return bool |
| 770 | */ |
| 771 | function notify_new_user_to_user($info_array = []) { |
| 772 | |
| 773 | /* |
| 774 | * todo - complete it after discussion |
| 775 | */ |
| 776 | |
| 777 | return true; |
| 778 | } |
| 779 | |
| 780 | |
| 781 | function create_line_app_user($code, $socialType) { |
| 782 | |
| 783 | $lineapp = new Line_App(); |
| 784 | $user = $lineapp->get_user_info($code); |
| 785 | |
| 786 | if (empty($user->email)) { |
| 787 | die(esc_html__('Please allow line app email permission', 'wp-social')); |
| 788 | } |
| 789 | |
| 790 | $login_settings_obj = new Login_Settings(); |
| 791 | $login_global_settings = $login_settings_obj->get_global_settings(); |
| 792 | $final_redirect = (isset( $login_global_settings['custom_login_url'] ) && isset( $login_global_settings['custom_login_url']['enable'] ) && isset( $login_global_settings['custom_login_url']['data'] )) ? $login_global_settings['custom_login_url']['data'] : get_home_url() . '/wp-admin'; |
| 793 | |
| 794 | $old_user = get_user_by('email', $user->email); |
| 795 | |
| 796 | if ($old_user == false) { |
| 797 | $default_role = get_option('default_role', ''); |
| 798 | $nicename = $user->name . time(); |
| 799 | $password = wp_generate_password(); |
| 800 | |
| 801 | $insertData = [ |
| 802 | 'first_name' => $user->name, |
| 803 | 'user_nicename' => $nicename, |
| 804 | 'user_email' => $user->email, |
| 805 | 'display_name' => $user->name, |
| 806 | 'user_login' => $user->email, |
| 807 | 'user_pass' => $password, |
| 808 | 'role' => $default_role |
| 809 | ]; |
| 810 | |
| 811 | $attach = isset($user->picture) ? save_image_from_url_as_attachment($user->picture) : ''; |
| 812 | |
| 813 | $user_id = xs_social_create_user($insertData); |
| 814 | |
| 815 | if ($user_id > 0) { |
| 816 | |
| 817 | //if image not save for this user then attach will be empty |
| 818 | if( !empty($attach) ){ |
| 819 | if (empty($attach['error'])) { |
| 820 | |
| 821 | update_user_meta($user_id, 'xs_social_register_by', $socialType); |
| 822 | update_user_meta($user_id, 'xs_social_profile_image', $attach['url']); |
| 823 | update_user_meta($user_id, 'xs_social_profile_image_id', $attach['attachment_id']); |
| 824 | } else { |
| 825 | update_user_meta($user_id, 'xs_social_profile_image', ''); |
| 826 | update_user_meta($user_id, 'xs_social_profile_image_error_log', $socialType . '::' . $attach['error']); |
| 827 | } |
| 828 | } |
| 829 | $wp_social_login_settings = get_option('xs_global_setting_data'); |
| 830 | |
| 831 | if (isset($wp_social_login_settings['email_new_registered_user']['enable']) && $wp_social_login_settings['email_new_registered_user']['enable'] == 1) { |
| 832 | notify_new_user_to_admin($user_id, $socialType); |
| 833 | } |
| 834 | |
| 835 | xs_user_login($user->email, $final_redirect); |
| 836 | |
| 837 | die(esc_html__('Most most unlikely error occurred in your case. user registration done but login failed!!', 'wp-social')); |
| 838 | } |
| 839 | } else { |
| 840 | |
| 841 | $id = $old_user->data->ID; |
| 842 | |
| 843 | update_user_meta($id, 'xs_social_register_by', $socialType); |
| 844 | |
| 845 | if (get_option('wp_social_login_sync') == 'yes') { |
| 846 | |
| 847 | |
| 848 | update_user_meta($id, 'first_name', $user->name); |
| 849 | update_user_meta($id, 'display_name', $user->name); |
| 850 | |
| 851 | |
| 852 | if(get_option('wp_social_login_sync_image_too') == 'yes') { |
| 853 | $attach = save_image_from_url_as_attachment($user->picture); |
| 854 | |
| 855 | if (empty($attach['error'])) { |
| 856 | wp_delete_attachment(get_user_meta($id, 'xs_social_profile_image_id')); |
| 857 | update_user_meta($id, 'xs_social_profile_image', $attach['url']); |
| 858 | update_user_meta($id, 'xs_social_profile_image_id', $attach['attachment_id']); |
| 859 | } |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | /** |
| 864 | * Proceeding to login |
| 865 | * |
| 866 | */ |
| 867 | |
| 868 | xs_user_login($old_user->data->user_login, $final_redirect); |
| 869 | |
| 870 | die(esc_html__('Most unlikely error occurred in your case.', 'wp-social')); |
| 871 | } |
| 872 | } |