| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly |
| 4 |
} |
| 5 |
|
| 6 |
global $wpdb, $current_user, $wpscfunction; |
| 7 |
|
| 8 |
$args = array(); |
| 9 |
$params = $request->get_params(); |
| 10 |
$data = isset($params['fields_data']) ? wp_kses_post($params['fields_data']) : ''; |
| 11 |
$response = array(); |
| 12 |
|
| 13 |
if (!$data) { |
| 14 |
$response = new WP_Error( |
| 15 |
'invalid_api_parameters', |
| 16 |
'Invalid api parameters provided.', |
| 17 |
array( |
| 18 |
'status' => 403, |
| 19 |
) |
| 20 |
); |
| 21 |
return; |
| 22 |
} |
| 23 |
|
| 24 |
$fields_data = json_decode($data); |
| 25 |
|
| 26 |
// customer name |
| 27 |
if ( isset($fields_data->customer_name) ){ |
| 28 |
$args['customer_name'] = $fields_data->customer_name; |
| 29 |
} else if ( is_user_logged_in() ) { |
| 30 |
$args['customer_name'] = $current_user->display_name; |
| 31 |
} |
| 32 |
|
| 33 |
// customer email |
| 34 |
if ( isset($fields_data->customer_email) ){ |
| 35 |
$args['customer_email'] = $fields_data->customer_email; |
| 36 |
} else if ( is_user_logged_in() ) { |
| 37 |
$args['customer_email'] = $current_user->user_email; |
| 38 |
} |
| 39 |
|
| 40 |
// error if there is no user found |
| 41 |
if ( !isset($args['customer_name']) || !isset($args['customer_email']) ) { |
| 42 |
$response = new WP_Error( |
| 43 |
'user_not_found', |
| 44 |
'Either user name or email not given.', |
| 45 |
array( |
| 46 |
'status' => 403, |
| 47 |
) |
| 48 |
); |
| 49 |
return; |
| 50 |
} |
| 51 |
|
| 52 |
// check agent created |
| 53 |
if ( is_user_logged_in() && $current_user->has_cap('wpsc_agent') && $args['customer_email'] != $current_user->user_email ) { |
| 54 |
$args['agent_created'] = $current_user->ID; |
| 55 |
} |
| 56 |
|
| 57 |
// Ticket subject |
| 58 |
$subject = get_term_by('slug', 'ticket_subject', 'wpsc_ticket_custom_fields' ); |
| 59 |
$wpsc_tf_limit = get_term_meta( $subject->term_id,'wpsc_tf_limit',true); |
| 60 |
$ticket_subject = isset($fields_data->ticket_subject) ? $fields_data->ticket_subject : ''; |
| 61 |
|
| 62 |
if($wpsc_tf_limit){ |
| 63 |
$ticket_subject = substr($ticket_subject,0,$wpsc_tf_limit); |
| 64 |
} |
| 65 |
|
| 66 |
$args['ticket_subject'] = $ticket_subject; |
| 67 |
|
| 68 |
// Ticket description |
| 69 |
$ticket_description = isset($fields_data->ticket_description) ? wp_kses_post(htmlspecialchars_decode($fields_data->ticket_description, ENT_QUOTES)) : ''; |
| 70 |
//$ticket_description = str_replace(' ', ' ', $ticket_description); |
| 71 |
|
| 72 |
$args['ticket_description'] = $ticket_description; |
| 73 |
$wpsc_guest_can_upload_files = get_option('wpsc_guest_can_upload_files'); |
| 74 |
if(is_user_logged_in() || $wpsc_guest_can_upload_files ){ |
| 75 |
$description_attachment = isset($fields_data->desc_attachment) ? $wpscfunction->sanitize_array($fields_data->desc_attachment) : array(); |
| 76 |
if($description_attachment) $args['desc_attachment'] = $description_attachment; |
| 77 |
} |
| 78 |
|
| 79 |
// Ticket category |
| 80 |
if( isset($fields_data->ticket_category) && intval($fields_data->ticket_category) > 0 && $wpscfunction->is_category($fields_data->ticket_category) ){ |
| 81 |
$args['ticket_category'] = $fields_data->ticket_category; |
| 82 |
} |
| 83 |
|
| 84 |
// Ticket priority |
| 85 |
if( isset($fields_data->ticket_priority) && intval($fields_data->ticket_priority) > 0 && $wpscfunction->is_priority($fields_data->ticket_priority) ){ |
| 86 |
$args['ticket_priority'] = $fields_data->ticket_priority; |
| 87 |
} |
| 88 |
|
| 89 |
// Custom fields |
| 90 |
$fields = get_terms([ |
| 91 |
'taxonomy' => 'wpsc_ticket_custom_fields', |
| 92 |
'hide_empty' => false, |
| 93 |
'orderby' => 'meta_value_num', |
| 94 |
'meta_key' => 'wpsc_tf_load_order', |
| 95 |
'order' => 'ASC', |
| 96 |
'meta_query' => array( |
| 97 |
'relation' => 'AND', |
| 98 |
array( |
| 99 |
'key' => 'agentonly', |
| 100 |
'value' => array(0,1), |
| 101 |
'compare' => 'IN' |
| 102 |
), |
| 103 |
array( |
| 104 |
'key' => 'wpsc_tf_type', |
| 105 |
'value' => '0', |
| 106 |
'compare' => '>' |
| 107 |
), |
| 108 |
), |
| 109 |
]); |
| 110 |
|
| 111 |
foreach ($fields as $field) { |
| 112 |
|
| 113 |
$tf_type = get_term_meta( $field->term_id, 'wpsc_tf_type', true); |
| 114 |
|
| 115 |
switch ($tf_type) { |
| 116 |
|
| 117 |
case '1': |
| 118 |
case '2': |
| 119 |
case '6': |
| 120 |
case '7': |
| 121 |
case '8': |
| 122 |
case '18': |
| 123 |
case '21': |
| 124 |
$text = isset($fields_data->{$field->slug}) ? sanitize_text_field($fields_data->{$field->slug}) : ''; |
| 125 |
if($text) $args[$field->slug] = $text; |
| 126 |
break; |
| 127 |
|
| 128 |
case '3': |
| 129 |
$arrVal = isset($fields_data->{$field->slug}) ? $fields_data->{$field->slug} : array(); |
| 130 |
foreach ( $arrVal as $val) { |
| 131 |
if ( $wpscfunction->is_cf_option($field->term_id,$val) ) { |
| 132 |
$args[$field->slug][] = sanitize_text_field($val); |
| 133 |
} |
| 134 |
} |
| 135 |
break; |
| 136 |
|
| 137 |
case '4': |
| 138 |
$text = isset($fields_data->{$field->slug}) ? sanitize_text_field($fields_data->{$field->slug}) : ''; |
| 139 |
$args[$field->slug] = $text && $wpscfunction->is_cf_option($field->term_id,$text) ? $text : ''; |
| 140 |
break; |
| 141 |
|
| 142 |
case '10': |
| 143 |
$arrVal = isset($fields_data->{$field->slug}) ? $fields_data->{$field->slug} : array(); |
| 144 |
if($arrVal) $args[$field->slug] = $wpscfunction->sanitize_array($arrVal); |
| 145 |
break; |
| 146 |
|
| 147 |
case '5': |
| 148 |
$text = isset($fields_data->{$field->slug}) ? wp_kses_post(htmlspecialchars_decode($fields_data->{$field->slug}, ENT_QUOTES)) : ''; |
| 149 |
if($text) $args[$field->slug] = $text; |
| 150 |
break; |
| 151 |
|
| 152 |
case '9': |
| 153 |
$number = isset($fields_data->{$field->slug}) ? intval($fields_data->{$field->slug}) : ''; |
| 154 |
if($number) $args[$field->slug] = $number; |
| 155 |
break; |
| 156 |
|
| 157 |
default: |
| 158 |
$args = apply_filters('wpsc_after_create_ticket_custom_field', $args, $field, $tf_type ); |
| 159 |
break; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
$args = apply_filters( 'wpsc_before_create_ticket_args', $args); |
| 164 |
|
| 165 |
$ticket_id = $wpscfunction->create_ticket($args); |
| 166 |
|
| 167 |
if ( $ticket_id > 0 ) { |
| 168 |
|
| 169 |
$response = array( |
| 170 |
'status' => 200, |
| 171 |
'ticket_id' => $ticket_id |
| 172 |
); |
| 173 |
|
| 174 |
} else { |
| 175 |
|
| 176 |
$response = new WP_Error( |
| 177 |
'operation_failed', |
| 178 |
'Ticket not created.', |
| 179 |
array( |
| 180 |
'status' => 403, |
| 181 |
) |
| 182 |
); |
| 183 |
|
| 184 |
} |
| 185 |
|