| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_ITW_Ticket_Fields' ) ) : |
| 7 |
|
| 8 |
final class WPSC_ITW_Ticket_Fields { |
| 9 |
|
| 10 |
/** |
| 11 |
* Ignore ticket custom field types for ticket fields |
| 12 |
* |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
public static $ignore_cft = array(); |
| 16 |
|
| 17 |
/** |
| 18 |
* Initialize this class |
| 19 |
* |
| 20 |
* @return void |
| 21 |
*/ |
| 22 |
public static function init() { |
| 23 |
|
| 24 |
// ignore cft. |
| 25 |
add_action( 'init', array( __CLASS__, 'ignore_cft' ) ); |
| 26 |
|
| 27 |
// get edit ticket fields. |
| 28 |
add_action( 'wp_ajax_wpsc_it_get_edit_ticket_fields', array( __CLASS__, 'it_get_edit_ticket_fields' ) ); |
| 29 |
add_action( 'wp_ajax_nopriv_wpsc_it_get_edit_ticket_fields', array( __CLASS__, 'it_get_edit_ticket_fields' ) ); |
| 30 |
add_action( 'wp_ajax_wpsc_it_set_edit_ticket_fields', array( __CLASS__, 'it_set_edit_ticket_fields' ) ); |
| 31 |
add_action( 'wp_ajax_nopriv_wpsc_it_set_edit_ticket_fields', array( __CLASS__, 'it_set_edit_ticket_fields' ) ); |
| 32 |
|
| 33 |
// ticket fields. |
| 34 |
add_action( 'wp_ajax_wpsc_get_tw_ticket_fields', array( __CLASS__, 'get_tw_ticket_fields' ) ); |
| 35 |
add_action( 'wp_ajax_wpsc_set_tw_ticket_fields', array( __CLASS__, 'set_tw_ticket_fields' ) ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Set ignore custom field types for ticket fields |
| 40 |
* |
| 41 |
* @return void |
| 42 |
*/ |
| 43 |
public static function ignore_cft() { |
| 44 |
|
| 45 |
self::$ignore_cft = apply_filters( 'wpsc_ignore_edit_ticket_field_cft', array( 'cf_html' ) ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Prints body of current widget |
| 50 |
* |
| 51 |
* @param WPSC_Ticket $ticket - ticket object. |
| 52 |
* @param array $settings - widget settings. |
| 53 |
* @return void |
| 54 |
*/ |
| 55 |
public static function print_widget( $ticket, $settings ) { |
| 56 |
|
| 57 |
$current_user = WPSC_Current_User::$current_user; |
| 58 |
if ( ! ( |
| 59 |
( |
| 60 |
( |
| 61 |
WPSC_Individual_Ticket::$view_profile == 'customer' || |
| 62 |
$ticket->customer->id == $current_user->customer->id |
| 63 |
) && |
| 64 |
$settings['allow-customer'] |
| 65 |
) || |
| 66 |
( WPSC_Individual_Ticket::$view_profile == 'agent' && in_array( $current_user->agent->role, $settings['allowed-agent-roles'] ) ) |
| 67 |
) ) { |
| 68 |
return; |
| 69 |
} |
| 70 |
$show_edit = ( |
| 71 |
$ticket->is_active && |
| 72 |
WPSC_Individual_Ticket::$view_profile == 'agent' && |
| 73 |
WPSC_Individual_Ticket::has_ticket_cap( 'ctf' ) |
| 74 |
); |
| 75 |
self::render_widget( $ticket, $settings, $show_edit, false ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Prints body of current widget |
| 80 |
* |
| 81 |
* @param WPSC_Ticket $ticket - ticket object. |
| 82 |
* @param array $settings - widget settings. |
| 83 |
* @return void |
| 84 |
*/ |
| 85 |
public static function print_archive_widget( $ticket, $settings ) { |
| 86 |
|
| 87 |
$current_user = WPSC_Current_User::$current_user; |
| 88 |
if ( ! ( |
| 89 |
( |
| 90 |
( |
| 91 |
WPSC_Individual_Archive_Ticket::$view_profile == 'customer' || |
| 92 |
$ticket->customer->id == $current_user->customer->id |
| 93 |
) && |
| 94 |
$settings['allow-customer'] |
| 95 |
) || |
| 96 |
( WPSC_Individual_Archive_Ticket::$view_profile == 'agent' && in_array( $current_user->agent->role, $settings['allowed-agent-roles'] ) ) |
| 97 |
) ) { |
| 98 |
return; |
| 99 |
} |
| 100 |
self::render_widget( $ticket, $settings, false, true ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Render widget |
| 105 |
* |
| 106 |
* @param WPSC_Ticket|WPSC_Archive_Ticket $ticket - ticket object. |
| 107 |
* @param array $settings - widget settings. |
| 108 |
* @param bool $show_edit - show edit button. |
| 109 |
* @param bool $is_archive - is archive ticket. |
| 110 |
* @return void |
| 111 |
*/ |
| 112 |
private static function render_widget( $ticket, $settings, $show_edit, $is_archive ) { |
| 113 |
$flag = false; |
| 114 |
$title = $settings['title'] |
| 115 |
? WPSC_Translations::get( 'wpsc-twt-ticket-fields', stripslashes( $settings['title'] ) ) |
| 116 |
: stripslashes( $settings['title'] ); |
| 117 |
?> |
| 118 |
<div class="wpsc-it-widget wpsc-itw-ticket-fields"> |
| 119 |
<div class="wpsc-widget-header"> |
| 120 |
<h2><?php echo esc_attr( $title ); ?></h2> |
| 121 |
<?php |
| 122 |
if ( $show_edit ) : |
| 123 |
?> |
| 124 |
<span onclick="wpsc_it_get_edit_ticket_fields(<?php echo esc_attr( $ticket->id ); ?>, '<?php echo esc_attr( wp_create_nonce( 'wpsc_it_get_edit_ticket_fields' ) ); ?>')"><?php WPSC_Icons::get( 'edit' ); ?></span> |
| 125 |
<?php |
| 126 |
endif |
| 127 |
?> |
| 128 |
<span class="wpsc-itw-toggle" data-widget="wpsc-itw-ticket-fields"><?php WPSC_Icons::get( 'chevron-up' ); ?></span> |
| 129 |
</div> |
| 130 |
<div class="wpsc-widget-body"> |
| 131 |
<?php |
| 132 |
$cft_exclude = apply_filters( 'wpsc_it_widget_exclude_cft', array() ); |
| 133 |
foreach ( WPSC_Custom_Field::$custom_fields as $cf ) : |
| 134 |
if ( in_array( $cf->type::$slug, $cft_exclude ) ) { |
| 135 |
continue; |
| 136 |
} |
| 137 |
if ( $cf->field == 'ticket' && ! $cf->type::$is_default && ( $ticket->{$cf->slug} || ( $cf->type::$slug == 'cf_number' && is_numeric( $ticket->{$cf->slug} ) ) ) ) : |
| 138 |
if ( $cf->type::$is_date && ! is_object( $ticket->{$cf->slug} ) ) { |
| 139 |
continue; |
| 140 |
} |
| 141 |
$flag = ! $flag ? true : $flag |
| 142 |
?> |
| 143 |
<div class="info-list-item"> |
| 144 |
<div class="info-label"><?php echo esc_attr( $cf->name ); ?>:</div> |
| 145 |
<div class="info-val"> |
| 146 |
<?php |
| 147 |
$cf->type::print_widget_ticket_field_val( $cf, $ticket ); |
| 148 |
?> |
| 149 |
</div> |
| 150 |
</div> |
| 151 |
<?php |
| 152 |
endif; |
| 153 |
endforeach; |
| 154 |
if ( ! $flag ) : |
| 155 |
?> |
| 156 |
<div class="wpsc-widget-default"><?php esc_attr_e( 'Not Applicable', 'supportcandy' ); ?></div> |
| 157 |
<?php |
| 158 |
endif; |
| 159 |
do_action( 'wpsc_itw_ticket_fields', $ticket ) |
| 160 |
?> |
| 161 |
</div> |
| 162 |
</div> |
| 163 |
<?php |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Get edit ticket fields |
| 168 |
* |
| 169 |
* @return void |
| 170 |
*/ |
| 171 |
public static function it_get_edit_ticket_fields() { |
| 172 |
|
| 173 |
if ( check_ajax_referer( 'wpsc_it_get_edit_ticket_fields', '_ajax_nonce', false ) != 1 ) { |
| 174 |
wp_send_json_error( 'Unauthorised request!', 401 ); |
| 175 |
} |
| 176 |
|
| 177 |
WPSC_Individual_Ticket::load_current_ticket(); |
| 178 |
|
| 179 |
$current_user = WPSC_Current_User::$current_user; |
| 180 |
if ( ! ( $current_user->is_agent && WPSC_Individual_Ticket::has_ticket_cap( 'ctf' ) ) ) { |
| 181 |
wp_send_json_error( 'Something went wrong!', 401 ); |
| 182 |
} |
| 183 |
|
| 184 |
$ticket = WPSC_Individual_Ticket::$ticket; |
| 185 |
|
| 186 |
$widgets = get_option( 'wpsc-ticket-widget' ); |
| 187 |
$title = $widgets['ticket-fields']['title']; |
| 188 |
|
| 189 |
ob_start() |
| 190 |
?> |
| 191 |
<form action="#" onsubmit="return false;" class="change-ticket-fields"> |
| 192 |
|
| 193 |
<div class="wpsc-input-group" style="align-items:flex-end;"> |
| 194 |
<input type="text" id="cft_search" placeholder="<?php esc_attr_e( 'Search...', 'supportcandy' ); ?>" autocomplete="off" style="max-width:200px;"> |
| 195 |
</div> |
| 196 |
|
| 197 |
<div id="tf_cf_list"> |
| 198 |
<?php |
| 199 |
|
| 200 |
foreach ( WPSC_Custom_Field::$custom_fields as $cf ) { |
| 201 |
|
| 202 |
if ( |
| 203 |
! class_exists( $cf->type ) || |
| 204 |
$cf->type::$is_default || |
| 205 |
$cf->field != 'ticket' || |
| 206 |
in_array( $cf->type::$slug, self::$ignore_cft ) |
| 207 |
) { |
| 208 |
continue; |
| 209 |
} |
| 210 |
|
| 211 |
$cf->type::print_edit_ticket_cf( $cf, $ticket ); |
| 212 |
} |
| 213 |
|
| 214 |
do_action( 'wpsc_get_tw_ticket_fields_body' ); |
| 215 |
?> |
| 216 |
</div> |
| 217 |
|
| 218 |
<input type="hidden" name="action" value="wpsc_it_set_edit_ticket_fields"> |
| 219 |
<input type="hidden" name="ticket_id" value="<?php echo esc_attr( $ticket->id ); ?>"> |
| 220 |
<input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_it_set_edit_ticket_fields' ) ); ?>"> |
| 221 |
|
| 222 |
<script> |
| 223 |
jQuery('#cft_search').keyup(function(e) { |
| 224 |
var val = jQuery(this).val().trim(); |
| 225 |
var regex = new RegExp(val, "i"); |
| 226 |
jQuery('.change-ticket-fields .wpsc-tff').each(function(){ |
| 227 |
var name = jQuery(this).find('.wpsc-tff-label').text(); |
| 228 |
if (name.search(regex) < 0) { |
| 229 |
jQuery(this).hide(); |
| 230 |
} else { |
| 231 |
jQuery(this).show(); |
| 232 |
} |
| 233 |
}) |
| 234 |
}); |
| 235 |
</script> |
| 236 |
|
| 237 |
</form> |
| 238 |
<?php |
| 239 |
do_action( 'wpsc_get_tw_ticket_fields_footer', $ticket ); |
| 240 |
$body = ob_get_clean(); |
| 241 |
|
| 242 |
ob_start(); |
| 243 |
?> |
| 244 |
<button class="wpsc-button small primary" onclick="wpsc_it_set_edit_ticket_fields(this, <?php echo esc_attr( $ticket->id ); ?>);"> |
| 245 |
<?php esc_attr_e( 'Submit', 'supportcandy' ); ?> |
| 246 |
</button> |
| 247 |
<button class="wpsc-button small secondary" onclick="wpsc_close_modal();"> |
| 248 |
<?php esc_attr_e( 'Cancel', 'supportcandy' ); ?> |
| 249 |
</button> |
| 250 |
<?php |
| 251 |
$footer = ob_get_clean(); |
| 252 |
|
| 253 |
$response = array( |
| 254 |
'title' => $title, |
| 255 |
'body' => $body, |
| 256 |
'footer' => $footer, |
| 257 |
); |
| 258 |
wp_send_json( $response ); |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Set edit ticket fields |
| 263 |
* |
| 264 |
* @return void |
| 265 |
*/ |
| 266 |
public static function it_set_edit_ticket_fields() { |
| 267 |
|
| 268 |
if ( check_ajax_referer( 'wpsc_it_set_edit_ticket_fields', '_ajax_nonce', false ) != 1 ) { |
| 269 |
wp_send_json_error( 'Unauthorised request!', 401 ); |
| 270 |
} |
| 271 |
|
| 272 |
WPSC_Individual_Ticket::load_current_ticket(); |
| 273 |
|
| 274 |
$current_user = WPSC_Current_User::$current_user; |
| 275 |
if ( ! ( $current_user->is_agent && WPSC_Individual_Ticket::has_ticket_cap( 'ctf' ) ) ) { |
| 276 |
wp_send_json_error( 'Something went wrong!', 401 ); |
| 277 |
} |
| 278 |
|
| 279 |
$ticket = clone WPSC_Individual_Ticket::$ticket; |
| 280 |
if ( ! $ticket->is_active ) { |
| 281 |
wp_send_json_error( 'Something went wrong!', 400 ); |
| 282 |
} |
| 283 |
|
| 284 |
foreach ( WPSC_Custom_Field::$custom_fields as $cf ) { |
| 285 |
|
| 286 |
if ( |
| 287 |
! class_exists( $cf->type ) || |
| 288 |
$cf->type::$is_default || |
| 289 |
$cf->field != 'ticket' || |
| 290 |
in_array( $cf->type::$slug, self::$ignore_cft ) |
| 291 |
) { |
| 292 |
continue; |
| 293 |
} |
| 294 |
|
| 295 |
$ticket = $cf->type::set_edit_ticket_cf( $cf, $ticket ); |
| 296 |
} |
| 297 |
|
| 298 |
if ( $ticket != WPSC_Individual_Ticket::$ticket ) { |
| 299 |
|
| 300 |
$ticket->date_updated = new DateTime(); |
| 301 |
$ticket->save(); |
| 302 |
|
| 303 |
do_action( 'wpsc_change_ticket_fields', WPSC_Individual_Ticket::$ticket, $ticket ); |
| 304 |
} |
| 305 |
|
| 306 |
wp_die(); |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* Get Ticket fields |
| 311 |
*/ |
| 312 |
public static function get_tw_ticket_fields() { |
| 313 |
|
| 314 |
if ( ! WPSC_Functions::is_site_admin() ) { |
| 315 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 316 |
} |
| 317 |
|
| 318 |
$ticket_widgets = get_option( 'wpsc-ticket-widget', array() ); |
| 319 |
$ticket_fields = $ticket_widgets['ticket-fields']; |
| 320 |
$title = $ticket_fields['title']; |
| 321 |
$roles = get_option( 'wpsc-agent-roles', array() ); |
| 322 |
ob_start(); |
| 323 |
?> |
| 324 |
|
| 325 |
<form action="#" onsubmit="return false;" class="wpsc-frm-edit-ticket-fields"> |
| 326 |
<div class="wpsc-input-group"> |
| 327 |
<div class="label-container"> |
| 328 |
<label for=""><?php esc_attr_e( 'Title', 'supportcandy' ); ?></label> |
| 329 |
</div> |
| 330 |
<input name="label" type="text" value="<?php echo esc_attr( $ticket_fields['title'] ); ?>" autocomplete="off"> |
| 331 |
</div> |
| 332 |
<div class="wpsc-input-group"> |
| 333 |
<div class="label-container"> |
| 334 |
<label for=""><?php esc_attr_e( 'Enable', 'supportcandy' ); ?></label> |
| 335 |
</div> |
| 336 |
<select name="is_enable"> |
| 337 |
<option <?php selected( $ticket_fields['is_enable'], '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option> |
| 338 |
<option <?php selected( $ticket_fields['is_enable'], '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option> |
| 339 |
</select> |
| 340 |
</div> |
| 341 |
<div class="wpsc-input-group"> |
| 342 |
<div class="label-container"> |
| 343 |
<label for=""><?php esc_attr_e( 'Allowed for customer', 'supportcandy' ); ?></label> |
| 344 |
</div> |
| 345 |
<select id="allow-customer" name="allow-customer"> |
| 346 |
<option <?php selected( $ticket_fields['allow-customer'], '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option> |
| 347 |
<option <?php selected( $ticket_fields['allow-customer'], '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option> |
| 348 |
</select> |
| 349 |
</div> |
| 350 |
<div class="wpsc-input-group"> |
| 351 |
<div class="label-container"> |
| 352 |
<label for=""><?php esc_attr_e( 'Allowed agent roles', 'supportcandy' ); ?></label> |
| 353 |
</div> |
| 354 |
<select multiple id="wpsc-select-agents" name="agents[]" placeholder="search agent..."> |
| 355 |
<?php |
| 356 |
foreach ( $roles as $key => $role ) : |
| 357 |
$selected = in_array( $key, $ticket_fields['allowed-agent-roles'] ) ? 'selected="selected"' : '' |
| 358 |
?> |
| 359 |
<option <?php echo esc_attr( $selected ); ?> value="<?php echo esc_attr( $key ); ?>"><?php echo esc_attr( $role['label'] ); ?></option> |
| 360 |
<?php |
| 361 |
endforeach; |
| 362 |
?> |
| 363 |
</select> |
| 364 |
</div> |
| 365 |
<script> |
| 366 |
jQuery('#wpsc-select-agents').selectWoo({ |
| 367 |
allowClear: false, |
| 368 |
placeholder: "" |
| 369 |
}); |
| 370 |
</script> |
| 371 |
<?php do_action( 'wpsc_get_ticket_fields_body' ); ?> |
| 372 |
<input type="hidden" name="action" value="wpsc_set_tw_ticket_fields"> |
| 373 |
<input type="hidden" name="_ajax_nonce" value="<?php echo esc_attr( wp_create_nonce( 'wpsc_set_tw_ticket_fields' ) ); ?>"> |
| 374 |
</form> |
| 375 |
<?php |
| 376 |
$body = ob_get_clean(); |
| 377 |
|
| 378 |
ob_start(); |
| 379 |
?> |
| 380 |
<button class="wpsc-button small primary" onclick="wpsc_set_tw_ticket_fields(this);"> |
| 381 |
<?php esc_attr_e( 'Submit', 'supportcandy' ); ?> |
| 382 |
</button> |
| 383 |
<button class="wpsc-button small secondary" onclick="wpsc_close_modal();"> |
| 384 |
<?php esc_attr_e( 'Cancel', 'supportcandy' ); ?> |
| 385 |
</button> |
| 386 |
<?php |
| 387 |
do_action( 'wpsc_get_tw_ticket_fields_widget_footer' ); |
| 388 |
$footer = ob_get_clean(); |
| 389 |
|
| 390 |
$response = array( |
| 391 |
'title' => $title, |
| 392 |
'body' => $body, |
| 393 |
'footer' => $footer, |
| 394 |
); |
| 395 |
wp_send_json( $response ); |
| 396 |
} |
| 397 |
|
| 398 |
/** |
| 399 |
* Set Ticket fields |
| 400 |
*/ |
| 401 |
public static function set_tw_ticket_fields() { |
| 402 |
|
| 403 |
if ( check_ajax_referer( 'wpsc_set_tw_ticket_fields', '_ajax_nonce', false ) != 1 ) { |
| 404 |
wp_send_json_error( 'Unauthorised request!', 401 ); |
| 405 |
} |
| 406 |
|
| 407 |
if ( ! WPSC_Functions::is_site_admin() ) { |
| 408 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 409 |
} |
| 410 |
|
| 411 |
$label = isset( $_POST['label'] ) ? sanitize_text_field( wp_unslash( $_POST['label'] ) ) : ''; |
| 412 |
if ( ! $label ) { |
| 413 |
wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 ); |
| 414 |
} |
| 415 |
|
| 416 |
$is_enable = isset( $_POST['is_enable'] ) ? intval( $_POST['is_enable'] ) : 0; |
| 417 |
$allow_for_customer = isset( $_POST['allow-customer'] ) ? intval( $_POST['allow-customer'] ) : 0; |
| 418 |
$agents = isset( $_POST['agents'] ) ? array_filter( array_map( 'intval', $_POST['agents'] ) ) : array(); |
| 419 |
|
| 420 |
$ticket_widgets = get_option( 'wpsc-ticket-widget', array() ); |
| 421 |
|
| 422 |
$ticket_widgets['ticket-fields']['title'] = $label; |
| 423 |
$ticket_widgets['ticket-fields']['is_enable'] = $is_enable; |
| 424 |
$ticket_widgets['ticket-fields']['allow-customer'] = $allow_for_customer; |
| 425 |
$ticket_widgets['ticket-fields']['allowed-agent-roles'] = $agents; |
| 426 |
update_option( 'wpsc-ticket-widget', $ticket_widgets ); |
| 427 |
|
| 428 |
// remove string translations. |
| 429 |
WPSC_Translations::remove( 'wpsc-twt-ticket-fields' ); |
| 430 |
WPSC_Translations::add( 'wpsc-twt-ticket-fields', stripslashes( $label ) ); |
| 431 |
wp_die(); |
| 432 |
} |
| 433 |
} |
| 434 |
endif; |
| 435 |
|
| 436 |
WPSC_ITW_Ticket_Fields::init(); |
| 437 |
|