| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_CF_Single_Select' ) ) : |
| 7 |
|
| 8 |
final class WPSC_CF_Single_Select { |
| 9 |
|
| 10 |
/** |
| 11 |
* Slug for this custom field type |
| 12 |
* |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
public static $slug = 'cf_single_select'; |
| 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 = 'TINYTEXT NULL DEFAULT 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_option'; |
| 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 = false; |
| 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 = true; |
| 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 = true; |
| 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 = true; |
| 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 = true; |
| 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 = true; |
| 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 = true; |
| 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 = 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 |
// Set custom field type. |
| 168 |
add_filter( 'wpsc_cf_types', array( __CLASS__, 'add_cf_type' ), 2 ); |
| 169 |
|
| 170 |
// ticket form. |
| 171 |
add_action( 'wpsc_js_validate_ticket_form', array( __CLASS__, 'js_validate_ticket_form' ) ); |
| 172 |
add_filter( 'wpsc_create_ticket_data', array( __CLASS__, 'set_create_ticket_data' ), 10, 3 ); |
| 173 |
add_action( 'wpsc_js_clear_value_hidden_fields', array( __CLASS__, 'js_clear_value_hidden_fields' ) ); |
| 174 |
|
| 175 |
// create ticket data for rest api. |
| 176 |
add_filter( 'wpsc_rest_create_ticket', array( __CLASS__, 'set_rest_ticket_data' ), 10, 3 ); |
| 177 |
|
| 178 |
// Ticket model. |
| 179 |
add_filter( 'wpsc_ticket_joins', array( __CLASS__, 'ticket_join' ), 9, 2 ); |
| 180 |
add_filter( 'wpsc_archive_ticket_joins', array( __CLASS__, 'ticket_join' ), 9, 2 ); |
| 181 |
|
| 182 |
// ticket search query. |
| 183 |
add_filter( 'wpsc_ticket_search', array( __CLASS__, 'ticket_search' ), 10, 5 ); |
| 184 |
add_filter( 'wpsc_archive_ticket_search', array( __CLASS__, 'ticket_search' ), 10, 5 ); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Load current class to reference classes |
| 189 |
* |
| 190 |
* @param array $classes - Associative array of class names indexed by its slug. |
| 191 |
* @return array |
| 192 |
*/ |
| 193 |
public static function load_ref_class( $classes ) { |
| 194 |
|
| 195 |
$classes[ self::$slug ] = array( |
| 196 |
'class' => __CLASS__, |
| 197 |
'save-key' => 'id', |
| 198 |
); |
| 199 |
return $classes; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Add custom field type to list |
| 204 |
* |
| 205 |
* @param array $cf_types - custom field types array. |
| 206 |
* @return array |
| 207 |
*/ |
| 208 |
public static function add_cf_type( $cf_types ) { |
| 209 |
|
| 210 |
$cf_types[ self::$slug ] = array( |
| 211 |
'label' => esc_attr__( 'Dropdown (single-select)', 'supportcandy' ), |
| 212 |
'class' => __CLASS__, |
| 213 |
); |
| 214 |
return $cf_types; |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Print operators for ticket form filter |
| 219 |
* |
| 220 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 221 |
* @param array $filter - Existing filters (if any). |
| 222 |
* @return void |
| 223 |
*/ |
| 224 |
public static function get_operators( $cf, $filter = array() ) { |
| 225 |
?> |
| 226 |
|
| 227 |
<div class="item conditional"> |
| 228 |
<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' ) ); ?>');"> |
| 229 |
<option value=""><?php esc_attr_e( 'Compare As', 'supportcandy' ); ?></option> |
| 230 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '=' ); ?> value="="><?php esc_attr_e( 'Equals', 'supportcandy' ); ?></option> |
| 231 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'IN' ); ?> value="IN"><?php esc_attr_e( 'Matches', 'supportcandy' ); ?></option> |
| 232 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'NOT IN' ); ?> value="NOT IN"><?php esc_attr_e( 'Not Matches', 'supportcandy' ); ?></option> |
| 233 |
</select> |
| 234 |
</div> |
| 235 |
<?php |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Print operators for ticket form filter |
| 240 |
* |
| 241 |
* @param string $operator - condition operator on which operands should be returned. |
| 242 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 243 |
* @param array $filter - Exising functions (if any). |
| 244 |
* @return void |
| 245 |
*/ |
| 246 |
public static function get_operands( $operator, $cf, $filter = array() ) { |
| 247 |
|
| 248 |
$is_multiple = $operator !== '=' ? true : false; |
| 249 |
$options = $cf->get_options(); |
| 250 |
$unique_id = uniqid( 'wpsc_' ); |
| 251 |
?> |
| 252 |
<div class="item conditional operand single"> |
| 253 |
<select class="operand_val_1 <?php echo esc_attr( $unique_id ); ?>" <?php echo $is_multiple ? 'multiple' : ''; ?>> |
| 254 |
<?php |
| 255 |
foreach ( $options as $option ) { |
| 256 |
$selected = ''; |
| 257 |
if ( isset( $filter['operand_val_1'] ) && ( ( $is_multiple && in_array( $option->id, $filter['operand_val_1'] ) ) || ( ! $is_multiple && $option->id == $filter['operand_val_1'] ) ) ) { |
| 258 |
$selected = 'selected="selected"'; |
| 259 |
} |
| 260 |
?> |
| 261 |
<option <?php echo esc_attr( $selected ); ?> value="<?php echo esc_attr( $option->id ); ?>"><?php echo esc_attr( $option->name ); ?></option> |
| 262 |
<?php |
| 263 |
} |
| 264 |
?> |
| 265 |
</select> |
| 266 |
</div> |
| 267 |
<script>jQuery('.operand_val_1.<?php echo esc_attr( $unique_id ); ?>').selectWoo();</script> |
| 268 |
<?php |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* Check condition for this type |
| 273 |
* |
| 274 |
* @param array $condition - condition data. |
| 275 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 276 |
* @param mixed $option - value to compare. |
| 277 |
* @return boolean |
| 278 |
*/ |
| 279 |
public static function is_valid( $condition, $cf, $option ) { |
| 280 |
|
| 281 |
$response = false; |
| 282 |
switch ( $condition['operator'] ) { |
| 283 |
|
| 284 |
case '=': |
| 285 |
$response = $condition['operand_val_1'] == $option ? true : false; |
| 286 |
break; |
| 287 |
|
| 288 |
case 'IN': |
| 289 |
$response = in_array( $option, $condition['operand_val_1'] ) ? true : false; |
| 290 |
break; |
| 291 |
|
| 292 |
case 'NOT IN': |
| 293 |
$response = ! in_array( $option, $condition['operand_val_1'] ) ? true : false; |
| 294 |
break; |
| 295 |
} |
| 296 |
return $response; |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* Parse filter and return sql query to be merged in ticket model query builder |
| 301 |
* |
| 302 |
* @param WPSC_Custom_Field $cf - custom field of this type. |
| 303 |
* @param mixed $compare - comparison operator. |
| 304 |
* @param mixed $val - value to compare. |
| 305 |
* @return string |
| 306 |
*/ |
| 307 |
public static function parse_filter( $cf, $compare, $val ) { |
| 308 |
|
| 309 |
global $wpdb; |
| 310 |
|
| 311 |
$column = self::get_sql_slug( $cf ); |
| 312 |
|
| 313 |
switch ( $compare ) { |
| 314 |
|
| 315 |
case '=': |
| 316 |
return $wpdb->prepare( |
| 317 |
"{$column} = %s", |
| 318 |
(string) $val |
| 319 |
); |
| 320 |
|
| 321 |
case 'IN': |
| 322 |
case 'NOT IN': |
| 323 |
if ( ! is_array( $val ) || empty( $val ) ) { |
| 324 |
return ( $compare === 'IN' ) ? '1=0' : '1=1'; |
| 325 |
} |
| 326 |
|
| 327 |
$placeholders = implode( |
| 328 |
',', |
| 329 |
array_fill( 0, count( $val ), '%s' ) |
| 330 |
); |
| 331 |
|
| 332 |
return $wpdb->prepare( |
| 333 |
"CONVERT({$column} USING utf8) {$compare} ({$placeholders})", |
| 334 |
array_values( $val ) |
| 335 |
); |
| 336 |
|
| 337 |
default: |
| 338 |
return '1=1'; |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
/** |
| 343 |
* Return orderby string |
| 344 |
* |
| 345 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 346 |
* @return string |
| 347 |
*/ |
| 348 |
public static function get_orderby_string( $cf ) { |
| 349 |
|
| 350 |
return 'op.name'; |
| 351 |
} |
| 352 |
|
| 353 |
/** |
| 354 |
* Check ticket condition |
| 355 |
* |
| 356 |
* @param array $condition - array with condition data. |
| 357 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 358 |
* @param WPSC_Ticket $ticket - ticket object. |
| 359 |
* @return boolean |
| 360 |
*/ |
| 361 |
public static function is_valid_ticket_condition( $condition, $cf, $ticket ) { |
| 362 |
|
| 363 |
if ( ! is_object( $ticket->{$cf->slug} ) ) { |
| 364 |
return false; |
| 365 |
} |
| 366 |
|
| 367 |
$flag = true; |
| 368 |
|
| 369 |
switch ( $condition['operator'] ) { |
| 370 |
|
| 371 |
case '=': |
| 372 |
$flag = $ticket->{$cf->slug}->id == $condition['operand_val_1'] ? true : false; |
| 373 |
break; |
| 374 |
|
| 375 |
case 'IN': |
| 376 |
$flag = in_array( $ticket->{$cf->slug}->id, $condition['operand_val_1'] ); |
| 377 |
break; |
| 378 |
|
| 379 |
case 'NOT IN': |
| 380 |
$flag = ! in_array( $ticket->{$cf->slug}->id, $condition['operand_val_1'] ); |
| 381 |
break; |
| 382 |
|
| 383 |
default: |
| 384 |
$flag = true; |
| 385 |
} |
| 386 |
|
| 387 |
return $flag; |
| 388 |
} |
| 389 |
|
| 390 |
/** |
| 391 |
* Check customer condition |
| 392 |
* |
| 393 |
* @param array $condition - array with condition data. |
| 394 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 395 |
* @param WPSC_Customer $customer - customer object. |
| 396 |
* @return boolean |
| 397 |
*/ |
| 398 |
public static function is_valid_customer_condition( $condition, $cf, $customer ) { |
| 399 |
|
| 400 |
if ( ! is_object( $customer->{$cf->slug} ) ) { |
| 401 |
return false; |
| 402 |
} |
| 403 |
|
| 404 |
$flag = true; |
| 405 |
|
| 406 |
switch ( $condition['operator'] ) { |
| 407 |
|
| 408 |
case '=': |
| 409 |
$flag = $customer->{$cf->slug}->id == $condition['operand_val_1'] ? true : false; |
| 410 |
break; |
| 411 |
|
| 412 |
case 'IN': |
| 413 |
$flag = in_array( $customer->{$cf->slug}->id, $condition['operand_val_1'] ); |
| 414 |
break; |
| 415 |
|
| 416 |
case 'NOT IN': |
| 417 |
$flag = ! in_array( $customer->{$cf->slug}->id, $condition['operand_val_1'] ); |
| 418 |
break; |
| 419 |
|
| 420 |
default: |
| 421 |
$flag = true; |
| 422 |
} |
| 423 |
|
| 424 |
return $flag; |
| 425 |
} |
| 426 |
|
| 427 |
/** |
| 428 |
* Print ticket form field |
| 429 |
* |
| 430 |
* @param WPSC_Custom_Field $cf - Custom field object. |
| 431 |
* @param array $tff - Array of ticket form field settings for this field. |
| 432 |
* @return string |
| 433 |
*/ |
| 434 |
public static function print_tff( $cf, $tff ) { |
| 435 |
|
| 436 |
$options = $cf->get_options(); |
| 437 |
$val = ''; |
| 438 |
if ( $cf->field == 'ticket' ) { |
| 439 |
$val = $cf->is_auto_fill && $cf->default_value ? stripslashes( htmlspecialchars( $cf->default_value[0] ) ) : ''; |
| 440 |
} else { |
| 441 |
$current_user = WPSC_Current_User::$current_user; |
| 442 |
$val = $current_user->is_customer && $current_user->customer->{$cf->slug} ? intval( $current_user->customer->{$cf->slug}->id ) : 0; |
| 443 |
} |
| 444 |
$unique_id = uniqid( 'wpsc_' ); |
| 445 |
|
| 446 |
ob_start(); |
| 447 |
?> |
| 448 |
<div class="<?php echo esc_attr( WPSC_Functions::get_tff_classes( $cf, $tff ) ); ?>" data-cft="<?php echo esc_attr( self::$slug ); ?>" data-slug="<?php echo esc_attr( $cf->slug ); ?>"> |
| 449 |
<div class="wpsc-tff-label"> |
| 450 |
<span class="name"><?php echo esc_attr( $cf->name ); ?></span> |
| 451 |
<?php |
| 452 |
if ( $tff['is-required'] ) { |
| 453 |
?> |
| 454 |
<span class="required-indicator">*</span> |
| 455 |
<?php |
| 456 |
} |
| 457 |
?> |
| 458 |
</div> |
| 459 |
<span class="extra-info"><?php echo esc_attr( $cf->extra_info ); ?></span> |
| 460 |
<select class="<?php echo esc_attr( $unique_id ); ?>" name="<?php echo esc_attr( $cf->slug ); ?>" onchange="wpsc_check_tff_visibility()"> |
| 461 |
<option value=""></option> |
| 462 |
<?php |
| 463 |
foreach ( $options as $option ) : |
| 464 |
?> |
| 465 |
<option <?php selected( $option->id, $val ); ?> value="<?php echo esc_attr( $option->id ); ?>"><?php echo esc_attr( $option->name ); ?></option> |
| 466 |
<?php |
| 467 |
endforeach; |
| 468 |
?> |
| 469 |
</select> |
| 470 |
<script> |
| 471 |
jQuery('select.<?php echo esc_attr( $unique_id ); ?>').selectWoo({ |
| 472 |
allowClear: true, |
| 473 |
placeholder: "<?php echo esc_attr( $cf->placeholder_text ); ?>" |
| 474 |
}); |
| 475 |
</script> |
| 476 |
</div> |
| 477 |
<?php |
| 478 |
return ob_get_clean(); |
| 479 |
} |
| 480 |
|
| 481 |
/** |
| 482 |
* Validate this type field in create ticket |
| 483 |
* |
| 484 |
* @return void |
| 485 |
*/ |
| 486 |
public static function js_validate_ticket_form() { |
| 487 |
?> |
| 488 |
|
| 489 |
case '<?php echo esc_attr( self::$slug ); ?>': |
| 490 |
var val = customField.find('select').first().val(); |
| 491 |
if (isRequired && !val) { |
| 492 |
isValid = false; |
| 493 |
} |
| 494 |
break; |
| 495 |
<?php |
| 496 |
echo PHP_EOL; |
| 497 |
} |
| 498 |
|
| 499 |
/** |
| 500 |
* Return custom field value in $_POST |
| 501 |
* Ignore phpcs nonce issue as we already checked where it is called from. |
| 502 |
* |
| 503 |
* @param string $slug - Custom field slug. |
| 504 |
* @param mixed $cf - Custom field object or false. |
| 505 |
* @return mixed |
| 506 |
*/ |
| 507 |
public static function get_tff_value( $slug, $cf = false ) { |
| 508 |
|
| 509 |
return isset( $_POST[ $slug ] ) ? intval( $_POST[ $slug ] ) : 0; // phpcs:ignore |
| 510 |
} |
| 511 |
|
| 512 |
/** |
| 513 |
* Return default value for custom field of this type |
| 514 |
* |
| 515 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 516 |
* @return mixed |
| 517 |
*/ |
| 518 |
public static function get_default_value( $cf ) { |
| 519 |
|
| 520 |
return $cf->default_value ? $cf->default_value[0] : ''; |
| 521 |
} |
| 522 |
|
| 523 |
/** |
| 524 |
* Check and return custom field value for new ticket to be created. |
| 525 |
* This function is used by filter for set create ticket form and called directly by my-profile for each applicable custom fields. |
| 526 |
* Ignore phpcs nonce issue as we already checked where it is called from. |
| 527 |
* |
| 528 |
* @param array $data - Array of values to to stored in ticket in an insert function. |
| 529 |
* @param array $custom_fields - Array containing all applicable custom fields indexed by unique custom field types. |
| 530 |
* @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. |
| 531 |
* @return array |
| 532 |
*/ |
| 533 |
public static function set_create_ticket_data( $data, $custom_fields, $is_my_profile ) { |
| 534 |
|
| 535 |
if ( isset( $custom_fields[ self::$slug ] ) ) { |
| 536 |
foreach ( $custom_fields[ self::$slug ] as $cf ) { |
| 537 |
$value = self::get_tff_value( $cf->slug ); |
| 538 |
if ( $cf->field == 'ticket' ) { |
| 539 |
|
| 540 |
$default_val = self::get_default_value( $cf ); |
| 541 |
$data[ $cf->slug ] = $value ? $value : $default_val; |
| 542 |
|
| 543 |
} elseif ( $cf->field == 'agentonly' ) { |
| 544 |
|
| 545 |
$data[ $cf->slug ] = self::get_default_value( $cf ); |
| 546 |
|
| 547 |
} elseif ( $cf->field == 'customer' && $data['customer'] != 0 ) { |
| 548 |
|
| 549 |
$tff = get_option( 'wpsc-tff' ); |
| 550 |
if ( ! $is_my_profile && ! isset( $tff[ $cf->slug ] ) ) { |
| 551 |
continue; |
| 552 |
} |
| 553 |
$customer = new WPSC_Customer( $data['customer'] ); |
| 554 |
$existing_val = $customer->{$cf->slug} ? $customer->{$cf->slug}->id : ''; |
| 555 |
$value = $value === 0 ? '' : $value; |
| 556 |
if ( $existing_val != $value ) { |
| 557 |
$customer->{$cf->slug} = $value; |
| 558 |
$customer->save(); |
| 559 |
|
| 560 |
// Set log for this change. |
| 561 |
WPSC_Log::insert( |
| 562 |
array( |
| 563 |
'type' => 'customer', |
| 564 |
'ref_id' => $customer->id, |
| 565 |
'modified_by' => WPSC_Current_User::$current_user->customer->id, |
| 566 |
'body' => wp_json_encode( |
| 567 |
array( |
| 568 |
'slug' => $cf->slug, |
| 569 |
'prev' => $existing_val, |
| 570 |
'new' => $value, |
| 571 |
) |
| 572 |
), |
| 573 |
'date_created' => ( new DateTime() )->format( 'Y-m-d H:i:s' ), |
| 574 |
) |
| 575 |
); |
| 576 |
} |
| 577 |
} |
| 578 |
} |
| 579 |
} |
| 580 |
return $data; |
| 581 |
} |
| 582 |
|
| 583 |
/** |
| 584 |
* Set create ticket data for rest api request |
| 585 |
* |
| 586 |
* @param array $data - create ticket data array. |
| 587 |
* @param WP_REST_Request $request - rest request object. |
| 588 |
* @param array $custom_fields - custom field objects indexed by unique custom field types. |
| 589 |
* @return array |
| 590 |
*/ |
| 591 |
public static function set_rest_ticket_data( $data, $request, $custom_fields ) { |
| 592 |
|
| 593 |
$current_user = WPSC_Current_User::$current_user; |
| 594 |
$tff = get_option( 'wpsc-tff' ); |
| 595 |
|
| 596 |
if ( isset( $custom_fields[ self::$slug ] ) ) { |
| 597 |
foreach ( $custom_fields[ self::$slug ] as $cf ) { |
| 598 |
|
| 599 |
if ( |
| 600 |
! in_array( $cf->field, array( 'ticket', 'agentonly', 'customer' ) ) || |
| 601 |
( $cf->field == 'customer' && ! isset( $tff[ $cf->slug ] ) ) |
| 602 |
) { |
| 603 |
continue; |
| 604 |
} |
| 605 |
|
| 606 |
$option = new WPSC_Option( intval( $request->get_param( $cf->slug ) ) ); |
| 607 |
|
| 608 |
if ( in_array( $cf->field, array( 'ticket', 'agentonly' ) ) ) { |
| 609 |
|
| 610 |
$data[ $cf->slug ] = $cf->field == 'ticket' && $option->id ? $option->id : self::get_default_value( $cf ); |
| 611 |
|
| 612 |
} else { |
| 613 |
|
| 614 |
$customer = new WPSC_Customer( $data['customer'] ); |
| 615 |
$existing_val = $customer->{$cf->slug}; |
| 616 |
|
| 617 |
if ( $option->id && $option->id != $existing_val->id ) { |
| 618 |
|
| 619 |
$customer->{$cf->slug} = $option; |
| 620 |
$customer->save(); |
| 621 |
|
| 622 |
// Set log for this change.. |
| 623 |
WPSC_Log::insert( |
| 624 |
array( |
| 625 |
'type' => 'customer', |
| 626 |
'ref_id' => $customer->id, |
| 627 |
'modified_by' => $current_user->customer->id, |
| 628 |
'body' => wp_json_encode( |
| 629 |
array( |
| 630 |
'slug' => $cf->slug, |
| 631 |
'prev' => $existing_val->id, |
| 632 |
'new' => $option->id, |
| 633 |
) |
| 634 |
), |
| 635 |
'date_created' => ( new DateTime() )->format( 'Y-m-d H:i:s' ), |
| 636 |
) |
| 637 |
); |
| 638 |
} |
| 639 |
} |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
return $data; |
| 644 |
} |
| 645 |
|
| 646 |
/** |
| 647 |
* Return slug string to be used in where condition of ticket model for this type of field |
| 648 |
* |
| 649 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 650 |
* @return string |
| 651 |
*/ |
| 652 |
public static function get_sql_slug( $cf ) { |
| 653 |
|
| 654 |
$join_char = in_array( $cf->field, array( 'ticket', 'agentonly' ) ) ? 't.' : 'c.'; |
| 655 |
return $join_char . $cf->slug; |
| 656 |
} |
| 657 |
|
| 658 |
/** |
| 659 |
* Return val field for meta query of this type of custom field |
| 660 |
* |
| 661 |
* @param array $condition - condition data. |
| 662 |
* @return mixed |
| 663 |
*/ |
| 664 |
public static function get_meta_value( $condition ) { |
| 665 |
|
| 666 |
$operator = $condition['operator']; |
| 667 |
switch ( $operator ) { |
| 668 |
|
| 669 |
case '=': |
| 670 |
case 'IN': |
| 671 |
case 'NOT IN': |
| 672 |
return $condition['operand_val_1']; |
| 673 |
} |
| 674 |
return false; |
| 675 |
} |
| 676 |
|
| 677 |
/** |
| 678 |
* Print generic input field for this type. |
| 679 |
* |
| 680 |
* @param WPSC_Custom_Field $cf - Custom field object. |
| 681 |
* @param mixed $value - optional. Input field will be printed with given value if given. |
| 682 |
* @return void |
| 683 |
*/ |
| 684 |
public static function print_cf_input( $cf, $value = '' ) { |
| 685 |
|
| 686 |
if ( $value && ! is_object( $value ) ) { |
| 687 |
|
| 688 |
$option = new WPSC_Option( $value ); |
| 689 |
if ( $option && $option->id ) { |
| 690 |
$value = $option; |
| 691 |
} |
| 692 |
} |
| 693 |
|
| 694 |
$options = $cf->get_options(); |
| 695 |
$unique_id = uniqid( 'wpsc_' ); |
| 696 |
?> |
| 697 |
<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 ); ?>"> |
| 698 |
<div class="wpsc-tff-label"> |
| 699 |
<span class="name"><?php echo esc_attr( $cf->name ); ?></span> |
| 700 |
</div> |
| 701 |
<?php |
| 702 |
$extra_info = stripslashes( $cf->extra_info ); |
| 703 |
if ( $extra_info ) : |
| 704 |
?> |
| 705 |
<span class="extra-info"><?php echo esc_attr( $cf->extra_info ); ?></span> |
| 706 |
<?php |
| 707 |
endif |
| 708 |
?> |
| 709 |
<select class="<?php echo esc_attr( $unique_id ); ?>" name="<?php echo esc_attr( $cf->slug ); ?>"> |
| 710 |
<option value=""></option> |
| 711 |
<?php |
| 712 |
foreach ( $options as $option ) { |
| 713 |
?> |
| 714 |
<option <?php $value && selected( $option->id, $value->id ); ?> value="<?php echo esc_attr( $option->id ); ?>"><?php echo esc_attr( $option->name ); ?></option> |
| 715 |
<?php |
| 716 |
} |
| 717 |
?> |
| 718 |
</select> |
| 719 |
<script> |
| 720 |
jQuery('select.<?php echo esc_attr( $unique_id ); ?>').selectWoo({ |
| 721 |
allowClear: true, |
| 722 |
placeholder: "" |
| 723 |
}); |
| 724 |
</script> |
| 725 |
</div> |
| 726 |
<?php |
| 727 |
} |
| 728 |
|
| 729 |
/** |
| 730 |
* Get value for generic field from post. |
| 731 |
* Ignore phpcs nonce issue as we already checked where it is called from. |
| 732 |
* |
| 733 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 734 |
* @return string |
| 735 |
*/ |
| 736 |
public static function get_cf_input_val( $cf ) { |
| 737 |
|
| 738 |
return isset( $_POST[ $cf->slug ] ) ? intval( $_POST[ $cf->slug ] ) : ''; // phpcs:ignore |
| 739 |
} |
| 740 |
|
| 741 |
/** |
| 742 |
* Print edit ticket custom field in individual ticket |
| 743 |
* |
| 744 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 745 |
* @param WPSC_Ticket $ticket - ticket object. |
| 746 |
* @return void |
| 747 |
*/ |
| 748 |
public static function print_edit_ticket_cf( $cf, $ticket ) { |
| 749 |
|
| 750 |
$options = $cf->get_options(); |
| 751 |
$unique_id = uniqid( 'wpsc_' ); |
| 752 |
?> |
| 753 |
<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 ); ?>"> |
| 754 |
<div class="wpsc-tff-label"> |
| 755 |
<span class="name"><?php echo esc_attr( $cf->name ); ?></span> |
| 756 |
</div> |
| 757 |
<?php |
| 758 |
$extra_info = stripslashes( $cf->extra_info ); |
| 759 |
if ( $extra_info ) { |
| 760 |
?> |
| 761 |
<span class="extra-info"><?php echo esc_attr( $cf->extra_info ); ?></span> |
| 762 |
<?php |
| 763 |
} |
| 764 |
?> |
| 765 |
<select class="<?php echo esc_attr( $unique_id ); ?>" name="<?php echo esc_attr( $cf->slug ); ?>"> |
| 766 |
<option value=""></option> |
| 767 |
<?php |
| 768 |
foreach ( $options as $option ) { |
| 769 |
?> |
| 770 |
<option <?php $ticket->{$cf->slug} && selected( $option->id, $ticket->{$cf->slug}->id ); ?> value="<?php echo esc_attr( $option->id ); ?>"><?php echo esc_attr( $option->name ); ?></option> |
| 771 |
<?php |
| 772 |
} |
| 773 |
?> |
| 774 |
</select> |
| 775 |
<script> |
| 776 |
jQuery('select.<?php echo esc_attr( $unique_id ); ?>').selectWoo({ |
| 777 |
allowClear: true, |
| 778 |
placeholder: "<?php echo esc_attr( $cf->placeholder_text ); ?>" |
| 779 |
}); |
| 780 |
</script> |
| 781 |
</div> |
| 782 |
<?php |
| 783 |
} |
| 784 |
|
| 785 |
/** |
| 786 |
* Set edit individual ticket for this custom field type. |
| 787 |
* Ignore phpcs nonce issue as we already checked where it is called from. |
| 788 |
* |
| 789 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 790 |
* @param WPSC_Ticket $ticket - ticket object. |
| 791 |
* @return WPSC_Ticket |
| 792 |
*/ |
| 793 |
public static function set_edit_ticket_cf( $cf, $ticket ) { |
| 794 |
|
| 795 |
$prev = $ticket->{$cf->slug} ? $ticket->{$cf->slug}->id : ''; |
| 796 |
$new = isset( $_POST[ $cf->slug ] ) ? intval( $_POST[ $cf->slug ] ) : ''; // phpcs:ignore |
| 797 |
$new = $new === 0 ? '' : $new; |
| 798 |
|
| 799 |
// Exit if there is no change. |
| 800 |
if ( $prev == $new ) { |
| 801 |
return $ticket; |
| 802 |
} |
| 803 |
|
| 804 |
// Change value. |
| 805 |
$ticket->{$cf->slug} = $new; |
| 806 |
$ticket->save(); |
| 807 |
|
| 808 |
return $ticket; |
| 809 |
} |
| 810 |
|
| 811 |
/** |
| 812 |
* Modify ticket field value of this custom field type using rest api |
| 813 |
* |
| 814 |
* @param WPSC_Ticket $ticket - ticket object. |
| 815 |
* @param WPSC_Custom_Field $cf - custom field. |
| 816 |
* @param mixed $value - value to be set. |
| 817 |
* @return void |
| 818 |
*/ |
| 819 |
public static function set_rest_edit_ticket_cf( $ticket, $cf, $value ) { |
| 820 |
|
| 821 |
$id = WPSC_Functions::sanitize_option( intval( $value ), $cf, $cf->get_options() ); |
| 822 |
$prev = is_object( $ticket->{$cf->slug} ) ? $ticket->{$cf->slug}->id : 0; |
| 823 |
if ( $prev != $id ) { |
| 824 |
$ticket->{$cf->slug} = $id ? $id : ''; |
| 825 |
} |
| 826 |
} |
| 827 |
|
| 828 |
/** |
| 829 |
* Insert log thread for this custom field type change |
| 830 |
* |
| 831 |
* @param WPSC_Custom_Field $cf - current custom field of this type. |
| 832 |
* @param WPSC_Ticket $prev - ticket object before making any changes. |
| 833 |
* @param WPSC_Ticket $new - ticket object after making changes. |
| 834 |
* @param string $current_date - date string to be stored as create time. |
| 835 |
* @param int $customer_id - current user customer id for blame. |
| 836 |
* @return void |
| 837 |
*/ |
| 838 |
public static function insert_ticket_log( $cf, $prev, $new, $current_date, $customer_id ) { |
| 839 |
|
| 840 |
// Exit if there is no change. |
| 841 |
if ( $prev->{$cf->slug} == $new->{$cf->slug} ) { |
| 842 |
return; |
| 843 |
} |
| 844 |
|
| 845 |
$prev_val = $prev->{$cf->slug} ? $prev->{$cf->slug}->id : ''; |
| 846 |
$new_val = $new->{$cf->slug} ? $new->{$cf->slug}->id : ''; |
| 847 |
|
| 848 |
$thread = WPSC_Thread::insert( |
| 849 |
array( |
| 850 |
'ticket' => $prev->id, |
| 851 |
'customer' => $customer_id, |
| 852 |
'type' => 'log', |
| 853 |
'body' => wp_json_encode( |
| 854 |
array( |
| 855 |
'slug' => $cf->slug, |
| 856 |
'prev' => $prev_val, |
| 857 |
'new' => $new_val, |
| 858 |
) |
| 859 |
), |
| 860 |
'date_created' => $current_date, |
| 861 |
'date_updated' => $current_date, |
| 862 |
) |
| 863 |
); |
| 864 |
} |
| 865 |
|
| 866 |
/** |
| 867 |
* Return data for this custom field while creating duplicate ticket |
| 868 |
* |
| 869 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 870 |
* @param WPSC_Ticket $ticket - ticket object. |
| 871 |
* @return mixed |
| 872 |
*/ |
| 873 |
public static function get_duplicate_ticket_data( $cf, $ticket ) { |
| 874 |
|
| 875 |
return $ticket->{$cf->slug} ? $ticket->{$cf->slug}->id : ''; |
| 876 |
} |
| 877 |
|
| 878 |
/** |
| 879 |
* Print edit field for this type in edit customer info |
| 880 |
* |
| 881 |
* @param WPSC_Custom_field $cf - custom field object. |
| 882 |
* @param WPSC_Customer $customer - customer object. |
| 883 |
* @param array $tff - ticket form field data. |
| 884 |
* @return string |
| 885 |
*/ |
| 886 |
public static function print_edit_customer_info( $cf, $customer, $tff ) { |
| 887 |
|
| 888 |
$options = $cf->get_options(); |
| 889 |
$val = $customer->id && $customer->{$cf->slug} ? intval( $customer->{$cf->slug}->id ) : 0; |
| 890 |
$unique_id = uniqid( 'wpsc_' ); |
| 891 |
|
| 892 |
ob_start(); |
| 893 |
?> |
| 894 |
<div class="<?php echo esc_attr( WPSC_Functions::get_tff_classes( $cf, $tff ) ); ?>" data-cft="<?php echo esc_attr( self::$slug ); ?>"> |
| 895 |
<div class="wpsc-tff-label"> |
| 896 |
<span class="name"><?php echo esc_attr( $cf->name ); ?></span> |
| 897 |
</div> |
| 898 |
<span class="extra-info"><?php echo esc_attr( $cf->extra_info ); ?></span> |
| 899 |
<select class="<?php echo esc_attr( $unique_id ); ?>" name="<?php echo esc_attr( $cf->slug ); ?>"> |
| 900 |
<option value=""></option> |
| 901 |
<?php |
| 902 |
foreach ( $options as $option ) : |
| 903 |
?> |
| 904 |
<option <?php selected( $option->id, $val ); ?> value="<?php echo esc_attr( $option->id ); ?>"><?php echo esc_attr( $option->name ); ?></option> |
| 905 |
<?php |
| 906 |
endforeach; |
| 907 |
?> |
| 908 |
</select> |
| 909 |
<script> |
| 910 |
jQuery('select.<?php echo esc_attr( $unique_id ); ?>').selectWoo({ |
| 911 |
allowClear: false, |
| 912 |
placeholder: "<?php echo esc_attr( $cf->placeholder_text ); ?>" |
| 913 |
}); |
| 914 |
</script> |
| 915 |
</div> |
| 916 |
<?php |
| 917 |
return ob_get_clean(); |
| 918 |
} |
| 919 |
|
| 920 |
/** |
| 921 |
* Print add new custom field setting properties |
| 922 |
* |
| 923 |
* @param string $field_class - Class name of the field. |
| 924 |
* @return void |
| 925 |
*/ |
| 926 |
public static function get_add_new_custom_field_properties( $field_class ) { |
| 927 |
|
| 928 |
if ( in_array( 'extra_info', $field_class::$allowed_properties ) ) : |
| 929 |
?> |
| 930 |
<div data-type="textfield" data-required="false" class="wpsc-input-group extra-info"> |
| 931 |
<div class="label-container"> |
| 932 |
<label for=""><?php esc_attr_e( 'Extra info', 'supportcandy' ); ?></label> |
| 933 |
</div> |
| 934 |
<input name="extra_info" type="text" autocomplete="off" /> |
| 935 |
</div> |
| 936 |
<?php |
| 937 |
endif; |
| 938 |
?> |
| 939 |
|
| 940 |
<div data-type="select-options" data-required="true" class="wpsc-input-group options"> |
| 941 |
<div class="label-container"> |
| 942 |
<label for=""> |
| 943 |
<?php esc_attr_e( 'Field options', 'supportcandy' ); ?> |
| 944 |
<span class="required-char">*</span> |
| 945 |
</label> |
| 946 |
</div> |
| 947 |
<div class="wpsc-options-container ui-sortable"></div> |
| 948 |
<script>jQuery(".wpsc-options-container").sortable({ handle: '.wpsc-sort-handle' });</script> |
| 949 |
<button class="wpsc-button small secondary" onclick="wpsc_get_add_new_option();"> |
| 950 |
<?php esc_attr_e( 'Add new', 'supportcandy' ); ?> |
| 951 |
</button> |
| 952 |
</div> |
| 953 |
<?php |
| 954 |
|
| 955 |
if ( in_array( 'default_value', $field_class::$allowed_properties ) ) : |
| 956 |
?> |
| 957 |
<div data-type="single-select" data-required="false" class="wpsc-input-group default-val-option-single"> |
| 958 |
<div class="label-container"> |
| 959 |
<label for=""> |
| 960 |
<?php esc_attr_e( 'Default value', 'supportcandy' ); ?> |
| 961 |
</label> |
| 962 |
</div> |
| 963 |
<select name="default-val-option-single[]" id="wpsc-default-val-option-single"> |
| 964 |
<option value=""></option> |
| 965 |
</select> |
| 966 |
</div> |
| 967 |
<?php |
| 968 |
endif; |
| 969 |
|
| 970 |
if ( in_array( 'is_auto_fill', $field_class::$allowed_properties ) ) : |
| 971 |
?> |
| 972 |
<div data-type="single-select" data-required="false" class="wpsc-input-group is_auto_fill"> |
| 973 |
<div class="label-container"> |
| 974 |
<label for=""> |
| 975 |
<?php esc_attr_e( 'Auto-fill in ticket form', 'supportcandy' ); ?> |
| 976 |
</label> |
| 977 |
</div> |
| 978 |
<select name="is_auto_fill"> |
| 979 |
<option value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option> |
| 980 |
<option value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option> |
| 981 |
</select> |
| 982 |
</div> |
| 983 |
<?php |
| 984 |
endif; |
| 985 |
|
| 986 |
if ( in_array( 'placeholder_text', $field_class::$allowed_properties ) ) : |
| 987 |
?> |
| 988 |
<div data-type="textfield" data-required="false" class="wpsc-input-group placeholder_text"> |
| 989 |
<div class="label-container"> |
| 990 |
<label for=""> |
| 991 |
<?php esc_attr_e( 'Placeholder', 'supportcandy' ); ?> |
| 992 |
</label> |
| 993 |
</div> |
| 994 |
<input type="text" name="placeholder_text" autocomplete="off"> |
| 995 |
</div> |
| 996 |
<?php |
| 997 |
endif; |
| 998 |
|
| 999 |
if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) : |
| 1000 |
?> |
| 1001 |
<div data-type="number" data-required="false" class="wpsc-input-group tl_width"> |
| 1002 |
<div class="label-container"> |
| 1003 |
<label for=""> |
| 1004 |
<?php esc_attr_e( 'Ticket list width (pixels)', 'supportcandy' ); ?> |
| 1005 |
</label> |
| 1006 |
</div> |
| 1007 |
<input type="number" name="tl_width" autocomplete="off"> |
| 1008 |
</div> |
| 1009 |
<?php |
| 1010 |
endif; |
| 1011 |
|
| 1012 |
if ( in_array( 'is_personal_info', $field_class::$allowed_properties ) ) : |
| 1013 |
?> |
| 1014 |
<div data-type="single-select" data-required="false" class="wpsc-input-group is_personal_info"> |
| 1015 |
<div class="label-container"> |
| 1016 |
<label for=""> |
| 1017 |
<?php esc_attr_e( 'Has personal info', 'supportcandy' ); ?> |
| 1018 |
</label> |
| 1019 |
</div> |
| 1020 |
<select name="is_personal_info"> |
| 1021 |
<option value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option> |
| 1022 |
<option value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option> |
| 1023 |
</select> |
| 1024 |
</div> |
| 1025 |
<?php |
| 1026 |
endif; |
| 1027 |
|
| 1028 |
if ( in_array( 'allow_my_profile', $field_class::$allowed_properties ) ) : |
| 1029 |
?> |
| 1030 |
<div data-type="single-select" data-required="false" class="wpsc-input-group allow_my_profile"> |
| 1031 |
<div class="label-container"> |
| 1032 |
<label for=""> |
| 1033 |
<?php esc_attr_e( 'Allow in my profile?', 'supportcandy' ); ?> |
| 1034 |
</label> |
| 1035 |
</div> |
| 1036 |
<select name="allow_my_profile"> |
| 1037 |
<option value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option> |
| 1038 |
<option value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option> |
| 1039 |
</select> |
| 1040 |
</div> |
| 1041 |
<?php |
| 1042 |
endif; |
| 1043 |
|
| 1044 |
if ( in_array( 'allow_ticket_form', $field_class::$allowed_properties ) ) : |
| 1045 |
?> |
| 1046 |
<div data-type="single-select" data-required="false" class="wpsc-input-group allow_ticket_form"> |
| 1047 |
<div class="label-container"> |
| 1048 |
<label for=""> |
| 1049 |
<?php esc_attr_e( 'Allow in ticket form?', 'supportcandy' ); ?> |
| 1050 |
</label> |
| 1051 |
</div> |
| 1052 |
<select name="allow_ticket_form"> |
| 1053 |
<option value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option> |
| 1054 |
<option value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option> |
| 1055 |
</select> |
| 1056 |
</div> |
| 1057 |
<?php |
| 1058 |
endif; |
| 1059 |
} |
| 1060 |
|
| 1061 |
/** |
| 1062 |
* Print edit custom field properties |
| 1063 |
* |
| 1064 |
* @param WPSC_Custom_Fields $cf - custom field object. |
| 1065 |
* @param string $field_class - class name of field category. |
| 1066 |
* @return void |
| 1067 |
*/ |
| 1068 |
public static function get_edit_custom_field_properties( $cf, $field_class ) { |
| 1069 |
|
| 1070 |
if ( in_array( 'extra_info', $field_class::$allowed_properties ) ) : |
| 1071 |
?> |
| 1072 |
<div data-type="textfield" data-required="false" class="wpsc-input-group extra-info"> |
| 1073 |
<div class="label-container"> |
| 1074 |
<label for=""><?php esc_attr_e( 'Extra info', 'supportcandy' ); ?></label> |
| 1075 |
</div> |
| 1076 |
<input name="extra_info" type="text" value="<?php echo esc_attr( $cf->extra_info ); ?>" autocomplete="off" /> |
| 1077 |
</div> |
| 1078 |
<?php |
| 1079 |
endif; |
| 1080 |
?> |
| 1081 |
|
| 1082 |
<div data-type="select-options" data-required="true" class="wpsc-input-group options"> |
| 1083 |
<div class="label-container"> |
| 1084 |
<label for=""> |
| 1085 |
<?php esc_attr_e( 'Field options', 'supportcandy' ); ?> |
| 1086 |
<span class="required-char">*</span> |
| 1087 |
</label> |
| 1088 |
</div> |
| 1089 |
<div class="wpsc-options-container ui-sortable"> |
| 1090 |
<?php |
| 1091 |
$options = $cf->get_options(); |
| 1092 |
foreach ( $options as $option ) : |
| 1093 |
?> |
| 1094 |
<div class="wpsc-option-item"> |
| 1095 |
<div class="content"> |
| 1096 |
<div class="wpsc-edit-option-container" style="display: none;"> |
| 1097 |
<input class="edit-option-text" type="text" value="" autocomplete="off" /> |
| 1098 |
<button onclick="wpsc_set_edit_option(this, '<?php echo esc_attr( wp_create_nonce( 'wpsc_set_edit_option' ) ); ?>');"> |
| 1099 |
<?php WPSC_Icons::get( 'check' ); ?> |
| 1100 |
</button> |
| 1101 |
<button class="cancel" onclick="wpsc_edit_option_cancel(this);"> |
| 1102 |
<?php WPSC_Icons::get( 'times' ); ?> |
| 1103 |
</button> |
| 1104 |
</div> |
| 1105 |
<div class="wpsc-option-listing-container"> |
| 1106 |
<span class="sort wpsc-sort-handle"><?php WPSC_Icons::get( 'sort' ); ?></span> |
| 1107 |
<div class="text"><?php echo esc_attr( $option->name ); ?></div> |
| 1108 |
<span class="edit" onclick="wpsc_edit_option(this);"><?php WPSC_Icons::get( 'edit' ); ?></span> |
| 1109 |
</div> |
| 1110 |
</div> |
| 1111 |
<div class="remove-container"> |
| 1112 |
<span onclick="wpsc_remove_option_item(this)"><?php WPSC_Icons::get( 'times-circle' ); ?></span> |
| 1113 |
</div> |
| 1114 |
<input type="hidden" class="option_id" name="options[]" value="<?php echo esc_attr( $option->id ); ?>"> |
| 1115 |
</div> |
| 1116 |
<?php |
| 1117 |
endforeach; |
| 1118 |
?> |
| 1119 |
</div> |
| 1120 |
<script>jQuery(".wpsc-options-container").sortable({ handle: '.wpsc-sort-handle' });</script> |
| 1121 |
<button class="wpsc-button small secondary" onclick="wpsc_get_add_new_option();"> |
| 1122 |
<?php esc_attr_e( 'Add new', 'supportcandy' ); ?> |
| 1123 |
</button> |
| 1124 |
</div> |
| 1125 |
<?php |
| 1126 |
|
| 1127 |
if ( in_array( 'default_value', $field_class::$allowed_properties ) ) : |
| 1128 |
$default_val = $cf->default_value ? $cf->default_value[0] : ''; |
| 1129 |
?> |
| 1130 |
<div data-type="single-select" data-required="false" class="wpsc-input-group default_value"> |
| 1131 |
<div class="label-container"> |
| 1132 |
<label for=""> |
| 1133 |
<?php esc_attr_e( 'Default value', 'supportcandy' ); ?> |
| 1134 |
</label> |
| 1135 |
</div> |
| 1136 |
<select name="default-val-option-single[]" id="wpsc-default-val-option-single"> |
| 1137 |
<option value=""></option> |
| 1138 |
<?php |
| 1139 |
$options = $cf->get_options(); |
| 1140 |
foreach ( $options as $option ) : |
| 1141 |
echo '<option ' . selected( $default_val, $option->id ) . ' value=' . esc_attr( $option->id ) . '>' . esc_attr( $option->name ) . '</option>'; |
| 1142 |
endforeach; |
| 1143 |
?> |
| 1144 |
</select> |
| 1145 |
</div> |
| 1146 |
<?php |
| 1147 |
endif; |
| 1148 |
|
| 1149 |
if ( in_array( 'is_auto_fill', $field_class::$allowed_properties ) ) : |
| 1150 |
?> |
| 1151 |
<div data-type="single-select" data-required="false" class="wpsc-input-group is_auto_fill"> |
| 1152 |
<div class="label-container"> |
| 1153 |
<label for=""> |
| 1154 |
<?php esc_attr_e( 'Auto-fill in ticket form', 'supportcandy' ); ?> |
| 1155 |
</label> |
| 1156 |
</div> |
| 1157 |
<select name="is_auto_fill"> |
| 1158 |
<option <?php selected( $cf->is_auto_fill, '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option> |
| 1159 |
<option <?php selected( $cf->is_auto_fill, '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option> |
| 1160 |
</select> |
| 1161 |
</div> |
| 1162 |
<?php |
| 1163 |
endif; |
| 1164 |
|
| 1165 |
if ( in_array( 'placeholder_text', $field_class::$allowed_properties ) ) : |
| 1166 |
?> |
| 1167 |
<div data-type="textfield" data-required="false" class="wpsc-input-group placeholder_text"> |
| 1168 |
<div class="label-container"> |
| 1169 |
<label for=""> |
| 1170 |
<?php esc_attr_e( 'Placeholder', 'supportcandy' ); ?> |
| 1171 |
</label> |
| 1172 |
</div> |
| 1173 |
<input type="text" name="placeholder_text" value="<?php echo esc_attr( $cf->placeholder_text ); ?>" autocomplete="off"> |
| 1174 |
</div> |
| 1175 |
<?php |
| 1176 |
endif; |
| 1177 |
|
| 1178 |
if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) : |
| 1179 |
?> |
| 1180 |
<div data-type="number" data-required="false" class="wpsc-input-group tl_width"> |
| 1181 |
<div class="label-container"> |
| 1182 |
<label for=""> |
| 1183 |
<?php esc_attr_e( 'Ticket list width (pixels)', 'supportcandy' ); ?> |
| 1184 |
</label> |
| 1185 |
</div> |
| 1186 |
<input type="number" name="tl_width" value="<?php echo intval( $cf->tl_width ); ?>" autocomplete="off"> |
| 1187 |
</div> |
| 1188 |
<?php |
| 1189 |
endif; |
| 1190 |
|
| 1191 |
if ( in_array( 'is_personal_info', $field_class::$allowed_properties ) ) : |
| 1192 |
?> |
| 1193 |
<div data-type="single-select" data-required="false" class="wpsc-input-group is_personal_info"> |
| 1194 |
<div class="label-container"> |
| 1195 |
<label for=""> |
| 1196 |
<?php esc_attr_e( 'Has personal info', 'supportcandy' ); ?> |
| 1197 |
</label> |
| 1198 |
</div> |
| 1199 |
<select name="is_personal_info"> |
| 1200 |
<option <?php selected( $cf->is_personal_info, '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option> |
| 1201 |
<option <?php selected( $cf->is_personal_info, '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option> |
| 1202 |
</select> |
| 1203 |
</div> |
| 1204 |
<?php |
| 1205 |
endif; |
| 1206 |
|
| 1207 |
if ( in_array( 'allow_my_profile', $field_class::$allowed_properties ) ) : |
| 1208 |
?> |
| 1209 |
<div data-type="single-select" data-required="false" class="wpsc-input-group allow_my_profile"> |
| 1210 |
<div class="label-container"> |
| 1211 |
<label for=""> |
| 1212 |
<?php esc_attr_e( 'Allow in my profile?', 'supportcandy' ); ?> |
| 1213 |
</label> |
| 1214 |
</div> |
| 1215 |
<select name="allow_my_profile"> |
| 1216 |
<option <?php selected( $cf->allow_my_profile, '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option> |
| 1217 |
<option <?php selected( $cf->allow_my_profile, '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option> |
| 1218 |
</select> |
| 1219 |
</div> |
| 1220 |
<?php |
| 1221 |
endif; |
| 1222 |
|
| 1223 |
if ( in_array( 'allow_ticket_form', $field_class::$allowed_properties ) ) : |
| 1224 |
?> |
| 1225 |
<div data-type="single-select" data-required="false" class="wpsc-input-group allow_ticket_form"> |
| 1226 |
<div class="label-container"> |
| 1227 |
<label for=""> |
| 1228 |
<?php esc_attr_e( 'Allow in ticket form?', 'supportcandy' ); ?> |
| 1229 |
</label> |
| 1230 |
</div> |
| 1231 |
<select name="allow_ticket_form"> |
| 1232 |
<option <?php selected( $cf->allow_ticket_form, '0' ); ?> value="0"><?php esc_attr_e( 'No', 'supportcandy' ); ?></option> |
| 1233 |
<option <?php selected( $cf->allow_ticket_form, '1' ); ?> value="1"><?php esc_attr_e( 'Yes', 'supportcandy' ); ?></option> |
| 1234 |
</select> |
| 1235 |
</div> |
| 1236 |
<?php |
| 1237 |
endif; |
| 1238 |
} |
| 1239 |
|
| 1240 |
/** |
| 1241 |
* Set custom field properties. Can be used by add/edit custom field. |
| 1242 |
* Ignore phpcs nonce issue as we already checked where it is called from. |
| 1243 |
* |
| 1244 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 1245 |
* @param string $field_class - class of field category. |
| 1246 |
* @return void |
| 1247 |
*/ |
| 1248 |
public static function set_cf_properties( $cf, $field_class ) { |
| 1249 |
|
| 1250 |
// extra info. |
| 1251 |
if ( in_array( 'extra_info', $field_class::$allowed_properties ) ) { |
| 1252 |
$cf->extra_info = isset( $_POST['extra_info'] ) ? sanitize_text_field( wp_unslash( $_POST['extra_info'] ) ) : ''; // phpcs:ignore |
| 1253 |
} |
| 1254 |
|
| 1255 |
// options. |
| 1256 |
$options = isset( $_POST['options'] ) ? array_filter( array_map( 'sanitize_text_field', wp_unslash($_POST['options']) ) ) : array(); // phpcs:ignore |
| 1257 |
if ( ! $options ) { |
| 1258 |
wp_send_json_error( new WP_Error( '001', 'Bad request!' ), 400 ); |
| 1259 |
} |
| 1260 |
|
| 1261 |
// default value. |
| 1262 |
if ( in_array( 'default_value', $field_class::$allowed_properties ) ) { |
| 1263 |
$cf->default_value = isset( $_POST['default-val-option-single'] ) ? array_filter( array_map( 'intval', $_POST['default-val-option-single'] ) ) : array(); // phpcs:ignore |
| 1264 |
} |
| 1265 |
|
| 1266 |
// auto fill. |
| 1267 |
if ( in_array( 'is_auto_fill', $field_class::$allowed_properties ) ) { |
| 1268 |
$cf->is_auto_fill = isset( $_POST['is_auto_fill'] ) ? sanitize_text_field( wp_unslash( $_POST['is_auto_fill'] ) ) : ''; // phpcs:ignore |
| 1269 |
} |
| 1270 |
|
| 1271 |
// placeholder! |
| 1272 |
if ( in_array( 'placeholder_text', $field_class::$allowed_properties ) ) { |
| 1273 |
$cf->placeholder_text = isset( $_POST['placeholder_text'] ) ? sanitize_text_field( wp_unslash( $_POST['placeholder_text'] ) ) : ''; // phpcs:ignore |
| 1274 |
} |
| 1275 |
|
| 1276 |
// personal info. |
| 1277 |
if ( in_array( 'is_personal_info', $field_class::$allowed_properties ) ) { |
| 1278 |
$cf->is_personal_info = isset( $_POST['is_personal_info'] ) ? intval( $_POST['is_personal_info'] ) : 0; // phpcs:ignore |
| 1279 |
} |
| 1280 |
|
| 1281 |
// my-profile. |
| 1282 |
if ( in_array( 'allow_my_profile', $field_class::$allowed_properties ) ) { |
| 1283 |
$cf->allow_my_profile = isset( $_POST['allow_my_profile'] ) ? intval( $_POST['allow_my_profile'] ) : 0; // phpcs:ignore |
| 1284 |
} |
| 1285 |
|
| 1286 |
// ticket form. |
| 1287 |
if ( in_array( 'allow_ticket_form', $field_class::$allowed_properties ) ) { |
| 1288 |
$cf->allow_ticket_form = isset( $_POST['allow_ticket_form'] ) ? intval( $_POST['allow_ticket_form'] ) : 0; // phpcs:ignore |
| 1289 |
} |
| 1290 |
|
| 1291 |
// tl_width! |
| 1292 |
if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) { |
| 1293 |
$tl_width = isset( $_POST['tl_width'] ) ? intval( $_POST['tl_width'] ) : 0; // phpcs:ignore |
| 1294 |
$cf->tl_width = $tl_width ? $tl_width : 100; |
| 1295 |
} |
| 1296 |
|
| 1297 |
// save! |
| 1298 |
$cf->save(); |
| 1299 |
$cf->set_options( $options, true ); |
| 1300 |
} |
| 1301 |
|
| 1302 |
/** |
| 1303 |
* Add SQL joins to ticket model for this field type |
| 1304 |
* |
| 1305 |
* @param array $joins - array of join string that can be imploded later. |
| 1306 |
* @param array $filter - user filter. |
| 1307 |
* @return array |
| 1308 |
*/ |
| 1309 |
public static function ticket_join( $joins, $filter ) { |
| 1310 |
|
| 1311 |
global $wpdb; |
| 1312 |
|
| 1313 |
$cf = WPSC_Custom_Field::get_cf_by_slug( $filter['orderby_slug'] ); |
| 1314 |
|
| 1315 |
if ( $cf && $cf->type::$slug == 'cf_single_select' ) { |
| 1316 |
$join_str = self::get_sql_slug( $cf ); |
| 1317 |
$joins[] = 'LEFT JOIN ' . $wpdb->prefix . 'psmsc_options op ON ' . $join_str . ' = op.id'; |
| 1318 |
} |
| 1319 |
|
| 1320 |
return $joins; |
| 1321 |
} |
| 1322 |
|
| 1323 |
/** |
| 1324 |
* Add ticket search compatibility for fields of this custom field type. |
| 1325 |
* |
| 1326 |
* @param array $sql - Array of sql peices that can be joined later. |
| 1327 |
* @param array $filter - User filter. |
| 1328 |
* @param array $custom_fields - Custom fields array applicable for search. |
| 1329 |
* @param string $search - search string. |
| 1330 |
* @param array $allowed_search_fields - Allowed search fields. |
| 1331 |
* @return array |
| 1332 |
*/ |
| 1333 |
public static function ticket_search( $sql, $filter, $custom_fields, $search, $allowed_search_fields ) { |
| 1334 |
|
| 1335 |
if ( isset( $custom_fields[ self::$slug ] ) ) { |
| 1336 |
|
| 1337 |
$search_items = false; |
| 1338 |
foreach ( $custom_fields[ self::$slug ] as $cf ) { |
| 1339 |
if ( in_array( $cf->slug, $allowed_search_fields ) ) { |
| 1340 |
if ( ! $search_items ) { |
| 1341 |
$search_items = WPSC_Option::get_tl_search_string( $search ); |
| 1342 |
if ( ! $search_items ) { |
| 1343 |
break; |
| 1344 |
} |
| 1345 |
} |
| 1346 |
$join_char = in_array( $cf->field, array( 'ticket', 'agentonly' ) ) ? 't.' : 'c.'; |
| 1347 |
$sql[] = $join_char . $cf->slug . ' IN(' . implode( ', ', $search_items ) . ')'; |
| 1348 |
} |
| 1349 |
} |
| 1350 |
} |
| 1351 |
|
| 1352 |
return $sql; |
| 1353 |
} |
| 1354 |
|
| 1355 |
/** |
| 1356 |
* Returns printable ticket value for custom field. Can be used in export tickets, replace macros etc. |
| 1357 |
* |
| 1358 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 1359 |
* @param WPSC_Ticket $ticket - ticket object. |
| 1360 |
* @param string $module - module name. |
| 1361 |
* @return string |
| 1362 |
*/ |
| 1363 |
public static function get_ticket_field_val( $cf, $ticket, $module = '' ) { |
| 1364 |
|
| 1365 |
$option = $ticket->{$cf->slug}; |
| 1366 |
$value = is_object( $option ) && $option->id ? $option->name : ''; |
| 1367 |
return apply_filters( 'wpsc_ticket_field_val_single_select', $value, $cf, $ticket, $module ); |
| 1368 |
} |
| 1369 |
|
| 1370 |
/** |
| 1371 |
* Print ticket value for given custom field on ticket list |
| 1372 |
* |
| 1373 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 1374 |
* @param WPSC_Ticket $ticket - ticket object. |
| 1375 |
* @return void |
| 1376 |
*/ |
| 1377 |
public static function print_tl_ticket_field_val( $cf, $ticket ) { |
| 1378 |
|
| 1379 |
echo esc_attr( self::get_ticket_field_val( $cf, $ticket ) ); |
| 1380 |
} |
| 1381 |
|
| 1382 |
/** |
| 1383 |
* Print ticket value for given custom field on widget |
| 1384 |
* |
| 1385 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 1386 |
* @param WPSC_Ticket $ticket - ticket object. |
| 1387 |
* @return void |
| 1388 |
*/ |
| 1389 |
public static function print_widget_ticket_field_val( $cf, $ticket ) { |
| 1390 |
|
| 1391 |
echo esc_attr( self::get_ticket_field_val( $cf, $ticket ) ); |
| 1392 |
} |
| 1393 |
|
| 1394 |
/** |
| 1395 |
* Returns printable customer value for custom field. Can be used in export tickets, replace macros etc. |
| 1396 |
* |
| 1397 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 1398 |
* @param WPSC_Customer $customer - customer object. |
| 1399 |
* @return string |
| 1400 |
*/ |
| 1401 |
public static function get_customer_field_val( $cf, $customer ) { |
| 1402 |
|
| 1403 |
$option = $customer->{$cf->slug}; |
| 1404 |
return is_object( $option ) && $option->id ? $option->name : ''; |
| 1405 |
} |
| 1406 |
|
| 1407 |
/** |
| 1408 |
* Print customer value for given custom field on ticket list |
| 1409 |
* |
| 1410 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 1411 |
* @param WPSC_Customer $customer - customer object. |
| 1412 |
* @return void |
| 1413 |
*/ |
| 1414 |
public static function print_tl_customer_field_val( $cf, $customer ) { |
| 1415 |
|
| 1416 |
echo esc_attr( self::get_customer_field_val( $cf, $customer ) ); |
| 1417 |
} |
| 1418 |
|
| 1419 |
/** |
| 1420 |
* Print customer value for given custom field on widget |
| 1421 |
* |
| 1422 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 1423 |
* @param WPSC_Customer $customer - customer object. |
| 1424 |
* @return void |
| 1425 |
*/ |
| 1426 |
public static function print_widget_customer_field_val( $cf, $customer ) { |
| 1427 |
|
| 1428 |
echo esc_attr( self::get_customer_field_val( $cf, $customer ) ); |
| 1429 |
} |
| 1430 |
|
| 1431 |
/** |
| 1432 |
* Print given value for custom field |
| 1433 |
* |
| 1434 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 1435 |
* @param mixed $val - value to convert and print. |
| 1436 |
* @return void |
| 1437 |
*/ |
| 1438 |
public static function print_val( $cf, $val ) { |
| 1439 |
|
| 1440 |
$option = is_object( $val ) ? $val : new WPSC_Option( $val ); |
| 1441 |
echo $option->id ? esc_attr( $option->name ) : esc_attr__( 'None', 'supportcandy' ); |
| 1442 |
} |
| 1443 |
|
| 1444 |
/** |
| 1445 |
* Return printable value for history log macro |
| 1446 |
* |
| 1447 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 1448 |
* @param mixed $val - value to convert and return. |
| 1449 |
* @return string |
| 1450 |
*/ |
| 1451 |
public static function get_history_log_val( $cf, $val ) { |
| 1452 |
|
| 1453 |
ob_start(); |
| 1454 |
self::print_val( $cf, $val ); |
| 1455 |
return ob_get_clean(); |
| 1456 |
} |
| 1457 |
|
| 1458 |
/** |
| 1459 |
* Print dashboard activity function |
| 1460 |
* |
| 1461 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 1462 |
* @param array $recent_logs - recent_logs object. |
| 1463 |
* @param array $body - body object. |
| 1464 |
* @param int $view - check if frontend. |
| 1465 |
* @return string |
| 1466 |
*/ |
| 1467 |
public static function print_activity( $cf, $recent_logs, $body, $view ) { |
| 1468 |
|
| 1469 |
$url = WPSC_Functions::get_ticket_url( $recent_logs->ticket->id, $view ); |
| 1470 |
if ( $body->new ) { |
| 1471 |
$ss_value = new WPSC_Option( $body->new ); |
| 1472 |
$value = $ss_value->name; |
| 1473 |
} else { |
| 1474 |
$value = 'None'; |
| 1475 |
} |
| 1476 |
return esc_attr( $recent_logs->customer->name ) . ' updated the' . esc_attr( $cf->name ) . ' value of <a href="' . esc_attr( $url ) . '" target="_blank">#' . esc_attr( $recent_logs->ticket->id ) . '</a> to ' . esc_attr( $value ); |
| 1477 |
} |
| 1478 |
|
| 1479 |
/** |
| 1480 |
* Print given value for custom field |
| 1481 |
* |
| 1482 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 1483 |
* @param mixed $val - value to convert and print. |
| 1484 |
* @return string |
| 1485 |
*/ |
| 1486 |
public static function get_field_value( $cf, $val ) { |
| 1487 |
|
| 1488 |
$option = is_object( $val ) ? $val : new WPSC_Option( $val ); |
| 1489 |
return $option->id ? esc_attr( $option->name ) : esc_attr__( 'None', 'supportcandy' ); |
| 1490 |
} |
| 1491 |
|
| 1492 |
/** |
| 1493 |
* Clear value of hidden fields |
| 1494 |
* |
| 1495 |
* @return void |
| 1496 |
*/ |
| 1497 |
public static function js_clear_value_hidden_fields() { |
| 1498 |
?> |
| 1499 |
case '<?php echo esc_attr( self::$slug ); ?>': |
| 1500 |
customField.find('select').first().val(''); |
| 1501 |
break; |
| 1502 |
<?php |
| 1503 |
echo PHP_EOL; |
| 1504 |
} |
| 1505 |
} |
| 1506 |
endif; |
| 1507 |
|
| 1508 |
WPSC_CF_Single_Select::init(); |
| 1509 |
|