/**
  * 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;
                 }
             }
         }
     }
 }
/**
 * Get tickets lit columns content.
 *
 * Based on the columns displayed in the front-end tickets list,
 * this function will display the column content by using its callback.
 * The callback can be a "standard" case like the title, or a custom function
 * as used by the custom fields mostly.
 *
 * @since  3.0.0
 * @param  string $column_id ID of the current column
 * @param  array  $column    Columns data
 * @return void
 */
function wpas_get_tickets_list_column_content($column_id, $column)
{
    $callback = $column['callback'];
    switch ($callback) {
        case 'id':
            echo '#' . get_the_ID();
            break;
        case 'status':
            echo wpas_get_ticket_status(get_the_ID());
            break;
        case 'title':
            // If the replies are displayed from the oldest to the newest we want to link directly to the latest reply in case there are multiple reply pages
            if ('ASC' === wpas_get_option('replies_order', 'ASC')) {
                $last_reply = wpas_get_replies(get_the_ID(), array('read', 'unread'), array('posts_per_page' => 1, 'order' => 'DESC'));
                $link = !empty($last_reply) ? wpas_get_reply_link($last_reply[0]->ID) : get_permalink(get_the_ID());
            } else {
                $link = get_permalink(get_the_ID());
            }
            ?>
<a href="<?php 
            echo $link;
            ?>
"><?php 
            the_title();
            ?>
</a><?php 
            break;
        case 'date':
            $offset = wpas_get_offset_html5();
            ?>
<time datetime="<?php 
            echo get_the_date('Y-m-d\\TH:i:s') . $offset;
            ?>
"><?php 
            echo get_the_date(get_option('date_format')) . ' ' . get_the_date(get_option('time_format'));
            ?>
</time><?php 
            break;
        case 'taxonomy':
            $terms = get_the_terms(get_the_ID(), $column_id);
            $list = array();
            if (empty($terms)) {
                continue;
            }
            foreach ($terms as $term) {
                array_push($list, $term->name);
            }
            echo implode(', ', $list);
            break;
        default:
            if (function_exists($callback)) {
                call_user_func($callback, $column_id, get_the_ID());
            }
            break;
    }
}
/**
 * Instantiate a new reply submission
 *
 * This helper function is used to trigger the creation of a new reply
 * after the reply submission form is posted on the front-end.
 *
 * @since 3.3
 *
 * @param array $data Reply data required to open a new ticket
 *
 * @return void
 */
function wpas_new_reply_submission($data)
{
    // Get parent ticket ID
    $parent_id = (int) $data['ticket_id'];
    if ('ticket' !== get_post_type($parent_id)) {
        wpas_add_error('reply_added_failed', __('Something went wrong. We couldn&#039;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($data['wpas_close_ticket']) ? true : false;
    if (!empty($data['wpas_user_reply'])) {
        /* Sanitize the data */
        $data = array('post_content' => wp_kses($data['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;
            }
        }
    }
}