| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Awesome Support/Admin/Functions/Actions |
| 4 |
* @author AwesomeSupport <contact@getawesomesupport.com> |
| 5 |
* @license GPL-2.0+ |
| 6 |
* @link https://getawesomesupport.com |
| 7 |
* @copyright 2015-2017 AwesomeSupport |
| 8 |
*/ |
| 9 |
|
| 10 |
// If this file is called directly, abort. |
| 11 |
if ( ! defined( 'WPINC' ) ) { |
| 12 |
die; |
| 13 |
} |
| 14 |
|
| 15 |
|
| 16 |
add_action( 'admin_head-post.php', 'admin_head_post_editing' ); |
| 17 |
/** |
| 18 |
* Check to see if user can view a ticket. If not, return them to the ticket list |
| 19 |
* |
| 20 |
* @since 3.3 |
| 21 |
* |
| 22 |
* @return void |
| 23 |
*/ |
| 24 |
function admin_head_post_editing() { |
| 25 |
|
| 26 |
global $post_type, $post_ID, $current_user; |
| 27 |
|
| 28 |
if( 'ticket' === $post_type ) { |
| 29 |
$can = wpas_can_view_ticket( $post_ID ); |
| 30 |
|
| 31 |
if( $can ) { |
| 32 |
//can view ticket - do nothing and continue. |
| 33 |
} |
| 34 |
else { |
| 35 |
//Not allowed to view ticket - write to log file and bail out. |
| 36 |
wpas_write_log('security', 'A logged in user attempted to access a ticket without the necessary permissions. ' . 'Ticket id: ' . (string) $post_ID . ', Logged In user ID: ' . (string) $current_user->ID ) ; |
| 37 |
wp_redirect( add_query_arg( array( 'post_type' => 'ticket' ), admin_url( 'edit.php' ) ) ); |
| 38 |
exit; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
} |
| 43 |
|
| 44 |
add_action( 'wpas_do_admin_close_ticket', 'wpas_admin_action_close_ticket' ); |
| 45 |
/** |
| 46 |
* Close a ticket |
| 47 |
* |
| 48 |
* @since 3.3 |
| 49 |
* |
| 50 |
* @param obj $data |
| 51 |
* |
| 52 |
* @return void |
| 53 |
*/ |
| 54 |
function wpas_admin_action_close_ticket( $data ) { |
| 55 |
|
| 56 |
global $pagenow; |
| 57 |
|
| 58 |
if ( ! is_admin() ) { |
| 59 |
return; |
| 60 |
} |
| 61 |
|
| 62 |
if ( ! isset( $data['post'] ) ) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
$post_id = (int) $data['post']; |
| 67 |
|
| 68 |
$closed = wpas_close_ticket( $post_id ); |
| 69 |
|
| 70 |
if( $closed ) { |
| 71 |
|
| 72 |
// Read-only redirect |
| 73 |
if ( 'post.php' === $pagenow ) { |
| 74 |
$redirect_to = add_query_arg( array( |
| 75 |
'action' => 'edit', |
| 76 |
'post' => $post_id, |
| 77 |
'wpas-message' => 'closed' |
| 78 |
), admin_url( 'post.php' ) ); |
| 79 |
} else { |
| 80 |
$redirect_to = add_query_arg( array( |
| 81 |
'post_type' => 'ticket', |
| 82 |
'post' => $post_id, |
| 83 |
'wpas-message' => 'closed' |
| 84 |
), admin_url( 'edit.php' ) ); |
| 85 |
} |
| 86 |
} else { |
| 87 |
$redirect_to = WPAS()->session->get( 'redirect' ); |
| 88 |
} |
| 89 |
|
| 90 |
wp_redirect( wp_sanitize_redirect( $redirect_to ) ); |
| 91 |
exit; |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
add_action( 'wpas_do_admin_open_ticket', 'wpas_admin_action_open_ticket' ); |
| 96 |
/** |
| 97 |
* (Re)open a ticket |
| 98 |
* |
| 99 |
* @since 3.3 |
| 100 |
* |
| 101 |
* @param obj $data |
| 102 |
* |
| 103 |
* @return void |
| 104 |
*/ |
| 105 |
function wpas_admin_action_open_ticket( $data ) { |
| 106 |
|
| 107 |
if ( ! is_admin() ) { |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
if ( ! isset( $data['post'] ) ) { |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
$post_id = (int) $data['post']; |
| 116 |
|
| 117 |
wpas_reopen_ticket( $post_id ); |
| 118 |
|
| 119 |
// Read-only redirect |
| 120 |
$redirect_to = add_query_arg( array( |
| 121 |
'action' => 'edit', |
| 122 |
'post' => $post_id, |
| 123 |
'wpas-message' => 'opened' |
| 124 |
), admin_url( 'post.php' ) ); |
| 125 |
|
| 126 |
wp_redirect( wp_sanitize_redirect( $redirect_to ) ); |
| 127 |
exit; |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
add_action( 'wpas_do_admin_trash_reply', 'wpas_admin_action_trash_reply' ); |
| 132 |
/** |
| 133 |
* Trash a reply |
| 134 |
* |
| 135 |
* @since 3.3 |
| 136 |
* |
| 137 |
* @param obj $data |
| 138 |
* |
| 139 |
* @return void |
| 140 |
*/ |
| 141 |
function wpas_admin_action_trash_reply( $data ) { |
| 142 |
|
| 143 |
if ( ! is_admin() ) { |
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
if ( ! isset( $data['reply_id'] ) ) { |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
$reply_id = (int) $data['reply_id']; |
| 152 |
$ticket_id = ''; // will populate this below... |
| 153 |
$trashed_post = null ; // will populate this below |
| 154 |
|
| 155 |
/* Get the contents of the post being trashed */ |
| 156 |
$trashed_contents = ''; |
| 157 |
if ( $reply_id > 0 ) { |
| 158 |
|
| 159 |
$trashed_post = get_post( $reply_id ); |
| 160 |
|
| 161 |
if ( ! is_null( $reply_id ) ) { |
| 162 |
$trashed_contents = $trashed_post->post_content ; |
| 163 |
$ticket_id = $trashed_post->post_parent; |
| 164 |
} else { |
| 165 |
return false ; |
| 166 |
} |
| 167 |
|
| 168 |
} |
| 169 |
|
| 170 |
/* |
| 171 |
* Remove the attachments from the post before trashing the post |
| 172 |
* Note that even if we restore the post from the trash, |
| 173 |
* the attachments will not be restored - they are permanently deleted here. |
| 174 |
* There is no UI provision in Awesome Support to restore a reply from the trash |
| 175 |
* anyway and no anticipated future need where we would need to. So no harm |
| 176 |
* deleting the attachments permanently here. |
| 177 |
*/ |
| 178 |
wpas_delete_post_attachments( $reply_id ) ; |
| 179 |
|
| 180 |
/* Now trash/delete the post */ |
| 181 |
if ( boolval( wpas_get_option( 'permanently_trash_replies', false ) ) ) { |
| 182 |
wp_delete_post( $reply_id, true ); // Permanentaly delete |
| 183 |
} else { |
| 184 |
wp_trash_post( $reply_id ); // Move to trash - having it in trash allows the UI to post a "reply was deleted" alert in the ticket. |
| 185 |
} |
| 186 |
|
| 187 |
/* Add a flag to the TICKET that shows one of its replies was deleted */ |
| 188 |
update_post_meta( $ticket_id, 'wpas_reply_was_deleted', '1' ) ; |
| 189 |
|
| 190 |
|
| 191 |
/* Fire the after-delete action hook */ |
| 192 |
do_action( 'wpas_admin_reply_trashed', $reply_id, $trashed_post, $ticket_id ); |
| 193 |
|
| 194 |
// Read-only redirect |
| 195 |
$redirect_to = add_query_arg( array( |
| 196 |
'action' => 'edit', |
| 197 |
'post' => $data['post'], |
| 198 |
), admin_url( 'post.php' ) ); |
| 199 |
|
| 200 |
wp_redirect( wp_sanitize_redirect( "$redirect_to#wpas-post-$reply_id" ) ); |
| 201 |
exit; |
| 202 |
|
| 203 |
} |
| 204 |
|
| 205 |
add_action( 'wpas_admin_reply_trashed', 'wpas_log_reply_trashed', 10,3 ); |
| 206 |
/** |
| 207 |
* Log the original contents of a deleted reply. |
| 208 |
* |
| 209 |
* Action hook: wpas_admin_reply_trashed |
| 210 |
* |
| 211 |
* @since 5.2.0 |
| 212 |
* @param int $reply_id - the id of the reply being edited. |
| 213 |
* @param array $original_reply - the original post content before tit was deleted. |
| 214 |
* @param int $ticket_id - ticket id that the reply belongs to |
| 215 |
* |
| 216 |
* @return void |
| 217 |
*/ |
| 218 |
function wpas_log_reply_trashed( $reply_id, $original_reply, $ticket_id ) { |
| 219 |
|
| 220 |
/* Do we log a summary or detail that includes the original content? */ |
| 221 |
if ( 'low' === wpas_get_option( 'log_content_edit_level', 'low' ) ) { |
| 222 |
$reply_contents_to_log = __( 'Original data not available because detailed logging is not turned on or allowed', 'awesome-support' ) ; |
| 223 |
} else { |
| 224 |
$reply_contents_to_log = $original_reply->post_content ; |
| 225 |
} |
| 226 |
|
| 227 |
// translators: %1$s is the reply number, %2$s is the ticket number. |
| 228 |
$x_content = __( 'Reply #%1$s was deleted from ticket #%2$s.', 'awesome-support' ); |
| 229 |
|
| 230 |
// Log it at the reply level |
| 231 |
wpas_log_edits( $reply_id, sprintf( $x_content, (string) $reply_id, (string) $original_reply->post_parent ), $reply_contents_to_log ); |
| 232 |
|
| 233 |
// Log it at the ticket level as well because if the reply gets permanently deleted, the only way to show it in the UI is at the ticket level |
| 234 |
wpas_log_edits( $original_reply->post_parent, sprintf( $x_content, (string) $reply_id, (string) $original_reply->post_parent ), $reply_contents_to_log ); |
| 235 |
|
| 236 |
} |