| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_DF_IP_Address' ) ) : |
| 7 |
|
| 8 |
final class WPSC_DF_IP_Address { |
| 9 |
|
| 10 |
/** |
| 11 |
* Slug for this custom field type |
| 12 |
* |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
public static $slug = 'df_ip_address'; |
| 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 = false; |
| 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 |
// 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 |
|
| 174 |
/** |
| 175 |
* Load current class to reference classes |
| 176 |
* |
| 177 |
* @param array $classes - Associative array of class names indexed by its slug. |
| 178 |
* @return array |
| 179 |
*/ |
| 180 |
public static function load_ref_class( $classes ) { |
| 181 |
|
| 182 |
$classes[ self::$slug ] = array( |
| 183 |
'class' => __CLASS__, |
| 184 |
'save-key' => 'id', |
| 185 |
); |
| 186 |
return $classes; |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Return default value for custom field of this type |
| 191 |
* |
| 192 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 193 |
* @return mixed |
| 194 |
*/ |
| 195 |
public static function get_default_value( $cf ) { |
| 196 |
|
| 197 |
return ''; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Check and return custom field value for new ticket to be created. |
| 202 |
* This function is used by filter for set create ticket form and called directly by my-profile for each applicable custom fields. |
| 203 |
* Ignore phpcs nonce issue as we already checked where it is called from. |
| 204 |
* |
| 205 |
* @param array $data - Array of values to to stored in ticket in an insert function. |
| 206 |
* @param array $custom_fields - Array containing all applicable custom fields indexed by unique custom field types. |
| 207 |
* @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. |
| 208 |
* @return array |
| 209 |
*/ |
| 210 |
public static function set_create_ticket_data( $data, $custom_fields, $is_my_profile ) { |
| 211 |
|
| 212 |
$data['ip_address'] = self::get_current_user_ip(); |
| 213 |
return $data; |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Set create ticket data for rest api request |
| 218 |
* |
| 219 |
* @param array $data - create ticket data array. |
| 220 |
* @param WP_REST_Request $request - rest request object. |
| 221 |
* @param array $custom_fields - custom field objects indexed by unique custom field types. |
| 222 |
* @return array |
| 223 |
*/ |
| 224 |
public static function set_rest_ticket_data( $data, $request, $custom_fields ) { |
| 225 |
|
| 226 |
$data['ip_address'] = self::get_current_user_ip(); |
| 227 |
return $data; |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Get current user IP address |
| 232 |
* |
| 233 |
* @return string |
| 234 |
*/ |
| 235 |
public static function get_current_user_ip() { |
| 236 |
|
| 237 |
$gdpr = get_option( 'wpsc-gdpr-settings', array() ); |
| 238 |
if ( ! $gdpr['ip-address-collection'] ) { |
| 239 |
return ''; |
| 240 |
} |
| 241 |
|
| 242 |
$ip_address = isset( $_SERVER ) && isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : ''; |
| 243 |
if ( strlen( $ip_address ) > 28 || $ip_address == '::1' ) { |
| 244 |
$ip_address = ''; |
| 245 |
} |
| 246 |
return $ip_address; |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Print edit custom field properties |
| 251 |
* |
| 252 |
* @param WPSC_Custom_Fields $cf - custom field object. |
| 253 |
* @param string $field_class - class name of field category. |
| 254 |
* @return void |
| 255 |
*/ |
| 256 |
public static function get_edit_custom_field_properties( $cf, $field_class ) { |
| 257 |
|
| 258 |
if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) :?> |
| 259 |
<div data-type="number" data-required="false" class="wpsc-input-group tl_width"> |
| 260 |
<div class="label-container"> |
| 261 |
<label for=""> |
| 262 |
<?php esc_attr_e( 'Ticket list width (pixels)', 'supportcandy' ); ?> |
| 263 |
</label> |
| 264 |
</div> |
| 265 |
<input type="number" name="tl_width" value="<?php echo intval( $cf->tl_width ); ?>" autocomplete="off"> |
| 266 |
</div> |
| 267 |
<?php |
| 268 |
endif; |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* Set custom field properties. Can be used by add/edit custom field. |
| 273 |
* Ignore phpcs nonce issue as we already checked where it is called from. |
| 274 |
* |
| 275 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 276 |
* @param string $field_class - class of field category. |
| 277 |
* @return void |
| 278 |
*/ |
| 279 |
public static function set_cf_properties( $cf, $field_class ) { |
| 280 |
|
| 281 |
// tl_width! |
| 282 |
if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) { |
| 283 |
$tl_width = isset( $_POST['tl_width'] ) ? intval( $_POST['tl_width'] ) : 0; // phpcs:ignore |
| 284 |
$cf->tl_width = $tl_width ? $tl_width : 100; |
| 285 |
} |
| 286 |
|
| 287 |
// save! |
| 288 |
$cf->save(); |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Returns printable ticket value for custom field. Can be used in export tickets, replace macros etc. |
| 293 |
* |
| 294 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 295 |
* @param WPSC_Ticket $ticket - ticket object. |
| 296 |
* @param string $module - module name. |
| 297 |
* @return string |
| 298 |
*/ |
| 299 |
public static function get_ticket_field_val( $cf, $ticket, $module = '' ) { |
| 300 |
|
| 301 |
return $ticket->{$cf->slug}; |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* Print ticket value for given custom field on ticket list |
| 306 |
* |
| 307 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 308 |
* @param WPSC_Ticket $ticket - ticket object. |
| 309 |
* @return void |
| 310 |
*/ |
| 311 |
public static function print_tl_ticket_field_val( $cf, $ticket ) { |
| 312 |
|
| 313 |
echo esc_attr( self::get_ticket_field_val( $cf, $ticket ) ); |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* Print ticket value for given custom field on widget |
| 318 |
* |
| 319 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 320 |
* @param WPSC_Ticket $ticket - ticket object. |
| 321 |
* @return void |
| 322 |
*/ |
| 323 |
public static function print_widget_ticket_field_val( $cf, $ticket ) { |
| 324 |
|
| 325 |
$ip = $ticket->{$cf->slug}; |
| 326 |
if ( $ip ) { |
| 327 |
echo '<a class="wpsc-link" href="https://ipinfo.io/' . esc_attr( $ip ) . '" target="_blank">' . esc_attr( $ip ) . '</a>'; |
| 328 |
} else { |
| 329 |
esc_attr_e( 'Not Applicable', 'supportcandy' ); |
| 330 |
} |
| 331 |
} |
| 332 |
} |
| 333 |
endif; |
| 334 |
|
| 335 |
WPSC_DF_IP_Address::init(); |
| 336 |
|