Advertisement
Guest User

Untitled

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