| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_DF_Browser' ) ) : |
| 7 |
|
| 8 |
final class WPSC_DF_Browser { |
| 9 |
|
| 10 |
/** |
| 11 |
* Slug for this custom field type |
| 12 |
* |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
public static $slug = 'df_browser'; |
| 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 = true; |
| 93 |
|
| 94 |
/** |
| 95 |
* Set whether fields created from this custom field type is allowed in ticket filter |
| 96 |
* |
| 97 |
* @var boolean |
| 98 |
*/ |
| 99 |
public static $is_filter = true; |
| 100 |
|
| 101 |
/** |
| 102 |
* Set whether fields created from this custom field type can be given character limits |
| 103 |
* |
| 104 |
* @var boolean |
| 105 |
*/ |
| 106 |
public static $has_char_limit = false; |
| 107 |
|
| 108 |
/** |
| 109 |
* Set whether fields created from this custom field type has custom options set in options table |
| 110 |
* |
| 111 |
* @var boolean |
| 112 |
*/ |
| 113 |
public static $has_options = false; |
| 114 |
|
| 115 |
/** |
| 116 |
* Set whether fields created from this custom field type can be auto-filled |
| 117 |
* |
| 118 |
* @var boolean |
| 119 |
*/ |
| 120 |
public static $is_auto_fill = false; |
| 121 |
|
| 122 |
/** |
| 123 |
* Set whether fields created from this custom field type can be available for ticket list sorting |
| 124 |
* |
| 125 |
* @var boolean |
| 126 |
*/ |
| 127 |
public static $is_sort = false; |
| 128 |
|
| 129 |
/** |
| 130 |
* Set whether fields created from this custom field type can have placeholder |
| 131 |
* |
| 132 |
* @var boolean |
| 133 |
*/ |
| 134 |
public static $is_placeholder = false; |
| 135 |
|
| 136 |
/** |
| 137 |
* Set whether fields created from this custom field type is applicable for visibility conditions in create ticket form |
| 138 |
* |
| 139 |
* @var boolean |
| 140 |
*/ |
| 141 |
public static $is_visibility_conditions = false; |
| 142 |
|
| 143 |
/** |
| 144 |
* Set whether fields created from this custom field type is applicable for macros |
| 145 |
* |
| 146 |
* @var boolean |
| 147 |
*/ |
| 148 |
public static $has_macro = true; |
| 149 |
|
| 150 |
/** |
| 151 |
* Set whether fields of this custom field type is applicalbe for search on ticket list page. |
| 152 |
* |
| 153 |
* @var boolean |
| 154 |
*/ |
| 155 |
public static $is_search = false; |
| 156 |
|
| 157 |
/** |
| 158 |
* Initialize the class |
| 159 |
* |
| 160 |
* @return void |
| 161 |
*/ |
| 162 |
public static function init() { |
| 163 |
|
| 164 |
// Get object of this class. |
| 165 |
add_filter( 'wpsc_load_ref_classes', array( __CLASS__, 'load_ref_class' ) ); |
| 166 |
|
| 167 |
// TFF! |
| 168 |
add_action( 'wpsc_create_ticket_data', array( __CLASS__, 'set_create_ticket_data' ), 10, 3 ); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Load current class to reference classes |
| 173 |
* |
| 174 |
* @param array $classes - Associative array of class names indexed by its slug. |
| 175 |
* @return array |
| 176 |
*/ |
| 177 |
public static function load_ref_class( $classes ) { |
| 178 |
|
| 179 |
$classes[ self::$slug ] = array( |
| 180 |
'class' => __CLASS__, |
| 181 |
'save-key' => 'id', |
| 182 |
); |
| 183 |
return $classes; |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Print operators for ticket form filter |
| 188 |
* |
| 189 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 190 |
* @param array $filter - Existing filters (if any). |
| 191 |
* @return void |
| 192 |
*/ |
| 193 |
public static function get_operators( $cf, $filter = array() ) { |
| 194 |
?> |
| 195 |
|
| 196 |
<div class="item conditional"> |
| 197 |
<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' ) ); ?>');"> |
| 198 |
<option value=""><?php esc_attr_e( 'Compare As', 'supportcandy' ); ?></option> |
| 199 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '=' ); ?> value="="><?php esc_attr_e( 'Equals', 'supportcandy' ); ?></option> |
| 200 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'LIKE' ); ?> value="LIKE"><?php esc_attr_e( 'Has Words', 'supportcandy' ); ?></option> |
| 201 |
</select> |
| 202 |
</div> |
| 203 |
<?php |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Print operators for ticket form filter |
| 208 |
* |
| 209 |
* @param string $operator - condition operator on which operands should be returned. |
| 210 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 211 |
* @param array $filter - Exising functions (if any). |
| 212 |
* @return void |
| 213 |
*/ |
| 214 |
public static function get_operands( $operator, $cf, $filter = array() ) { |
| 215 |
|
| 216 |
$value = isset( $filter['operand_val_1'] ) ? stripslashes( $filter['operand_val_1'] ) : ''; |
| 217 |
if ( $operator == 'LIKE' ) { |
| 218 |
?> |
| 219 |
|
| 220 |
<div class="item conditional operand single"> |
| 221 |
<textarea class="operand_val_1" placeholder="<?php esc_attr_e( 'One condition per line!', 'supportcandy' ); ?>" style="width: 100%;"><?php echo esc_attr( $value ); ?></textarea> |
| 222 |
</div> |
| 223 |
<?php |
| 224 |
|
| 225 |
} else { |
| 226 |
?> |
| 227 |
|
| 228 |
<div class="item conditional operand single"> |
| 229 |
<input |
| 230 |
type="text" |
| 231 |
class="operand_val_1" |
| 232 |
value="<?php echo esc_attr( $value ); ?>" |
| 233 |
autocomplete="off"/> |
| 234 |
</div> |
| 235 |
<?php |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Parse filter and return sql query to be merged in ticket model query builder |
| 241 |
* |
| 242 |
* @param WPSC_Custom_Field $cf - custom field of this type. |
| 243 |
* @param mixed $compare - comparison operator. |
| 244 |
* @param mixed $val - value to compare. |
| 245 |
* @return string |
| 246 |
*/ |
| 247 |
public static function parse_filter( $cf, $compare, $val ) { |
| 248 |
|
| 249 |
$str = ''; |
| 250 |
|
| 251 |
switch ( $compare ) { |
| 252 |
|
| 253 |
case '=': |
| 254 |
$str = 't.' . $cf->slug . '=\'' . esc_sql( $val ) . '\''; |
| 255 |
break; |
| 256 |
|
| 257 |
case 'IN': |
| 258 |
$str = 'CONVERT(t.' . $cf->slug . ' USING utf8) IN(\'' . implode( '\', \'', esc_sql( $val ) ) . '\')'; |
| 259 |
break; |
| 260 |
|
| 261 |
default: |
| 262 |
$str = '1=1'; |
| 263 |
} |
| 264 |
|
| 265 |
return $str; |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Check ticket condition |
| 270 |
* |
| 271 |
* @param array $condition - array with condition data. |
| 272 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 273 |
* @param WPSC_Ticket $ticket - ticket object. |
| 274 |
* @return boolean |
| 275 |
*/ |
| 276 |
public static function is_valid_ticket_condition( $condition, $cf, $ticket ) { |
| 277 |
|
| 278 |
$flag = true; |
| 279 |
|
| 280 |
switch ( $condition['operator'] ) { |
| 281 |
|
| 282 |
case '=': |
| 283 |
$flag = $ticket->{$cf->slug} == $condition['operand_val_1'] ? true : false; |
| 284 |
break; |
| 285 |
|
| 286 |
case 'IN': |
| 287 |
$flag = in_array( $ticket->{$cf->slug}, $condition['operand_val_1'] ); |
| 288 |
break; |
| 289 |
|
| 290 |
default: |
| 291 |
$flag = true; |
| 292 |
} |
| 293 |
|
| 294 |
return $flag; |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Return default value for custom field of this type |
| 299 |
* |
| 300 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 301 |
* @return mixed |
| 302 |
*/ |
| 303 |
public static function get_default_value( $cf ) { |
| 304 |
|
| 305 |
return ''; |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Check and return custom field value for new ticket to be created. |
| 310 |
* This function is used by filter for set create ticket form and called directly by my-profile for each applicable custom fields. |
| 311 |
* Ignore phpcs nonce issue as we already checked where it is called from. |
| 312 |
* |
| 313 |
* @param array $data - Array of values to to stored in ticket in an insert function. |
| 314 |
* @param array $custom_fields - Array containing all applicable custom fields indexed by unique custom field types. |
| 315 |
* @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. |
| 316 |
* @return array |
| 317 |
*/ |
| 318 |
public static function set_create_ticket_data( $data, $custom_fields, $is_my_profile ) { |
| 319 |
|
| 320 |
$data['browser'] = self::get_user_browser(); |
| 321 |
return $data; |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* Get browser from user agent |
| 326 |
* |
| 327 |
* @return String |
| 328 |
*/ |
| 329 |
public static function get_user_browser() { |
| 330 |
|
| 331 |
$user_agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : ''; |
| 332 |
|
| 333 |
$browsers = array( |
| 334 |
array( |
| 335 |
'regex' => '/Navigator(.*)/i', |
| 336 |
'name' => 'Navigator', |
| 337 |
), |
| 338 |
array( |
| 339 |
'regex' => '/Firefox(.*)/i', |
| 340 |
'name' => 'Firefox', |
| 341 |
), |
| 342 |
array( |
| 343 |
'regex' => '/MSIE(.*)/i', |
| 344 |
'name' => 'Internet Explorer', |
| 345 |
), |
| 346 |
array( |
| 347 |
'regex' => '/chrome(.*)/i', |
| 348 |
'name' => 'Google Chrome', |
| 349 |
), |
| 350 |
array( |
| 351 |
'regex' => '/MAXTHON(.*)/i', |
| 352 |
'name' => 'MAXTHON', |
| 353 |
), |
| 354 |
array( |
| 355 |
'regex' => '/Opera(.*)/i', |
| 356 |
'name' => 'Opera', |
| 357 |
), |
| 358 |
array( |
| 359 |
'regex' => '/Edg(.*)/i', |
| 360 |
'name' => 'Edge', |
| 361 |
), |
| 362 |
array( |
| 363 |
'regex' => '/Konqueror(.*)/i', |
| 364 |
'name' => 'Konqueror', |
| 365 |
), |
| 366 |
array( |
| 367 |
'regex' => '/SamsungBrowser(.*)/i', |
| 368 |
'name' => 'SamsungBrowser', |
| 369 |
), |
| 370 |
array( |
| 371 |
'regex' => '/Safari(.*)/i', |
| 372 |
'name' => 'Safari', |
| 373 |
), |
| 374 |
); |
| 375 |
|
| 376 |
foreach ( $browsers as $browser ) { |
| 377 |
if ( preg_match( $browser['regex'], $user_agent ) ) { |
| 378 |
return $browser['name']; |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
return ''; |
| 383 |
} |
| 384 |
|
| 385 |
/** |
| 386 |
* Return val field for meta query of this type of custom field |
| 387 |
* |
| 388 |
* @param array $condition - condition data. |
| 389 |
* @return mixed |
| 390 |
*/ |
| 391 |
public static function get_meta_value( $condition ) { |
| 392 |
|
| 393 |
$operator = $condition['operator']; |
| 394 |
switch ( $operator ) { |
| 395 |
case '=': |
| 396 |
case 'LIKE': |
| 397 |
return $condition['operand_val_1']; |
| 398 |
} |
| 399 |
return false; |
| 400 |
} |
| 401 |
|
| 402 |
/** |
| 403 |
* Print edit custom field properties |
| 404 |
* |
| 405 |
* @param WPSC_Custom_Fields $cf - custom field object. |
| 406 |
* @param string $field_class - class name of field category. |
| 407 |
* @return void |
| 408 |
*/ |
| 409 |
public static function get_edit_custom_field_properties( $cf, $field_class ) { |
| 410 |
|
| 411 |
if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) : |
| 412 |
?> |
| 413 |
<div data-type="number" data-required="false" class="wpsc-input-group tl_width"> |
| 414 |
<div class="label-container"> |
| 415 |
<label for=""> |
| 416 |
<?php esc_attr_e( 'Ticket list width (pixels)', 'supportcandy' ); ?> |
| 417 |
</label> |
| 418 |
</div> |
| 419 |
<input type="number" name="tl_width" value="<?php echo esc_attr( intval( $cf->tl_width ) ); ?>" autocomplete="off"> |
| 420 |
</div> |
| 421 |
<?php |
| 422 |
endif; |
| 423 |
} |
| 424 |
|
| 425 |
/** |
| 426 |
* Set custom field properties. Can be used by add/edit custom field. |
| 427 |
* Ignore phpcs nonce issue as we already checked where it is called from. |
| 428 |
* |
| 429 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 430 |
* @param string $field_class - class of field category. |
| 431 |
* @return void |
| 432 |
*/ |
| 433 |
public static function set_cf_properties( $cf, $field_class ) { |
| 434 |
|
| 435 |
// tl_width! |
| 436 |
if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) { |
| 437 |
$tl_width = isset( $_POST['tl_width'] ) ? intval( $_POST['tl_width'] ) : 0; // phpcs:ignore |
| 438 |
$cf->tl_width = $tl_width ? $tl_width : 100; |
| 439 |
} |
| 440 |
|
| 441 |
// save! |
| 442 |
$cf->save(); |
| 443 |
} |
| 444 |
|
| 445 |
/** |
| 446 |
* Returns printable ticket value for custom field. Can be used in export tickets, replace macros etc. |
| 447 |
* |
| 448 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 449 |
* @param WPSC_Ticket $ticket - ticket object. |
| 450 |
* @param string $module - module name. |
| 451 |
* @return string |
| 452 |
*/ |
| 453 |
public static function get_ticket_field_val( $cf, $ticket, $module = '' ) { |
| 454 |
|
| 455 |
return apply_filters( 'wpsc_ticket_field_val_browser', $ticket->{$cf->slug}, $cf, $ticket, $module ); |
| 456 |
} |
| 457 |
|
| 458 |
/** |
| 459 |
* Print ticket value for given custom field on ticket list |
| 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_tl_ticket_field_val( $cf, $ticket ) { |
| 466 |
|
| 467 |
echo esc_attr( self::get_ticket_field_val( $cf, $ticket ) ); |
| 468 |
} |
| 469 |
|
| 470 |
/** |
| 471 |
* Print ticket value for given custom field on widget |
| 472 |
* |
| 473 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 474 |
* @param WPSC_Ticket $ticket - ticket object. |
| 475 |
* @return void |
| 476 |
*/ |
| 477 |
public static function print_widget_ticket_field_val( $cf, $ticket ) { |
| 478 |
|
| 479 |
$browser = $ticket->{$cf->slug}; |
| 480 |
echo $browser ? esc_attr( $browser ) : esc_attr__( 'Not Applicable', 'supportcandy' ); |
| 481 |
} |
| 482 |
} |
| 483 |
endif; |
| 484 |
|
| 485 |
WPSC_DF_Browser::init(); |
| 486 |
|