/** * 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; } } } } }
/** * 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'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; } } } }
function test_wpas_add_reply() { $ticket_id = wpas_insert_ticket($this->ticket_data, false); $reply_id = wpas_add_reply($this->reply_data, $ticket_id); $this->assertInternalType('int', $reply_id); }
/** * Save ticket custom fields. * * This function will save all custom fields associated * to the ticket post type. Be it core custom fields * or user added custom fields. * * @param (int) $post_id Current post ID * @since 3.0.0 */ public function save_ticket($post_id) { /* We should already being avoiding Ajax, but let's make sure */ if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || wp_is_post_revision($post_id)) { return; } if (defined('DOING_AJAX') && DOING_AJAX) { return; } /* Now we check the nonce */ if (!isset($_POST[Awesome_Support_Admin::$nonce_name]) || !wp_verify_nonce($_POST[Awesome_Support_Admin::$nonce_name], Awesome_Support_Admin::$nonce_action)) { return; } /* Does the current user has permission? */ if (!current_user_can('edit_ticket', $post_id)) { return; } global $current_user, $wpas_cf; /** * Store possible logs */ $log = array(); /** * If no ticket status is found we are in the situation where * the agent is creating a ticket on behalf of the user. There are * a couple of things that we need to do then. */ if ('' === ($original_status = get_post_meta($post_id, '_wpas_status', true))) { /** * First of all, set the ticket as open. This is very important. */ add_post_meta($post_id, '_wpas_status', 'open', true); /** * Send the confirmation e-mail to the user. * * @since 3.1.5 */ wpas_email_notify($post_id, 'submission_confirmation'); } /* Save the possible ticket reply */ if (isset($_POST['wpas_reply']) && isset($_POST['wpas_reply_ticket']) && '' != $_POST['wpas_reply']) { /* Check for the nonce */ if (wp_verify_nonce($_POST['wpas_reply_ticket'], 'reply_ticket')) { $user_id = $current_user->ID; $content = wp_kses_post($_POST['wpas_reply']); $data = apply_filters('wpas_post_reply_admin_args', array('post_content' => $content, 'post_status' => 'read', 'post_type' => 'ticket_reply', 'post_author' => $user_id, 'post_parent' => $post_id, 'ping_status' => 'closed', 'comment_status' => 'closed')); /** * Remove the save_post hook now as we're going to trigger * a new one by inserting the reply (and logging the history later). */ remove_action('save_post_ticket', array($this, 'save_ticket')); /** * Fires right before a ticket reply is submitted * * @since 3.2.6 * * @param int $post_id Ticket ID * @param array $data Data to be inserted as the reply */ do_action('wpas_post_reply_admin_before', $post_id, $data); /* Insert the reply in DB */ $reply = wpas_add_reply($data, $post_id); /** * Fires right after a ticket reply is submitted * * @since 3.2.6 * * @param int $post_id Ticket ID * @param array $data Data to be inserted as the reply * @param bool|int Reply ID on success, false on failure */ do_action('wpas_post_reply_admin_after', $post_id, $data, $reply); /* In case the insertion failed... */ if (is_wp_error($reply)) { /* Set the redirection */ $_SESSION['wpas_redirect'] = add_query_arg(array('wpas-message' => 'wpas_reply_error'), get_permalink($post_id)); } else { /* E-Mail the client */ $new_reply = new WPAS_Email_Notification($post_id, array('reply_id' => $reply, 'action' => 'reply_agent')); /* The agent wants to close the ticket */ if (isset($_POST['wpas_do']) && 'reply_close' == $_POST['wpas_do']) { /* Confirm the post type and close */ if ('ticket' == get_post_type($post_id)) { /** * wpas_ticket_before_close_by_agent hook */ do_action('wpas_ticket_before_close_by_agent', $post_id); /* Close */ $closed = wpas_close_ticket($post_id); /* E-Mail the client */ new WPAS_Email_Notification($post_id, array('action' => 'closed')); /** * wpas_ticket_closed_by_agent hook */ do_action('wpas_ticket_closed_by_agent', $post_id); } } } } } /* Now we can save the custom fields */ $wpas_cf->save_custom_fields($post_id, $_POST); /* Log the action */ if (!empty($log)) { wpas_log($post_id, $log); } /* If this was a ticket update, we need to know where to go now... */ if ('' !== $original_status) { /* Go back to the tickets list */ if (isset($_POST['wpas_back_to_list']) && true === boolval($_POST['wpas_back_to_list']) || isset($_POST['where_after']) && 'back_to_list' === $_POST['where_after']) { $_SESSION['wpas_redirect'] = add_query_arg(array('post_type' => 'ticket'), admin_url('edit.php')); } } }