Advertisement
rdusnr

Untitled

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