| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_DF_Status' ) ) : |
| 7 |
|
| 8 |
final class WPSC_DF_Status { |
| 9 |
|
| 10 |
/** |
| 11 |
* Slug for this custom field type |
| 12 |
* |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
public static $slug = 'df_status'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Set whether this custom field type is of type date |
| 19 |
* |
| 20 |
* @var boolean |
| 21 |
*/ |
| 22 |
public static $is_date = false; |
| 23 |
|
| 24 |
/** |
| 25 |
* Set whether this custom field type has applicable to date range |
| 26 |
* |
| 27 |
* @var boolean |
| 28 |
*/ |
| 29 |
public static $has_date_range = false; |
| 30 |
|
| 31 |
/** |
| 32 |
* Set whether this custom field type has multiple values |
| 33 |
* |
| 34 |
* @var boolean |
| 35 |
*/ |
| 36 |
public static $has_multiple_val = false; |
| 37 |
|
| 38 |
/** |
| 39 |
* Data type for column created in tickets table |
| 40 |
* |
| 41 |
* @var string |
| 42 |
*/ |
| 43 |
public static $data_type = 'INT NOT NULL'; |
| 44 |
|
| 45 |
/** |
| 46 |
* Set whether this custom field type has reference to other class |
| 47 |
* |
| 48 |
* @var boolean |
| 49 |
*/ |
| 50 |
public static $has_ref = true; |
| 51 |
|
| 52 |
/** |
| 53 |
* Reference class for this custom field type so that its value(s) return with object or array of objects automatically. Empty string indicate no reference. |
| 54 |
* |
| 55 |
* @var string |
| 56 |
*/ |
| 57 |
public static $ref_class = 'wpsc_status'; |
| 58 |
|
| 59 |
/** |
| 60 |
* Set whether this custom field field type is system default (no fields can be created from it). |
| 61 |
* |
| 62 |
* @var boolean |
| 63 |
*/ |
| 64 |
public static $is_default = true; |
| 65 |
|
| 66 |
/** |
| 67 |
* Set whether this field type has extra information that can be used in ticket form, edit custom fields, etc. |
| 68 |
* |
| 69 |
* @var boolean |
| 70 |
*/ |
| 71 |
public static $has_extra_info = true; |
| 72 |
|
| 73 |
/** |
| 74 |
* Set whether this custom field type can accept personal info. |
| 75 |
* |
| 76 |
* @var boolean |
| 77 |
*/ |
| 78 |
public static $has_personal_info = false; |
| 79 |
|
| 80 |
/** |
| 81 |
* Set whether fields created from this custom field type is allowed in create ticket form |
| 82 |
* |
| 83 |
* @var boolean |
| 84 |
*/ |
| 85 |
public static $is_ctf = false; |
| 86 |
|
| 87 |
/** |
| 88 |
* Set whether fields created from this custom field type is allowed in ticket list |
| 89 |
* |
| 90 |
* @var boolean |
| 91 |
*/ |
| 92 |
public static $is_list = true; |
| 93 |
|
| 94 |
/** |
| 95 |
* Set whether fields created from this custom field type is allowed in ticket filter |
| 96 |
* |
| 97 |
* @var boolean |
| 98 |
*/ |
| 99 |
public static $is_filter = true; |
| 100 |
|
| 101 |
/** |
| 102 |
* Set whether fields created from this custom field type can be given character limits |
| 103 |
* |
| 104 |
* @var boolean |
| 105 |
*/ |
| 106 |
public static $has_char_limit = false; |
| 107 |
|
| 108 |
/** |
| 109 |
* Set whether fields created from this custom field type has custom options set in options table |
| 110 |
* |
| 111 |
* @var boolean |
| 112 |
*/ |
| 113 |
public static $has_options = false; |
| 114 |
|
| 115 |
/** |
| 116 |
* Set whether fields created from this custom field type can be available for ticket list sorting |
| 117 |
* |
| 118 |
* @var boolean |
| 119 |
*/ |
| 120 |
public static $is_sort = true; |
| 121 |
|
| 122 |
/** |
| 123 |
* Set whether fields created from this custom field type can be auto-filled |
| 124 |
* |
| 125 |
* @var boolean |
| 126 |
*/ |
| 127 |
public static $is_auto_fill = false; |
| 128 |
|
| 129 |
/** |
| 130 |
* Set whether fields created from this custom field type can have placeholder |
| 131 |
* |
| 132 |
* @var boolean |
| 133 |
*/ |
| 134 |
public static $is_placeholder = false; |
| 135 |
|
| 136 |
/** |
| 137 |
* Set whether fields created from this custom field type is applicable for visibility conditions in create ticket form |
| 138 |
* |
| 139 |
* @var boolean |
| 140 |
*/ |
| 141 |
public static $is_visibility_conditions = false; |
| 142 |
|
| 143 |
/** |
| 144 |
* Set whether fields created from this custom field type is applicable for macros |
| 145 |
* |
| 146 |
* @var boolean |
| 147 |
*/ |
| 148 |
public static $has_macro = true; |
| 149 |
|
| 150 |
/** |
| 151 |
* Set whether fields of this custom field type is applicalbe for search on ticket list page. |
| 152 |
* |
| 153 |
* @var boolean |
| 154 |
*/ |
| 155 |
public static $is_search = false; |
| 156 |
|
| 157 |
/** |
| 158 |
* Initialize the class |
| 159 |
* |
| 160 |
* @return void |
| 161 |
*/ |
| 162 |
public static function init() { |
| 163 |
|
| 164 |
// Get object of this class. |
| 165 |
add_filter( 'wpsc_load_ref_classes', array( __CLASS__, 'load_ref_class' ) ); |
| 166 |
|
| 167 |
// ticket form. |
| 168 |
add_filter( 'wpsc_create_ticket_data', array( __CLASS__, 'set_create_ticket_data' ), 10, 3 ); |
| 169 |
|
| 170 |
// create ticket data for rest api. |
| 171 |
add_filter( 'wpsc_rest_create_ticket', array( __CLASS__, 'set_rest_ticket_data' ), 10, 3 ); |
| 172 |
|
| 173 |
// Ticket model. |
| 174 |
add_filter( 'wpsc_ticket_joins', array( __CLASS__, 'ticket_join' ), 10, 2 ); |
| 175 |
add_filter( 'wpsc_archive_ticket_joins', array( __CLASS__, 'ticket_join' ), 10, 2 ); |
| 176 |
|
| 177 |
// Individual ticket. |
| 178 |
add_action( 'wp_ajax_wpsc_it_close_ticket', array( __CLASS__, 'it_close_ticket' ) ); |
| 179 |
add_action( 'wp_ajax_nopriv_wpsc_it_close_ticket', array( __CLASS__, 'it_close_ticket' ) ); |
| 180 |
|
| 181 |
// Set custom field type. |
| 182 |
add_filter( 'wpsc_default_cf_types', array( __CLASS__, 'default_cf_types' ), 4 ); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Load current class to reference classes |
| 187 |
* |
| 188 |
* @param array $classes - Associative array of class names indexed by its slug. |
| 189 |
* @return array |
| 190 |
*/ |
| 191 |
public static function load_ref_class( $classes ) { |
| 192 |
|
| 193 |
$classes[ self::$slug ] = array( |
| 194 |
'class' => __CLASS__, |
| 195 |
'save-key' => 'id', |
| 196 |
); |
| 197 |
return $classes; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Add default custom field type to list |
| 202 |
* |
| 203 |
* @param array $default_cf_types - default custom field types array. |
| 204 |
* @return array |
| 205 |
*/ |
| 206 |
public static function default_cf_types( $default_cf_types ) { |
| 207 |
$default_cf_types[ self::$slug ] = array( |
| 208 |
'label' => esc_attr__( 'Status', 'supportcandy' ), |
| 209 |
'class' => __CLASS__, |
| 210 |
); |
| 211 |
return $default_cf_types; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Print operators for ticket form filter |
| 216 |
* |
| 217 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 218 |
* @param array $filter - Existing filters (if any). |
| 219 |
* @return void |
| 220 |
*/ |
| 221 |
public static function get_operators( $cf, $filter = array() ) { |
| 222 |
?> |
| 223 |
|
| 224 |
<div class="item conditional"> |
| 225 |
<select class="operator" onchange="wpsc_tc_get_operand(this, '<?php echo esc_attr( $cf->slug ); ?>', '<?php echo esc_attr( wp_create_nonce( 'wpsc_tc_get_operand' ) ); ?>');"> |
| 226 |
<option value=""><?php esc_attr_e( 'Compare As', 'supportcandy' ); ?></option> |
| 227 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '=' ); ?> value="="><?php esc_attr_e( 'Equals', 'supportcandy' ); ?></option> |
| 228 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'IN' ); ?> value="IN"><?php esc_attr_e( 'Matches', 'supportcandy' ); ?></option> |
| 229 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'NOT IN' ); ?> value="NOT IN"><?php esc_attr_e( 'Not Matches', 'supportcandy' ); ?></option> |
| 230 |
</select> |
| 231 |
</div> |
| 232 |
<?php |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Check condition for this type |
| 237 |
* |
| 238 |
* @param array $condition - condition data. |
| 239 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 240 |
* @param mixed $status - value to compare. |
| 241 |
* @return boolean |
| 242 |
*/ |
| 243 |
public static function is_valid( $condition, $cf, $status ) { |
| 244 |
|
| 245 |
$response = false; |
| 246 |
switch ( $condition['operator'] ) { |
| 247 |
|
| 248 |
case '=': |
| 249 |
$response = $condition['operand_val_1'] == $status ? true : false; |
| 250 |
break; |
| 251 |
|
| 252 |
case 'IN': |
| 253 |
$response = in_array( $status, $condition['operand_val_1'] ) ? true : false; |
| 254 |
break; |
| 255 |
|
| 256 |
case 'NOT IN': |
| 257 |
$response = ! in_array( $status, $condition['operand_val_1'] ) ? true : false; |
| 258 |
break; |
| 259 |
} |
| 260 |
return $response; |
| 261 |
} |
| 262 |
|
| 263 |
/** |
| 264 |
* Print operators for ticket form filter |
| 265 |
* |
| 266 |
* @param string $operator - condition operator on which operands should be returned. |
| 267 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 268 |
* @param array $filter - Exising functions (if any). |
| 269 |
* @return void |
| 270 |
*/ |
| 271 |
public static function get_operands( $operator, $cf, $filter = array() ) { |
| 272 |
|
| 273 |
$is_multiple = $operator !== '=' ? true : false; |
| 274 |
$statuses = WPSC_Status::find( array( 'items_per_page' => 0 ) )['results']; |
| 275 |
$unique_id = uniqid( 'wpsc_' ); |
| 276 |
?> |
| 277 |
<div class="item conditional operand single"> |
| 278 |
<select class="operand_val_1 <?php echo esc_attr( $unique_id ); ?>" <?php echo $is_multiple ? 'multiple' : ''; ?>> |
| 279 |
<?php |
| 280 |
foreach ( $statuses as $status ) { |
| 281 |
$selected = ''; |
| 282 |
if ( isset( $filter['operand_val_1'] ) && ( ( $is_multiple && in_array( $status->id, $filter['operand_val_1'] ) ) || ( ! $is_multiple && $status->id == $filter['operand_val_1'] ) ) ) { |
| 283 |
$selected = 'selected="selected"'; |
| 284 |
} |
| 285 |
?> |
| 286 |
<option <?php echo esc_attr( $selected ); ?> value="<?php echo esc_attr( $status->id ); ?>"><?php echo esc_attr( $status->name ); ?></option> |
| 287 |
<?php |
| 288 |
} |
| 289 |
?> |
| 290 |
</select> |
| 291 |
</div> |
| 292 |
<script>jQuery('.operand_val_1.<?php echo esc_attr( $unique_id ); ?>').selectWoo();</script> |
| 293 |
<?php |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Parse filter and return sql query to be merged in ticket model query builder |
| 298 |
* |
| 299 |
* @param WPSC_Custom_Field $cf - custom field of this type. |
| 300 |
* @param mixed $compare - comparison operator. |
| 301 |
* @param mixed $val - value to compare. |
| 302 |
* @return string |
| 303 |
*/ |
| 304 |
public static function parse_filter( $cf, $compare, $val ) { |
| 305 |
|
| 306 |
global $wpdb; |
| 307 |
$column = 't.' . sanitize_key( $cf->slug ); |
| 308 |
|
| 309 |
/** |
| 310 |
* Normalize IDs (INT only) |
| 311 |
*/ |
| 312 |
$normalize_ids = static function ( $values ) { |
| 313 |
|
| 314 |
$values = is_array( $values ) ? $values : array( $values ); |
| 315 |
$ids = array(); |
| 316 |
|
| 317 |
foreach ( $values as $v ) { |
| 318 |
if ( is_numeric( $v ) && (int) $v > 0 ) { |
| 319 |
$ids[] = (int) $v; |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
return array_unique( $ids ); |
| 324 |
}; |
| 325 |
|
| 326 |
switch ( $compare ) { |
| 327 |
|
| 328 |
case '=': |
| 329 |
$ids = $normalize_ids( $val ); |
| 330 |
if ( empty( $ids ) ) { |
| 331 |
return '1=0'; |
| 332 |
} |
| 333 |
|
| 334 |
return $wpdb->prepare( |
| 335 |
"{$column} RLIKE %s", |
| 336 |
'(^|[|])' . $ids[0] . '($|[|])' |
| 337 |
); |
| 338 |
|
| 339 |
case 'IN': |
| 340 |
$ids = $normalize_ids( $val ); |
| 341 |
if ( empty( $ids ) ) { |
| 342 |
return '1=0'; |
| 343 |
} |
| 344 |
|
| 345 |
$regex = '(^|[|])(' . implode( '|', $ids ) . ')($|[|])'; |
| 346 |
|
| 347 |
return $wpdb->prepare( |
| 348 |
"{$column} RLIKE %s", |
| 349 |
$regex |
| 350 |
); |
| 351 |
|
| 352 |
case 'NOT IN': |
| 353 |
$ids = $normalize_ids( $val ); |
| 354 |
if ( empty( $ids ) ) { |
| 355 |
return '1=1'; |
| 356 |
} |
| 357 |
|
| 358 |
$regex = '(^|[|])(' . implode( '|', $ids ) . ')($|[|])'; |
| 359 |
|
| 360 |
return $wpdb->prepare( |
| 361 |
"{$column} NOT RLIKE %s", |
| 362 |
$regex |
| 363 |
); |
| 364 |
|
| 365 |
default: |
| 366 |
return '1=1'; |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
/** |
| 371 |
* Check ticket condition |
| 372 |
* |
| 373 |
* @param array $condition - array with condition data. |
| 374 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 375 |
* @param WPSC_Ticket $ticket - ticket object. |
| 376 |
* @return boolean |
| 377 |
*/ |
| 378 |
public static function is_valid_ticket_condition( $condition, $cf, $ticket ) { |
| 379 |
|
| 380 |
if ( ! is_object( $ticket->{$cf->slug} ) ) { |
| 381 |
return false; |
| 382 |
} |
| 383 |
|
| 384 |
$flag = true; |
| 385 |
|
| 386 |
switch ( $condition['operator'] ) { |
| 387 |
|
| 388 |
case '=': |
| 389 |
$flag = $ticket->{$cf->slug}->id == $condition['operand_val_1'] ? true : false; |
| 390 |
break; |
| 391 |
|
| 392 |
case 'IN': |
| 393 |
$flag = in_array( $ticket->{$cf->slug}->id, $condition['operand_val_1'] ); |
| 394 |
break; |
| 395 |
|
| 396 |
case 'NOT IN': |
| 397 |
$flag = ! in_array( $ticket->{$cf->slug}->id, $condition['operand_val_1'] ); |
| 398 |
break; |
| 399 |
|
| 400 |
default: |
| 401 |
$flag = true; |
| 402 |
} |
| 403 |
|
| 404 |
return $flag; |
| 405 |
} |
| 406 |
|
| 407 |
/** |
| 408 |
* Return default value for custom field of this type |
| 409 |
* |
| 410 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 411 |
* @return mixed |
| 412 |
*/ |
| 413 |
public static function get_default_value( $cf ) { |
| 414 |
|
| 415 |
return $cf->default_value[0]; |
| 416 |
} |
| 417 |
|
| 418 |
/** |
| 419 |
* Check and return custom field value for new ticket to be created. |
| 420 |
* This function is used by filter for set create ticket form and called directly by my-profile for each applicable custom fields. |
| 421 |
* Ignore phpcs nonce issue as we already checked where it is called from. |
| 422 |
* |
| 423 |
* @param array $data - Array of values to to stored in ticket in an insert function. |
| 424 |
* @param array $custom_fields - Array containing all applicable custom fields indexed by unique custom field types. |
| 425 |
* @param boolean $is_my_profile - Whether it or not it is created from my-profile. This function is used by create ticket as well as my-profile. Due to customer fields handling is done same way, this flag gives apportunity to identify where it being called. |
| 426 |
* @return array |
| 427 |
*/ |
| 428 |
public static function set_create_ticket_data( $data, $custom_fields, $is_my_profile ) { |
| 429 |
|
| 430 |
$data['status'] = self::get_default_value( $custom_fields[ self::$slug ][0] ); |
| 431 |
return $data; |
| 432 |
} |
| 433 |
|
| 434 |
/** |
| 435 |
* Set create ticket data for rest api request |
| 436 |
* |
| 437 |
* @param array $data - create ticket data array. |
| 438 |
* @param WP_REST_Request $request - rest request object. |
| 439 |
* @param array $custom_fields - custom field objects indexed by unique custom field types. |
| 440 |
* @return array |
| 441 |
*/ |
| 442 |
public static function set_rest_ticket_data( $data, $request, $custom_fields ) { |
| 443 |
|
| 444 |
$cf = WPSC_Custom_Field::get_cf_by_slug( 'status' ); |
| 445 |
$data['status'] = self::get_default_value( $cf ); |
| 446 |
return $data; |
| 447 |
} |
| 448 |
|
| 449 |
/** |
| 450 |
* Add SQL joins to ticket model for this field type |
| 451 |
* |
| 452 |
* @param array $joins - array of join string that can be imploded later. |
| 453 |
* @param array $filter - user filter. |
| 454 |
* @return array |
| 455 |
*/ |
| 456 |
public static function ticket_join( $joins, $filter ) { |
| 457 |
|
| 458 |
global $wpdb; |
| 459 |
if ( $filter['orderby_slug'] == 'status' ) { |
| 460 |
$joins[] = 'LEFT JOIN ' . $wpdb->prefix . 'psmsc_statuses st ON t.status = st.id'; |
| 461 |
} |
| 462 |
return $joins; |
| 463 |
} |
| 464 |
|
| 465 |
/** |
| 466 |
* Return val field for meta query of this type of custom field |
| 467 |
* |
| 468 |
* @param array $condition - condition data. |
| 469 |
* @return mixed |
| 470 |
*/ |
| 471 |
public static function get_meta_value( $condition ) { |
| 472 |
|
| 473 |
$operator = $condition['operator']; |
| 474 |
switch ( $operator ) { |
| 475 |
|
| 476 |
case '=': |
| 477 |
case 'IN': |
| 478 |
case 'NOT IN': |
| 479 |
return $condition['operand_val_1']; |
| 480 |
} |
| 481 |
return false; |
| 482 |
} |
| 483 |
|
| 484 |
/** |
| 485 |
* Close ticket |
| 486 |
* |
| 487 |
* @return void |
| 488 |
*/ |
| 489 |
public static function it_close_ticket() { |
| 490 |
|
| 491 |
if ( check_ajax_referer( 'wpsc_it_close_ticket', '_ajax_nonce', false ) != 1 ) { |
| 492 |
wp_send_json_error( 'Unauthorized request!', 401 ); |
| 493 |
} |
| 494 |
|
| 495 |
WPSC_Individual_Ticket::load_current_ticket(); |
| 496 |
$current_user = WPSC_Current_User::$current_user; |
| 497 |
$ticket = WPSC_Individual_Ticket::$ticket; |
| 498 |
|
| 499 |
if ( ! $ticket->is_active ) { |
| 500 |
wp_send_json_error( 'Unauthorized!', 401 ); |
| 501 |
} |
| 502 |
|
| 503 |
$gs = get_option( 'wpsc-gs-general' ); |
| 504 |
$tl_advanced = get_option( 'wpsc-tl-ms-advanced' ); |
| 505 |
|
| 506 |
$close_flag = false; |
| 507 |
if ( |
| 508 |
( $current_user->customer->id == $ticket->customer->id && in_array( 'customer', $gs['allow-close-ticket'] ) ) || |
| 509 |
( $current_user->is_agent && WPSC_Individual_Ticket::has_ticket_cap( 'cs' ) && in_array( $current_user->agent->role, $gs['allow-close-ticket'] ) ) |
| 510 |
) { |
| 511 |
$close_flag = true; |
| 512 |
} |
| 513 |
|
| 514 |
$close_flag = apply_filters( 'wpsc_it_action_close_flag', $close_flag, $ticket ); |
| 515 |
|
| 516 |
if ( ! ( |
| 517 |
$close_flag && |
| 518 |
! ( |
| 519 |
$ticket->status->id == $gs['close-ticket-status'] || |
| 520 |
in_array( $ticket->status->id, $tl_advanced['closed-ticket-statuses'] ) |
| 521 |
) |
| 522 |
) ) { |
| 523 |
wp_send_json_error( 'Unauthorized!', 401 ); |
| 524 |
} |
| 525 |
|
| 526 |
WPSC_Individual_Ticket::change_status( $ticket->status->id, $gs['close-ticket-status'], $current_user->customer->id ); |
| 527 |
wp_die(); |
| 528 |
} |
| 529 |
|
| 530 |
/** |
| 531 |
* Return data for this custom field while creating duplicate ticket |
| 532 |
* |
| 533 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 534 |
* @param WPSC_Ticket $ticket - ticket object. |
| 535 |
* @return mixed |
| 536 |
*/ |
| 537 |
public static function get_duplicate_ticket_data( $cf, $ticket ) { |
| 538 |
|
| 539 |
return $cf->default_value[0]; |
| 540 |
} |
| 541 |
|
| 542 |
/** |
| 543 |
* Print generic input field for this type. |
| 544 |
* |
| 545 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 546 |
* @param WPSC_Status $value - optional. Input field will be printed with given value. |
| 547 |
* @return void |
| 548 |
*/ |
| 549 |
public static function print_cf_input( $cf, $value ) { |
| 550 |
|
| 551 |
if ( ! is_object( $value ) ) { |
| 552 |
|
| 553 |
$status = $value ? new WPSC_Status( $value ) : 0; |
| 554 |
if ( $status && $status->id ) { |
| 555 |
$value = $status; |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
$statuses = WPSC_Status::find( array( 'items_per_page' => 0 ) )['results']; |
| 560 |
$unique_id = uniqid( 'wpsc_' ); |
| 561 |
?> |
| 562 |
<div class="wpsc-tff wpsc-sm-12 wpsc-md-12 wpsc-lg-12 wpsc-visible wpsc-xs-12" data-cft="<?php echo esc_attr( self::$slug ); ?>"> |
| 563 |
<div class="wpsc-tff-label"> |
| 564 |
<span class="name"><?php echo esc_attr( $cf->name ); ?></span> |
| 565 |
</div> |
| 566 |
<?php |
| 567 |
$extra_info = stripslashes( $cf->extra_info ); |
| 568 |
if ( $extra_info ) : |
| 569 |
?> |
| 570 |
<span class="extra-info"><?php echo esc_attr( $cf->extra_info ); ?></span> |
| 571 |
<?php |
| 572 |
endif |
| 573 |
?> |
| 574 |
<select class="<?php echo esc_attr( $unique_id ); ?>" name="<?php echo esc_attr( $cf->slug ); ?>"> |
| 575 |
<option value=""></option> |
| 576 |
<?php |
| 577 |
foreach ( $statuses as $status ) { |
| 578 |
?> |
| 579 |
<option <?php $value && selected( $status->id, $value->id ); ?> value="<?php echo esc_attr( $status->id ); ?>"><?php echo esc_attr( $status->name ); ?></option> |
| 580 |
<?php |
| 581 |
} |
| 582 |
?> |
| 583 |
</select> |
| 584 |
<script> |
| 585 |
jQuery('select.<?php echo esc_attr( $unique_id ); ?>').selectWoo({ |
| 586 |
allowClear: true, |
| 587 |
placeholder: "<?php echo esc_attr( $cf->placeholder_text ); ?>" |
| 588 |
}); |
| 589 |
</script> |
| 590 |
</div> |
| 591 |
<?php |
| 592 |
} |
| 593 |
|
| 594 |
/** |
| 595 |
* Get value for generic field from post. |
| 596 |
* Ignore phpcs nonce issue as we already checked where it is called from. |
| 597 |
* |
| 598 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 599 |
* @return string |
| 600 |
*/ |
| 601 |
public static function get_cf_input_val( $cf ) { |
| 602 |
|
| 603 |
$value = isset( $_POST[ $cf->slug ] ) ? intval( $_POST[ $cf->slug ] ) : 0; // phpcs:ignore |
| 604 |
$status = $value ? new WPSC_Status( $value ) : 0; |
| 605 |
return $status && $status->id ? $value : ''; |
| 606 |
} |
| 607 |
|
| 608 |
/** |
| 609 |
* Print edit custom field properties |
| 610 |
* |
| 611 |
* @param WPSC_Custom_Fields $cf - custom field object. |
| 612 |
* @param string $field_class - class name of field category. |
| 613 |
* @return void |
| 614 |
*/ |
| 615 |
public static function get_edit_custom_field_properties( $cf, $field_class ) { |
| 616 |
|
| 617 |
if ( in_array( 'extra_info', $field_class::$allowed_properties ) ) : |
| 618 |
?> |
| 619 |
<div data-type="textfield" data-required="false" class="wpsc-input-group extra-info"> |
| 620 |
<div class="label-container"> |
| 621 |
<label for=""><?php esc_attr_e( 'Extra info', 'supportcandy' ); ?></label> |
| 622 |
</div> |
| 623 |
<input name="extra_info" type="text" value="<?php echo esc_attr( $cf->extra_info ); ?>" autocomplete="off" /> |
| 624 |
</div> |
| 625 |
<?php |
| 626 |
endif; |
| 627 |
|
| 628 |
if ( in_array( 'default_value', $field_class::$allowed_properties ) ) : |
| 629 |
$default_val = $cf->default_value ? $cf->default_value[0] : ''; |
| 630 |
?> |
| 631 |
<div data-type="single-select" data-required="false" class="wpsc-input-group default_value"> |
| 632 |
<div class="label-container"> |
| 633 |
<label for=""> |
| 634 |
<?php esc_attr_e( 'Default value', 'supportcandy' ); ?> |
| 635 |
<span class="required-char">*</span> |
| 636 |
</label> |
| 637 |
</div> |
| 638 |
<select id="wpsc-default-ts" name="default_value"> |
| 639 |
<?php |
| 640 |
$statuses = WPSC_Status::find( array( 'items_per_page' => 0 ) )['results']; |
| 641 |
foreach ( $statuses as $status ) : |
| 642 |
?> |
| 643 |
<option <?php selected( $status->id, $default_val ); ?> value="<?php echo esc_attr( $status->id ); ?>"><?php echo esc_attr( $status->name ); ?></option> |
| 644 |
<?php |
| 645 |
endforeach; |
| 646 |
?> |
| 647 |
</select> |
| 648 |
<script> |
| 649 |
jQuery('#wpsc-default-ts').selectWoo({ |
| 650 |
allowClear: true, |
| 651 |
placeholder: "" |
| 652 |
}); |
| 653 |
</script> |
| 654 |
</div> |
| 655 |
<?php |
| 656 |
endif; |
| 657 |
|
| 658 |
if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) : |
| 659 |
?> |
| 660 |
<div data-type="number" data-required="false" class="wpsc-input-group tl_width"> |
| 661 |
<div class="label-container"> |
| 662 |
<label for=""> |
| 663 |
<?php esc_attr_e( 'Ticket list width (pixels)', 'supportcandy' ); ?> |
| 664 |
</label> |
| 665 |
</div> |
| 666 |
<input type="number" name="tl_width" value="<?php echo intval( $cf->tl_width ); ?>" autocomplete="off"> |
| 667 |
</div> |
| 668 |
<?php |
| 669 |
endif; |
| 670 |
} |
| 671 |
|
| 672 |
/** |
| 673 |
* Set custom field properties. Can be used by add/edit custom field. |
| 674 |
* Ignore phpcs nonce issue as we already checked where it is called from. |
| 675 |
* |
| 676 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 677 |
* @param string $field_class - class of field category. |
| 678 |
* @return void |
| 679 |
*/ |
| 680 |
public static function set_cf_properties( $cf, $field_class ) { |
| 681 |
|
| 682 |
// extra info. |
| 683 |
if ( in_array( 'extra_info', $field_class::$allowed_properties ) ) { |
| 684 |
$cf->extra_info = isset( $_POST['extra_info'] ) ? sanitize_text_field( wp_unslash( $_POST['extra_info'] ) ) : ''; // phpcs:ignore |
| 685 |
} |
| 686 |
|
| 687 |
// default value. |
| 688 |
if ( in_array( 'default_value', $field_class::$allowed_properties ) ) { |
| 689 |
$default_value = isset( $_POST['default_value'] ) ? intval( $_POST['default_value'] ) : 0; // phpcs:ignore |
| 690 |
if ( $default_value ) { |
| 691 |
$cf->default_value = array( $default_value ); |
| 692 |
} |
| 693 |
} |
| 694 |
|
| 695 |
// tl_width! |
| 696 |
if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) { |
| 697 |
$tl_width = isset( $_POST['tl_width'] ) ? intval( $_POST['tl_width'] ) : 0; // phpcs:ignore |
| 698 |
$cf->tl_width = $tl_width ? $tl_width : 100; |
| 699 |
} |
| 700 |
|
| 701 |
// save! |
| 702 |
$cf->save(); |
| 703 |
} |
| 704 |
|
| 705 |
/** |
| 706 |
* Return orderby string |
| 707 |
* |
| 708 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 709 |
* @return string |
| 710 |
*/ |
| 711 |
public static function get_orderby_string( $cf ) { |
| 712 |
|
| 713 |
$statuses = implode( |
| 714 |
',', |
| 715 |
array_map( |
| 716 |
fn( $status ) => $status->id, |
| 717 |
WPSC_Status::find( array( 'items_per_page' => 0 ) )['results'] |
| 718 |
) |
| 719 |
); |
| 720 |
return 'FIELD( t.status, ' . $statuses . ' )'; |
| 721 |
} |
| 722 |
|
| 723 |
/** |
| 724 |
* Returns printable ticket value for custom field. Can be used in export tickets, replace macros etc. |
| 725 |
* |
| 726 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 727 |
* @param WPSC_Ticket $ticket - ticket object. |
| 728 |
* @param string $module - module name. |
| 729 |
* @return string |
| 730 |
*/ |
| 731 |
public static function get_ticket_field_val( $cf, $ticket, $module = '' ) { |
| 732 |
|
| 733 |
return apply_filters( 'wpsc_ticket_field_val_status', $ticket->status->name, $cf, $ticket, $module ); |
| 734 |
} |
| 735 |
|
| 736 |
/** |
| 737 |
* Print ticket value for given custom field on ticket list |
| 738 |
* |
| 739 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 740 |
* @param WPSC_Ticket $ticket - ticket object. |
| 741 |
* @return void |
| 742 |
*/ |
| 743 |
public static function print_tl_ticket_field_val( $cf, $ticket ) { |
| 744 |
|
| 745 |
$status = $ticket->status; |
| 746 |
?> |
| 747 |
<div class="wpsc-tag" style="background-color: <?php echo esc_attr( $status->bg_color ); ?>; color:<?php echo esc_attr( $status->color ); ?>;"><?php echo esc_attr( $status->name ); ?></div> |
| 748 |
<?php |
| 749 |
} |
| 750 |
|
| 751 |
/** |
| 752 |
* Print ticket value for given custom field on widget |
| 753 |
* |
| 754 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 755 |
* @param WPSC_Ticket $ticket - ticket object. |
| 756 |
* @return void |
| 757 |
*/ |
| 758 |
public static function print_widget_ticket_field_val( $cf, $ticket ) { |
| 759 |
|
| 760 |
self::print_tl_ticket_field_val( $cf, $ticket ); |
| 761 |
} |
| 762 |
|
| 763 |
/** |
| 764 |
* Print given value for custom field |
| 765 |
* |
| 766 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 767 |
* @param mixed $val - value to convert and print. |
| 768 |
* @return void |
| 769 |
*/ |
| 770 |
public static function print_val( $cf, $val ) { |
| 771 |
|
| 772 |
$status = is_object( $val ) ? $val : new WPSC_Status( $val ); |
| 773 |
if ( $status->id ) { |
| 774 |
?> |
| 775 |
<div class="wpsc-tag" style="background-color: <?php echo esc_attr( $status->bg_color ); ?>; color:<?php echo esc_attr( $status->color ); ?>;"><?php echo esc_attr( $status->name ); ?></div> |
| 776 |
<?php |
| 777 |
} else { |
| 778 |
esc_attr_e( 'None', 'supportcandy' ); |
| 779 |
} |
| 780 |
} |
| 781 |
|
| 782 |
/** |
| 783 |
* Return printable value for history log macro |
| 784 |
* |
| 785 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 786 |
* @param mixed $val - value to convert and return. |
| 787 |
* @return string |
| 788 |
*/ |
| 789 |
public static function get_history_log_val( $cf, $val ) { |
| 790 |
|
| 791 |
$status = is_object( $val ) ? $val : new WPSC_Status( $val ); |
| 792 |
return $status->id ? $status->name : esc_attr__( 'None', 'supportcandy' ); |
| 793 |
} |
| 794 |
|
| 795 |
/** |
| 796 |
* Print dashboard activity function |
| 797 |
* |
| 798 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 799 |
* @param array $recent_logs - recent_logs object. |
| 800 |
* @param array $body - body object. |
| 801 |
* @param int $view - check if frontend. |
| 802 |
* @return string |
| 803 |
*/ |
| 804 |
public static function print_activity( $cf, $recent_logs, $body, $view ) { |
| 805 |
|
| 806 |
$url = WPSC_Functions::get_ticket_url( $recent_logs->ticket->id, $view ); |
| 807 |
$new_status = new WPSC_Status( $body->new ); |
| 808 |
if ( $new_status->id ) { |
| 809 |
$status_value = $new_status->name; |
| 810 |
} else { |
| 811 |
$status_value = 'None'; |
| 812 |
} |
| 813 |
return esc_attr( $recent_logs->customer->name ) . ' updated the ' . esc_attr( $cf->name ) . ' of <a href="' . esc_attr( $url ) . '" target="_blank">#' . esc_attr( $recent_logs->ticket->id ) . '</a> to ' . esc_attr( $status_value ); |
| 814 |
} |
| 815 |
} |
| 816 |
endif; |
| 817 |
|
| 818 |
WPSC_DF_Status::init(); |
| 819 |
|