| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_DF_Last_Reply_By' ) ) : |
| 7 |
|
| 8 |
final class WPSC_DF_Last_Reply_By { |
| 9 |
|
| 10 |
/** |
| 11 |
* Slug for this custom field type |
| 12 |
* |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
public static $slug = 'df_last_reply_by'; |
| 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 = 'BIGINT 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_customer'; |
| 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 = false; |
| 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 auto-filled |
| 117 |
* |
| 118 |
* @var boolean |
| 119 |
*/ |
| 120 |
public static $is_auto_fill = false; |
| 121 |
|
| 122 |
/** |
| 123 |
* Set whether fields created from this custom field type can be available for ticket list sorting |
| 124 |
* |
| 125 |
* @var boolean |
| 126 |
*/ |
| 127 |
public static $is_sort = 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 |
// Set custom field type. |
| 168 |
add_filter( 'wpsc_default_cf_types', array( __CLASS__, 'default_cf_types' ), 4 ); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Load current class to reference classes |
| 173 |
* |
| 174 |
* @param array $classes - Associative array of class names indexed by its slug. |
| 175 |
* @return array |
| 176 |
*/ |
| 177 |
public static function load_ref_class( $classes ) { |
| 178 |
|
| 179 |
$classes[ self::$slug ] = array( |
| 180 |
'class' => __CLASS__, |
| 181 |
'save-key' => 'id', |
| 182 |
); |
| 183 |
return $classes; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Add default custom field type to list |
| 188 |
* |
| 189 |
* @param array $default_cf_types - default custom field types array. |
| 190 |
* @return array |
| 191 |
*/ |
| 192 |
public static function default_cf_types( $default_cf_types ) { |
| 193 |
$default_cf_types[ self::$slug ] = array( |
| 194 |
'label' => esc_attr__( 'Last Reply By', 'supportcandy' ), |
| 195 |
'class' => __CLASS__, |
| 196 |
); |
| 197 |
return $default_cf_types; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Print operators for ticket form filter |
| 202 |
* |
| 203 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 204 |
* @param array $filter - Existing filters (if any). |
| 205 |
* @return void |
| 206 |
*/ |
| 207 |
public static function get_operators( $cf, $filter = array() ) { |
| 208 |
?> |
| 209 |
|
| 210 |
<div class="item conditional"> |
| 211 |
<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' ) ); ?>');"> |
| 212 |
<option value=""><?php esc_attr_e( 'Compare As', 'supportcandy' ); ?></option> |
| 213 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '=' ); ?> value="="><?php esc_attr_e( 'Equals', 'supportcandy' ); ?></option> |
| 214 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'IN' ); ?> value="IN"><?php esc_attr_e( 'Matches', 'supportcandy' ); ?></option> |
| 215 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'NOT IN' ); ?> value="NOT IN"><?php esc_attr_e( 'Not Matches', 'supportcandy' ); ?></option> |
| 216 |
</select> |
| 217 |
</div> |
| 218 |
<?php |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Print operators for ticket form filter |
| 223 |
* |
| 224 |
* @param string $operator - condition operator on which operands should be returned. |
| 225 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 226 |
* @param array $filter - Exising functions (if any). |
| 227 |
* @return void |
| 228 |
*/ |
| 229 |
public static function get_operands( $operator, $cf, $filter = array() ) { |
| 230 |
|
| 231 |
$is_multiple = $operator !== '=' ? true : false; |
| 232 |
$unique_id = uniqid( 'wpsc_' ); |
| 233 |
?> |
| 234 |
<div class="item conditional operand single"> |
| 235 |
<select class="operand_val_1 <?php echo esc_attr( $unique_id ); ?>" <?php echo $is_multiple ? 'multiple' : ''; ?>> |
| 236 |
<?php |
| 237 |
|
| 238 |
if ( $is_multiple && isset( $filter['operand_val_1'] ) ) { |
| 239 |
|
| 240 |
foreach ( $filter['operand_val_1'] as $customer_id ) { |
| 241 |
$customer = new WPSC_Customer( intval( $customer_id ) ) |
| 242 |
?> |
| 243 |
<option selected="selected" value="<?php echo esc_attr( $customer->id ); ?>"><?php echo esc_attr( $customer->name ); ?></option> |
| 244 |
<?php |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
if ( ! $is_multiple && isset( $filter['operand_val_1'] ) ) { |
| 249 |
|
| 250 |
$customer = new WPSC_Customer( intval( $filter['operand_val_1'] ) ) |
| 251 |
?> |
| 252 |
<option selected="selected" value="<?php echo esc_attr( $customer->id ); ?>"><?php echo esc_attr( $customer->name ); ?></option> |
| 253 |
<?php |
| 254 |
} |
| 255 |
?> |
| 256 |
|
| 257 |
</select> |
| 258 |
</div> |
| 259 |
<script> |
| 260 |
jQuery('.operand_val_1.<?php echo esc_attr( $unique_id ); ?>').selectWoo({ |
| 261 |
ajax: { |
| 262 |
url: supportcandy.ajax_url, |
| 263 |
dataType: 'json', |
| 264 |
delay: 250, |
| 265 |
data: function (params) { |
| 266 |
return { |
| 267 |
q: params.term, // search term |
| 268 |
page: params.page, |
| 269 |
action: 'wpsc_customer_filter_autocomplete', |
| 270 |
_ajax_nonce: '<?php echo esc_attr( wp_create_nonce( 'wpsc_customer_filter_autocomplete' ) ); ?>' |
| 271 |
}; |
| 272 |
}, |
| 273 |
processResults: function (data, params) { |
| 274 |
var terms = []; |
| 275 |
if ( data ) { |
| 276 |
jQuery.each( data, function( id, text ) { |
| 277 |
terms.push( { id: text.id, text: text.title } ); |
| 278 |
}); |
| 279 |
} |
| 280 |
return { |
| 281 |
results: terms |
| 282 |
}; |
| 283 |
}, |
| 284 |
cache: true |
| 285 |
}, |
| 286 |
escapeMarkup: function (markup) { return markup; }, // let our custom formatter work |
| 287 |
minimumInputLength: 1 |
| 288 |
}); |
| 289 |
</script> |
| 290 |
<?php |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Parse filter and return sql query to be merged in ticket model query builder |
| 295 |
* |
| 296 |
* @param WPSC_Custom_Field $cf - custom field of this type. |
| 297 |
* @param mixed $compare - comparison operator. |
| 298 |
* @param mixed $val - value to compare. |
| 299 |
* @return string |
| 300 |
*/ |
| 301 |
public static function parse_filter( $cf, $compare, $val ) { |
| 302 |
|
| 303 |
global $wpdb; |
| 304 |
$column = 't.' . sanitize_key( $cf->slug ); |
| 305 |
|
| 306 |
/** |
| 307 |
* Normalize IDs (INT only) |
| 308 |
*/ |
| 309 |
$normalize_ids = static function ( $values ) { |
| 310 |
|
| 311 |
$values = is_array( $values ) ? $values : array( $values ); |
| 312 |
$ids = array(); |
| 313 |
|
| 314 |
foreach ( $values as $v ) { |
| 315 |
if ( is_numeric( $v ) && (int) $v > 0 ) { |
| 316 |
$ids[] = (int) $v; |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
return array_unique( $ids ); |
| 321 |
}; |
| 322 |
|
| 323 |
switch ( $compare ) { |
| 324 |
|
| 325 |
case '=': |
| 326 |
$ids = $normalize_ids( $val ); |
| 327 |
if ( empty( $ids ) ) { |
| 328 |
return '1=0'; |
| 329 |
} |
| 330 |
|
| 331 |
return $wpdb->prepare( |
| 332 |
"{$column} RLIKE %s", |
| 333 |
'(^|[|])' . $ids[0] . '($|[|])' |
| 334 |
); |
| 335 |
|
| 336 |
case 'IN': |
| 337 |
$ids = $normalize_ids( $val ); |
| 338 |
if ( empty( $ids ) ) { |
| 339 |
return '1=0'; |
| 340 |
} |
| 341 |
|
| 342 |
$regex = '(^|[|])(' . implode( '|', $ids ) . ')($|[|])'; |
| 343 |
|
| 344 |
return $wpdb->prepare( |
| 345 |
"{$column} RLIKE %s", |
| 346 |
$regex |
| 347 |
); |
| 348 |
|
| 349 |
case 'NOT IN': |
| 350 |
$ids = $normalize_ids( $val ); |
| 351 |
if ( empty( $ids ) ) { |
| 352 |
return '1=1'; |
| 353 |
} |
| 354 |
|
| 355 |
$regex = '(^|[|])(' . implode( '|', $ids ) . ')($|[|])'; |
| 356 |
|
| 357 |
return $wpdb->prepare( |
| 358 |
"{$column} NOT RLIKE %s", |
| 359 |
$regex |
| 360 |
); |
| 361 |
|
| 362 |
default: |
| 363 |
return '1=1'; |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
/** |
| 368 |
* Check ticket condition |
| 369 |
* |
| 370 |
* @param array $condition - array with condition data. |
| 371 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 372 |
* @param WPSC_Ticket $ticket - ticket object. |
| 373 |
* @return boolean |
| 374 |
*/ |
| 375 |
public static function is_valid_ticket_condition( $condition, $cf, $ticket ) { |
| 376 |
|
| 377 |
if ( ! is_object( $ticket->{$cf->slug} ) ) { |
| 378 |
return false; |
| 379 |
} |
| 380 |
|
| 381 |
$flag = true; |
| 382 |
|
| 383 |
switch ( $condition['operator'] ) { |
| 384 |
|
| 385 |
case '=': |
| 386 |
$flag = $ticket->{$cf->slug}->id == $condition['operand_val_1'] ? true : false; |
| 387 |
break; |
| 388 |
|
| 389 |
case 'IN': |
| 390 |
$flag = in_array( $ticket->{$cf->slug}->id, $condition['operand_val_1'] ); |
| 391 |
break; |
| 392 |
|
| 393 |
case 'NOT IN': |
| 394 |
$flag = ! in_array( $ticket->{$cf->slug}->id, $condition['operand_val_1'] ); |
| 395 |
break; |
| 396 |
|
| 397 |
default: |
| 398 |
$flag = true; |
| 399 |
} |
| 400 |
|
| 401 |
return $flag; |
| 402 |
} |
| 403 |
|
| 404 |
/** |
| 405 |
* Return val field for meta query of this type of custom field |
| 406 |
* |
| 407 |
* @param array $condition - condition data. |
| 408 |
* @return mixed |
| 409 |
*/ |
| 410 |
public static function get_meta_value( $condition ) { |
| 411 |
|
| 412 |
$operator = $condition['operator']; |
| 413 |
switch ( $operator ) { |
| 414 |
|
| 415 |
case '=': |
| 416 |
case 'IN': |
| 417 |
case 'NOT IN': |
| 418 |
return $condition['operand_val_1']; |
| 419 |
} |
| 420 |
return false; |
| 421 |
} |
| 422 |
|
| 423 |
/** |
| 424 |
* Print edit custom field properties |
| 425 |
* |
| 426 |
* @param WPSC_Custom_Fields $cf - custom field object. |
| 427 |
* @param string $field_class - class name of field category. |
| 428 |
* @return void |
| 429 |
*/ |
| 430 |
public static function get_edit_custom_field_properties( $cf, $field_class ) { |
| 431 |
|
| 432 |
if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) : |
| 433 |
?> |
| 434 |
<div data-type="number" data-required="false" class="wpsc-input-group tl_width"> |
| 435 |
<div class="label-container"> |
| 436 |
<label for=""> |
| 437 |
<?php esc_attr_e( 'Ticket list width (pixels)', 'supportcandy' ); ?> |
| 438 |
</label> |
| 439 |
</div> |
| 440 |
<input type="number" name="tl_width" value="<?php echo esc_attr( intval( $cf->tl_width ) ); ?>" autocomplete="off"> |
| 441 |
</div> |
| 442 |
<?php |
| 443 |
endif; |
| 444 |
} |
| 445 |
|
| 446 |
/** |
| 447 |
* Set custom field properties. Can be used by add/edit custom field. |
| 448 |
* Ignore phpcs nonce issue as we already checked where it is called from. |
| 449 |
* |
| 450 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 451 |
* @param string $field_class - class of field category. |
| 452 |
* @return void |
| 453 |
*/ |
| 454 |
public static function set_cf_properties( $cf, $field_class ) { |
| 455 |
|
| 456 |
// tl_width! |
| 457 |
if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) { |
| 458 |
$tl_width = isset( $_POST['tl_width'] ) ? intval( $_POST['tl_width'] ) : 0; // phpcs:ignore |
| 459 |
$cf->tl_width = $tl_width ? $tl_width : 100; |
| 460 |
} |
| 461 |
|
| 462 |
// save! |
| 463 |
$cf->save(); |
| 464 |
} |
| 465 |
|
| 466 |
/** |
| 467 |
* Returns printable ticket value for custom field. Can be used in export tickets, replace macros etc. |
| 468 |
* |
| 469 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 470 |
* @param WPSC_Ticket $ticket - ticket object. |
| 471 |
* @param string $module - module name. |
| 472 |
* @return string |
| 473 |
*/ |
| 474 |
public static function get_ticket_field_val( $cf, $ticket, $module = '' ) { |
| 475 |
|
| 476 |
$value = $ticket->last_reply_by->id ? esc_attr( $ticket->last_reply_by->name ) : ''; |
| 477 |
return apply_filters( 'wpsc_ticket_field_val_last_reply_by', $value, $cf, $ticket, $module ); |
| 478 |
} |
| 479 |
|
| 480 |
/** |
| 481 |
* Print ticket value for given custom field on ticket list |
| 482 |
* |
| 483 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 484 |
* @param WPSC_Ticket $ticket - ticket object. |
| 485 |
* @return void |
| 486 |
*/ |
| 487 |
public static function print_tl_ticket_field_val( $cf, $ticket ) { |
| 488 |
|
| 489 |
echo $ticket->last_reply_by->id ? esc_attr( $ticket->last_reply_by->name ) : ''; |
| 490 |
} |
| 491 |
|
| 492 |
/** |
| 493 |
* Print ticket value for given custom field on widget |
| 494 |
* |
| 495 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 496 |
* @param WPSC_Ticket $ticket - ticket object. |
| 497 |
* @return void |
| 498 |
*/ |
| 499 |
public static function print_widget_ticket_field_val( $cf, $ticket ) { |
| 500 |
|
| 501 |
self::print_tl_ticket_field_val( $cf, $ticket ); |
| 502 |
} |
| 503 |
} |
| 504 |
endif; |
| 505 |
|
| 506 |
WPSC_DF_Last_Reply_By::init(); |
| 507 |
|