| 1 |
<?php |
| 2 |
|
| 3 |
namespace Gianism\Service; |
| 4 |
use Facebook\GraphNodes\GraphUser; |
| 5 |
use Facebook\SignedRequest; |
| 6 |
use Gianism\Helper\FacebookCookiePersistentDataHandler; |
| 7 |
|
| 8 |
/** |
| 9 |
* Description of facebook_controller |
| 10 |
* |
| 11 |
* @package Gianism |
| 12 |
* @since 2.0.0 |
| 13 |
* @author Takahashi Fumiki |
| 14 |
* @property-read \Facebook\Facebook|\WP_Error $api Facebook object |
| 15 |
* @property-read \Facebook\Facebook|\WP_Error $admin Facebook object for Admin Use |
| 16 |
* @property-read int|string $admin_id |
| 17 |
* @property-read array|false $admin_account |
| 18 |
* @property-read array $admin_pages |
| 19 |
*/ |
| 20 |
class Facebook extends NoMailService { |
| 21 |
|
| 22 |
/** |
| 23 |
* Service name to display |
| 24 |
* |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
public $verbose_service_name = 'Facebook'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Get SVG path. |
| 31 |
* |
| 32 |
* @return string |
| 33 |
*/ |
| 34 |
public function svg_path() { |
| 35 |
return 'facebook-white.png'; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* @var string Minimum graph api version |
| 40 |
*/ |
| 41 |
public $minimum_api_version = 'v6.0'; |
| 42 |
|
| 43 |
/** |
| 44 |
* Facebook app version |
| 45 |
* |
| 46 |
* @var string |
| 47 |
*/ |
| 48 |
public $fb_version = ''; |
| 49 |
|
| 50 |
/** |
| 51 |
* Facebook application ID |
| 52 |
* |
| 53 |
* @var string |
| 54 |
*/ |
| 55 |
public $fb_app_id = ''; |
| 56 |
|
| 57 |
/** |
| 58 |
* Facebook application secret |
| 59 |
* |
| 60 |
* @var string |
| 61 |
*/ |
| 62 |
public $fb_app_secret = ''; |
| 63 |
|
| 64 |
/** |
| 65 |
* Whether if use global setting |
| 66 |
* |
| 67 |
* @var bool |
| 68 |
*/ |
| 69 |
public $fb_use_api = false; |
| 70 |
|
| 71 |
/** |
| 72 |
* Facebook API Controller |
| 73 |
* |
| 74 |
* @var \Facebook\Facebook |
| 75 |
*/ |
| 76 |
private $_api = null; |
| 77 |
|
| 78 |
/** |
| 79 |
* Facebook API for user |
| 80 |
* |
| 81 |
* @var \Facebook\Facebook |
| 82 |
*/ |
| 83 |
private $_admin_api = null; |
| 84 |
|
| 85 |
/** |
| 86 |
* Meta key of user_meta for facebook id |
| 87 |
* |
| 88 |
* @var string |
| 89 |
*/ |
| 90 |
public $umeta_id = '_wpg_facebook_id'; |
| 91 |
|
| 92 |
/** |
| 93 |
* Meta key of usermeta for facebook mail |
| 94 |
* |
| 95 |
* @var string |
| 96 |
*/ |
| 97 |
public $umeta_mail = '_wpg_facebook_mail'; |
| 98 |
|
| 99 |
/** |
| 100 |
* Meta key of usermeta for Facebook access token |
| 101 |
* |
| 102 |
* @var string |
| 103 |
*/ |
| 104 |
public $umeta_token = '_wpg_facebook_access_token'; |
| 105 |
|
| 106 |
/** |
| 107 |
* Pseudo email address |
| 108 |
* |
| 109 |
* @var string |
| 110 |
*/ |
| 111 |
protected $pseudo_domain = 'pseudo.facebook.com'; |
| 112 |
|
| 113 |
/** |
| 114 |
* @var array |
| 115 |
*/ |
| 116 |
private $_signed_request = array(); |
| 117 |
|
| 118 |
/** |
| 119 |
* Key to retrieve |
| 120 |
* |
| 121 |
* @var array |
| 122 |
*/ |
| 123 |
protected $option_keys = [ |
| 124 |
'fb_enabled' => false, |
| 125 |
'fb_app_id' => '', |
| 126 |
'fb_app_secret' => '', |
| 127 |
'fb_use_api' => false, |
| 128 |
'fb_version' => '', |
| 129 |
]; |
| 130 |
|
| 131 |
/** |
| 132 |
* Init action |
| 133 |
*/ |
| 134 |
protected function init_action() { |
| 135 |
// Update option |
| 136 |
if ( $this->fb_use_api ) { |
| 137 |
// Save action |
| 138 |
add_action( 'admin_init', array( $this, 'update_facebook_admin' ) ); |
| 139 |
// Add view for API |
| 140 |
add_filter( |
| 141 |
'gianism_setting_screen_views', |
| 142 |
function ( $views, $slug ) { |
| 143 |
if ( 'gianism' === $slug ) { |
| 144 |
$views['fb-api'] = sprintf( '<i class="lsf lsf-facebook"></i> %s', $this->_( 'Facebook API' ) ); |
| 145 |
} |
| 146 |
return $views; |
| 147 |
}, |
| 148 |
10, |
| 149 |
2 |
| 150 |
); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
/** |
| 156 |
* Disconnect user from this service |
| 157 |
* |
| 158 |
* @param int $user_id |
| 159 |
* |
| 160 |
* @return void |
| 161 |
*/ |
| 162 |
public function disconnect( $user_id ) { |
| 163 |
delete_user_meta( $user_id, $this->umeta_id ); |
| 164 |
delete_user_meta( $user_id, $this->umeta_mail ); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Returns API endpoint |
| 169 |
* |
| 170 |
* @param string $action |
| 171 |
* @throws \Exception |
| 172 |
* @return bool|false|string |
| 173 |
*/ |
| 174 |
protected function get_api_url( $action ) { |
| 175 |
switch ( $action ) { |
| 176 |
case 'connect': |
| 177 |
case 'login': |
| 178 |
$permission = [ 'email' ]; |
| 179 |
break; |
| 180 |
case 'publish': |
| 181 |
$permission = [ 'publish_actions' ]; |
| 182 |
break; |
| 183 |
case 'admin': |
| 184 |
$permission = [ 'manage_pages' ]; |
| 185 |
break; |
| 186 |
default: |
| 187 |
$permission = []; |
| 188 |
break; |
| 189 |
} |
| 190 |
/** |
| 191 |
* Permission for Facebook |
| 192 |
* |
| 193 |
* @since 3.0.0 |
| 194 |
* @see https://developers.facebook.com/docs/facebook-login/permissions/ |
| 195 |
* @filter gianism_facebook_permissions |
| 196 |
* @param array $permission |
| 197 |
* @param string $scope |
| 198 |
* @return array |
| 199 |
*/ |
| 200 |
$permission = apply_filters( 'gianism_facebook_permissions', $permission, $action ); |
| 201 |
if ( ! $permission ) { |
| 202 |
return false; |
| 203 |
} |
| 204 |
$helper = $this->api->getRedirectLoginHelper(); |
| 205 |
return $helper->getLoginUrl( $this->get_redirect_endpoint(), $permission ); |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Handle publish action |
| 210 |
* |
| 211 |
* @param \WP_Query $wp_query |
| 212 |
*/ |
| 213 |
public function handle_publish( \WP_Query $wp_query ) { |
| 214 |
try { |
| 215 |
$url = $this->get_api_url( 'publish' ); |
| 216 |
$this->session->write( 'redirect_to', $this->input->get( 'redirect_to' ) ); |
| 217 |
$this->session->write( 'action', 'publish' ); |
| 218 |
$this->session->write( 'hook', $this->input->get( 'hook' ) ); |
| 219 |
$this->session->write( 'args', $_GET ); |
| 220 |
wp_redirect( $url ); |
| 221 |
exit; |
| 222 |
} catch ( \Exception $e ) { |
| 223 |
$this->input->wp_die( $e->getMessage() ); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* @param \WP_Query $wp_query |
| 229 |
*/ |
| 230 |
public function handle_admin( \WP_Query $wp_query ) { |
| 231 |
try { |
| 232 |
if ( $this->input->request( 'publish' ) ) { |
| 233 |
add_filter( |
| 234 |
'gianism_facebook_permissions', |
| 235 |
function ( $permission, $action ) { |
| 236 |
if ( 'admin' === $action ) { |
| 237 |
$permission[] = 'publish_actions'; |
| 238 |
} |
| 239 |
return $permission; |
| 240 |
}, |
| 241 |
9, |
| 242 |
2 |
| 243 |
); |
| 244 |
} |
| 245 |
$url = $this->get_api_url( 'admin' ); |
| 246 |
$this->session->write( 'redirect_to', $this->input->get( 'redirect_to' ) ); |
| 247 |
$this->session->write( 'action', 'admin' ); |
| 248 |
wp_redirect( $url ); |
| 249 |
exit; |
| 250 |
} catch ( \Exception $e ) { |
| 251 |
$this->input->wp_die( $e->getMessage() ); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Communicate with Facebook API |
| 257 |
* |
| 258 |
* @global \wpdb $wpdb |
| 259 |
* |
| 260 |
* @param string $action |
| 261 |
* |
| 262 |
* @return void |
| 263 |
*/ |
| 264 |
protected function handle_default( $action ) { |
| 265 |
global $wpdb; |
| 266 |
// Get common values |
| 267 |
$redirect_url = (string) $this->session->get( 'redirect_to' ); |
| 268 |
// Process actions |
| 269 |
switch ( $action ) { |
| 270 |
case 'login': // Make user login |
| 271 |
try { |
| 272 |
// Is logged in? |
| 273 |
if ( is_user_logged_in() ) { |
| 274 |
throw new \Exception( $this->_( 'You are already logged in' ) ); |
| 275 |
} |
| 276 |
// Get user ID |
| 277 |
$user = $this->get_returned_user(); |
| 278 |
// If user doesn't exist, try to register. |
| 279 |
$user_id = $this->get_meta_owner( $this->umeta_id, $user['id'] ); |
| 280 |
if ( ! $user_id ) { |
| 281 |
// Test |
| 282 |
$this->test_user_can_register(); |
| 283 |
// Check email |
| 284 |
if ( isset( $user['email'] ) && is_email( $user['email'] ) ) { |
| 285 |
$email = (string) $user['email']; |
| 286 |
} else { |
| 287 |
$email = $this->create_pseudo_email( $user['id'] ); |
| 288 |
} |
| 289 |
// Does mail duplicated? |
| 290 |
if ( $this->mail_owner( $email ) ) { |
| 291 |
throw new \Exception( $this->duplicate_account_string() ); |
| 292 |
} |
| 293 |
/** |
| 294 |
* Filter default user login name on register. |
| 295 |
* |
| 296 |
* There might be no available string for login name, so use Facebook id for login. |
| 297 |
* |
| 298 |
* @filter gianism_register_name |
| 299 |
* @param string $user_login user login name. |
| 300 |
* @param string $service Service name. |
| 301 |
* @param mixed $user User data or something. It varies by service. |
| 302 |
*/ |
| 303 |
$user_name = apply_filters( 'gianism_register_name', 'fb-' . $user['id'], $this->service_name, $user ); |
| 304 |
// Check if username exists |
| 305 |
$user_id = wp_create_user( $user_name, wp_generate_password(), $email ); |
| 306 |
if ( is_wp_error( $user_id ) ) { |
| 307 |
throw new \Exception( $this->registration_error_string() ); |
| 308 |
} |
| 309 |
// Ok, let's update user meta |
| 310 |
$wpdb->update( |
| 311 |
$wpdb->users, |
| 312 |
[ |
| 313 |
'display_name' => sanitize_text_field( $user['name'] ), |
| 314 |
// 'user_url' => $user['link'], // Deprecated because of REST API 3 udpdate: https://developers.facebook.com/blog/post/2018/05/01/enhanced-developer-app-review-and-graph-api-3.0-now-live/ |
| 315 |
], |
| 316 |
[ |
| 317 |
'ID' => $user_id, |
| 318 |
], |
| 319 |
[ '%s', '%s' ], |
| 320 |
[ '%d' ] |
| 321 |
); |
| 322 |
foreach ( [ |
| 323 |
$this->umeta_id => $user['id'], |
| 324 |
$this->umeta_mail => $email, |
| 325 |
'nickname' => sanitize_text_field( $user['name'] ), |
| 326 |
'first_name' => sanitize_text_field( $user['first_name'] ), |
| 327 |
'last_name' => sanitize_text_field( $user['last_name'] ), |
| 328 |
] as $key => $value ) { |
| 329 |
update_user_meta( $user_id, $key, $value ); |
| 330 |
} |
| 331 |
$this->user_password_unknown( $user_id ); |
| 332 |
$this->hook_connect( $user_id, $user, true ); |
| 333 |
$this->welcome( $user['name'] ); |
| 334 |
} |
| 335 |
// Make user logged in |
| 336 |
$this->set_auth_cookie( $user_id ); |
| 337 |
$redirect_url = $this->filter_redirect( $redirect_url, 'login' ); |
| 338 |
} catch ( \Exception $e ) { |
| 339 |
$this->auth_fail( $e->getMessage() ); |
| 340 |
$redirect_url = $this->filter_redirect( wp_login_url( $redirect_url, true ), 'login-failure' ); |
| 341 |
} |
| 342 |
// Redirect user |
| 343 |
wp_redirect( $redirect_url ); |
| 344 |
exit; |
| 345 |
break; |
| 346 |
case 'connect': // Connect user account to Facebook |
| 347 |
// Connection finished. Let's redirect. |
| 348 |
if ( ! $redirect_url ) { |
| 349 |
$redirect_url = admin_url( 'profile.php' ); |
| 350 |
} |
| 351 |
try { |
| 352 |
// Get user ID |
| 353 |
$user = $this->get_returned_user(); |
| 354 |
// Check email |
| 355 |
if ( ! isset( $user['email'] ) || ! is_email( $user['email'] ) ) { |
| 356 |
throw new \Exception( $this->mail_fail_string() ); |
| 357 |
} |
| 358 |
$email = $user['email']; |
| 359 |
$email_owner = $this->mail_owner( $email ); |
| 360 |
// Check if other user has these as meta_value |
| 361 |
if ( $email_owner && get_current_user_id() !== $email_owner ) { |
| 362 |
throw new \Exception( $this->duplicate_account_string() ); |
| 363 |
} |
| 364 |
// Now let's save user_data |
| 365 |
update_user_meta( get_current_user_id(), $this->umeta_id, $user['id'] ); |
| 366 |
update_user_meta( get_current_user_id(), $this->umeta_mail, $email ); |
| 367 |
// Fires hook |
| 368 |
$this->hook_connect( get_current_user_id(), $this->api ); |
| 369 |
// Save message |
| 370 |
$this->welcome( $user['name'] ); |
| 371 |
$redirect_url = $this->filter_redirect( $redirect_url, 'connect' ); |
| 372 |
} catch ( \Exception $e ) { |
| 373 |
$this->auth_fail( $e->getMessage() ); |
| 374 |
$redirect_url = $this->filter_redirect( $redirect_url, 'connect-failure' ); |
| 375 |
} |
| 376 |
wp_redirect( $redirect_url ); |
| 377 |
exit; |
| 378 |
break; |
| 379 |
case 'publish': |
| 380 |
try { |
| 381 |
$hook = $this->session->get( 'hook' ); |
| 382 |
$args = $this->session->get( 'args' ); |
| 383 |
$user = $this->get_returned_user(); |
| 384 |
// Check permission exists, save it. |
| 385 |
$token = $this->api->getRedirectLoginHelper()->getAccessToken( $this->get_redirect_endpoint() ); |
| 386 |
$perms = $this->api->get( '/me/permissions', $token ); |
| 387 |
// Get hook |
| 388 |
if ( $perms && isset( $perms['data'][0]['publish_actions'] ) && $perms['data'][0]['publish_actions'] ) { |
| 389 |
// Save access token. |
| 390 |
update_user_meta( get_current_user_id(), $this->umeta_token, $token ); |
| 391 |
// If action is set, do it. |
| 392 |
if ( ! empty( $hook ) ) { |
| 393 |
do_action( (string) $hook, $this->api, $args, $token ); |
| 394 |
} |
| 395 |
} |
| 396 |
} catch ( \Exception $e ) { |
| 397 |
$this->add_message( $e->getMessage(), true ); |
| 398 |
} |
| 399 |
wp_redirect( $redirect_url ); |
| 400 |
exit; |
| 401 |
break; |
| 402 |
case 'admin': |
| 403 |
try { |
| 404 |
$helper = $this->api->getRedirectLoginHelper(); |
| 405 |
$token = $helper->getAccessToken( $this->get_redirect_endpoint() ); |
| 406 |
$oauth = $this->api->getOAuth2Client(); |
| 407 |
$long_token = $oauth->getLongLivedAccessToken( $token ); |
| 408 |
// O.K. Token ready and save it. |
| 409 |
update_option( 'gianism_facebook_admin_token', $long_token ); |
| 410 |
// phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested |
| 411 |
update_option( 'gianism_facebook_admin_refreshed', current_time( 'timestamp' ) ); |
| 412 |
$this->add_message( $this->_( 'Access token is saved.' ) ); |
| 413 |
} catch ( \Exception $e ) { |
| 414 |
$this->add_message( $e->getMessage(), true ); |
| 415 |
} |
| 416 |
wp_redirect( $redirect_url ); |
| 417 |
exit; |
| 418 |
break; |
| 419 |
default: |
| 420 |
/** |
| 421 |
* Do something if no action is set |
| 422 |
* |
| 423 |
* @action gianism_extra_action |
| 424 |
* @param string $service_name facebook, google, etc. |
| 425 |
* @param string $action |
| 426 |
* @param array{redirect_to:string} $args |
| 427 |
*/ |
| 428 |
do_action( |
| 429 |
'gianism_extra_action', |
| 430 |
$this->service_name, |
| 431 |
$action, |
| 432 |
[ |
| 433 |
'redirect_to' => $redirect_url, |
| 434 |
] |
| 435 |
); |
| 436 |
$this->input->wp_die( sprintf( $this->_( 'Sorry, but wrong access. Please go back to <a href="%s">%s</a>.' ), home_url( '/' ), get_bloginfo( 'name' ) ), 500, false ); |
| 437 |
break; |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
/** |
| 442 |
* Returns login url which get additional permission |
| 443 |
* |
| 444 |
* @param string $redirect_url |
| 445 |
* @param string $action This action hook will booted. |
| 446 |
* @param array $args Additional key-value |
| 447 |
* |
| 448 |
* @return string |
| 449 |
*/ |
| 450 |
public function get_publish_permission_link( $redirect_url = null, $action = '', $args = array() ) { |
| 451 |
if ( ! $redirect_url ) { |
| 452 |
$redirect_url = admin_url( 'profile.php' ); |
| 453 |
} |
| 454 |
$arguments = array( |
| 455 |
'redirect_to' => $redirect_url, |
| 456 |
); |
| 457 |
if ( ! empty( $action ) ) { |
| 458 |
$arguments['hook'] = $action; |
| 459 |
} |
| 460 |
|
| 461 |
return $this->get_redirect_endpoint( 'publish', $this->service_name . '_publish', array_merge( $arguments, $args ) ); |
| 462 |
} |
| 463 |
|
| 464 |
/** |
| 465 |
* Get admin connect link |
| 466 |
* |
| 467 |
* @param bool $require_publish |
| 468 |
* |
| 469 |
* @return string |
| 470 |
*/ |
| 471 |
public function get_admin_connect_link( $require_publish = false ) { |
| 472 |
$arguments = array( |
| 473 |
'redirect_to' => admin_url( 'options-general.php?page=gianism&view=fb-api' ), |
| 474 |
); |
| 475 |
if ( $require_publish ) { |
| 476 |
$arguments['publish'] = 'true'; |
| 477 |
} |
| 478 |
|
| 479 |
return $this->get_redirect_endpoint( 'admin', $this->service_name . '_admin', $arguments ); |
| 480 |
} |
| 481 |
|
| 482 |
/** |
| 483 |
* Update admin account id. |
| 484 |
*/ |
| 485 |
public function update_facebook_admin() { |
| 486 |
if ( current_user_can( 'manage_options' ) && 'gianism' === $this->input->get( 'page' ) && wp_verify_nonce( $this->input->post( '_wpnonce' ), 'gianism_fb_account' ) ) { |
| 487 |
update_option( 'gianism_facebook_admin_id', sanitize_text_field( $this->input->post( 'fb_account_id' ) ) ); |
| 488 |
$this->add_message( $this->_( 'Saved facebook account to use.' ) ); |
| 489 |
wp_redirect( admin_url( 'options-general.php?page=gianism&view=fb-api' ) ); |
| 490 |
exit; |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
/** |
| 495 |
* Get returned user |
| 496 |
* |
| 497 |
* @return GraphUser|null |
| 498 |
* @throws \Exception |
| 499 |
*/ |
| 500 |
public function get_returned_user() { |
| 501 |
$redirect_helper = $this->api->getRedirectLoginHelper(); |
| 502 |
$access_token = $redirect_helper->getAccessToken( $this->get_redirect_endpoint() ); |
| 503 |
if ( ! $access_token ) { |
| 504 |
throw new \Exception( $redirect_helper->getError(), $redirect_helper->getErrorCode() ); |
| 505 |
} |
| 506 |
$user = $this->get_user_profile( 'login', $access_token ); |
| 507 |
if ( ! $user ) { |
| 508 |
throw new \Exception( $this->_( 'Sorry, but failed to get user data.' ), 500 ); |
| 509 |
} |
| 510 |
return $user; |
| 511 |
} |
| 512 |
|
| 513 |
/** |
| 514 |
* Return user id of email if exists |
| 515 |
* |
| 516 |
* @param string $email |
| 517 |
* |
| 518 |
* @return int |
| 519 |
*/ |
| 520 |
public function mail_owner( $email ) { |
| 521 |
$owner = email_exists( $email ); |
| 522 |
if ( $owner ) { |
| 523 |
return $owner; |
| 524 |
} |
| 525 |
return $this->get_meta_owner( $this->umeta_mail, $email ); |
| 526 |
} |
| 527 |
|
| 528 |
/** |
| 529 |
* Returns user id with facebook id |
| 530 |
* |
| 531 |
* @param string $fb_id |
| 532 |
* |
| 533 |
* @return int |
| 534 |
*/ |
| 535 |
public function id_owner( $fb_id ) { |
| 536 |
return $this->get_meta_owner( $this->umeta_id, $fb_id ); |
| 537 |
} |
| 538 |
|
| 539 |
/** |
| 540 |
* Get signed request |
| 541 |
* |
| 542 |
* @param string $key |
| 543 |
* @throws \Exception |
| 544 |
* @return SignedRequest |
| 545 |
*/ |
| 546 |
private function signed_request( $key ) { |
| 547 |
$page_helper = $this->api->getPageTabHelper(); |
| 548 |
return $page_helper->getSignedRequest(); |
| 549 |
} |
| 550 |
|
| 551 |
/** |
| 552 |
* Returns if user is connected to Facebook |
| 553 |
* |
| 554 |
* @param int $user_id |
| 555 |
* |
| 556 |
* @return bool |
| 557 |
*/ |
| 558 |
public function is_connected( $user_id ) { |
| 559 |
return (bool) $this->get_facebook_id( $user_id ); |
| 560 |
} |
| 561 |
|
| 562 |
/** |
| 563 |
* Returns User's Facebook ID |
| 564 |
* |
| 565 |
* @param int $wp_user_id |
| 566 |
* |
| 567 |
* @return int |
| 568 |
*/ |
| 569 |
public function get_facebook_id( $wp_user_id ) { |
| 570 |
return get_user_meta( $wp_user_id, $this->umeta_id, true ) ?: 0; |
| 571 |
} |
| 572 |
|
| 573 |
/** |
| 574 |
* Returns Facebook mail |
| 575 |
* |
| 576 |
* @param int $wp_user_id |
| 577 |
* |
| 578 |
* @return string |
| 579 |
*/ |
| 580 |
public function get_user_mail( $wp_user_id ) { |
| 581 |
return (string) get_user_meta( $wp_user_id, $this->umeta_mail, true ); |
| 582 |
} |
| 583 |
|
| 584 |
/** |
| 585 |
* Get current users profile |
| 586 |
* |
| 587 |
* @param string $context |
| 588 |
* @param string $token string |
| 589 |
* @param string $user_id Default 'me' |
| 590 |
* @throws \Exception |
| 591 |
* @return GraphUser|null |
| 592 |
*/ |
| 593 |
protected function get_user_profile( $context, $token, $user_id = 'me' ) { |
| 594 |
/** |
| 595 |
* Information field of Facebook user. |
| 596 |
* |
| 597 |
* Default is id, name, email. |
| 598 |
* |
| 599 |
* @filter gianism_user_profile_fields |
| 600 |
* @see https://developers.facebook.com/docs/graph-api/reference/user |
| 601 |
* @param array $fields Default array('id', 'name', 'email', 'first_name', 'last_name') |
| 602 |
* @param string $context 'login' or 'connect' |
| 603 |
*/ |
| 604 |
$fields = apply_filters( |
| 605 |
'gianism_user_profile_fields', |
| 606 |
[ |
| 607 |
'id', |
| 608 |
'name', |
| 609 |
'email', |
| 610 |
// 'link', |
| 611 |
'first_name', |
| 612 |
'last_name', |
| 613 |
], |
| 614 |
$context |
| 615 |
); |
| 616 |
|
| 617 |
return $this->api->get( "/{$user_id}?fields=" . implode( ',', $fields ), $token )->getGraphUser(); |
| 618 |
} |
| 619 |
|
| 620 |
/** |
| 621 |
* Save Facebook Mail |
| 622 |
* |
| 623 |
* @param string $mail |
| 624 |
* @param int $user_id |
| 625 |
* |
| 626 |
* @return void |
| 627 |
*/ |
| 628 |
public function set_user_mail( $mail, $user_id ) { |
| 629 |
update_user_meta( $user_id, $this->umeta_mail, $mail ); |
| 630 |
} |
| 631 |
|
| 632 |
/** |
| 633 |
* Save Facebook ID |
| 634 |
* |
| 635 |
* @param string $fb_user_id |
| 636 |
* @param int $wp_user_id |
| 637 |
* |
| 638 |
* @return void |
| 639 |
*/ |
| 640 |
public function set_user_id( $fb_user_id, $wp_user_id ) { |
| 641 |
update_user_meta( $wp_user_id, $this->umeta_id, $fb_user_id ); |
| 642 |
} |
| 643 |
|
| 644 |
/** |
| 645 |
* Get HTTP client handler for Facebook SDK. |
| 646 |
* |
| 647 |
* @return \Facebook\HttpClients\FacebookCurlHttpClient|null |
| 648 |
*/ |
| 649 |
protected function get_http_client_handler() { |
| 650 |
// Return a custom cURL client that uses system CA certificates |
| 651 |
return new \Gianism\Helper\FacebookSystemCaCurlClient(); |
| 652 |
} |
| 653 |
|
| 654 |
/** |
| 655 |
* @param array{ |
| 656 |
* app_id?:string, |
| 657 |
* app_secret?:string, |
| 658 |
* default_graph_version?:string, |
| 659 |
* persistent_data_handler?:\Facebook\PersistentData\PersistentDataInterface |
| 660 |
* } $config Setting for Facebook API Client |
| 661 |
* |
| 662 |
* @return \Facebook\Facebook |
| 663 |
*/ |
| 664 |
public function get_facebook_client( $config = [] ) { |
| 665 |
$default_config = apply_filters( |
| 666 |
'gianism_default_facebook_client_config', |
| 667 |
[ |
| 668 |
'app_id' => $this->fb_app_id, |
| 669 |
'app_secret' => $this->fb_app_secret, |
| 670 |
'default_graph_version' => $this->get_graph_version(), |
| 671 |
'persistent_data_handler' => new FacebookCookiePersistentDataHandler(), |
| 672 |
], |
| 673 |
$this |
| 674 |
); |
| 675 |
// Use custom HTTP client to avoid certificate issues |
| 676 |
$http_client = $this->get_http_client_handler(); |
| 677 |
if ( $http_client ) { |
| 678 |
$default_config['http_client_handler'] = $http_client; |
| 679 |
} |
| 680 |
// Allow user to override. |
| 681 |
$config = array_merge( $default_config, $config ); |
| 682 |
return new \Facebook\Facebook( $config ); |
| 683 |
} |
| 684 |
|
| 685 |
/** |
| 686 |
* Getter |
| 687 |
* |
| 688 |
* @param string $name |
| 689 |
* @return mixed |
| 690 |
*/ |
| 691 |
public function __get( $name ) { |
| 692 |
switch ( $name ) { |
| 693 |
case 'api': |
| 694 |
if ( is_null( $this->_api ) ) { |
| 695 |
$this->_api = $this->get_facebook_client(); |
| 696 |
} |
| 697 |
return $this->_api; |
| 698 |
case 'admin': |
| 699 |
if ( is_null( $this->_admin_api ) ) { |
| 700 |
$token = $this->option->get( 'gianism_facebook_admin_token', false ); |
| 701 |
if ( ! $this->fb_use_api || ! $token ) { |
| 702 |
return new \WP_Error( 404, $this->_( 'Token is not set. Please get it.' ) ); |
| 703 |
} |
| 704 |
try { |
| 705 |
$this->_admin_api = $this->get_facebook_client(); |
| 706 |
// Check last updated |
| 707 |
$updated = $this->option->get( 'gianism_facebook_admin_refreshed', 0 ); |
| 708 |
// phpcs:ignore WordPress.DateTime.CurrentTimeTimestamp.Requested |
| 709 |
if ( ! $updated || current_time( 'timestamp' ) > $updated + ( 60 * 60 * 24 * 60 ) ) { |
| 710 |
return new \WP_Error( 410, $this->_( 'Token is outdated. Please update it.' ) ); |
| 711 |
} |
| 712 |
$this->_admin_api->setDefaultAccessToken( $token ); |
| 713 |
} catch ( \Exception $e ) { |
| 714 |
return new \WP_Error( $e->getCode(), $e->getMessage() ); |
| 715 |
} |
| 716 |
} |
| 717 |
return $this->_admin_api; |
| 718 |
case 'admin_account': |
| 719 |
if ( is_wp_error( $this->admin ) ) { |
| 720 |
return false; |
| 721 |
} |
| 722 |
try { |
| 723 |
return $this->admin->get( '/me' )->getGraphUser(); |
| 724 |
} catch ( \Exception $e ) { |
| 725 |
return false; |
| 726 |
} |
| 727 |
case 'admin_pages': |
| 728 |
if ( is_wp_error( $this->admin ) ) { |
| 729 |
return []; |
| 730 |
} else { |
| 731 |
try { |
| 732 |
$response = $this->admin->get( '/me/accounts' )->getGraphEdge(); |
| 733 |
$pages = []; |
| 734 |
foreach ( $response as $node ) { |
| 735 |
$pages[] = [ |
| 736 |
'id' => $node->getProperty( 'id' ), |
| 737 |
'name' => $node->getProperty( 'name' ), |
| 738 |
'token' => $node->getProperty( 'access_token' ), |
| 739 |
]; |
| 740 |
} |
| 741 |
return $pages; |
| 742 |
} catch ( \Exception $e ) { |
| 743 |
trigger_error( $e->getMessage(), E_USER_WARNING ); |
| 744 |
return []; |
| 745 |
} |
| 746 |
} |
| 747 |
case 'admin_id': |
| 748 |
return $this->option->get( 'gianism_facebook_admin_id', 'me' ); |
| 749 |
default: |
| 750 |
return parent::__get( $name ); |
| 751 |
} |
| 752 |
} |
| 753 |
|
| 754 |
/** |
| 755 |
* Get graph api version. |
| 756 |
* |
| 757 |
* @since 4.0.0 |
| 758 |
* @return string |
| 759 |
*/ |
| 760 |
public function get_graph_version() { |
| 761 |
$version = $this->fb_version; |
| 762 |
if ( ! preg_match( '/^v\d+\.\d+$/u', $version ) ) { |
| 763 |
return $this->minimum_api_version; |
| 764 |
} elseif ( version_compare( $version, $this->minimum_api_version, '<' ) ) { |
| 765 |
return $this->minimum_api_version; |
| 766 |
} else { |
| 767 |
return $version; |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
/** |
| 772 |
* Get current page api |
| 773 |
* |
| 774 |
* @package Gianism |
| 775 |
* @since 3.0.6 |
| 776 |
* @return \Facebook\Facebook|\WP_Error |
| 777 |
*/ |
| 778 |
public function get_current_page_api() { |
| 779 |
$page_id = $this->admin_id; |
| 780 |
if ( 'me' === $page_id ) { |
| 781 |
return new \WP_Error( 500, __( 'Page is not set.', 'wp-gianism' ) ); |
| 782 |
} |
| 783 |
$token = ''; |
| 784 |
foreach ( $this->admin_pages as $page ) { |
| 785 |
if ( $page_id === $page['id'] ) { |
| 786 |
$token = $page['token']; |
| 787 |
break; |
| 788 |
} |
| 789 |
} |
| 790 |
if ( ! $token ) { |
| 791 |
return new \WP_Error( 404, __( 'No page found. Do you have permission for that page?', 'wp-gianism' ) ); |
| 792 |
} |
| 793 |
try { |
| 794 |
$api = $this->get_facebook_client(); |
| 795 |
$api->setDefaultAccessToken( $token ); |
| 796 |
return $api; |
| 797 |
} catch ( \Exception $e ) { |
| 798 |
return new \WP_Error( $e->getCode(), $e->getMessage() ); |
| 799 |
} |
| 800 |
} |
| 801 |
|
| 802 |
/** |
| 803 |
* Returns if user like my page. Only available on Facebook Tab page or application. |
| 804 |
* |
| 805 |
* @deprecated 3.0.0 |
| 806 |
* @return bool |
| 807 |
*/ |
| 808 |
public function is_user_like_me_on_fangate() { |
| 809 |
_deprecated_function( __METHOD__, 'Gianism 3.0.0', null ); |
| 810 |
return false; |
| 811 |
} |
| 812 |
|
| 813 |
/** |
| 814 |
* Returns if current facebook user is WordPress registered user. |
| 815 |
* |
| 816 |
* If current Facebook user is registerd on your WordPress, returns user ID on WordPress. |
| 817 |
* |
| 818 |
* @deprecated 3.0.0 |
| 819 |
* @return int |
| 820 |
*/ |
| 821 |
public function is_registered_user_on_fangate() { |
| 822 |
_deprecated_function( __METHOD__, 'Gianism 3.0.0', null ); |
| 823 |
return false; |
| 824 |
} |
| 825 |
|
| 826 |
/** |
| 827 |
* Return true if current user is not logged in Facebook |
| 828 |
* |
| 829 |
* @deprecated 3.0.0 |
| 830 |
* @return boolean |
| 831 |
*/ |
| 832 |
public function is_guest_on_fangate() { |
| 833 |
_deprecated_function( __METHOD__, 'Gianism 3.0.0', null ); |
| 834 |
return false; |
| 835 |
} |
| 836 |
|
| 837 |
/** |
| 838 |
* Returns if current page is fan gate. |
| 839 |
* |
| 840 |
* @deprecated 3.0.0 |
| 841 |
* @return bool |
| 842 |
*/ |
| 843 |
public function is_fangate() { |
| 844 |
_deprecated_function( __METHOD__, 'Gianism 3.0.0', null ); |
| 845 |
return false; |
| 846 |
} |
| 847 |
|
| 848 |
/** |
| 849 |
* Initialize Facebook Fangate Scripts |
| 850 |
* |
| 851 |
* @deprecated 3.0.0 |
| 852 |
*/ |
| 853 |
public function fan_gate_helper() { |
| 854 |
// Do nothing |
| 855 |
_deprecated_function( __METHOD__, 'Gianism 3.0.0', null ); |
| 856 |
} |
| 857 |
} |
| 858 |
|