Пример #1
0
function hocwp_social_login_google_ajax_callback()
{
    $result = array('redirect_to' => '', 'logged_in' => false);
    $data = hocwp_get_method_value('data');
    $data = hocwp_json_string_to_array($data);
    $connect = hocwp_get_method_value('connect');
    if (hocwp_array_has_value($data)) {
        $verified = (bool) hocwp_get_value_by_key($data, 'verified');
        $allow_not_verified = apply_filters('hocwp_allow_social_user_signup_not_verified', true);
        if ($verified || $allow_not_verified) {
            $id = hocwp_get_value_by_key($data, 'id');
            $requested_redirect_to = hocwp_get_method_value('redirect_to');
            $redirect_to = home_url('/');
            $transient_name = hocwp_build_transient_name('hocwp_social_login_google_%s', $id);
            $user_id = get_transient($transient_name);
            $user = get_user_by('id', $user_id);
            if ($connect && is_user_logged_in()) {
                $user = wp_get_current_user();
                $user_id = $user->ID;
            }
            $find_users = get_users(array('meta_key' => 'google', 'meta_value' => $id));
            if (hocwp_array_has_value($find_users)) {
                $user = $find_users[0];
                $user_id = $user->ID;
            }
            if (false === $user_id || !hocwp_id_number_valid($user_id) || !is_a($user, 'WP_User') || $connect) {
                $avatar = hocwp_get_value_by_key($data, array('image', 'url'));
                if ($connect) {
                    update_user_meta($user_id, 'google', $id);
                    update_user_meta($user_id, 'avatar', $avatar);
                    update_user_meta($user_id, 'google_data', $data);
                    $result['redirect_to'] = get_edit_profile_url($user_id);
                    $result['logged_in'] = true;
                } else {
                    $email = hocwp_get_value_by_key($data, array('emails', 0, 'value'));
                    if (is_email($email)) {
                        $name = hocwp_get_value_by_key($data, 'displayName');
                        $first_name = hocwp_get_value_by_key($data, array('name', 'givenName'));
                        $last_name = hocwp_get_value_by_key($data, array('name', 'familyName'));
                        $password = wp_generate_password();
                        $user_id = null;
                        if (username_exists($email)) {
                            $user = get_user_by('login', $email);
                            $user_id = $user->ID;
                        } elseif (email_exists($email)) {
                            $user = get_user_by('email', $email);
                            $user_id = $user->ID;
                        }
                        $old_user = true;
                        if (!hocwp_id_number_valid($user_id)) {
                            $user_data = array('username' => $email, 'email' => $email, 'password' => $password);
                            $user_id = hocwp_add_user($user_data);
                            if (hocwp_id_number_valid($user_id)) {
                                $old_user = false;
                            }
                        }
                        if (hocwp_id_number_valid($user_id)) {
                            $user = get_user_by('id', $user_id);
                            $redirect_to = apply_filters('login_redirect', $redirect_to, $requested_redirect_to, $user);
                            if (!$old_user) {
                                update_user_meta($user_id, 'google', $id);
                                $user_data = array('ID' => $user_id, 'display_name' => $name, 'first_name' => $first_name, 'last_name' => $last_name);
                                wp_update_user($user_data);
                                update_user_meta($user_id, 'avatar', $avatar);
                                update_user_meta($user_id, 'google_data', $data);
                            }
                            hocwp_user_force_login($user_id);
                            $result['redirect_to'] = $redirect_to;
                            $result['logged_in'] = true;
                            set_transient($transient_name, $user_id, DAY_IN_SECONDS);
                        }
                    }
                }
            } else {
                update_user_meta($user_id, 'google_data', $data);
                $user = get_user_by('id', $user_id);
                $redirect_to = apply_filters('login_redirect', $redirect_to, $requested_redirect_to, $user);
                hocwp_user_force_login($user_id);
                $result['redirect_to'] = $redirect_to;
                $result['logged_in'] = true;
            }
        }
    }
    wp_send_json($result);
}
Пример #2
0
function hocwp_execute_register()
{
    $http_post = 'POST' == $_SERVER['REQUEST_METHOD'];
    $user_login = '';
    $user_email = '';
    $pwd = '';
    $pwd_again = '';
    $phone = '';
    $captcha = '';
    $error = false;
    $message = __('There was an error occurred, please try again.', 'hocwp-theme');
    $inserted = false;
    $user_id = 0;
    $registration_redirect = hocwp_get_value_by_key($_REQUEST, 'redirect_to');
    $redirect_to = apply_filters('registration_redirect', $registration_redirect);
    if (is_user_logged_in()) {
        if (empty($redirect_to)) {
            $redirect_to = home_url('/');
        }
        wp_redirect($redirect_to);
        exit;
    }
    $transient = '';
    if ($http_post) {
        $action = hocwp_get_method_value('action');
        if ('register' === $action) {
            $user_login = hocwp_get_method_value('user_login');
            $user_email = hocwp_get_method_value('user_email');
            $pwd = hocwp_get_method_value('pwd');
            $pwd_again = hocwp_get_method_value('pwd_again');
            $phone = hocwp_get_method_value('phone');
            $captcha = hocwp_get_method_value('captcha');
            $user_login = sanitize_user($user_login, true);
            $user_email = sanitize_email($user_email);
            $transient_name = hocwp_build_transient_name('hocwp_register_user_%s', $user_email);
            if (false === ($transient = get_transient($transient_name))) {
                if (empty($user_login) || empty($user_email) || empty($pwd) || empty($pwd_again) || empty($phone) || empty($captcha)) {
                    $error = true;
                    $message = __('Please enter your complete registration information.', 'hocwp-theme');
                } elseif (!is_email($user_email)) {
                    $error = true;
                    $message = __('The email address is not correct.', 'hocwp-theme');
                } elseif ($pwd !== $pwd_again) {
                    $error = true;
                    $message = __('Password is incorrect.', 'hocwp-theme');
                } elseif (username_exists($user_login)) {
                    $error = true;
                    $message = __('Account already exists.', 'hocwp-theme');
                } elseif (email_exists($user_email)) {
                    $error = true;
                    $message = __('The email address already exists.', 'hocwp-theme');
                } else {
                    if (isset($_POST['captcha'])) {
                        $capt = new HOCWP_Captcha();
                        if (!$capt->check($captcha)) {
                            $error = true;
                            $message = __('The security code is incorrect.', 'hocwp-theme');
                        }
                    }
                }
                if (!$error) {
                    $user_data = array('username' => $user_login, 'password' => $pwd, 'email' => $user_email);
                    $user = hocwp_add_user($user_data);
                    if (hocwp_id_number_valid($user)) {
                        update_user_meta($user, 'phone', $phone);
                        $inserted = true;
                        hocwp_user_force_login($user);
                        $message = __('Your account has been successfully created.', 'hocwp-theme');
                        $user_id = $user;
                        set_transient($transient_name, $user_id);
                    }
                }
                if ($inserted && !empty($redirect_to)) {
                    wp_redirect($redirect_to);
                    exit;
                }
            } else {
                if (hocwp_id_number_valid($transient)) {
                    $inserted = true;
                    $message = __('Your account has been successfully created.', 'hocwp-theme');
                }
            }
        }
    }
    $result = array('user_login' => $user_login, 'user_email' => $user_email, 'pwd' => $pwd, 'pwd_again' => $pwd_again, 'phone' => $phone, 'captcha' => $captcha, 'error' => $error, 'message' => $message, 'inserted' => $inserted, 'redirect_to' => $redirect_to, 'user_id' => $user_id, 'transient' => $transient);
    return $result;
}