Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if ( ! function_exists( 'kleo_fb_intialize' ) ) {
- function kleo_fb_intialize() {
- /* If not our action, bail out */
- if ( ! isset( $_POST['action'] ) || ( isset( $_POST['action'] ) && $_POST['action'] != 'fb_intialize' ) ) {
- return false;
- }
- @error_reporting( 0 ); // Don't break the JSON result
- header( 'Content-type: application/json' );
- if ( is_user_logged_in() ) {
- die( wp_json_encode( array( 'error' => esc_html__( 'You are already logged in.', 'kleo' ) ) ) );
- }
- if ( ! isset( $_REQUEST['FB_response'] ) || ! isset( $_REQUEST['FB_userdata'] ) ) {
- die( wp_json_encode( array( 'error' => esc_html__( 'Authentication required.', 'kleo' ) ) ) );
- }
- $FB_response = $_REQUEST['FB_response'];
- $FB_userdata = $_REQUEST['FB_userdata'];
- $FB_userid = $FB_userdata['id'];
- if ( ! $FB_userid ) {
- die( wp_json_encode( array( 'error' => esc_html__( 'Please connect your facebook account.', 'kleo' ) ) ) );
- }
- global $wpdb;
- //check if we already have matched our facebook account
- $user_ID = $wpdb->get_var( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = '_fbid' AND meta_value = '$FB_userid'" );
- $redirect = '';
- $redirect_type = 'redirect';
- //if facebook is not connected
- if ( ! $user_ID ) {
- $user_email = $FB_userdata['email'];
- $user_ID = $wpdb->get_var( "SELECT ID FROM $wpdb->users WHERE user_email = '" . $wpdb->escape( $user_email ) . "'" );
- //Register user
- if ( ! $user_ID ) {
- if ( ! get_option( 'users_can_register' ) ) {
- die( wp_json_encode( array( 'error' => esc_html__( 'Registration is not open at this time. Please come back later.', 'kleo' ) ) ) );
- }
- if ( sq_option( 'facebook_register', 0 ) == 0 ) {
- die( wp_json_encode( array( 'error' => esc_html__( 'Registration using Facebook is not currently allowed. Please use our Register page', 'kleo' ) ) ) );
- }
- extract( $FB_userdata );
- $display_name = $name;
- $first_name = '';
- $last_name = '';
- $name_array = explode( ' ', $name, 2 );
- $first_name = $name_array[0];
- if ( isset( $name_array[1] ) ) {
- $last_name = $name_array[1];
- }
- $user_email = $email;
- if ( empty( $user_email ) ) {
- 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' ) ) ) );
- }
- if ( empty( $name ) ) {
- die( wp_json_encode( array(
- 'error' => 'empty_name',
- esc_html__( 'We didn\'t find your name. Please complete your facebook account before proceeding.', 'kleo' )
- ) ) );
- }
- $user_login = sanitize_title_with_dashes( sanitize_user( $display_name, true ) );
- if ( username_exists( $user_login ) ) {
- $user_login = $user_login . time();
- }
- $user_pass = wp_generate_password( 12, false );
- $userdata = compact( 'user_login', 'user_email', 'user_pass', 'display_name', 'first_name', 'last_name' );
- $userdata = apply_filters( 'kleo_fb_register_data', $userdata );
- $user_ID = wp_insert_user( $userdata );
- if ( is_wp_error( $user_ID ) ) {
- die( wp_json_encode( array( 'error' => $user_ID->get_error_message() ) ) );
- }
- if ( sq_option( 'facebook_sent_email_login_details', '1' ) == '1' ) {
- //send email with password
- wp_new_user_notification( $user_ID, wp_unslash( $user_pass ) );
- }
- //add Facebook image
- update_user_meta( $user_ID, 'kleo_fb_picture', 'https://graph.facebook.com/' . $id . '/picture' );
- do_action( 'fb_register_action', $user_ID );
- do_action( 'user_register', $user_ID );
- update_user_meta( $user_ID, '_fbid', $id );
- $logintype = 'register';
- /* Registration logic redirect */
- if ( function_exists( 'bp_is_active' ) && sq_option( 'facebook_register_redirect', 'default' ) == 'default' ) {
- $redirect_url = bp_core_get_user_domain( $user_ID ) . 'profile/edit/group/4/?fb=registered';
- } elseif ( sq_option( 'facebook_register_redirect', 'default' ) == 'reload' ) {
- $redirect_type = 'reload';
- $redirect_url = home_url();
- } elseif ( sq_option( 'facebook_register_redirect', 'default' ) == 'custom' ) {
- $redirect_url = sq_option( 'facebook_register_redirect_url', '' );
- if ( function_exists( 'bp_is_active' ) ) {
- $logged_in_link = bp_core_get_user_domain( $user_ID );
- $redirect_url = str_replace( '##profile_link##', $logged_in_link, $redirect_url );
- }
- }
- if ( ! isset( $redirect_url ) || empty( $redirect_url ) ) {
- $redirect_type = 'reload';
- $redirect_url = home_url();
- }
- $redirect = apply_filters( 'kleo_fb_register_redirect', $redirect_url, $user_ID );
- } else {
- update_user_meta( $user_ID, '_fbid', $FB_userdata['id'] );
- //add Facebook image
- update_user_meta( $user_ID, 'kleo_fb_picture', 'https://graph.facebook.com/' . $FB_userdata['id'] . '/picture' );
- $logintype = 'login';
- }
- } else {
- $logintype = 'login';
- }
- $user = get_user_by( 'id', $user_ID );
- if ( $logintype == 'login' ) {
- $redirect_to = home_url();
- if ( function_exists( 'bp_is_active' ) ) {
- $redirect_to = bp_core_get_user_domain( $user_ID );
- }
- /* Check the configured type of redirect */
- if ( sq_option( 'login_redirect' ) == 'reload' ) {
- $redirect_type = 'reload';
- }
- /**
- * Filter the login redirect URL.
- *
- * @since 3.0.0
- *
- * @param string $redirect_to The redirect destination URL.
- * @param string $requested_redirect_to The requested redirect destination URL passed as a parameter.
- * @param WP_User|WP_Error $user WP_User object if login was successful, WP_Error object otherwise.
- */
- $redirect = apply_filters( 'login_redirect', $redirect_to, '', $user );
- }
- wp_set_auth_cookie( $user_ID, false, false );
- /**
- * Fires after the user has successfully logged in.
- *
- * @since 1.5.0
- *
- * @param string $user_login Username.
- * @param WP_User $user WP_User object of the logged-in user.
- */
- do_action( 'wp_login', $user->user_login, $user );
- die ( wp_json_encode( array(
- 'loggedin' => true,
- 'type' => $logintype,
- 'url' => $redirect,
- 'redirectType' => $redirect_type,
- 'message' => esc_html__( 'Login successful, redirecting...', 'kleo' ),
- ) ) );
- //Update the user after creation
- wp_update_user( array(
- 'ID' => (int) $user_ID,
- ) );
- }
- }
- if ( ! is_admin() ) {
- add_action( 'init', 'kleo_fb_intialize' );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement