fields = $this->get_fields();
$this->campaign_related = $this->get_campaign_related();
$this->aggregate_campaigns = $this->get_aggregate_campaigns();
$this->operators = $this->get_operators();
$this->simple_operators = $this->get_simple_operators();
$this->string_operators = $this->get_string_operators();
$this->bool_operators = $this->get_bool_operators();
$this->campaign_rules = self::get_campaign_rules();
add_action( 'ig_es_show_campaign_rules', array( $this, 'show_campaign_rules' ), 10, 2 );
add_action( 'ig_es_campaign_show_conditions', array( $this, 'show_conditions' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
}
/**
* Register the JavaScript for campaign rules.
*/
public function enqueue_scripts() {
$current_page = ig_es_get_request_data( 'page' );
if ( in_array( $current_page, array( 'es_notifications', 'es_newsletters', 'es_sequence' ), true ) ) {
wp_register_script( 'alpine', plugins_url( '/js/alpine.js', __FILE__ ), array(), '2.8.2', false );
wp_enqueue_script( 'alpine' );
}
}
/**
* Method to show campaign rules
*
* @param array $campaign_data Broadcast data
*
* @since 4.6.11
*/
public function show_campaign_rules( $campaign_id = 0, $campaign_data = array() ) {
$current_page = ig_es_get_request_data( 'page' );
$campaign_type = '';
if ( 'es_newsletters' === $current_page ) {
$campaign_type = 'newsletter';
} elseif ( 'es_notifications' === $current_page ) {
$campaign_type = 'post_notification';
} else {
$campaign_type = 'sequence_message';
}
$conditions = array();
if ( ! empty( $campaign_data['meta'] ) ) {
$campaign_meta = maybe_unserialize( $campaign_data['meta'] );
$conditions = ! empty( $campaign_meta['list_conditions'] ) ? $campaign_meta['list_conditions'] : array();
}
$args = array();
if ( IG_CAMPAIGN_TYPE_NEWSLETTER === $campaign_type ) {
$args = array(
'include_types' => array(
IG_CAMPAIGN_TYPE_NEWSLETTER,
),
'status' => array(
IG_ES_CAMPAIGN_STATUS_QUEUED,
IG_ES_CAMPAIGN_STATUS_FINISHED,
),
);
} elseif ( IG_CAMPAIGN_TYPE_POST_NOTIFICATION === $campaign_type ) {
$args = array(
'include_types' => array(
IG_CAMPAIGN_TYPE_POST_NOTIFICATION,
),
);
} elseif ( IG_CAMPAIGN_TYPE_POST_DIGEST === $campaign_type ) {
$args = array(
'include_types' => array(
IG_CAMPAIGN_TYPE_POST_DIGEST,
),
);
} else {
$args = array(
'include_types' => array(
'sequence_message',
),
);
}
if ( ! empty( $campaign_id ) ) {
$args['campaigns_not_in'] = array( $campaign_id );
}
$all_campaigns = ES()->campaigns_db->get_campaigns( $args );
$lists = ES()->lists_db->get_list_id_name_map();
$countries_data = ES_Geolocation::get_countries();
if ( 'es_newsletters' === $current_page ) {
$input_name = 'data[meta][list_conditions]';
} elseif ( 'es_notifications' === $current_page ) {
$input_name = 'data[meta][list_conditions]';
} else {
$input_name = 'seq_data[' . $campaign_id . '][list_conditions]';
}
$select_list_attr = ES()->is_pro() ? 'multiple="multiple"' : '';
$select_list_class = ES()->is_pro() ? 'ig-es-campaign-rule-form-multiselect' : 'form-select';
$sidebar_id = 'sidebar_' . $campaign_id;
?>
$condition_group ) :
if ( ! empty( $condition_group ) ) :
?>
' . esc_html__( 'and', 'email-subscribers' );
if ( count( $condition_group ) > 1 ) {
echo esc_html( ' ( ' );
}
echo wp_kses( '', $allowedtags );
}
foreach ( $condition_group as $j => $condition ) :
$condition_html = $this->get_condition_html( $condition );
?>
' . esc_html__( ' or', 'email-subscribers' ) . '';
}
?>
1 ) {
echo '
' . esc_html__( ') ', 'email-subscribers' ) . '';
}
?>
':
return 'contains';
case '!<>':
return 'contains_not';
case '^':
return 'begin_with';
case '$':
return 'end_with';
case '>=':
return 'is_greater_equal';
case '<=':
return 'is_smaller_equal';
case '>':
return 'is_greater';
case '<':
return 'is_smaller';
case '%':
return 'pattern';
case '!%':
return 'not_pattern';
}
return $operator;
}
/**
* Get condition HTML
*
* @return string Get condition HTML
*/
private function get_condition_html( $condition, $formated = true ) {
$field = isset( $condition['field'] ) ? $condition['field'] : ( isset( $condition[0] ) ? $condition[0] : '' );
$operator = isset( $condition['operator'] ) ? $condition['operator'] : ( isset( $condition[1] ) ? $condition[1] : '' );
$value = stripslashes_deep( isset( $condition['value'] ) ? $condition['value'] : ( isset( $condition[2] ) ? $condition[2] : '' ) );
$return = array(
'field' => '
' . $this->nice_name( $field, 'field', $field ) . '',
'operator' => '',
'value' => '',
);
$opening_quote = esc_html_x( '“', 'opening curly double quote', 'email-subscribers' );
$closing_quote = esc_html_x( '”', 'closing curly double quote', 'email-subscribers' );
if ( isset( $this->campaign_rules['Campaign'][ $field ] ) ) {
if ( ! is_array( $value ) ) {
$value = array( $value );
}
$return['value'] = '
' . $opening_quote . implode( $closing_quote . ' ' . esc_html__( 'or', 'email-subscribers' ) . ' ' . $opening_quote, array_map( array( $this, 'get_campaign_name' ), $value ) ) . $closing_quote . '';
} elseif ( isset( $this->campaign_rules['List'][ $field ] ) ) {
if ( ! is_array( $value ) ) {
$value = array( $value );
}
$return['value'] = '
' . $opening_quote . implode( $closing_quote . ' ' . esc_html__( 'or', 'email-subscribers' ) . ' ' . $opening_quote, array_map( array( $this, 'get_list_name' ), $value ) ) . $closing_quote . '';
} elseif ( 'country_code' === $field ) {
if ( ! is_array( $value ) ) {
$value = array( $value );
}
$return['operator'] = '
' . $this->nice_name( $operator, 'operator', $field ) . '';
$return['value'] = $opening_quote . implode( $closing_quote . ' ' . esc_html__( 'or', 'email-subscribers' ) . ' ' . $opening_quote, array_map( array( $this, 'get_country_name' ), $value ) ) . $closing_quote;
} elseif ( 'bounce_status' === $field ) {
if ( ! is_array( $value ) ) {
$value = array( $value );
}
$return['operator'] = '
' . $this->nice_name( $operator, 'operator', $field ) . '';
$return['value'] = $opening_quote . implode( $closing_quote . ' ' . esc_html__( 'or', 'email-subscribers' ) . ' ' . $opening_quote, array_map( array( $this, 'get_bounce_status_name' ), $value ) ) . $closing_quote;
} elseif ( false !== strpos( $field, 'cf_' ) ) {
if ( ! is_array( $value ) ) {
$value = array( $value );
}
$return['operator'] = '
' . $this->nice_name( $operator, 'operator', $field ) . '';
$return['value'] = $opening_quote . implode( $closing_quote . ' ' . esc_html__( 'or', 'email-subscribers' ) . ' ' . $opening_quote, $value ) . $closing_quote;
} else {
$return['operator'] = '
' . $this->nice_name( $operator, 'operator', $field ) . '';
$return['value'] = $opening_quote . '
' . $this->nice_name( $value, 'value', $field ) . '' . $closing_quote;
}
return $formated ? $return : strip_tags( $return );
}
/**
* Get names for field, operator and value
*
* @return string Formatted string
*/
private function nice_name( $string, $type = null, $field = null ) {
switch ( $type ) {
case 'field':
foreach ( $this->campaign_rules as $rule_group => $rules ) {
foreach ( $rules as $rule_slug => $rule ) {
if ( $string === $rule_slug ) {
return $rule['name'];
}
}
}
break;
case 'operator':
if ( isset( $this->operators[ $string ] ) ) {
return $this->operators[ $string ];
}
if ( 'AND' == $string ) {
return esc_html__( 'and', 'email-subscribers' );
}
if ( 'OR' == $string ) {
return esc_html__( 'or', 'email-subscribers' );
}
break;
}
return $string;
}
/**
* Get list of campaign rules
*
* @return array List of campaign rules
*/
public static function get_campaign_rules() {
$campaign_rules = array(
'List' => array(
'_lists__in' => array(
'name' => esc_html__( 'is in List', 'email-subscribers' ),
),
),
);
$campaign_rules = apply_filters( 'ig_es_campaign_rules', $campaign_rules );
return $campaign_rules;
}
/**
* Get list of subscribers data based rules
*
* @return array List of subscribers data based rules
*/
private function get_fields() {
$fields = array(
'email' => esc_html__( 'Email', 'email-subscribers' ),
'country_code' => esc_html__( 'Country', 'email-subscribers' ),
);
return $fields;
}
/**
* Get list of campaign related rules
*
* @return array List of aggregate campaigns related rules
*/
private function get_campaign_related() {
return array(
'_sent' => esc_html__( 'has received', 'email-subscribers' ),
'_sent__not_in' => esc_html__( 'has not received', 'email-subscribers' ),
'_open' => esc_html__( 'has received and opened', 'email-subscribers' ),
'_open__not_in' => esc_html__( 'has received but not opened', 'email-subscribers' ),
'_click' => esc_html__( 'has received and clicked', 'email-subscribers' ),
'_click__not_in' => esc_html__( 'has received and not clicked', 'email-subscribers' ),
);
}
/**
* Get list of aggregate campaigns related rules
*
* @return array List of aggregate campaigns related rules
*/
private function get_aggregate_campaigns() {
return array(
'_last_5' => esc_html__( 'Any of the Last 5 Campaigns', 'email-subscribers' ),
'_last_7_day' => esc_html__( 'Any Campaigns within the last 7 days', 'email-subscribers' ),
'_last_1_month' => esc_html__( 'Any Campaigns within the last 1 month', 'email-subscribers' ),
'_last_3_months' => esc_html__( 'Any Campaigns within the last 3 months', 'email-subscribers' ),
'_last_6_months' => esc_html__( 'Any Campaigns within the last 6 months', 'email-subscribers' ),
'_last_12_months' => esc_html__( 'Any Campaigns within the last 12 months', 'email-subscribers' ),
);
}
/**
* Get list of comparison operators
*
* @return array List of comparison operators
*/
public function get_operators() {
return array(
'is' => esc_html__( 'is', 'email-subscribers' ),
'is_not' => esc_html__( 'is not', 'email-subscribers' ),
'contains' => esc_html__( 'contains', 'email-subscribers' ),
'contains_not' => esc_html__( 'contains not', 'email-subscribers' ),
'begin_with' => esc_html__( 'begins with', 'email-subscribers' ),
'end_with' => esc_html__( 'ends with', 'email-subscribers' ),
'is_greater' => esc_html__( 'is greater than', 'email-subscribers' ),
'is_smaller' => esc_html__( 'is smaller than', 'email-subscribers' ),
'is_greater_equal' => esc_html__( 'is greater or equal', 'email-subscribers' ),
'is_smaller_equal' => esc_html__( 'is smaller or equal', 'email-subscribers' ),
'pattern' => esc_html__( 'match regex pattern', 'email-subscribers' ),
'not_pattern' => esc_html__( 'does not match regex pattern', 'email-subscribers' ),
);
}
/**
* Get list of simple operators
*
* @return array Simple operators
*/
public function get_simple_operators() {
return array(
'is' => esc_html__( 'is', 'email-subscribers' ),
'is_not' => esc_html__( 'is not', 'email-subscribers' ),
'is_greater' => esc_html__( 'is greater than', 'email-subscribers' ),
'is_smaller' => esc_html__( 'is smaller than', 'email-subscribers' ),
'is_greater_equal' => esc_html__( 'is greater or equal', 'email-subscribers' ),
'is_smaller_equal' => esc_html__( 'is smaller or equal', 'email-subscribers' ),
);
}
/**
* Get list of string operators
*
* @return array String operators
*/
public function get_string_operators() {
return array(
'is' => esc_html__( 'is', 'email-subscribers' ),
'is_not' => esc_html__( 'is not', 'email-subscribers' ),
'contains' => esc_html__( 'contains', 'email-subscribers' ),
'contains_not' => esc_html__( 'contains not', 'email-subscribers' ),
'begin_with' => esc_html__( 'begins with', 'email-subscribers' ),
'end_with' => esc_html__( 'ends with', 'email-subscribers' ),
'pattern' => esc_html__( 'match regex pattern', 'email-subscribers' ),
'not_pattern' => esc_html__( 'does not match regex pattern', 'email-subscribers' ),
);
}
/**
* Get list of boolean operators
*
* @return array Boolean operator
*/
public function get_bool_operators() {
return array(
'is' => esc_html__( 'is', 'email-subscribers' ),
'is_not' => esc_html__( 'is not', 'email-subscribers' ),
);
}
/**
* Get campaign name
*
* @param int $campaign_id Campaign ID
*
* @return string $name Campaign name
*/
public function get_campaign_name( $campaign_id ) {
if ( ! $campaign_id ) {
return esc_html__( 'Any campaign', 'email-subscribers' );
}
if ( isset( $this->aggregate_campaigns[ $campaign_id ] ) ) {
return $this->aggregate_campaigns[ $campaign_id ];
}
$campaign = ES()->campaigns_db->get( $campaign_id );
if ( empty( $campaign['name'] ) ) {
$name = '#' . $campaign_id;
} else {
$name = $campaign['name'];
}
return '
' . $name . '';
}
/**
* Get list name
*
* @param int $list_id list ID
*
* @return string $list_name list name
*/
public function get_list_name( $list_id ) {
if ( ! $list_id ) {
return esc_html__( 'Any list', 'email-subscribers' );
}
$lists = ES()->lists_db->get_list_id_name_map();
$list_name = isset( $lists[ $list_id ] ) ? $lists[ $list_id ] : $list_id;
return $list_name;
}
/**
* Get bounce status
*
* @param string $bounce_status bounce status code
*
* @return string bounce status
*/
public function get_bounce_status_name( $bounce_status ) {
switch ( $bounce_status ) {
case '2':
return esc_html__( 'Hard bounced', 'email-subscribers' );
case '1':
return esc_html__( 'Soft bounced', 'email-subscribers' );
case '0':
return esc_html__( 'Un-bounced', 'email-subscribers' );
default:
return esc_html__( 'Any status', 'email-subscribers' );
}
}
/**
* Get country name
*
* @param string $code country code
*
* @return string $country_name country name
*/
public function get_country_name( $code ) {
$country_name = ES_Geolocation::get_country_name_from_country_code( $code );
return $country_name;
}
/**
* Remove empty conditions from campaign data
* We check condition field and condition value to check for empty conditions
*
* @param array $conditions_data
*
* @return array $conditions_data
*/
public static function remove_empty_conditions( $conditions_data = array() ) {
if ( ! empty( $conditions_data ) ) {
$list_conditions = $conditions_data;
foreach ( $list_conditions as $i => $and_cond ) {
foreach ( $and_cond as $j => $cond ) {
if ( ! isset( $list_conditions[ $i ][ $j ]['field'] ) || ! isset( $list_conditions[ $i ][ $j ]['value'] ) ) {
unset( $list_conditions[ $i ][ $j ] );
} elseif ( is_array( $list_conditions[ $i ][ $j ]['value'] ) ) {
$list_conditions[ $i ][ $j ]['value'] = array_values( array_unique( $list_conditions[ $i ][ $j ]['value'] ) );
}
}
}
// Remove any empty value array.
$conditions_data = array_values( array_filter( $list_conditions ) );
}
return $conditions_data;
}
}
}
new IG_ES_Campaign_Rules();