-
Notifications
You must be signed in to change notification settings - Fork 4
/
social-routes.php
195 lines (148 loc) · 5.04 KB
/
social-routes.php
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
class api_routes_social {
public function __init() {
add_filter( 'json_endpoints', array( $this, 'register_routes' ) );
add_action( 'delete_user', array( $this, '__user_delete' ) );
}
public function register_routes( $routes ) {
$routes['/social_login'] = array(
array( array( $this, '__social_login'), WP_JSON_Server::CREATABLE | WP_JSON_Server::ACCEPT_JSON )
);
$routes['/social_registration'] = array(
array( array( $this, '__social_registration'), WP_JSON_Server::CREATABLE | WP_JSON_Server::ACCEPT_JSON )
);
return $routes;
}
public function __social_login( $data ) {
// Expects social_id or user_email
$user = $this->__user_exists_check( $data );
if( !$user['user'] )
return new WP_Error( 'No User', __( 'Not a valid user' ), array( 'status' => 401 ) );
$user = $user['user'];
wp_set_current_user( $user->ID, $user->user_login );
wp_set_auth_cookie( $user->ID );
do_action( 'wp_login', $user->user_login, $user );
return $this->create_response( $user );
}
public function __social_registration( $data ) {
// Expects social_id, user_email, and other user_info per WP user data
$user = $this->__user_exists_check( $data );
if( $user['user'] ) {
return $this->create_response( $this->__social_login( $data ) );
}
$user_id = $this->__create_user( $data );
if( is_wp_error( $user_id ) ) {
return new WP_Error( 'Registration Error', __( $user_id->get_error_message() ), array( 'status' => 400 ) );
}
$user = get_user_by( 'id', $user_id );
wp_set_current_user( $user->ID, $user->user_login );
wp_set_auth_cookie( $user->ID );
do_action( 'wp_login', $user->user_login, $user );
return $this->create_response( $user );
}
private function __user_exists_check( $data ) {
// check if user exists in WP or DB
$return = array('user' => false );
if( isset( $data['user_email'] ) ) {
$email_check = email_exists( $data['user_email'] );
if( $email_check ) {
$db_user = $this->__user_db_check( $data['social_id'] );
if( !$db_user ) {
$this->__create_user_db( $email_check, $data['social_id'] );
}
$return['user'] = get_user_by( 'id', $email_check );
}
}
if( isset( $data['social_id'] ) && $return['user'] == false ) {
$db_user = $this->__user_db_check( $data['social_id'] );
if( $db_user ) {
$return['user'] = get_user_by( 'id', $db_user->wp_user_id );
}
}
if( !isset( $data['social_id'] ) && !isset( $data['user_email'] ) ) {
return new WP_Error( 'No Data', __( 'Expecting social_id or user_email' ), array( 'status' => 400 ) );
}
return $return;
}
private function __user_db_check( $social_id ) {
// Check wp_social_api table for user
global $wpdb;
$table_name = $wpdb->prefix . 'wp_api_social';
$db_user_row = $wpdb->get_row( $wpdb->prepare(
"SELECT * FROM $table_name WHERE social_id = %s ",
$social_id
));
return $db_user_row;
}
public function __create_user( $data ) {
$random_password = wp_generate_password( $length=12, $include_standard_special_chars=false );
if( isset( $data['nickname'] ) ) {
$username = str_replace( ' ', '_', $data['nickname'] );
} else {
$username = 'social_'.$data['social_id'];
}
$username = strtolower( $username );
if( isset( $data['user_email'] ) ) {
$user_email = $data['user_email'];
} else {
$user_email = $data['social_id'].'@'.$_SERVER['SERVER_NAME'];
}
$user_id = wp_create_user( $username, $random_password, $user_email );
if( is_wp_error( $user_id ) ) {
return new WP_Error( 'Create Error', __( $user_id->get_error_message() ), array( 'status' => 401 ) );
}
$user_update = array( 'ID' => $user_id );
if( isset( $data['first_name'] ) ) {
$user_update['first_name'] = $data['first_name'];
}
if( isset( $data['last_name'] ) ) {
$user_update['last_name'] = $data['last_name'];
}
if( isset( $data['description'] ) ) {
$user_update['description'] = $data['description'];
}
if( isset( $data['nickname'] ) ) {
$user_update['user_nicename'] = $data['nickname'];
}
$update_user = wp_update_user( $user_update );
if( is_wp_error( $update_user ) ) {
return new WP_Error( 'Update Error', __( $update_user->get_error_message() ), array( 'status' => 400 ) );
}
$this->__create_user_db( $user_id, $data['social_id'] );
return $user_id;
}
private function __create_user_db( $user_id, $social_id ) {
global $wpdb;
$table_name = $wpdb->prefix . 'wp_api_social';
$db = $wpdb->insert(
$table_name,
array(
'created_time' => current_time( 'mysql' ),
'social_id' => $social_id,
'wp_user_id' => $user_id
),
array(
'%s',
'%d',
'%d'
));
}
public function __user_delete( $user_id ) {
global $wpdb;
$table_name = $wpdb->prefix . 'wp_api_social';
$db = $wpdb->delete(
$table_name,
array(
'wp_user_id' => $user_id
),
array(
'%d',
));
}
private function create_response( $return ) {
$response = new WP_JSON_Response();
$response->set_data( $return );
return $response;
}
}
?>