| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly |
| 4 |
} |
| 5 |
|
| 6 |
global $wpscfunction, $current_user,$wpdb; |
| 7 |
|
| 8 |
$params = $request->get_params(); |
| 9 |
|
| 10 |
// Get tiket id |
| 11 |
$ticket_id = isset( $params['id'] ) ? sanitize_text_field($params['id']) : 0; |
| 12 |
$data = isset( $params['fields_data']) ? sanitize_text_field($params['fields_data']) : ''; |
| 13 |
|
| 14 |
$response = array(); |
| 15 |
|
| 16 |
if (!$data) { |
| 17 |
$response = new WP_Error( |
| 18 |
'invalid_api_parameters', |
| 19 |
'Invalid api parameters provided.', |
| 20 |
array( |
| 21 |
'status' => 403, |
| 22 |
) |
| 23 |
); |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
$fields_data = json_decode($data); |
| 28 |
$ticket_data = $wpscfunction->get_ticket($ticket_id); |
| 29 |
|
| 30 |
if (! $ticket_data) { |
| 31 |
$response = new WP_Error( |
| 32 |
'unauthorized', |
| 33 |
'Ticket not found.', |
| 34 |
array( |
| 35 |
'status' => 403, |
| 36 |
) |
| 37 |
); |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
// Changed raised by |
| 42 |
if ( isset($fields_data->customer_name) || isset($fields_data->customer_email) ){ |
| 43 |
$old_customer_name = $ticket_data['customer_name']; |
| 44 |
$old_customer_email = $ticket_data['customer_email']; |
| 45 |
$customer_name = isset($fields_data->customer_name) && $fields_data->customer_name != $old_customer_name ? $fields_data->customer_name : $ticket_data['customer_name']; |
| 46 |
$customer_email = isset($fields_data->customer_email) && $fields_data->customer_email != $old_customer_email ? $fields_data->customer_email : $ticket_data['customer_email']; |
| 47 |
|
| 48 |
if ($wpscfunction->has_permission('change_raised_by',$ticket_id) && ($fields_data->customer_name != $old_customer_name || $fields_data->customer_email != $old_customer_email )){ |
| 49 |
$wpscfunction->change_raised_by($ticket_id, $customer_name, $customer_email); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
// Change ticket subject |
| 54 |
if ( $wpscfunction->has_permission('change_ticket_fields',$ticket_id) && isset($fields_data->ticket_subject)){ |
| 55 |
$vals = array('ticket_subject' => $fields_data->ticket_subject); |
| 56 |
$wpdb->update($wpdb->prefix.'wpsc_ticket', $vals, array('id'=>$ticket_id)); |
| 57 |
} |
| 58 |
|
| 59 |
// Change ticket status |
| 60 |
if( isset($fields_data->ticket_status) && $wpscfunction->has_permission('change_status',$ticket_id) && intval($fields_data->ticket_status) > 0 && $wpscfunction->is_status($fields_data->ticket_status) && $fields_data->ticket_status != $ticket_data['ticket_status'] ){ |
| 61 |
$wpscfunction->change_status( $ticket_id, $fields_data->ticket_status); |
| 62 |
} |
| 63 |
|
| 64 |
// Change ticket category |
| 65 |
if( isset($fields_data->ticket_category) && $wpscfunction->has_permission('change_status',$ticket_id) && intval($fields_data->ticket_category) > 0 && $wpscfunction->is_category($fields_data->ticket_category) && $fields_data->ticket_category != $ticket_data['ticket_category'] ){ |
| 66 |
$wpscfunction->change_category( $ticket_id, $fields_data->ticket_category); |
| 67 |
} |
| 68 |
|
| 69 |
// Change ticket priority |
| 70 |
if( isset($fields_data->ticket_priority) && $wpscfunction->has_permission('change_status',$ticket_id) && intval($fields_data->ticket_priority) > 0 && $wpscfunction->is_priority($fields_data->ticket_priority) && $fields_data->ticket_priority != $ticket_data['ticket_priority'] ){ |
| 71 |
$wpscfunction->change_priority( $ticket_id, $fields_data->ticket_priority); |
| 72 |
} |
| 73 |
|
| 74 |
// Assign agent to ticket |
| 75 |
$agents = isset($fields_data->assigned_agent) && is_array($fields_data->assigned_agent) ? $fields_data->assigned_agent : array() ; |
| 76 |
$old_assigned_agents = $wpscfunction->get_ticket_meta($ticket_id,'assigned_agent'); |
| 77 |
$assigned_agents = array(); |
| 78 |
|
| 79 |
foreach( $agents as $agent ){ |
| 80 |
$agent = intval($agent) ? intval($agent) : 0; |
| 81 |
if ($agent){ |
| 82 |
$assigned_agents[] = $agent; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
$assign_agent = array_unique($assigned_agents); |
| 87 |
|
| 88 |
if( $agents && $wpscfunction->has_permission('assign_agent',$ticket_id) && intval($fields_data->assigned_agent) > 0 && $wpscfunction->is_agent($assign_agent) &&($old_assigned_agents != $assign_agent)){ |
| 89 |
$wpscfunction->assign_agent( $ticket_id, $assign_agent); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
$fields = get_terms([ |
| 94 |
'taxonomy' => 'wpsc_ticket_custom_fields', |
| 95 |
'hide_empty' => false, |
| 96 |
'orderby' => 'meta_value_num', |
| 97 |
'meta_key' => 'wpsc_tf_load_order', |
| 98 |
'order' => 'ASC', |
| 99 |
'meta_query' => array( |
| 100 |
'relation' => 'AND', |
| 101 |
array( |
| 102 |
'key' => 'agentonly', |
| 103 |
'value' => array(0,1), |
| 104 |
'compare' => 'IN' |
| 105 |
), |
| 106 |
array( |
| 107 |
'key' => 'wpsc_tf_type', |
| 108 |
'value' => '0', |
| 109 |
'compare' => '>' |
| 110 |
), |
| 111 |
), |
| 112 |
]); |
| 113 |
|
| 114 |
foreach ($fields as $field ) { |
| 115 |
|
| 116 |
$wpsc_tf_type = get_term_meta( $field->term_id, 'wpsc_tf_type', true); |
| 117 |
$agentonly = get_term_meta( $field->term_id, 'agentonly', true); |
| 118 |
|
| 119 |
switch ( $wpsc_tf_type ) { |
| 120 |
|
| 121 |
case '1': |
| 122 |
case '2': |
| 123 |
case '4': |
| 124 |
case '7': |
| 125 |
case '8': |
| 126 |
case '18': |
| 127 |
$oldVal = $wpscfunction->get_ticket_meta( $ticket_id, $field->slug, true); |
| 128 |
$newVal = isset($fields_data->{$field->slug}) ? sanitize_text_field($fields_data->{$field->slug}) : ''; |
| 129 |
if ( isset($fields_data->{$field->slug}) && (($agentonly == 0 && $wpscfunction->has_permission('change_ticket_fields',$ticket_id)) || ($agentonly == 1 && $wpscfunction->has_permission('change_agentonly_fields',$ticket_id)) ) && $oldVal != $newVal ) { |
| 130 |
$wpscfunction->change_field( $ticket_id, $field->slug, $newVal ); |
| 131 |
} |
| 132 |
break; |
| 133 |
|
| 134 |
case '3': |
| 135 |
$oldVal = $wpscfunction->get_ticket_meta( $ticket_id, $field->slug ); |
| 136 |
$newVal = isset($fields_data->{$field->slug}) && $fields_data->{$field->slug} ? $wpscfunction->sanitize_array($fields_data->{$field->slug}) : array(); |
| 137 |
if ( isset($fields_data->{$field->slug}) && (($agentonly == 0 && $wpscfunction->has_permission('change_ticket_fields',$ticket_id)) || ($agentonly == 1 && $wpscfunction->has_permission('change_agentonly_fields',$ticket_id)) ) && $oldVal != $newVal ) { |
| 138 |
$wpscfunction->change_field( $ticket_id, $field->slug, $newVal ); |
| 139 |
} |
| 140 |
break; |
| 141 |
|
| 142 |
case '5': |
| 143 |
$oldVal = $wpscfunction->get_ticket_meta( $ticket_id, $field->slug, true); |
| 144 |
$newVal = isset($fields_data->{$field->slug}) ? sanitize_textarea_field($fields_data->{$field->slug}) : ''; |
| 145 |
if ( isset($fields_data->{$field->slug}) && (($agentonly == 0 && $wpscfunction->has_permission('change_ticket_fields',$ticket_id)) || ($agentonly == 1 && $wpscfunction->has_permission('change_agentonly_fields',$ticket_id)) ) && $oldVal != $newVal ) { |
| 146 |
$wpscfunction->change_field( $ticket_id, $field->slug, $newVal ); |
| 147 |
} |
| 148 |
break; |
| 149 |
|
| 150 |
case '6': |
| 151 |
|
| 152 |
$oldVal = $wpscfunction->datetimeToCalenderFormat($wpscfunction->get_ticket_meta( $ticket_id, $field->slug, true)); |
| 153 |
$newVal = isset($fields_data->{$field->slug}) ? sanitize_text_field($fields_data->{$field->slug}) : ''; |
| 154 |
$label = get_term_meta( $field->term_id, 'wpsc_tf_label',true); |
| 155 |
|
| 156 |
if ( isset($fields_data->{$field->slug}) && (($agentonly == 0 && $wpscfunction->has_permission('change_ticket_fields',$ticket_id)) || ($agentonly == 1 && $wpscfunction->has_permission('change_agentonly_fields',$ticket_id)) ) && $oldVal != $newVal ) { |
| 157 |
|
| 158 |
$fields_value_new = $wpscfunction->calenderDateFormatToDateTime($newVal); |
| 159 |
|
| 160 |
if ( $oldVal && !$newVal ) { |
| 161 |
$wpscfunction->delete_ticket_meta($ticket_id,$field->slug); |
| 162 |
$log_str = sprintf( __('%1$s removed %2$s','supportcandy'), '<strong>'.$current_user->display_name.'</strong>','<strong>'.$label.'</strong>' ); |
| 163 |
} else if( !$oldVal && $newVal ){ |
| 164 |
$wpscfunction->add_ticket_meta( $ticket_id, $field->slug, $fields_value_new ); |
| 165 |
$log_str = sprintf( __('%1$s changed %2$s to %3$s','supportcandy'), '<strong>'.$current_user->display_name.'</strong>','<strong>'.$label.'</strong>','<strong>'.$newVal.'</strong>'); |
| 166 |
} else { |
| 167 |
$wpscfunction->update_ticket_meta($ticket_id,$field->slug,array('meta_value' => $fields_value_new)); |
| 168 |
$log_str = sprintf( __('%1$s changed %2$s from %3$s to %4$s','supportcandy'), '<strong>'.$current_user->display_name.'</strong>','<strong>'.$label.'</strong>','<strong>'.$oldVal.'</strong>','<strong>'.$newVal.'</strong>'); |
| 169 |
} |
| 170 |
|
| 171 |
$args = array( |
| 172 |
'ticket_id' => $ticket_id, |
| 173 |
'reply_body' => $log_str, |
| 174 |
'thread_type' => 'log' |
| 175 |
); |
| 176 |
$args = apply_filters( 'wpsc_thread_args', $args ); |
| 177 |
$wpscfunction->submit_ticket_thread($args); |
| 178 |
|
| 179 |
} |
| 180 |
break; |
| 181 |
|
| 182 |
case '9': |
| 183 |
$oldVal = $wpscfunction->get_ticket_meta( $ticket_id, $field->slug, true); |
| 184 |
$newVal = isset($fields_data->{$field->slug}) ? intval($fields_data->{$field->slug}) : ''; |
| 185 |
if ( isset($fields_data->{$field->slug}) && (($agentonly == 0 && $wpscfunction->has_permission('change_ticket_fields',$ticket_id)) || ($agentonly == 1 && $wpscfunction->has_permission('change_agentonly_fields',$ticket_id)) ) && $oldVal != $newVal ) { |
| 186 |
$wpscfunction->change_field( $ticket_id, $field->slug, $newVal ); |
| 187 |
} |
| 188 |
break; |
| 189 |
|
| 190 |
case '10': |
| 191 |
$oldVal = $wpscfunction->get_ticket_meta( $ticket_id, $field->slug ); |
| 192 |
$newVal = isset($fields_data->{$field->slug}) && $fields_data->{$field->slug} ? $wpscfunction->sanitize_array($fields_data->{$field->slug}) : array(); |
| 193 |
if ( isset($fields_data->{$field->slug}) && (($agentonly == 0 && $wpscfunction->has_permission('change_ticket_fields',$ticket_id)) || ($agentonly == 1 && $wpscfunction->has_permission('change_agentonly_fields',$ticket_id)) ) && $oldVal != $newVal ) { |
| 194 |
foreach ($newVal as $key => $value) { |
| 195 |
$attachment_id = intval($value); |
| 196 |
update_term_meta ($attachment_id, 'active', '1'); |
| 197 |
} |
| 198 |
$wpscfunction->change_field( $ticket_id, $field->slug, $newVal ); |
| 199 |
} |
| 200 |
break; |
| 201 |
|
| 202 |
case '21': |
| 203 |
$time_format = get_term_meta($field->term_id,'wpsc_time_format',true); |
| 204 |
if($time_format == '12'){ |
| 205 |
$oldVal = trim($wpscfunction->get_ticket_meta($ticket_id, $field->slug, true)); |
| 206 |
if($oldVal){ |
| 207 |
$oldVal = date("h:i:s a", strtotime($wpscfunction->get_ticket_meta( $ticket_id, $field->slug, true))); |
| 208 |
} |
| 209 |
$newVal = isset($fields_data->{$field->slug}) ? trim(sanitize_text_field($fields_data->{$field->slug})) : ''; |
| 210 |
if ( isset($fields_data->{$field->slug}) && (($agentonly == 0 && $wpscfunction->has_permission('change_ticket_fields',$ticket_id)) || ($agentonly == 1 && $wpscfunction->has_permission('change_agentonly_fields',$ticket_id)) ) && $oldVal != $newVal) { |
| 211 |
$wpscfunction->change_field( $ticket_id, $field->slug,$newVal ); |
| 212 |
} |
| 213 |
}else{ |
| 214 |
$oldVal = trim($wpscfunction->get_ticket_meta( $ticket_id, $field->slug, true)); |
| 215 |
$newVal = isset($fields_data->{$field->slug}) ? trim(sanitize_text_field($fields_data->{$field->slug})) : ''; |
| 216 |
if ( isset($fields_data->{$field->slug}) && (($agentonly == 0 && $wpscfunction->has_permission('change_ticket_fields',$ticket_id)) || ($agentonly == 1 && $wpscfunction->has_permission('change_agentonly_fields',$ticket_id)) ) && $oldVal != $newVal) { |
| 217 |
$wpscfunction->change_field( $ticket_id, $field->slug,$newVal ); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
break; |
| 222 |
|
| 223 |
default: |
| 224 |
do_action( 'wpsc_set_change_ticket_field', $fields_data, $field, $ticket_id, $wpsc_tf_type ); |
| 225 |
|
| 226 |
} |
| 227 |
|
| 228 |
} |
| 229 |
|
| 230 |
$wpdb->update($wpdb->prefix.'wpsc_ticket',array('date_updated'=> date("Y-m-d H:i:s") ),array('id'=>$ticket_id)); |
| 231 |
|
| 232 |
$response = array( |
| 233 |
'status' => 200, |
| 234 |
'message' => "Field(s) updated successfully!" |
| 235 |
); |
| 236 |
|