/** * Change the redirection URL. * * In case the upload fails we want to notify the user. * We change the redirection URL and integrate a custom message * encoded in base64 that will be interpreted by the notification class. * * @since 3.0.0 * * @param string $location Original redirection URL * * @return string New redirection URL */ public function redirect_error($location) { $url = remove_query_arg('message', $location); $error = is_array($this->error_message) ? implode(', ', $this->error_message) : $this->error_message; wpas_add_error('files_not_uploaded', sprintf(__('Your reply has been correctly submitted but the attachment was not uploaded. %s', 'awesome-support'), $error)); $location = wp_sanitize_redirect($url); return $location; }
/** * 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; } }
/** * Try to log the user in. * * This function is hooked onto wpas_do_login so that the login process can be triggered * when the login form is submitted. * * @since 2.0 * * @param array $data Function arguments (the superglobal vars if the function is triggered by wpas_do_login) * * @return void */ function wpas_try_login($data) { /** * Try to log the user if credentials are submitted. */ if (isset($data['wpas_log'])) { // 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)); } } $credentials = array('user_login' => $data['wpas_log']); if (isset($data['rememberme'])) { $credentials['remember'] = true; } $credentials['user_password'] = isset($data['wpas_pwd']) ? $data['wpas_pwd'] : ''; /** * Give a chance to third-parties to add new checks to the login process * * @since 3.2.0 * @var bool|WP_Error */ $login = apply_filters('wpas_try_login', false); if (is_wp_error($login)) { $error = $login->get_error_message(); wpas_add_error('login_failed', $error); wp_safe_redirect($redirect_to); exit; } $login = wp_signon($credentials); if (is_wp_error($login)) { $code = $login->get_error_code(); $error = $login->get_error_message(); // Pre-populate the user login if the problem is with the password if ('incorrect_password' === $code) { $redirect_to = add_query_arg('wpas_log', $credentials['user_login'], $redirect_to); } wpas_add_error('login_failed', $error); wp_safe_redirect($redirect_to); exit; } elseif ($login instanceof WP_User) { wp_safe_redirect($redirect_to); exit; } else { wpas_add_error('login_failed', __('We were unable to log you in for an unknown reason.', 'awesome-support')); wp_safe_redirect($redirect_to); 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); }
/** * Try to log the user in. * * If credentials are passed through the POST data * we try to log the user in. */ function wpas_try_login() { global $post; /** * Try to log the user if credentials are submitted. */ if (isset($_POST['wpas_log'])) { $credentials = array('user_login' => $_POST['wpas_log']); if (isset($_POST['rememberme'])) { $credentials['remember'] = true; } $credentials['user_password'] = isset($_POST['wpas_pwd']) ? $_POST['wpas_pwd'] : ''; /** * Give a chance to third-parties to add new checks to the login process * * @since 3.2.0 * @var bool|WP_Error */ $login = apply_filters('wpas_try_login', false); if (is_wp_error($login)) { $error = $login->get_error_message(); wpas_add_error('login_failed', $error); wp_redirect(wp_sanitize_redirect(get_permalink($post->ID))); exit; } $login = wp_signon($credentials); if (is_wp_error($login)) { $error = $login->get_error_message(); wpas_add_error('login_failed', $error); wp_redirect(wp_sanitize_redirect(get_permalink($post->ID))); exit; } elseif (is_a($login, 'WP_User')) { wp_redirect(get_permalink($post->ID)); exit; } else { wpas_add_error('login_failed', __('We were unable to log you in for an unknown reason.', 'awesome-support')); wp_redirect(wp_sanitize_redirect(get_permalink($post->ID))); exit; } } }