/**
 * Register user account.
 *
 * This function is hooked onto wpas_do_register so that the registration process can be triggered
 * when the registration form is submitted.
 *
 * @param array $data User data
 *
 * @since  1.0.0
 * @return void
 */
function wpas_register_account($data)
{
    // Get the redirect URL
    $redirect_to = home_url();
    if (isset($data['redirect_to'])) {
        $redirect_to = wp_sanitize_redirect($data['redirect_to']);
        // If a redirect URL is specified we use it
    } else {
        global $post;
        // Otherwise we try to get the URL of the originating page
        if (isset($post) && $post instanceof WP_Post) {
            $redirect_to = wp_sanitize_redirect(get_permalink($post->ID));
        }
    }
    /* Make sure registrations are open */
    $registration = wpas_get_option('allow_registrations', 'allow');
    if ('allow' !== $registration) {
        wpas_add_error('registration_not_allowed', __('Registrations are currently not allowed.', 'awesome-support'));
        wp_safe_redirect($redirect_to);
        exit;
    }
    // Prepare user data
    $user = array('email' => isset($data['wpas_email']) ? $data['wpas_email'] : '', 'first_name' => isset($data['wpas_first_name']) ? $data['wpas_first_name'] : '', 'last_name' => isset($data['wpas_last_name']) ? $data['wpas_last_name'] : '', 'pwd' => isset($data['wpas_password']) ? $data['wpas_password'] : '');
    /**
     * wpas_pre_register_account hook
     *
     * This hook is triggered all the time
     * even if the checks don't pass.
     *
     * @since  3.0.1
     */
    do_action('wpas_pre_register_account', $user);
    if (wpas_get_option('terms_conditions', false) && !isset($data['wpas_terms'])) {
        wpas_add_error('accept_terms_conditions', esc_html__('You did not accept the terms and conditions.', 'awesome-support'));
        wp_safe_redirect($redirect_to);
        exit;
    }
    /**
     * wpas_register_account_before hook
     *
     * Fired right before the user is added to the database.
     */
    do_action('wpas_register_account_before', $user);
    // Try and insert the new user in the database
    $user_id = wpas_insert_user($user);
    if (is_wp_error($user_id)) {
        /**
         * wpas_register_account_before hook
         *
         * Fired right after a failed attempt to register a user.
         *
         * @since  3.0.1
         */
        do_action('wpas_register_account_failed', $user_id, $user);
        $errors = implode('<br>', $user_id->get_error_messages());
        wpas_add_error('missing_fields', $errors);
        wp_safe_redirect($redirect_to);
        exit;
    } else {
        /**
         * wpas_register_account_before hook
         *
         * Fired right after the user is successfully added to the database.
         *
         * @since  3.0.1
         */
        do_action('wpas_register_account_after', $user_id, $user);
        if (headers_sent()) {
            wpas_add_notification('account_created', esc_html__('Your account has been created. Please log-in.', 'awesome-support'));
            wp_safe_redirect($redirect_to);
            exit;
        }
        if (!is_user_logged_in()) {
            /* Automatically log the user in */
            wp_set_current_user($user_id, get_user_by('ID', $user_id)->data->user_email);
            wp_set_auth_cookie($user_id);
            wp_safe_redirect($redirect_to);
            exit;
        }
    }
}
 function test_wpas_insert_user_invalid()
 {
     $user_id = wpas_insert_user(array('email' => '', 'first_name' => $this->first_name, 'last_name' => $this->last_name, 'pwd' => $this->pwd), false);
     $this->assertInstanceOf('WP_Error', $user_id);
 }