| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly |
| 4 |
} |
| 5 |
|
| 6 |
$conditions = $conditions ? json_decode($conditions) : array(); |
| 7 |
$meta_keys = $this->get_all_meta_keys(); |
| 8 |
$flag = true; |
| 9 |
|
| 10 |
/** |
| 11 |
* Conditions of same fields sort |
| 12 |
*/ |
| 13 |
$unique_conditions = array(); |
| 14 |
|
| 15 |
foreach ($conditions as $condition){ |
| 16 |
|
| 17 |
$unique_conditions[$condition->field][] = $condition; |
| 18 |
|
| 19 |
} |
| 20 |
|
| 21 |
foreach ( $unique_conditions as $field_key => $unique_condition ){ |
| 22 |
|
| 23 |
$inner_flag = false; |
| 24 |
|
| 25 |
if ( is_numeric($field_key) ){ |
| 26 |
|
| 27 |
foreach ( $unique_condition as $condition) { |
| 28 |
|
| 29 |
$custom_field = get_term_by('id', $field_key, 'wpsc_ticket_custom_fields'); |
| 30 |
$custom_field_type = get_term_meta( $custom_field->term_id, 'wpsc_tf_type', true); |
| 31 |
|
| 32 |
if($custom_field_type=='0'){ |
| 33 |
|
| 34 |
switch ( $custom_field->slug ) { |
| 35 |
|
| 36 |
case 'ticket_category': |
| 37 |
case 'ticket_priority': |
| 38 |
case 'ticket_status': |
| 39 |
case 'customer_name': |
| 40 |
case 'customer_email': |
| 41 |
case 'ticket_subject': |
| 42 |
|
| 43 |
$field_val = $this->get_ticket_fields( $ticket_id, $custom_field->slug ); |
| 44 |
if( $condition->compare == 'match' ) { |
| 45 |
$inner_flag = $condition->cond_val == $field_val ? true : false; |
| 46 |
} elseif( $condition->compare == 'not_match' ) { |
| 47 |
$inner_flag = $condition->cond_val != $field_val ? true : false; |
| 48 |
}else { |
| 49 |
if(strpos($condition->cond_val , '*')){ |
| 50 |
if(fnmatch($condition->cond_val,$field_val)){ |
| 51 |
$inner_flag = true; |
| 52 |
} |
| 53 |
}else{ |
| 54 |
$inner_flag = strpos( $field_val, $condition->cond_val ) !== false ? true : false; |
| 55 |
} |
| 56 |
} |
| 57 |
break; |
| 58 |
|
| 59 |
case 'assigned_agent' : |
| 60 |
|
| 61 |
$field_val = $this->get_ticket_meta( $ticket_id, $custom_field->slug ); |
| 62 |
if( $condition->compare == 'match' ) { |
| 63 |
$inner_flag = in_array( $condition->cond_val, $field_val ) ? true : false; |
| 64 |
} elseif( $condition->compare == 'not_match' ) { |
| 65 |
$inner_flag = (!in_array( $condition->cond_val, $field_val )) ? true : false; |
| 66 |
} |
| 67 |
break; |
| 68 |
|
| 69 |
case 'ticket_description': |
| 70 |
|
| 71 |
$ticket_description = $this->get_ticket_description( $ticket_id ); |
| 72 |
if( $condition->compare == 'match' ) { |
| 73 |
$inner_flag = $condition->cond_val == strip_tags($ticket_description['description']) ? true : false; |
| 74 |
}elseif( $condition->compare == 'not_match' ) { |
| 75 |
$inner_flag = $condition->cond_val != strip_tags($ticket_description['description']) ? true : false; |
| 76 |
}else { |
| 77 |
$inner_flag = strpos( $ticket_description['description'], $condition->cond_val ) !== false ? true : false; |
| 78 |
} |
| 79 |
break; |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
} else { |
| 84 |
|
| 85 |
switch($custom_field_type){ |
| 86 |
|
| 87 |
case 1 : |
| 88 |
case 2 : |
| 89 |
case 4 : |
| 90 |
case 5 : |
| 91 |
case 7 : |
| 92 |
case 8 : |
| 93 |
case 9 : |
| 94 |
|
| 95 |
$field_val = $this->get_ticket_meta( $ticket_id, $custom_field->slug, true ); |
| 96 |
if( $condition->compare == 'match' ) { |
| 97 |
$inner_flag = $condition->cond_val == $field_val ? true : false; |
| 98 |
}elseif( $condition->compare == 'not_match' ) { |
| 99 |
$inner_flag = $condition->cond_val != $field_val ? true : false; |
| 100 |
}else { |
| 101 |
if(strpos($condition->cond_val , '*')){ |
| 102 |
if(fnmatch($condition->cond_val,$field_val)){ |
| 103 |
$inner_flag = true; |
| 104 |
} |
| 105 |
}else{ |
| 106 |
$inner_flag = strpos( $field_val, $condition->cond_val ) !== false ? true : false; |
| 107 |
} |
| 108 |
} |
| 109 |
break; |
| 110 |
|
| 111 |
case 3: |
| 112 |
|
| 113 |
$field_val = $this->get_ticket_meta( $ticket_id, $custom_field->slug ); |
| 114 |
if( $condition->compare == 'match' ) { |
| 115 |
$inner_flag = in_array( $condition->cond_val, $field_val ) ? true : false; |
| 116 |
} elseif( $condition->compare == 'not_match' ) { |
| 117 |
$inner_flag = (!in_array( $condition->cond_val, $field_val )) ? true : false; |
| 118 |
} |
| 119 |
|
| 120 |
break; |
| 121 |
|
| 122 |
default: |
| 123 |
$inner_flag = apply_filters( 'wpsc_check_ticket_conditions_custom_field_type', $inner_flag, $ticket_id, $condition, $custom_field_type , $custom_field ); |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
if( $inner_flag ) break; |
| 130 |
|
| 131 |
} |
| 132 |
|
| 133 |
} else { |
| 134 |
|
| 135 |
if( $field_key == 'agent_created' ){ |
| 136 |
|
| 137 |
$agent_created = $this->get_ticket_fields( $ticket_id, 'agent_created' ); |
| 138 |
foreach ( $unique_condition as $condition) { |
| 139 |
if( $condition->compare == 'match' ) { |
| 140 |
$inner_flag = ( $condition->cond_val == 'agent' && $agent_created > 0 ) || ( $condition->cond_val == 'user' && $agent_created == 0 ) ? true : false; |
| 141 |
}elseif( $condition->compare == 'not_match' ) { |
| 142 |
$inner_flag = ( $condition->cond_val == 'agent' && $agent_created == 0 ) || ( $condition->cond_val == 'user' && $agent_created > 0 ) ? true : false; |
| 143 |
} |
| 144 |
if( $inner_flag ) break; |
| 145 |
} |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
if( $field_key == 'user_type' ){ |
| 150 |
|
| 151 |
$user_type = $this->get_ticket_fields( $ticket_id, 'user_type' ); |
| 152 |
foreach ( $unique_condition as $condition) { |
| 153 |
if( $condition->compare == 'match' ) { |
| 154 |
$inner_flag = ( $condition->cond_val == 'user' && $user_type == 'user' ) || ( $condition->cond_val == 'guest' && $user_type == 'guest' ) ? true : false; |
| 155 |
}elseif( $condition->compare == 'not_match' ) { |
| 156 |
$inner_flag = ( $condition->cond_val == 'user' && $user_type == 'guest' ) || ( $condition->cond_val == 'guest' && $user_type == 'user' ) ? true : false; |
| 157 |
} |
| 158 |
if( $inner_flag ) break; |
| 159 |
} |
| 160 |
|
| 161 |
} |
| 162 |
|
| 163 |
if( $field_key == 'user_wp_roles' ){ |
| 164 |
|
| 165 |
$user_email = $this->get_ticket_fields( $ticket_id, 'customer_email' ); |
| 166 |
$user = get_user_by('email', $user_email); |
| 167 |
if($user){ |
| 168 |
$user_data = get_userdata($user->ID); |
| 169 |
$roles = $user_data->roles; |
| 170 |
foreach ( $unique_condition as $condition) { |
| 171 |
if( $condition->compare == 'match' ) { |
| 172 |
$inner_flag = ( in_array($condition->cond_val, $roles) ) ? true : false; |
| 173 |
}elseif( $condition->compare == 'not_match' ) { |
| 174 |
$inner_flag = ( !in_array($condition->cond_val, $roles) ) ? true : false; |
| 175 |
} |
| 176 |
if( $inner_flag ) break; |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
$inner_flag = apply_filters( 'wpsc_check_custom_ticket_condition', $inner_flag, $ticket_id, $unique_condition ); |
| 182 |
|
| 183 |
} |
| 184 |
|
| 185 |
if( !$inner_flag ){ |
| 186 |
|
| 187 |
$flag = false; |
| 188 |
break; |
| 189 |
|
| 190 |
} |
| 191 |
|
| 192 |
} |
| 193 |
|