type ) &&
$cf->type::$is_filter &&
in_array( $cf->field, array( 'ticket', 'agentonly', 'customer' ) )
) ) {
continue;
}
$levels = array( 'admin' );
if ( in_array( $cf->slug, $agent_filters ) ) {
$levels[] = 'agent';
}
if ( in_array( $cf->slug, $customer_filters ) ) {
$levels[] = 'customer';
}
self::$conditions[ $cf->slug ] = array(
'name' => $cf->name,
'type' => 'cf',
'levels' => $levels,
);
}
// custom conditions.
$other_conditions = apply_filters(
'wpsc_ticket_conditions',
array(
'submitted_by' => esc_attr__( 'Ticket submitted by', 'supportcandy' ),
'user_role' => esc_attr__( 'WP user role', 'supportcandy' ),
)
);
foreach ( $other_conditions as $slug => $name ) {
self::$conditions[ $slug ] = array(
'name' => $name,
'type' => 'other',
'levels' => array( 'admin' ),
);
}
}
/**
* Print condition input in the form
*
* @param string $name - form element name. Helpful to identify if multiple conditions need to be added.
* @param string $hook - filter hook for conditions. Uniquely identify and filter conditions available in the form.
* @param string $set_conditions - pre-defined conditions to print within input.
* @param boolean $is_required - whether or not to print required character for label.
* @param string $label - whether or not to print required character for label.
* @return void
*/
public static function print( $name, $hook, $set_conditions = '', $is_required = false, $label = '' ) {
$label = $label ? $label : __( 'Conditions', 'supportcandy' );
$conditions = apply_filters( $hook, self::$conditions );
$set_conditions = $set_conditions ? json_decode( html_entity_decode( $set_conditions ), true ) : array();
$unique_id = wp_unique_id( 'wpsc_' );
?>
level, self::$conditions[ $slug ]['levels'] ) ) {
wp_send_json_error( 'Unauthorised request!', 401 );
}
if ( self::$conditions[ $slug ]['type'] == 'cf' ) {
$cf = WPSC_Custom_Field::get_cf_by_slug( $slug );
$cf->type::get_operators( $cf );
} else {
self::print_operators( $slug );
}
wp_die();
}
/**
* Print condition operators
*
* @param string $slug - condition slug.
* @param array $filter - predefined condition to set operator.
* @return void
*/
public static function print_operators( $slug, $filter = array() ) {
switch ( $slug ) {
case 'submitted_by':
?>
level, self::$conditions[ $slug ]['levels'] ) ) {
wp_send_json_error( 'Unauthorised request!', 401 );
}
$operator = isset( $_POST['operator'] ) ? sanitize_text_field( wp_unslash( $_POST['operator'] ) ) : '';
if ( ! $operator ) {
wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 );
}
if ( self::$conditions[ $slug ]['type'] == 'cf' ) {
$cf = WPSC_Custom_Field::get_cf_by_slug( $slug );
$cf->type::get_operands( $operator, $cf );
} else {
self::print_operands( $slug, $operator );
}
wp_die();
}
/**
* Print condition operands
*
* @param string $slug - condition slug.
* @param string $operator - operator string.
* @param array $filter - predefined condition to set operand.
* @return void
*/
public static function print_operands( $slug, $operator, $filter = array() ) {
global $wp_roles;
switch ( $slug ) {
case 'submitted_by':
?>
level, self::$conditions[ $or_condition['slug'] ]['levels'] ) )
) {
continue;
}
$cf = WPSC_Custom_Field::get_cf_by_slug( $or_condition['slug'] );
$val = $cf->type::get_meta_value( $or_condition );
if ( $val === false ) {
continue;
}
$temp[] = array(
'slug' => $or_condition['slug'],
'compare' => $or_condition['operator'],
'val' => $val,
);
}
if ( $temp ) {
$meta_query[] = array_merge( array( 'relation' => 'OR' ), $temp );
}
}
return $meta_query;
}
/**
* Return whether or not given conditions are satisfied by the given ticket
*
* @param string $conditions - conditions json from database.
* @param WPSC_Ticket $ticket - ticket object.
* @return boolean
*/
public static function is_valid( $conditions, $ticket ) {
$conditions = $conditions ? json_decode( html_entity_decode( $conditions ), true ) : array();
if ( ! $conditions ) {
return true;
}
foreach ( $conditions as $and_condition ) {
$flag = false;
foreach ( $and_condition as $or_condition ) {
if ( ! isset( $or_condition['slug'] ) || ! isset( self::$conditions[ $or_condition['slug'] ] ) ) {
continue;
}
if ( self::$conditions[ $or_condition['slug'] ]['type'] == 'cf' ) {
$cf = WPSC_Custom_Field::get_cf_by_slug( $or_condition['slug'] );
if ( in_array( $cf->type::$slug, self::$ignore_cft ) ) {
continue;
}
if ( $cf->field == 'customer' ) {
$flag = $cf->type::is_valid_customer_condition( $or_condition, $cf, $ticket->customer );
} else {
$flag = $cf->type::is_valid_ticket_condition( $or_condition, $cf, $ticket );
}
} else {
switch ( $or_condition['slug'] ) {
case 'submitted_by':
if (
(
$or_condition['operand_val_1'] == 'agent' &&
is_object( $ticket->agent_created )
) ||
(
$or_condition['operand_val_1'] == 'user' &&
! is_object( $ticket->agent_created )
)
) {
$flag = true;
}
break;
case 'user_role':
$user_roles = $ticket->customer->user->roles;
switch ( $or_condition['operator'] ) {
case '=':
$flag = in_array( $or_condition['operand_val_1'], $user_roles ) ? true : false;
break;
case 'IN':
foreach ( $user_roles as $id ) {
if ( in_array( $id, $or_condition['operand_val_1'] ) ) {
$flag = true;
break;
}
}
break;
case 'NOT IN':
$temp = true;
foreach ( $user_roles as $id ) {
if ( in_array( $id, $or_condition['operand_val_1'] ) ) {
$temp = false;
break;
}
}
$flag = $temp;
break;
}
break;
default:
$flag = apply_filters( 'wpsc_tc_is_valid', $flag, $or_condition['slug'], $or_condition, $ticket );
break;
}
}
// no need to check further OR conditions if this condition is TRUE.
if ( $flag ) {
break;
}
}
// no need to check further AND conditions if this condition is FALSE.
if ( ! $flag ) {
return false;
}
}
// if we make upto this point, all conditions are satisfied.
return true;
}
}
endif;
WPSC_Ticket_Conditions::init();