-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallback.php
More file actions
101 lines (84 loc) · 4.29 KB
/
callback.php
File metadata and controls
101 lines (84 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
require_once '../../../wp-load.php';
if (!isset($_GET['access_token'])) {
wp_safe_redirect(home_url('?jam_no_token'));
die;
}
require_once JAM_PLUGIN_DIR . 'class/apicall.class.php';
$settings = JustAuthMe::get()->fetchSettings();
$apiCall = new ApiCall();
$apiCall->setUrl('https://core.justauth.me/api/data?access_token=' . $_GET['access_token'] . '&secret=' . $settings['secret'])
->exec();
$obj = $apiCall->responseObj();
global $wpdb;
$sql = $wpdb->prepare("SELECT COUNT(*) AS cnt FROM " . JustAuthMe::get()->getUserTableName() . " WHERE jam_id = %s", $obj->jam_id);
$cnt = (int) $wpdb->get_results($sql)[0]->cnt;
if ($cnt > 0) {
$sql = $wpdb->prepare("SELECT user_id FROM " . JustAuthMe::get()->getUserTableName() . " WHERE jam_id = %s", $obj->jam_id);
$uid = (int) $wpdb->get_results($sql)[0]->user_id;
JustAuthMe::get()->login($uid);
} else {
if (isset($obj->email)) {
$user = get_user_by_email($obj->email);
if ($user !== false) {
$sql = $wpdb->prepare("INSERT INTO " . JustAuthMe::get()->getUserTableName() . " (user_id, jam_id) VALUES(%d, %s)", [$user->ID, $obj->jam_id]);
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta($sql);
JustAuthMe::get()->login($user->ID);
} elseif (get_option('users_can_register')) {
$email_local = explode('@', $obj->email)[0];
$i = '';
do {
$username = $email_local . $i;
$i++;
} while (get_user_by('slug', $username) !== false);
$passwd = wp_generate_password(32, true, true);
$userdata = [];
if (isset($obj->firstname)) {
$userdata['first_name'] = $obj->firstname;
}
if (isset($obj->lastname)) {
$userdata['last_name'] = $obj->lastname;
}
if (defined('WC_VERSION')) {
$uid = wc_create_new_customer($obj->email, $username, $passwd, $userdata);
WC()->customer = new WC_Customer($uid);
WC()->customer->set_props([
'billing_first_name' => $obj->firstname ? $obj->firstname : '',
'billing_last_name' => $obj->lastname ? $obj->lastname : '',
'billing_address_1' => $obj->address_1 ? $obj->address_1 : '',
'billing_address_2' => $obj->address_2 ? $obj->address_2 : '',
'billing_city' => $obj->city ? $obj->city : '',
'billing_postcode' => $obj->postal_code ? $obj->postal_code : '',
'billing_country' => $obj->country ? $obj->country : '',
'billing_state' => $obj->state ? $obj->state : '',
'billing_email' => $obj->email,
'shipping_first_name' => $obj->firstname ? $obj->firstname : '',
'shipping_last_name' => $obj->lastname ? $obj->lastname : '',
'shipping_address_1' => $obj->address_1 ? $obj->address_1 : '',
'shipping_address_2' => $obj->address_2 ? $obj->address_2 : '',
'shipping_city' => $obj->city ? $obj->city : '',
'shipping_postcode' => $obj->postal_code ? $obj->postal_code : '',
'shipping_country' => $obj->country ? $obj->country : '',
'shipping_state' => $obj->state ? $obj->state : ''
]);
WC()->customer->save();
} else {
$userdata['user_login'] = $username;
$userdata['user_email'] = $obj->email;
$userdata['user_pass'] = $passwd;
$uid = wp_insert_user($userdata);
}
$sql = $wpdb->prepare("INSERT INTO " . JustAuthMe::get()->getUserTableName() . " (user_id, jam_id) VALUES(%d, %s)", [$uid, $obj->jam_id]);
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta($sql);
JustAuthMe::get()->login($uid);
} else {
wp_safe_redirect(home_url('?jam_cant_register'));
die;
}
} else {
wp_safe_redirect(home_url('?jam_no_email'));
die;
}
}