'wpleads_lead_tab_direct_email', 'label' => __('Direct Email', 'inbound-pro') ); array_push($tabs, $args); return $tabs; } /** * */ public static function add_direct_email_tab_contents() { global $post, $inbound_settings; /* Enqueue Sweet Alert support */ wp_enqueue_script('sweet-alert-js', INBOUND_EMAIL_URLPATH . 'assets/libraries/SweetAlert/sweet-alert.js'); wp_enqueue_style('sweet-alert-css', INBOUND_EMAIL_URLPATH . 'assets/libraries/SweetAlert/sweet-alert.css'); /*get the email address we're sending to*/ $recipient_email_addr = Leads_Field_Map::get_field($post->ID, 'wpleads_email_address'); /*get all the "automated" emails*/ $email_templates = get_posts(array( 'numberposts' => -1, 'post_status' => 'automated', 'post_type' => 'inbound-email', )); /*get the current user*/ $user = wp_get_current_user(); $default_from_name = (isset($inbound_settings['mailer']['default-from-email'])) ? $inbound_settings['mailer']['default-from-name'] : ''; $user_email = $default_from_name; /*get the mail service settings*/ $email_service = (isset($inbound_settings['mailer']['mail-service'])) ? $inbound_settings['mailer']['mail-service'] : 'wp_mail'; /***setup the sending domains dropdown***/ /*get the available Sparkpost sending domains*/ if( strstr($email_service , 'sparkpost') ){ $sparkpost = new Inbound_SparkPost( $inbound_settings['mailer']['sparkpost-key'] , $inbound_settings['mailer']['mail-service'] ); $domain_query = $sparkpost->get_domains(); /*get the current user's email without the domain*/ $parts = explode('@', $user->data->user_email ); $user_email = $parts[0]; /*if there are no errors*/ if(!isset($domain_query['errors']) && empty($domain_query['errors'])){ if (count($domain_query['results']) <1 ) { $sending_dropdown = ''; } else { $sending_dropdown = ''; } foreach($domain_query as $domains){ foreach($domains as $domain){ /*if the sending domain is owned, or has DKIM or SPF setup*/ if($domain['status']['ownership_verified'] == 1 || $domain['status']['spf_status'] == 'valid' || $domain['status']['dkim_status'] == 'valid'){ /*if the user's email is hosted on a verified sending domain*/ if(substr($user->email, strpos($user->email, '@') +1) == $domain['domain']){ /*set that domain as the selected one for the domain selector dropdown*/ $sending_dropdown .= ''; }else{ $sending_dropdown .= ''; } } } } $sending_dropdown .= ''; echo ''; }else{ /*if there were errors, set the user's email for the one used on the site*/ $user_email = $default_from_name; } } /*put the email ids and names in an array for use in the email dropdown selector*/ $template_id_and_name = array(); foreach ($email_templates as $email_template) { $template_id_and_name[$email_template->ID] = $email_template->post_title; } /*these are the fields in the "Email Lead" tab*/ $custom_fields = array( 'use_premade_template' => array( 'label' => __('Use template?', 'inbound-pro'), 'description' => __('Use this to choose whether to send a custom or a premade email', 'inbound-pro'), 'id' => 'premade_template_chooser', 'type' => 'dropdown', 'default' => '0', 'class' => 'premade_template_chooser', 'options' => array('0' => 'No', '1' => 'Yes'), ), 'subject' => array( 'description' => __('Subject line of the email. This field is variation dependant!', 'inbound-pro'), 'label' => __('Subject', 'inbound-pro'), 'id' => 'subject', 'type' => 'text', 'default' => '', 'class' => 'direct_email_lead_field', ), 'from_name' => array( 'label' => __('From Name', 'inbound-pro'), 'description' => __('The name of the sender. This field is variation dependant!', 'inbound-pro'), 'id' => 'from_name', 'type' => 'text', 'default' => $user->display_name, 'class' => 'direct_email_lead_field', ), 'from_email' => array( 'label' => __('From Email', 'inbound-pro'), 'description' => __('The email address of the sender. This field is variation dependant!', 'inbound-pro'), 'id' => 'from_email', 'type' => 'text', 'default' => $user_email, 'class' => 'direct_email_lead_field', ), 'reply_email' => array( 'label' => __('Reply Email', 'inbound-pro'), 'description' => __('The email address recipients can reply to. This field is variation dependant!', 'inbound-pro'), 'id' => 'reply_email', 'type' => 'text', 'default' => $user->user_email, 'class' => 'direct_email_lead_field', ), 'recipient_email_address' => array( 'label' => __('Recipient', 'inbound-pro'), 'description' => __('The email address of the recipient.', 'inbound-pro'), 'id' => 'recipient_email_address', 'type' => 'text', 'default' => $recipient_email_addr, 'class' => '', ), 'email_message_box' => array( 'label' => __('Message', 'inbound-pro'), 'description' => __('Use this editor to create a short custom email messages', 'inbound-pro'), 'id' => 'email_message_box', 'type' => 'wysiwyg', 'default' => __('Email content goes in here. You may want to send yourself one to see how it looks.', 'inbound-pro'), 'class' => 'email_message_box', 'disable_variants' => '1', ), 'premade_email_templates' => array( 'label' => __('Select Template', 'inbound-pro'), 'description' => __('Use this to select which premade email to use.', 'inbound-pro'), 'id' => 'premade_template_selector', 'type' => 'dropdown', 'default' => '0', 'class' => 'premade_template_selector', 'options' => $template_id_and_name, ), 'email_variation' => array( 'label' => __('Choose varation', 'inbound-pro'), 'description' => __('Use this to select which variation of the premade email to use.', 'inbound-pro'), 'id' => 'email_variation_selector', 'type' => 'dropdown', 'default' => '0', 'class' => 'email_variation_selector', 'options' => array('0' => 'A'), ) ); ?>
__('The lead id was not supplied', 'inbound-pro'), 'title' => __('System Error:', 'inbound-pro'))); die(); } if (empty($_POST['recipient_email']) || !is_email($_POST['recipient_email'])) { echo json_encode(array('basic_error' => __('There\'s an error with the recipient email', 'inbound-pro'), 'title' => __('Field Error:', 'inbound-pro'))); die(); } if (empty($_POST['premade_email_id']) && $_POST['premade_email_id'] != '0') { echo json_encode(array('system_error' => __('The email template id was not supplied', 'inbound-pro'), 'title' => __('System Error:', 'inbound-pro'))); die(); } if (empty($_POST['variation_selected']) && $_POST['variation_selected'] != '0') { echo json_encode(array('system_error' => __('The variation id was not supplied', 'inbound-pro'), 'title' => __('System Error:', 'inbound-pro'))); die(); } $post_id = intval($_POST['post_id']); $recipient_email = sanitize_text_field($_POST['recipient_email']); $premade_email_id = intval($_POST['premade_email_id']); $variation_selected = intval($_POST['variation_selected']); /*sending args*/ $args = array( 'email_address' => $recipient_email, 'email_id' => $premade_email_id, 'vid' => $variation_selected, 'lead_id' => $post_id, 'is_test' => 0, 'tags' => array('direct-email') ); /*send the email!*/ Inbound_Mail_Daemon::send_solo_email($args); echo json_encode(array('success' => __('Your email has been sent!', 'inbound-pro'), 'title' => __('SUCCESS!', 'inbound-pro'))); die(); } /** * */ public static function send_direct_message_to_lead() { /* these are the variables set by the user */ $data = array( 'subject' => array( 'label' => __('Subject', 'inbound-pro'), 'value' => sanitize_text_field($_POST['subject']) ), 'from_name' => array( 'label' => __('From Name', 'inbound-pro'), 'value' => sanitize_text_field($_POST['from_name']) ), 'from_email' => array( 'label' => __('From Email', 'inbound-pro'), 'value' => sanitize_text_field($_POST['from_email']) ), 'reply_email' => array( 'label' => __('Reply Email', 'inbound-pro'), 'value' => sanitize_text_field($_POST['reply_email']) ), 'recipient_email' => array( 'label' => __('Recipeient Email', 'inbound-pro'), 'value' => sanitize_text_field($_POST['recipient_email']) ), 'email_content' => array( 'label' => __('Email Content', 'inbound-pro'), 'value' => stripslashes($_POST['email_content']) ) ); /*check to make sure the variables are set*/ foreach ($data as $key => $value) { if (empty($value['value'])) { echo json_encode(array('basic_error' => __('Please fill in the ' . $value['label'], 'inbound-pro'), 'title' => __('Empty field', 'inbound-pro'))); die(); } } /* sanitise hidden values */ $data['post_id'] = (int) $_POST['post_id']; $data['lead_id'] = (int) $_POST['post_id']; $data['user_id'] = (int) $_POST['user_id']; /*check to make sure the email addresses are setup correctly*/ if (!is_email($data['recipient_email']['value'])) { echo json_encode(array('basic_error' => __('There\'s an error with the Recipient Email Address', 'inbound-pro'), 'title' => __('Field Error:', 'inbound-pro'))); die(); } if (!is_email($data['from_email']['value'])) { echo json_encode(array('basic_error' => __('There\'s an error with the From Email', 'inbound-pro'), 'title' => __('Field Error:', 'inbound-pro'))); die(); } if (!is_email($data['reply_email']['value']) && !strstr($data['reply_email']['value'] , '[') && !strstr($data['reply_email']['value'] ,'%') ) { echo json_encode(array('basic_error' => __('There\'s an error with the Reply Email', 'inbound-pro'), 'title' => __('Field Error:', 'inbound-pro'))); die(); } /*check to make sure the post and user ids have been supplied*/ if (empty($data['post_id'])) { echo json_encode(array('system_error' => __('The post id was not supplied', 'inbound-pro'), 'title' => __('System Error:', 'inbound-pro'))); die(); } if (empty($data['user_id'])) { echo json_encode(array('system_error' => __('The user id was not supplied', 'inbound-pro'), 'title' => __('System Error:', 'inbound-pro'))); die(); } /*get the current time according to the wp format*/ $timezone = get_option('timezone_string' , 'EST'); $timezone = ($timezone) ? $timezone : 'EST'; $time = new DateTime('', new DateTimeZone($timezone)); $format = get_option('date_format') . ' \a\t ' . get_option('time_format'); /* sanitize email content */ $data['email_content']['value'] = strip_tags( $data['email_content']['value'],'

  1. '); /* prepare content with tracked links */ $data['email_content']['value'] = self::rebuild_links( $data['email_content']['value'] , $data ); /*assemble the post data*/ $direct_email = array( 'post_title' => __('Direct email to ', 'inbound-pro') . $data['recipient_email']['value'] . __(' on ', 'inbound-pro') . $time->format($format), 'post_content' => wpautop($data['email_content']['value']), 'post_status' => 'direct_email', 'post_author' => $data['user_id'], 'post_type' => 'inbound-email', ); /*create the email*/ $data['direct_email_id'] = wp_insert_post($direct_email); /* assemble the settings mailer uses for sending the email */ $mailer_settings = array( 'variations' => array( 0 => array( 'selected_template' => '', 'user_ID' => $data['user_id'], 'subject' => $data['subject']['value'], 'from_name' => $data['from_name']['value'], 'from_email' => $data['from_email']['value'], 'reply_email' => $data['reply_email']['value'], 'variation_status' => 'active', ), ), 'email_type' => 'direct', ); /*add the settings to the email*/ Inbound_Email_Meta::update_settings($data['direct_email_id'], $mailer_settings); /*sending args*/ $args = array( 'email_address' => $data['recipient_email']['value'], 'email_id' => $data['direct_email_id'], 'vid' => 0, 'lead_id' => $data['post_id'], 'is_test' => 0, 'is_direct' => true, 'tags' => array('direct-email') ); /*and send*/ $response = Inbound_Mail_Daemon::send_solo_email($args); $repsonse = wp_remote_retrieve_body( $response ); if (isset($response['errors'])) { wp_delete_post($data['direct_email_id'], true); echo json_encode($response); } else { echo json_encode(array('success' => __('Your custom email has been sent!', 'inbound-pro'), 'title' => __('SUCCESS!', 'inbound-pro'))); $data['response'] = $response; self::store_direct_mail_event( $data ); } // echo json_encode(error_get_last()); //debug die(); } /** * @param $data */ public static function store_direct_mail_event( $data ) { /* recipients */ $args = array( 'event_name' => 'inbound_direct_message', 'email_id' => $data['direct_email_id'], 'variation_id' => 0, 'form_id' => 0, 'lead_id' => $data['post_id'], 'event_details' => json_encode($data) ); Inbound_Events::store_event($args); } /** * Adds Inbound Form Submissions to Quick Stat Box */ public static function display_quick_stat_direct_messages($post) { global $post; if (!isset($_REQUEST['range'])) { self::$range = 1095; } else { self::$range = intval($_REQUEST['range']); } /* load this early */ self::$event_names = self::get_event_names(); ?> $post->ID, 'event_name' => self::$event_names['click'] ); $click_events = Inbound_Events::get_events($params); ?> $post->ID, 'event_name' => self::$event_names['open'] ); $opens = Inbound_Events::get_events($params); ?> $post->ID, 'event_name' => 'sparkpost_bounce' ); $bounces = Inbound_Events::get_events($params); ?> 'sparkpost_delivery', 'open' => 'sparkpost_open', 'click' => 'sparkpost_click' ); break; case "sparkpost-eu": return array( 'delivery' => 'sparkpost_delivery', 'open' => 'sparkpost_open', 'click' => 'sparkpost_click' ); break; case "wp_mail": return array( 'delivery' => 'wpmail_delivery', 'open' => 'wpmail_open', 'click' => 'wpmail_click' ); break; } } /** * Gets number of direct mail messages sent to lead */ public static function get_email_send_count( $lead_id ){ global $wpdb; $table_name = $wpdb->prefix . "inbound_events"; $query = 'SELECT count(*) FROM '.$table_name.' WHERE `lead_id` = "'.$lead_id.'"'; $query .= 'AND `event_name` = "'.self::$event_names['delivery'].'"'; $count = $wpdb->get_var( $query , 0, 0 ); /* return null if nothing there */ return ($count) ? $count : 0; } /** * Gets number of direct mail messages sent to lead */ public static function get_direct_mail_count( $lead_id ){ global $wpdb; $table_name = $wpdb->prefix . "inbound_events"; $query = 'SELECT count(*) FROM '.$table_name.' WHERE `lead_id` = "'.$lead_id.'"'; $query .= 'AND `event_name` = "inbound_direct_message"'; $count = $wpdb->get_var( $query , 0, 0 ); /* return null if nothing there */ return ($count) ? $count : 0; } /** * Gets number of direct mail messages sent to lead */ public static function get_unsubscribes_count( $lead_id ){ global $wpdb; $table_name = $wpdb->prefix . "inbound_events"; $query = 'SELECT count(*) FROM '.$table_name.' WHERE `lead_id` = "'.$lead_id.'"'; $query .= 'AND `event_name` = "inbound_unsubscribe"'; $count = $wpdb->get_var( $query , 0, 0 ); /* return null if nothing there */ return ($count) ? $count : 0; } /** * Rebuild links with tracking params */ public static function rebuild_links($html , $data ) { preg_match_all('/href="([^\s"]+)/', $html, $links); if (!$links) { return $html; } /* Iterate over the extracted links and display their URLs */ foreach ($links[1] as $link) { /* Do not modify unsubscribe links or non links */ if (strstr($link, '?token=') || !strstr($link, '://')) { continue; } $safe_link = Inbound_API::analytics_track_links(array( 'email_id' => '', 'lead_lists' => '', 'id' => $data['lead_id'], 'lead_id' => $data['lead_id'], 'page_id' => $data['lead_id'], 'vid' =>0, 'url' => $link, 'utm_source' => urlencode($data['subject']['value']), 'utm_medium' => 'email', 'utm_campaign' => '', 'tracking_id' => urlencode($data['subject']['value']) )); $html = str_replace("'" . $link . "'", "'" . $safe_link['url'] . "'", $html); $html = str_replace('"' . $link . '"', '"' . $safe_link['url'] . '"', $html); } return $html; } } add_action('admin_init', 'inbound_confirm_email_service_provider'); /** * Only load Inbound_Mailer_Direct_Email_Leads if an email service provider has been selected */ function inbound_confirm_email_service_provider() { $email_settings = Inbound_Mailer_Settings::get_settings(); $email_service = (isset($email_settings['mail-service'])) ? $email_settings['mail-service'] : 'wp_mail'; if (isset($email_service) && $email_service != 'none' ) { new Inbound_Mailer_Direct_Email_Leads; } }