Advertisement
Guest User

Untitled

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