';
protected $svgUniform = '';
const requiredApi1 = 'Google People API';
protected $sync_fields = array(
'locale' => array(
'label' => 'Locale',
'node' => 'me',
),
'genders' => array(
'label' => 'Genders',
'node' => 'people',
'description' => self::requiredApi1,
),
'biographies' => array(
'label' => 'Biographies',
'node' => 'people',
'description' => self::requiredApi1,
),
'birthdays' => array(
'label' => 'Birthdays',
'node' => 'people',
'scope' => 'https://www.googleapis.com/auth/user.birthday.read',
'description' => self::requiredApi1,
),
'occupations' => array(
'label' => 'Occupations',
'node' => 'people',
'description' => self::requiredApi1,
),
'organizations' => array(
'label' => 'Organizations',
'node' => 'people',
'description' => self::requiredApi1,
),
'locations' => array(
'label' => 'Locations',
'node' => 'people',
'description' => self::requiredApi1,
),
'ageRanges' => array(
'label' => 'Age ranges',
'node' => 'people',
'description' => self::requiredApi1,
),
'addresses' => array(
'label' => 'Addresses',
'node' => 'people',
'scope' => 'https://www.googleapis.com/auth/user.addresses.read',
'description' => self::requiredApi1,
),
'phoneNumbers' => array(
'label' => 'Phone Numbers',
'node' => 'people',
'scope' => 'https://www.googleapis.com/auth/user.phonenumbers.read',
'description' => self::requiredApi1,
)
);
public function __construct() {
$this->id = 'google';
$this->label = 'Google';
$this->path = dirname(__FILE__);
$this->requiredFields = array(
'client_id' => 'Client ID',
'client_secret' => 'Client Secret'
);
parent::__construct(array(
'client_id' => '',
'client_secret' => '',
'select_account' => 1,
'skin' => 'light',
'login_label' => 'Continue with Google',
'register_label' => 'Sign up with Google',
'link_label' => 'Link account with Google',
'unlink_label' => 'Unlink account from Google'
));
}
protected function forTranslation() {
__('Continue with Google', 'nextend-facebook-connect');
__('Sign up with Google', 'nextend-facebook-connect');
__('Link account with Google', 'nextend-facebook-connect');
__('Unlink account from Google', 'nextend-facebook-connect');
}
public function getRawDefaultButton() {
$skin = $this->settings->get('skin');
switch ($skin) {
case 'dark':
$color = $this->color;
$svg = $this->svg;
break;
case 'light':
$color = '#fff';
$svg = $this->svg;
break;
default:
$color = $this->colorUniform;
$svg = $this->svgUniform;
}
return '';
}
public function getRawIconButton() {
$skin = $this->settings->get('skin');
switch ($skin) {
case 'dark':
$color = $this->color;
$svg = $this->svg;
break;
case 'light':
$color = '#fff';
$svg = $this->svg;
break;
default:
$color = $this->colorUniform;
$svg = $this->svgUniform;
}
return '';
}
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 'skin':
$newData[$key] = trim(sanitize_text_field($value));
break;
case 'client_id':
case 'client_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 'select_account':
$newData[$key] = $value ? 1 : 0;
break;
}
}
return $newData;
}
public function getClient() {
if ($this->client === null) {
require_once dirname(__FILE__) . '/google-client.php';
$this->client = new NextendSocialProviderGoogleClient($this->id);
$this->client->setClientId($this->settings->get('client_id'));
$this->client->setClientSecret($this->settings->get('client_secret'));
$this->client->setRedirectUri($this->getRedirectUriForOAuthFlow());
if (!$this->settings->get('select_account')) {
$this->client->setPrompt('');
}
}
return $this->client;
}
/**
* @return array
* @throws Exception
*/
protected function getCurrentUserInfo() {
$fields = array(
'id',
'name',
'email',
'family_name',
'given_name',
'picture',
);
$extra_me_fields = apply_filters('nsl_google_sync_node_fields', array(), 'me');
return $this->getClient()
->get('userinfo?fields=' . implode(',', array_merge($fields, $extra_me_fields)));
}
public function getMe() {
return $this->authUserData;
}
/**
* @return array
* @throws Exception
*/
public function getMyPeople() {
$extra_people_fields = apply_filters('nsl_google_sync_node_fields', array(), 'people');
if (!empty($extra_people_fields)) {
return $this->getClient()
->get('people/me?personFields=' . implode(',', $extra_people_fields), array(), 'https://people.googleapis.com/v1/');
}
return $extra_people_fields;
}
/**
* @param $key
*
* @return string
*/
public function getAuthUserData($key) {
switch ($key) {
case 'id':
return $this->authUserData['id'];
case 'email':
return $this->authUserData['email'];
case 'name':
return !empty($this->authUserData['name']) ? $this->authUserData['name'] : '';
case 'first_name':
return !empty($this->authUserData['given_name']) ? $this->authUserData['given_name'] : '';
case 'last_name':
return !empty($this->authUserData['family_name']) ? $this->authUserData['family_name'] : '';
case 'picture':
return $this->authUserData['picture'];
}
return parent::getAuthUserData($key);
}
public function syncProfile($user_id, $provider, $access_token) {
if ($this->needUpdateAvatar($user_id)) {
$this->updateAvatar($user_id, $this->getAuthUserData('picture'));
}
$this->storeAccessToken($user_id, $access_token);
}
public function deleteLoginPersistentData() {
parent::deleteLoginPersistentData();
if ($this->client !== null) {
$this->client->deleteLoginPersistentData();
}
}
public function getAvatar($user_id) {
if (!$this->isUserConnected($user_id)) {
return false;
}
$picture = $this->getUserData($user_id, 'profile_picture');
if (!$picture || $picture == '') {
return false;
}
return $picture;
}
public function getSyncDataFieldDescription($fieldName) {
if (isset($this->sync_fields[$fieldName]['description'])) {
return sprintf(__('Required API: %1$s', 'nextend-facebook-connect'), $this->sync_fields[$fieldName]['description']);
}
return parent::getSyncDataFieldDescription($fieldName);
}
}
NextendSocialLogin::addProvider(new NextendSocialProviderGoogle);