Nessuna descrizione

user.php 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. <?php
  2. use NSL\Notices;
  3. use NSL\Persistent\Persistent;
  4. require_once(NSL_PATH . '/includes/userData.php');
  5. class NextendSocialUser {
  6. /** @var NextendSocialProvider */
  7. protected $provider;
  8. protected $access_token;
  9. private $userExtraData;
  10. protected $user_id;
  11. protected $shouldAutoLogin = false;
  12. /**
  13. * NextendSocialUser constructor.
  14. *
  15. * @param NextendSocialProvider $provider
  16. * @param $access_token
  17. */
  18. public function __construct($provider, $access_token) {
  19. $this->provider = $provider;
  20. $this->access_token = $access_token;
  21. }
  22. /**
  23. * @param $key
  24. * $key is like id, email, name, first_name, last_name
  25. * Returns a single userdata of the current provider or empty sting if $key is invalid.
  26. *
  27. * @return string
  28. */
  29. public function getAuthUserData($key) {
  30. return $this->provider->getAuthUserData($key);
  31. }
  32. /**
  33. * Connect with a Provider
  34. * If user is not logged in
  35. * - and has no linked social data (in wp_social_users table), prepare them for register.
  36. * - but if has linked social data, log them in.
  37. * If the user is logged in, retrieve the user data,
  38. * - if the user has no linked social data with the selected provider and there is no other user who linked that id
  39. * , link them and sync the access_token.
  40. */
  41. public function liveConnectGetUserProfile() {
  42. $user_id = $this->provider->getUserIDByProviderIdentifier($this->getAuthUserData('id'));
  43. if ($user_id !== null && !get_user_by('id', $user_id)) {
  44. $this->provider->removeConnectionByUserID($user_id);
  45. $user_id = null;
  46. }
  47. if (!is_user_logged_in()) {
  48. if ($user_id == null) {
  49. $this->prepareRegister();
  50. } else {
  51. $this->login($user_id);
  52. }
  53. } else {
  54. $current_user = wp_get_current_user();
  55. if ($user_id === null) {
  56. // Let's connect the account to the current user!
  57. if ($this->provider->linkUserToProviderIdentifier($current_user->ID, $this->getAuthUserData('id'))) {
  58. $this->provider->syncProfile($current_user->ID, $this->provider, $this->access_token);
  59. Notices::addSuccess(sprintf(__('Your %1$s account is successfully linked with your account. Now you can sign in with %2$s easily.', 'nextend-facebook-connect'), $this->provider->getLabel(), $this->provider->getLabel()));
  60. } else {
  61. Notices::addError(sprintf(__('You have already linked a(n) %s account. Please unlink the current and then you can link another %s account.', 'nextend-facebook-connect'), $this->provider->getLabel(), $this->provider->getLabel()));
  62. }
  63. } else if ($current_user->ID != $user_id) {
  64. Notices::addError(sprintf(__('This %s account is already linked to another user.', 'nextend-facebook-connect'), $this->provider->getLabel()));
  65. }
  66. }
  67. }
  68. /**
  69. * Prepares the registration and registers the user.
  70. * If the email is not registered yet, checks if register is enabled call register() function.
  71. * If the email is already registered, checks if autolink is enabled, if it is, log the user in.
  72. * Autolink enabled: links the current provider account with the existing social account and attempts to login.
  73. * Autolink disabled: Add error with already registered email message.
  74. */
  75. protected function prepareRegister() {
  76. $user_id = false;
  77. $providerUserID = $this->getAuthUserData('id');
  78. $email = '';
  79. if (NextendSocialLogin::$settings->get('store_email') == 1) {
  80. $email = $this->getAuthUserData('email');
  81. }
  82. if (empty($email)) {
  83. $email = '';
  84. } else {
  85. $user_id = email_exists($email);
  86. }
  87. /**
  88. * Can be used for overriding the account where the social account should be automatically linked to.
  89. */
  90. $user_id = apply_filters('nsl_match_social_account_to_user_id', $user_id, $this, $this->provider);
  91. if ($user_id === false) { // Real register
  92. if (apply_filters('nsl_is_register_allowed', true, $this->provider)) {
  93. $this->register($providerUserID, $email);
  94. } else {
  95. //unset the persistent data, so if an error happened, the user can re-authenticate with providers (Google) that offer account selector screen
  96. Persistent::delete($this->provider->getId() . '_at');
  97. Persistent::delete($this->provider->getId() . '_state');
  98. $registerDisabledMessage = apply_filters('nsl_disabled_register_error_message', '');
  99. $registerDisabledRedirectURL = apply_filters('nsl_disabled_register_redirect_url', '');
  100. $nslLoginUrl = NextendSocialLogin::getLoginUrl();
  101. $defaultDisabledMessage = __('User registration is currently not allowed.');
  102. $proxyPage = NextendSocialLogin::getProxyPage();
  103. if ($proxyPage) {
  104. if (empty($registerDisabledMessage) && $registerDisabledMessage !== false) {
  105. /**
  106. * There is no custom message and proxy page is used, so we need to inform the user with our own message.
  107. */
  108. $registerDisabledMessage = $defaultDisabledMessage;
  109. }
  110. } else {
  111. if (empty($registerDisabledMessage) && $registerDisabledMessage !== false) {
  112. if (!empty($registerDisabledRedirectURL)) {
  113. /**
  114. * There is no custom message and it is a custom redirect url, so we need to inform the user with our own message.
  115. */
  116. $registerDisabledMessage = $defaultDisabledMessage;
  117. }
  118. } else {
  119. if (empty($registerDisabledRedirectURL)) {
  120. /**
  121. * By default WordPress displays an error message if the $_GET['registration'] is set to "disabled"
  122. * To avoid displaying the default and the custom error message, the url should not contain it.
  123. */
  124. $registerDisabledRedirectURL = $nslLoginUrl;
  125. }
  126. }
  127. }
  128. if (!empty($registerDisabledMessage)) {
  129. $errors = new WP_Error();
  130. $errors->add('registerdisabled', $registerDisabledMessage);
  131. Notices::addError($errors->get_error_message());
  132. }
  133. if (empty($registerDisabledRedirectURL)) {
  134. $registerDisabledRedirectURL = add_query_arg('registration', 'disabled', $nslLoginUrl);
  135. }
  136. NextendSocialProvider::redirect(__('Authentication error', 'nextend-facebook-connect'), NextendSocialLogin::enableNoticeForUrl($registerDisabledRedirectURL));
  137. exit;
  138. }
  139. } else if ($this->autoLink($user_id, $providerUserID)) {
  140. $this->login($user_id);
  141. }
  142. $this->provider->redirectToLoginForm();
  143. }
  144. /**
  145. * @param $username
  146. * Makes the username in an appropriate format. Removes white space and some special characters.
  147. * Also turns it into lowercase. And put a prefix before the username if user_prefix is set.
  148. * If this formated username is valid returns it, else return false.
  149. *
  150. * @return bool|string
  151. */
  152. protected function sanitizeUserName($username) {
  153. if (empty($username)) {
  154. return false;
  155. }
  156. $username = strtolower($username);
  157. $username = preg_replace('/\s+/', '', $username);
  158. $sanitized_user_login = sanitize_user($this->provider->settings->get('user_prefix') . $username, true);
  159. if (empty($sanitized_user_login)) {
  160. return false;
  161. }
  162. if (!validate_username($sanitized_user_login)) {
  163. return false;
  164. }
  165. return $sanitized_user_login;
  166. }
  167. /**
  168. * @param $providerID
  169. * @param $email
  170. * Registers the user.
  171. *
  172. * @return bool
  173. */
  174. protected function register($providerID, $email) {
  175. NextendSocialLogin::$WPLoginCurrentFlow = 'register';
  176. $sanitized_user_login = false;
  177. if (NextendSocialLogin::$settings->get('store_name') == 1) {
  178. /**
  179. * First checks provided first_name & last_name if it is not available checks name if it is neither available checks secondary_name.
  180. */
  181. $sanitized_user_login = $this->sanitizeUserName($this->getAuthUserData('first_name') . $this->getAuthUserData('last_name'));
  182. if ($sanitized_user_login === false) {
  183. $sanitized_user_login = $this->sanitizeUserName($this->getAuthUserData('username'));
  184. if ($sanitized_user_login === false) {
  185. $sanitized_user_login = $this->sanitizeUserName($this->getAuthUserData('name'));
  186. }
  187. }
  188. }
  189. $email = '';
  190. if (NextendSocialLogin::$settings->get('store_email') == 1) {
  191. $email = $this->getAuthUserData('email');
  192. }
  193. $userData = array(
  194. 'email' => $email,
  195. 'username' => $sanitized_user_login
  196. );
  197. do_action('nsl_before_register', $this->provider);
  198. do_action('nsl_' . $this->provider->getId() . '_before_register');
  199. if (NextendSocialLogin::$settings->get('terms_show') == '1') {
  200. add_filter('nsl_registration_require_extra_input', array(
  201. $this,
  202. 'require_extra_input_terms'
  203. ));
  204. }
  205. /** @var array $userData Validated user data */
  206. $userData = $this->finalizeUserData($userData);
  207. /**
  208. * -If neither of the usernames ( first_name & last_name, secondary_name) are appropriate, the fallback username will be combined with and id that was sent by the provider.
  209. * -In this way we can generate an appropriate username.
  210. */
  211. if (empty($userData['username'])) {
  212. $userData['username'] = sanitize_user($this->provider->settings->get('user_fallback') . md5(uniqid(rand())), true);
  213. }
  214. /**
  215. * If the username is already in use, it will get a number suffix, that is not registered yet.
  216. */
  217. $default_user_name = $userData['username'];
  218. $i = 1;
  219. while (username_exists($userData['username'])) {
  220. $userData['username'] = $default_user_name . $i;
  221. $i++;
  222. }
  223. /**
  224. * Generates a random password. And set the default_password_nag to true. So the user get notify about randomly generated password.
  225. */
  226. if (empty($userData['password'])) {
  227. $userData['password'] = wp_generate_password(12, false);
  228. add_action('user_register', array(
  229. $this,
  230. 'registerCompleteDefaultPasswordNag'
  231. ));
  232. }
  233. /**
  234. * Preregister, checks what roles shall be informed about the registration and sends a notification to them.
  235. */
  236. do_action('nsl_pre_register_new_user', $this);
  237. $loginRestriction = NextendSocialLogin::$settings->get('login_restriction');
  238. if ($loginRestriction) {
  239. $errors = new WP_Error();
  240. //Prevent New User Approve registration before NSL registration
  241. if (class_exists('pw_new_user_approve', false)) {
  242. remove_action('register_post', array(
  243. pw_new_user_approve::instance(),
  244. 'create_new_user'
  245. ), 10);
  246. }
  247. //Ultimate Member redirects before we update the Avatar, we need to sync before the redirect
  248. if (class_exists('UM', false)) {
  249. add_action('um_registration_after_auto_login', array(
  250. $this,
  251. 'syncProfileUser'
  252. ), 10);
  253. }
  254. /*For TML 6.4.17 Register notification integration*/
  255. do_action('register_post', $userData['username'], $userData['email'], $errors);
  256. if ($errors->get_error_code()) {
  257. //unset the persistent data, so if an error happened, the user can re-authenticate with providers (Google) that offer account selector screen
  258. Persistent::delete($this->provider->getId() . '_at');
  259. Persistent::delete($this->provider->getId() . '_state');
  260. Notices::addError($errors);
  261. $this->redirectToLastLocationLogin(true);
  262. }
  263. }
  264. /**
  265. * Eduma theme user priority 1000 to auto log in users. We need to stay under that priority @see https://themeforest.net/item/education-wordpress-theme-education-wp/14058034
  266. * WooCommerce Follow-Up Emails use priority 10, so we need higher @see https://woocommerce.com/products/follow-up-emails/
  267. *
  268. * If there was no error during the registration process,
  269. * -links the user to the providerIdentifier ( wp_social_users table in database store this link ).
  270. * -set the roles for the user.
  271. * -login the user.
  272. */
  273. add_action('user_register', array(
  274. $this,
  275. 'registerComplete'
  276. ), 31);
  277. $autoLoginPriority = apply_filters('nsl_autologin_priority', 40);
  278. add_action('user_register', array(
  279. $this,
  280. 'doAutoLogin'
  281. ), $autoLoginPriority);
  282. $this->userExtraData = $userData;
  283. $user_data = array(
  284. 'user_login' => wp_slash($userData['username']),
  285. 'user_email' => wp_slash($userData['email']),
  286. 'user_pass' => $userData['password']
  287. );
  288. if (NextendSocialLogin::$settings->get('store_name') == 1) {
  289. $name = $this->getAuthUserData('name');
  290. if (!empty($name)) {
  291. $user_data['display_name'] = $name;
  292. }
  293. $first_name = $this->getAuthUserData('first_name');
  294. if (!empty($first_name)) {
  295. $user_data['first_name'] = $first_name;
  296. }
  297. $last_name = $this->getAuthUserData('last_name');
  298. if (!empty($last_name)) {
  299. $user_data['last_name'] = $last_name;
  300. }
  301. }
  302. //Prevent sending the Woocommerce User Email Verification notification if Login restriction is turned off.
  303. if (class_exists('XLWUEV_Core', false) && !$loginRestriction) {
  304. remove_action('user_register', array(
  305. XLWUEV_Woocommerce_Confirmation_Email_Public::instance(),
  306. 'custom_form_user_register'
  307. ), 10);
  308. remove_action('woocommerce_created_customer_notification', array(
  309. XLWUEV_Woocommerce_Confirmation_Email_Public::instance(),
  310. 'new_user_registration_from_registration_form'
  311. ), 10);
  312. }
  313. $error = wp_insert_user($user_data);
  314. if (is_wp_error($error)) {
  315. Notices::addError($error);
  316. $this->redirectToLastLocationLogin(true);
  317. } else if ($error === 0) {
  318. $this->registerError();
  319. exit;
  320. }
  321. //registerComplete will log in user and redirects. If we reach here, the user creation failed.
  322. return false;
  323. }
  324. /**
  325. * By setting the default_password_nag to true, will inform the user about random password usage.
  326. */
  327. public function registerCompleteDefaultPasswordNag($user_id) {
  328. update_user_option($user_id, 'default_password_nag', true, true);
  329. }
  330. /**
  331. * @param $user_id
  332. * Retrieves the name, first_name, last_name and update the user data.
  333. * Also set a reminder to change the generated password.
  334. * Links the user with the provider. Set their roles. Send notification about the registration to the selected
  335. * roles. Logs the user in.
  336. *
  337. * @return bool
  338. */
  339. public function registerComplete($user_id) {
  340. if (is_wp_error($user_id) || $user_id === 0) {
  341. /** Registration failed */
  342. $this->registerError();
  343. return false;
  344. }
  345. if (class_exists('WooCommerce', false)) {
  346. if (NextendSocialLogin::$settings->get('store_name') == 1) {
  347. $first_name = $this->getAuthUserData('first_name');
  348. if (!empty($first_name)) {
  349. update_user_meta($user_id, 'billing_first_name', $first_name);
  350. }
  351. $last_name = $this->getAuthUserData('last_name');
  352. if (!empty($last_name)) {
  353. update_user_meta($user_id, 'billing_last_name', $last_name);
  354. }
  355. }
  356. }
  357. update_user_option($user_id, 'default_password_nag', true, true);
  358. $this->provider->linkUserToProviderIdentifier($user_id, $this->getAuthUserData('id'), true);
  359. do_action('nsl_registration_store_extra_input', $user_id, $this->userExtraData);
  360. do_action('nsl_register_new_user', $user_id, $this->provider);
  361. do_action('nsl_' . $this->provider->getId() . '_register_new_user', $user_id, $this->provider);
  362. $this->provider->deleteLoginPersistentData();
  363. do_action('register_new_user', $user_id);
  364. //BuddyPress - add register activity to accounts registered with social login
  365. if (class_exists('BuddyPress', false)) {
  366. if (bp_is_active('activity')) {
  367. if (!function_exists('bp_core_new_user_activity')) {
  368. require_once(buddypress()->plugin_dir . '/bp-members/bp-members-activity.php');
  369. }
  370. bp_core_new_user_activity($user_id);
  371. }
  372. }
  373. /*Ultimate Member Registration integration -> Registration notificationhoz*/
  374. $loginRestriction = NextendSocialLogin::$settings->get('login_restriction');
  375. if (class_exists('UM', false) && $loginRestriction) {
  376. //Necessary to clear the UM user cache that was generated by: um\core\User:set_gravatar
  377. UM()
  378. ->user()
  379. ->remove_cache($user_id);
  380. add_filter('um_get_current_page_url', array(
  381. $this,
  382. 'um_get_loginpage'
  383. ));
  384. do_action('um_user_register', $user_id, array(
  385. 'submitted' => array(
  386. 'timestamp' => current_time('timestamp')
  387. )
  388. ));
  389. }
  390. //Woocommerce User Email Verification integration - By default it blocks login with NSL
  391. if (class_exists('XLWUEV_Core', false) && !$loginRestriction) {
  392. update_user_meta($user_id, 'wcemailverified', 'true');
  393. }
  394. $this->shouldAutoLogin = true;
  395. return true;
  396. }
  397. private function registerError() {
  398. /** @var $wpdb WPDB */ global $wpdb;
  399. $isDebug = NextendSocialLogin::$settings->get('debug') == 1;
  400. if ($isDebug) {
  401. if ($wpdb->last_error !== '') {
  402. echo "<div id='error'><p class='wpdberror'><strong>WordPress database error:</strong> [" . esc_html($wpdb->last_error) . "]<br /><code>" . esc_html($wpdb->last_query) . "</code></p></div>";
  403. }
  404. }
  405. $this->provider->deleteLoginPersistentData();
  406. if ($isDebug) {
  407. exit;
  408. }
  409. }
  410. protected function login($user_id) {
  411. /** @var $wpdb WPDB */ global $wpdb;
  412. $loginRestriction = NextendSocialLogin::$settings->get('login_restriction');
  413. if ($loginRestriction) {
  414. $user = new WP_User($user_id);
  415. $user = apply_filters('authenticate', $user, $user->get('user_login'), null);
  416. if (is_wp_error($user)) {
  417. Notices::addError($user);
  418. $this->provider->redirectToLoginForm();
  419. return $user;
  420. }
  421. /**
  422. * Other plugins use this hook to prevent log in
  423. */
  424. $user = apply_filters('wp_authenticate_user', $user, null);
  425. if (is_wp_error($user)) {
  426. Notices::addError($user);
  427. $this->provider->redirectToLoginForm();
  428. return $user;
  429. }
  430. }
  431. $this->user_id = $user_id;
  432. add_action('nsl_' . $this->provider->getId() . '_login', array(
  433. $this->provider,
  434. 'syncProfile'
  435. ), 10, 3);
  436. $isLoginAllowed = apply_filters('nsl_' . $this->provider->getId() . '_is_login_allowed', true, $this->provider, $user_id);
  437. if ($isLoginAllowed) {
  438. wp_set_current_user($user_id);
  439. $secure_cookie = is_ssl();
  440. $secure_cookie = apply_filters('secure_signon_cookie', $secure_cookie, array());
  441. global $auth_secure_cookie; // XXX ugly hack to pass this to wp_authenticate_cookie
  442. $auth_secure_cookie = $secure_cookie;
  443. wp_set_auth_cookie($user_id, true, $secure_cookie);
  444. $user_info = get_userdata($user_id);
  445. $this->provider->logLoginDate($user_id);
  446. $addStrongerRedirect = NextendSocialLogin::$settings->get('redirect_prevent_external') == 1 || $this->provider->hasFixedRedirect();
  447. if ($addStrongerRedirect) {
  448. /**
  449. * If another plugin tries to redirect in wp_login action, we will intercept and use our redirects
  450. */
  451. add_filter('wp_redirect', array(
  452. $this,
  453. 'wp_redirect_filter'
  454. ), 10000000);
  455. /**
  456. * Fix: WishList Member exits before our redirects.
  457. */
  458. if (class_exists('WishListMember', false)) {
  459. add_filter('wishlistmember_login_redirect_override', '__return_true');
  460. }
  461. }
  462. do_action('wp_login', $user_info->user_login, $user_info);
  463. if ($addStrongerRedirect) {
  464. /**
  465. * Remove redirect interception when not needed anymore
  466. */
  467. remove_filter('wp_redirect', array(
  468. $this,
  469. 'wp_redirect_filter'
  470. ), 10000000);
  471. }
  472. $this->finishLogin();
  473. } else {
  474. $this->provider->deleteLoginPersistentData();
  475. $loginDisabledMessage = apply_filters('nsl_disabled_login_error_message', '');
  476. $loginDisabledRedirectURL = apply_filters('nsl_disabled_login_redirect_url', '');
  477. if (!empty($loginDisabledMessage)) {
  478. Notices::clear();
  479. $errors = new WP_Error();
  480. $errors->add('logindisabled', $loginDisabledMessage);
  481. Notices::addError($errors->get_error_message());
  482. }
  483. if (!empty($loginDisabledRedirectURL)) {
  484. NextendSocialProvider::redirect(__('Authentication error', 'nextend-facebook-connect'), NextendSocialLogin::enableNoticeForUrl($loginDisabledRedirectURL));
  485. }
  486. }
  487. $this->provider->redirectToLoginForm();
  488. }
  489. public function doAutoLogin($user_id) {
  490. if ($this->shouldAutoLogin) {
  491. $this->login($user_id);
  492. }
  493. }
  494. public function wp_redirect_filter($redirect) {
  495. $this->finishLogin();
  496. exit;
  497. }
  498. protected function finishLogin() {
  499. do_action('nsl_login', $this->user_id, $this->provider);
  500. do_action('nsl_' . $this->provider->getId() . '_login', $this->user_id, $this->provider, $this->access_token);
  501. $this->redirectToLastLocationLogin();
  502. }
  503. /**
  504. * Redirect the user to
  505. * -the Fixed redirect url if it is set
  506. * -where the login happened if redirect is specified in the url
  507. * -the Default redirect url if it is set, and if redirect was not specified in the url
  508. *
  509. * @param bool $notice
  510. */
  511. public function redirectToLastLocationLogin($notice = false) {
  512. if (NextendSocialLogin::$settings->get('redirect_prevent_external') == 0) {
  513. add_filter('nsl_' . $this->provider->getId() . 'default_last_location_redirect', array(
  514. $this,
  515. 'loginLastLocationRedirect'
  516. ), 9, 2);
  517. }
  518. $this->provider->redirectToLastLocation($notice);
  519. }
  520. /**
  521. * @param $redirect_to
  522. * @param $requested_redirect_to
  523. * Modifies where the user shall be redirected, after successful login.
  524. *
  525. * @return mixed|void
  526. */
  527. public function loginLastLocationRedirect($redirect_to, $requested_redirect_to) {
  528. return apply_filters('login_redirect', $redirect_to, $requested_redirect_to, wp_get_current_user());
  529. }
  530. /**
  531. * @param $user_id
  532. * @param $providerUserID
  533. * If autoLink is enabled, it links the current account with the provider.
  534. *
  535. * @return bool
  536. */
  537. public function autoLink($user_id, $providerUserID) {
  538. $isAutoLinkAllowed = true;
  539. $isAutoLinkAllowed = apply_filters('nsl_' . $this->provider->getId() . '_auto_link_allowed', $isAutoLinkAllowed, $this->provider, $user_id);
  540. if ($isAutoLinkAllowed) {
  541. $isLinkSuccessful = $this->provider->linkUserToProviderIdentifier($user_id, $providerUserID);
  542. if ($isLinkSuccessful) {
  543. return $isLinkSuccessful;
  544. } else {
  545. $this->provider->deleteLoginPersistentData();
  546. $alreadyLinkedMessage = apply_filters('nsl_already_linked_error_message', sprintf(__('We found a user with your %1$s email address. Unfortunately it belongs to a different %1$s account, so we are unable to log you in. Please use the linked %1$s account or log in with your password!', 'nextend-facebook-connect'), $this->provider->getLabel()));
  547. Notices::addError($alreadyLinkedMessage);
  548. }
  549. }
  550. return false;
  551. }
  552. /**
  553. * @return NextendSocialProvider
  554. */
  555. public function getProvider() {
  556. return $this->provider;
  557. }
  558. /**
  559. * @param $userData
  560. *
  561. * @return array
  562. * @throws NSLContinuePageRenderException
  563. */
  564. public function finalizeUserData($userData) {
  565. $data = new NextendSocialUserData($userData, $this, $this->provider);
  566. return $data->toArray();
  567. }
  568. public function require_extra_input_terms($askExtraData) {
  569. add_action('nsl_registration_form_end', array(
  570. $this,
  571. 'registration_form_terms'
  572. ), 10000);
  573. return true;
  574. }
  575. public function registration_form_terms($userData) {
  576. ?>
  577. <p>
  578. <?php
  579. $terms = $this->provider->settings->get('terms');
  580. if (empty($terms)) {
  581. $terms = NextendSocialLogin::$settings->get('terms');
  582. }
  583. if (function_exists('get_privacy_policy_url')) {
  584. $terms = str_replace('#privacy_policy_url', get_privacy_policy_url(), $terms);
  585. }
  586. echo __($terms, 'nextend-facebook-connect');
  587. ?>
  588. </p>
  589. <?php
  590. }
  591. public function syncProfileUser($user_id) {
  592. $this->provider->syncProfile($user_id, $this->provider, $this->access_token);
  593. }
  594. public function um_get_loginpage($page_url) {
  595. return um_get_core_page('login');
  596. }
  597. }