| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_DF_ID' ) ) : |
| 7 |
|
| 8 |
final class WPSC_DF_ID { |
| 9 |
|
| 10 |
/** |
| 11 |
* Slug for this custom field type |
| 12 |
* |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
public static $slug = 'df_id'; |
| 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 = 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 = 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 = false; |
| 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 = true; |
| 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 search query. |
| 168 |
add_filter( 'wpsc_ticket_search', array( __CLASS__, 'ticket_search' ), 10, 5 ); |
| 169 |
add_filter( 'wpsc_archive_ticket_search', array( __CLASS__, 'ticket_search' ), 10, 5 ); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Load current class to reference classes |
| 174 |
* |
| 175 |
* @param array $classes - Associative array of class names indexed by its slug. |
| 176 |
* @return array |
| 177 |
*/ |
| 178 |
public static function load_ref_class( $classes ) { |
| 179 |
|
| 180 |
$classes[ self::$slug ] = array( |
| 181 |
'class' => __CLASS__, |
| 182 |
'save-key' => 'id', |
| 183 |
); |
| 184 |
return $classes; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Print operators for ticket form filter |
| 189 |
* |
| 190 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 191 |
* @param array $filter - Existing filters (if any). |
| 192 |
* @return void |
| 193 |
*/ |
| 194 |
public static function get_operators( $cf, $filter = array() ) { |
| 195 |
?> |
| 196 |
|
| 197 |
<div class="item conditional"> |
| 198 |
<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' ) ); ?>');"> |
| 199 |
<option value=""><?php esc_attr_e( 'Compare As', 'supportcandy' ); ?></option> |
| 200 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '=' ); ?> value="="><?php esc_attr_e( 'Equals', 'supportcandy' ); ?></option> |
| 201 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'IN' ); ?> value="IN"><?php esc_attr_e( 'Matches', 'supportcandy' ); ?></option> |
| 202 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'NOT IN' ); ?> value="NOT IN"><?php esc_attr_e( 'Not Matches', 'supportcandy' ); ?></option> |
| 203 |
</select> |
| 204 |
</div> |
| 205 |
<?php |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Print operators for ticket form filter |
| 210 |
* |
| 211 |
* @param string $operator - condition operator on which operands should be returned. |
| 212 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 213 |
* @param array $filter - Exising functions (if any). |
| 214 |
* @return void |
| 215 |
*/ |
| 216 |
public static function get_operands( $operator, $cf, $filter = array() ) { |
| 217 |
|
| 218 |
$value = isset( $filter['operand_val_1'] ) ? stripslashes( $filter['operand_val_1'] ) : ''; |
| 219 |
if ( in_array( $operator, array( 'IN', 'NOT IN' ) ) ) { |
| 220 |
?> |
| 221 |
|
| 222 |
<div class="item conditional operand single"> |
| 223 |
<textarea class="operand_val_1" placeholder="<?php esc_attr_e( 'One condition per line!', 'supportcandy' ); ?>" style="width: 100%;"><?php echo esc_attr( $value ); ?></textarea> |
| 224 |
</div> |
| 225 |
<?php |
| 226 |
|
| 227 |
} else { |
| 228 |
?> |
| 229 |
|
| 230 |
<div class="item conditional operand single"> |
| 231 |
<input |
| 232 |
type="text" |
| 233 |
class="operand_val_1" |
| 234 |
onchange="wpsc_check_tff_visibility()" |
| 235 |
onkeydown="javascript: return event.keyCode == 69 ? false : true" |
| 236 |
oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');" |
| 237 |
value="<?php echo intval( $value ); ?>" |
| 238 |
autocomplete="off"/> |
| 239 |
</div> |
| 240 |
<?php |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Parse filter and return sql query to be merged in ticket model query builder |
| 246 |
* |
| 247 |
* @param WPSC_Custom_Field $cf - custom field of this type. |
| 248 |
* @param mixed $compare - comparison operator. |
| 249 |
* @param mixed $val - value to compare. |
| 250 |
* @return string |
| 251 |
*/ |
| 252 |
public static function parse_filter( $cf, $compare, $val ) { |
| 253 |
|
| 254 |
$str = ''; |
| 255 |
|
| 256 |
switch ( $compare ) { |
| 257 |
|
| 258 |
case '=': |
| 259 |
$str = 't.' . $cf->slug . '=\'' . esc_sql( $val ) . '\''; |
| 260 |
break; |
| 261 |
|
| 262 |
case 'IN': |
| 263 |
$str = 't.' . $cf->slug . ' IN(\'' . implode( '\', \'', esc_sql( $val ) ) . '\')'; |
| 264 |
break; |
| 265 |
|
| 266 |
case 'NOT IN': |
| 267 |
$str = 't.' . $cf->slug . ' NOT IN(\'' . implode( '\', \'', esc_sql( $val ) ) . '\')'; |
| 268 |
break; |
| 269 |
|
| 270 |
default: |
| 271 |
$str = '1=1'; |
| 272 |
} |
| 273 |
|
| 274 |
return $str; |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Check ticket condition |
| 279 |
* |
| 280 |
* @param array $condition - array with condition data. |
| 281 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 282 |
* @param WPSC_Ticket $ticket - ticket object. |
| 283 |
* @return boolean |
| 284 |
*/ |
| 285 |
public static function is_valid_ticket_condition( $condition, $cf, $ticket ) { |
| 286 |
|
| 287 |
$flag = true; |
| 288 |
$value = stripslashes( $ticket->{$cf->slug} ); |
| 289 |
$terms = explode( PHP_EOL, $condition['operand_val_1'] ); |
| 290 |
|
| 291 |
if ( ! $value ) { |
| 292 |
return false; |
| 293 |
} |
| 294 |
|
| 295 |
switch ( $condition['operator'] ) { |
| 296 |
|
| 297 |
case '=': |
| 298 |
$flag = $condition['operand_val_1'] == $value ? true : false; |
| 299 |
break; |
| 300 |
|
| 301 |
case 'IN': |
| 302 |
$flag = false; |
| 303 |
foreach ( $terms as $term ) { |
| 304 |
$term = intval( trim( $term ) ); |
| 305 |
if ( $term == $value ) { |
| 306 |
$flag = true; |
| 307 |
break; |
| 308 |
} |
| 309 |
} |
| 310 |
break; |
| 311 |
|
| 312 |
case 'NOT IN': |
| 313 |
foreach ( $terms as $term ) { |
| 314 |
$term = intval( trim( $term ) ); |
| 315 |
if ( $term == $value ) { |
| 316 |
$flag = false; |
| 317 |
break; |
| 318 |
} |
| 319 |
} |
| 320 |
break; |
| 321 |
|
| 322 |
default: |
| 323 |
$flag = true; |
| 324 |
} |
| 325 |
|
| 326 |
return $flag; |
| 327 |
} |
| 328 |
|
| 329 |
/** |
| 330 |
* Add ticket search compatibility for fields of this custom field type. |
| 331 |
* |
| 332 |
* @param array $sql - Array of sql peices that can be joined later. |
| 333 |
* @param array $filter - User filter. |
| 334 |
* @param array $custom_fields - Custom fields array applicable for search. |
| 335 |
* @param string $search - search string. |
| 336 |
* @param array $allowed_search_fields - Allowed search fields. |
| 337 |
* @return array |
| 338 |
*/ |
| 339 |
public static function ticket_search( $sql, $filter, $custom_fields, $search, $allowed_search_fields ) { |
| 340 |
|
| 341 |
if ( in_array( 'id', $allowed_search_fields ) ) { |
| 342 |
$sql[] = 't.id LIKE \'%' . $search . '%\''; |
| 343 |
} |
| 344 |
return $sql; |
| 345 |
} |
| 346 |
|
| 347 |
/** |
| 348 |
* Return orderby string |
| 349 |
* |
| 350 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 351 |
* @return string |
| 352 |
*/ |
| 353 |
public static function get_orderby_string( $cf ) { |
| 354 |
|
| 355 |
return 't.id'; |
| 356 |
} |
| 357 |
|
| 358 |
/** |
| 359 |
* Return val field for meta query of this type of custom field |
| 360 |
* |
| 361 |
* @param array $condition - condition data. |
| 362 |
* @return mixed |
| 363 |
*/ |
| 364 |
public static function get_meta_value( $condition ) { |
| 365 |
|
| 366 |
$operator = $condition['operator']; |
| 367 |
switch ( $operator ) { |
| 368 |
|
| 369 |
case '=': |
| 370 |
return $condition['operand_val_1']; |
| 371 |
|
| 372 |
case 'IN': |
| 373 |
case 'NOT IN': |
| 374 |
$val = explode( PHP_EOL, $condition['operand_val_1'] ); |
| 375 |
$temp_val = array(); |
| 376 |
foreach ( $val as $id ) { |
| 377 |
$id = intval( trim( $id ) ); |
| 378 |
if ( $id ) { |
| 379 |
$temp_val[] = $id; |
| 380 |
} |
| 381 |
} |
| 382 |
return $temp_val ? $temp_val : false; |
| 383 |
} |
| 384 |
return false; |
| 385 |
} |
| 386 |
|
| 387 |
/** |
| 388 |
* Print edit custom field properties |
| 389 |
* |
| 390 |
* @param WPSC_Custom_Fields $cf - custom field object. |
| 391 |
* @param string $field_class - class name of field category. |
| 392 |
* @return void |
| 393 |
*/ |
| 394 |
public static function get_edit_custom_field_properties( $cf, $field_class ) { |
| 395 |
|
| 396 |
if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) : |
| 397 |
?> |
| 398 |
<div data-type="number" data-required="false" class="wpsc-input-group tl_width"> |
| 399 |
<div class="label-container"> |
| 400 |
<label for=""> |
| 401 |
<?php esc_attr_e( 'Ticket list width (pixels)', 'supportcandy' ); ?> |
| 402 |
</label> |
| 403 |
</div> |
| 404 |
<input type="number" name="tl_width" value="<?php echo intval( $cf->tl_width ); ?>" autocomplete="off"> |
| 405 |
</div> |
| 406 |
<?php |
| 407 |
endif; |
| 408 |
} |
| 409 |
|
| 410 |
/** |
| 411 |
* Set custom field properties. Can be used by add/edit custom field. |
| 412 |
* Ignore phpcs nonce issue as we already checked where it is called from. |
| 413 |
* |
| 414 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 415 |
* @param string $field_class - class of field category. |
| 416 |
* @return void |
| 417 |
*/ |
| 418 |
public static function set_cf_properties( $cf, $field_class ) { |
| 419 |
|
| 420 |
// tl_width! |
| 421 |
if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) { |
| 422 |
$tl_width = isset( $_POST['tl_width'] ) ? intval( $_POST['tl_width'] ) : 0; // phpcs:ignore |
| 423 |
$cf->tl_width = $tl_width ? $tl_width : 100; |
| 424 |
} |
| 425 |
|
| 426 |
// save! |
| 427 |
$cf->save(); |
| 428 |
} |
| 429 |
|
| 430 |
/** |
| 431 |
* Returns printable ticket value for custom field. Can be used in export tickets, replace macros etc. |
| 432 |
* |
| 433 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 434 |
* @param WPSC_Ticket $ticket - ticket object. |
| 435 |
* @param string $module - module name. |
| 436 |
* @return string |
| 437 |
*/ |
| 438 |
public static function get_ticket_field_val( $cf, $ticket, $module = '' ) { |
| 439 |
|
| 440 |
return $ticket->id; |
| 441 |
} |
| 442 |
|
| 443 |
/** |
| 444 |
* Print ticket value for given custom field on ticket list |
| 445 |
* |
| 446 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 447 |
* @param WPSC_Ticket $ticket - ticket object. |
| 448 |
* @return void |
| 449 |
*/ |
| 450 |
public static function print_tl_ticket_field_val( $cf, $ticket ) { |
| 451 |
|
| 452 |
echo '#' . esc_attr( $ticket->{$cf->slug} ); |
| 453 |
} |
| 454 |
} |
| 455 |
endif; |
| 456 |
|
| 457 |
WPSC_DF_ID::init(); |
| 458 |
|