暂无描述

admin.php 45KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  1. <?php
  2. use NSL\Notices;
  3. define('NSL_ADMIN_PATH', __FILE__);
  4. require_once dirname(__FILE__) . '/upgrader.php';
  5. NextendSocialUpgrader::init();
  6. class NextendSocialLoginAdmin {
  7. public static function init() {
  8. add_action('admin_menu', 'NextendSocialLoginAdmin::admin_menu', 1);
  9. add_action('admin_init', 'NextendSocialLoginAdmin::admin_init');
  10. add_filter('plugin_action_links', 'NextendSocialLoginAdmin::plugin_action_links', 10, 2);
  11. add_filter('nsl_update_settings_validate_nextend_social_login', 'NextendSocialLoginAdmin::validateSettings', 10, 2);
  12. add_action('wp_ajax_nsl_save_review_state', 'NextendSocialLoginAdmin::save_review_state');
  13. }
  14. public static function getAdminUrl($view = 'providers') {
  15. return add_query_arg(array(
  16. 'page' => 'nextend-social-login',
  17. 'view' => $view
  18. ), admin_url('options-general.php'));
  19. }
  20. public static function getAdminSettingsUrl($subview = 'general') {
  21. return add_query_arg(array(
  22. 'page' => 'nextend-social-login',
  23. 'view' => 'global-settings',
  24. 'subview' => $subview
  25. ), admin_url('options-general.php'));
  26. }
  27. public static function admin_menu() {
  28. $menu = add_options_page('Nextend Social Login', 'Nextend Social Login', 'manage_options', 'nextend-social-login', array(
  29. 'NextendSocialLoginAdmin',
  30. 'display_admin'
  31. ));
  32. add_action('admin_print_styles-' . $menu, 'NextendSocialLoginAdmin::admin_css');
  33. }
  34. public static function admin_css() {
  35. wp_enqueue_style('nsl-admin-stylesheet', plugins_url('/style.css?nsl-ver=' . urlencode(NextendSocialLogin::$version), NSL_ADMIN_PATH));
  36. }
  37. public static function display_admin() {
  38. $view = !empty($_REQUEST['view']) ? $_REQUEST['view'] : '';
  39. if (substr($view, 0, 9) == 'provider-') {
  40. $providerID = substr($view, 9);
  41. if (isset(NextendSocialLogin::$providers[$providerID])) {
  42. self::display_admin_area('provider', $providerID);
  43. return;
  44. }
  45. }
  46. switch ($view) {
  47. case 'fix-redirect-uri':
  48. self::display_admin_area('fix-redirect-uri');
  49. break;
  50. case 'debug':
  51. self::display_admin_area('debug');
  52. break;
  53. case 'test-connection':
  54. self::display_admin_area('test-connection');
  55. break;
  56. case 'global-settings':
  57. self::display_admin_area('global-settings');
  58. break;
  59. case 'pro-addon':
  60. self::display_admin_area('pro-addon');
  61. break;
  62. case 'install-pro':
  63. if (check_admin_referer('nextend-social-login')) {
  64. self::display_admin_area('install-pro');
  65. } else {
  66. self::display_admin_area('providers');
  67. }
  68. break;
  69. default:
  70. self::display_admin_area('providers');
  71. break;
  72. }
  73. }
  74. /**
  75. * @param string $view
  76. * @param string $currentProvider
  77. */
  78. private static function display_admin_area($view, $currentProvider = '') {
  79. if (empty($currentProvider)) {
  80. include(dirname(__FILE__) . '/templates/header.php');
  81. include(dirname(__FILE__) . '/templates/menu.php');
  82. Notices::displayNotices();
  83. /** @var string $view */
  84. include(dirname(__FILE__) . '/templates/' . $view . '.php');
  85. include(dirname(__FILE__) . '/templates/footer.php');
  86. } else {
  87. include(dirname(__FILE__) . '/templates/' . $view . '.php');
  88. }
  89. }
  90. public static function renderProSettings() {
  91. include(dirname(__FILE__) . '/templates/global-settings-pro.php');
  92. }
  93. public static function admin_init() {
  94. if (current_user_can('manage_options')) {
  95. if (!defined('NSL_PRO_PATH')) {
  96. require_once(dirname(__FILE__) . '/notice.php');
  97. }
  98. if (!isset($_GET['page']) || $_GET['page'] != 'nextend-social-login' || !isset($_GET['view']) || $_GET['view'] != 'fix-redirect-uri') {
  99. add_action('admin_notices', 'NextendSocialLoginAdmin::show_oauth_uri_notice');
  100. }
  101. if (!self::isPro() && NextendSocialLogin::$settings->get('woocommerce_dismissed') == 0 && class_exists('woocommerce', false) && count(NextendSocialLogin::$enabledProviders)) {
  102. add_action('admin_notices', 'NextendSocialLoginAdmin::show_woocommerce_notice');
  103. }
  104. if (defined('THEME_MY_LOGIN_VERSION') && version_compare(THEME_MY_LOGIN_VERSION, '7.0.0', '>=')) {
  105. if (!NextendSocialLogin::getRegisterFlowPage() || !NextendSocialLogin::getProxyPage()) {
  106. add_action('admin_notices', 'NextendSocialLoginAdmin::show_theme_my_login_notice');
  107. }
  108. }
  109. }
  110. if (isset($_GET['page']) && $_GET['page'] == 'nextend-social-login') {
  111. if (!empty($_GET['view'])) {
  112. switch ($_GET['view']) {
  113. case 'enable':
  114. case 'sub-enable':
  115. if (!empty($_GET['provider'])) {
  116. if (check_admin_referer('nextend-social-login_enable_' . $_GET['provider'])) {
  117. NextendSocialLogin::enableProvider($_GET['provider']);
  118. }
  119. if ($_GET['view'] == 'sub-enable') {
  120. wp_redirect(NextendSocialLogin::$providers[$_GET['provider']]->getAdmin()
  121. ->getUrl('settings'));
  122. exit;
  123. }
  124. wp_redirect(self::getAdminUrl());
  125. exit;
  126. }
  127. break;
  128. case 'disable':
  129. case 'sub-disable':
  130. if (!empty($_GET['provider'])) {
  131. if (check_admin_referer('nextend-social-login_disable_' . $_GET['provider'])) {
  132. NextendSocialLogin::disableProvider($_GET['provider']);
  133. }
  134. if ($_GET['view'] == 'sub-disable') {
  135. wp_redirect(NextendSocialLogin::$providers[$_GET['provider']]->getAdmin()
  136. ->getUrl('settings'));
  137. exit;
  138. }
  139. wp_redirect(self::getAdminUrl());
  140. exit;
  141. }
  142. break;
  143. case 'update_oauth_redirect_url':
  144. if (check_admin_referer('nextend-social-login_update_oauth_redirect_url')) {
  145. foreach (NextendSocialLogin::$enabledProviders as $provider) {
  146. $provider->updateOauthRedirectUrl();
  147. }
  148. }
  149. wp_redirect(self::getAdminUrl());
  150. exit;
  151. case 'dismiss_woocommerce':
  152. if (check_admin_referer('nsl_dismiss_woocommerce')) {
  153. NextendSocialLogin::$settings->update(array(
  154. 'woocommerce_dismissed' => 1
  155. ));
  156. if (!empty($_REQUEST['redirect_to'])) {
  157. wp_safe_redirect($_REQUEST['redirect_to']);
  158. exit;
  159. }
  160. }
  161. wp_redirect(self::getAdminUrl());
  162. break;
  163. }
  164. }
  165. }
  166. add_action('admin_post_nextend-social-login', 'NextendSocialLoginAdmin::save_form_data');
  167. add_action('wp_ajax_nextend-social-login', 'NextendSocialLoginAdmin::ajax_save_form_data');
  168. add_action('admin_enqueue_scripts', 'NextendSocialLoginAdmin::admin_enqueue_scripts');
  169. if (!function_exists('json_decode')) {
  170. add_settings_error('nextend-social', 'settings_updated', printf(__('%s needs json_decode function.', 'nextend-facebook-connect'), 'Nextend Social Login') . ' ' . __('Please contact your server administrator and ask for solution!', 'nextend-facebook-connect'), 'error');
  171. }
  172. add_action('show_user_profile', array(
  173. 'NextendSocialLoginAdmin',
  174. 'showUserFields'
  175. ));
  176. add_action('edit_user_profile', array(
  177. 'NextendSocialLoginAdmin',
  178. 'showUserFields'
  179. ));
  180. add_filter('display_post_states', array(
  181. 'NextendSocialLoginAdmin',
  182. 'display_post_states'
  183. ), 10, 2);
  184. if (defined('WPML_PLUGIN_BASENAME')) {
  185. add_action('nsl_getting_started_warnings', array(
  186. 'NextendSocialLoginAdmin',
  187. 'show_WPML_warning'
  188. ));
  189. add_filter('nsl_redirect_uri_override', array(
  190. 'NextendSocialLoginAdmin',
  191. 'WPML_override_provider_redirect_uris'
  192. ), 10, 2);
  193. };
  194. add_action('nsl_getting_started_warnings', array(
  195. 'NextendSocialLoginAdmin',
  196. 'show_getting_started_warning'
  197. ), 100, 2);
  198. }
  199. public static function save_form_data() {
  200. if (current_user_can('manage_options') && check_admin_referer('nextend-social-login')) {
  201. foreach ($_POST as $k => $v) {
  202. if (is_string($v)) {
  203. $_POST[$k] = stripslashes($v);
  204. }
  205. }
  206. $view = !empty($_REQUEST['view']) ? $_REQUEST['view'] : '';
  207. if ($view == 'global-settings') {
  208. NextendSocialLogin::$settings->update($_POST);
  209. Notices::addSuccess(__('Settings saved.'));
  210. wp_redirect(self::getAdminSettingsUrl(!empty($_REQUEST['subview']) ? $_REQUEST['subview'] : ''));
  211. exit;
  212. } else if ($view == 'pro-addon') {
  213. NextendSocialLogin::$settings->update($_POST);
  214. if (NextendSocialLogin::hasLicense()) {
  215. Notices::addSuccess(__('The activation was successful', 'nextend-facebook-connect'));
  216. }
  217. wp_redirect(self::getAdminUrl($view));
  218. exit;
  219. } else if ($view == 'pro-addon-deauthorize') {
  220. NextendSocialLogin::$settings->update(array(
  221. 'license_key' => ''
  222. ));
  223. Notices::addSuccess(__('Deactivate completed.', 'nextend-facebook-connect'));
  224. wp_redirect(self::getAdminUrl('pro-addon'));
  225. exit;
  226. } else if (substr($view, 0, 9) == 'provider-') {
  227. $providerID = substr($view, 9);
  228. if (isset(NextendSocialLogin::$providers[$providerID])) {
  229. if (NextendSocialLogin::$providers[$providerID]->settings->update($_POST)) {
  230. Notices::addSuccess(__('Settings saved.'));
  231. }
  232. wp_redirect(NextendSocialLogin::$providers[$providerID]->getAdmin()
  233. ->getUrl(isset($_POST['subview']) ? $_POST['subview'] : ''));
  234. exit;
  235. }
  236. }
  237. }
  238. wp_redirect(self::getAdminUrl());
  239. exit;
  240. }
  241. public static function ajax_save_form_data() {
  242. check_ajax_referer('nextend-social-login');
  243. if (current_user_can('manage_options')) {
  244. $view = !empty($_POST['view']) ? $_POST['view'] : '';
  245. switch ($view) {
  246. case 'orderProviders':
  247. if (!empty($_POST['ordering'])) {
  248. NextendSocialLogin::$settings->update(array(
  249. 'ordering' => $_POST['ordering']
  250. ));
  251. }
  252. break;
  253. case 'newsletterSubscribe':
  254. $user_info = wp_get_current_user();
  255. update_user_meta($user_info->ID, 'nsl_newsletter_subscription', 1);
  256. break;
  257. }
  258. }
  259. }
  260. public static function validateSettings($newData, $postedData) {
  261. if (isset($postedData['redirect'])) {
  262. if (isset($postedData['custom_redirect_enabled']) && $postedData['custom_redirect_enabled'] == '1') {
  263. $newData['redirect'] = trim(sanitize_text_field($postedData['redirect']));
  264. } else {
  265. $newData['redirect'] = '';
  266. }
  267. }
  268. if (isset($postedData['redirect_reg'])) {
  269. if (isset($postedData['custom_redirect_reg_enabled']) && $postedData['custom_redirect_reg_enabled'] == '1') {
  270. $newData['redirect_reg'] = trim(sanitize_text_field($postedData['redirect_reg']));
  271. } else {
  272. $newData['redirect_reg'] = '';
  273. }
  274. }
  275. if (isset($postedData['default_redirect'])) {
  276. if (isset($postedData['default_redirect_enabled']) && $postedData['default_redirect_enabled'] == '1') {
  277. $newData['default_redirect'] = trim(sanitize_text_field($postedData['default_redirect']));
  278. } else {
  279. $newData['default_redirect'] = '';
  280. }
  281. }
  282. if (isset($postedData['default_redirect_reg'])) {
  283. if (isset($postedData['default_redirect_reg_enabled']) && $postedData['default_redirect_reg_enabled'] == '1') {
  284. $newData['default_redirect_reg'] = trim(sanitize_text_field($postedData['default_redirect_reg']));
  285. } else {
  286. $newData['default_redirect_reg'] = '';
  287. }
  288. }
  289. foreach ($postedData as $key => $value) {
  290. switch ($key) {
  291. case 'debug':
  292. case 'login_restriction':
  293. case 'avatars_in_all_media':
  294. case 'custom_register_label':
  295. case 'terms_show':
  296. case 'store_name':
  297. case 'store_email':
  298. case 'avatar_store':
  299. case 'store_access_token':
  300. case 'redirect_prevent_external':
  301. if ($value == 1) {
  302. $newData[$key] = 1;
  303. } else {
  304. $newData[$key] = 0;
  305. }
  306. break;
  307. case 'terms':
  308. $newData[$key] = wp_kses_post($value);
  309. break;
  310. case 'blacklisted_urls':
  311. $newData[$key] = sanitize_textarea_field($postedData[$key]);
  312. break;
  313. case 'show_login_form':
  314. case 'login_form_button_align':
  315. case 'show_registration_form':
  316. case 'show_embedded_login_form':
  317. case 'embedded_login_form_button_align':
  318. case 'redirect_overlay':
  319. $newData[$key] = sanitize_text_field($value);
  320. break;
  321. case 'enabled':
  322. if (is_array($value)) {
  323. $newData[$key] = $value;
  324. }
  325. break;
  326. case 'ordering':
  327. if (is_array($value)) {
  328. $newData[$key] = $value;
  329. }
  330. break;
  331. case 'license_key':
  332. Notices::clear();
  333. $value = trim(sanitize_text_field($value));
  334. if (!empty($value)) {
  335. try {
  336. $response = self::apiCall('test-license', array('license_key' => $value));
  337. if ($response === 'OK') {
  338. $newData['licenses'] = array(
  339. array(
  340. 'license_key' => $value,
  341. 'domain' => NextendSocialLogin::getDomain()
  342. )
  343. );
  344. wp_clean_plugins_cache();
  345. }
  346. } catch (Exception $e) {
  347. Notices::addError($e->getMessage());
  348. }
  349. } else {
  350. wp_clean_plugins_cache();
  351. $newData['licenses'] = array();
  352. }
  353. break;
  354. case 'review_state':
  355. case 'woocommerce_dismissed':
  356. $newData[$key] = intval($value);
  357. break;
  358. case 'register-flow-page':
  359. case 'proxy-page':
  360. if (get_post($value) !== null) {
  361. $newData[$key] = $value;
  362. } else {
  363. $newData[$key] = '';
  364. }
  365. break;
  366. case 'allow_register':
  367. if ($value == '0') {
  368. $newData[$key] = 0;
  369. } else if ($value == '1') {
  370. $newData[$key] = 1;
  371. } else {
  372. $newData[$key] = -1;
  373. }
  374. break;
  375. }
  376. }
  377. return $newData;
  378. }
  379. public static function plugin_action_links($links, $file) {
  380. if ($file != NSL_PLUGIN_BASENAME) {
  381. return $links;
  382. }
  383. $settings_link = '<a href="' . esc_url(menu_page_url('nextend-social-login', false)) . '">' . __('Settings') . '</a>';
  384. $reactivate_link = sprintf('<a href="%s">%s</a>', wp_nonce_url(admin_url('admin.php?page=nextend-social-login&repairnsl=1'), 'repairnsl'), 'Analyze & Repair');
  385. array_unshift($links, $settings_link, $reactivate_link);
  386. return $links;
  387. }
  388. public static function admin_enqueue_scripts() {
  389. if ('settings_page_nextend-social-login' === get_current_screen()->id) {
  390. // Since WordPress 4.9
  391. if (function_exists('wp_enqueue_code_editor')) {
  392. // Enqueue code editor and settings for manipulating HTML.
  393. $settings = wp_enqueue_code_editor(array('type' => 'text/html'));
  394. // Bail if user disabled CodeMirror.
  395. if (false === $settings) {
  396. return;
  397. }
  398. wp_add_inline_script('code-editor', sprintf('jQuery( function() { var settings = %s; jQuery(".nextend-html-editor").each(function(i, el){wp.codeEditor.initialize( el, settings);}); } );', wp_json_encode($settings)));
  399. $settings['codemirror']['readOnly'] = 'nocursor';
  400. wp_add_inline_script('code-editor', sprintf('jQuery( function() { var settings = %s; jQuery(".nextend-html-editor-readonly").each(function(i, el){wp.codeEditor.initialize( el, settings);}); } );', wp_json_encode($settings)));
  401. }
  402. if (isset($_GET['view']) && $_GET['view'] == 'pro-addon') {
  403. wp_enqueue_script('plugin-install');
  404. wp_enqueue_script('updates');
  405. }
  406. }
  407. }
  408. private static $endpoint = 'https://api.nextendweb.com/v2/nextend-api/v2/';
  409. public static function getEndpoint($action = '') {
  410. return self::$endpoint . 'product/nsl/' . urlencode($action);
  411. }
  412. /**
  413. * @param $action
  414. * @param array $args
  415. *
  416. * @return bool|mixed
  417. * @throws Exception
  418. */
  419. public static function apiCall($action, $args = array()) {
  420. $body = array(
  421. 'platform' => 'wordpress',
  422. 'domain' => NextendSocialLogin::getDomain()
  423. );
  424. $activation_data = NextendSocialLogin::getLicense();
  425. if ($activation_data !== false) {
  426. $body['license_key'] = $activation_data['license_key'];
  427. } else {
  428. $body['license_key'] = '';
  429. }
  430. $http_args = array(
  431. 'timeout' => 15,
  432. 'user-agent' => 'WordPress',
  433. 'body' => array_merge($body, $args)
  434. );
  435. $request = wp_remote_get(self::getEndpoint($action), $http_args);
  436. if (is_wp_error($request)) {
  437. throw new Exception($request->get_error_message());
  438. } else if (wp_remote_retrieve_response_code($request) !== 200) {
  439. $response = json_decode(wp_remote_retrieve_body($request), true);
  440. if (isset($response['message'])) {
  441. $message = 'Nextend Social Login Pro Addon: ' . $response['message'];
  442. Notices::addError($message);
  443. return new WP_Error('error', $message);
  444. }
  445. throw new Exception(sprintf(__('Unexpected response: %s', 'nextend-facebook-connect'), wp_remote_retrieve_body($request)));
  446. }
  447. $response = json_decode(wp_remote_retrieve_body($request), true);
  448. return $response;
  449. }
  450. public static function showProBox() {
  451. if (!self::isPro()) {
  452. include(dirname(__FILE__) . '/templates/pro.php');
  453. }
  454. }
  455. public static function getProState() {
  456. if (NextendSocialLogin::hasLicense()) {
  457. if (self::isPro()) {
  458. return 'activated';
  459. } else if (!current_user_can('install_plugins')) {
  460. return 'no-capability';
  461. } else if (class_exists('NextendSocialLoginPRO', false) && version_compare(NextendSocialLoginPRO::$version, NextendSocialLogin::$nslPROMinVersion, '<')) {
  462. return 'not-compatible';
  463. } else {
  464. if (file_exists(WP_PLUGIN_DIR . '/nextend-social-login-pro/nextend-social-login-pro.php')) {
  465. return 'installed';
  466. } else {
  467. return 'not-installed';
  468. }
  469. }
  470. }
  471. return 'no-license';
  472. }
  473. public static function trackUrl($url, $source) {
  474. return add_query_arg(array(
  475. 'utm_campaign' => 'nsl',
  476. 'utm_source' => urlencode($source),
  477. 'utm_medium' => 'nsl-wordpress-' . (apply_filters('nsl-pro', false) ? 'pro' : 'free')
  478. ), $url);
  479. }
  480. public static function save_review_state() {
  481. check_ajax_referer('nsl_save_review_state');
  482. if (isset($_POST['review_state'])) {
  483. $review_state = intval($_POST['review_state']);
  484. if ($review_state > 0) {
  485. NextendSocialLogin::$settings->update(array(
  486. 'review_state' => $review_state
  487. ));
  488. }
  489. }
  490. wp_die();
  491. }
  492. public static function show_oauth_uri_notice() {
  493. foreach (NextendSocialLogin::$enabledProviders as $provider) {
  494. if (!$provider->checkOauthRedirectUrl()) {
  495. echo '<div class="error">
  496. <p>' . sprintf(__('%s detected that your login url changed. You must update the Oauth redirect URIs in the related social applications.', 'nextend-facebook-connect'), '<b>Nextend Social Login</b>') . '</p>
  497. <p class="submit"><a href="' . NextendSocialLoginAdmin::getAdminUrl('fix-redirect-uri') . '" class="button button-primary">' . __('Fix Error', 'nextend-facebook-connect') . ' - ' . __('Oauth Redirect URI', 'nextend-facebook-connect') . '</a></p>
  498. </div>';
  499. break;
  500. }
  501. }
  502. }
  503. public static function show_woocommerce_notice() {
  504. $redirectTo = array();
  505. $currentPageUrl = NextendSocialLogin::getCurrentPageURL();
  506. if ($currentPageUrl !== false) {
  507. $redirectTo['redirect_to'] = urlencode($currentPageUrl);
  508. }
  509. $dismissUrl = wp_nonce_url(add_query_arg($redirectTo, NextendSocialLoginAdmin::getAdminUrl('dismiss_woocommerce')), 'nsl_dismiss_woocommerce');
  510. echo '<div class="notice notice-info">
  511. <p>' . sprintf(__('%1$s detected that %2$s installed on your site. You need the Pro Addon to display Social Login buttons in %2$s login form!', 'nextend-facebook-connect'), '<b>Nextend Social Login</b>', '<b>WooCommerce</b>') . '</p>
  512. <p><a href="' . NextendSocialLoginAdmin::trackUrl('https://nextendweb.com/social-login/', 'woocommerce-notice') . '" target="_blank" onclick="window.location.href=\'' . esc_url($dismissUrl) . '\';" class="button button-primary">' . __('Dismiss and check Pro Addon', 'nextend-facebook-connect') . '</a> <a href="' . esc_url($dismissUrl) . '" class="button button-secondary">' . __('Dismiss', 'nextend-facebook-connect') . '</a></p>
  513. </div>';
  514. }
  515. public static function show_theme_my_login_notice() {
  516. echo '<div class="notice notice-info">
  517. <p>' . sprintf(__('%1$s detected that %2$s installed on your site. You must set "<b>Page for register flow</b>" and "<b>OAuth redirect uri proxy page</b>" in %1$s to work properly.', 'nextend-facebook-connect'), '<b>Nextend Social Login</b>', '<b>Theme My Login</b>') . '</p>
  518. <p><a href="' . NextendSocialLoginAdmin::getAdminSettingsUrl('general') . '" class="button button-primary">' . __('Fix now', 'nextend-facebook-connect') . '</a></p>
  519. </div>';
  520. }
  521. public static function isPro() {
  522. return apply_filters('nsl-pro', false);
  523. }
  524. public static function showUserFields($user) {
  525. include(dirname(__FILE__) . '/EditUser.php');
  526. }
  527. public static function authorizeBox($view = 'pro-addon') {
  528. $args = array(
  529. 'product' => 'nsl',
  530. 'domain' => NextendSocialLogin::getDomain(),
  531. 'platform' => 'wordpress'
  532. );
  533. $authorizeUrl = NextendSocialLoginAdmin::trackUrl('https://secure.nextendweb.com/authorize/', 'authorize');
  534. ?>
  535. <div class="nsl-box nsl-box-yellow nsl-box-padlock">
  536. <h2 class="title"><?php _e('Activate your Pro Addon', 'nextend-facebook-connect'); ?></h2>
  537. <p><?php _e('To be able to use the Pro features, you need to activate Nextend Social Login Pro Addon. You can do this by clicking on the Activate button below then select the related purchase.', 'nextend-facebook-connect'); ?></p>
  538. <p>
  539. <a href="#"
  540. onclick="NSLActivate()"
  541. class="button button-primary"><?php _e('Activate', 'nextend-facebook-connect'); ?></a>
  542. </p>
  543. </div>
  544. <script type="text/javascript">
  545. (function ($) {
  546. var args = <?php echo wp_json_encode($args); ?>;
  547. window.addEventListener('message', function (e) {
  548. if (e.origin === 'https://secure.nextendweb.com') {
  549. if (typeof window.authorizeWindow === 'undefined') {
  550. if (typeof e.source !== 'undefined') {
  551. window.authorizeWindow = e.source;
  552. } else {
  553. return false;
  554. }
  555. }
  556. try {
  557. var envelope = JSON.parse(e.data);
  558. if (envelope.action) {
  559. switch (envelope.action) {
  560. case 'ready':
  561. window.authorizeWindow.postMessage(JSON.stringify({
  562. 'action': 'authorize',
  563. 'data': args
  564. }), 'https://secure.nextendweb.com');
  565. break;
  566. case 'license':
  567. $('#nsl_license_key').val(envelope.license_key);
  568. $('#nsl_license_form').submit();
  569. break;
  570. }
  571. }
  572. } catch (ex) {
  573. console.error(ex);
  574. console.log(e);
  575. }
  576. }
  577. });
  578. })(jQuery);
  579. function NSLActivate() {
  580. var isIE = (function detectIE() {
  581. var ua = window.navigator.userAgent;
  582. var msie = ua.indexOf('MSIE ');
  583. if (msie > 0) {
  584. // IE 10 or older => return version number
  585. return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
  586. }
  587. var trident = ua.indexOf('Trident/');
  588. if (trident > 0) {
  589. // IE 11 => return version number
  590. var rv = ua.indexOf('rv:');
  591. return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
  592. }
  593. var edge = ua.indexOf('Edge/');
  594. if (edge > 0) {
  595. // Edge (IE 12+) => return version number
  596. return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
  597. }
  598. // other browser
  599. return false;
  600. })();
  601. if (isIE <= 11) {
  602. /**
  603. * Trick for cross origin popup postMessage in IE 11
  604. * @see <https://stackoverflow.com/a/36630058/305604>
  605. */
  606. window.authorizeWindow = NSLPopup('/', 'authorize-window', 800, 800);
  607. window.authorizeWindow.location.href = 'about:blank';
  608. window.authorizeWindow.location.href = '<?php echo $authorizeUrl; ?>';
  609. } else {
  610. window.authorizeWindow = NSLPopup('<?php echo $authorizeUrl; ?>', 'authorize-window', 800, 800);
  611. }
  612. return false;
  613. }
  614. </script>
  615. <form id="nsl_license_form" method="post" action="<?php echo admin_url('admin-post.php'); ?>"
  616. novalidate="novalidate" style="display:none;">
  617. <?php wp_nonce_field('nextend-social-login'); ?>
  618. <input type="hidden" name="action" value="nextend-social-login"/>
  619. <input type="hidden" name="view" value="<?php echo $view; ?>"/>
  620. <table class="form-table">
  621. <tbody>
  622. <tr>
  623. <th scope="row"><label
  624. for="nsl_license_key"><?php _e('License key', 'nextend-facebook-connect'); ?></label>
  625. </th>
  626. <?php
  627. $license_key = '';
  628. $authorizedData = NextendSocialLogin::getLicense();
  629. if ($authorizedData !== false) {
  630. $license_key = $authorizedData['license_key'];
  631. }
  632. ?>
  633. <td><input name="license_key" type="text" id="nsl_license_key"
  634. value="<?php echo esc_attr($license_key); ?>"
  635. class="regular-text">
  636. </td>
  637. </tr>
  638. </tbody>
  639. </table>
  640. </form>
  641. <?php
  642. }
  643. public static function display_post_states($post_states, $post) {
  644. if (NextendSocialLogin::getProxyPage() === $post->ID) {
  645. $post_states['nsl_proxy_page'] = __('OAuth proxy page') . ' — NSL';
  646. }
  647. if (NextendSocialLogin::getRegisterFlowPage() === $post->ID) {
  648. $post_states['nsl_proxy_page'] = __('Register flow page') . ' — NSL';
  649. }
  650. return $post_states;
  651. }
  652. public static function show_WPML_warning() {
  653. printf(__('<strong><u>Warning</u></strong>: You are using <b>%1$s</b>! Depending on your %1$s configuration the Redirect URI can be different. For more information please check our %2$s %1$s compatibility tutorial%3$s!', 'nextend-facebook-connect'), 'WPML', '<a href="https://nextendweb.com/nextend-social-login-docs/how-to-make-nextend-social-login-compatible-with-wpml/" target="_blank">', '</a>');
  654. }
  655. /**
  656. * @param array $redirectUrls
  657. * @param NextendSocialProvider $provider
  658. *
  659. * Used for:
  660. * -overriding the redirect url with the language specific redirect URLs in provider Getting Started
  661. * sections.
  662. * -generating language specific redirect urls for the OAuth check warning.
  663. *
  664. * @return array
  665. */
  666. public static function WPML_override_provider_redirect_uris($redirectUrls, $provider) {
  667. $addArg = true;
  668. if ($provider->oauthRedirectBehavior !== 'default') {
  669. /**
  670. * We shouldn't add any query parameters into the redirect url if:
  671. * -query parameters are not supported in the redirect uri
  672. * -or the redirect is handled over the REST /redirect_uri endpoint of the provider.
  673. */
  674. $addArg = false;
  675. }
  676. global $sitepress;
  677. if ($sitepress && method_exists($sitepress, 'get_active_languages')) {
  678. $WPML_active_languages = $sitepress->get_active_languages();
  679. if (count($WPML_active_languages) > 1 && defined('ICL_LANGUAGE_CODE')) {
  680. $originalLanguageCode = ICL_LANGUAGE_CODE;
  681. $defaultLanguageCode = self::get_default_WPML_language_code();
  682. $languageCodeWasOverridden = false;
  683. $converted_URLs = array();
  684. $args = array('loginSocial' => $provider->getId());
  685. if ($provider->oauthRedirectBehavior !== 'rest_redirect') {
  686. $proxyPage = NextendSocialLogin::getProxyPage();
  687. if ($proxyPage) {
  688. /**
  689. * OAuth flow handled over OAuth redirect uri proxy page
  690. * This needs to be handled differently than /wp-login.php URLs, because in these cases
  691. * the slug of the translated OAuth redirect uri proxy page can be different as well!
  692. */
  693. foreach ($WPML_active_languages as $lang) {
  694. $convertedURL = get_permalink(apply_filters('wpml_object_id', $proxyPage, 'page', false, $lang['code']));
  695. if ($convertedURL) {
  696. if ($addArg) {
  697. $convertedURL = add_query_arg($args, $convertedURL);
  698. } else {
  699. /**
  700. * Converted URLs may contain GET parameters, so we need to remove them for the providers that don't support GET parameters in the redirect urls.
  701. */
  702. $convertedURLPieces = explode('?', $convertedURL);
  703. $convertedURL = $convertedURLPieces[0];
  704. }
  705. $converted_URLs[] = $convertedURL;
  706. }
  707. }
  708. } else {
  709. //OAuth flow handled over wp-login.php
  710. $WPML_language_url_format = false;
  711. if (method_exists($sitepress, 'get_setting')) {
  712. $WPML_language_url_format = $sitepress->get_setting('language_negotiation_type');
  713. }
  714. 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) || (get_option(\WPML\UrlHandling\WPLoginUrlConverter::SETTINGS_KEY, false) && !$addArg))))) {
  715. /**
  716. * We need to display the original redirect url when the
  717. * Language URL format is set to "Language name added as a parameter and:
  718. * -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
  719. * -if "Login and registration pages - Allow translating the login and registration pages" is disabled
  720. * -if "Login and registration pages - Allow translating the login and registration pages" is enabled, but the provider doesn't support GET parameters in the redirect URL
  721. */
  722. return $redirectUrls;
  723. } else {
  724. global $wpml_url_converter;
  725. /**
  726. * 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
  727. * 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.
  728. */
  729. if ($wpml_url_converter && method_exists($wpml_url_converter, 'convert_url')) {
  730. /**
  731. * When WPML is set to a non-default language in the backend, then the $wpml_url_converter->convert_url() function won't generate language specific URL
  732. * if the provided language code is the same the the language code that the backend currently uses.
  733. */
  734. if ($originalLanguageCode && $defaultLanguageCode && $originalLanguageCode !== $defaultLanguageCode) {
  735. self::change_WPML_language_code($defaultLanguageCode, false);
  736. $languageCodeWasOverridden = true;
  737. }
  738. foreach ($WPML_active_languages as $lang) {
  739. $convertedURL = $wpml_url_converter->convert_url(site_url('wp-login.php'), $lang['code']);
  740. if ($addArg) {
  741. $convertedURL = add_query_arg($args, $convertedURL);
  742. }
  743. $converted_URLs[] = $convertedURL;
  744. }
  745. if ($languageCodeWasOverridden) {
  746. /**
  747. * we need to switch back to the original language if we had to switch earlier
  748. */
  749. self::change_WPML_language_code($originalLanguageCode, true);
  750. $languageCodeWasOverridden = false;
  751. }
  752. }
  753. }
  754. }
  755. } else {
  756. /**
  757. * For providers with REST API redirect url, we should generate language specific versions from the rest route.
  758. * These urls should never contain the ?loginSocial={{providerID}} parameter. Since that is the main reason of the provider prefers uses the REST API endpoint.
  759. * The redirect url is not affected by the "/wp-login.php" or "OAuth redirect uri proxy page" changes in this case.
  760. */
  761. $WPML_language_url_format = false;
  762. if (method_exists($sitepress, 'get_setting')) {
  763. $WPML_language_url_format = $sitepress->get_setting('language_negotiation_type');
  764. }
  765. if (!$WPML_language_url_format || ($WPML_language_url_format && $WPML_language_url_format == 3)) {
  766. /**
  767. * We need to return the original provider REST API url when:
  768. * -the Language URL format is set to "Language name added as a parameter
  769. * -or if there is no Language URL format set
  770. */
  771. $converted_URLs = $redirectUrls;
  772. } else {
  773. global $wpml_url_converter;
  774. if ($wpml_url_converter && method_exists($wpml_url_converter, 'convert_url')) {
  775. /**
  776. * When the WPML language in the backend is set to "All", then WPML will generate an invalid REST API url with this "all" string appearing in it,
  777. * so we would generate wrong redirect urls.
  778. * For this reason it is better if we always use the default language for the URL generation here, too.
  779. */
  780. if ($originalLanguageCode && $defaultLanguageCode && $originalLanguageCode !== $defaultLanguageCode) {
  781. self::change_WPML_language_code($defaultLanguageCode, false);
  782. $languageCodeWasOverridden = true;
  783. }
  784. $redirectUrl = $provider->getBaseRedirectUriForAppCreation();
  785. foreach ($WPML_active_languages as $lang) {
  786. $convertedURL = $wpml_url_converter->convert_url($redirectUrl, $lang['code']);
  787. /**
  788. * WPML might generate URLs with trailing slash, however we use the REST redirect URL without trailing slash.
  789. */
  790. $convertedURL = rtrim($convertedURL, '/');
  791. if ($addArg) {
  792. $convertedURL = add_query_arg($args, $convertedURL);
  793. }
  794. $converted_URLs[] = $convertedURL;
  795. }
  796. if ($languageCodeWasOverridden) {
  797. /**
  798. * we need to switch back to the original language if we had to switch earlier
  799. */
  800. self::change_WPML_language_code($originalLanguageCode, true);
  801. $languageCodeWasOverridden = false;
  802. }
  803. }
  804. }
  805. }
  806. if (!empty($converted_URLs)) {
  807. return $converted_URLs;
  808. }
  809. }
  810. }
  811. return $redirectUrls;
  812. }
  813. /**
  814. * Returns the default language code used by WPML.
  815. *
  816. * @return bool|string
  817. */
  818. public static function get_default_WPML_language_code() {
  819. global $sitepress;
  820. if ($sitepress) {
  821. return $sitepress->get_default_language();
  822. }
  823. return false;
  824. }
  825. /**
  826. * Thins function can be used for changing the language code that WPML use during URL conversion.
  827. *
  828. * @param string $languageCode - the language code that WPML will switch to
  829. * @param bool $restore - if true, that means we shouldn't override the language for the
  830. * get_language_from_url() function of WPML.
  831. */
  832. public static function change_WPML_language_code($languageCode, $restore) {
  833. global $sitepress;
  834. if ($sitepress) {
  835. $sitepress->switch_lang($languageCode, true);
  836. if ($restore) {
  837. remove_filter('wpml_get_language_from_url', 'NextendSocialLoginAdmin::get_default_WPML_language_code', 1000000000);
  838. } else {
  839. add_filter('wpml_get_language_from_url', 'NextendSocialLoginAdmin::get_default_WPML_language_code', 1000000000);
  840. }
  841. }
  842. }
  843. public static function show_getting_started_warning($provider, $lastUpdated) {
  844. if ($provider && $lastUpdated) {
  845. $lastUpdatedDate = date_format(date_create_from_format('Y-m-d', $lastUpdated), get_option('date_format'));
  846. $supportURL = 'https://nextendweb.com/contact-us/nextend-social-login-support/';
  847. $version = defined('NSL_PRO_PATH') ? 'Pro-Addon' : 'Free';
  848. $args = array(
  849. 'topic' => 'Wrong-Steps',
  850. 'provider' => $provider->getLabel(),
  851. 'version' => $version
  852. );
  853. $supportUrlWithArgs = add_query_arg($args, $supportURL);
  854. printf(__('<p><strong><u>Warning</u></strong>: Providers change the App setup process quite often, which means some steps below might not be accurate. If you see significant difference in the written instructions and what you see at the provider, feel free to %1$sreport it%2$s, so we can check and update the instructions.<br><strong>Last updated:</strong> %3$s.</p>', 'nextend-facebook-connect'), '<a href="' . $supportUrlWithArgs . '" target="_blank">', '</a>', $lastUpdatedDate);
  855. }
  856. }
  857. }