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: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227:
<?php
if ( !class_exists( 'Inbound_Automation_Action_Send_Email' ) ) {
class Inbound_Automation_Action_Send_Email {
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();
if ( class_exists('Inbound_Mailer_Post_Type') ) {
$emails = Inbound_Mailer_Post_Type::get_automation_emails_as( 'ARRAY' );
if (!$emails) {
$emails[] = __( 'No Automation emails detected. Please create an automated email first.' , 'inbound-pro' );
}
} else {
$emails = array('email component not installed.');
}
$actions['send_email'] = array (
'class_name' => get_class(),
'id' => 'send_email',
'label' => 'Send Email',
'description' => __('Send an email using available filter data.', 'inbound-pro'),
'settings' => array (
array (
'id' => 'send_to',
'label' => __( 'Send Email To' , 'inbound-pro' ),
'description' => __('Choose where to send the email', 'inbound-pro'),
'type' => 'dropdown',
'options' => array(
'lead' => __( 'Lead' , 'inbound-pro' ),
'custom' => __( 'Custom Email Address' , 'inbound-pro' ),
'lead_list' => __( 'Lead List' , 'inbound-pro' ),
)
),
array (
'id' => 'custom_email',
'label' => __( 'Enter Email Address' , 'inbound-pro' ),
'description' => __('Input email address here.', 'inbound-pro'),
'type' => 'text',
'hidden' => true,
'reveal' => array(
'selector' => 'send_to',
'value' => 'custom'
)
),
array (
'id' => 'lead_lists',
'label' => __( 'Select Lead List' , 'inbound-pro' ),
'description' => __('Select the lead list to send an email to. This action could take awhile for large lists. ', 'inbound-pro'),
'type' => 'select2',
'hidden' => true,
'reveal' => array(
'selector' => 'send_to',
'value' => 'lead_list'
),
'options' => $lead_lists
),
array (
'id' => 'email_id',
'label' => __( 'Select Email' , 'inbound-pro' ),
'description' => __('Select the Email to send to. ', 'inbound-pro'),
'type' => 'dropdown',
'options' => $emails
)
)
);
return $actions;
}
public static function run_action( $action , $trigger_data ) {
$Inbound_Templating_Engine = Inbound_Templating_Engine();
$trigger_data = apply_filters( 'action/send_email/trigger_data' , $trigger_data );
if (isset($trigger_data['post_object']['ID']) && $trigger_data['post_object']['ID']) {
if (get_post_meta($trigger_data['post_object']['ID'] , 'inbound_automation_email_sent' , true )) {
inbound_record_log(
__( 'Email Blocked - Post Locked' , 'inbound-pro') ,
'<h2>'.__('Details', 'inbound-pro') .'</h2><pre>'.__( 'It seems this email has been manually locked or has already been sent via an automation rule once.' , 'inbound-pro' ).'</pre>' .
'<h2>'.__('Action Settings' , 'inbound-pro') .'</h2><pre>'. print_r($action,true).'</pre><h2>'.__('Action Settings' , 'inbound-pro') .'</h2><pre>'.print_r($trigger_data,true) .'</pre>',
$action['rule_id'] ,
$action['job_id'] ,
'action_event'
);
return;
}
}
switch ($action['send_to']) {
case 'lead':
$Inbound_Mail_Daemon = new Inbound_Mail_Daemon();
$vid = Inbound_Mailer_Variations::get_next_variant_marker( $action['email_id'] );
$lead_lists = (isset($trigget_data['lead_data']['lead_lists'])) ? $trigget_data['lead_data']['lead_lists'] : array();
$trigger_data['lead_data']['email'] = (isset($trigger_data['lead_data']['email'])) ? $trigger_data['lead_data']['email'] : '' ;
$trigger_data['lead_data']['email'] = (isset($trigger_data['lead_data']['wpleads_email_address'])) ? $trigger_data['lead_data']['wpleads_email_address'] : $trigger_data['lead_data']['email'] ;
if ( isset($trigger_data['user_id']) ) {
$user = new WP_User( $trigger_data['user_id'] );
$trigger_data['lead_data']['email'] = $user->data->user_email;
$trigger_data['lead_data']['id'] = LeadStorage::lookup_lead_by_email($trigger_data['lead_data']['email']);
if (!$trigger_data['lead_data']['id']) {
$args = array(
'user_ID' => $trigger_data['user_id'],
'wpleads_email_address' => $trigger_data['lead_data']['email'],
'wpleads_first_name' => $user->data->display_name
);
$trigger_data['lead_data']['id'] = inbound_store_lead( $args );
}
}
$toggle = get_post_meta( $trigger_data['lead_data']['id'] , 'inbound_automation_mute' , true);
if ($toggle) {
return;
}
$response = $Inbound_Mail_Daemon->send_solo_email( array(
'email_address' => $trigger_data['lead_data']['email'],
'lead_id' => $trigger_data['lead_data']['id'],
'email_id' => $action['email_id'],
'tags' => array( 'automated' ),
'vid' => $vid,
'lead_lists' => $lead_lists,
'rule_id' => $action['rule_id'],
'job_id' => $action['job_id'],
));
BREAK;
case 'custom':
$Inbound_Mail_Daemon = new Inbound_Mail_Daemon();
$vid = Inbound_Mailer_Variations::get_next_variant_marker( $action['email_id'] );
$response = $Inbound_Mail_Daemon->send_solo_email( array(
'email_address' => $action['custom_email'],
'email_id' => $action['email_id'],
'vid' => $vid,
'lead_id' => isset($trigger_data['lead_data']['id']) ? $trigger_data['lead_data']['id'] : 0 ,
'tags' => array('automated'),
'rule_id' => $action['rule_id'],
'job_id' => $action['job_id'],
));
BREAK;
case 'lead_list':
$Inbound_Mailer_Scheduling = new Inbound_Mailer_Scheduling;
$response = $Inbound_Mailer_Scheduling->schedule_email( $action['email_id'] , $trigger_data , $action , $action['lead_lists'] );
$response = __( sprintf( '%s emails have been scheduled' , $response ) , 'inbound-pro' );
BREAK;
}
if (isset($trigger_data['post_object']['ID']) && $trigger_data['post_object']['ID']) {
update_post_meta($trigger_data['post_object']['ID'] , 'inbound_automation_email_sent' , true );
}
inbound_record_log(
__( 'Send Email' , 'inbound-pro') ,
'<h2>'.__('Email Server Response', 'inbound-pro') .'</h2><pre>'.print_r($response,true).'</pre>' .
'<h2>'.__('Action Settings' , 'inbound-pro') .'</h2><pre>'. print_r($action,true).'</pre><h2>'.__('Action Settings' , 'inbound-pro') .'</h2><pre>'.print_r($trigger_data,true) .'</pre>',
$action['rule_id'] ,
$action['job_id'] ,
'action_event'
);
}
}
new Inbound_Automation_Action_Send_Email();
}