/**
  * Actions run on plugin initialization.
  *
  * A certain number of things can possibly run after
  * the plugin initialized. Those actions are fired from here
  * if the trigger is present.
  *
  * @since  3.0.0
  * @return void
  */
 public function init()
 {
     /**
      * Log user in.
      *
      * If we have a login in the post data we try to log the user in.
      * The login process relies on the WordPress core functions. If the login
      * is successful, the user is redirected to the page he was requesting,
      * otherwise the standard WordPress error messages are returned.
      *
      * @since 3.0.0
      */
     if (isset($_POST['wpas_login'])) {
         add_action('wp', 'wpas_try_login');
     }
     /**
      * Register a new account.
      *
      * If wpas_registration is passed we trigger the account registration function.
      * The registration function will do a certain number of checks and if all of them
      * are successful, a new user is created using the WordPress core functions.
      *
      * The reason why we are not using a simpler process is to keep full control over
      * what's returned to the user and where the user is returned.
      *
      * @since 3.0.0
      */
     if (isset($_POST['wpas_registration'])) {
         add_action('wp', 'wpas_register_account', 10, 0);
     }
     /**
      * Run custom actions.
      *
      * The plugin can run a number of custom actions triggered by a URL parameter.
      * If the $action parameter is set in the URL we run this method.
      *
      * @since  3.0.0
      */
     if (isset($_GET['action'])) {
         add_action('wp', array($this, 'custom_actions'));
     }
     /**
      * Open a new ticket.
      *
      * If a ticket title is passed in the post we trigger the function that adds
      * new tickets. The function does a certain number of checks and has several
      * action hooks and filters. Post-insertion actions like adding post metas
      * and redirecting the user are run from here.
      *
      * @since  3.0.0
      */
     if (isset($_POST['wpas_title'])) {
         // Verify the nonce first
         if (!isset($_POST['wpas_nonce']) || !wp_verify_nonce($_POST['wpas_nonce'], 'new_ticket')) {
             /* Save the input */
             wpas_save_values();
             // Redirect to submit page
             wp_redirect(add_query_arg(array('message' => 4), get_permalink(wpas_get_option('ticket_submit'))));
             exit;
         }
         $ticket_id = wpas_open_ticket(array('title' => $_POST['wpas_title'], 'message' => $_POST['wpas_message']));
         /* Submission failure */
         if (false === $ticket_id) {
             /* Save the input */
             wpas_save_values();
             /**
              * Redirect to the newly created ticket
              */
             $submit = wpas_get_option('ticket_submit');
             wpas_redirect('ticket_added_failed', add_query_arg(array('message' => 6), get_permalink($submit)), $submit);
             exit;
         } else {
             /**
              * Empty the temporary sessions
              */
             unset($_SESSION['wpas_submission_form']);
             unset($_SESSION['wpas_submission_error']);
             /**
              * Redirect to the newly created ticket
              */
             wpas_redirect('ticket_added', get_permalink($ticket_id), $ticket_id);
             exit;
         }
     }
     /**
      * Save a new reply.
      *
      * This adds a new reply to an existing ticket. The ticket
      * can possibly be closed by the user in which case we update
      * the post meta if the reply submission is successful.
      *
      * @since 3.0.0
      */
     if (isset($_POST['wpas_user_reply'])) {
         /**
          * Define if the reply can be submitted empty or not.
          *
          * @since  3.0.0
          * @var boolean
          */
         $can_submit_empty = apply_filters('wpas_can_reply_be_empty', false);
         /**
          * Get the parent ticket ID.
          */
         $parent_id = intval($_POST['ticket_id']);
         if (empty($_POST['wpas_user_reply']) && false === $can_submit_empty) {
             wpas_redirect('reply_not_added', add_query_arg(array('message' => wpas_create_notification(__('You cannot submit an empty reply.', 'wpas'))), get_permalink($parent_id)), $parent_id);
             exit;
         }
         /* Sanitize the data */
         $data = array('post_content' => wp_kses($_POST['wpas_user_reply'], wp_kses_allowed_html('post')));
         /* Add the reply */
         $reply_id = wpas_add_reply($data, $parent_id);
         /* Possibly close the ticket */
         if (isset($_POST['wpas_close_ticket']) && false !== $reply_id) {
             wpas_close_ticket(intval($_POST['ticket_id']));
         }
         if (false === $reply_id) {
             wpas_redirect('reply_added_failed', add_query_arg(array('message' => '7'), get_permalink($parent_id)));
             exit;
         } else {
             /**
              * Delete the activity transient.
              */
             delete_transient("wpas_activity_meta_post_{$parent_id}");
             wpas_redirect('reply_added', add_query_arg(array('message' => '8'), get_permalink($parent_id)) . "#reply-{$reply_id}", $parent_id);
             exit;
         }
     }
 }
 /**
  * Actions run on plugin initialization.
  *
  * A certain number of things can possibly run after
  * the plugin initialized. Those actions are fired from here
  * if the trigger is present.
  *
  * @since  3.0.0
  * @return void
  */
 public function init()
 {
     /**
      * Log user in.
      *
      * If we have a login in the post data we try to log the user in.
      * The login process relies on the WordPress core functions. If the login
      * is successful, the user is redirected to the page he was requesting,
      * otherwise the standard WordPress error messages are returned.
      *
      * @since 3.0.0
      */
     if (isset($_POST['wpas_login'])) {
         add_action('wp', 'wpas_try_login');
     }
     /**
      * Register a new account.
      *
      * If wpas_registration is passed we trigger the account registration function.
      * The registration function will do a certain number of checks and if all of them
      * are successful, a new user is created using the WordPress core functions.
      *
      * The reason why we are not using a simpler process is to keep full control over
      * what's returned to the user and where the user is returned.
      *
      * @since 3.0.0
      */
     if (isset($_POST['wpas_registration'])) {
         add_action('wp', 'wpas_register_account', 10, 0);
     }
     /**
      * Run custom actions.
      *
      * The plugin can run a number of custom actions triggered by a URL parameter.
      * If the $action parameter is set in the URL we run this method.
      *
      * @since  3.0.0
      */
     if (isset($_GET['action'])) {
         add_action('wp', array($this, 'custom_actions'));
     }
     /**
      * Open a new ticket.
      *
      * If a ticket title is passed in the post we trigger the function that adds
      * new tickets. The function does a certain number of checks and has several
      * action hooks and filters. Post-insertion actions like adding post metas
      * and redirecting the user are run from here.
      *
      * @since  3.0.0
      */
     if (!is_admin() && isset($_POST['wpas_title'])) {
         // Verify the nonce first
         if (!isset($_POST['wpas_nonce']) || !wp_verify_nonce($_POST['wpas_nonce'], 'new_ticket')) {
             /* Save the input */
             wpas_save_values();
             // Redirect to submit page
             wpas_add_error('nonce_verification_failed', __('The authenticity of your submission could not be validated. If this ticket is legitimate please try submitting again.', 'awesome-support'));
             wp_redirect(wp_sanitize_redirect(home_url($_POST['_wp_http_referer'])));
             exit;
         }
         $ticket_id = wpas_open_ticket(array('title' => $_POST['wpas_title'], 'message' => $_POST['wpas_message']));
         /* Submission failure */
         if (false === $ticket_id) {
             /* Save the input */
             wpas_save_values();
             /**
              * Redirect to the newly created ticket
              */
             wpas_add_error('submission_error', __('The ticket couldn\'t be submitted for an unknown reason.', 'awesome-support'));
             wp_redirect(wp_sanitize_redirect(home_url($_POST['_wp_http_referer'])));
             exit;
         } else {
             /**
              * Empty the temporary sessions
              */
             WPAS()->session->clean('submission_form');
             /**
              * Redirect to the newly created ticket
              */
             wpas_redirect('ticket_added', get_permalink($ticket_id), $ticket_id);
             exit;
         }
     }
     /**
      * Save a new reply.
      *
      * This adds a new reply to an existing ticket. The ticket
      * can possibly be closed by the user in which case we update
      * the post meta if the reply submission is successful.
      *
      * @since 3.0.0
      */
     if (isset($_POST['wpas_user_reply'])) {
         // Get parent ticket ID
         $parent_id = filter_input(INPUT_POST, 'ticket_id', FILTER_SANITIZE_NUMBER_INT);
         if ('ticket' !== get_post_type($parent_id)) {
             wpas_add_error('reply_added_failed', __('Something went wrong. We couldn't identify your ticket. Please try again.', 'awesome-support'));
             wpas_redirect('reply_added_failed', get_permalink($parent_id));
             exit;
         }
         // Define if the ticket must be closed
         $close = isset($_POST['wpas_close_ticket']) ? true : false;
         if (!empty($_POST['wpas_user_reply'])) {
             /* Sanitize the data */
             $data = array('post_content' => wp_kses($_POST['wpas_user_reply'], wp_kses_allowed_html('post')));
             /* Add the reply */
             $reply_id = wpas_add_reply($data, $parent_id);
         }
         /* Possibly close the ticket */
         if ($close) {
             wpas_close_ticket($parent_id);
             // Redirect now if no reply was posted
             if (!isset($reply_id)) {
                 wpas_add_notification('ticket_closed', __('The ticket was successfully closed', 'awesome-support'));
                 wpas_redirect('ticket_closed', get_permalink($parent_id));
                 exit;
             }
         }
         if (isset($reply_id)) {
             if (false === $reply_id) {
                 wpas_add_error('reply_added_failed', __('Your reply could not be submitted for an unknown reason.', 'awesome-support'));
                 wpas_redirect('reply_added_failed', get_permalink($parent_id));
                 exit;
             } else {
                 if ($close) {
                     wpas_add_notification('reply_added_closed', __('Thanks for your reply. The ticket is now closed.', 'awesome-support'));
                 } else {
                     wpas_add_notification('reply_added', __('Your reply has been submitted. Your agent will reply ASAP.', 'awesome-support'));
                 }
                 if (false !== ($link = wpas_get_reply_link($reply_id))) {
                     wpas_redirect('reply_added', $link);
                     exit;
                 }
             }
         }
     }
 }
/**
 * Open a new ticket.
 *
 * @since  3.0.0
 * @param  array $data Ticket data
 * @return boolean
 */
function wpas_open_ticket($data)
{
    $title = isset($data['title']) ? wp_strip_all_tags($data['title']) : false;
    $content = isset($data['message']) ? wp_kses($data['message'], wp_kses_allowed_html('post')) : false;
    /**
     * Prepare vars
     */
    $submit = wpas_get_option('ticket_submit');
    // ID of the submission page
    // Verify user capability
    if (!current_user_can('create_ticket')) {
        // Save the input
        wpas_save_values();
        // Redirect to submit page
        wp_redirect(add_query_arg(array('message' => 11), get_permalink($submit)));
        // Break
        exit;
    }
    // Make sure we have at least a title and a message
    if (false === $title || empty($title)) {
        // Save the input
        wpas_save_values();
        // Redirect to submit page
        wp_redirect(add_query_arg(array('message' => 3), get_permalink($submit)));
        // Break
        exit;
    }
    if (true === ($description_mandatory = apply_filters('wpas_ticket_submission_description_mandatory', true)) && (false === $content || empty($content))) {
        // Save the input
        wpas_save_values();
        // Redirect to submit page
        wp_redirect(add_query_arg(array('message' => 10), get_permalink($submit)));
        // Break
        exit;
    }
    /**
     * Allow the submission.
     *
     * This variable is used to add additional checks in the submission process.
     * If the $go var is set to true, it gives a green light to this method
     * and the ticket will be submitted. If the var is set to false, the process
     * will be aborted.
     *
     * @since  3.0.0
     */
    $go = apply_filters('wpas_before_submit_new_ticket_checks', true);
    /* Check for the green light */
    if (is_wp_error($go)) {
        /* Retrieve error messages. */
        $messages = $go->get_error_messages();
        /* Save the input */
        wpas_save_values();
        /* Redirect to submit page */
        wp_redirect(add_query_arg(array('message' => wpas_create_notification($messages)), get_permalink($submit)));
        exit;
    }
    /**
     * Gather current user info
     */
    if (is_user_logged_in()) {
        global $current_user;
        $user_id = $current_user->ID;
    } else {
        // Save the input
        wpas_save_values();
        // Redirect to submit page
        wp_redirect(add_query_arg(array('message' => 5), get_permalink($submit)));
        // Break
        exit;
    }
    /**
     * Submit the ticket.
     *
     * Now that all the verifications are passed
     * we can proceed to the actual ticket submission.
     */
    $post = array('post_content' => $content, 'post_name' => $title, 'post_title' => $title, 'post_status' => 'queued', 'post_type' => 'ticket', 'post_author' => $user_id, 'ping_status' => 'closed', 'comment_status' => 'closed');
    return wpas_insert_ticket($post, false, false);
}
/**
 * Instantiate a new ticket submission
 *
 * This helper function is used to trigger the creation of a new ticket
 * after the ticket submission form is posted on the front-end.
 *
 * @since 3.3
 *
 * @param array $data Ticket data required to open a new ticket
 *
 * @return void
 */
function wpas_new_ticket_submission($data)
{
    if (!is_admin() && isset($data['wpas_title'])) {
        // Verify the nonce first
        if (!isset($data['wpas_nonce']) || !wp_verify_nonce($data['wpas_nonce'], 'new_ticket')) {
            /* Save the input */
            wpas_save_values();
            // Redirect to submit page
            wpas_add_error('nonce_verification_failed', __('The authenticity of your submission could not be validated. If this ticket is legitimate please try submitting again.', 'awesome-support'));
            wp_redirect(wp_sanitize_redirect(home_url($_POST['_wp_http_referer'])));
            exit;
        }
        $ticket_id = wpas_open_ticket(array('title' => $data['wpas_title'], 'message' => $data['wpas_message']));
        /* Submission failure */
        if (false === $ticket_id) {
            /* Save the input */
            wpas_save_values();
            /**
             * Redirect to the newly created ticket
             */
            wpas_add_error('submission_error', __('The ticket couldn\'t be submitted for an unknown reason.', 'awesome-support'));
            wp_redirect(wp_sanitize_redirect(home_url($data['_wp_http_referer'])));
            exit;
        } else {
            /**
             * Empty the temporary sessions
             */
            WPAS()->session->clean('submission_form');
            /**
             * Redirect to the newly created ticket
             */
            wpas_redirect('ticket_added', get_permalink($ticket_id), $ticket_id);
            exit;
        }
    }
}
/**
 * Open a new ticket.
 *
 * @since  3.0.0
 * @param  array $data Ticket data
 * @return boolean
 */
function wpas_open_ticket($data)
{
    $title = isset($data['title']) ? wp_strip_all_tags($data['title']) : false;
    $content = isset($data['message']) ? wp_kses($data['message'], wp_kses_allowed_html('post')) : false;
    /**
     * Prepare vars
     */
    $submit = isset($_POST['_wp_http_referer']) ? wpas_get_submission_page_url(url_to_postid($_POST['_wp_http_referer'])) : wpas_get_submission_page_url();
    // Fallback in case the referrer failed
    if (empty($submit)) {
        $submission_pages = wpas_get_option('ticket_submit');
        $submit = $submission_pages[0];
        $submit = wp_sanitize_redirect(get_permalink($submit));
    }
    // Verify user capability
    if (!current_user_can('create_ticket')) {
        // Save the input
        wpas_save_values();
        // Redirect to submit page
        wpas_add_error('cannot_open_ticket', __('You do not have the capacity to open a new ticket.', 'wpas'));
        wp_redirect($submit);
        // Break
        exit;
    }
    // Make sure we have at least a title and a message
    if (false === $title || empty($title)) {
        // Save the input
        wpas_save_values();
        // Redirect to submit page
        wpas_add_error('missing_title', __('It is mandatory to provide a title for your issue.', 'wpas'));
        wp_redirect($submit);
        // Break
        exit;
    }
    if (true === ($description_mandatory = apply_filters('wpas_ticket_submission_description_mandatory', true)) && (false === $content || empty($content))) {
        // Save the input
        wpas_save_values();
        // Redirect to submit page
        wpas_add_error('missing_description', __('It is mandatory to provide a description for your issue.', 'wpas'));
        wp_redirect($submit);
        // Break
        exit;
    }
    /**
     * Allow the submission.
     *
     * This variable is used to add additional checks in the submission process.
     * If the $go var is set to true, it gives a green light to this method
     * and the ticket will be submitted. If the var is set to false, the process
     * will be aborted.
     *
     * @since  3.0.0
     */
    $go = apply_filters('wpas_before_submit_new_ticket_checks', true);
    /* Check for the green light */
    if (is_wp_error($go)) {
        /* Retrieve error messages. */
        $messages = $go->get_error_messages();
        /* Save the input */
        wpas_save_values();
        /* Redirect to submit page */
        wpas_add_error('validation_issue', $messages);
        wp_redirect($submit);
        exit;
    }
    /**
     * Gather current user info
     */
    if (is_user_logged_in()) {
        global $current_user;
        $user_id = $current_user->ID;
    } else {
        // Save the input
        wpas_save_values();
        // Redirect to submit page
        wpas_add_error('unknown_user', __('Only registered accounts can submit a ticket. Please register first.', 'wpas'));
        wp_redirect($submit);
        exit;
    }
    /**
     * Submit the ticket.
     *
     * Now that all the verifications are passed
     * we can proceed to the actual ticket submission.
     */
    $post = array('post_content' => $content, 'post_name' => $title, 'post_title' => $title, 'post_status' => 'queued', 'post_type' => 'ticket', 'post_author' => $user_id, 'ping_status' => 'closed', 'comment_status' => 'closed');
    return wpas_insert_ticket($post, false, false);
}