rdusnr

Untitled

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