| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_TC_Last_Reply' ) ) : |
| 7 |
|
| 8 |
final class WPSC_TC_Last_Reply { |
| 9 |
|
| 10 |
/** |
| 11 |
* Initialize the class |
| 12 |
* |
| 13 |
* @return void |
| 14 |
*/ |
| 15 |
public static function init() { |
| 16 |
|
| 17 |
// ticket conditions. |
| 18 |
add_filter( 'wpsc_ticket_conditions', array( __CLASS__, 'load_ticket_condition' ) ); |
| 19 |
add_action( 'wpsc_tc_print_operators', array( __CLASS__, 'tc_print_operators' ), 10, 2 ); |
| 20 |
add_action( 'wpsc_tc_print_operand', array( __CLASS__, 'tc_print_operand' ), 10, 3 ); |
| 21 |
add_filter( 'wpsc_tc_is_valid', array( __CLASS__, 'tc_is_valid' ), 10, 4 ); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Load ticket condition for last reply |
| 26 |
* |
| 27 |
* @param array $conditions - conditions filter array. |
| 28 |
* @return array |
| 29 |
*/ |
| 30 |
public static function load_ticket_condition( $conditions ) { |
| 31 |
|
| 32 |
$conditions['last_reply'] = esc_attr__( 'Last Reply', 'supportcandy' ); |
| 33 |
return $conditions; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Print operator for last reply. |
| 38 |
* |
| 39 |
* @param string $slug - slug to check. |
| 40 |
* @param array $filter - preset condition. |
| 41 |
* @return void |
| 42 |
*/ |
| 43 |
public static function tc_print_operators( $slug, $filter ) { |
| 44 |
|
| 45 |
if ( $slug != 'last_reply' ) { |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
?> |
| 50 |
<div class="item conditional"> |
| 51 |
<select class="operator" onchange="wpsc_tc_get_operand(this, '<?php echo esc_attr( $slug ); ?>', '<?php echo esc_attr( wp_create_nonce( 'wpsc_tc_get_operand' ) ); ?>');"> |
| 52 |
<option value=""><?php echo esc_attr( wpsc__( 'Compare As', 'supportcandy' ) ); ?></option> |
| 53 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'LIKE' ); ?> value="LIKE"><?php echo esc_attr( wpsc__( 'Has Words', 'supportcandy' ) ); ?></option> |
| 54 |
<option <?php isset( $filter['operator'] ) && selected( $filter['operator'], 'NOT LIKE' ); ?> value="NOT LIKE"><?php echo esc_attr( wpsc__( 'Does Not Have Words', 'supportcandy' ) ); ?></option> |
| 55 |
</select> |
| 56 |
</div> |
| 57 |
<?php |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Print operand for last reply. |
| 62 |
* |
| 63 |
* @param string $slug - slug to check. |
| 64 |
* @param string $operator - operator value. |
| 65 |
* @param array $filter - preset condition. |
| 66 |
* @return void |
| 67 |
*/ |
| 68 |
public static function tc_print_operand( $slug, $operator, $filter ) { |
| 69 |
|
| 70 |
if ( $slug != 'last_reply' ) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
$value = isset( $filter['operand_val_1'] ) ? stripslashes( $filter['operand_val_1'] ) : ''; |
| 75 |
?> |
| 76 |
<div class="item conditional operand single"> |
| 77 |
<textarea class="operand_val_1" placeholder="<?php esc_attr_e( 'One condition per line!', 'supportcandy' ); ?>" style="width: 100%;"><?php echo esc_attr( $value ); ?></textarea> |
| 78 |
</div> |
| 79 |
<?php |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Check whether condition for the last reply is valid |
| 84 |
* |
| 85 |
* @param boolean $is_valid - filter value. |
| 86 |
* @param string $slug - slug to check. |
| 87 |
* @param array $condition - condition to check. |
| 88 |
* @param WPSC_Ticket $ticket - ticket object on which condition to check. |
| 89 |
* @return boolean |
| 90 |
*/ |
| 91 |
public static function tc_is_valid( $is_valid, $slug, $condition, $ticket ) { |
| 92 |
|
| 93 |
if ( $slug != 'last_reply' ) { |
| 94 |
return $is_valid; |
| 95 |
} |
| 96 |
|
| 97 |
$threads = WPSC_Thread::find( |
| 98 |
array( |
| 99 |
'meta_query' => array( |
| 100 |
'relation' => 'AND', |
| 101 |
array( |
| 102 |
'slug' => 'ticket', |
| 103 |
'compare' => '=', |
| 104 |
'val' => $ticket->id, |
| 105 |
), |
| 106 |
array( |
| 107 |
'slug' => 'type', |
| 108 |
'compare' => '=', |
| 109 |
'val' => 'reply', |
| 110 |
), |
| 111 |
), |
| 112 |
'orderby' => 'id', |
| 113 |
'order' => 'DESC', |
| 114 |
'items_per_page' => 1, |
| 115 |
) |
| 116 |
); |
| 117 |
|
| 118 |
if ( empty( $threads['results'] ) ) { |
| 119 |
return $condition['operator'] == 'NOT LIKE'; |
| 120 |
} |
| 121 |
|
| 122 |
$last_reply = $threads['results'][0]; |
| 123 |
$value = strtolower( stripslashes( wp_strip_all_tags( $last_reply->body ) ) ); |
| 124 |
$terms = array_filter( |
| 125 |
array_map( |
| 126 |
function ( $term ) { |
| 127 |
return strtolower( trim( $term ) ); |
| 128 |
}, |
| 129 |
explode( PHP_EOL, $condition['operand_val_1'] ) |
| 130 |
) |
| 131 |
); |
| 132 |
|
| 133 |
switch ( $condition['operator'] ) { |
| 134 |
|
| 135 |
case 'LIKE': |
| 136 |
$is_valid = false; |
| 137 |
foreach ( $terms as $term ) { |
| 138 |
$index = strpos( $value, trim( stripslashes( $term ) ) ); |
| 139 |
if ( is_numeric( $index ) ) { |
| 140 |
$is_valid = true; |
| 141 |
break; |
| 142 |
} |
| 143 |
} |
| 144 |
break; |
| 145 |
|
| 146 |
case 'NOT LIKE': |
| 147 |
$is_valid = true; |
| 148 |
foreach ( $terms as $term ) { |
| 149 |
$index = strpos( $value, trim( stripslashes( $term ) ) ); |
| 150 |
if ( is_numeric( $index ) ) { |
| 151 |
$is_valid = false; |
| 152 |
break; |
| 153 |
} |
| 154 |
} |
| 155 |
break; |
| 156 |
} |
| 157 |
|
| 158 |
return $is_valid; |
| 159 |
} |
| 160 |
} |
| 161 |
endif; |
| 162 |
|
| 163 |
WPSC_TC_Last_Reply::init(); |
| 164 |
|