Advertisement
Guest User

Untitled

a guest
Jun 12th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.51 KB | None | 0 0
  1.  
  2. if ( ! function_exists( 'kleo_fb_intialize' ) ) {
  3.  
  4. function kleo_fb_intialize() {
  5.  
  6. /* If not our action, bail out */
  7. if ( ! isset( $_POST['action'] ) || ( isset( $_POST['action'] ) && $_POST['action'] != 'fb_intialize' ) ) {
  8. return false;
  9. }
  10.  
  11. @error_reporting( 0 ); // Don't break the JSON result
  12. header( 'Content-type: application/json' );
  13.  
  14. if ( is_user_logged_in() ) {
  15. die( wp_json_encode( array( 'error' => esc_html__( 'You are already logged in.', 'kleo' ) ) ) );
  16. }
  17.  
  18. if ( ! isset( $_REQUEST['FB_response'] ) || ! isset( $_REQUEST['FB_userdata'] ) ) {
  19. die( wp_json_encode( array( 'error' => esc_html__( 'Authentication required.', 'kleo' ) ) ) );
  20. }
  21.  
  22. $FB_response = $_REQUEST['FB_response'];
  23. $FB_userdata = $_REQUEST['FB_userdata'];
  24. $FB_userid = $FB_userdata['id'];
  25.  
  26.  
  27. if ( ! $FB_userid ) {
  28. die( wp_json_encode( array( 'error' => esc_html__( 'Please connect your facebook account.', 'kleo' ) ) ) );
  29. }
  30.  
  31. global $wpdb;
  32. //check if we already have matched our facebook account
  33. $user_ID = $wpdb->get_var( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '_fbid' AND meta_value = '$FB_userid'" );
  34.  
  35. $redirect = '';
  36. $redirect_type = 'redirect';
  37.  
  38. //if facebook is not connected
  39. if ( ! $user_ID ) {
  40. $user_email = $FB_userdata['email'];
  41. $user_ID = $wpdb->get_var( "SELECT ID FROM $wpdb->users WHERE user_email = '" . $wpdb->escape( $user_email ) . "'" );
  42.  
  43. //Register user
  44. if ( ! $user_ID ) {
  45. if ( ! get_option( 'users_can_register' ) ) {
  46. die( wp_json_encode( array( 'error' => esc_html__( 'Registration is not open at this time. Please come back later.', 'kleo' ) ) ) );
  47. }
  48. if ( sq_option( 'facebook_register', 0 ) == 0 ) {
  49. die( wp_json_encode( array( 'error' => esc_html__( 'Registration using Facebook is not currently allowed. Please use our Register page', 'kleo' ) ) ) );
  50. }
  51.  
  52. extract( $FB_userdata );
  53.  
  54. $display_name = $name;
  55.  
  56. $first_name = '';
  57. $last_name = '';
  58. $name_array = explode( ' ', $name, 2 );
  59. $first_name = $name_array[0];
  60. if ( isset( $name_array[1] ) ) {
  61. $last_name = $name_array[1];
  62. }
  63.  
  64. $user_email = $email;
  65. if ( empty( $user_email ) ) {
  66. die( wp_json_encode( array( 'error' => esc_html__( 'Please click again to login with Facebook and allow the application to use your email address', 'kleo' ) ) ) );
  67. }
  68.  
  69. if ( empty( $name ) ) {
  70. die( wp_json_encode( array(
  71. 'error' => 'empty_name',
  72. esc_html__( 'We didn\'t find your name. Please complete your facebook account before proceeding.', 'kleo' )
  73. ) ) );
  74. }
  75.  
  76. $user_login = sanitize_title_with_dashes( sanitize_user( $display_name, true ) );
  77.  
  78. if ( username_exists( $user_login ) ) {
  79. $user_login = $user_login . time();
  80. }
  81.  
  82. $user_pass = wp_generate_password( 12, false );
  83. $userdata = compact( 'user_login', 'user_email', 'user_pass', 'display_name', 'first_name', 'last_name' );
  84. $userdata = apply_filters( 'kleo_fb_register_data', $userdata );
  85.  
  86. $user_ID = wp_insert_user( $userdata );
  87. if ( is_wp_error( $user_ID ) ) {
  88. die( wp_json_encode( array( 'error' => $user_ID->get_error_message() ) ) );
  89. }
  90.  
  91. if ( sq_option( 'facebook_sent_email_login_details', '1' ) == '1' ) {
  92. //send email with password
  93. wp_new_user_notification( $user_ID, wp_unslash( $user_pass ) );
  94. }
  95. //add Facebook image
  96. update_user_meta( $user_ID, 'kleo_fb_picture', 'https://graph.facebook.com/' . $id . '/picture' );
  97.  
  98. do_action( 'fb_register_action', $user_ID );
  99. do_action( 'user_register', $user_ID );
  100.  
  101. update_user_meta( $user_ID, '_fbid', $id );
  102.  
  103. $logintype = 'register';
  104.  
  105. /* Registration logic redirect */
  106. if ( function_exists( 'bp_is_active' ) && sq_option( 'facebook_register_redirect', 'default' ) == 'default' ) {
  107. $redirect_url = bp_core_get_user_domain( $user_ID ) . 'profile/edit/group/4/?fb=registered';
  108. } elseif ( sq_option( 'facebook_register_redirect', 'default' ) == 'reload' ) {
  109. $redirect_type = 'reload';
  110. $redirect_url = home_url();
  111. } elseif ( sq_option( 'facebook_register_redirect', 'default' ) == 'custom' ) {
  112. $redirect_url = sq_option( 'facebook_register_redirect_url', '' );
  113. if ( function_exists( 'bp_is_active' ) ) {
  114. $logged_in_link = bp_core_get_user_domain( $user_ID );
  115. $redirect_url = str_replace( '##profile_link##', $logged_in_link, $redirect_url );
  116. }
  117. }
  118.  
  119. if ( ! isset( $redirect_url ) || empty( $redirect_url ) ) {
  120. $redirect_type = 'reload';
  121. $redirect_url = home_url();
  122. }
  123.  
  124. $redirect = apply_filters( 'kleo_fb_register_redirect', $redirect_url, $user_ID );
  125. } else {
  126. update_user_meta( $user_ID, '_fbid', $FB_userdata['id'] );
  127. //add Facebook image
  128. update_user_meta( $user_ID, 'kleo_fb_picture', 'https://graph.facebook.com/' . $FB_userdata['id'] . '/picture' );
  129. $logintype = 'login';
  130. }
  131. } else {
  132. $logintype = 'login';
  133. }
  134.  
  135. $user = get_user_by( 'id', $user_ID );
  136.  
  137. if ( $logintype == 'login' ) {
  138.  
  139. $redirect_to = home_url();
  140. if ( function_exists( 'bp_is_active' ) ) {
  141. $redirect_to = bp_core_get_user_domain( $user_ID );
  142. }
  143.  
  144. /* Check the configured type of redirect */
  145. if ( sq_option( 'login_redirect' ) == 'reload' ) {
  146. $redirect_type = 'reload';
  147. }
  148.  
  149. /**
  150. * Filter the login redirect URL.
  151. *
  152. * @since 3.0.0
  153. *
  154. * @param string $redirect_to The redirect destination URL.
  155. * @param string $requested_redirect_to The requested redirect destination URL passed as a parameter.
  156. * @param WP_User|WP_Error $user WP_User object if login was successful, WP_Error object otherwise.
  157. */
  158.  
  159. $redirect = apply_filters( 'login_redirect', $redirect_to, '', $user );
  160. }
  161.  
  162. wp_set_auth_cookie( $user_ID, false, false );
  163. /**
  164. * Fires after the user has successfully logged in.
  165. *
  166. * @since 1.5.0
  167. *
  168. * @param string $user_login Username.
  169. * @param WP_User $user WP_User object of the logged-in user.
  170. */
  171. do_action( 'wp_login', $user->user_login, $user );
  172.  
  173. die ( wp_json_encode( array(
  174. 'loggedin' => true,
  175. 'type' => $logintype,
  176. 'url' => $redirect,
  177. 'redirectType' => $redirect_type,
  178. 'message' => esc_html__( 'Login successful, redirecting...', 'kleo' ),
  179. ) ) );
  180.  
  181. //Update the user after creation
  182. wp_update_user( array(
  183. 'ID' => (int) $user_ID,
  184. ) );
  185.  
  186.  
  187. }
  188. }
  189.  
  190. if ( ! is_admin() ) {
  191. add_action( 'init', 'kleo_fb_intialize' );
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement