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