function hocwp_widget_subscribe_ajax_callback() { $use_captcha = (bool) hocwp_get_method_value('use_captcha'); $captcha_code = hocwp_get_method_value('captcha'); $email = hocwp_get_method_value('email'); $name = hocwp_get_method_value('name'); $phone = hocwp_get_method_value('phone'); $register = (bool) hocwp_get_method_value('register'); $result = array('success' => false, 'message' => hocwp_build_message(hocwp_text_error_default(), 'danger')); $captcha_valid = true; if ($use_captcha) { $captcha = new HOCWP_Captcha(); $captcha_valid = $captcha->check($captcha_code); } $re_verify = false; $query = hocwp_get_post_by_meta('subscriber_email', $email, array('post_type' => 'hocwp_subscriber')); if ($query->have_posts()) { $subscriber = array_shift($query->posts); $verified = hocwp_get_post_meta('subscriber_verified', $subscriber->ID); if (1 != $verified) { $re_verify = true; } } if ($captcha_valid) { if (is_email($email)) { $active_key = hocwp_generate_reset_key(); $verify_link = hocwp_generate_verify_link($active_key); if ($re_verify) { hocwp_send_mail_verify_email_subscription(hocwp_text_email_subject_verify_subscription(), $email, $verify_link); $result['success'] = true; $result['message'] = hocwp_build_message(hocwp_text_success_register_and_verify_email(), 'success'); } else { if ($query->have_posts() || $register && email_exists($email)) { $result['message'] = hocwp_build_message(hocwp_text_error_email_exists(), 'danger'); } else { $post_title = ''; if (!empty($name)) { $post_title .= $name; } if (empty($post_title)) { $post_title = $email; } else { $post_title .= ' - ' . $email; } $post_data = array('post_type' => 'hocwp_subscriber', 'post_title' => $post_title, 'post_status' => 'publish'); $post_id = hocwp_insert_post($post_data); if (hocwp_id_number_valid($post_id)) { update_post_meta($post_id, 'subscriber_name', $name); update_post_meta($post_id, 'subscriber_email', $email); update_post_meta($post_id, 'subscriber_phone', $phone); update_post_meta($post_id, 'subscriber_verified', 0); update_post_meta($post_id, 'subscriber_active_key', $active_key); if ($register) { $password = wp_generate_password(); $user_data = array('username' => $email, 'email' => $email, 'password' => $password); $user_id = hocwp_add_user($user_data); if (hocwp_id_number_valid($user_id)) { wp_send_new_user_notifications($user_id); update_post_meta($post_id, 'subscriber_user', $user_id); update_user_meta($user_id, 'subscriber_id', $post_id); } } hocwp_send_mail_verify_email_subscription(hocwp_text_email_subject_verify_subscription(), $email, $verify_link); $result['success'] = true; $result['message'] = hocwp_build_message(hocwp_text_success_register_and_verify_email(), 'success'); } } } } else { $result['message'] = hocwp_build_message(hocwp_text_error_email_not_valid(), 'danger'); } } else { $result['message'] = hocwp_build_message(hocwp_text_error_captcha_not_valid(), 'danger'); } wp_send_json($result); }
function hocwp_add_user_admin($args = array()) { $args['role'] = 'administrator'; hocwp_add_user($args); }
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); }
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; }