Sin descripción

provider.php 49KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  1. <?php
  2. use NSL\Notices;
  3. use NSL\Persistent\Persistent;
  4. require_once dirname(__FILE__) . '/provider-admin.php';
  5. require_once dirname(__FILE__) . '/provider-dummy.php';
  6. require_once dirname(__FILE__) . '/user.php';
  7. abstract class NextendSocialProvider extends NextendSocialProviderDummy {
  8. protected $dbID;
  9. protected $optionKey;
  10. protected $enabled = false;
  11. /** @var NextendSocialAuth */
  12. protected $client;
  13. protected $authUserData = array();
  14. protected $requiredFields = array();
  15. protected $svg = '';
  16. protected $sync_fields = array();
  17. /**
  18. * NextendSocialProvider constructor.
  19. *
  20. * @param $defaultSettings
  21. */
  22. public function __construct($defaultSettings) {
  23. if (empty($this->dbID)) {
  24. $this->dbID = $this->id;
  25. }
  26. $this->optionKey = 'nsl_' . $this->id;
  27. do_action('nsl_provider_init', $this);
  28. $this->sync_fields = apply_filters('nsl_' . $this->getId() . '_sync_fields', $this->sync_fields);
  29. $extraSettings = apply_filters('nsl_' . $this->getId() . '_extra_settings', array(
  30. 'ask_email' => 'when-empty',
  31. 'ask_user' => 'never',
  32. 'ask_password' => 'never',
  33. 'auto_link' => 'email',
  34. 'disabled_roles' => array(),
  35. 'register_roles' => array(
  36. 'default'
  37. )
  38. ));
  39. foreach ($this->getSyncFields() as $field_name => $fieldData) {
  40. $extraSettings['sync_fields/fields/' . $field_name . '/enabled'] = 0;
  41. $extraSettings['sync_fields/fields/' . $field_name . '/meta_key'] = $this->id . '_' . $field_name;
  42. }
  43. $this->settings = new NextendSocialLoginSettings($this->optionKey, array_merge(array(
  44. 'settings_saved' => '0',
  45. 'tested' => '0',
  46. 'custom_default_button' => '',
  47. 'custom_icon_button' => '',
  48. 'login_label' => '',
  49. 'register_label' => '',
  50. 'link_label' => '',
  51. 'unlink_label' => '',
  52. 'user_prefix' => '',
  53. 'user_fallback' => '',
  54. 'oauth_redirect_url' => '',
  55. 'terms' => '',
  56. 'sync_fields/link' => 0,
  57. 'sync_fields/login' => 0
  58. ), $extraSettings, $defaultSettings));
  59. $this->admin = new NextendSocialProviderAdmin($this);
  60. add_action('rest_api_init', array(
  61. $this,
  62. 'registerRedirectRESTRoute'
  63. ));
  64. }
  65. public function needPro() {
  66. return false;
  67. }
  68. /**
  69. * @return string
  70. */
  71. public function getDbID() {
  72. return $this->dbID;
  73. }
  74. public function getOptionKey() {
  75. return $this->optionKey;
  76. }
  77. public function getRawDefaultButton() {
  78. return '<div class="nsl-button nsl-button-default nsl-button-' . $this->id . '" style="background-color:' . $this->color . ';"><div class="nsl-button-svg-container">' . $this->svg . '</div><div class="nsl-button-label-container">{{label}}</div></div>';
  79. }
  80. public function getRawIconButton() {
  81. return '<div class="nsl-button nsl-button-icon nsl-button-' . $this->id . '" style="background-color:' . $this->color . ';"><div class="nsl-button-svg-container">' . $this->svg . '</div></div>';
  82. }
  83. public function getDefaultButton($label) {
  84. $button = $this->settings->get('custom_default_button');
  85. if (!empty($button)) {
  86. return str_replace('{{label}}', __($label, 'nextend-facebook-connect'), $button);
  87. }
  88. return str_replace('{{label}}', __($label, 'nextend-facebook-connect'), $this->getRawDefaultButton());
  89. }
  90. public function getIconButton() {
  91. $button = $this->settings->get('custom_icon_button');
  92. if (!empty($button)) {
  93. return $button;
  94. }
  95. return $this->getRawIconButton();
  96. }
  97. public function getLoginUrl() {
  98. $args = array('loginSocial' => $this->getId());
  99. if (isset($_REQUEST['interim-login'])) {
  100. $args['interim-login'] = 1;
  101. }
  102. return add_query_arg($args, NextendSocialLogin::getLoginUrl());
  103. }
  104. /**
  105. * Returns the url where the Provider App should redirect during the OAuth flow.
  106. *
  107. * @return string
  108. */
  109. public function getRedirectUriForOAuthFlow() {
  110. if ($this->oauthRedirectBehavior === 'rest_redirect') {
  111. return rest_url('/nextend-social-login/v1/' . $this->id . '/redirect_uri');
  112. }
  113. $args = array('loginSocial' => $this->id);
  114. return add_query_arg($args, NextendSocialLogin::getLoginUrl());
  115. }
  116. /**
  117. * Returns a single redirect URL that:
  118. * - we us as default redirect uri suggestion in the Getting Started and Fixed redirect uri pages.
  119. * - we store to detect the OAuth redirect url changes
  120. *
  121. * @return string
  122. */
  123. public function getBaseRedirectUriForAppCreation() {
  124. $redirectUri = $this->getRedirectUriForOAuthFlow();
  125. if ($this->oauthRedirectBehavior === 'default_redirect_but_app_has_restriction') {
  126. $parts = explode('?', $redirectUri);
  127. return $parts[0];
  128. }
  129. return $redirectUri;
  130. }
  131. /**
  132. * This function should return an array of URLs generated from getRedirectUri().
  133. *
  134. * We display the generated results in the Getting Started section and the Fixed redirect uri pages.
  135. * Also we use these for the OAuth redirect uri change checking.
  136. *
  137. * @return array
  138. */
  139. public function getAllRedirectUrisForAppCreation() {
  140. /**
  141. * Parameters:
  142. * 1: Array with an URL that should be added to the App by default.
  143. *
  144. * 2: The provider instance
  145. */
  146. return apply_filters('nsl_redirect_uri_override', array($this->getBaseRedirectUriForAppCreation()), $this);
  147. }
  148. /**
  149. * Enable the selected provider.
  150. *
  151. * @return bool
  152. */
  153. public function enable() {
  154. $this->enabled = true;
  155. do_action('nsl_' . $this->getId() . '_enabled');
  156. return true;
  157. }
  158. /**
  159. * Check if provider is enabled.
  160. *
  161. * @return bool
  162. */
  163. public function isEnabled() {
  164. return $this->enabled;
  165. }
  166. /**
  167. * Check if provider is verified.
  168. *
  169. * @return bool
  170. */
  171. public function isTested() {
  172. return !!$this->settings->get('tested');
  173. }
  174. /**
  175. * Check if the current redirect url of the provider matches with the one that we stored when the provider was
  176. * configured. Returns "false" if they are different, so a new URL needs to be added to the App.
  177. *
  178. * @return bool
  179. */
  180. public function checkOauthRedirectUrl() {
  181. $oauth_redirect_url = $this->settings->get('oauth_redirect_url');
  182. $redirectUrls = $this->getAllRedirectUrisForAppCreation();
  183. if (is_array($redirectUrls)) {
  184. /**
  185. * Before 3.1.2 we saved the default redirect url of the provider ( e.g.:
  186. * https://example.com/wp-login.php?loginSocial=twitter ) for the OAuth check. However, some providers ( e.g.
  187. * Microsoft ) can use the REST API URL as redirect url. In these cases if the URL of the OAuth page was changed,
  188. * we gave a false warning for such providers.
  189. *
  190. * We shouldn't throw warnings for users who have the redirect uri stored still with the old format.
  191. * For this reason we need to push the legacy redirect url into the $redirectUrls array, too!
  192. */
  193. $legacyRedirectURL = add_query_arg(array('loginSocial' => $this->getId()), NextendSocialLogin::getLoginUrl());
  194. if (!in_array($legacyRedirectURL, $redirectUrls)) {
  195. $redirectUrls[] = $legacyRedirectURL;
  196. }
  197. if (in_array($oauth_redirect_url, $redirectUrls)) {
  198. return true;
  199. }
  200. }
  201. return false;
  202. }
  203. public function updateOauthRedirectUrl() {
  204. $this->settings->update(array(
  205. 'oauth_redirect_url' => $this->getBaseRedirectUriForAppCreation()
  206. ));
  207. }
  208. /**
  209. * @return array
  210. */
  211. public function getRequiredFields() {
  212. return $this->requiredFields;
  213. }
  214. /**
  215. * Get the current state of a Provider.
  216. *
  217. * @return string
  218. */
  219. public function getState() {
  220. foreach ($this->requiredFields as $name => $label) {
  221. $value = $this->settings->get($name);
  222. if (empty($value)) {
  223. return 'not-configured';
  224. }
  225. }
  226. if (!$this->isTested()) {
  227. return 'not-tested';
  228. }
  229. if (!$this->isEnabled()) {
  230. return 'disabled';
  231. }
  232. return 'enabled';
  233. }
  234. /**
  235. * Authenticate and connect with the provider.
  236. */
  237. public function connect() {
  238. try {
  239. $this->doAuthenticate();
  240. } catch (NSLContinuePageRenderException $e) {
  241. // This is not an error. We allow the page to continue the normal display flow and later we inject our things.
  242. // Used by Theme my login function where we override the shortcode and we display our email request.
  243. } catch (Exception $e) {
  244. $this->onError($e);
  245. }
  246. }
  247. /**
  248. * @return NextendSocialAuth
  249. */
  250. protected abstract function getClient();
  251. public function getTestUrl() {
  252. return $this->getClient()
  253. ->getTestUrl();
  254. }
  255. /**
  256. * @throws NSLContinuePageRenderException
  257. */
  258. protected function doAuthenticate() {
  259. if (!headers_sent()) {
  260. //All In One WP Security sets a LOCATION header, so we need to remove it to do a successful test.
  261. if (function_exists('header_remove')) {
  262. header_remove("LOCATION");
  263. } else {
  264. header('LOCATION:', true); //Under PHP 5.3
  265. }
  266. }
  267. //If it is a real login action, add the actions for the connection.
  268. if (!$this->isTest()) {
  269. add_action($this->id . '_login_action_before', array(
  270. $this,
  271. 'liveConnectBefore'
  272. ));
  273. add_action($this->id . '_login_action_redirect', array(
  274. $this,
  275. 'liveConnectRedirect'
  276. ));
  277. add_action($this->id . '_login_action_get_user_profile', array(
  278. $this,
  279. 'liveConnectGetUserProfile'
  280. ));
  281. $interim_login = isset($_REQUEST['interim-login']);
  282. if ($interim_login) {
  283. Persistent::set($this->id . '_interim_login', 1);
  284. }
  285. /**
  286. * Store the settings for the provider login.
  287. */
  288. $display = isset($_REQUEST['display']);
  289. if ($display && $_REQUEST['display'] == 'popup') {
  290. Persistent::set($this->id . '_display', 'popup');
  291. }
  292. } else { //This is just to verify the settings.
  293. add_action($this->id . '_login_action_get_user_profile', array(
  294. $this,
  295. 'testConnectGetUserProfile'
  296. ));
  297. }
  298. // Redirect if the registration is blocked by another Plugin like Cerber.
  299. if (function_exists('cerber_is_allowed')) {
  300. $allowed = cerber_is_allowed();
  301. if (!$allowed) {
  302. global $wp_cerber;
  303. $error = $wp_cerber->getErrorMsg();
  304. Notices::addError($error);
  305. $this->redirectToLoginForm();
  306. }
  307. }
  308. do_action($this->id . '_login_action_before', $this);
  309. $client = $this->getClient();
  310. $accessTokenData = $this->getAnonymousAccessToken();
  311. $client->checkError();
  312. do_action($this->id . '_login_action_redirect', $this);
  313. /**
  314. * Check if we have an accessToken and a code.
  315. * If there is no access token and code it redirects to the Authorization Url.
  316. */
  317. if (!$accessTokenData && !$client->hasAuthenticateData()) {
  318. header('LOCATION: ' . $client->createAuthUrl());
  319. exit;
  320. } else {
  321. /**
  322. * If the code is OK but there is no access token, authentication is necessary.
  323. */
  324. if (!$accessTokenData) {
  325. $accessTokenData = $client->authenticate();
  326. $accessTokenData = $this->requestLongLivedToken($accessTokenData);
  327. /**
  328. * store the access token
  329. */
  330. $this->setAnonymousAccessToken($accessTokenData);
  331. } else {
  332. $client->setAccessTokenData($accessTokenData);
  333. }
  334. /**
  335. * if the login display was in popup window,
  336. * in the source window the user is redirected to the login url.
  337. * and the popup window must be closed
  338. */
  339. if (Persistent::get($this->id . '_display') == 'popup') {
  340. Persistent::delete($this->id . '_display');
  341. ?>
  342. <!doctype html>
  343. <html lang=en>
  344. <head>
  345. <meta charset=utf-8>
  346. <title><?php _e('Authentication successful', 'nextend-facebook-connect'); ?></title>
  347. <script type="text/javascript">
  348. try {
  349. if (window.opener !== null && window.opener !== window) {
  350. var sameOrigin = true;
  351. try {
  352. var currentOrigin = window.location.protocol + '//' + window.location.hostname;
  353. if (window.opener.location.href.substring(0, currentOrigin.length) !== currentOrigin) {
  354. sameOrigin = false;
  355. }
  356. } catch (e) {
  357. /**
  358. * Blocked cross origin
  359. */
  360. sameOrigin = false;
  361. }
  362. if (sameOrigin) {
  363. var url = <?php echo wp_json_encode($this->getLoginUrl()); ?>;
  364. if (typeof window.opener.nslRedirect === 'function') {
  365. window.opener.nslRedirect(url);
  366. } else {
  367. window.opener.location = url;
  368. }
  369. window.close();
  370. } else {
  371. window.location.reload(true);
  372. }
  373. } else {
  374. window.location.reload(true);
  375. }
  376. } catch (e) {
  377. window.location.reload(true);
  378. }
  379. </script>
  380. </head>
  381. <body><a href="<?php echo esc_url($this->getLoginUrl()); ?>"><?php echo 'Continue...'; ?></a></body>
  382. </html>
  383. <?php
  384. exit;
  385. }
  386. /**
  387. * Retrieves the userinfo trough the REST API and connect with the provider.
  388. * Redirects to the last location.
  389. */
  390. $this->authUserData = $this->getCurrentUserInfo();
  391. do_action($this->id . '_login_action_get_user_profile', $accessTokenData);
  392. }
  393. }
  394. /**
  395. * @param $access_token
  396. * Connect with the selected provider.
  397. * After a successful login, we no longer need the previous persistent data.
  398. */
  399. public function liveConnectGetUserProfile($access_token) {
  400. $socialUser = new NextendSocialUser($this, $access_token);
  401. $socialUser->liveConnectGetUserProfile();
  402. $this->deleteLoginPersistentData();
  403. $this->redirectToLastLocationOther(true);
  404. }
  405. /**
  406. * @param $user_id
  407. * @param $providerIdentifier
  408. * @param $isRegister
  409. * Insert the userid into the wp_social_users table,
  410. * in this way a link is created between user accounts and the providers.
  411. *
  412. * @return bool
  413. */
  414. public function linkUserToProviderIdentifier($user_id, $providerIdentifier, $isRegister = false) {
  415. /** @var $wpdb WPDB */ global $wpdb;
  416. $connectedProviderID = $this->getProviderIdentifierByUserID($user_id);
  417. if ($connectedProviderID !== null) {
  418. if ($connectedProviderID == $providerIdentifier) {
  419. // This provider already linked to this user
  420. return true;
  421. }
  422. // User already have this provider attached to his account with different provider id.
  423. return false;
  424. }
  425. if ($isRegister) {
  426. /**
  427. * This is a register action.
  428. */
  429. $wpdb->insert($wpdb->prefix . 'social_users', array(
  430. 'ID' => $user_id,
  431. 'type' => $this->dbID,
  432. 'identifier' => $providerIdentifier,
  433. 'register_date' => current_time('mysql'),
  434. 'link_date' => current_time('mysql'),
  435. ), array(
  436. '%d',
  437. '%s',
  438. '%s',
  439. '%s',
  440. '%s'
  441. ));
  442. } else {
  443. /**
  444. * This is a link action.
  445. */
  446. $wpdb->insert($wpdb->prefix . 'social_users', array(
  447. 'ID' => $user_id,
  448. 'type' => $this->dbID,
  449. 'identifier' => $providerIdentifier,
  450. 'link_date' => current_time('mysql'),
  451. ), array(
  452. '%d',
  453. '%s',
  454. '%s',
  455. '%s'
  456. ));
  457. }
  458. do_action('nsl_' . $this->getId() . '_link_user', $user_id, $this->getId());
  459. return true;
  460. }
  461. public function getUserIDByProviderIdentifier($identifier) {
  462. /** @var $wpdb WPDB */ global $wpdb;
  463. return $wpdb->get_var($wpdb->prepare('SELECT ID FROM `' . $wpdb->prefix . 'social_users` WHERE type = %s AND identifier = %s', array(
  464. $this->dbID,
  465. $identifier
  466. )));
  467. }
  468. protected function getProviderIdentifierByUserID($user_id) {
  469. /** @var $wpdb WPDB */ global $wpdb;
  470. return $wpdb->get_var($wpdb->prepare('SELECT identifier FROM `' . $wpdb->prefix . 'social_users` WHERE type = %s AND ID = %s', array(
  471. $this->dbID,
  472. $user_id
  473. )));
  474. }
  475. /**
  476. * @param $user_id
  477. * Delete the link between the user account and the provider.
  478. */
  479. public function removeConnectionByUserID($user_id) {
  480. /** @var $wpdb WPDB */ global $wpdb;
  481. $wpdb->query($wpdb->prepare('DELETE FROM `' . $wpdb->prefix . 'social_users` WHERE type = %s AND ID = %d', array(
  482. $this->dbID,
  483. $user_id
  484. )));
  485. }
  486. protected function unlinkUser() {
  487. //Filter to disable unlinking social accounts
  488. $unlinkAllowed = apply_filters('nsl_allow_unlink', true);
  489. if ($unlinkAllowed) {
  490. $user_info = wp_get_current_user();
  491. if ($user_info->ID) {
  492. $this->removeConnectionByUserID($user_info->ID);
  493. do_action('nsl_unlink_user', $user_info->ID, $this->getId());
  494. return true;
  495. }
  496. }
  497. return false;
  498. }
  499. /**
  500. * If the current user has linked the account with a provider return the user identifier else false.
  501. *
  502. * @return bool|null|string
  503. */
  504. public function isCurrentUserConnected() {
  505. /** @var $wpdb WPDB */ global $wpdb;
  506. $current_user = wp_get_current_user();
  507. $ID = $wpdb->get_var($wpdb->prepare('SELECT identifier FROM `' . $wpdb->prefix . 'social_users` WHERE type LIKE %s AND ID = %d', array(
  508. $this->dbID,
  509. $current_user->ID
  510. )));
  511. if ($ID === null) {
  512. return false;
  513. }
  514. return $ID;
  515. }
  516. /**
  517. * @param $user_id
  518. * If a user has linked the account with a provider return the user identifier else false.
  519. *
  520. * @return bool|null|string
  521. */
  522. public function isUserConnected($user_id) {
  523. /** @var $wpdb WPDB */ global $wpdb;
  524. $ID = $wpdb->get_var($wpdb->prepare('SELECT identifier FROM `' . $wpdb->prefix . 'social_users` WHERE type LIKE %s AND ID = %d', array(
  525. $this->dbID,
  526. $user_id
  527. )));
  528. if ($ID === null) {
  529. return false;
  530. }
  531. return $ID;
  532. }
  533. public function findUserByAccessToken($access_token) {
  534. return $this->getUserIDByProviderIdentifier($this->findSocialIDByAccessToken($access_token));
  535. }
  536. public function findSocialIDByAccessToken($access_token) {
  537. $client = $this->getClient();
  538. $client->setAccessTokenData($access_token);
  539. $this->authUserData = $this->getCurrentUserInfo();
  540. return $this->getAuthUserData('id');
  541. }
  542. public function getConnectButton($buttonStyle = 'default', $redirectTo = null, $trackerData = false, $labelType = 'login') {
  543. $arg = array();
  544. if (!empty($redirectTo)) {
  545. $arg['redirect'] = urlencode($redirectTo);
  546. } else if (!empty($_GET['redirect_to'])) {
  547. $arg['redirect'] = urlencode($_GET['redirect_to']);
  548. } else {
  549. $currentPageUrl = NextendSocialLogin::getCurrentPageURL();
  550. if ($currentPageUrl !== false) {
  551. $arg['redirect'] = urlencode($currentPageUrl);
  552. }
  553. }
  554. if ($trackerData !== false) {
  555. $arg['trackerdata'] = urlencode($trackerData);
  556. $arg['trackerdata_hash'] = urlencode(wp_hash($trackerData));
  557. }
  558. $label = $this->settings->get('login_label');
  559. $useCustomRegisterLabel = NextendSocialLogin::$settings->get('custom_register_label');
  560. if ($labelType == 'register' && $useCustomRegisterLabel) {
  561. $label = $this->settings->get('register_label');;
  562. }
  563. switch ($buttonStyle) {
  564. case 'icon':
  565. $button = $this->getIconButton();
  566. break;
  567. default:
  568. $button = $this->getDefaultButton($label);
  569. break;
  570. }
  571. return '<a href="' . esc_url(add_query_arg($arg, $this->getLoginUrl())) . '" rel="nofollow" aria-label="' . esc_attr__($label) . '" data-plugin="nsl" data-action="connect" data-provider="' . esc_attr($this->getId()) . '" data-popupwidth="' . $this->getPopupWidth() . '" data-popupheight="' . $this->getPopupHeight() . '">' . $button . '</a>';
  572. }
  573. public function getLinkButton() {
  574. $args = array(
  575. 'action' => 'link'
  576. );
  577. $redirect = NextendSocialLogin::getCurrentPageURL();
  578. if ($redirect !== false) {
  579. $args['redirect'] = urlencode($redirect);
  580. }
  581. return '<a href="' . esc_url(add_query_arg($args, $this->getLoginUrl())) . '" style="text-decoration:none;display:inline-block;box-shadow:none;" data-plugin="nsl" data-action="link" data-provider="' . esc_attr($this->getId()) . '" data-popupwidth="' . $this->getPopupWidth() . '" data-popupheight="' . $this->getPopupHeight() . '" aria-label="' . esc_attr__($this->settings->get('link_label')) . '">' . $this->getDefaultButton($this->settings->get('link_label')) . '</a>';
  582. }
  583. public function getUnLinkButton() {
  584. $args = array(
  585. 'action' => 'unlink'
  586. );
  587. $redirect = NextendSocialLogin::getCurrentPageURL();
  588. if ($redirect !== false) {
  589. $args['redirect'] = urlencode($redirect);
  590. }
  591. return '<a href="' . esc_url(add_query_arg($args, $this->getLoginUrl())) . '" style="text-decoration:none;display:inline-block;box-shadow:none;" data-plugin="nsl" data-action="unlink" data-provider="' . esc_attr($this->getId()) . '" aria-label="' . esc_attr__($this->settings->get('unlink_label')) . '">' . $this->getDefaultButton($this->settings->get('unlink_label')) . '</a>';
  592. }
  593. public function redirectToLoginForm() {
  594. self::redirect(__('Authentication error', 'nextend-facebook-connect'), NextendSocialLogin::enableNoticeForUrl(NextendSocialLogin::getLoginUrl()));
  595. }
  596. /**
  597. * -Allows for logged in users to unlink their account from a provider, if it was linked, and
  598. * redirects to the last location.
  599. * -During linking process, store the action as link. After the linking process is finished,
  600. * delete this stored info and redirects to the last location.
  601. */
  602. public function liveConnectBefore() {
  603. if (is_user_logged_in() && $this->isCurrentUserConnected()) {
  604. if (isset($_GET['action']) && $_GET['action'] == 'unlink') {
  605. if ($this->unlinkUser()) {
  606. Notices::addSuccess(__('Unlink successful.', 'nextend-facebook-connect'));
  607. } else {
  608. Notices::addError(__('Unlink is not allowed!', 'nextend-facebook-connect'));
  609. }
  610. }
  611. $this->redirectToLastLocationOther(true);
  612. exit;
  613. }
  614. if (isset($_GET['action']) && $_GET['action'] == 'link') {
  615. Persistent::set($this->id . '_action', 'link');
  616. }
  617. if (is_user_logged_in() && Persistent::get($this->id . '_action') != 'link') {
  618. $this->deleteLoginPersistentData();
  619. $this->redirectToLastLocationOther();
  620. exit;
  621. }
  622. }
  623. /**
  624. * Store where the user logged in.
  625. */
  626. public function liveConnectRedirect() {
  627. if (!empty($_GET['trackerdata']) && !empty($_GET['trackerdata_hash'])) {
  628. if (wp_hash($_GET['trackerdata']) === $_GET['trackerdata_hash']) {
  629. Persistent::set('trackerdata', $_GET['trackerdata']);
  630. }
  631. }
  632. if (!empty($_GET['redirect'])) {
  633. Persistent::set('redirect', $_GET['redirect']);
  634. }
  635. }
  636. public function redirectToLastLocation($notice = false) {
  637. $url = $this->getLastLocationRedirectTo();
  638. if (Persistent::get($this->id . '_interim_login') == 1) {
  639. $this->deleteLoginPersistentData();
  640. $args['interim_login'] = 'nsl';
  641. $url = add_query_arg($args, NextendSocialLogin::getLoginUrl('login'));
  642. if ($notice) {
  643. $url = NextendSocialLogin::enableNoticeForUrl($url);
  644. }
  645. self::redirect(__('Authentication successful', 'nextend-facebook-connect'), $url);
  646. exit;
  647. }
  648. if ($notice) {
  649. $url = NextendSocialLogin::enableNoticeForUrl($url);
  650. }
  651. self::redirect(__('Authentication successful', 'nextend-facebook-connect'), $url);
  652. }
  653. /**
  654. * @param bool $notice
  655. */
  656. protected function redirectToLastLocationOther($notice = false) {
  657. $this->redirectToLastLocation($notice);
  658. }
  659. protected function validateRedirect($location) {
  660. $location = wp_sanitize_redirect($location);
  661. return wp_validate_redirect($location, apply_filters('wp_safe_redirect_fallback', admin_url(), 302));
  662. }
  663. public function hasFixedRedirect() {
  664. if (NextendSocialLogin::$WPLoginCurrentFlow == 'register') {
  665. $fixedRedirect = NextendSocialLogin::$settings->get('redirect_reg');
  666. $fixedRedirect = apply_filters($this->id . '_register_redirect_url', $fixedRedirect, $this);
  667. if (!empty($fixedRedirect)) {
  668. return true;
  669. }
  670. } else if (NextendSocialLogin::$WPLoginCurrentFlow == 'login') {
  671. $fixedRedirect = NextendSocialLogin::$settings->get('redirect');
  672. $fixedRedirect = apply_filters($this->id . '_login_redirect_url', $fixedRedirect, $this);
  673. if (!empty($fixedRedirect)) {
  674. return true;
  675. }
  676. }
  677. return false;
  678. }
  679. /**
  680. * If fixed redirect url is set, redirect to fixed redirect url.
  681. * If fixed redirect url is not set, but redirect is in the url redirect to the $_GET['redirect'].
  682. * If fixed redirect url is not set and there is no redirect in the url, redirects to the default redirect url if it
  683. * is set.
  684. * Else redirect to the site url.
  685. *
  686. * @return mixed|void
  687. */
  688. protected function getLastLocationRedirectTo() {
  689. $redirect_to = '';
  690. $requested_redirect_to = '';
  691. $fixedRedirect = '';
  692. if (NextendSocialLogin::$WPLoginCurrentFlow == 'register') {
  693. $fixedRedirect = NextendSocialLogin::$settings->get('redirect_reg');
  694. $fixedRedirect = apply_filters($this->id . '_register_redirect_url', $fixedRedirect, $this);
  695. } else if (NextendSocialLogin::$WPLoginCurrentFlow == 'login') {
  696. $fixedRedirect = NextendSocialLogin::$settings->get('redirect');
  697. $fixedRedirect = apply_filters($this->id . '_login_redirect_url', $fixedRedirect, $this);
  698. }
  699. if (!empty($fixedRedirect)) {
  700. $redirect_to = $fixedRedirect;
  701. } else {
  702. $requested_redirect_to = Persistent::get('redirect');
  703. if (!empty($requested_redirect_to)) {
  704. if (empty($requested_redirect_to) || !NextendSocialLogin::isAllowedRedirectUrl($requested_redirect_to)) {
  705. if (!empty($_GET['redirect']) && NextendSocialLogin::isAllowedRedirectUrl($_GET['redirect'])) {
  706. $requested_redirect_to = $_GET['redirect'];
  707. } else {
  708. $requested_redirect_to = '';
  709. }
  710. }
  711. if (empty($requested_redirect_to)) {
  712. $redirect_to = site_url();
  713. } else {
  714. $redirect_to = $requested_redirect_to;
  715. }
  716. $redirect_to = wp_sanitize_redirect($redirect_to);
  717. $redirect_to = wp_validate_redirect($redirect_to, site_url());
  718. $redirect_to = $this->validateRedirect($redirect_to);
  719. } else if (!empty($_GET['redirect']) && NextendSocialLogin::isAllowedRedirectUrl($_GET['redirect'])) {
  720. $redirect_to = $_GET['redirect'];
  721. $redirect_to = wp_sanitize_redirect($redirect_to);
  722. $redirect_to = wp_validate_redirect($redirect_to, site_url());
  723. $redirect_to = $this->validateRedirect($redirect_to);
  724. }
  725. if (empty($redirect_to)) {
  726. $defaultRedirect = '';
  727. if (NextendSocialLogin::$WPLoginCurrentFlow == 'register') {
  728. $defaultRedirect = NextendSocialLogin::$settings->get('default_redirect_reg');
  729. $defaultRedirect = apply_filters($this->id . '_default_register_redirect_url', $defaultRedirect, $this);
  730. } else if (NextendSocialLogin::$WPLoginCurrentFlow == 'login') {
  731. $defaultRedirect = NextendSocialLogin::$settings->get('default_redirect');
  732. $defaultRedirect = apply_filters($this->id . '_default_[login_redirect_url', $defaultRedirect, $this);
  733. }
  734. if ((!empty($defaultRedirect))) {
  735. $redirect_to = $defaultRedirect;
  736. }
  737. }
  738. $redirect_to = apply_filters('nsl_' . $this->getId() . 'default_last_location_redirect', $redirect_to, $requested_redirect_to);
  739. }
  740. if ($redirect_to == '' || $redirect_to == $this->getLoginUrl()) {
  741. $redirect_to = site_url();
  742. }
  743. Persistent::delete('redirect');
  744. return apply_filters('nsl_' . $this->getId() . 'last_location_redirect', $redirect_to, $requested_redirect_to);
  745. }
  746. /**
  747. * @param $user_id
  748. * @param $provider NextendSocialProvider
  749. * @param $access_token string
  750. */
  751. public function syncProfile($user_id, $provider, $access_token) {
  752. }
  753. /**
  754. * Check if a logged in user with manage_options capability, want to verify their provider settings.
  755. *
  756. * @return bool
  757. */
  758. public function isTest() {
  759. if (is_user_logged_in() && current_user_can('manage_options')) {
  760. if (isset($_REQUEST['test'])) {
  761. Persistent::set('test', 1);
  762. return true;
  763. } else if (Persistent::get('test') == 1) {
  764. return true;
  765. }
  766. }
  767. return false;
  768. }
  769. /**
  770. * Make the current provider in verified mode, and update the oauth_redirect_url.
  771. */
  772. public function testConnectGetUserProfile() {
  773. $this->deleteLoginPersistentData();
  774. $this->settings->update(array(
  775. 'tested' => 1,
  776. 'oauth_redirect_url' => $this->getBaseRedirectUriForAppCreation()
  777. ));
  778. Notices::addSuccess(__('The test was successful', 'nextend-facebook-connect'));
  779. ?>
  780. <!doctype html>
  781. <html lang=en>
  782. <head>
  783. <meta charset=utf-8>
  784. <title><?php _e('The test was successful', 'nextend-facebook-connect'); ?></title>
  785. <script type="text/javascript">
  786. window.opener.location.reload(true);
  787. window.close();
  788. </script>
  789. </head>
  790. </html>
  791. <?php
  792. exit;
  793. }
  794. /**
  795. * @param $accessToken
  796. * Store the accessToken data.
  797. */
  798. protected function setAnonymousAccessToken($accessToken) {
  799. Persistent::set($this->id . '_at', $accessToken);
  800. }
  801. protected function getAnonymousAccessToken() {
  802. return Persistent::get($this->id . '_at');
  803. }
  804. public function deleteLoginPersistentData() {
  805. Persistent::delete($this->id . '_at');
  806. Persistent::delete($this->id . '_interim_login');
  807. Persistent::delete($this->id . '_display');
  808. Persistent::delete($this->id . '_action');
  809. Persistent::delete('test');
  810. }
  811. /**
  812. * @param $e Exception
  813. */
  814. protected function onError($e) {
  815. if (NextendSocialLogin::$settings->get('debug') == 1 || $this->isTest()) {
  816. header('HTTP/1.0 401 Unauthorized');
  817. echo "Error: " . $e->getMessage() . "\n";
  818. } else {
  819. //@TODO we might need to make difference between user cancelled auth and error and redirect the user based on that.
  820. $url = $this->getLastLocationRedirectTo();
  821. ?>
  822. <!doctype html>
  823. <html lang=en>
  824. <head>
  825. <meta charset=utf-8>
  826. <title><?php echo __('Authentication failed', 'nextend-facebook-connect'); ?></title>
  827. <script type="text/javascript">
  828. try {
  829. if (window.opener !== null && window.opener !== window) {
  830. var sameOrigin = true;
  831. try {
  832. var currentOrigin = window.location.protocol + '//' + window.location.hostname;
  833. if (window.opener.location.href.substring(0, currentOrigin.length) !== currentOrigin) {
  834. sameOrigin = false;
  835. }
  836. } catch (e) {
  837. /**
  838. * Blocked cross origin
  839. */
  840. sameOrigin = false;
  841. }
  842. if (sameOrigin) {
  843. window.close();
  844. }
  845. }
  846. } catch (e) {
  847. }
  848. window.location = <?php echo wp_json_encode($url); ?>;
  849. </script>
  850. <meta http-equiv="refresh" content="0;<?php echo esc_attr($url); ?>">
  851. </head>
  852. <body>
  853. </body>
  854. </html>
  855. <?php
  856. }
  857. $this->deleteLoginPersistentData();
  858. exit;
  859. }
  860. protected function saveUserData($user_id, $key, $data) {
  861. update_user_meta($user_id, $this->id . '_' . $key, $data);
  862. }
  863. protected function getUserData($user_id, $key) {
  864. return get_user_meta($user_id, $this->id . '_' . $key, true);
  865. }
  866. public function getAccessToken($user_id) {
  867. return $this->getUserData($user_id, 'access_token');
  868. }
  869. /**
  870. * @param $user_id
  871. *
  872. * @return bool
  873. * @deprecated
  874. *
  875. */
  876. public function getAvatar($user_id) {
  877. return false;
  878. }
  879. /**
  880. * @return array
  881. */
  882. protected function getCurrentUserInfo() {
  883. return array();
  884. }
  885. protected function requestLongLivedToken($accessTokenData) {
  886. return $accessTokenData;
  887. }
  888. /**
  889. * @param $key
  890. *
  891. * @return string
  892. */
  893. public function getAuthUserData($key) {
  894. return '';
  895. }
  896. /**
  897. * @param $title
  898. * @param $url
  899. * Redirect the source of the popup window to a specified url.
  900. */
  901. public static function redirect($title, $url) {
  902. ?>
  903. <!doctype html>
  904. <html lang=en>
  905. <head>
  906. <meta charset=utf-8>
  907. <title><?php echo $title; ?></title>
  908. <script type="text/javascript">
  909. try {
  910. if (window.opener !== null && window.opener !== window) {
  911. var sameOrigin = true;
  912. try {
  913. var currentOrigin = window.location.protocol + '//' + window.location.hostname;
  914. if (window.opener.location.href.substring(0, currentOrigin.length) !== currentOrigin) {
  915. sameOrigin = false;
  916. }
  917. } catch (e) {
  918. /**
  919. * Blocked cross origin
  920. */
  921. sameOrigin = false;
  922. }
  923. if (sameOrigin) {
  924. window.opener.location = <?php echo wp_json_encode($url); ?>;
  925. window.close();
  926. }
  927. }
  928. } catch (e) {
  929. }
  930. window.location = <?php echo wp_json_encode($url); ?>;
  931. </script>
  932. <meta http-equiv="refresh" content="0;<?php echo esc_attr($url); ?>">
  933. </head>
  934. <body>
  935. </body>
  936. </html>
  937. <?php
  938. exit;
  939. }
  940. public function getSyncFields() {
  941. return $this->sync_fields;
  942. }
  943. public function hasSyncFields() {
  944. return !empty($this->sync_fields);
  945. }
  946. public function validateSettings($newData, $postedData) {
  947. return $newData;
  948. }
  949. protected function needUpdateAvatar($user_id) {
  950. return apply_filters('nsl_avatar_store', NextendSocialLogin::$settings->get('avatar_store'), $user_id, $this);
  951. }
  952. protected function updateAvatar($user_id, $url) {
  953. do_action('nsl_update_avatar', $this, $user_id, $url);
  954. }
  955. public function exportPersonalData($userID) {
  956. $data = array();
  957. $socialID = $this->isUserConnected($userID);
  958. if ($socialID !== false) {
  959. $data[] = array(
  960. 'name' => $this->getLabel() . ' ' . __('Identifier', 'nextend-facebook-connect'),
  961. 'value' => $socialID,
  962. );
  963. }
  964. $accessToken = $this->getAccessToken($userID);
  965. if (!empty($accessToken)) {
  966. $data[] = array(
  967. 'name' => $this->getLabel() . ' ' . __('Access token', 'nextend-facebook-connect'),
  968. 'value' => $accessToken,
  969. );
  970. }
  971. $profilePicture = $this->getUserData($userID, 'profile_picture');
  972. if (!empty($profilePicture)) {
  973. $data[] = array(
  974. 'name' => $this->getLabel() . ' ' . __('Profile Picture'),
  975. 'value' => $profilePicture,
  976. );
  977. }
  978. foreach ($this->getSyncFields() as $fieldName => $fieldData) {
  979. $meta_key = $this->settings->get('sync_fields/fields/' . $fieldName . '/meta_key');
  980. if (!empty($meta_key)) {
  981. $value = get_user_meta($userID, $meta_key, true);
  982. if (!empty($value)) {
  983. $data[] = array(
  984. 'name' => $this->getLabel() . ' ' . $fieldData['label'],
  985. 'value' => $value
  986. );
  987. }
  988. }
  989. }
  990. return $data;
  991. }
  992. protected function storeAccessToken($userID, $accessToken) {
  993. if (NextendSocialLogin::$settings->get('store_access_token') == 1) {
  994. $this->saveUserData($userID, 'access_token', $accessToken);
  995. }
  996. }
  997. public function getSyncDataFieldDescription($fieldName) {
  998. return '';
  999. }
  1000. /**
  1001. * @param $user_id
  1002. * Update social_users table with login date of the user.
  1003. */
  1004. public function logLoginDate($user_id) {
  1005. /** @var $wpdb WPDB */ global $wpdb;
  1006. $wpdb->update($wpdb->prefix . 'social_users', array('login_date' => current_time('mysql'),), array(
  1007. 'ID' => $user_id,
  1008. 'type' => $this->dbID
  1009. ), array(
  1010. '%s',
  1011. '%s'
  1012. ));
  1013. }
  1014. public function registerRedirectRESTRoute() {
  1015. if ($this->oauthRedirectBehavior === 'rest_redirect') {
  1016. register_rest_route('nextend-social-login/v1', $this->id . '/redirect_uri', array(
  1017. 'methods' => WP_REST_Server::READABLE,
  1018. 'callback' => array(
  1019. $this,
  1020. 'redirectToProviderEndpointWithStateAndCode'
  1021. ),
  1022. 'args' => array(
  1023. 'state' => array(
  1024. 'required' => true,
  1025. ),
  1026. 'code' => array(
  1027. 'required' => true,
  1028. )
  1029. ),
  1030. 'permission_callback' => '__return_true',
  1031. ));
  1032. }
  1033. }
  1034. /**
  1035. * @param WP_REST_Request $request Full details about the request.
  1036. *
  1037. * Registers a REST API endpoints for a provider. This endpoint handles the redirect to the login endpoint of the
  1038. * currently used provider. The state and code GET parameters will be added to the login URL, so we can imitate as
  1039. * if the provider would already returned the state and code parameters to the original login url.
  1040. *
  1041. * @return WP_Error|WP_REST_Response
  1042. */
  1043. public function redirectToProviderEndpointWithStateAndCode($request) {
  1044. $params = $request->get_params();
  1045. $errorMessage = '';
  1046. if (!empty($params['state']) && !empty($params['code'])) {
  1047. $provider = NextendSocialLogin::$allowedProviders[$this->id];
  1048. try {
  1049. $providerEndpoint = $provider->getLoginUrl();
  1050. if (defined('WPML_PLUGIN_BASENAME')) {
  1051. $providerEndpoint = $provider->getTranslatedLoginURLForRestRedirect();
  1052. }
  1053. $providerEndpointWithStateAndCode = add_query_arg(array(
  1054. 'state' => $params['state'],
  1055. 'code' => $params['code']
  1056. ), $providerEndpoint);
  1057. wp_safe_redirect($providerEndpointWithStateAndCode);
  1058. exit;
  1059. } catch (Exception $e) {
  1060. $errorMessage = $e->getMessage();
  1061. }
  1062. } else {
  1063. if (empty($params['state']) && empty($params['code'])) {
  1064. $errorMessage = 'The code and state parameters are empty!';
  1065. } else if (empty($params['state'])) {
  1066. $errorMessage = 'The state parameter is empty!';
  1067. } else {
  1068. $errorMessage = 'The code parameter is empty!';
  1069. }
  1070. }
  1071. return new WP_Error('error', $errorMessage);
  1072. }
  1073. /**
  1074. * Generates a single translated login URL where the REST /redirect_uri endpoint of the currently used provider
  1075. * should redirect to instead of the original login url.
  1076. *
  1077. * @return string
  1078. */
  1079. public function getTranslatedLoginURLForRestRedirect() {
  1080. $originalLoginUrl = $this->getLoginUrl();
  1081. /**
  1082. * We should attempt to generate translated login URLs only if WPML is active and there is a language code defined.
  1083. */
  1084. if (defined('WPML_PLUGIN_BASENAME') && defined('ICL_LANGUAGE_CODE')) {
  1085. global $sitepress;
  1086. $languageCode = ICL_LANGUAGE_CODE;
  1087. if ($sitepress && method_exists($sitepress, 'get_active_languages') && $languageCode) {
  1088. $WPML_active_languages = $sitepress->get_active_languages();
  1089. if (count($WPML_active_languages) > 1) {
  1090. /**
  1091. * Fix:
  1092. * When WPML has the language URL format set to "Language name added as a parameter",
  1093. * we can not pass that parameter in the Authorization request in some cases ( e.g.: Microsoft ).
  1094. * In these cases the user will end up redirected to the redirect URL without language parameter,
  1095. * so after the login we won't be able to redirect them to registration flow page of the corresponding language.
  1096. * In these cases we need to use the language code according to the url where we should redirect after the login.
  1097. */
  1098. $WPML_language_url_format = false;
  1099. if (method_exists($sitepress, 'get_setting')) {
  1100. $WPML_language_url_format = $sitepress->get_setting('language_negotiation_type');
  1101. }
  1102. if ($WPML_language_url_format && $WPML_language_url_format == 3) {
  1103. $persistentRedirect = Persistent::get('redirect');
  1104. if ($persistentRedirect) {
  1105. $persistentRedirectQueryParams = array();
  1106. $persistentRedirectQueryString = parse_url($persistentRedirect, PHP_URL_QUERY);
  1107. parse_str($persistentRedirectQueryString, $persistentRedirectQueryParams);
  1108. if (isset($persistentRedirectQueryParams['lang']) && !empty($persistentRedirectQueryParams['lang'])) {
  1109. $languageParam = sanitize_text_field($persistentRedirectQueryParams['lang']);
  1110. if (in_array($languageParam, array_keys($WPML_active_languages))) {
  1111. /**
  1112. * The language code that we got from the persistent redirect url is a valid language code for WPML,
  1113. * so we can use this code.
  1114. */
  1115. $languageCode = $languageParam;
  1116. }
  1117. }
  1118. }
  1119. }
  1120. $args = array('loginSocial' => $this->getId());
  1121. $proxyPage = NextendSocialLogin::getProxyPage();
  1122. if ($proxyPage) {
  1123. //OAuth flow handled over OAuth redirect uri proxy page
  1124. $convertedURL = get_permalink(apply_filters('wpml_object_id', $proxyPage, 'page', false, $languageCode));
  1125. if ($convertedURL) {
  1126. $convertedURL = add_query_arg($args, $convertedURL);
  1127. return $convertedURL;
  1128. }
  1129. } else {
  1130. //OAuth flow handled over wp-login.php
  1131. if ($WPML_language_url_format && $WPML_language_url_format == 3 && (!class_exists('\WPML\UrlHandling\WPLoginUrlConverter') || (class_exists('\WPML\UrlHandling\WPLoginUrlConverter') && (!get_option(\WPML\UrlHandling\WPLoginUrlConverter::SETTINGS_KEY, false))))) {
  1132. /**
  1133. * We need to display the original redirect url when the
  1134. * Language URL format is set to "Language name added as a parameter and:
  1135. * -when the WPLoginUrlConverter class doesn't exists, since that case it is an old WPML version that can not translate the /wp-login.php page
  1136. * -if "Login and registration pages - Allow translating the login and registration pages" is disabled
  1137. */
  1138. return $originalLoginUrl;
  1139. } else {
  1140. global $wpml_url_converter;
  1141. /**
  1142. * When the language URL format is set to "Different languages in directories" or "A different domain per language", then the Redirect URI will be different for each languages
  1143. * Also when the language URL format is set to "Language name added as a parameter" and the "Login and registration pages - Allow translating the login and registration pages" setting is enabled, the urls will be different.
  1144. */
  1145. if ($wpml_url_converter && method_exists($wpml_url_converter, 'convert_url')) {
  1146. $convertedURL = $wpml_url_converter->convert_url(site_url('wp-login.php'), $languageCode);
  1147. $convertedURL = add_query_arg($args, $convertedURL);
  1148. return $convertedURL;
  1149. }
  1150. }
  1151. }
  1152. }
  1153. }
  1154. }
  1155. return $originalLoginUrl;
  1156. }
  1157. }