| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_DF_Last_Reply_Source' ) ) : |
| 7 |
|
| 8 |
final class WPSC_DF_Last_Reply_Source { |
| 9 |
|
| 10 |
/** |
| 11 |
* Slug for this custom field type |
| 12 |
* |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
public static $slug = 'df_last_reply_source'; |
| 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 = 'VARCHAR(50) 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 = false; |
| 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 = ''; |
| 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 = false; |
| 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 = false; |
| 149 |
|
| 150 |
/** |
| 151 |
* Source list |
| 152 |
* |
| 153 |
* @var boolean |
| 154 |
*/ |
| 155 |
public static $sources = array(); |
| 156 |
|
| 157 |
/** |
| 158 |
* Set whether fields of this custom field type is applicalbe for search on ticket list page. |
| 159 |
* |
| 160 |
* @var boolean |
| 161 |
*/ |
| 162 |
public static $is_search = false; |
| 163 |
|
| 164 |
/** |
| 165 |
* Initialize the class |
| 166 |
* |
| 167 |
* @return void |
| 168 |
*/ |
| 169 |
public static function init() { |
| 170 |
|
| 171 |
// Get object of this class. |
| 172 |
add_filter( 'wpsc_load_ref_classes', array( __CLASS__, 'load_ref_class' ) ); |
| 173 |
|
| 174 |
// ticket form. |
| 175 |
add_filter( 'wpsc_create_ticket_data', array( __CLASS__, 'set_create_ticket_data' ), 10, 3 ); |
| 176 |
|
| 177 |
// create ticket data for rest api. |
| 178 |
add_filter( 'wpsc_rest_create_ticket', array( __CLASS__, 'set_rest_ticket_data' ), 10, 3 ); |
| 179 |
|
| 180 |
// source list. |
| 181 |
add_action( 'init', array( __CLASS__, 'load_last_reply_sources' ) ); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Load current class to reference classes |
| 186 |
* |
| 187 |
* @param array $classes - Associative array of class names indexed by its slug. |
| 188 |
* @return array |
| 189 |
*/ |
| 190 |
public static function load_ref_class( $classes ) { |
| 191 |
|
| 192 |
$classes[ self::$slug ] = array( |
| 193 |
'class' => __CLASS__, |
| 194 |
'save-key' => 'id', |
| 195 |
); |
| 196 |
return $classes; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Load last reply source list |
| 201 |
* |
| 202 |
* @return void |
| 203 |
*/ |
| 204 |
public static function load_last_reply_sources() { |
| 205 |
|
| 206 |
self::$sources = apply_filters( |
| 207 |
'wpsc_last_reply_source_list', |
| 208 |
array( |
| 209 |
'browser' => __( 'Browser', 'supportcandy' ), |
| 210 |
'rest-api' => 'REST API', |
| 211 |
) |
| 212 |
); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Print operators for ticket form filter |
| 217 |
* |
| 218 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 219 |
* @param array $filter - Existing filters (if any). |
| 220 |
* @return void |
| 221 |
*/ |
| 222 |
public static function get_operators( $cf, $filter = array() ) { |
| 223 |
?> |
| 224 |
|
| 225 |
<div class="item conditional"> |
| 226 |
<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' ) ); ?>');"> |
| 227 |
<option value=""><?php esc_attr_e( 'Compare As', 'supportcandy' ); ?></option> |
| 228 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '=' ); ?> value="="><?php esc_attr_e( 'Equals', 'supportcandy' ); ?></option> |
| 229 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'IN' ); ?> value="IN"><?php esc_attr_e( 'Matches', 'supportcandy' ); ?></option> |
| 230 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'NOT IN' ); ?> value="NOT IN"><?php esc_attr_e( 'Not Matches', 'supportcandy' ); ?></option> |
| 231 |
</select> |
| 232 |
</div> |
| 233 |
<?php |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Print operators for ticket form filter |
| 238 |
* |
| 239 |
* @param string $operator - condition operator on which operands should be returned. |
| 240 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 241 |
* @param array $filter - Exising functions (if any). |
| 242 |
* @return void |
| 243 |
*/ |
| 244 |
public static function get_operands( $operator, $cf, $filter = array() ) { |
| 245 |
|
| 246 |
$is_multiple = $operator !== '=' ? true : false; |
| 247 |
$unique_id = uniqid( 'wpsc_' ); |
| 248 |
?> |
| 249 |
|
| 250 |
<div class="item conditional operand single"> |
| 251 |
<select class="operand_val_1 <?php echo esc_attr( $unique_id ); ?>" <?php echo $is_multiple ? 'multiple' : ''; ?>> |
| 252 |
<?php |
| 253 |
foreach ( self::$sources as $key => $label ) { |
| 254 |
$selected = ''; |
| 255 |
if ( |
| 256 |
isset( $filter['operand_val_1'] ) && |
| 257 |
( |
| 258 |
( $is_multiple && in_array( $key, $filter['operand_val_1'] ) ) || |
| 259 |
( ! $is_multiple && $key == $filter['operand_val_1'] ) |
| 260 |
) |
| 261 |
) { |
| 262 |
$selected = 'selected'; |
| 263 |
} |
| 264 |
?> |
| 265 |
<option <?php echo esc_attr( $selected ); ?> value="<?php echo esc_attr( $key ); ?>"><?php echo esc_attr( $label ); ?></option> |
| 266 |
<?php |
| 267 |
} |
| 268 |
?> |
| 269 |
</select> |
| 270 |
</div> |
| 271 |
<script>jQuery('.operand_val_1.<?php echo esc_attr( $unique_id ); ?>').selectWoo();</script> |
| 272 |
<?php |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Parse filter and return sql query to be merged in ticket model query builder |
| 277 |
* |
| 278 |
* @param WPSC_Custom_Field $cf - custom field of this type. |
| 279 |
* @param mixed $compare - comparison operator. |
| 280 |
* @param mixed $val - value to compare. |
| 281 |
* @return string |
| 282 |
*/ |
| 283 |
public static function parse_filter( $cf, $compare, $val ) { |
| 284 |
|
| 285 |
$str = ''; |
| 286 |
|
| 287 |
switch ( $compare ) { |
| 288 |
|
| 289 |
case '=': |
| 290 |
$str = 't.' . $cf->slug . '=\'' . esc_sql( $val ) . '\''; |
| 291 |
break; |
| 292 |
|
| 293 |
case 'IN': |
| 294 |
$str = 'CONVERT(t.' . $cf->slug . ' USING utf8) IN(\'' . implode( '\', \'', esc_sql( $val ) ) . '\')'; |
| 295 |
break; |
| 296 |
|
| 297 |
case 'NOT IN': |
| 298 |
$str = 'CONVERT(t.' . $cf->slug . ' USING utf8) NOT IN(\'' . implode( '\', \'', esc_sql( $val ) ) . '\')'; |
| 299 |
break; |
| 300 |
|
| 301 |
default: |
| 302 |
$str = '1=1'; |
| 303 |
} |
| 304 |
|
| 305 |
return $str; |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Check ticket condition |
| 310 |
* |
| 311 |
* @param array $condition - array with condition data. |
| 312 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 313 |
* @param WPSC_Ticket $ticket - ticket object. |
| 314 |
* @return boolean |
| 315 |
*/ |
| 316 |
public static function is_valid_ticket_condition( $condition, $cf, $ticket ) { |
| 317 |
|
| 318 |
$flag = true; |
| 319 |
switch ( $condition['operator'] ) { |
| 320 |
|
| 321 |
case '=': |
| 322 |
$flag = $ticket->{$cf->slug} == $condition['operand_val_1'] ? true : false; |
| 323 |
break; |
| 324 |
|
| 325 |
case 'IN': |
| 326 |
$flag = in_array( $ticket->{$cf->slug}, $condition['operand_val_1'] ); |
| 327 |
break; |
| 328 |
|
| 329 |
case 'NOT IN': |
| 330 |
$flag = ! in_array( $ticket->{$cf->slug}, $condition['operand_val_1'] ); |
| 331 |
break; |
| 332 |
|
| 333 |
default: |
| 334 |
$flag = true; |
| 335 |
} |
| 336 |
|
| 337 |
return $flag; |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* Check and return custom field value for new ticket to be created. |
| 342 |
* This function is used by filter for set create ticket form and called directly by my-profile for each applicable custom fields. |
| 343 |
* Ignore phpcs nonce issue as we already checked where it is called from. |
| 344 |
* |
| 345 |
* @param array $data - Array of values to to stored in ticket in an insert function. |
| 346 |
* @param array $custom_fields - Array containing all applicable custom fields indexed by unique custom field types. |
| 347 |
* @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. |
| 348 |
* @return array |
| 349 |
*/ |
| 350 |
public static function set_create_ticket_data( $data, $custom_fields, $is_my_profile ) { |
| 351 |
|
| 352 |
$data['last_reply_source'] = 'browser'; |
| 353 |
return $data; |
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* Set create ticket data for rest api request |
| 358 |
* |
| 359 |
* @param array $data - create ticket data array. |
| 360 |
* @param WP_REST_Request $request - rest request object. |
| 361 |
* @param array $custom_fields - custom field objects indexed by unique custom field types. |
| 362 |
* @return array |
| 363 |
*/ |
| 364 |
public static function set_rest_ticket_data( $data, $request, $custom_fields ) { |
| 365 |
|
| 366 |
$data['last_reply_source'] = 'rest-api'; |
| 367 |
return $data; |
| 368 |
} |
| 369 |
|
| 370 |
/** |
| 371 |
* Return val field for meta query of this type of custom field |
| 372 |
* |
| 373 |
* @param array $condition - condition data. |
| 374 |
* @return mixed |
| 375 |
*/ |
| 376 |
public static function get_meta_value( $condition ) { |
| 377 |
|
| 378 |
$operator = $condition['operator']; |
| 379 |
switch ( $operator ) { |
| 380 |
|
| 381 |
case '=': |
| 382 |
case 'IN': |
| 383 |
case 'NOT IN': |
| 384 |
return $condition['operand_val_1']; |
| 385 |
} |
| 386 |
return false; |
| 387 |
} |
| 388 |
|
| 389 |
/** |
| 390 |
* Print edit custom field properties |
| 391 |
* |
| 392 |
* @param WPSC_Custom_Fields $cf - custom field object. |
| 393 |
* @param string $field_class - class name of field category. |
| 394 |
* @return void |
| 395 |
*/ |
| 396 |
public static function get_edit_custom_field_properties( $cf, $field_class ) { |
| 397 |
|
| 398 |
if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) : |
| 399 |
?> |
| 400 |
<div data-type="number" data-required="false" class="wpsc-input-group tl_width"> |
| 401 |
<div class="label-container"> |
| 402 |
<label for=""> |
| 403 |
<?php esc_attr_e( 'Ticket list width (pixels)', 'supportcandy' ); ?> |
| 404 |
</label> |
| 405 |
</div> |
| 406 |
<input type="number" name="tl_width" value="<?php echo intval( $cf->tl_width ); ?>" autocomplete="off"> |
| 407 |
</div> |
| 408 |
<?php |
| 409 |
endif; |
| 410 |
} |
| 411 |
|
| 412 |
/** |
| 413 |
* Set custom field properties. Can be used by add/edit custom field. |
| 414 |
* Ignore phpcs nonce issue as we already checked where it is called from. |
| 415 |
* |
| 416 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 417 |
* @param string $field_class - class of field category. |
| 418 |
* @return void |
| 419 |
*/ |
| 420 |
public static function set_cf_properties( $cf, $field_class ) { |
| 421 |
|
| 422 |
// tl_width! |
| 423 |
if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) { |
| 424 |
$tl_width = isset( $_POST['tl_width'] ) ? intval( $_POST['tl_width'] ) : 0; // phpcs:ignore |
| 425 |
$cf->tl_width = $tl_width ? $tl_width : 100; |
| 426 |
} |
| 427 |
|
| 428 |
// save! |
| 429 |
$cf->save(); |
| 430 |
} |
| 431 |
|
| 432 |
/** |
| 433 |
* Returns printable ticket value for custom field. Can be used in export tickets, replace macros etc. |
| 434 |
* |
| 435 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 436 |
* @param WPSC_Ticket $ticket - ticket object. |
| 437 |
* @param string $module - module name. |
| 438 |
* @return string |
| 439 |
*/ |
| 440 |
public static function get_ticket_field_val( $cf, $ticket, $module = '' ) { |
| 441 |
|
| 442 |
$value = $ticket->{$cf->slug} && isset( self::$sources[ $ticket->{$cf->slug} ] ) ? self::$sources[ $ticket->{$cf->slug} ] : ''; |
| 443 |
return apply_filters( 'wpsc_ticket_field_val_last_reply_source', $value, $cf, $ticket, $module ); |
| 444 |
} |
| 445 |
|
| 446 |
/** |
| 447 |
* Print ticket value for given custom field on ticket list |
| 448 |
* |
| 449 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 450 |
* @param WPSC_Ticket $ticket - ticket object. |
| 451 |
* @return void |
| 452 |
*/ |
| 453 |
public static function print_tl_ticket_field_val( $cf, $ticket ) { |
| 454 |
|
| 455 |
echo $ticket->{$cf->slug} && isset( self::$sources[ $ticket->{$cf->slug} ] ) ? esc_attr( self::$sources[ $ticket->{$cf->slug} ] ) : ''; |
| 456 |
} |
| 457 |
|
| 458 |
/** |
| 459 |
* Print ticket value for given custom field on widget |
| 460 |
* |
| 461 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 462 |
* @param WPSC_Ticket $ticket - ticket object. |
| 463 |
* @return void |
| 464 |
*/ |
| 465 |
public static function print_widget_ticket_field_val( $cf, $ticket ) { |
| 466 |
|
| 467 |
echo $ticket->{$cf->slug} && isset( self::$sources[ $ticket->{$cf->slug} ] ) ? esc_attr( self::$sources[ $ticket->{$cf->slug} ] ) : esc_attr__( 'Not Applicable', 'supportcandy' ); |
| 468 |
} |
| 469 |
} |
| 470 |
endif; |
| 471 |
|
| 472 |
WPSC_DF_Last_Reply_Source::init(); |
| 473 |
|