| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly |
| 4 |
} |
| 5 |
|
| 6 |
global $current_user, $wpscfunction,$wpdb; |
| 7 |
|
| 8 |
$wpsc_ticket_id_type = get_option('wpsc_ticket_id_type'); |
| 9 |
|
| 10 |
// Ticket Status |
| 11 |
$default_status = get_option('wpsc_default_ticket_status'); |
| 12 |
$ticket_status = isset($args['ticket_status']) ? intval($args['ticket_status']) : $default_status; |
| 13 |
$ticket_status = apply_filters('wpsc_create_ticket_status', $ticket_status, $args); |
| 14 |
|
| 15 |
// Customer name |
| 16 |
$customer_name = isset($args['customer_name']) ? $args['customer_name'] : ''; |
| 17 |
|
| 18 |
// Customer email |
| 19 |
$customer_email = isset($args['customer_email']) ? sanitize_text_field($args['customer_email']) : ''; |
| 20 |
|
| 21 |
// Subject |
| 22 |
$subject = get_term_by('slug', 'ticket_subject', 'wpsc_ticket_custom_fields' ); |
| 23 |
$wpsc_default_sub = get_term_meta( $subject->term_id,'wpsc_tf_default_subject',true); |
| 24 |
$wpsc_tf_limit = get_term_meta( $subject->term_id,'wpsc_tf_limit',true); |
| 25 |
$ticket_subject = isset($args['ticket_subject']) ? sanitize_text_field($args['ticket_subject']) : apply_filters( 'wpsc_default_subject_text', $wpsc_default_sub,$args ); |
| 26 |
$ticket_subject = $wpscfunction->remove_emoji($ticket_subject); |
| 27 |
if($wpsc_tf_limit){ |
| 28 |
$ticket_subject = substr($args['ticket_subject'],0,$wpsc_tf_limit); |
| 29 |
} |
| 30 |
|
| 31 |
// Category |
| 32 |
$default_category = get_option('wpsc_default_ticket_category'); |
| 33 |
$ticket_category = isset($args['ticket_category']) ? intval($args['ticket_category']) : $default_category; |
| 34 |
$ticket_category = apply_filters('wpsc_create_ticket_category', $ticket_category, $args); |
| 35 |
|
| 36 |
// Priority |
| 37 |
$default_priority = get_option('wpsc_default_ticket_priority'); |
| 38 |
$ticket_priority = isset($args['ticket_priority']) ? intval($args['ticket_priority']) : $default_priority; |
| 39 |
$ticket_priority = apply_filters('wpsc_create_ticket_priority', $ticket_priority, $args); |
| 40 |
|
| 41 |
$reply_source = isset($args['reply_source']) ? sanitize_text_field($args['reply_source']) : 'browser'; |
| 42 |
|
| 43 |
$report_display = get_option('wpsc_report_raised_by_user'); |
| 44 |
|
| 45 |
$user_data = get_user_by('email' ,$customer_email); |
| 46 |
if($user_data){ |
| 47 |
$user_type = "user"; |
| 48 |
} |
| 49 |
else { |
| 50 |
$user_type = "guest"; |
| 51 |
} |
| 52 |
|
| 53 |
$agent_created = 0; |
| 54 |
|
| 55 |
if ( ($current_user->has_cap('wpsc_agent') && $customer_email!=$current_user->user_email && !is_multisite()) || ($current_user->has_cap('wpsc_agent') && is_multisite() && !is_super_admin($current_user->ID))) { |
| 56 |
$agents = get_terms([ |
| 57 |
'taxonomy' => 'wpsc_agents', |
| 58 |
'hide_empty' => false, |
| 59 |
'meta_query' => array( |
| 60 |
'relation' => 'AND', |
| 61 |
array( |
| 62 |
'key' => 'user_id', |
| 63 |
'value' => $current_user->ID, |
| 64 |
'compare' => '=' |
| 65 |
) |
| 66 |
), |
| 67 |
]); |
| 68 |
if(!$agents) die(); |
| 69 |
$agent_created = $agents[0]->term_id; |
| 70 |
} |
| 71 |
|
| 72 |
$ip_address = isset($_SERVER) && isset($_SERVER['REMOTE_ADDR'])?$_SERVER['REMOTE_ADDR']:''; |
| 73 |
if (strlen($ip_address)>28 || $ip_address == '::1') { |
| 74 |
$ip_address = ''; |
| 75 |
} |
| 76 |
|
| 77 |
$values = array( |
| 78 |
'ticket_status' => $ticket_status, |
| 79 |
'customer_name' => $customer_name, |
| 80 |
'customer_email' => $customer_email, |
| 81 |
'ticket_subject' => $ticket_subject, |
| 82 |
'user_type' => $user_type, |
| 83 |
'ticket_category' => $ticket_category, |
| 84 |
'ticket_priority' => $ticket_priority, |
| 85 |
'date_created' => date("Y-m-d H:i:s"), |
| 86 |
'date_updated' => date("Y-m-d H:i:s"), |
| 87 |
'ip_address' => $ip_address, |
| 88 |
'agent_created' => $agent_created, |
| 89 |
'ticket_auth_code' => $this->getRandomString(10), |
| 90 |
'active' => '1' |
| 91 |
); |
| 92 |
|
| 93 |
if(!$wpsc_ticket_id_type){ |
| 94 |
$id = 0; |
| 95 |
$id_range = $wpscfunction->get_ticket_id_range(); |
| 96 |
do { |
| 97 |
$id = rand($id_range['start_id'], $id_range['end_id']); |
| 98 |
$sql = "select id from {$wpdb->prefix}wpsc_ticket where id=" . $id; |
| 99 |
$result = $wpdb->get_var($sql); |
| 100 |
} while ($result); |
| 101 |
$values['id'] = $id; |
| 102 |
} |
| 103 |
|
| 104 |
$ticket_id = $wpscfunction->create_new_ticket($values); |
| 105 |
|
| 106 |
// Custom fields |
| 107 |
$fields = get_terms([ |
| 108 |
'taxonomy' => 'wpsc_ticket_custom_fields', |
| 109 |
'hide_empty' => false, |
| 110 |
'orderby' => 'meta_value_num', |
| 111 |
'meta_key' => 'wpsc_tf_load_order', |
| 112 |
'order' => 'ASC', |
| 113 |
'meta_query' => array( |
| 114 |
'relation' => 'AND', |
| 115 |
array( |
| 116 |
'key' => 'agentonly', |
| 117 |
'value' => '0', |
| 118 |
'compare' => '=' |
| 119 |
), |
| 120 |
array( |
| 121 |
'key' => 'wpsc_tf_type', |
| 122 |
'value' => '0', |
| 123 |
'compare' => '>' |
| 124 |
), |
| 125 |
), |
| 126 |
]); |
| 127 |
|
| 128 |
if($fields){ |
| 129 |
foreach ($fields as $field) { |
| 130 |
$tf_type = get_term_meta( $field->term_id, 'wpsc_tf_type', true); |
| 131 |
switch ($tf_type) { |
| 132 |
case '1': |
| 133 |
case '2': |
| 134 |
case '4': |
| 135 |
case '5': |
| 136 |
case '7': |
| 137 |
case '8': |
| 138 |
case '9': |
| 139 |
case '21': |
| 140 |
$text = isset($args[$field->slug]) ? $args[$field->slug] : ''; |
| 141 |
if($text){ |
| 142 |
$wpscfunction->add_ticket_meta($ticket_id, $field->slug,$text); |
| 143 |
} |
| 144 |
break; |
| 145 |
case '3': |
| 146 |
case '10': |
| 147 |
$arrVal = isset($args[$field->slug]) && is_array($args[$field->slug]) ? $args[$field->slug] : array(); |
| 148 |
if($arrVal){ |
| 149 |
foreach ($arrVal as $key => $value) { |
| 150 |
$wpscfunction->add_ticket_meta($ticket_id, $field->slug,$value); |
| 151 |
update_term_meta ($value, 'active', '1'); |
| 152 |
} |
| 153 |
} |
| 154 |
break; |
| 155 |
|
| 156 |
case '6': |
| 157 |
case '18': |
| 158 |
$date = isset($args[$field->slug]) && $args[$field->slug] ? $args[$field->slug] : ''; |
| 159 |
if($date) { |
| 160 |
$date = $wpscfunction->calenderDateFormatToDateTime($date); |
| 161 |
$wpscfunction->add_ticket_meta($ticket_id, $field->slug,$date); |
| 162 |
} |
| 163 |
break; |
| 164 |
|
| 165 |
default: |
| 166 |
do_action('wpsc_add_ticket_meta_custom_field',$ticket_id,$tf_type,$args,$field); |
| 167 |
break; |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
$assigned_agent = array(); |
| 173 |
$assigned_agent = apply_filters( 'wpsc_get_ticket_assigned_agent', $assigned_agent,$args,$ticket_id); |
| 174 |
|
| 175 |
if(!empty($assigned_agent)){ |
| 176 |
$wpscfunction->assign_agent( $ticket_id, array_unique($assigned_agent)); |
| 177 |
}else{ |
| 178 |
$wpscfunction->add_ticket_meta($ticket_id,'assigned_agent','0'); |
| 179 |
} |
| 180 |
|
| 181 |
$wpscfunction->add_ticket_meta($ticket_id,'prev_assigned_agent','0'); |
| 182 |
|
| 183 |
// Description |
| 184 |
$description = get_term_by('slug', 'ticket_description', 'wpsc_ticket_custom_fields' ); |
| 185 |
$wpsc_default_desc = get_term_meta( $description->term_id,'wpsc_tf_default_description',true); |
| 186 |
$ticket_description = isset($args['ticket_description']) ? $args['ticket_description'] : apply_filters( 'wpsc_default_description_text', $wpsc_default_desc ); |
| 187 |
$description_attachment = isset($args['desc_attachment']) ? $args['desc_attachment'] : array(); |
| 188 |
$attachments = array(); |
| 189 |
foreach ($description_attachment as $key => $value) { |
| 190 |
$attachment_id = intval($value); |
| 191 |
$attachments[] = $attachment_id; |
| 192 |
update_term_meta ($attachment_id, 'active', '1'); |
| 193 |
} |
| 194 |
|
| 195 |
$signature = ''; |
| 196 |
|
| 197 |
if($agent_created && $report_display == 'agent'){ |
| 198 |
$signature = get_user_meta($current_user->ID,'wpsc_agent_signature',true); |
| 199 |
}elseif($user_data && $user_data->has_cap('wpsc_agent')){ |
| 200 |
$signature = get_user_meta($user_data->ID,'wpsc_agent_signature',true); |
| 201 |
} |
| 202 |
if($signature){ |
| 203 |
$signature= stripcslashes(htmlspecialchars_decode($signature, ENT_QUOTES)); |
| 204 |
$ticket_description.= $signature; |
| 205 |
} |
| 206 |
|
| 207 |
$os_platform = $wpscfunction->get_os(); |
| 208 |
$browser = $wpscfunction->get_browser(); |
| 209 |
|
| 210 |
$user_seen = 'null'; |
| 211 |
if($current_user->user_email == $customer_email){ |
| 212 |
$user_seen = date("Y-m-d H:i:s"); |
| 213 |
} |
| 214 |
|
| 215 |
if($agent_created){ |
| 216 |
$agent_name = $current_user->display_name; |
| 217 |
$agent_email = $current_user->user_email; |
| 218 |
} |
| 219 |
|
| 220 |
// Save thread description |
| 221 |
$thread_args = array( |
| 222 |
'ticket_id' => $ticket_id, |
| 223 |
'reply_body' => $this->replace_macro($ticket_description,$ticket_id), |
| 224 |
'customer_name' => $agent_created && $report_display == 'agent' ? $agent_name : $customer_name, |
| 225 |
'customer_email' => $agent_created && $report_display == 'agent' ? $agent_email : $customer_email, |
| 226 |
'attachments' => $attachments, |
| 227 |
'thread_type' => 'report', |
| 228 |
'ip_address' => $ip_address, |
| 229 |
'reply_source' => $reply_source , |
| 230 |
'os' => $os_platform, |
| 231 |
'browser' => $browser, |
| 232 |
'user_seen' => $user_seen |
| 233 |
); |
| 234 |
|
| 235 |
$thread_args = apply_filters( 'wpsc_thread_args', $thread_args ); |
| 236 |
$thread_id = $this->submit_ticket_thread($thread_args); |
| 237 |
|
| 238 |
$wpsc_reg_guest_user_after_create_ticket = get_option('wpsc_reg_guest_user_after_create_ticket'); |
| 239 |
|
| 240 |
if($wpsc_reg_guest_user_after_create_ticket && !email_exists($customer_email) ) { |
| 241 |
$random_password = wp_generate_password( $length=12, $include_standard_special_chars=false ); |
| 242 |
$user_id = wp_create_user( $customer_name, $random_password, $customer_email ); |
| 243 |
$creds = array( |
| 244 |
'user_login' => $customer_name, |
| 245 |
'user_password' => $random_password, |
| 246 |
); |
| 247 |
wp_new_user_notification($user_id,null,'both'); |
| 248 |
|
| 249 |
if (!$agent_created) { |
| 250 |
wp_signon($creds, false); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
do_action( 'wpsc_ticket_created', $ticket_id ); |
| 255 |
|