Нема описа

facebook.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. use NSL\Notices;
  3. class NextendSocialProviderFacebook extends NextendSocialProvider {
  4. protected $dbID = 'fb';
  5. /** @var NextendSocialProviderFacebookClient */
  6. protected $client;
  7. protected $color = '#1877F2';
  8. protected $svg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1365.3 1365.3" height="1365.3" width="1365.3"><path d="M1365.3 682.7A682.7 682.7 0 10576 1357V880H402.7V682.7H576V532.3c0-171.1 102-265.6 257.9-265.6 74.6 0 152.8 13.3 152.8 13.3v168h-86.1c-84.8 0-111.3 52.6-111.3 106.6v128h189.4L948.4 880h-159v477a682.8 682.8 0 00576-674.3" fill="#fff"/></svg>';
  9. protected $svgBlue = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1365.3 1365.3" height="1365.3" width="1365.3"><path d="M1365.3 682.7A682.7 682.7 0 10576 1357V880H402.7V682.7H576V532.3c0-171.1 102-265.6 257.9-265.6 74.6 0 152.8 13.3 152.8 13.3v168h-86.1c-84.8 0-111.3 52.6-111.3 106.6v128h189.4L948.4 880h-159v477a682.8 682.8 0 00576-674.3" fill="#1877f2"/><path d="M948.4 880l30.3-197.3H789.3v-128c0-54 26.5-106.7 111.3-106.7h86V280s-78-13.3-152.7-13.3c-156 0-257.9 94.5-257.9 265.6v150.4H402.7V880H576v477a687.8 687.8 0 00213.3 0V880h159.1" fill="#fff"/></svg>';
  10. protected $svgBlack = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1365.3 1365.3" height="1365.3" width="1365.3"><path d="M1365.3 682.7A682.7 682.7 0 10576 1357V880H402.7V682.7H576V532.3c0-171.1 102-265.6 257.9-265.6 74.6 0 152.8 13.3 152.8 13.3v168h-86.1c-84.8 0-111.3 52.6-111.3 106.6v128h189.4L948.4 880h-159v477a682.8 682.8 0 00576-674.3" fill="#100f0d"/><path d="M948.4 880l30.3-197.3H789.3v-128c0-54 26.5-106.7 111.3-106.7h86V280s-78-13.3-152.7-13.3c-156 0-257.9 94.5-257.9 265.6v150.4H402.7V880H576v477a687.8 687.8 0 00213.3 0V880h159.1" fill="#fff"/></svg>';
  11. protected $popupWidth = 600;
  12. protected $popupHeight = 679;
  13. protected $sync_fields = array(
  14. 'age_range' => array(
  15. 'label' => 'Age range',
  16. 'node' => 'me',
  17. 'scope' => 'user_age_range'
  18. ),
  19. 'birthday' => array(
  20. 'label' => 'Birthday',
  21. 'node' => 'me',
  22. 'scope' => 'user_birthday'
  23. ),
  24. 'link' => array(
  25. 'label' => 'Profile link',
  26. 'node' => 'me',
  27. 'scope' => 'user_link'
  28. ),
  29. 'hometown' => array(
  30. 'label' => 'Hometown',
  31. 'node' => 'me',
  32. 'scope' => 'user_hometown'
  33. ),
  34. 'location' => array(
  35. 'label' => 'Location',
  36. 'node' => 'me',
  37. 'scope' => 'user_location'
  38. ),
  39. 'gender' => array(
  40. 'label' => 'Gender',
  41. 'node' => 'me',
  42. 'scope' => 'user_gender'
  43. ),
  44. 'quotes' => array(
  45. 'label' => 'Quotes',
  46. 'node' => 'me',
  47. 'scope' => 'user_likes'
  48. )
  49. );
  50. public function __construct() {
  51. $this->id = 'facebook';
  52. $this->label = 'Facebook';
  53. $this->path = dirname(__FILE__);
  54. $this->requiredFields = array(
  55. 'appid' => 'App ID',
  56. 'secret' => 'App Secret'
  57. );
  58. add_filter('nsl_finalize_settings_' . $this->optionKey, array(
  59. $this,
  60. 'finalizeSettings'
  61. ));
  62. parent::__construct(array(
  63. 'appid' => '',
  64. 'secret' => '',
  65. 'skin' => 'dark',
  66. 'login_label' => 'Continue with <b>Facebook</b>',
  67. 'register_label' => 'Sign up with <b>Facebook</b>',
  68. 'link_label' => 'Link account with <b>Facebook</b>',
  69. 'unlink_label' => 'Unlink account from <b>Facebook</b>'
  70. ));
  71. }
  72. protected function forTranslation() {
  73. __('Continue with <b>Facebook</b>', 'nextend-facebook-connect');
  74. __('Sign up with <b>Facebook</b>', 'nextend-facebook-connect');
  75. __('Link account with <b>Facebook</b>', 'nextend-facebook-connect');
  76. __('Unlink account from <b>Facebook</b>', 'nextend-facebook-connect');
  77. }
  78. public function getRawDefaultButton() {
  79. $skin = $this->settings->get('skin');
  80. switch ($skin) {
  81. case 'light':
  82. $color = '#fff';
  83. $svg = $this->svgBlue;
  84. break;
  85. case 'black':
  86. $color = '#000';
  87. $svg = $this->svg;
  88. break;
  89. case 'white':
  90. $color = '#fff';
  91. $svg = $this->svgBlack;
  92. break;
  93. default:
  94. $color = $this->color;
  95. $svg = $this->svg;
  96. }
  97. return '<div class="nsl-button nsl-button-default nsl-button-' . $this->id . '" data-skin="' . $skin . '" style="background-color:' . $color . ';"><div class="nsl-button-svg-container">' . $svg . '</div><div class="nsl-button-label-container">{{label}}</div></div>';
  98. }
  99. public function getRawIconButton() {
  100. $skin = $this->settings->get('skin');
  101. switch ($skin) {
  102. case 'light':
  103. $color = '#fff';
  104. $svg = $this->svgBlue;
  105. break;
  106. case 'black':
  107. $color = '#000';
  108. $svg = $this->svg;
  109. break;
  110. case 'white':
  111. $color = '#fff';
  112. $svg = $this->svgBlack;
  113. break;
  114. default:
  115. $color = $this->color;
  116. $svg = $this->svg;
  117. }
  118. return '<div class="nsl-button nsl-button-icon nsl-button-' . $this->id . '" data-skin="' . $skin . '" style="background-color:' . $color . ';"><div class="nsl-button-svg-container">' . $svg . '</div></div>';
  119. }
  120. public function finalizeSettings($settings) {
  121. if (defined('NEXTEND_FB_APP_ID')) {
  122. $settings['appid'] = NEXTEND_FB_APP_ID;
  123. }
  124. if (defined('NEXTEND_FB_APP_SECRET')) {
  125. $settings['secret'] = NEXTEND_FB_APP_SECRET;
  126. }
  127. return $settings;
  128. }
  129. /**
  130. * @return NextendSocialProviderFacebookClient
  131. */
  132. public function getClient() {
  133. if ($this->client === null) {
  134. require_once dirname(__FILE__) . '/facebook-client.php';
  135. $this->client = new NextendSocialProviderFacebookClient($this->id, $this->isTest());
  136. $this->client->setClientId($this->settings->get('appid'));
  137. $this->client->setClientSecret($this->settings->get('secret'));
  138. $this->client->setRedirectUri($this->getRedirectUriForOAuthFlow());
  139. }
  140. return $this->client;
  141. }
  142. public function validateSettings($newData, $postedData) {
  143. $newData = parent::validateSettings($newData, $postedData);
  144. foreach ($postedData AS $key => $value) {
  145. switch ($key) {
  146. case 'tested':
  147. if ($postedData[$key] == '1' && (!isset($newData['tested']) || $newData['tested'] != '0')) {
  148. $newData['tested'] = 1;
  149. } else {
  150. $newData['tested'] = 0;
  151. }
  152. break;
  153. case 'appid':
  154. case 'secret':
  155. $newData[$key] = trim(sanitize_text_field($value));
  156. if ($this->settings->get($key) !== $newData[$key]) {
  157. $newData['tested'] = 0;
  158. }
  159. if (empty($newData[$key])) {
  160. 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]));
  161. }
  162. break;
  163. case 'skin':
  164. $newData[$key] = trim(sanitize_text_field($value));
  165. break;
  166. }
  167. }
  168. return $newData;
  169. }
  170. /**
  171. * @param $accessTokenData
  172. *
  173. * @return string
  174. * @throws Exception
  175. */
  176. protected function requestLongLivedToken($accessTokenData) {
  177. $client = $this->getClient();
  178. if (!$client->isAccessTokenLongLived()) {
  179. return $client->requestLongLivedAccessToken();
  180. }
  181. return $accessTokenData;
  182. }
  183. /**
  184. * @return array|mixed
  185. * @throws Exception
  186. */
  187. protected function getCurrentUserInfo() {
  188. $fields = array(
  189. 'id',
  190. 'name',
  191. 'email',
  192. 'first_name',
  193. 'last_name',
  194. 'picture.type(large)'
  195. );
  196. $extra_fields = apply_filters('nsl_facebook_sync_node_fields', array(), 'me');
  197. return $this->getClient()
  198. ->get('/me?fields=' . implode(',', array_merge($fields, $extra_fields)));
  199. }
  200. public function getMe() {
  201. return $this->authUserData;
  202. }
  203. public function getAuthUserData($key) {
  204. switch ($key) {
  205. case 'id':
  206. return $this->authUserData['id'];
  207. case 'email':
  208. return !empty($this->authUserData['email']) ? $this->authUserData['email'] : '';
  209. case 'name':
  210. return $this->authUserData['name'];
  211. case 'first_name':
  212. return $this->authUserData['first_name'];
  213. case 'last_name':
  214. return $this->authUserData['last_name'];
  215. case 'picture':
  216. $profilePicture = $this->authUserData['picture'];
  217. if (!empty($profilePicture) && !empty($profilePicture['data'])) {
  218. if (isset($profilePicture['data']['is_silhouette']) && !$profilePicture['data']['is_silhouette']) {
  219. return $profilePicture['data']['url'];
  220. }
  221. }
  222. return '';
  223. }
  224. return parent::getAuthUserData($key);
  225. }
  226. public function syncProfile($user_id, $provider, $access_token) {
  227. if ($this->needUpdateAvatar($user_id)) {
  228. if ($this->getAuthUserData('picture')) {
  229. $this->updateAvatar($user_id, $this->getAuthUserData('picture'));
  230. }
  231. }
  232. $this->storeAccessToken($user_id, $access_token);
  233. }
  234. protected function saveUserData($user_id, $key, $data) {
  235. switch ($key) {
  236. case 'access_token':
  237. update_user_meta($user_id, 'fb_user_access_token', $data);
  238. break;
  239. default:
  240. parent::saveUserData($user_id, $key, $data);
  241. break;
  242. }
  243. }
  244. protected function getUserData($user_id, $key) {
  245. switch ($key) {
  246. case 'access_token':
  247. return get_user_meta($user_id, 'fb_user_access_token', true);
  248. break;
  249. }
  250. return parent::getUserData($user_id, $key);
  251. }
  252. public function deleteLoginPersistentData() {
  253. parent::deleteLoginPersistentData();
  254. if ($this->client !== null) {
  255. $this->client->deleteLoginPersistentData();
  256. }
  257. }
  258. public function getSyncDataFieldDescription($fieldName) {
  259. if (isset($this->sync_fields[$fieldName]['scope'])) {
  260. return sprintf(__('Required scope: %1$s', 'nextend-facebook-connect'), $this->sync_fields[$fieldName]['scope']);
  261. }
  262. return parent::getSyncDataFieldDescription($fieldName);
  263. }
  264. }
  265. NextendSocialLogin::addProvider(new NextendSocialProviderFacebook);