| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_DF_Date_Created' ) ) : |
| 7 |
|
| 8 |
final class WPSC_DF_Date_Created { |
| 9 |
|
| 10 |
/** |
| 11 |
* Slug for this custom field type |
| 12 |
* |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
public static $slug = 'df_date_created'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Set whether this custom field type is of type date |
| 19 |
* |
| 20 |
* @var boolean |
| 21 |
*/ |
| 22 |
public static $is_date = true; |
| 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 = 'DATETIME 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 = 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 = 'datetime'; |
| 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 = 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 = 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 |
* Print operators for ticket form filter |
| 191 |
* |
| 192 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 193 |
* @param array $filter - Existing filters (if any). |
| 194 |
* @return void |
| 195 |
*/ |
| 196 |
public static function get_operators( $cf, $filter = array() ) { |
| 197 |
?> |
| 198 |
|
| 199 |
<div class="item conditional"> |
| 200 |
<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' ) ); ?>');"> |
| 201 |
<option value=""><?php esc_attr_e( 'Compare As', 'supportcandy' ); ?></option> |
| 202 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '=' ); ?> value="="><?php esc_attr_e( 'Equals', 'supportcandy' ); ?></option> |
| 203 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '<' ); ?> value="<"><?php esc_attr_e( 'Less than', 'supportcandy' ); ?></option> |
| 204 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '<=' ); ?> value="<="><?php esc_attr_e( 'Less than or equals', 'supportcandy' ); ?></option> |
| 205 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '>' ); ?> value=">"><?php esc_attr_e( 'Greater than', 'supportcandy' ); ?></option> |
| 206 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], '>=' ); ?> value=">="><?php esc_attr_e( 'Greater than or equals', 'supportcandy' ); ?></option> |
| 207 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'BETWEEN' ); ?> value="BETWEEN"><?php esc_attr_e( 'Between', 'supportcandy' ); ?></option> |
| 208 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'today' ); ?> value="today"><?php esc_attr_e( 'Today', 'supportcandy' ); ?></option> |
| 209 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'yesterday' ); ?> value="yesterday"><?php esc_attr_e( 'Yesterday', 'supportcandy' ); ?></option> |
| 210 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'this-week' ); ?> value="this-week"><?php esc_attr_e( 'This week', 'supportcandy' ); ?></option> |
| 211 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'last-week' ); ?> value="last-week"><?php esc_attr_e( 'Last week', 'supportcandy' ); ?></option> |
| 212 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'last-7-days' ); ?> value="last-7-days"><?php esc_attr_e( 'Last 7 days', 'supportcandy' ); ?></option> |
| 213 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'last-30-days' ); ?> value="last-30-days"><?php esc_attr_e( 'Last 30 days', 'supportcandy' ); ?></option> |
| 214 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'this-month' ); ?> value="this-month"><?php esc_attr_e( 'This month', 'supportcandy' ); ?></option> |
| 215 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'last-month' ); ?> value="last-month"><?php esc_attr_e( 'Last month', 'supportcandy' ); ?></option> |
| 216 |
</select> |
| 217 |
</div> |
| 218 |
<?php |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Print operators for ticket form filter |
| 223 |
* |
| 224 |
* @param string $operator - condition operator on which operands should be returned. |
| 225 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 226 |
* @param array $filter - Exising functions (if any). |
| 227 |
* @return void |
| 228 |
*/ |
| 229 |
public static function get_operands( $operator, $cf, $filter = array() ) { |
| 230 |
|
| 231 |
$unique_id = uniqid( 'wpsc_' ); |
| 232 |
$is_between = $operator == 'BETWEEN' ? true : false; |
| 233 |
$operand_val_1 = isset( $filter['operand_val_1'] ) ? $filter['operand_val_1'] : ''; |
| 234 |
$operand_val_2 = isset( $filter['operand_val_2'] ) ? $filter['operand_val_2'] : ''; |
| 235 |
|
| 236 |
$short_filters = array( 'today', 'yesterday', 'this-week', 'last-week', 'last-7-days', 'last-30-days', 'this-month', 'last-month' ); |
| 237 |
if ( in_array( $operator, $short_filters ) ) { |
| 238 |
$today = ( new DateTime() )->format( 'Y-m-d' ); |
| 239 |
?> |
| 240 |
<div class="item conditional operand <?php echo ! $is_between ? 'single' : ''; ?>"> |
| 241 |
<input type="hidden" class="operand_val_1 <?php echo esc_attr( $unique_id ); ?>" value="<?php echo esc_attr( $today ); ?>"> |
| 242 |
</div> |
| 243 |
<?php |
| 244 |
} else { |
| 245 |
?> |
| 246 |
<div class="item conditional operand <?php echo ! $is_between ? 'single' : ''; ?>"> |
| 247 |
<input type="text" class="operand_val_1 <?php echo esc_attr( $unique_id ); ?>" placeholder="<?php echo $is_between ? esc_attr__( 'From date', 'supportcandy' ) : ''; ?>" value="<?php echo esc_attr( $operand_val_1 ); ?>"> |
| 248 |
</div> |
| 249 |
<script> |
| 250 |
jQuery('.operand_val_1.<?php echo esc_attr( $unique_id ); ?>').flatpickr({ |
| 251 |
locale: "<?php echo esc_attr( WPSC_Functions::get_locale_iso() ); ?>", |
| 252 |
}); |
| 253 |
</script> |
| 254 |
<?php |
| 255 |
} |
| 256 |
?> |
| 257 |
<?php |
| 258 |
|
| 259 |
if ( $is_between ) { |
| 260 |
?> |
| 261 |
<div class="item conditional operand"> |
| 262 |
<input type="text" class="operand_val_2 <?php echo esc_attr( $unique_id ); ?>" placeholder="<?php esc_attr_e( 'To date', 'supportcandy' ); ?>" value=""> |
| 263 |
</div> |
| 264 |
<script> |
| 265 |
jQuery('.operand_val_1.<?php echo esc_attr( $unique_id ); ?>').change(function() { |
| 266 |
var fromDate = jQuery(this).val().trim(); |
| 267 |
jQuery('.operand_val_2.<?php echo esc_attr( $unique_id ); ?>').flatpickr({ |
| 268 |
minDate: fromDate, |
| 269 |
defaultDate: fromDate, |
| 270 |
locale: "<?php echo esc_attr( WPSC_Functions::get_locale_iso() ); ?>", |
| 271 |
}); |
| 272 |
}); |
| 273 |
<?php |
| 274 |
if ( $operand_val_2 ) { |
| 275 |
?> |
| 276 |
jQuery('.operand_val_2.<?php echo esc_attr( $unique_id ); ?>').flatpickr({ |
| 277 |
minDate: '<?php echo esc_attr( $operand_val_1 ); ?>', |
| 278 |
defaultDate: '<?php echo esc_attr( $operand_val_2 ); ?>', |
| 279 |
locale: "<?php echo esc_attr( WPSC_Functions::get_locale_iso() ); ?>", |
| 280 |
}); |
| 281 |
<?php |
| 282 |
} |
| 283 |
?> |
| 284 |
</script> |
| 285 |
<?php |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
/** |
| 290 |
* Parse filter and return sql query to be merged in ticket model query builder |
| 291 |
* |
| 292 |
* @param WPSC_Custom_Field $cf - custom field of this type. |
| 293 |
* @param mixed $compare - comparison operator. |
| 294 |
* @param mixed $val - value to compare. |
| 295 |
* @return string |
| 296 |
*/ |
| 297 |
public static function parse_filter( $cf, $compare, $val ) { |
| 298 |
|
| 299 |
$str = ''; |
| 300 |
|
| 301 |
switch ( $compare ) { |
| 302 |
|
| 303 |
case '=': |
| 304 |
$from = WPSC_Functions::get_utc_date_str( $val . ' 00:00:00' ); |
| 305 |
$to = WPSC_Functions::get_utc_date_str( $val . ' 23:59:59' ); |
| 306 |
$str = 't.' . $cf->slug . ' BETWEEN \'' . esc_sql( $from ) . '\' AND \'' . esc_sql( $to ) . '\''; |
| 307 |
break; |
| 308 |
|
| 309 |
case '<': |
| 310 |
$from = WPSC_Functions::get_utc_date_str( $val . ' 00:00:00' ); |
| 311 |
$str = 't.' . $cf->slug . $compare . '\'' . esc_sql( $from ) . '\''; |
| 312 |
break; |
| 313 |
|
| 314 |
case '>': |
| 315 |
$to = WPSC_Functions::get_utc_date_str( $val . ' 23:59:59' ); |
| 316 |
$str = 't.' . $cf->slug . $compare . '\'' . esc_sql( $to ) . '\''; |
| 317 |
break; |
| 318 |
|
| 319 |
case '<=': |
| 320 |
$from = WPSC_Functions::get_utc_date_str( $val . ' 00:00:00' ); |
| 321 |
$to = WPSC_Functions::get_utc_date_str( $val . ' 23:59:59' ); |
| 322 |
$arr = array( |
| 323 |
't.' . $cf->slug . esc_sql( $compare ) . '\'' . esc_sql( $from ) . '\'', |
| 324 |
't.' . $cf->slug . ' BETWEEN \'' . esc_sql( $from ) . '\' AND \'' . esc_sql( $to ) . '\'', |
| 325 |
); |
| 326 |
$str = '(' . implode( ' OR ', $arr ) . ')'; |
| 327 |
break; |
| 328 |
|
| 329 |
case '>=': |
| 330 |
$from = WPSC_Functions::get_utc_date_str( $val . ' 00:00:00' ); |
| 331 |
$to = WPSC_Functions::get_utc_date_str( $val . ' 23:59:59' ); |
| 332 |
$arr = array( |
| 333 |
"t.date_created >= '" . esc_sql( $from ) . "'", |
| 334 |
"t.date_created BETWEEN '" . esc_sql( $from ) . "' AND '" . esc_sql( $to ) . "'", |
| 335 |
); |
| 336 |
$str = '(' . implode( ' OR ', $arr ) . ')'; |
| 337 |
break; |
| 338 |
|
| 339 |
case 'BETWEEN': |
| 340 |
$from = $val['operand_val_1']; |
| 341 |
$to = $val['operand_val_2']; |
| 342 |
if ( preg_match( '/^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}$/', $from ) ) { |
| 343 |
$from = WPSC_Functions::get_utc_date_str( $val['operand_val_1'] ); |
| 344 |
$to = WPSC_Functions::get_utc_date_str( $val['operand_val_2'] ); |
| 345 |
} else { |
| 346 |
$from = WPSC_Functions::get_utc_date_str( $val['operand_val_1'] . ' 00:00:00' ); |
| 347 |
$to = WPSC_Functions::get_utc_date_str( $val['operand_val_2'] . ' 23:59:59' ); |
| 348 |
} |
| 349 |
$str = 't.' . $cf->slug . ' BETWEEN \'' . esc_sql( $from ) . '\' AND \'' . esc_sql( $to ) . '\''; |
| 350 |
break; |
| 351 |
|
| 352 |
case 'today': |
| 353 |
$today = new DateTime(); |
| 354 |
$str = 't.' . $cf->slug . ' BETWEEN \'' . esc_sql( $today->format( 'Y-m-d 00:00:00 ' ) ) . '\' AND \'' . esc_sql( $today->format( 'Y-m-d 23:59:59 ' ) ) . '\''; |
| 355 |
break; |
| 356 |
|
| 357 |
case 'yesterday': |
| 358 |
$yesterday = ( new DateTime() )->modify( '-1 day' ); |
| 359 |
$str = 't.' . $cf->slug . ' BETWEEN \'' . esc_sql( $yesterday->format( 'Y-m-d 00:00:00 ' ) ) . '\' AND \'' . esc_sql( $yesterday->format( 'Y-m-d 23:59:59 ' ) ) . '\''; |
| 360 |
break; |
| 361 |
|
| 362 |
case 'this-week': |
| 363 |
$start_week = new DateTime( 'monday this week' ); |
| 364 |
$end_week = new DateTime( 'sunday this week' ); |
| 365 |
$str = 't.' . $cf->slug . ' BETWEEN \'' . esc_sql( $start_week->format( 'Y-m-d 00:00:00 ' ) ) . '\' AND \'' . esc_sql( $end_week->format( 'Y-m-d 23:59:59 ' ) ) . '\''; |
| 366 |
break; |
| 367 |
|
| 368 |
case 'last-week': |
| 369 |
$start_week = new DateTime( 'monday last week' ); |
| 370 |
$end_week = new DateTime( 'sunday last week' ); |
| 371 |
$str = 't.' . $cf->slug . ' BETWEEN \'' . esc_sql( $start_week->format( 'Y-m-d 00:00:00 ' ) ) . '\' AND \'' . esc_sql( $end_week->format( 'Y-m-d 23:59:59 ' ) ) . '\''; |
| 372 |
break; |
| 373 |
|
| 374 |
case 'last-7-days': |
| 375 |
$end_date = new DateTime(); |
| 376 |
$start_date = ( new DateTime() )->modify( '-7 days' ); |
| 377 |
$str = 't.' . $cf->slug . ' BETWEEN \'' . esc_sql( $start_date->format( 'Y-m-d 00:00:00 ' ) ) . '\' AND \'' . esc_sql( $end_date->format( 'Y-m-d 23:59:59 ' ) ) . '\''; |
| 378 |
break; |
| 379 |
|
| 380 |
case 'last-30-days': |
| 381 |
$end_date = new DateTime(); |
| 382 |
$start_date = ( new DateTime() )->modify( '-30 days' ); |
| 383 |
$str = 't.' . $cf->slug . ' BETWEEN \'' . esc_sql( $start_date->format( 'Y-m-d 00:00:00 ' ) ) . '\' AND \'' . esc_sql( $end_date->format( 'Y-m-d 23:59:59 ' ) ) . '\''; |
| 384 |
break; |
| 385 |
|
| 386 |
case 'this-month': |
| 387 |
$start_month = new DateTime( 'first day of this month' ); |
| 388 |
$end_month = new DateTime( 'last day of this month' ); |
| 389 |
$str = 't.' . $cf->slug . ' BETWEEN \'' . esc_sql( $start_month->format( 'Y-m-d 00:00:00 ' ) ) . '\' AND \'' . esc_sql( $end_month->format( 'Y-m-d 23:59:59 ' ) ) . '\''; |
| 390 |
break; |
| 391 |
|
| 392 |
case 'last-month': |
| 393 |
$start_month = new DateTime( 'first day of last month' ); |
| 394 |
$end_month = new DateTime( 'last day of last month' ); |
| 395 |
$str = 't.' . $cf->slug . ' BETWEEN \'' . esc_sql( $start_month->format( 'Y-m-d 00:00:00 ' ) ) . '\' AND \'' . esc_sql( $end_month->format( 'Y-m-d 23:59:59 ' ) ) . '\''; |
| 396 |
break; |
| 397 |
|
| 398 |
default: |
| 399 |
$str = '1=1'; |
| 400 |
} |
| 401 |
|
| 402 |
return $str; |
| 403 |
} |
| 404 |
|
| 405 |
/** |
| 406 |
* Return orderby string |
| 407 |
* |
| 408 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 409 |
* @return string |
| 410 |
*/ |
| 411 |
public static function get_orderby_string( $cf ) { |
| 412 |
|
| 413 |
return 't.' . $cf->slug; |
| 414 |
} |
| 415 |
|
| 416 |
/** |
| 417 |
* Check ticket condition |
| 418 |
* |
| 419 |
* @param array $condition - array with condition data. |
| 420 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 421 |
* @param WPSC_Ticket $ticket - ticket object. |
| 422 |
* @return boolean |
| 423 |
*/ |
| 424 |
public static function is_valid_ticket_condition( $condition, $cf, $ticket ) { |
| 425 |
|
| 426 |
$flag = true; |
| 427 |
$date = $ticket->{$cf->slug}; |
| 428 |
if ( ! is_object( $date ) ) { |
| 429 |
return false; |
| 430 |
} |
| 431 |
|
| 432 |
$date->setTimezone( wp_timezone() ); |
| 433 |
|
| 434 |
switch ( $condition['operator'] ) { |
| 435 |
|
| 436 |
case '=': |
| 437 |
$flag = $date->format( 'Y-m-d' ) == $condition['operand_val_1'] ? true : false; |
| 438 |
break; |
| 439 |
|
| 440 |
case '<': |
| 441 |
$operand = new DateTime( $condition['operand_val_1'] ); |
| 442 |
$flag = $operand < $date ? true : false; |
| 443 |
break; |
| 444 |
|
| 445 |
case '>': |
| 446 |
$operand = new DateTime( $condition['operand_val_1'] ); |
| 447 |
$flag = $operand > $date ? true : false; |
| 448 |
break; |
| 449 |
|
| 450 |
case '<=': |
| 451 |
$operand = new DateTime( $condition['operand_val_1'] ); |
| 452 |
$flag = $operand <= $date ? true : false; |
| 453 |
break; |
| 454 |
|
| 455 |
case '>=': |
| 456 |
$operand = new DateTime( $condition['operand_val_1'] ); |
| 457 |
$flag = $operand >= $date ? true : false; |
| 458 |
break; |
| 459 |
|
| 460 |
case 'BETWEEN': |
| 461 |
$operand1 = new DateTime( $condition['operand_val_1'] ); |
| 462 |
$operand2 = new DateTime( $condition['operand_val_2'] ); |
| 463 |
$flag = $operand1 <= $date && $operand2 >= $date ? true : false; |
| 464 |
break; |
| 465 |
|
| 466 |
default: |
| 467 |
$flag = true; |
| 468 |
} |
| 469 |
|
| 470 |
return $flag; |
| 471 |
} |
| 472 |
|
| 473 |
/** |
| 474 |
* Return default value for custom field of this type |
| 475 |
* |
| 476 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 477 |
* @return mixed |
| 478 |
*/ |
| 479 |
public static function get_default_value( $cf ) { |
| 480 |
|
| 481 |
$now = new DateTime(); |
| 482 |
return $now->format( 'Y-m-d H:i:s' ); |
| 483 |
} |
| 484 |
|
| 485 |
/** |
| 486 |
* Check and return custom field value for new ticket to be created. |
| 487 |
* This function is used by filter for set create ticket form and called directly by my-profile for each applicable custom fields. |
| 488 |
* Ignore phpcs nonce issue as we already checked where it is called from. |
| 489 |
* |
| 490 |
* @param array $data - Array of values to to stored in ticket in an insert function. |
| 491 |
* @param array $custom_fields - Array containing all applicable custom fields indexed by unique custom field types. |
| 492 |
* @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. |
| 493 |
* @return array |
| 494 |
*/ |
| 495 |
public static function set_create_ticket_data( $data, $custom_fields, $is_my_profile ) { |
| 496 |
|
| 497 |
$data['date_created'] = self::get_default_value( $custom_fields[ self::$slug ][0] ); |
| 498 |
return $data; |
| 499 |
} |
| 500 |
|
| 501 |
/** |
| 502 |
* Set create ticket data for rest api request |
| 503 |
* |
| 504 |
* @param array $data - create ticket data array. |
| 505 |
* @param WP_REST_Request $request - rest request object. |
| 506 |
* @param array $custom_fields - custom field objects indexed by unique custom field types. |
| 507 |
* @return array |
| 508 |
*/ |
| 509 |
public static function set_rest_ticket_data( $data, $request, $custom_fields ) { |
| 510 |
|
| 511 |
$data['date_created'] = self::get_default_value( $custom_fields[ self::$slug ][0] ); |
| 512 |
return $data; |
| 513 |
} |
| 514 |
|
| 515 |
/** |
| 516 |
* Return val field for meta query of this type of custom field |
| 517 |
* |
| 518 |
* @param array $condition - condition data. |
| 519 |
* @return mixed |
| 520 |
*/ |
| 521 |
public static function get_meta_value( $condition ) { |
| 522 |
|
| 523 |
$operator = $condition['operator']; |
| 524 |
switch ( $operator ) { |
| 525 |
|
| 526 |
case '=': |
| 527 |
case '<': |
| 528 |
case '<=': |
| 529 |
case '>': |
| 530 |
case '>=': |
| 531 |
case 'today': |
| 532 |
case 'yesterday': |
| 533 |
case 'this-week': |
| 534 |
case 'last-week': |
| 535 |
case 'last-7-days': |
| 536 |
case 'last-30-days': |
| 537 |
case 'this-month': |
| 538 |
case 'last-month': |
| 539 |
return $condition['operand_val_1']; |
| 540 |
|
| 541 |
case 'BETWEEN': |
| 542 |
if ( |
| 543 |
! ( isset( $condition['operand_val_1'] ) && preg_match( '/^(\d{4})-\d{2}-(\d{2})$/', $condition['operand_val_1'] ) ) || |
| 544 |
! ( isset( $condition['operand_val_2'] ) && preg_match( '/^(\d{4})-\d{2}-(\d{2})$/', $condition['operand_val_2'] ) ) |
| 545 |
) { |
| 546 |
return false; |
| 547 |
} |
| 548 |
|
| 549 |
return array( |
| 550 |
'operand_val_1' => $condition['operand_val_1'], |
| 551 |
'operand_val_2' => $condition['operand_val_2'], |
| 552 |
); |
| 553 |
} |
| 554 |
return false; |
| 555 |
} |
| 556 |
|
| 557 |
/** |
| 558 |
* Return data for this custom field while creating duplicate ticket |
| 559 |
* |
| 560 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 561 |
* @param WPSC_Ticket $ticket - ticket object. |
| 562 |
* @return mixed |
| 563 |
*/ |
| 564 |
public static function get_duplicate_ticket_data( $cf, $ticket ) { |
| 565 |
|
| 566 |
return ( new DateTime() )->format( 'Y-m-d H:i:s' ); |
| 567 |
} |
| 568 |
|
| 569 |
/** |
| 570 |
* Print edit custom field properties |
| 571 |
* |
| 572 |
* @param WPSC_Custom_Fields $cf - custom field object. |
| 573 |
* @param string $field_class - class name of field category. |
| 574 |
* @return void |
| 575 |
*/ |
| 576 |
public static function get_edit_custom_field_properties( $cf, $field_class ) { |
| 577 |
?> |
| 578 |
|
| 579 |
<div data-type="date-format" data-required="false" class="wpsc-input-group date-format"> |
| 580 |
<div class="label-container"> |
| 581 |
<label for=""><?php esc_attr_e( 'Display as', 'supportcandy' ); ?></label> |
| 582 |
</div> |
| 583 |
<select name="date_display_as"> |
| 584 |
<option <?php selected( $cf->date_display_as, 'date' ); ?> value="date"><?php esc_attr_e( 'Date Format', 'supportcandy' ); ?></option> |
| 585 |
<option <?php selected( $cf->date_display_as, 'diff' ); ?> value="diff"><?php esc_attr_e( 'Date Difference (e.g. 1 hour ago)', 'supportcandy' ); ?></option> |
| 586 |
</select> |
| 587 |
</div> |
| 588 |
|
| 589 |
<div data-type="date-format" data-required="false" class="wpsc-input-group date-format"> |
| 590 |
<div class="label-container"> |
| 591 |
<label for=""><?php esc_attr_e( 'Date format', 'supportcandy' ); ?></label> |
| 592 |
</div> |
| 593 |
<input type="text" name="date_format" value="<?php echo esc_attr( $cf->date_format ); ?>" autocomplete="off" /> |
| 594 |
</div> |
| 595 |
<?php |
| 596 |
|
| 597 |
if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) : |
| 598 |
?> |
| 599 |
<div data-type="number" data-required="false" class="wpsc-input-group tl_width"> |
| 600 |
<div class="label-container"> |
| 601 |
<label for=""> |
| 602 |
<?php esc_attr_e( 'Ticket list width (pixels)', 'supportcandy' ); ?> |
| 603 |
</label> |
| 604 |
</div> |
| 605 |
<input type="number" name="tl_width" value="<?php echo esc_attr( intval( $cf->tl_width ) ); ?>" autocomplete="off"> |
| 606 |
</div> |
| 607 |
<?php |
| 608 |
endif; |
| 609 |
} |
| 610 |
|
| 611 |
/** |
| 612 |
* Set custom field properties. Can be used by add/edit custom field. |
| 613 |
* Ignore phpcs nonce issue as we already checked where it is called from. |
| 614 |
* |
| 615 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 616 |
* @param string $field_class - class of field category. |
| 617 |
* @return void |
| 618 |
*/ |
| 619 |
public static function set_cf_properties( $cf, $field_class ) { |
| 620 |
|
| 621 |
// date display as. |
| 622 |
$cf->date_display_as = isset( $_POST['date_display_as'] ) ? sanitize_text_field( wp_unslash( $_POST['date_display_as'] ) ) : 'date'; // phpcs:ignore |
| 623 |
|
| 624 |
// date format. |
| 625 |
$cf->date_format = isset( $_POST['date_format'] ) ? sanitize_text_field( wp_unslash( $_POST['date_format'] ) ) : ''; // phpcs:ignore |
| 626 |
|
| 627 |
// tl_width! |
| 628 |
if ( in_array( 'tl_width', $field_class::$allowed_properties ) ) { |
| 629 |
$tl_width = isset( $_POST['tl_width'] ) ? intval( $_POST['tl_width'] ) : 0; // phpcs:ignore |
| 630 |
$cf->tl_width = $tl_width ? $tl_width : 100; |
| 631 |
} |
| 632 |
|
| 633 |
// save! |
| 634 |
$cf->save(); |
| 635 |
} |
| 636 |
|
| 637 |
/** |
| 638 |
* Returns printable ticket value for custom field. Can be used in export tickets, replace macros etc. |
| 639 |
* |
| 640 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 641 |
* @param WPSC_Ticket $ticket - ticket object. |
| 642 |
* @param string $module - module name. |
| 643 |
* @return string |
| 644 |
*/ |
| 645 |
public static function get_ticket_field_val( $cf, $ticket, $module = '' ) { |
| 646 |
|
| 647 |
$general_settings = get_option( 'wpsc-gs-general' ); |
| 648 |
$format = $cf->date_format ? $cf->date_format : $general_settings['default-date-format']; |
| 649 |
$date = $ticket->{$cf->slug}; |
| 650 |
$value = is_object( $date ) ? wp_date( $format, $date->setTimezone( wp_timezone() )->getTimestamp() ) : ''; |
| 651 |
|
| 652 |
return apply_filters( 'wpsc_ticket_field_val_date_created', $value, $cf, $ticket, $module ); |
| 653 |
} |
| 654 |
|
| 655 |
/** |
| 656 |
* Print ticket value for given custom field on ticket list |
| 657 |
* |
| 658 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 659 |
* @param WPSC_Ticket $ticket - ticket object. |
| 660 |
* @return void |
| 661 |
*/ |
| 662 |
public static function print_tl_ticket_field_val( $cf, $ticket ) { |
| 663 |
|
| 664 |
$now = new DateTime(); |
| 665 |
$date = $ticket->{$cf->slug}; |
| 666 |
$date_str = self::get_ticket_field_val( $cf, $ticket ); |
| 667 |
$diff_str = WPSC_Functions::date_interval_highest_unit_ago( $date->diff( $now ) ); |
| 668 |
if ( $cf->date_display_as == 'date' ) { |
| 669 |
?> |
| 670 |
<span title="<?php echo esc_attr( $diff_str ); ?>"><?php echo esc_attr( $date_str ); ?></span> |
| 671 |
<?php |
| 672 |
} else { |
| 673 |
?> |
| 674 |
<span title="<?php echo esc_attr( $date_str ); ?>"><?php echo esc_attr( $diff_str ); ?></span> |
| 675 |
<?php |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
/** |
| 680 |
* Print ticket value for given custom field on widget |
| 681 |
* |
| 682 |
* @param WPSC_Custom_Field $cf - custom field object. |
| 683 |
* @param WPSC_Ticket $ticket - ticket object. |
| 684 |
* @return void |
| 685 |
*/ |
| 686 |
public static function print_widget_ticket_field_val( $cf, $ticket ) { |
| 687 |
|
| 688 |
self::print_tl_ticket_field_val( $cf, $ticket ); |
| 689 |
} |
| 690 |
} |
| 691 |
endif; |
| 692 |
|
| 693 |
WPSC_DF_Date_Created::init(); |
| 694 |
|