';
protected $svgBlue = '';
protected $svgBlack = '';
protected $popupWidth = 475;
protected $popupHeight = 175;
protected $sync_fields = array(
'age_range' => array(
'label' => 'Age range',
'node' => 'me',
'scope' => 'user_age_range'
),
'birthday' => array(
'label' => 'Birthday',
'node' => 'me',
'scope' => 'user_birthday'
),
'link' => array(
'label' => 'Profile link',
'node' => 'me',
'scope' => 'user_link'
),
'hometown' => array(
'label' => 'Hometown',
'node' => 'me',
'scope' => 'user_hometown'
),
'location' => array(
'label' => 'Location',
'node' => 'me',
'scope' => 'user_location'
),
'gender' => array(
'label' => 'Gender',
'node' => 'me',
'scope' => 'user_gender'
)
);
public function __construct() {
$this->id = 'facebook';
$this->label = 'Facebook';
$this->path = dirname(__FILE__);
$this->requiredFields = array(
'appid' => 'App ID',
'secret' => 'App Secret'
);
add_filter('nsl_finalize_settings_' . $this->optionKey, array(
$this,
'finalizeSettings'
));
parent::__construct(array(
'appid' => '',
'secret' => '',
'skin' => 'dark',
'login_label' => 'Continue with Facebook',
'register_label' => 'Sign up with Facebook',
'link_label' => 'Link account with Facebook',
'unlink_label' => 'Unlink account from Facebook'
));
}
protected function forTranslation() {
__('Continue with Facebook', 'nextend-facebook-connect');
__('Sign up with Facebook', 'nextend-facebook-connect');
__('Link account with Facebook', 'nextend-facebook-connect');
__('Unlink account from Facebook', 'nextend-facebook-connect');
}
public function getRawDefaultButton() {
$skin = $this->settings->get('skin');
switch ($skin) {
case 'light':
$color = '#fff';
$svg = $this->svgBlue;
break;
case 'black':
$color = '#000';
$svg = $this->svg;
break;
case 'white':
$color = '#fff';
$svg = $this->svgBlack;
break;
default:
$color = $this->color;
$svg = $this->svg;
}
return '';
}
public function getRawIconButton() {
$skin = $this->settings->get('skin');
switch ($skin) {
case 'light':
$color = '#fff';
$svg = $this->svgBlue;
break;
case 'black':
$color = '#000';
$svg = $this->svg;
break;
case 'white':
$color = '#fff';
$svg = $this->svgBlack;
break;
default:
$color = $this->color;
$svg = $this->svg;
}
return '';
}
public function finalizeSettings($settings) {
if (defined('NEXTEND_FB_APP_ID')) {
$settings['appid'] = NEXTEND_FB_APP_ID;
}
if (defined('NEXTEND_FB_APP_SECRET')) {
$settings['secret'] = NEXTEND_FB_APP_SECRET;
}
return $settings;
}
/**
* @return NextendSocialProviderFacebookClient
*/
public function getClient() {
if ($this->client === null) {
require_once dirname(__FILE__) . '/facebook-client.php';
$this->client = new NextendSocialProviderFacebookClient($this->id, $this->isTest());
$this->client->setClientId($this->settings->get('appid'));
$this->client->setClientSecret($this->settings->get('secret'));
$this->client->setRedirectUri($this->getRedirectUri());
}
return $this->client;
}
public function validateSettings($newData, $postedData) {
$newData = parent::validateSettings($newData, $postedData);
foreach ($postedData AS $key => $value) {
switch ($key) {
case 'tested':
if ($postedData[$key] == '1' && (!isset($newData['tested']) || $newData['tested'] != '0')) {
$newData['tested'] = 1;
} else {
$newData['tested'] = 0;
}
break;
case 'appid':
case 'secret':
$newData[$key] = trim(sanitize_text_field($value));
if ($this->settings->get($key) !== $newData[$key]) {
$newData['tested'] = 0;
}
if (empty($newData[$key])) {
Notices::addError(sprintf(__('The %1$s entered did not appear to be a valid. Please enter a valid %2$s.', 'nextend-facebook-connect'), $this->requiredFields[$key], $this->requiredFields[$key]));
}
break;
case 'skin':
$newData[$key] = trim(sanitize_text_field($value));
break;
}
}
return $newData;
}
/**
* @param $accessTokenData
*
* @return string
* @throws Exception
*/
protected function requestLongLivedToken($accessTokenData) {
$client = $this->getClient();
if (!$client->isAccessTokenLongLived()) {
return $client->requestLongLivedAccessToken();
}
return $accessTokenData;
}
/**
* @return array|mixed
* @throws Exception
*/
protected function getCurrentUserInfo() {
$fields = array(
'id',
'name',
'email',
'first_name',
'last_name',
'picture.type(large)'
);
$extra_fields = apply_filters('nsl_facebook_sync_node_fields', array(), 'me');
return $this->getClient()
->get('/me?fields=' . implode(',', array_merge($fields, $extra_fields)));
}
public function getMe() {
return $this->authUserData;
}
public function getAuthUserData($key) {
switch ($key) {
case 'id':
return $this->authUserData['id'];
case 'email':
return !empty($this->authUserData['email']) ? $this->authUserData['email'] : '';
case 'name':
return $this->authUserData['name'];
case 'first_name':
return $this->authUserData['first_name'];
case 'last_name':
return $this->authUserData['last_name'];
case 'picture':
$profilePicture = $this->authUserData['picture'];
if (!empty($profilePicture) && !empty($profilePicture['data'])) {
if (isset($profilePicture['data']['is_silhouette']) && !$profilePicture['data']['is_silhouette']) {
return $profilePicture['data']['url'];
}
}
return '';
}
return parent::getAuthUserData($key);
}
public function syncProfile($user_id, $provider, $access_token) {
if ($this->needUpdateAvatar($user_id)) {
if ($this->getAuthUserData('picture')) {
$this->updateAvatar($user_id, $this->getAuthUserData('picture'));
}
}
$this->storeAccessToken($user_id, $access_token);
}
protected function saveUserData($user_id, $key, $data) {
switch ($key) {
case 'access_token':
update_user_meta($user_id, 'fb_user_access_token', $data);
break;
default:
parent::saveUserData($user_id, $key, $data);
break;
}
}
protected function getUserData($user_id, $key) {
switch ($key) {
case 'access_token':
return get_user_meta($user_id, 'fb_user_access_token', true);
break;
}
return parent::getUserData($user_id, $key);
}
public function deleteLoginPersistentData() {
parent::deleteLoginPersistentData();
if ($this->client !== null) {
$this->client->deleteLoginPersistentData();
}
}
public function getSyncDataFieldDescription($fieldName) {
if (isset($this->sync_fields[$fieldName]['scope'])) {
return sprintf(__('Required scope: %1$s', 'nextend-facebook-connect'), $this->sync_fields[$fieldName]['scope']);
}
return parent::getSyncDataFieldDescription($fieldName);
}
}
NextendSocialLogin::addProvider(new NextendSocialProviderFacebook);