Guest User

Untitled

a guest
Apr 27th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.22 KB | None | 0 0
  1. <?php
  2. if ( ! function_exists( 'kleo_fb_head' ) ) {
  3. /**
  4. * @return bool|string
  5. */
  6. function kleo_fb_head() {
  7.  
  8. if ( is_user_logged_in() ) {
  9. return false;
  10. }
  11.  
  12. ?>
  13. <div id="fb-root"></div>
  14. <?php
  15. }
  16. }
  17. if ( ! function_exists( 'kleo_fb_footer' ) ) {
  18.  
  19. function kleo_fb_footer()
  20. {
  21.  
  22. if (is_user_logged_in()) {
  23. return false;
  24. }
  25.  
  26. ?>
  27. <script>
  28. // Additional JS functions here
  29. window.fbAsyncInit = function () {
  30. FB.init({
  31. appId: '<?php echo sq_option('fb_app_id'); ?>', // App ID
  32. version: 'v2.12',
  33. status: true, // check login status
  34. cookie: true, // enable cookies to allow the server to access the session
  35. xfbml: true // parse XFBML
  36. //oauth: true
  37. });
  38.  
  39. // Additional init code here
  40. jQuery('body').trigger('sq_fb.init');
  41.  
  42. };
  43.  
  44. // Load the SDK asynchronously
  45. (function (d, s, id) {
  46. var js, fjs = d.getElementsByTagName(s)[0];
  47. if (d.getElementById(id)) return;
  48. js = d.createElement(s);
  49. js.id = id;
  50. js.src = "//connect.facebook.net/<?php echo apply_filters('kleo_facebook_js_locale', 'en_US'); ?>/sdk.js";
  51. fjs.parentNode.insertBefore(js, fjs);
  52. }(document, 'script', 'facebook-jssdk'));
  53.  
  54. </script>
  55. <script type="text/javascript">
  56. var fbAjaxUrl = '<?php echo site_url('wp-login.php', 'login_post'); ?>';
  57.  
  58. jQuery(document).ready(function () {
  59.  
  60. jQuery('.kleo-facebook-connect').on('click', function () {
  61.  
  62. // fix iOS Chrome
  63. if (navigator.userAgent.match('CriOS') || navigator.userAgent.match(/Android/i)) {
  64. window.open('https://www.facebook.com/dialog/oauth?client_id=<?php echo sq_option('fb_app_id'); ?>&redirect_uri=' + document.location.href + '&scope=email&response_type=token', '', null);
  65. } else {
  66. FB.login(function (FB_response) {
  67. if (FB_response.authResponse) {
  68. fb_intialize(FB_response, '');
  69. }
  70. },
  71. {
  72. scope: 'email',
  73. auth_type: 'rerequest',
  74. return_scopes: true
  75. });
  76. }
  77. });
  78.  
  79. //if (navigator.userAgent.match('CriOS') || navigator.userAgent.match(/Android/i)) {
  80. jQuery("body").on("sq_fb.init", function () {
  81. var accToken = jQuery.getUrlVar('#access_token');
  82. if (accToken) {
  83. var fbArr = {scopes: "email"};
  84. fb_intialize(fbArr, accToken);
  85. }
  86. });
  87. //}
  88.  
  89. });
  90.  
  91. function fb_intialize(FB_response, token) {
  92. FB.api('/me', 'GET', {
  93. fields: 'id,email,verified,name',
  94. access_token: token
  95. },
  96. function (FB_userdata) {
  97. jQuery.ajax({
  98. type: 'POST',
  99. url: fbAjaxUrl,
  100. data: {"action": "fb_intialize", "FB_userdata": FB_userdata, "FB_response": FB_response},
  101. success: function (user) {
  102. if (user.error) {
  103. alert(user.error);
  104. }
  105. else if (user.loggedin) {
  106. jQuery('#kleo-login-result').html(user.message);
  107.  
  108. if (window.location.href.indexOf("wp-login.php") > -1) {
  109. window.location = user.url;
  110. } else if (user.redirectType == 'reload') {
  111. window.location.reload();
  112. } else {
  113. window.location = user.url;
  114. }
  115.  
  116. }
  117. }
  118. });
  119. }
  120. );
  121. }
  122.  
  123. jQuery.extend({
  124. getUrlVars: function () {
  125. var vars = [], hash;
  126. var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
  127. for (var i = 0; i < hashes.length; i++) {
  128. hash = hashes[i].split('=');
  129. vars.push(hash[0]);
  130. vars[hash[0]] = hash[1];
  131. }
  132. return vars;
  133. },
  134. getUrlVar: function (name) {
  135. return jQuery.getUrlVars()[name];
  136. }
  137. });
  138. </script>
  139. <?php
  140. }
  141. }
  142.  
  143. if ( ! function_exists( 'kleo_fb_loginform_script' ) ) {
  144.  
  145. function kleo_fb_loginform_script()
  146. {
  147. //Enqueue jQuery
  148. wp_enqueue_script('jquery');
  149.  
  150. //Output CSS
  151. echo '<style type="text/css" media="screen">
  152. .hr-title, .gap-30, .gap-10 {display: none;}
  153. .kleo-facebook-connect.btn.btn-default {
  154. background-color: #3b5997;
  155. border-color: #2b4780;
  156. color: #fff;
  157. border-radius: 2px;
  158. font-size: 13px;
  159. font-weight: normal;
  160. margin: 3px 0;
  161. min-width: 80px;
  162. transition: all 0.4s ease-in-out 0s;
  163. cursor: pointer;
  164. display: inline-block;
  165. line-height: 1.42857;
  166. padding: 6px 12px;
  167. text-align: center;
  168. text-decoration: none;
  169. vertical-align: middle;
  170. white-space: nowrap;
  171. }
  172. </style>';
  173. }
  174. }
  175.  
  176. if ( sq_option( 'facebook_login', 0 ) == 1 ) {
  177. add_action( 'kleo_after_body', 'kleo_fb_head' );
  178. add_action( 'login_head', 'kleo_fb_head' );
  179. add_action( 'login_head', 'kleo_fb_loginform_script' );
  180. add_action( 'wp_footer', 'kleo_fb_footer', 99 );
  181. add_action( 'login_footer', 'kleo_fb_footer', 99 );
  182. }
  183.  
  184. if ( ! function_exists( 'kleo_fb_intialize' ) ) {
  185.  
  186. function kleo_fb_intialize()
  187. {
  188.  
  189. /* If not our action, bail out */
  190. if (!isset($_POST['action']) || (isset($_POST['action']) && $_POST['action'] != 'fb_intialize')) {
  191. return false;
  192. }
  193.  
  194. @error_reporting(0); // Don't break the JSON result
  195. header('Content-type: application/json');
  196.  
  197. if (is_user_logged_in()) {
  198. die(wp_json_encode(array('error' => __('You are already logged in.', 'kleo_framework'))));
  199. }
  200.  
  201. if (!isset($_REQUEST['FB_response']) || !isset($_REQUEST['FB_userdata'])) {
  202. die(wp_json_encode(array('error' => __('Authentication required.', 'kleo_framework'))));
  203. }
  204.  
  205. $FB_response = $_REQUEST['FB_response'];
  206. $FB_userdata = $_REQUEST['FB_userdata'];
  207. $FB_userid = $FB_userdata['id'];
  208.  
  209. if (!$FB_userid) {
  210. die(wp_json_encode(array('error' => __('Please connect your facebook account.', 'kleo_framework'))));
  211. }
  212.  
  213. global $wpdb;
  214. //check if we already have matched our facebook account
  215. $user_ID = $wpdb->get_var("SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '_fbid' AND meta_value = '$FB_userid'");
  216.  
  217. $redirect = '';
  218. $redirect_type = 'redirect';
  219.  
  220. //if facebook is not connected
  221. if (!$user_ID) {
  222. $user_email = $FB_userdata['email'];
  223. $user_ID = $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_email = '" . $wpdb->escape($user_email) . "'");
  224.  
  225. //Register user
  226. if (!$user_ID) {
  227. if (!get_option('users_can_register')) {
  228. die(wp_json_encode(array('error' => __('Registration is not open at this time. Please come back later.', 'kleo_framework'))));
  229. }
  230. if (sq_option('facebook_register', 0) == 0) {
  231. die(wp_json_encode(array('error' => __('Registration using Facebook is not currently allowed. Please use our Register page', 'kleo_framework'))));
  232. }
  233.  
  234. extract($FB_userdata);
  235.  
  236. $name = strip_tags( $name );
  237.  
  238. // Kill Chinese characters
  239. $name = preg_replace( '|[^a-z0-9 _.\-@一-龥]|ui', '', $name );
  240.  
  241. // Consolidate contiguous whitespace
  242. $name = preg_replace( '|\s+|', ' ', $name );
  243.  
  244. $display_name = $name;
  245.  
  246. $first_name = '';
  247. $last_name = '';
  248. $name_array = explode(' ', $name, 2);
  249. $first_name = $name_array[0];
  250. if (isset($name_array[1])) {
  251. $last_name = $name_array[1];
  252. }
  253.  
  254.  
  255. if (empty($verified) || !$verified) {
  256. die(wp_json_encode(array('error' => __('Your facebook account is not verified. You have to verify your account before proceed login or registering on this site.', 'kleo_framework'))));
  257. }
  258.  
  259. $user_email = $email;
  260. if (empty($user_email)) {
  261. die(wp_json_encode(array('error' => __('Please click again to login with Facebook and allow the application to use your email address', 'kleo_framework'))));
  262. }
  263.  
  264. if (empty($name)) {
  265. die(wp_json_encode(array(
  266. 'error' => 'empty_name',
  267. __('We didn\'t find your name. Please complete your facebook account before proceeding.', 'kleo_framework')
  268. )));
  269. }
  270.  
  271. $user_login = sanitize_title_with_dashes(sanitize_user($display_name, true));
  272.  
  273. if (username_exists($user_login)) {
  274. $user_login = $user_login . time();
  275. }
  276.  
  277. $user_pass = wp_generate_password(12, false);
  278. $userdata = compact('user_login', 'user_email', 'user_pass', 'display_name', 'first_name', 'last_name');
  279. $userdata = apply_filters('kleo_fb_register_data', $userdata);
  280.  
  281. $user_ID = wp_insert_user($userdata);
  282. if (is_wp_error($user_ID)) {
  283. die(wp_json_encode(array('error' => $user_ID->get_error_message())));
  284. }
  285.  
  286. if (sq_option('facebook_sent_email_login_details', '1') == '1') {
  287. //send email with password
  288. wp_new_user_notification($user_ID, wp_unslash($user_pass));
  289. }
  290. //add Facebook image
  291. update_user_meta($user_ID, 'kleo_fb_picture', 'https://graph.facebook.com/' . $id . '/picture');
  292.  
  293. do_action('fb_register_action', $user_ID);
  294. do_action('user_register', $user_ID);
  295.  
  296. update_user_meta($user_ID, '_fbid', $id);
  297.  
  298. $logintype = 'register';
  299.  
  300. /* Registration logic redirect */
  301. if (function_exists('bp_is_active') && sq_option('facebook_register_redirect', 'default') == 'default') {
  302. $redirect_url = bp_core_get_user_domain($user_ID) . 'profile/edit/group/1/?fb=registered';
  303. } elseif (sq_option('facebook_register_redirect', 'default') == 'reload') {
  304. $redirect_type = 'reload';
  305. $redirect_url = home_url();
  306. } elseif (sq_option('facebook_register_redirect', 'default') == 'custom') {
  307. $redirect_url = sq_option('facebook_register_redirect_url', '');
  308. if (function_exists('bp_is_active')) {
  309. $logged_in_link = bp_core_get_user_domain($user_ID);
  310. $redirect_url = str_replace('##profile_link##', $logged_in_link, $redirect_url);
  311. }
  312. }
  313.  
  314. if (!isset($redirect_url) || empty($redirect_url)) {
  315. $redirect_type = 'reload';
  316. $redirect_url = home_url();
  317. }
  318.  
  319. $redirect = apply_filters('kleo_fb_register_redirect', $redirect_url, $user_ID);
  320. } else {
  321. update_user_meta($user_ID, '_fbid', $FB_userdata['id']);
  322. //add Facebook image
  323. update_user_meta($user_ID, 'kleo_fb_picture', 'https://graph.facebook.com/' . $FB_userdata['id'] . '/picture');
  324. $logintype = 'login';
  325. }
  326. } else {
  327. $logintype = 'login';
  328. }
  329.  
  330. $user = get_user_by('id', $user_ID);
  331.  
  332. if ($logintype == 'login') {
  333.  
  334. $redirect_to = home_url();
  335. if (function_exists('bp_is_active')) {
  336. $redirect_to = bp_core_get_user_domain($user_ID);
  337. }
  338.  
  339. /* Check the configured type of redirect */
  340. if (sq_option('login_redirect') == 'reload') {
  341. $redirect_type = 'reload';
  342. }
  343.  
  344. /**
  345. * Filter the login redirect URL.
  346. *
  347. * @since 3.0.0
  348. *
  349. * @param string $redirect_to The redirect destination URL.
  350. * @param string $requested_redirect_to The requested redirect destination URL passed as a parameter.
  351. * @param WP_User|WP_Error $user WP_User object if login was successful, WP_Error object otherwise.
  352. */
  353.  
  354. $redirect = apply_filters('login_redirect', $redirect_to, '', $user);
  355. }
  356.  
  357. wp_set_auth_cookie($user_ID, false, false);
  358. /**
  359. * Fires after the user has successfully logged in.
  360. *
  361. * @since 1.5.0
  362. *
  363. * @param string $user_login Username.
  364. * @param WP_User $user WP_User object of the logged-in user.
  365. */
  366. do_action('wp_login', $user->user_login, $user);
  367.  
  368. die(wp_json_encode(array(
  369. 'loggedin' => true,
  370. 'type' => $logintype,
  371. 'url' => $redirect,
  372. 'redirectType' => $redirect_type,
  373. 'message' => __('Login successful, redirecting...', 'kleo_framework'),
  374. )));
  375. }
  376. }
  377.  
  378. if ( ! is_admin() ) {
  379. add_action( 'init', 'kleo_fb_intialize' );
  380. }
  381.  
  382.  
  383. //If registered via Facebook -> show message
  384. add_action( 'template_notices', 'kleo_fb_register_message' );
  385. if ( ! function_exists( 'kleo_fb_register_message' ) ) {
  386. function kleo_fb_register_message() {
  387. if ( isset( $_GET['fb'] ) && $_GET['fb'] == 'registered' ) {
  388. echo '<div class="clearfix"></div><div class="alert alert-success" id="message" data-alert>';
  389. echo __( 'Thank you for registering. Please make sure to complete your profile fields below.', 'kleo_framework' );
  390. echo '</div>';
  391. }
  392. }
  393. }
  394.  
  395.  
  396. //display Facebook avatar
  397. if ( sq_option( 'facebook_avatar', 1 ) == 1 ) {
  398. //show Facebook avatar in WP
  399. add_filter( 'get_avatar', 'kleo_fb_show_avatar', 5, 5 );
  400. //show Facebook avatar in Buddypress
  401. add_filter( 'bp_core_fetch_avatar', 'kleo_fb_bp_show_avatar', 3, 5 );
  402. //show Facebook avatar in Buddypress - url version
  403. add_filter( 'bp_core_fetch_avatar_url', 'kleo_fb_bp_show_avatar_url', 3, 2 );
  404. }
  405.  
  406. if ( ! function_exists( 'kleo_fb_show_avatar' ) ) {
  407.  
  408. function kleo_fb_show_avatar($avatar = '', $id_or_email, $size = 96, $default = '', $alt = false)
  409. {
  410. $id = 0;
  411. if (is_numeric($id_or_email)) {
  412. $id = $id_or_email;
  413. } elseif (is_string($id_or_email)) {
  414. $u = get_user_by('email', $id_or_email);
  415. if ($u) {
  416. $id = $u->id;
  417. }
  418. } elseif (is_object($id_or_email)) {
  419. $id = $id_or_email->user_id;
  420. }
  421.  
  422. if ($id == 0) {
  423. return $avatar;
  424. }
  425.  
  426. //if we have an avatar uploaded and is not Gravatar return it
  427. if (strpos($avatar, home_url()) !== false && strpos($avatar, 'gravatar') === false) {
  428. return $avatar;
  429. }
  430.  
  431. //if we don't have a Facebook photo
  432. $pic = get_user_meta($id, 'kleo_fb_picture', true);
  433. if (!$pic || $pic == '') {
  434. return $avatar;
  435. }
  436.  
  437. $avatar = preg_replace('/src=("|\').*?("|\')/i', 'src=\'' . $pic . apply_filters('fb_show_avatar_params', '?width=580&amp;height=580') . '\'', $avatar);
  438.  
  439. return $avatar;
  440. }
  441. }
  442.  
  443. if ( ! function_exists( 'kleo_fb_bp_show_avatar' ) ) {
  444. function kleo_fb_bp_show_avatar($avatar = '', $params, $id)
  445. {
  446. if (!is_numeric($id) || strpos($avatar, 'gravatar') === false) {
  447. return $avatar;
  448. }
  449.  
  450. //if we have an avatar uploaded and is not Gravatar return it
  451. if (strpos($avatar, home_url()) !== false && strpos($avatar, 'gravatar') === false) {
  452. return $avatar;
  453. }
  454.  
  455. $pic = get_user_meta($id, 'kleo_fb_picture', true);
  456. if (!$pic || $pic == '') {
  457. return $avatar;
  458. }
  459. $avatar = preg_replace('/src=("|\').*?("|\')/i', 'src=\'' . $pic . apply_filters('fb_show_avatar_params', '?width=580&amp;height=580') . '\'', $avatar);
  460.  
  461. return $avatar;
  462. }
  463. }
  464. if ( ! function_exists( 'kleo_fb_bp_show_avatar_url' ) ) {
  465. function kleo_fb_bp_show_avatar_url($gravatar, $params)
  466. {
  467.  
  468. //if we have an avatar uploaded and is not Gravatar return it
  469. if (strpos($gravatar, home_url()) !== false && strpos($gravatar, 'gravatar') === false) {
  470. return $gravatar;
  471. }
  472.  
  473. $pic = get_user_meta($params['item_id'], 'kleo_fb_picture', true);
  474. if (!$pic || $pic == '') {
  475. return $gravatar;
  476. }
  477.  
  478. return $pic . apply_filters('fb_show_avatar_params', '?width=580&amp;height=580');
  479. }
  480. }
  481.  
  482.  
  483. /* Add a new activity stream when registering with Facebook */
  484. if ( ! function_exists( 'sq_fb_register_activity' ) ) {
  485. /**
  486. * @param int $user_id
  487. *
  488. * @return void
  489. */
  490. function sq_fb_register_activity( $user_id ) {
  491.  
  492. if ( ! function_exists( 'bp_activity_add' ) ) {
  493. return;
  494. }
  495.  
  496. $userlink = bp_core_get_userlink( $user_id );
  497. bp_activity_add( array(
  498. 'user_id' => $user_id,
  499. 'action' => apply_filters( 'xprofile_fb_register_action', sprintf( __( '%s became a registered member', 'buddypress' ), $userlink ), $user_id ),
  500. 'component' => 'xprofile',
  501. 'type' => 'new_member',
  502. ) );
  503. }
  504. }
  505. add_action( 'fb_register_action', 'sq_fb_register_activity' );
Add Comment
Please, Sign In to add comment