/**
  * Run pre-defined actions.
  *
  * Specific actions can be performed on page load.
  * Those actions are triggered by a URL parameter ($action).
  *
  * @since  3.0.0
  * @return void
  */
 public function custom_actions()
 {
     if (!isset($_GET['action'])) {
         return;
     }
     $action = sanitize_text_field($_GET['action']);
     switch ($action) {
         case 'reopen':
             if (isset($_GET['ticket_id'])) {
                 $ticket_id = filter_input(INPUT_GET, 'ticket_id', FILTER_SANITIZE_NUMBER_INT);
                 if (!wpas_can_submit_ticket($ticket_id) && !current_user_can('edit_ticket')) {
                     wpas_add_error('cannot_reopen_ticket', __('You are not allowed to re-open this ticket', 'awesome-support'));
                     wpas_redirect('ticket_reopen', wpas_get_tickets_list_page_url());
                     exit;
                 }
                 wpas_reopen_ticket($ticket_id);
                 wpas_add_notification('ticket_reopen', __('The ticket has been successfully re-opened.', 'awesome-support'));
                 wpas_redirect('ticket_reopen', wp_sanitize_redirect(get_permalink($ticket_id)));
                 exit;
             }
             break;
     }
 }
/**
 * Trigger the re-open ticket function
 *
 * This is triggered by the wpas_do custom actions.
 *
 * @since 3.3
 *
 * @param array $data Superglobal data
 *
 * @return void
 */
function wpas_reopen_ticket_trigger($data)
{
    if (isset($data['ticket_id'])) {
        $ticket_id = (int) $data['ticket_id'];
        if (!wpas_can_submit_ticket($ticket_id) && !current_user_can('edit_ticket')) {
            wpas_add_error('cannot_reopen_ticket', __('You are not allowed to re-open this ticket', 'awesome-support'));
            wpas_redirect('ticket_reopen', wpas_get_tickets_list_page_url());
            exit;
        }
        wpas_reopen_ticket($ticket_id);
        wpas_add_notification('ticket_reopen', __('The ticket has been successfully re-opened.', 'awesome-support'));
        wpas_redirect('ticket_reopen', wp_sanitize_redirect(get_permalink($ticket_id)));
        exit;
    }
}
/**
 * Add a new error
 *
 * @since 3.2
 *
 * @param string $error_id      ID of the error to add
 * @param string $error_message Error message
 *
 * @return void
 */
function wpas_add_error($error_id, $error_message)
{
    wpas_add_notification($error_id, $error_message, 'errors');
}
/**
 * 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;
        }
    }
}
/**
 * Register user account.
 *
 * @param array|bool $data User data
 *
 * @since  1.0.0
 * @return void
 */
function wpas_register_account($data = false)
{
    global $post;
    /* 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_redirect(wp_sanitize_redirect(get_permalink($post->ID)));
        exit;
    }
    if (false === $data) {
        $data = $_POST;
    }
    $email = isset($data['wpas_email']) && !empty($data['wpas_email']) ? sanitize_email($data['wpas_email']) : false;
    $first_name = isset($data['wpas_first_name']) && !empty($data['wpas_first_name']) ? sanitize_text_field($data['wpas_first_name']) : false;
    $last_name = isset($data['wpas_last_name']) && !empty($data['wpas_last_name']) ? sanitize_text_field($data['wpas_last_name']) : false;
    $pwd = isset($data['wpas_password']) && !empty($data['wpas_password']) ? $data['wpas_password'] : false;
    /**
     * Give a chance to third-parties to add new checks to the account registration process
     *
     * @since 3.2.0
     * @var bool|WP_Error
     */
    $errors = apply_filters('wpas_register_account_errors', false, $first_name, $last_name, $email);
    if (false !== $errors) {
        $notice = implode('\\n\\r', $errors->get_error_messages());
        wpas_add_error('registration_error', $notice);
        wp_redirect(wp_sanitize_redirect(get_permalink($post->ID)));
        exit;
    }
    /**
     * 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', $data);
    if (wpas_get_option('terms_conditions', false) && !isset($data['terms'])) {
        wpas_add_error('accept_terms_conditions', __('You did not accept the terms and conditions.', 'awesome-support'));
        wp_redirect(wp_sanitize_redirect(get_permalink($post->ID)));
        exit;
    }
    /* Make sure we have all the necessary data. */
    if (false === ($email || $first_name || $last_name || $pwd)) {
        wpas_add_error('missing_fields', __('You didn\'t correctly fill all the fields.', 'awesome-support'));
        wp_redirect(wp_sanitize_redirect(get_permalink($post->ID)));
        exit;
    }
    $username = sanitize_user(strtolower($first_name) . strtolower($last_name));
    $user = get_user_by('login', $username);
    /* Check for existing username */
    if (is_a($user, 'WP_User')) {
        $suffix = 1;
        do {
            $alt_username = sanitize_user($username . $suffix);
            $user = get_user_by('login', $alt_username);
            $suffix++;
        } while (is_a($user, 'WP_User'));
        $username = $alt_username;
    }
    /**
     * wpas_insert_user_data filter
     *
     * @since  3.1.5
     * @var    array User account arguments
     */
    $args = apply_filters('wpas_insert_user_data', array('user_login' => $username, 'user_email' => $email, 'first_name' => $first_name, 'last_name' => $last_name, 'display_name' => "{$first_name} {$last_name}", 'user_pass' => $pwd, 'role' => 'wpas_user'));
    /**
     * wpas_register_account_before hook
     *
     * Fired right before the user is added to the database.
     */
    do_action('wpas_register_account_before', $args);
    $user_id = wp_insert_user(apply_filters('wpas_user_registration_data', $args));
    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, $args);
        $error = $user_id->get_error_message();
        wpas_add_error('missing_fields', $error);
        wp_redirect(wp_sanitize_redirect(get_permalink($post->ID)));
        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, $args);
        /* Delete the user information data from session. */
        unset($_SESSION['wpas_registration_form']);
        if (true === apply_filters('wpas_new_user_notification', true)) {
            wp_new_user_notification($user_id);
        }
        if (headers_sent()) {
            wpas_add_notification('account_created', __('Your account has been created. Please log-in.', 'awesome-support'));
            wp_redirect(wp_sanitize_redirect(get_permalink($post->ID)));
            exit;
        }
        if (!is_user_logged_in()) {
            /* Automatically log the user in */
            wp_set_current_user($user_id, $email);
            wp_set_auth_cookie($user_id);
            wp_redirect(get_permalink($post->ID));
            exit;
        }
    }
}
/**
 * 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_redirect($redirect_to);
        exit;
    }
    $user = array();
    $user['email'] = isset($data['wpas_email']) && !empty($data['wpas_email']) ? sanitize_email($data['wpas_email']) : false;
    $user['first_name'] = isset($data['wpas_first_name']) && !empty($data['wpas_first_name']) ? sanitize_text_field($data['wpas_first_name']) : false;
    $user['last_name'] = isset($data['wpas_last_name']) && !empty($data['wpas_last_name']) ? sanitize_text_field($data['wpas_last_name']) : false;
    $user['pwd'] = isset($data['wpas_password']) && !empty($data['wpas_password']) ? $data['wpas_password'] : false;
    $error = false;
    foreach ($user as $field => $value) {
        if (empty($value)) {
            if (false === $error) {
                $error = new WP_Error();
            }
            $error->add('missing_field_' . $field, sprintf(esc_html__('The %s field is mandatory for registering an account', 'awesome-support'), ucwords(str_replace('_', ' ', $field))));
        }
    }
    /**
     * Give a chance to third-parties to add new checks to the account registration process
     *
     * @since 3.2.0
     * @var bool|WP_Error
     */
    $errors = apply_filters('wpas_register_account_errors', $error, $user['first_name'], $user['last_name'], $user['email']);
    if (false !== $errors) {
        $notice = implode('<br>', $errors->get_error_messages());
        wpas_add_error('registration_error', $notice);
        wp_redirect($redirect_to);
        exit;
    }
    /**
     * 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', $data);
    if (wpas_get_option('terms_conditions', false) && !isset($data['terms'])) {
        wpas_add_error('accept_terms_conditions', __('You did not accept the terms and conditions.', 'awesome-support'));
        wp_redirect($redirect_to);
        exit;
    }
    $username = sanitize_user(strtolower($user['first_name']) . strtolower($user['last_name']));
    $user_check = get_user_by('login', $username);
    /* Check for existing username */
    if (is_a($user_check, 'WP_User')) {
        $suffix = 1;
        do {
            $alt_username = sanitize_user($username . $suffix);
            $user_check = get_user_by('login', $alt_username);
            $suffix++;
        } while (is_a($user_check, 'WP_User'));
        $username = $alt_username;
    }
    /**
     * wpas_insert_user_data filter
     *
     * @since  3.1.5
     * @var    array User account arguments
     */
    $args = apply_filters('wpas_insert_user_data', array('user_login' => $username, 'user_email' => $user['email'], 'first_name' => $user['first_name'], 'last_name' => $user['last_name'], 'display_name' => "{$user['first_name']} {$user['last_name']}", 'user_pass' => $user['pwd'], 'role' => 'wpas_user'));
    /**
     * wpas_register_account_before hook
     *
     * Fired right before the user is added to the database.
     */
    do_action('wpas_register_account_before', $args);
    $user_id = wp_insert_user(apply_filters('wpas_user_registration_data', $args));
    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, $args);
        $error = $user_id->get_error_message();
        wpas_add_error('missing_fields', $error);
        wp_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, $args);
        if (true === apply_filters('wpas_new_user_notification', true)) {
            wp_new_user_notification($user_id);
        }
        if (headers_sent()) {
            wpas_add_notification('account_created', __('Your account has been created. Please log-in.', 'awesome-support'));
            wp_redirect($redirect_to);
            exit;
        }
        if (!is_user_logged_in()) {
            /* Automatically log the user in */
            wp_set_current_user($user_id, $user['email']);
            wp_set_auth_cookie($user_id);
            wp_redirect($redirect_to);
            exit;
        }
    }
}