| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly |
| 4 |
} |
| 5 |
|
| 6 |
global $current_user,$wpscfunction; |
| 7 |
|
| 8 |
if ( |
| 9 |
check_ajax_referer('set_thread_ticket', '_ajax_nonce', false) != 1 |
| 10 |
) wp_send_json_error('Unauthorised request!', 400); |
| 11 |
|
| 12 |
if (!($current_user->ID && $current_user->has_cap('wpsc_agent'))) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
$ticket_id = isset($_POST['ticket_id']) ? sanitize_text_field($_POST['ticket_id']) : '' ; |
| 17 |
$subject = isset($_POST['subject']) ? sanitize_text_field($_POST['subject']) : ''; |
| 18 |
$thread_id = isset($_POST['thread_id']) ? sanitize_text_field($_POST['thread_id']) : '' ; |
| 19 |
|
| 20 |
// Ticket Status |
| 21 |
$default_status = get_option('wpsc_default_ticket_status'); |
| 22 |
|
| 23 |
// Category |
| 24 |
$default_category = get_option('wpsc_default_ticket_category'); |
| 25 |
|
| 26 |
// Priority |
| 27 |
$default_priority = get_option('wpsc_default_ticket_priority'); |
| 28 |
|
| 29 |
$ticket = $wpscfunction->get_ticket($ticket_id); |
| 30 |
|
| 31 |
$tickets = array(); |
| 32 |
foreach ($ticket as $key => $value) { |
| 33 |
$tickets[$key] = $value; |
| 34 |
} |
| 35 |
|
| 36 |
$content_post = get_post($thread_id); |
| 37 |
$thread_body = $content_post->post_content; |
| 38 |
|
| 39 |
$values = array( |
| 40 |
'ticket_status' => $default_status, |
| 41 |
'customer_name' => $tickets['customer_name'], |
| 42 |
'customer_email' => $tickets['customer_email'], |
| 43 |
'ticket_subject' => $subject, |
| 44 |
'user_type' => $tickets['user_type'], |
| 45 |
'ticket_category' => $default_category, |
| 46 |
'ticket_priority' => $default_priority, |
| 47 |
'date_created' => date("Y-m-d H:i:s"), |
| 48 |
'date_updated' => date("Y-m-d H:i:s"), |
| 49 |
'ip_address' => $tickets['ip_address'], |
| 50 |
'agent_created' => 1, |
| 51 |
'ticket_auth_code' => $wpscfunction->getRandomString(10), |
| 52 |
'active' => '1', |
| 53 |
'ticket_description'=> $thread_body, |
| 54 |
); |
| 55 |
|
| 56 |
$custom_fields = get_terms([ |
| 57 |
'taxonomy' => 'wpsc_ticket_custom_fields', |
| 58 |
'hide_empty' => false, |
| 59 |
'orderby' => 'meta_value_num', |
| 60 |
'meta_key' => 'wpsc_tf_visibility', |
| 61 |
'order' => 'ASC', |
| 62 |
'meta_query' => array( |
| 63 |
array( |
| 64 |
'key' => 'agentonly', |
| 65 |
'value' => '0', |
| 66 |
'compare' => '=' |
| 67 |
) |
| 68 |
), |
| 69 |
]); |
| 70 |
|
| 71 |
if($custom_fields){ |
| 72 |
foreach ($custom_fields as $field) { |
| 73 |
$wpsc_tf_type = get_term_meta( $field->term_id, 'wpsc_tf_type',true); |
| 74 |
|
| 75 |
if($wpsc_tf_type == 3 || $wpsc_tf_type==10){ |
| 76 |
$value = $wpscfunction->get_ticket_meta($ticket_id,$field->slug); |
| 77 |
} |
| 78 |
else { |
| 79 |
$value = $wpscfunction->get_ticket_meta($ticket_id,$field->slug,true); |
| 80 |
} |
| 81 |
if($wpsc_tf_type == 6){ |
| 82 |
$date = $wpscfunction->get_ticket_meta($ticket_id,$field->slug,true); |
| 83 |
$value = $date ? date('Y-m-d', strtotime($date)):''; |
| 84 |
} |
| 85 |
if($value){ |
| 86 |
$values[$field->slug]=$value; |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
$new_ticket_id = $wpscfunction->create_ticket($values); |
| 92 |
|
| 93 |
// Assigned agents |
| 94 |
$agents = $wpscfunction->get_ticket_meta($ticket_id,'assigned_agent'); |
| 95 |
|
| 96 |
if($agents[0]){ |
| 97 |
$wpscfunction->assign_agent( $new_ticket_id, $agents); |
| 98 |
} |
| 99 |
|
| 100 |
//Agent fields |
| 101 |
$fields = get_terms([ |
| 102 |
'taxonomy' => 'wpsc_ticket_custom_fields', |
| 103 |
'hide_empty' => false, |
| 104 |
'orderby' => 'meta_value_num', |
| 105 |
'meta_key' => 'wpsc_tf_load_order', |
| 106 |
'order' => 'ASC', |
| 107 |
'meta_query' => array( |
| 108 |
array( |
| 109 |
'key' => 'agentonly', |
| 110 |
'value' => '1', |
| 111 |
'compare' => '=' |
| 112 |
) |
| 113 |
), |
| 114 |
]); |
| 115 |
|
| 116 |
foreach ($fields as $field) { |
| 117 |
$tf_type = get_term_meta( $field->term_id, 'wpsc_tf_type', true); |
| 118 |
switch ($tf_type) { |
| 119 |
case '1': |
| 120 |
case '2': |
| 121 |
case '4': |
| 122 |
case '5': |
| 123 |
case '6': |
| 124 |
case '7': |
| 125 |
case '8': |
| 126 |
case '9': |
| 127 |
case '11': |
| 128 |
case '12': |
| 129 |
case '13': |
| 130 |
case '14': |
| 131 |
$text = $wpscfunction->get_ticket_meta($ticket_id,$field->slug,true); |
| 132 |
$wpscfunction->add_ticket_meta($new_ticket_id, $field->slug,$text); |
| 133 |
break; |
| 134 |
|
| 135 |
case '3': |
| 136 |
case '10': |
| 137 |
$arrVal = $wpscfunction->get_ticket_meta($ticket_id,$field->slug); |
| 138 |
if($arrVal){ |
| 139 |
foreach ($arrVal as $key => $value) { |
| 140 |
$wpscfunction->add_ticket_meta( $new_ticket_id, $field->slug, $value ); |
| 141 |
update_term_meta ($value, 'active', '1'); |
| 142 |
} |
| 143 |
}else { |
| 144 |
$wpscfunction->add_ticket_meta( $new_ticket_id, $field->slug, '' ); |
| 145 |
} |
| 146 |
break; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
echo json_encode($new_ticket_id); |
| 151 |
?> |