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