Bez popisu

nextend-social-login.php 53KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  1. <?php
  2. use NSL\Notices;
  3. use NSL\Persistent\Persistent;
  4. require_once(NSL_PATH . '/includes/exceptions.php');
  5. require_once dirname(__FILE__) . '/NSL/Persistent/Persistent.php';
  6. require_once dirname(__FILE__) . '/NSL/Notices.php';
  7. require_once dirname(__FILE__) . '/NSL/REST.php';
  8. require_once dirname(__FILE__) . '/NSL/GDPR.php';
  9. require_once(NSL_PATH . '/class-settings.php');
  10. require_once(NSL_PATH . '/includes/provider.php');
  11. require_once(NSL_PATH . '/admin/admin.php');
  12. require_once(NSL_PATH . '/compat.php');
  13. class NextendSocialLogin {
  14. public static $version = '3.1.3';
  15. public static $nslPROMinVersion = '3.1.3';
  16. public static $proxyPage = false;
  17. public static function checkVersion() {
  18. if (version_compare(self::$version, NextendSocialLoginPRO::$nslMinVersion, '<')) {
  19. if (did_action('init')) {
  20. NextendSocialLogin::noticeUpdateFree();
  21. } else {
  22. add_action('init', 'NextendSocialLogin::noticeUpdateFree');
  23. }
  24. return false;
  25. }
  26. if (version_compare(NextendSocialLoginPRO::$version, self::$nslPROMinVersion, '<')) {
  27. if (did_action('init')) {
  28. NextendSocialLogin::noticeUpdatePro();
  29. } else {
  30. add_action('init', 'NextendSocialLogin::noticeUpdatePro');
  31. }
  32. return false;
  33. }
  34. return true;
  35. }
  36. public static function noticeUpdateFree() {
  37. if (is_admin() && current_user_can('manage_options')) {
  38. $file = 'nextend-facebook-connect/nextend-facebook-connect.php';
  39. Notices::addError(sprintf(__('Please update %1$s to version %2$s or newer.', 'nextend-facebook-connect'), "Nextend Social Login", NextendSocialLoginPRO::$nslMinVersion) . ' <a href="' . esc_url(wp_nonce_url(admin_url('update.php?action=upgrade-plugin&plugin=') . $file, 'upgrade-plugin_' . $file)) . '">' . __('Update now!', 'nextend-facebook-connect') . '</a>');
  40. }
  41. }
  42. public static function noticeUpdatePro() {
  43. if (is_admin() && current_user_can('manage_options')) {
  44. $file = 'nextend-social-login-pro/nextend-social-login-pro.php';
  45. Notices::addError(sprintf(__('Please update %1$s to version %2$s or newer.', 'nextend-facebook-connect'), "Nextend Social Login Pro Addon", self::$nslPROMinVersion) . ' <a href="' . esc_url(wp_nonce_url(admin_url('update.php?action=upgrade-plugin&plugin=') . $file, 'upgrade-plugin_' . $file)) . '">' . __('Update now!', 'nextend-facebook-connect') . '</a>');
  46. }
  47. }
  48. /** @var NextendSocialLoginSettings */
  49. public static $settings;
  50. private static $styles = array(
  51. 'fullwidth' => array(
  52. 'container' => 'nsl-container-block-fullwidth',
  53. 'align' => array()
  54. ),
  55. 'default' => array(
  56. 'container' => 'nsl-container-block',
  57. 'align' => array(
  58. 'left',
  59. 'right',
  60. 'center',
  61. )
  62. ),
  63. 'icon' => array(
  64. 'container' => 'nsl-container-inline',
  65. 'align' => array(
  66. 'left',
  67. 'right',
  68. 'center',
  69. )
  70. ),
  71. 'grid' => array(
  72. 'container' => 'nsl-container-grid',
  73. 'align' => array(
  74. 'left',
  75. 'right',
  76. 'center',
  77. 'space-around',
  78. 'space-between',
  79. )
  80. )
  81. );
  82. public static $providersPath;
  83. /**
  84. * @var NextendSocialProviderDummy[]
  85. */
  86. public static $providers = array();
  87. /**
  88. * @var NextendSocialProvider[]
  89. */
  90. public static $allowedProviders = array();
  91. /**
  92. * @var NextendSocialProvider[]
  93. */
  94. public static $enabledProviders = array();
  95. private static $ordering = array();
  96. private static $loginHeadAdded = false;
  97. private static $loginMainButtonsAdded = false;
  98. public static $counter = 1;
  99. public static $WPLoginCurrentView = '';
  100. public static $WPLoginCurrentFlow = 'login';
  101. private static $allowedPostStates = array(
  102. 'classic-editor-plugin',
  103. 'elementor'
  104. );
  105. public static function init() {
  106. add_action('plugins_loaded', 'NextendSocialLogin::plugins_loaded');
  107. register_activation_hook(NSL_PATH_FILE, 'NextendSocialLogin::install');
  108. add_action('delete_user', 'NextendSocialLogin::delete_user');
  109. self::$settings = new NextendSocialLoginSettings('nextend_social_login', array(
  110. 'enabled' => array(),
  111. 'register-flow-page' => '',
  112. 'proxy-page' => '',
  113. 'ordering' => array(
  114. 'facebook',
  115. 'google',
  116. 'twitter'
  117. ),
  118. 'licenses' => array(),
  119. 'terms_show' => 0,
  120. 'terms' => __('By clicking Register, you accept our <a href="#privacy_policy_url" target="_blank">Privacy Policy</a>', 'nextend-facebook-connect'),
  121. 'store_name' => 1,
  122. 'store_email' => 1,
  123. 'avatar_store' => 1,
  124. 'store_access_token' => 1,
  125. 'redirect_prevent_external' => 0,
  126. 'redirect' => '',
  127. 'redirect_reg' => '',
  128. 'default_redirect' => '',
  129. 'default_redirect_reg' => '',
  130. 'blacklisted_urls' => '',
  131. 'redirect_overlay' => 'overlay-with-spinner-and-message',
  132. 'target' => 'prefer-popup',
  133. 'allow_register' => -1,
  134. 'allow_unlink' => 1,
  135. 'show_login_form' => 'show',
  136. 'login_form_button_align' => 'left',
  137. 'show_registration_form' => 'show',
  138. 'login_form_button_style' => 'default',
  139. 'login_form_layout' => 'below',
  140. 'show_embedded_login_form' => 'show',
  141. 'embedded_login_form_button_align' => 'left',
  142. 'embedded_login_form_button_style' => 'default',
  143. 'embedded_login_form_layout' => 'below',
  144. 'custom_actions' => '',
  145. 'custom_actions_button_style' => 'default',
  146. 'custom_actions_button_layout' => 'default',
  147. 'custom_actions_button_align' => 'left',
  148. 'comment_login_button' => 'show',
  149. 'comment_button_align' => 'left',
  150. 'comment_button_style' => 'default',
  151. 'buddypress_register_button' => 'bp_before_account_details_fields',
  152. 'buddypress_register_button_align' => 'left',
  153. 'buddypress_register_button_style' => 'default',
  154. 'buddypress_register_form_layout' => 'default',
  155. 'buddypress_login' => 'show',
  156. 'buddypress_login_form_layout' => 'default',
  157. 'buddypress_login_button_style' => 'default',
  158. 'buddypress_sidebar_login' => 'show',
  159. 'woocommerce_login' => 'after',
  160. 'woocommerce_login_form_layout' => 'default',
  161. 'woocommerce_register' => 'after',
  162. 'woocommerce_register_form_layout' => 'default',
  163. 'woocommerce_billing' => 'before',
  164. 'woocommerce_billing_form_layout' => 'default',
  165. 'woocoommerce_form_button_style' => 'default',
  166. 'woocoommerce_form_button_align' => 'left',
  167. 'woocommerce_account_details' => 'before',
  168. 'woocommerce_cfw' => 'show',
  169. 'woocommerce_cfw_layout' => 'below',
  170. 'memberpress_login' => 'before',
  171. 'memberpress_form_button_align' => 'left',
  172. 'memberpress_login_form_button_style' => 'default',
  173. 'memberpress_login_form_layout' => 'below-separator',
  174. 'memberpress_signup' => 'before',
  175. 'memberpress_signup_form_button_style' => 'default',
  176. 'memberpress_signup_form_layout' => 'below-separator',
  177. 'memberpress_account_details' => 'after',
  178. 'registration_notification_notify' => '0',
  179. 'debug' => '0',
  180. 'show_linked_providers' => '0',
  181. 'login_restriction' => '0',
  182. 'avatars_in_all_media' => '0',
  183. 'custom_register_label' => '0',
  184. 'review_state' => -1,
  185. 'woocommerce_dismissed' => 0,
  186. 'woocoommerce_registration_email_template' => 'woocommerce',
  187. 'userpro_show_login_form' => 'show',
  188. 'userpro_show_register_form' => 'show',
  189. 'userpro_login_form_button_style' => 'default',
  190. 'userpro_login_form_layout' => 'below',
  191. 'userpro_register_form_button_style' => 'default',
  192. 'userpro_register_form_layout' => 'below',
  193. 'userpro_form_button_align' => 'left',
  194. 'ultimatemember_login' => 'after',
  195. 'ultimatemember_login_form_button_style' => 'default',
  196. 'ultimatemember_login_form_layout' => 'below-separator',
  197. 'ultimatemember_register' => 'after',
  198. 'ultimatemember_register_form_button_style' => 'default',
  199. 'ultimatemember_register_form_layout' => 'below-separator',
  200. 'ultimatemember_account_details' => 'after',
  201. 'ultimatemember_form_button_align' => 'left',
  202. 'edd_login' => 'after',
  203. 'edd_login_form_button_style' => 'default',
  204. 'edd_login_form_layout' => 'default',
  205. 'edd_register' => 'after',
  206. 'edd_register_form_button_style' => 'default',
  207. 'edd_register_form_layout' => 'default',
  208. 'edd_checkout' => 'form_after',
  209. 'edd_checkout_form_button_style' => 'default',
  210. 'edd_checkout_form_layout' => 'default',
  211. 'edd_form_button_align' => 'left',
  212. 'admin_bar_roles' => array(),
  213. ));
  214. add_action('itsec_initialized', 'NextendSocialLogin::disable_better_wp_security_block_long_urls', -1);
  215. add_action('bp_loaded', 'NextendSocialLogin::buddypress_loaded');
  216. }
  217. public static function plugins_loaded() {
  218. NextendSocialLoginAdmin::init();
  219. $lastVersion = get_option('nsl-version');
  220. if ($lastVersion != self::$version) {
  221. NextendSocialLogin::install();
  222. if (empty($lastVersion) || version_compare($lastVersion, '3.0.14', '<=')) {
  223. $old_license_status = NextendSocialLogin::$settings->get('license_key_ok');
  224. if ($old_license_status) {
  225. $domain = NextendSocialLogin::$settings->get('authorized_domain');
  226. if (empty($domain)) {
  227. $domain = self::getDomain();
  228. }
  229. NextendSocialLogin::$settings->set('licenses', array(
  230. array(
  231. 'license_key' => NextendSocialLogin::$settings->get('license_key'),
  232. 'domain' => $domain
  233. )
  234. ));
  235. }
  236. }
  237. update_option('nsl-version', self::$version, true);
  238. wp_redirect(set_url_scheme('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']));
  239. exit;
  240. } else if (isset($_REQUEST['repairnsl']) && current_user_can('manage_options') && check_admin_referer('repairnsl')) {
  241. self::install();
  242. wp_redirect(admin_url('admin.php?page=nextend-social-login'));
  243. exit;
  244. }
  245. do_action('nsl_start');
  246. load_plugin_textdomain('nextend-facebook-connect', false, basename(dirname(__FILE__)) . '/languages/');
  247. Notices::init();
  248. self::$providersPath = NSL_PATH . '/providers/';
  249. $providers = array_diff(scandir(self::$providersPath), array(
  250. '..',
  251. '.'
  252. ));
  253. foreach ($providers as $provider) {
  254. if (file_exists(self::$providersPath . $provider . '/' . $provider . '.php')) {
  255. require_once(self::$providersPath . $provider . '/' . $provider . '.php');
  256. }
  257. }
  258. do_action('nsl_add_providers');
  259. self::$ordering = array_flip(self::$settings->get('ordering'));
  260. uksort(self::$providers, 'NextendSocialLogin::sortProviders');
  261. uksort(self::$allowedProviders, 'NextendSocialLogin::sortProviders');
  262. uksort(self::$enabledProviders, 'NextendSocialLogin::sortProviders');
  263. do_action('nsl_providers_loaded');
  264. if (NextendSocialLogin::$settings->get('allow_register') != 1) {
  265. add_filter('nsl_is_register_allowed', 'NextendSocialLogin::is_register_allowed');
  266. }
  267. add_action('login_form_login', 'NextendSocialLogin::login_form_login');
  268. /**
  269. * We need smaller priority, as some plugins like Ultimate Member may trigger a redirect before us.
  270. */
  271. add_action('login_form_register', 'NextendSocialLogin::login_form_register', 9);
  272. add_action('login_form_link', 'NextendSocialLogin::login_form_link');
  273. add_action('bp_core_screen_signup', 'NextendSocialLogin::bp_login_form_register');
  274. add_action('login_form_unlink', 'NextendSocialLogin::login_form_unlink');
  275. add_action('template_redirect', 'NextendSocialLogin::alternate_login_page_template_redirect');
  276. add_action('parse_request', 'NextendSocialLogin::editProfileRedirect');
  277. //check if DOM is ready
  278. add_action('wp_print_scripts', 'NextendSocialLogin::nslDOMReady');
  279. if (count(self::$enabledProviders) > 0) {
  280. if (self::$settings->get('show_login_form') == 'hide') {
  281. add_action('login_form_login', 'NextendSocialLogin::removeLoginFormAssets');
  282. } else {
  283. add_action('login_form', 'NextendSocialLogin::addLoginFormButtons');
  284. }
  285. if (NextendSocialLogin::$settings->get('show_registration_form') == 'hide') {
  286. add_action('login_form_register', 'NextendSocialLogin::removeLoginFormAssets');
  287. } else {
  288. add_action('register_form', 'NextendSocialLogin::addRegisterFormButtons');
  289. }
  290. if (NextendSocialLogin::$settings->get('show_embedded_login_form') != 'hide') {
  291. add_filter('login_form_bottom', 'NextendSocialLogin::filterAddEmbeddedLoginFormButtons');
  292. }
  293. //some themes trigger both the bp_sidebar_login_form action and the login_form action.
  294. switch (NextendSocialLogin::$settings->get('buddypress_sidebar_login')) {
  295. case 'show':
  296. add_action('bp_sidebar_login_form', 'NextendSocialLogin::addLoginButtons');
  297. break;
  298. }
  299. add_action('profile_personal_options', 'NextendSocialLogin::addLinkAndUnlinkButtons');
  300. /*
  301. * Shopkeeper theme fix. Remove normal login form hooks while WooCommerce registration/login form rendering
  302. */
  303. add_action('woocommerce_login_form_start', 'NextendSocialLogin::remove_action_login_form_buttons');
  304. add_action('woocommerce_login_form_end', 'NextendSocialLogin::add_action_login_form_buttons');
  305. add_action('woocommerce_register_form_start', 'NextendSocialLogin::remove_action_login_form_buttons');
  306. add_action('woocommerce_register_form_end', 'NextendSocialLogin::add_action_login_form_buttons');
  307. /* End of fix */
  308. add_action('wp_head', 'NextendSocialLogin::styles', 100);
  309. /*
  310. *
  311. * We need to call in our styles on the AMP pages using this action, since:
  312. * -the "AMP" plugin does not call wp_head in Reader mode.
  313. * -the "AMP for WP" plugin does not call wp_head in AMP view at all.
  314. * -AMP plugins only allow adding custom CSS in the unique <style> tag with the attribute "amp-custom". Callbacks are only allowed to output bare CSS on this action.
  315. */
  316. add_action('amp_post_template_css', 'NextendSocialLogin::stylesWithoutTag');
  317. add_action('admin_head', 'NextendSocialLogin::styles', 100);
  318. add_action('login_head', 'NextendSocialLogin::loginHead', 100);
  319. add_action('wp_print_footer_scripts', 'NextendSocialLogin::scripts', 100);
  320. add_action('login_footer', 'NextendSocialLogin::scripts', 100);
  321. require_once dirname(__FILE__) . '/includes/avatar.php';
  322. add_shortcode('nextend_social_login', 'NextendSocialLogin::shortcode');
  323. }
  324. add_action('admin_print_footer_scripts', 'NextendSocialLogin::scripts', 100);
  325. require_once(NSL_PATH . '/widget.php');
  326. do_action('nsl_init');
  327. /**
  328. * Fix for Hide my WP plugin @see https://codecanyon.net/item/hide-my-wp-amazing-security-plugin-for-wordpress/4177158
  329. */
  330. if (class_exists('HideMyWP', false)) {
  331. if (!empty($_REQUEST['loginSocial'])) {
  332. global $HideMyWP;
  333. $loginPath = '/wp-login.php';
  334. if (is_object($HideMyWP) && substr($_SERVER['PHP_SELF'], -1 * strlen($loginPath))) {
  335. $login_query = $HideMyWP->opt('login_query');
  336. if (!$login_query) {
  337. $login_query = 'hide_my_wp';
  338. }
  339. $_GET[$login_query] = $HideMyWP->opt('admin_key');
  340. }
  341. }
  342. }
  343. if (!empty($_REQUEST['loginSocial'])) {
  344. // Fix for all-in-one-wp-security-and-firewall
  345. if (empty($_GET['action'])) {
  346. $_GET['action'] = 'nsl-login';
  347. }
  348. // Fix for wps-hide-login
  349. if (empty($_REQUEST['action'])) {
  350. $_REQUEST['action'] = 'nsl-login';
  351. }
  352. // Fix for Social Rabbit as it catch our code response from Facebook
  353. if (class_exists('\SR\Utils\Scheduled', true)) {
  354. add_action('init', 'NextendSocialLogin::fixSocialRabbit', 0);
  355. }
  356. // Fix for Dokan https://wedevs.com/dokan/
  357. if (function_exists('dokan_redirect_to_register')) {
  358. remove_action('login_init', 'dokan_redirect_to_register', 10);
  359. }
  360. // Fix for Jetpack SSO
  361. add_filter('jetpack_sso_bypass_login_forward_wpcom', '__return_false');
  362. /**
  363. * Fix: our autologin after the registration prevents WooRewards (MyRewards) plugin from awarding the points for the registration
  364. * so we need to make our autologin happen after WooRewards have already awarded the points. They use 999999 priority.
  365. * @url https://plugins.longwatchstudio.com/product/woorewards/
  366. */
  367. if (class_exists('LWS_WooRewards')) {
  368. add_filter('nsl_autologin_priority', function () {
  369. return 10000000;
  370. });
  371. }
  372. }
  373. }
  374. public static function fixSocialRabbit() {
  375. remove_action('init', '\SR\Utils\Scheduled::init', 10);
  376. }
  377. public static function removeLoginFormAssets() {
  378. remove_action('login_head', 'NextendSocialLogin::loginHead', 100);
  379. remove_action('wp_print_footer_scripts', 'NextendSocialLogin::scripts', 100);
  380. remove_action('login_footer', 'NextendSocialLogin::scripts', 100);
  381. }
  382. public static function styles() {
  383. $stylesheet = self::get_template_part('style.css');
  384. if (!empty($stylesheet) && file_exists($stylesheet)) {
  385. echo '<style type="text/css">' . file_get_contents($stylesheet) . '</style>';
  386. }
  387. }
  388. public static function stylesWithoutTag() {
  389. $stylesheet = self::get_template_part('style.css');
  390. if (!empty($stylesheet) && file_exists($stylesheet)) {
  391. echo file_get_contents($stylesheet);
  392. }
  393. }
  394. public static function nslDOMReady() {
  395. echo '<script type="text/javascript">
  396. window._nslDOMReady = function (callback) {
  397. if ( document.readyState === "complete" || document.readyState === "interactive" ) {
  398. callback();
  399. } else {
  400. document.addEventListener( "DOMContentLoaded", callback );
  401. }
  402. };
  403. </script>';
  404. }
  405. public static function loginHead() {
  406. self::styles();
  407. $template = self::get_template_part('login/' . sanitize_file_name(self::$settings->get('login_form_layout')) . '.php');
  408. if (!empty($template) && file_exists($template)) {
  409. require($template);
  410. }
  411. self::$loginHeadAdded = true;
  412. }
  413. public static function scripts() {
  414. static $once = null;
  415. if ($once === null) {
  416. $scripts = NSL_PATH . '/js/nsl.js';
  417. if (file_exists($scripts)) {
  418. $localizedStrings = array(
  419. 'redirect_overlay_title' => __('Hold On', 'nextend-facebook-connect'),
  420. 'redirect_overlay_text' => __('You are being redirected to another page,<br>it may take a few seconds.', 'nextend-facebook-connect')
  421. );
  422. echo '<script type="text/javascript">(function (undefined) {var _localizedStrings=' . wp_json_encode($localizedStrings) . ';var _targetWindow=' . wp_json_encode(self::$settings->get('target')) . ';var _redirectOverlay=' . wp_json_encode(self::$settings->get('redirect_overlay')) . ";\n" . file_get_contents($scripts) . '})();</script>';
  423. }
  424. $once = true;
  425. }
  426. }
  427. public static function install() {
  428. /** @var $wpdb WPDB */ global $wpdb;
  429. $table_name = $wpdb->prefix . "social_users";
  430. $charset_collate = $wpdb->get_charset_collate();
  431. $lastVersion = get_option('nsl-version');
  432. /*
  433. * We should run these codes only if our database table already exists.
  434. */
  435. if ($wpdb->get_var("SHOW TABLES LIKE '" . $table_name . "'") === $table_name) {
  436. /**
  437. * In 3.0.27 we added a new column to the social_users table as autoincrement and primary key.
  438. * This causes an SQL error for the dbDelta() function so we need to add it beforehand.
  439. */
  440. if (version_compare($lastVersion, '3.0.26', '<=')) {
  441. $row = $wpdb->get_results("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '" . DB_NAME . "' AND TABLE_NAME = '" . $table_name . "' AND COLUMN_NAME = 'social_users_id';");
  442. if (!$row) {
  443. $alterQuery = "ALTER TABLE " . $table_name . " ADD `social_users_id` int NOT NULL AUTO_INCREMENT PRIMARY KEY;";
  444. $wpdb->query($alterQuery);
  445. }
  446. }
  447. if (version_compare($lastVersion, '3.0.27', '<=')) {
  448. /*
  449. * In version 3.0.21 we started storing the register_date, login_date and link_date with '0000-00-00 00:00:00' as default value.
  450. * That value returned an invalid value error on databases where 'sql_mode' has 'NO_ZERO_DATE, NO_ZERO_IN_DATE' modes, so it prevented us from modifying our database structure.
  451. */
  452. $row = $wpdb->get_results("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '" . DB_NAME . "' AND TABLE_NAME = '" . $table_name . "' AND COLUMN_NAME = 'register_date';");
  453. if (!empty($row)) {
  454. $alterQuery = "ALTER TABLE " . $table_name . " CHANGE `register_date` `register_date` datetime DEFAULT NULL, CHANGE `login_date` `login_date` datetime DEFAULT NULL, CHANGE `link_date` `link_date` datetime DEFAULT NULL;";
  455. $result = $wpdb->query($alterQuery);
  456. if ($result) {
  457. $wpdb->update($table_name, array('register_date' => NULL,), array(
  458. 'register_date' => '0000-00-00 00:00:00'
  459. ));
  460. $wpdb->update($table_name, array('login_date' => NULL,), array(
  461. 'login_date' => '0000-00-00 00:00:00'
  462. ));
  463. $wpdb->update($table_name, array('link_date' => NULL,), array(
  464. 'link_date' => '0000-00-00 00:00:00'
  465. ));
  466. }
  467. }
  468. }
  469. }
  470. $sql = "CREATE TABLE " . $table_name . " (
  471. `social_users_id` int NOT NULL AUTO_INCREMENT,
  472. `ID` int NOT NULL,
  473. `type` varchar(20) NOT NULL,
  474. `identifier` varchar(100) NOT NULL,
  475. `register_date` datetime default NULL,
  476. `login_date` datetime default NULL,
  477. `link_date` datetime default NULL,
  478. PRIMARY KEY (social_users_id),
  479. KEY `ID` (`ID`,`type`),
  480. KEY `identifier` (`identifier`)
  481. ) " . $charset_collate . ";";
  482. require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
  483. dbDelta($sql);
  484. }
  485. public static function sortProviders($a, $b) {
  486. if (isset(self::$ordering[$a]) && isset(self::$ordering[$b])) {
  487. if (self::$ordering[$a] < self::$ordering[$b]) {
  488. return -1;
  489. }
  490. return 1;
  491. }
  492. if (isset(self::$ordering[$a])) {
  493. return -1;
  494. }
  495. return 1;
  496. }
  497. /**
  498. * @param $provider NextendSocialProviderDummy
  499. */
  500. public static function addProvider($provider) {
  501. if (in_array($provider->getId(), self::$settings->get('enabled'))) {
  502. if ($provider->isTested() && $provider->enable()) {
  503. self::$enabledProviders[$provider->getId()] = $provider;
  504. }
  505. }
  506. self::$providers[$provider->getId()] = $provider;
  507. if ($provider instanceof NextendSocialProvider) {
  508. self::$allowedProviders[$provider->getId()] = $provider;
  509. }
  510. }
  511. public static function enableProvider($providerID) {
  512. if (isset(self::$providers[$providerID])) {
  513. $enabled = self::$settings->get('enabled');
  514. $enabled[] = self::$providers[$providerID]->getId();
  515. $enabled = array_unique($enabled);
  516. self::$settings->update(array(
  517. 'enabled' => $enabled
  518. ));
  519. }
  520. }
  521. public static function disableProvider($providerID) {
  522. if (isset(self::$providers[$providerID])) {
  523. $enabled = array_diff(self::$settings->get('enabled'), array(self::$providers[$providerID]->getId()));
  524. self::$settings->update(array(
  525. 'enabled' => $enabled
  526. ));
  527. }
  528. }
  529. public static function isProviderEnabled($providerID) {
  530. return isset(self::$enabledProviders[$providerID]);
  531. }
  532. public static function alternate_login_page_template_redirect() {
  533. $isAlternatePage = ((self::getProxyPage() !== false && (is_page(self::getProxyPage()) || get_permalink() === get_permalink(self::getProxyPage()))) || (self::getRegisterFlowPage() !== false && (is_page(self::getRegisterFlowPage()) || get_permalink() === get_permalink(self::getRegisterFlowPage()))));
  534. if ($isAlternatePage) {
  535. nocache_headers();
  536. if (!empty($_REQUEST['loginSocial']) || (isset($_GET['interim_login']) && $_GET['interim_login'] === 'nsl')) {
  537. $action = isset($_GET['action']) ? $_GET['action'] : 'login';
  538. if (!in_array($action, array(
  539. 'login',
  540. 'register',
  541. 'link',
  542. 'unlink'
  543. ))) {
  544. $action = 'login';
  545. }
  546. switch ($action) {
  547. case 'login':
  548. NextendSocialLogin::login_form_login();
  549. break;
  550. case 'register':
  551. NextendSocialLogin::login_form_register();
  552. break;
  553. case 'link':
  554. NextendSocialLogin::login_form_link();
  555. break;
  556. case 'unlink':
  557. NextendSocialLogin::login_form_unlink();
  558. break;
  559. }
  560. } else {
  561. if (!is_front_page() && !is_home()) {
  562. if (Notices::hasErrors()) {
  563. wp_redirect(NextendSocialLogin::enableNoticeForUrl(home_url()));
  564. exit;
  565. }
  566. wp_redirect(home_url());
  567. exit;
  568. }
  569. }
  570. }
  571. }
  572. public static function login_form_login() {
  573. self::$WPLoginCurrentView = 'login';
  574. self::login_init();
  575. }
  576. public static function login_form_register() {
  577. self::$WPLoginCurrentView = 'register';
  578. self::login_init();
  579. }
  580. public static function bp_login_form_register() {
  581. self::$WPLoginCurrentView = 'register-bp';
  582. self::login_init();
  583. }
  584. public static function login_form_link() {
  585. self::$WPLoginCurrentView = 'link';
  586. self::login_init();
  587. }
  588. public static function login_form_unlink() {
  589. self::$WPLoginCurrentView = 'unlink';
  590. self::login_init();
  591. }
  592. public static function login_init() {
  593. add_filter('wp_login_errors', 'NextendSocialLogin::wp_login_errors');
  594. if (isset($_GET['interim_login']) && $_GET['interim_login'] === 'nsl' && is_user_logged_in()) {
  595. self::onInterimLoginSuccess();
  596. }
  597. if (isset($_REQUEST['loginFacebook']) && $_REQUEST['loginFacebook'] == '1') {
  598. $_REQUEST['loginSocial'] = 'facebook';
  599. }
  600. if (isset($_REQUEST['loginGoogle']) && $_REQUEST['loginGoogle'] == '1') {
  601. $_REQUEST['loginSocial'] = 'google';
  602. }
  603. if (isset($_REQUEST['loginTwitter']) && $_REQUEST['loginTwitter'] == '1') {
  604. $_REQUEST['loginTwitter'] = 'twitter';
  605. }
  606. if (isset($_REQUEST['loginSocial']) && is_string($_REQUEST['loginSocial']) && isset(self::$providers[$_REQUEST['loginSocial']]) && (self::$providers[$_REQUEST['loginSocial']]->isEnabled() || self::$providers[$_REQUEST['loginSocial']]->isTest())) {
  607. nocache_headers();
  608. self::$providers[$_REQUEST['loginSocial']]->connect();
  609. }
  610. }
  611. private static function onInterimLoginSuccess() {
  612. require_once(NSL_PATH . '/admin/interim.php');
  613. }
  614. public static function wp_login_errors($errors) {
  615. if (empty($errors)) {
  616. $errors = new WP_Error();
  617. }
  618. $errorMessages = Notices::getErrors();
  619. if ($errorMessages !== false) {
  620. foreach ($errorMessages as $errorMessage) {
  621. $errors->add('error', $errorMessage);
  622. }
  623. }
  624. return $errors;
  625. }
  626. public static function editProfileRedirect() {
  627. global $wp;
  628. if (isset($wp->query_vars['editProfileRedirect'])) {
  629. if (function_exists('bp_loggedin_user_domain')) {
  630. header('LOCATION: ' . bp_loggedin_user_domain() . 'profile/edit/group/1/');
  631. } else {
  632. header('LOCATION: ' . self_admin_url('profile.php'));
  633. }
  634. exit;
  635. }
  636. }
  637. public static function filterAddEmbeddedLoginFormButtons($ret) {
  638. return $ret . self::getEmbeddedLoginForm();
  639. }
  640. private static function getEmbeddedLoginForm($labelType = 'login') {
  641. ob_start();
  642. self::styles();
  643. $index = self::$counter++;
  644. $containerID = 'nsl-custom-login-form-' . $index;
  645. echo '<div id="' . $containerID . '">' . self::renderButtonsWithContainer(self::$settings->get('embedded_login_form_button_style'), false, false, false, self::$settings->get('embedded_login_form_button_align'), $labelType) . '</div>';
  646. $template = self::get_template_part('embedded-login/' . sanitize_file_name(self::$settings->get('embedded_login_form_layout')) . '.php');
  647. if (!empty($template) && file_exists($template)) {
  648. include($template);
  649. }
  650. return ob_get_clean();
  651. }
  652. public static function addLoginFormButtons() {
  653. echo self::getRenderedLoginButtons();
  654. }
  655. public static function addLoginButtons() {
  656. echo self::getRenderedLoginButtons();
  657. }
  658. public static function addRegisterFormButtons() {
  659. echo self::getRenderedLoginButtons('register');
  660. }
  661. public static function remove_action_login_form_buttons() {
  662. remove_action('login_form', 'NextendSocialLogin::addLoginFormButtons');
  663. remove_action('register_form', 'NextendSocialLogin::addRegisterFormButtons');
  664. }
  665. public static function add_action_login_form_buttons() {
  666. add_action('login_form', 'NextendSocialLogin::addLoginFormButtons');
  667. add_action('register_form', 'NextendSocialLogin::addRegisterFormButtons');
  668. }
  669. private static function getRenderedLoginButtons($labelType = 'login') {
  670. if (!self::$loginHeadAdded || self::$loginMainButtonsAdded) {
  671. return self::getEmbeddedLoginForm($labelType);
  672. }
  673. self::$loginMainButtonsAdded = true;
  674. $ret = '<div id="nsl-custom-login-form-main">';
  675. $ret .= self::renderButtonsWithContainer(self::$settings->get('login_form_button_style'), false, false, false, self::$settings->get('login_form_button_align'), $labelType);
  676. $ret .= '</div>';
  677. return $ret;
  678. }
  679. public static function addLinkAndUnlinkButtons() {
  680. echo self::renderLinkAndUnlinkButtons();
  681. }
  682. /**
  683. * @param bool|false|string $heading
  684. * @param bool $link
  685. * @param bool $unlink
  686. * @param string $align
  687. * @param array|string $providers
  688. * @param string $style
  689. *
  690. * @return string
  691. */
  692. public static function renderLinkAndUnlinkButtons($heading = '', $link = true, $unlink = true, $align = "left", $providers = false, $style = "default") {
  693. if (count(self::$enabledProviders)) {
  694. /**
  695. * We shouldn't allow the icon style for Link and Unlink buttons
  696. */
  697. if ($style === 'icon') {
  698. $style = 'default';
  699. }
  700. $buttons = '';
  701. if ($heading !== false) {
  702. if (empty($heading)) {
  703. $heading = __('Social Login', 'nextend-facebook-connect');
  704. }
  705. $buttons = '<h2>' . $heading . '</h2>';
  706. }
  707. if ($unlink) {
  708. //Filter to disable unlinking social accounts
  709. $isUnlinkAllowed = apply_filters('nsl_allow_unlink', true);
  710. if (!$isUnlinkAllowed) {
  711. $unlink = false;
  712. }
  713. }
  714. $enabledProviders = false;
  715. if (is_array($providers)) {
  716. $enabledProviders = array();
  717. foreach ($providers as $provider) {
  718. if ($provider && isset(self::$enabledProviders[$provider->getId()])) {
  719. $enabledProviders[$provider->getId()] = $provider;
  720. }
  721. }
  722. }
  723. if ($enabledProviders === false) {
  724. $enabledProviders = self::$enabledProviders;
  725. }
  726. if (count($enabledProviders)) {
  727. $buttons = '';
  728. foreach ($enabledProviders as $provider) {
  729. if ($provider->isCurrentUserConnected()) {
  730. if ($unlink) {
  731. $buttons .= $provider->getUnLinkButton();
  732. }
  733. } else {
  734. if ($link) {
  735. $buttons .= $provider->getLinkButton();
  736. }
  737. }
  738. }
  739. $buttons = '<div class="nsl-container-buttons">' . $buttons . '</div>';
  740. return '<div class="nsl-container ' . self::$styles[$style]['container'] . '"' . ($style !== 'fullwidth' ? ' data-align="' . esc_attr($align) . '"' : '') . '>' . $buttons . '</div>';
  741. }
  742. }
  743. return '';
  744. }
  745. /**
  746. * @param $user_id
  747. *
  748. * @return bool
  749. * @deprecated
  750. *
  751. */
  752. public static function getAvatar($user_id) {
  753. foreach (self::$enabledProviders as $provider) {
  754. $avatar = $provider->getAvatar($user_id);
  755. if ($avatar !== false) {
  756. return $avatar;
  757. }
  758. }
  759. return false;
  760. }
  761. public static function shortcode($atts) {
  762. if (!is_array($atts)) {
  763. $atts = array();
  764. }
  765. $atts = array_merge(array(
  766. 'style' => 'default',
  767. 'provider' => false,
  768. 'login' => 1,
  769. 'link' => 0,
  770. 'unlink' => 0,
  771. 'heading' => false,
  772. 'align' => 'left',
  773. ), $atts);
  774. $providers = false;
  775. $providerID = $atts['provider'] === false ? false : $atts['provider'];
  776. if ($providerID !== false && isset(self::$enabledProviders[$providerID])) {
  777. $providers = array(self::$enabledProviders[$providerID]);
  778. }
  779. if (!is_user_logged_in()) {
  780. if (filter_var($atts['login'], FILTER_VALIDATE_BOOLEAN) === false) {
  781. return '';
  782. }
  783. $atts = array_merge(array(
  784. 'redirect' => false,
  785. 'trackerdata' => false,
  786. 'labeltype' => 'login'
  787. ), $atts);
  788. return self::renderButtonsWithContainerAndTitle($atts['heading'], $atts['style'], $providers, $atts['redirect'], $atts['trackerdata'], $atts['align'], $atts['labeltype']);
  789. }
  790. $link = filter_var($atts['link'], FILTER_VALIDATE_BOOLEAN);
  791. $unlink = filter_var($atts['unlink'], FILTER_VALIDATE_BOOLEAN);
  792. if ($link || $unlink) {
  793. return self::renderLinkAndUnlinkButtons($atts['heading'], $link, $unlink, $atts['align'], $providers, $atts['style']);
  794. }
  795. return '';
  796. }
  797. /**
  798. * @param string $style
  799. * @param bool|NextendSocialProvider[] $providers
  800. * @param bool|string $redirect_to
  801. * @param bool $trackerData
  802. * @param string $align
  803. * @param string $labelType
  804. *
  805. * @return string
  806. */
  807. public static function renderButtonsWithContainer($style = 'default', $providers = false, $redirect_to = false, $trackerData = false, $align = 'left', $labelType = 'login') {
  808. return self::renderButtonsWithContainerAndTitle(false, $style, $providers, $redirect_to, $trackerData, $align, $labelType);
  809. }
  810. private static function renderButtonsWithContainerAndTitle($heading = false, $style = 'default', $providers = false, $redirect_to = false, $trackerData = false, $align = 'left', $labelType = 'login') {
  811. if (!isset(self::$styles[$style])) {
  812. $style = 'default';
  813. }
  814. if (!in_array($align, self::$styles[$style]['align'])) {
  815. $align = 'left';
  816. }
  817. $enabledProviders = false;
  818. if (is_array($providers)) {
  819. $enabledProviders = array();
  820. foreach ($providers as $provider) {
  821. if ($provider && isset(self::$enabledProviders[$provider->getId()])) {
  822. $enabledProviders[$provider->getId()] = $provider;
  823. }
  824. }
  825. }
  826. if ($enabledProviders === false) {
  827. $enabledProviders = self::$enabledProviders;
  828. }
  829. if (count($enabledProviders)) {
  830. $buttons = '';
  831. foreach ($enabledProviders as $provider) {
  832. $buttons .= $provider->getConnectButton($style, $redirect_to, $trackerData, $labelType);
  833. }
  834. if (!empty($heading)) {
  835. $heading = '<h2>' . $heading . '</h2>';
  836. } else {
  837. $heading = '';
  838. }
  839. $buttons = '<div class="nsl-container-buttons">' . $buttons . '</div>';
  840. $ret = '<div class="nsl-container ' . self::$styles[$style]['container'] . '"' . ($style !== 'fullwidth' ? ' data-align="' . esc_attr($align) . '"' : '') . '>' . $heading . $buttons . '</div>';
  841. if (defined('DOING_AJAX') && DOING_AJAX) {
  842. $id = md5(uniqid('nsl-ajax-'));
  843. $ret = '<div id="' . $id . '">' . $ret . '</div><script>window._nslDOMReady(function(){var socialButtonContainer=document.getElementById("' . $id . '");if(socialButtonContainer){var socialButtons=socialButtonContainer.querySelectorAll("a");socialButtons.forEach(function(el,i){var href=el.getAttribute("href");if(href.indexOf("?")===-1){href+="?"}else{href+="&"}
  844. el.setAttribute("href",href+"redirect="+encodeURIComponent(window.location.href))})}});</script>';
  845. }
  846. return $ret;
  847. }
  848. return '';
  849. }
  850. public static function getCurrentPageURL() {
  851. if (defined('DOING_AJAX') && DOING_AJAX) {
  852. return false;
  853. }
  854. $currentUrl = set_url_scheme('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
  855. if (!self::isAllowedRedirectUrl($currentUrl)) {
  856. return false;
  857. }
  858. return $currentUrl;
  859. }
  860. public static function getLoginUrl($scheme = 'login') {
  861. static $alternateLoginPage = null;
  862. if ($alternateLoginPage === null) {
  863. $proxyPage = self::getProxyPage();
  864. if ($proxyPage !== false) {
  865. $alternateLoginPage = get_permalink($proxyPage);
  866. }
  867. if (empty($alternateLoginPage)) {
  868. $alternateLoginPage = false;
  869. }
  870. }
  871. if ($alternateLoginPage !== false) {
  872. return $alternateLoginPage;
  873. }
  874. return site_url('wp-login.php', $scheme);
  875. }
  876. public static function getRegisterUrl() {
  877. return wp_registration_url();
  878. }
  879. public static function isAllowedRedirectUrl($url) {
  880. $loginUrl = self::getLoginUrl();
  881. // If the currentUrl is the loginUrl, then we should not return it for redirects
  882. if (strpos($url, $loginUrl) === 0) {
  883. return false;
  884. }
  885. $loginUrl2 = site_url('wp-login.php');
  886. // If the currentUrl is the loginUrl, then we should not return it for redirects
  887. if ($loginUrl2 !== $loginUrl && strpos($url, $loginUrl2) === 0) {
  888. return false;
  889. }
  890. $registerUrl = wp_registration_url();
  891. // If the currentUrl is the registerUrl, then we should not return it for redirects
  892. if (strpos($url, $registerUrl) === 0) {
  893. return false;
  894. }
  895. $blacklistedUrls = NextendSocialLogin::$settings->get('blacklisted_urls');
  896. if (!empty($blacklistedUrls)) {
  897. $blackListedUrlArray = preg_split('/\r\n|\r|\n/', $blacklistedUrls);
  898. // If the currentUrl is blacklisted, then we should not return it for redirects
  899. foreach ($blackListedUrlArray as $blackListedUrl) {
  900. //If the url contains the blackListedUrl returns false
  901. if (strpos($url, $blackListedUrl) !== false) {
  902. return false;
  903. }
  904. }
  905. }
  906. return true;
  907. }
  908. public static function get_template_part($file_name, $name = null) {
  909. // Execute code for this part
  910. do_action('get_template_part_' . $file_name, $file_name, $name);
  911. // Setup possible parts
  912. $templates = array();
  913. $templates[] = $file_name;
  914. // Allow template parts to be filtered
  915. $templates = apply_filters('nsl_get_template_part', $templates, $file_name, $name);
  916. // Return the part that is found
  917. return self::locate_template($templates);
  918. }
  919. public static function locate_template($template_names) {
  920. // No file found yet
  921. $located = false;
  922. // Try to find a template file
  923. foreach ((array)$template_names as $template_name) {
  924. // Continue if template is empty
  925. if (empty($template_name)) {
  926. continue;
  927. }
  928. // Trim off any slashes from the template name
  929. $template_name = ltrim($template_name, '/');
  930. // Check child theme first
  931. if (file_exists(trailingslashit(get_stylesheet_directory()) . 'nsl/' . $template_name)) {
  932. $located = trailingslashit(get_stylesheet_directory()) . 'nsl/' . $template_name;
  933. break;
  934. // Check parent theme next
  935. } else if (file_exists(trailingslashit(get_template_directory()) . 'nsl/' . $template_name)) {
  936. $located = trailingslashit(get_template_directory()) . 'nsl/' . $template_name;
  937. break;
  938. // Check theme compatibility last
  939. } else if (file_exists(trailingslashit(self::get_templates_dir()) . $template_name)) {
  940. $located = trailingslashit(self::get_templates_dir()) . $template_name;
  941. break;
  942. } else if (defined('NSL_PRO_PATH') && file_exists(trailingslashit(NSL_PRO_PATH) . 'template-parts/' . $template_name)) {
  943. $located = trailingslashit(NSL_PRO_PATH) . 'template-parts/' . $template_name;
  944. break;
  945. }
  946. }
  947. return $located;
  948. }
  949. public static function get_templates_dir() {
  950. return NSL_PATH . '/template-parts';
  951. }
  952. public static function delete_user($user_id) {
  953. /** @var $wpdb WPDB */ global $wpdb, $blog_id;
  954. $wpdb->delete($wpdb->prefix . 'social_users', array(
  955. 'ID' => $user_id
  956. ), array(
  957. '%d'
  958. ));
  959. $attachment_id = get_user_meta($user_id, $wpdb->get_blog_prefix($blog_id) . 'user_avatar', true);
  960. if (wp_attachment_is_image($attachment_id)) {
  961. wp_delete_attachment($attachment_id, true);
  962. }
  963. }
  964. public static function disable_better_wp_security_block_long_urls() {
  965. if (class_exists('ITSEC_System_Tweaks', false)) {
  966. remove_action('itsec_initialized', array(
  967. ITSEC_System_Tweaks::get_instance(),
  968. 'block_long_urls'
  969. ));
  970. }
  971. }
  972. public static function buddypress_loaded() {
  973. add_action('bp_settings_setup_nav', 'NextendSocialLogin::bp_settings_setup_nav');
  974. }
  975. public static function bp_settings_setup_nav() {
  976. if (!bp_is_active('settings')) {
  977. return;
  978. }
  979. // Determine user to use.
  980. if (bp_loggedin_user_domain()) {
  981. $user_domain = bp_loggedin_user_domain();
  982. } else {
  983. return;
  984. }
  985. // Get the settings slug.
  986. $settings_slug = bp_get_settings_slug();
  987. bp_core_new_subnav_item(array(
  988. 'name' => __('Social Accounts', 'nextend-facebook-connect'),
  989. 'slug' => 'social',
  990. 'parent_url' => trailingslashit($user_domain . $settings_slug),
  991. 'parent_slug' => $settings_slug,
  992. 'screen_function' => 'NextendSocialLogin::bp_display_account_link',
  993. 'position' => 30,
  994. 'user_has_access' => bp_core_can_edit_settings()
  995. ), 'members');
  996. }
  997. public static function bp_display_account_link() {
  998. add_action('bp_template_title', 'NextendSocialLogin::bp_template_title');
  999. add_action('bp_template_content', 'NextendSocialLogin::bp_template_content');
  1000. bp_core_load_template(apply_filters('bp_core_template_plugin', 'members/single/plugins'));
  1001. }
  1002. public static function bp_template_title() {
  1003. _e('Social Login', 'nextend-facebook-connect');
  1004. }
  1005. public static function bp_template_content() {
  1006. echo self::renderLinkAndUnlinkButtons(false, true, true, NextendSocialLogin::$settings->get('buddypress_register_button_align'), false, NextendSocialLogin::$settings->get('buddypress_login_button_style'));
  1007. }
  1008. public static function getTrackerData() {
  1009. return Persistent::get('trackerdata');
  1010. }
  1011. public static function getDomain() {
  1012. return preg_replace('/^www\./', '', parse_url(site_url(), PHP_URL_HOST));
  1013. }
  1014. public static function getRegisterFlowPage() {
  1015. static $registerFlowPage = null;
  1016. if ($registerFlowPage === null) {
  1017. $registerFlowPage = intval(self::$settings->get('register-flow-page'));
  1018. if (empty($registerFlowPage) || get_post($registerFlowPage) === null) {
  1019. $registerFlowPage = false;
  1020. }
  1021. }
  1022. return $registerFlowPage;
  1023. }
  1024. public static function getProxyPage() {
  1025. static $proxyPage = null;
  1026. if ($proxyPage === null) {
  1027. $proxyPage = intval(self::$settings->get('proxy-page'));
  1028. if (empty($proxyPage) || get_post($proxyPage) === null) {
  1029. $proxyPage = false;
  1030. }
  1031. }
  1032. return $proxyPage;
  1033. }
  1034. public static function getFreePagesForRegisterFlow($pages) {
  1035. $availablePages = array();
  1036. foreach ($pages as $page) {
  1037. $post_states = array();
  1038. $post_states = apply_filters('display_post_states', $post_states, $page);
  1039. if (NextendSocialLogin::getRegisterFlowPage() === $page->ID || !$post_states || (count($post_states) === 1 && array_intersect(self::$allowedPostStates, array_keys($post_states)))) {
  1040. $availablePages[] = $page;
  1041. }
  1042. }
  1043. return $availablePages;
  1044. }
  1045. public static function getFreePagesForOauthProxyPage($pages) {
  1046. $availablePages = array();
  1047. foreach ($pages as $page) {
  1048. $post_states = array();
  1049. $post_states = apply_filters('display_post_states', $post_states, $page);
  1050. if (NextendSocialLogin::getProxyPage() === $page->ID || !$post_states || (count($post_states) === 1 && array_intersect(self::$allowedPostStates, array_keys($post_states)))) {
  1051. $availablePages[] = $page;
  1052. }
  1053. }
  1054. return $availablePages;
  1055. }
  1056. public static function is_register_allowed($isAllowed) {
  1057. $allow_register = NextendSocialLogin::$settings->get('allow_register');
  1058. switch ($allow_register) {
  1059. //WordPress default membership
  1060. case -1:
  1061. if (get_option('users_can_register')) {
  1062. return true;
  1063. }
  1064. break;
  1065. }
  1066. return false;
  1067. }
  1068. public static function hasLicense($strict = true) {
  1069. return self::getLicense($strict) !== false;
  1070. }
  1071. public static function getLicense($strict = true) {
  1072. $licenses = NextendSocialLogin::$settings->get('licenses');
  1073. $currentDomain = '.' . NextendSocialLogin::getDomain();
  1074. $currentDomainLength = strlen($currentDomain);
  1075. for ($i = 0; $i < count($licenses); $i++) {
  1076. $authorizedDomain = '.' . preg_replace('/^www\./', '', $licenses[$i]['domain']);
  1077. $authorizedDomainLength = strlen($authorizedDomain);
  1078. if ($authorizedDomain === $currentDomain || strrpos($currentDomain, $authorizedDomain) === $currentDomainLength - $authorizedDomainLength) {
  1079. return $licenses[$i];
  1080. }
  1081. if (strrpos($currentDomain, $authorizedDomain) === $currentDomainLength - $authorizedDomainLength) {
  1082. return $licenses[$i];
  1083. }
  1084. if (strrpos($authorizedDomain, $currentDomain) === $authorizedDomainLength - $currentDomainLength) {
  1085. return $licenses[$i];
  1086. }
  1087. }
  1088. if (!$strict && !empty($licenses)) {
  1089. return $licenses[0];
  1090. }
  1091. return false;
  1092. }
  1093. public static function hasConfigurationWithNoEnabledProviders() {
  1094. if (count(NextendSocialLogin::$enabledProviders) === 0) {
  1095. foreach (NextendSocialLogin::$providers as $provider) {
  1096. $state = $provider->getState();
  1097. // Has providers configured, but none of them are enabled
  1098. if ($state === 'disabled') {
  1099. return true;
  1100. }
  1101. }
  1102. }
  1103. return false;
  1104. }
  1105. public static function enableNoticeForUrl($url) {
  1106. return add_query_arg(array('nsl-notice' => 1), $url);
  1107. }
  1108. public static function getUserIDByIdOrEmail($id_or_email) {
  1109. $id = 0;
  1110. /**
  1111. * Get the user id depending on the $id_or_email, it can be the user id, email and object.
  1112. */
  1113. if (is_numeric($id_or_email)) {
  1114. $id = $id_or_email;
  1115. } else if (is_string($id_or_email)) {
  1116. $user = get_user_by('email', $id_or_email);
  1117. if ($user) {
  1118. $id = $user->ID;
  1119. }
  1120. } else if (is_object($id_or_email)) {
  1121. if (!empty($id_or_email->comment_author_email)) {
  1122. $user = get_user_by('email', $id_or_email->comment_author_email);
  1123. if ($user) {
  1124. $id = $user->ID;
  1125. }
  1126. } else if (!empty($id_or_email->user_id)) {
  1127. $id = $id_or_email->user_id;
  1128. }
  1129. }
  1130. return $id;
  1131. }
  1132. }
  1133. NextendSocialLogin::init();