| 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 |
|
| 13 |
// Customer name |
| 14 |
$customer_name = isset($args['customer_name']) ? sanitize_text_field($args['customer_name']) : ''; |
| 15 |
|
| 16 |
// Customer email |
| 17 |
$customer_email = isset($args['customer_email']) ? sanitize_email($args['customer_email']) : ''; |
| 18 |
|
| 19 |
// Subject |
| 20 |
$ticket_subject = isset($args['ticket_subject']) ? sanitize_text_field($args['ticket_subject']) : apply_filters( 'wpsc_default_subject_text', __('NA','supportcandy') ); |
| 21 |
|
| 22 |
// Category |
| 23 |
$default_category = get_option('wpsc_default_ticket_category'); |
| 24 |
$ticket_category = isset($args['ticket_category']) ? intval($args['ticket_category']) : $default_category; |
| 25 |
|
| 26 |
// Priority |
| 27 |
$default_priority = get_option('wpsc_default_ticket_priority'); |
| 28 |
|
| 29 |
$user_data = get_user_by('email' ,$customer_email); |
| 30 |
if($user_data){ |
| 31 |
$user_type = "user"; |
| 32 |
} |
| 33 |
else { |
| 34 |
$user_type = "guest"; |
| 35 |
} |
| 36 |
|
| 37 |
// Agent created |
| 38 |
$agent_created_value = 0; |
| 39 |
if ( !is_multisite() || !is_super_admin($current_user->ID)) { |
| 40 |
if ($current_user->has_cap('wpsc_agent') && $customer_email!=$current_user->user_email) { |
| 41 |
$agents = get_terms([ |
| 42 |
'taxonomy' => 'wpsc_agents', |
| 43 |
'hide_empty' => false, |
| 44 |
'meta_query' => array( |
| 45 |
'relation' => 'AND', |
| 46 |
array( |
| 47 |
'key' => 'user_id', |
| 48 |
'value' => $current_user->ID, |
| 49 |
'compare' => '=' |
| 50 |
) |
| 51 |
), |
| 52 |
]); |
| 53 |
if(!$agents) die(); |
| 54 |
$agent_created_value = $agents[0]->term_id; |
| 55 |
} else { |
| 56 |
$agent_created_value = 0; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
$values = array( |
| 61 |
'ticket_status' => $default_status, |
| 62 |
'customer_name' => $customer_name, |
| 63 |
'customer_email' => $customer_email, |
| 64 |
'ticket_subject' => $ticket_subject, |
| 65 |
'user_type' => $user_type, |
| 66 |
'ticket_category' => $default_category, |
| 67 |
'ticket_priority' => $default_priority, |
| 68 |
'date_created' => date("Y-m-d H:i:s"), |
| 69 |
'date_updated' => date("Y-m-d H:i:s"), |
| 70 |
'ip_address' => ' ', |
| 71 |
'agent_created' => $agent_created_value, |
| 72 |
'ticket_auth_code' => $this->getRandomString(10), |
| 73 |
'active' => '1' |
| 74 |
); |
| 75 |
if(!$wpsc_ticket_id_type){ |
| 76 |
$id = 0; |
| 77 |
$id_range = $wpscfunction->get_ticket_id_range(); |
| 78 |
do { |
| 79 |
$id = rand($id_range['start_id'], $id_range['end_id']); |
| 80 |
$sql = "select id from {$wpdb->prefix}wpsc_ticket where id=" . $id; |
| 81 |
$result = $wpdb->get_var($sql); |
| 82 |
} while ($result); |
| 83 |
$values['id'] = $id; |
| 84 |
} |
| 85 |
$wpdb->insert( $wpdb->prefix . 'wpsc_ticket', $values); |
| 86 |
|
| 87 |
$ticket_id = $wpdb->insert_id; |
| 88 |
|
| 89 |
$wpscfunction->add_ticket_meta($ticket_id,'assigned_agent',0); |
| 90 |
|
| 91 |
// Custom fields |
| 92 |
$fields = get_terms([ |
| 93 |
'taxonomy' => 'wpsc_ticket_custom_fields', |
| 94 |
'hide_empty' => false, |
| 95 |
'orderby' => 'meta_value_num', |
| 96 |
'meta_key' => 'wpsc_tf_load_order', |
| 97 |
'order' => 'ASC', |
| 98 |
'meta_query' => array( |
| 99 |
'relation' => 'AND', |
| 100 |
array( |
| 101 |
'key' => 'agentonly', |
| 102 |
'value' => array(0,1), |
| 103 |
'compare' => 'IN' |
| 104 |
), |
| 105 |
array( |
| 106 |
'key' => 'wpsc_tf_type', |
| 107 |
'value' => '0', |
| 108 |
'compare' => '>' |
| 109 |
), |
| 110 |
), |
| 111 |
]); |
| 112 |
|
| 113 |
foreach ($fields as $field) { |
| 114 |
$tf_type = get_term_meta( $field->term_id, 'wpsc_tf_type', true); |
| 115 |
switch ($tf_type) { |
| 116 |
case '1': |
| 117 |
case '2': |
| 118 |
case '4': |
| 119 |
case '5': |
| 120 |
case '7': |
| 121 |
case '8': |
| 122 |
case '9': |
| 123 |
$text = isset($args[$field->slug]) ? $args[$field->slug] : ''; |
| 124 |
$wpscfunction->add_ticket_meta($ticket_id, $field->slug,$text); |
| 125 |
break; |
| 126 |
|
| 127 |
case '3': |
| 128 |
case '10': |
| 129 |
$arrVal = isset($args[$field->slug]) && is_array($args[$field->slug]) ? $args[$field->slug] : array(); |
| 130 |
if($arrVal){ |
| 131 |
foreach ($arrVal as $key => $value) { |
| 132 |
$wpscfunction->add_ticket_meta($ticket_id, $field->slug,$value); |
| 133 |
update_term_meta ($value, 'active', '1'); |
| 134 |
} |
| 135 |
} |
| 136 |
break; |
| 137 |
case '6': |
| 138 |
case '18': |
| 139 |
case '21': |
| 140 |
$date = isset($args[$field->slug]) && $args[$field->slug] ? $args[$field->slug] : ''; |
| 141 |
if($date) { |
| 142 |
$wpscfunction->add_ticket_meta($ticket_id, $field->slug,$date); |
| 143 |
} |
| 144 |
break; |
| 145 |
|
| 146 |
default: |
| 147 |
do_action('wpsc_add_ticket_meta_custom_field',$ticket_id,$tf_type,$args,$field); |
| 148 |
break; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
// Description |
| 153 |
$ticket_description = isset($args['ticket_description']) ? $args['ticket_description'] : apply_filters( 'wpsc_default_description_text', __('NA','supportcandy') ); |
| 154 |
$description_attachment = isset($args['desc_attachment']) ? $args['desc_attachment'] : array(); |
| 155 |
$attachments = array(); |
| 156 |
if($description_attachment){ |
| 157 |
foreach ($description_attachment as $key => $value) { |
| 158 |
$attachment_id = intval($value); |
| 159 |
$attachments[] = $attachment_id; |
| 160 |
update_term_meta ($attachment_id, 'active', '1'); |
| 161 |
} |
| 162 |
} |
| 163 |
do_action( 'wpsc_ticket_created', $ticket_id ); |