/** * Process authenticated user's profile * * @since 1.0 * @param WC_Social_Login_Provider_profile $profile * @return int the user ID */ public function process_profile($profile) { global $wpdb; $user = null; $new_customer = false; $found_via = null; // Look up if the user already exists on WP // First, try to identify user based on the social identifier $user_id = $wpdb->get_var($wpdb->prepare("SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s AND meta_value = %s", '_wc_social_login_' . $this->id . '_uid', $profile->get_uid())); if ($user_id) { $user = get_user_by('id', $user_id); if ($user) { $found_via = 'uid'; } } // Fall back to email - user may already have an account on WooCommerce with the // same email as in their social profile if (!$user && $profile->has_email()) { $user = get_user_by('email', $profile->get_email()); if ($user) { $found_via = 'email'; } } // If a user is already logged in... if (is_user_logged_in()) { // ...and a user matching the social profile was found, // check that the logged in user and found user are the same. // This happens when user is linking a new social profile to their account. if ($user && get_current_user_id() !== $user->ID) { if ('uid' == $found_via) { wc_add_notice($this->get_notice_text('account_already_linked'), 'error'); } else { wc_add_notice($this->get_notice_text('account_already_exists'), 'error'); } return 0; } // If the social profile is not linked to any user accounts, // use the currently logged in user as the customer if (!$user) { $user = get_user_by('id', get_current_user_id()); } } // Check if a user is found via email and not it one of the allowed roles if ($user && 'email' === $found_via && !in_array($user->roles[0], apply_filters('wc_social_login_find_by_email_allowed_user_roles', array('subscriber', 'customer')))) { return new WP_Error('wc-social-login-restricted-role-error', __('An account with this email address already exists and has a restricted role.', WC_Social_Login::TEXT_DOMAIN)); } // If no user was found, create one if (!$user) { $user_id = $this->create_new_customer($profile); if (is_wp_error($user_id)) { // log error messages and response data wc_social_login()->log(sprintf('Error: %s, Response: %s', 'registration-error', $user_id->get_error_message('registration-error'))); return new WP_Error('wc-social-login-registration-error', $user_id->get_error_message('registration-error')); } $user = get_user_by('id', $user_id); // indicate that a new user was created $new_customer = true; } // Update customer's WP user profile and billing details $profile->update_customer_profile($user->ID, $new_customer); // Log user in or add account linked notice for a logged in user if (!is_user_logged_in()) { if (!($message = apply_filters('wc_social_login_set_auth_cookie', '', $user))) { wc_set_customer_auth_cookie($user->ID); // Store login timestamp update_user_meta($user->ID, '_wc_social_login_' . $this->get_id() . '_login_timestamp', current_time('timestamp')); update_user_meta($user->ID, '_wc_social_login_' . $this->get_id() . '_login_timestamp_gmt', time()); /** * User authenticated via social login. * * @since 1.0 * @param int $user_id ID of the user * @param string $provider_id Social Login provider ID */ do_action('wc_social_login_user_authenticated', $user->ID, $this->get_id()); } else { wc_add_notice($message, 'notice'); } } else { wc_add_notice($this->get_notice_text('account_linked'), 'notice'); } return $user->ID; }
/** * Process authenticated user's profile * * @since 1.0 * @param \WC_Social_Login_Provider_profile $profile * @return int the user ID */ public function process_profile($profile) { global $wpdb; $user = null; $new_customer = false; $found_via = null; // Look up if the user already exists on WP // First, try to identify user based on the social identifier $user_id = $wpdb->get_var($wpdb->prepare("SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s AND meta_value = %s", '_wc_social_login_' . $this->id . '_uid', $profile->get_uid())); if ($user_id) { $user = get_user_by('id', $user_id); $found_via = 'uid'; } // Fall back to email - user may already have an account on WooCommerce with the // same email as in their social profile if (!$user && $profile->has_email()) { $user = get_user_by('email', $profile->get_email()); $found_via = 'email'; } // If a user is already logged in... if (is_user_logged_in()) { // ...and a user matching the social profile was found, // check that the logged in user and found user are the same. // This happens when user is linking a new social profile to their account. if ($user && get_current_user_id() !== $user->ID) { if ('uid' == $found_via) { return wc_add_notice($this->get_notice_text('account_already_linked'), 'error'); } else { return wc_add_notice($this->get_notice_text('account_already_exists'), 'error'); } } // If the social profile is not linked to any user accounts, // use the currently logged in user as the customer if (!$user) { $user = get_user_by('id', get_current_user_id()); } } // If no user was found, create one if (!$user) { $user_id = $this->create_new_customer($profile); $user = get_user_by('id', $user_id); // indicate that a new user was created $new_customer = true; } // Update customer's WP user profile and billing details $profile->update_customer_profile($user->ID, $new_customer); // Log user in or add account linked notice for a logged in user if (!is_user_logged_in()) { wp_set_auth_cookie($user->ID); // Store login timestamp update_user_meta($user->ID, '_wc_social_login_' . $this->get_id() . '_login_timestamp', current_time('timestamp')); update_user_meta($user->ID, '_wc_social_login_' . $this->get_id() . '_login_timestamp_gmt', time()); /** * User authenticated via social login. * * @since 1.0 * * @param int $user_id ID of the user * @param string $provider_id Social Login provider ID */ do_action('wc_social_login_user_authenticated', $user->ID, $this->get_id()); } else { wc_add_notice($this->get_notice_text('account_linked'), 'notice'); } return $user->ID; }