forked from 2Fwebd/alka-facebook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alka-facebook.php
376 lines (300 loc) · 8.63 KB
/
alka-facebook.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
/**
* Plugin Name: Alka Facebook
* Description: Login and Register your users using Facebook's API
* Version: 1.0.0
* Author: Alkaweb
* Author URI: http://alka-web.com
* Text Domain: alkaweb
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* GitHub Plugin URI: https://github.com/2Fwebd/alka-facebook
*/
/*
* Import the Facebook SDK and load all the classes
*/
include( plugin_dir_path( __FILE__ ) . 'facebook-sdk/autoload.php' );
/*
* Classes required to call the Facebook API
* They will be used by our class
*/
use Facebook\Facebook;
use Facebook\Exceptions\FacebookSDKException;
use Facebook\Exceptions\FacebookResponseException;
/**
* Class AlkaFacebook
*/
class AlkaFacebook {
/**
* Facebook APP ID
*
* @var string
*/
private $app_id = 'xxxxxxxxxxxxxx';
/**
* Facebook APP Secret
*
* @var string
*/
private $app_secret = 'xxxxxxxxxxxxxx';
/**
* Callback URL used by the API
*
* @var string
*/
private $callback_url = 'http://your-site.com/wp-admin/admin-ajax.php?action=alka_facebook';
/**
* Access token from Facebook
*
* @var string
*/
private $access_token;
/**
* Where we redirect our user after the process
*
* @var string
*/
private $redirect_url;
/**
* User details from the API
*/
private $facebook_details;
/**
* AlkaFacebook constructor.
*/
public function __construct() {
$facebook_creeds = get_option( 'alka_facebook', array() );
if ( ! empty( $facebook_creeds['app_id'] ) ) {
$this->app_id = $facebook_creeds['app_id'];
}
if ( ! empty( $facebook_creeds['app_secret'] ) ) {
$this->app_secret = $facebook_creeds['app_secret'];
}
// We register our shortcode
add_shortcode( 'alka_facebook', array( $this, 'renderShortcode' ) );
// Callback URL
add_action( 'wp_ajax_alka_facebook', array( $this, 'apiCallback' ) );
add_action( 'wp_ajax_nopriv_alka_facebook', array( $this, 'apiCallback' ) );
if ( is_admin() ) {
require_once 'admin/alka-facebook-admin.php';
}
}
/**
* Render the shortcode [alka_facebook/]
*
* It displays our Login / Register button
*/
public function renderShortcode() {
// No need for the button is the user is already logged
if ( is_user_logged_in() ) {
return;
}
// Start the session
if ( ! session_id() ) {
@session_start();
}
// We save the URL for the redirection:
if ( ! isset( $_SESSION['alka_facebook_url'] ) ) {
$_SESSION['alka_facebook_url'] = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}
// Different labels according to whether the user is allowed to register or not
if ( get_option( 'users_can_register' ) ) {
$button_label = __( 'Login or Register with Facebook', 'alkaweb' );
} else {
$button_label = __( 'Login with Facebook', 'alkaweb' );
}
// HTML markup
$html = '<div id="alka-facebook-wrapper">';
// Messages
if ( isset( $_SESSION['alka_facebook_message'] ) ) {
$message = $_SESSION['alka_facebook_message'];
$html .= '<div id="alka-facebook-message" class="alert alert-danger">' . $message . '</div>';
// We remove them from the session
unset( $_SESSION['alka_facebook_message'] );
}
// Button
$html .= '<a href="' . $this->getLoginUrl() . '" class="btn" id="alka-facebook-button">' . $button_label . '</a>';
$html .= '</div>';
// Write it down
return $html;
}
/**
* Init the API Connection
*
* @return Facebook
*/
private function initApi() {
$facebook = new Facebook( [
'app_id' => $this->app_id,
'app_secret' => $this->app_secret,
'default_graph_version' => 'v2.2',
'persistent_data_handler' => 'session'
] );
return $facebook;
}
/**
* Login URL to Facebook API
*
* @return string
*/
private function getLoginUrl() {
if ( ! session_id() ) {
session_start();
}
$fb = $this->initApi();
$helper = $fb->getRedirectLoginHelper();
// Optional permissions
$permissions = [ 'email' ];
$url = $helper->getLoginUrl( $this->callback_url, $permissions );
return esc_url( $url );
}
/**
* API call back running whenever we hit /wp-admin/admin-ajax.php?action=alka_facebook
* This code handles the Login / Regsitration part
*/
public function apiCallback() {
if ( ! session_id() ) {
session_start();
}
// Set the Redirect URL:
$this->redirect_url = ( isset( $_SESSION['alka_facebook_url'] ) ) ? $_SESSION['alka_facebook_url'] : home_url();
// We start the connection
$fb = $this->initApi();
// We save the token in our instance
$this->access_token = $this->getToken( $fb );
// We get the user details
$this->facebook_details = $this->getUserDetails( $fb );
// We first try to login the user
$this->loginUser();
// Otherwise, we create a new account
$this->createUser();
// Redirect the user
header( "Location: " . $this->redirect_url, true );
die();
}
/**
* Get a TOKEN from the Facebook API
* Or redirect back if there is an error
*
* @param $fb Facebook
*
* @return string - The Token
*/
private function getToken( $fb ) {
// Assign the Session variable for Facebook
$_SESSION['FBRLH_state'] = $_GET['state'];
// Load the Facebook SDK helper
$helper = $fb->getRedirectLoginHelper();
// Try to get an access token
try {
$accessToken = $helper->getAccessToken();
} // When Graph returns an error
catch ( FacebookResponseException $e ) {
$error = __( 'Graph returned an error: ', 'alkaweb' ) . $e->getMessage();
$message = array(
'type' => 'error',
'content' => $error
);
} // When validation fails or other local issues
catch ( FacebookSDKException $e ) {
$error = __( 'Facebook SDK returned an error: ', 'alkaweb' ) . $e->getMessage();
$message = array(
'type' => 'error',
'content' => $error,
);
}
// If we don't got a token, it means we had an error
if ( ! isset( $accessToken ) ) {
// Report our errors
$_SESSION['alka_facebook_message'] = $message;
// Redirect
header( "Location: " . $this->redirect_url, true );
die();
}
return $accessToken->getValue();
}
/**
* Get user details through the Facebook API
*
* @link https://developers.facebook.com/docs/facebook-login/permissions#reference-public_profile
*
* @param $fb Facebook
*
* @return \Facebook\GraphNodes\GraphUser
*/
private function getUserDetails( $fb ) {
try {
$response = $fb->get( '/me?fields=id,name,first_name,last_name,email,link', $this->access_token );
} catch ( FacebookResponseException $e ) {
$error = __( 'Graph returned an error: ', 'alkaweb' ) . $e->getMessage();
$message = array(
'type' => 'error',
'content' => $error,
);
} catch ( FacebookSDKException $e ) {
$error = __( 'Facebook SDK returned an error: ', 'alkaweb' ) . $e->getMessage();
$message = array(
'type' => 'error',
'content' => $error,
);
}
// If we caught an error
if ( isset( $message ) ) {
// Report our errors
$_SESSION['alka_facebook_message'] = $message;
// Redirect
header( "Location: " . $this->redirect_url, true );
die();
}
return $response->getGraphUser();
}
/**
* Login an user to WordPress
*
* @link https://codex.wordpress.org/Function_Reference/get_users
* @return bool|void
*/
private function loginUser() {
// We look for the `eo_facebook_id` to see if there is any match
$wp_users = get_users( array(
'meta_key' => 'alka_facebook_id',
'meta_value' => $this->facebook_details['id'],
'number' => 1,
'count_total' => false,
'fields' => 'id',
) );
if ( empty( $wp_users[0] ) ) {
return false;
}
// Log the user ?
wp_set_auth_cookie( $wp_users[0] );
}
/**
* Create a new WordPress account using Facebook Details
*/
private function createUser() {
$fb_user = $this->facebook_details;
// Create an username
$username = sanitize_user( str_replace( ' ', '_', strtolower( $this->facebook_details['name'] ) ) );
// Creating our user
$new_user = wp_create_user( $username, wp_generate_password(), $fb_user['email'] );
if ( is_wp_error( $new_user ) ) {
// Report our errors
$_SESSION['alka_facebook_message'] = $new_user->get_error_message();
// Redirect
header( "Location: " . $this->redirect_url, true );
die();
}
// Setting the meta
update_user_meta( $new_user, 'first_name', $fb_user['first_name'] );
update_user_meta( $new_user, 'last_name', $fb_user['last_name'] );
update_user_meta( $new_user, 'user_url', $fb_user['link'] );
update_user_meta( $new_user, 'alka_facebook_id', $fb_user['id'] );
// Log the user ?
wp_set_auth_cookie( $new_user );
}
}
/*
* Starts our plugins, easy!
*/
new AlkaFacebook();