1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168:
<?php
if ( !class_exists( 'Inbound_Automation_Action_Add_Remove_List' ) ) {
class Inbound_Automation_Action_Add_Remove_List {
function __construct() {
add_filter( 'inbound_automation_actions' , array( __CLASS__ , 'define_action' ) , 1 , 1);
}
public static function define_action( $actions ) {
$lead_lists = Inbound_Leads::get_lead_lists_as_array();
$actions['add_remove_lead_list'] = array (
'class_name' => get_class(),
'id' => 'add_remove_lead_list',
'label' => __( 'Add/Remove From List' , 'inbound-pro' ),
'description' => __( 'Action to add or remove a lead from a lead list.' , 'inbound-pro' ),
'settings' => array (
array (
'id' => 'remove_from_lead_lists',
'label' => __( 'Remove from these lists:' , 'inbound-pro' ),
'description' => __( 'Remove lead from lists in this input.' , 'inbound-pro' ),
'type' => 'select2',
'hidden' => false,
'options' => $lead_lists
),
array (
'id' => 'add_to_lead_lists',
'label' => __( 'Add to these lists:' , 'inbound-pro' ),
'description' => __( 'Add lead to the lists in this input.' , 'inbound-pro' ),
'type' => 'select2',
'hidden' => false,
'options' => $lead_lists
),
array (
'id' => 'message',
'label' => '',
'type' => 'html',
'hidden' => false,
'description' => __( 'Notice to admin' , 'inbound-pro' ),
'default' => '<i>' . __( 'If a user has unsubscribed from a list then automation will not resubscribe them. They will have to fill out an inbound form adding them to the list or be re-added manually by an administrator.' , 'inbound-pro' ) . '</i>'
)
)
);
return $actions;
}
public static function run_action( $action , $arguments ) {
$added = array();
$removed = array();
$skipped = array();
$final_args = array();
foreach ($arguments as $key => $argument) {
if (is_array($argument) ) {
foreach ($argument as $k => $value ) {
$final_args[ $k ] = $value;
}
} else {
$final_args[ $key ] = $argument;
}
}
if ( !isset($final_args['id']) && isset($final_args['user_id']) ) {
$user = new WP_User($final_args['user_id']);
$final_args['id'] = LeadStorage::lookup_lead_by_email($user->data->user_email);
}
if ( empty($final_args['id']) ) {
inbound_record_log(
__( 'List Change - Fail' , 'inbound-pro') ,
'<h2>'.__('Failed - bad lead id', 'inbound-pro') .'</h2><pre>'.print_r(arguments,true).'</pre>' .
$action['rule_id'] ,
$action['job_id'] ,
'action_event'
);
return;
}
if ( isset($action['add_to_lead_lists']) && is_array($action['add_to_lead_lists']) ) {
$stop_rules = Inbound_Mailer_Unsubscribe::get_stop_rules( $final_args['id'] );
foreach ( $action['add_to_lead_lists'] as $list_id ) {
if ( isset($stop_rules[$list_id]) && ( $stop_rules[$list_id] == 'unsubscribed' || $stop_rules[$list_id] === true ) ) {
$skipped[] = $list_id;
continue;
}
Inbound_Leads::add_lead_to_list( $final_args['id'] , intval($list_id) );
$added[] = $list_id;
}
}
if ( isset($action['remove_from_lead_lists']) && is_array($action['remove_from_lead_lists']) ) {
foreach ( $action['remove_from_lead_lists'] as $list_id ) {
Inbound_Leads::remove_lead_from_list( $final_args['id'] , intval($list_id) );
$removed[] = $list_id;
}
}
inbound_record_log(
__( 'List Change' , 'inbound-pro') ,
'<h2>'.__('Addded to', 'inbound-pro') .'</h2><pre>'.print_r($added,true).'</pre>' .
'<h2>'.__('Removed from' , 'inbound-pro') .'</h2><pre>'. print_r($removed,true).'</pre>'.
'<h2>'.__('Skipped due to prior unsubscribe' , 'inbound-pro') .'</h2><pre>'. print_r($skipped,true).'</pre>',
$action['rule_id'] ,
$action['job_id'] ,
'action_event'
);
}
}
new Inbound_Automation_Action_Add_Remove_List();
}