Advertisement
Guest User

Untitled

a guest
Jul 11th, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.95 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: 'v3.0',
  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,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. /* If not our action, bail out */
  189. if (!isset($_POST['action']) || (isset($_POST['action']) && $_POST['action'] != 'fb_intialize')) {
  190. return false;
  191. }
  192.  
  193. @error_reporting(0); // Don't break the JSON result
  194. header('Content-type: application/json');
  195.  
  196. if (is_user_logged_in()) {
  197. die(wp_json_encode(array('error' => __('You are already logged in.', 'kleo_framework'))));
  198. }
  199.  
  200. if (!isset($_REQUEST['FB_response']) || !isset($_REQUEST['FB_userdata'])) {
  201. die(wp_json_encode(array('error' => __('Authentication required.', 'kleo_framework'))));
  202. }
  203.  
  204. $FB_response = $_REQUEST['FB_response'];
  205. $FB_userdata = $_REQUEST['FB_userdata'];
  206. $FB_userid = $FB_userdata['id'];
  207.  
  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. $display_name = $name;
  237.  
  238. $first_name = '';
  239. $last_name = '';
  240. $name_array = explode(' ', $name, 2);
  241. $first_name = $name_array[0];
  242. if (isset($name_array[1])) {
  243. $last_name = $name_array[1];
  244. }
  245.  
  246. $user_email = $email;
  247. if (empty($user_email)) {
  248. die(wp_json_encode(array('error' => __('Please click again to login with Facebook and allow the application to use your email address', 'kleo_framework'))));
  249. }
  250.  
  251. if (empty($name)) {
  252. die(wp_json_encode(array(
  253. 'error' => 'empty_name',
  254. __('We didn\'t find your name. Please complete your facebook account before proceeding.', 'kleo_framework')
  255. )));
  256. }
  257.  
  258. $user_login = sanitize_title_with_dashes(sanitize_user($display_name, true));
  259.  
  260. if (username_exists($user_login)) {
  261. $user_login = $user_login . time();
  262. }
  263.  
  264. $user_pass = wp_generate_password(12, false);
  265. $userdata = compact('user_login', 'user_email', 'user_pass', 'display_name', 'first_name', 'last_name');
  266. $userdata = apply_filters('kleo_fb_register_data', $userdata);
  267.  
  268. $user_ID = wp_insert_user($userdata);
  269. if (is_wp_error($user_ID)) {
  270. die(wp_json_encode(array('error' => $user_ID->get_error_message())));
  271. }
  272.  
  273. if (sq_option('facebook_sent_email_login_details', '1') == '1') {
  274. //send email with password
  275. wp_new_user_notification($user_ID,'', wp_unslash($user_pass));
  276. }
  277. //add Facebook image
  278. update_user_meta($user_ID, 'kleo_fb_picture', 'https://graph.facebook.com/' . $id . '/picture');
  279.  
  280. do_action('fb_register_action', $user_ID);
  281. do_action('user_register', $user_ID);
  282.  
  283. if (sq_option('facebook_sent_email_login_details', '1') == '1') {
  284. //send email with password
  285. wp_new_user_notification($user_ID,'', wp_unslash($user_pass));
  286. }
  287.  
  288. update_user_meta( $user_ID, '_fbid', $id );
  289.  
  290. $logintype = 'register';
  291.  
  292. /* Registration logic redirect */
  293. if (function_exists('bp_is_active') && sq_option('facebook_register_redirect', 'default') == 'default') {
  294. $redirect_url = bp_core_get_user_domain($user_ID) . 'profile/edit/group/1/?fb=registered';
  295. } elseif (sq_option('facebook_register_redirect', 'default') == 'reload') {
  296. $redirect_type = 'reload';
  297. $redirect_url = home_url();
  298. } elseif (sq_option('facebook_register_redirect', 'default') == 'custom') {
  299. $redirect_url = sq_option('facebook_register_redirect_url', '');
  300. if (function_exists('bp_is_active')) {
  301. $logged_in_link = bp_core_get_user_domain($user_ID);
  302. $redirect_url = str_replace('##profile_link##', $logged_in_link, $redirect_url);
  303. }
  304. }
  305.  
  306. if (!isset($redirect_url) || empty($redirect_url)) {
  307. $redirect_type = 'reload';
  308. $redirect_url = home_url();
  309. }
  310.  
  311. $redirect = apply_filters( 'kleo_fb_register_redirect', $redirect_url, $user_ID );
  312. } else {
  313. update_user_meta( $user_ID, '_fbid', $FB_userdata['id'] );
  314. //add Facebook image
  315. update_user_meta( $user_ID, 'kleo_fb_picture', 'https://graph.facebook.com/' . $FB_userdata['id'] . '/picture' );
  316. $logintype = 'login';
  317. }
  318. } else {
  319. $logintype = 'login';
  320. }
  321.  
  322. $user = get_user_by('id', $user_ID);
  323.  
  324. if ($logintype == 'login') {
  325.  
  326. $redirect_to = home_url();
  327. if (function_exists('bp_is_active')) {
  328. $redirect_to = bp_core_get_user_domain($user_ID);
  329. }
  330.  
  331. /* Check the configured type of redirect */
  332. if (sq_option('login_redirect') == 'reload') {
  333. $redirect_type = 'reload';
  334. }
  335.  
  336. /**
  337. * Filter the login redirect URL.
  338. *
  339. * @since 3.0.0
  340. *
  341. * @param string $redirect_to The redirect destination URL.
  342. * @param string $requested_redirect_to The requested redirect destination URL passed as a parameter.
  343. * @param WP_User|WP_Error $user WP_User object if login was successful, WP_Error object otherwise.
  344. */
  345.  
  346. $redirect = apply_filters('login_redirect', $redirect_to, '', $user);
  347. }
  348.  
  349. wp_set_auth_cookie( $user_ID, false, false );
  350. /**
  351. * Fires after the user has successfully logged in.
  352. *
  353. * @since 1.5.0
  354. *
  355. * @param string $user_login Username.
  356. * @param WP_User $user WP_User object of the logged-in user.
  357. */
  358. do_action( 'wp_login', $user->user_login, $user );
  359.  
  360. die (wp_json_encode( array(
  361. 'loggedin' => true,
  362. 'type' => $logintype,
  363. 'url' => $redirect,
  364. 'redirectType' => $redirect_type,
  365. 'message' => __('Login successful, redirecting...', 'kleo_framework'),
  366. ) ) );
  367. }
  368. }
  369.  
  370. if ( ! is_admin() ) {
  371. add_action( 'init', 'kleo_fb_intialize' );
  372. }
  373.  
  374.  
  375. //If registered via Facebook -> show message
  376. add_action( 'template_notices', 'kleo_fb_register_message' );
  377. if ( ! function_exists( 'kleo_fb_register_message' ) ) {
  378. function kleo_fb_register_message() {
  379. if ( isset( $_GET['fb'] ) && $_GET['fb'] == 'registered' ) {
  380. echo '<div class="clearfix"></div><div class="alert alert-success" id="message" data-alert>';
  381. echo __( 'Thank you for registering. Please make sure to complete your profile fields below.', 'kleo_framework' );
  382. echo '</div>';
  383. }
  384. }
  385. }
  386.  
  387.  
  388. //display Facebook avatar
  389. if ( sq_option( 'facebook_avatar', 1 ) == 1 ) {
  390. //show Facebook avatar in WP
  391. add_filter( 'get_avatar', 'kleo_fb_show_avatar', 5, 5 );
  392. //show Facebook avatar in Buddypress
  393. add_filter( 'bp_core_fetch_avatar', 'kleo_fb_bp_show_avatar', 3, 5 );
  394. //show Facebook avatar in Buddypress - url version
  395. add_filter( 'bp_core_fetch_avatar_url', 'kleo_fb_bp_show_avatar_url', 3, 2 );
  396. }
  397.  
  398. if ( ! function_exists( 'kleo_fb_show_avatar' ) ) {
  399.  
  400. function kleo_fb_show_avatar($avatar = '', $id_or_email, $size = 96, $default = '', $alt = false)
  401. {
  402. $id = 0;
  403. if (is_numeric($id_or_email)) {
  404. $id = $id_or_email;
  405. } elseif (is_string($id_or_email)) {
  406. $u = get_user_by('email', $id_or_email);
  407. if ($u) {
  408. $id = $u->id;
  409. }
  410. } elseif (is_object($id_or_email)) {
  411. $id = $id_or_email->user_id;
  412. }
  413.  
  414. if ($id == 0) {
  415. return $avatar;
  416. }
  417.  
  418. //if we have an avatar uploaded and is not Gravatar return it
  419. if (strpos($avatar, home_url()) !== false && strpos($avatar, 'gravatar') === false) {
  420. return $avatar;
  421. }
  422.  
  423. //if we don't have a Facebook photo
  424. $pic = get_user_meta($id, 'kleo_fb_picture', true);
  425. if (!$pic || $pic == '') {
  426. return $avatar;
  427. }
  428.  
  429. $avatar = preg_replace('/src=("|\').*?("|\')/i', 'src=\'' . $pic . apply_filters('fb_show_avatar_params', '?width=580&amp;height=580') . '\'', $avatar);
  430.  
  431. return $avatar;
  432. }
  433. }
  434.  
  435. if ( ! function_exists( 'kleo_fb_bp_show_avatar' ) ) {
  436. function kleo_fb_bp_show_avatar($avatar = '', $params, $id)
  437. {
  438. if (!is_numeric($id) || strpos($avatar, 'gravatar') === false) {
  439. return $avatar;
  440. }
  441.  
  442. //if we have an avatar uploaded and is not Gravatar return it
  443. if (strpos($avatar, home_url()) !== false && strpos($avatar, 'gravatar') === false) {
  444. return $avatar;
  445. }
  446.  
  447. $pic = get_user_meta($id, 'kleo_fb_picture', true);
  448. if (!$pic || $pic == '') {
  449. return $avatar;
  450. }
  451. $avatar = preg_replace('/src=("|\').*?("|\')/i', 'src=\'' . $pic . apply_filters('fb_show_avatar_params', '?width=580&amp;height=580') . '\'', $avatar);
  452.  
  453. return $avatar;
  454. }
  455. }
  456. if ( ! function_exists( 'kleo_fb_bp_show_avatar_url' ) ) {
  457. function kleo_fb_bp_show_avatar_url($gravatar, $params)
  458. {
  459.  
  460. //if we have an avatar uploaded and is not Gravatar return it
  461. if (strpos($gravatar, home_url()) !== false && strpos($gravatar, 'gravatar') === false) {
  462. return $gravatar;
  463. }
  464.  
  465. $pic = get_user_meta($params['item_id'], 'kleo_fb_picture', true);
  466. if (!$pic || $pic == '') {
  467. return $gravatar;
  468. }
  469.  
  470. return $pic . apply_filters('fb_show_avatar_params', '?width=580&amp;height=580');
  471. }
  472. }
  473.  
  474.  
  475. /* Add a new activity stream when registering with Facebook */
  476. if ( ! function_exists( 'sq_fb_register_activity' ) ) {
  477. /**
  478. * @param int $user_id
  479. *
  480. * @return void
  481. */
  482. function sq_fb_register_activity( $user_id ) {
  483.  
  484. if ( ! function_exists( 'bp_activity_add' ) ) {
  485. return;
  486. }
  487.  
  488. $userlink = bp_core_get_userlink( $user_id );
  489. bp_activity_add( array(
  490. 'user_id' => $user_id,
  491. 'action' => apply_filters( 'xprofile_fb_register_action', sprintf( __( '%s became a registered member', 'buddypress' ), $userlink ), $user_id ),
  492. 'component' => 'xprofile',
  493. 'type' => 'new_member',
  494. ) );
  495. }
  496. }
  497. add_action( 'fb_register_action', 'sq_fb_register_activity' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement