"; return $html; } /** * Compatibility with older WP version * get_usermeta (deprecated from 3.0) */ if ( !function_exists('get_user_meta') ) { function get_user_meta ( $user, $key, $single=false ) { return get_usermeta ( $user, $key ); } } /** * Sort a multidimensional array on a array kay (found on http://php.net/manual/en/function.sort.php) * @array array the array * @key str the field to use as key to sort * @order str sort method: "ASC", "DESC" */ function alo_em_msort ($array, $key, $order = "ASC") { $tmp = array(); foreach($array as $akey => $array2) { $tmp[$akey] = $array2[$key]; } if ($order == "DESC") { arsort($tmp , SORT_NUMERIC ); } else { asort($tmp , SORT_NUMERIC ); } $tmp2 = array(); foreach($tmp as $key => $value) { $tmp2[$key] = $array[$key]; } return $tmp2; } /** * Remove HTML tags, including invisible text such as style and * script code, and embedded objects. Add line breaks around * block-level tags to prevent word joining after tag removal. * (based on http://nadeausoftware.com/articles/2007/09/php_tip_how_strip_html_tags_web_page ) */ function alo_em_html2plain ( $text ) { // transform in utf-8 if not yet if ( mb_detect_encoding($text, "UTF-8") != "UTF-8" ) $text = utf8_encode($text); $text = preg_replace( array( // Remove invisible content '@
]*?>.*?@siu', '@@siu', '@@siu', '@@siu', '@@siu', '@@siu', '@". print_r ( $recipients, true ). ""; $cache = array(); if ( isset( $recipients['registered'] ) && $recipients['registered'] == 1 ) $cache['registered'] = "0"; if ( isset( $recipients['subscribers'] ) && $recipients['subscribers'] == 1 ) { $cache['subscribers'] = "0"; } else { if ( isset( $recipients['list'] ) ) { $cache['list'] = array(); foreach ( $recipients['list'] as $index => $id ) { $cache['list'][$id] = "0"; } } } if ( isset( $recipients['lang'] ) ) $cache['lang'] = $recipients['lang']; delete_post_meta ( $newsletter, "_easymail_cache_recipients" ); add_post_meta ( $newsletter, "_easymail_cache_recipients", $cache ); } /** * Get the Recipients cache for Newsletter * @return arr */ function alo_em_get_cache_recipients ( $newsletter ) { $recipients = get_post_meta ( $newsletter, "_easymail_cache_recipients" ); return ( !empty( $recipients[0] ) ) ? $recipients[0] : false; } /** * Save the Recipients cache for Newsletter */ function alo_em_save_cache_recipients ( $newsletter, $recipients ) { delete_post_meta ( $newsletter, "_easymail_cache_recipients" ); add_post_meta ( $newsletter, "_easymail_cache_recipients", $recipients ); } /** * Delete the Recipients cache for Newsletter */ function alo_em_delete_cache_recipients ( $newsletter ) { delete_post_meta ( $newsletter, "_easymail_cache_recipients" ); } /** * Get the Recipients cache for Newsletter * @param int limit: how many * @param bol if send now or add to queue */ function alo_em_add_recipients_from_cache_to_db ( $newsletter, $limit=10, $sendnow=false ) { $cache = alo_em_get_cache_recipients( $newsletter ); //echo "CACHE BEFORE\n
". print_r ( $cache, true ). ""; // DEBUG if ( $cache && is_array( $cache ) ) { //$recipients = array(); $start = 0; $now_doing = false; $finished = false; // Get the 1st required group if ( isset( $cache['registered'] ) ) { $recipients = alo_em_get_recipients_registered(); $now_doing = "registered"; $start = $cache['registered']; } if ( isset( $cache['subscribers'] ) && !$now_doing ) { $recipients = alo_em_get_recipients_subscribers(); $now_doing = "subscribers"; $start = $cache['subscribers']; } else if ( isset( $cache['list'] ) && !$now_doing ) { $lists = array(); foreach ( $cache['list'] as $id => $list_start ) { $recipients = alo_em_get_recipients_subscribers( $id ); $now_doing = "list"; $now_doing_list = $id; $start = $list_start; break; // the 1st list } //$recipients = alo_em_get_recipients_subscribers( $lists ); } // If not registered round, check languages if ( $now_doing && $now_doing != "registered" && isset ( $cache['lang'] ) && is_array( $cache['lang'] ) ) { foreach ( $recipients as $index => $rec ) { /* $search_lang = ( !empty( $rec->lang ) ) ? $rec->lang : "UNKNOWN"; // if subscriber has not specified lang if ( !in_array( $search_lang, $cache['lang'] ) ) unset ( $recipients[$index] ); */ $rec_lang = ( !empty( $rec->lang ) ) ? $rec->lang : "UNKNOWN"; // if subscriber has not specified lang if ( $rec_lang == "UNKNOWN" || !in_array( $rec_lang, alo_em_get_all_languages() ) ) { // unknown or not installed lang if ( !in_array( "UNKNOWN", $cache['lang'] ) ) unset ( $recipients[$index] ); } else { // installed lang if ( !in_array( $rec_lang, $cache['lang'] ) ) unset ( $recipients[$index] ); } } } //echo "RECIPIENTS\n
". print_r ( $recipients, true ). ""; // DEBUG if ( $now_doing && $recipients ) { $added = 0; // to count how many added in this round end( $recipients ); $end = key ( $recipients ); // the last index in recipients reset( $recipients ); for ( $i = $start; $i <= $end; $i ++ ) { if ( $i == $end ) $finished = $now_doing; // // if end reached, group finished if ( !isset( $recipients[$i] ) ) { // if ( $i == count( $recipients )-1 ) break; else continue; continue; } $email = ( $now_doing == "registered" ) ? $recipients[$i]->user_email : $recipients[$i]->email; if ( alo_em_get_recipient_by_email_and_newsletter( $email, $newsletter ) ) continue; // if already added, skip $args = array( 'newsletter' => $newsletter, 'email' => $email ); $new_id = alo_em_add_recipient( $args, true ); if ( $new_id ) { $added ++; if ( $sendnow ) { // send only one mail (and wait the the request sleep) and exit /* $recipient = alo_em_get_subscriber( $email ); $recipient->subscriber = $recipient->ID; $recipient->ID = $new_id; $recipient->newsletter = $newsletter; */ //$recipient = (object) array ( 'newsletter' => $newsletter, 'email' => $email, 'ID' => $new_id ); $recipient = alo_em_get_recipient_by_id( $new_id ); alo_em_send_newsletter_to ( $recipient ); if ( alo_em_get_sleepvalue() > 0 ) usleep ( alo_em_get_sleepvalue() * 1000 ); break; } if ( $added >= $limit ) { // if limit reached, exit break; } } // Update the offset for next switch ( $now_doing ) { case "registered": $cache['registered'] = $i; break; case "subscribers": $cache['subscribers'] = $i; break; case "list": $cache['list'][$now_doing_list] = $i; break; } //echo "NOW $i\n
". print_r ( $cache, true ). ""; // DEBUG } } // If group finished, delete it from cache if ( $finished ) : switch ( $finished ) : case "registered": unset ( $cache['registered'] ); break; case "subscribers": unset ( $cache['subscribers'] ); break; case "list": unset ( $cache['list'][$now_doing_list] ); break; endswitch; endif; //echo "NEW CACHE $i\n
". print_r ( $cache, true ). ""; // DEBUG // If completed ALL groups, delete cache and mark newsletter as "sendable" if ( !isset( $cache['registered'] ) && !isset( $cache['subscribers'] ) && empty( $cache['list'] ) ) { if ( count( alo_em_get_recipients_in_queue( 1, $newsletter ) ) == 0 && $sendnow ) { alo_em_set_newsletter_as_completed ( $newsletter ); } else { alo_em_edit_newsletter_status ( $newsletter, 'sendable' ); } alo_em_delete_cache_recipients( $newsletter ); } else { alo_em_save_cache_recipients ( $newsletter, $cache ); } } } /** * Get single Recipient by email and newsletter */ function alo_em_get_recipient_by_email_and_newsletter ( $email, $newsletter ) { global $wpdb; //return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}easymail_recipients WHERE email=%s AND newsletter=%d", $email, $newsletter ) ); $rec = $wpdb->get_row( $wpdb->prepare( "SELECT r.*, s.lang, s.unikey, s.name, s.ID AS subscriber FROM {$wpdb->prefix}easymail_recipients AS r LEFT JOIN {$wpdb->prefix}easymail_subscribers AS s ON r.email = s.email WHERE r.email=%s AND r.newsletter=%d", $email, $newsletter ) ); if ( $rec ) { if ( $user_id = email_exists( $email ) ) { $rec->user_id = $user_id; if ( get_user_meta( $user_id, 'first_name', true ) != "" ) $rec->firstname = ucfirst( get_user_meta( $user_id, 'first_name', true ) ); } else { $rec->firstname = $rec->name; $rec->user_id = false; } } return $rec; } /** * Get single Recipient by ID */ function alo_em_get_recipient_by_id ( $recipient ) { global $wpdb; $rec = $wpdb->get_row( $wpdb->prepare( "SELECT r.*, s.lang, s.unikey, s.name, s.ID AS subscriber FROM {$wpdb->prefix}easymail_recipients AS r LEFT JOIN {$wpdb->prefix}easymail_subscribers AS s ON r.email = s.email WHERE r.ID=%d", $recipient ) ); if ( $rec && isset( $rec->email ) ) { if ( $user_id = email_exists( $rec->email ) ) { $rec->user_id = $user_id; if ( get_user_meta( $user_id, 'first_name', true ) != "" ) $rec->firstname = ucfirst( get_user_meta( $user_id, 'first_name', true ) ); } else { $rec->firstname = $rec->name; $rec->user_id = false; } } return $rec; } /** * Add Recipient by email and newsletter *@param arr recipient info: email. newsletter... *@param bol add only if subscriber is active *@return int|bol id added of false */ function alo_em_add_recipient ( $args, $only_if_active=true ) { global $wpdb; $defaults = array( 'email' => false, 'newsletter' => false, 'result' => '0' ); $fields = wp_parse_args( $args, $defaults ); $added = false; if ( $fields['email'] && $fields['newsletter'] ) { if ( !$only_if_active || ( $only_if_active && ( alo_em_check_subscriber_state( $fields['email'] ) == 1 /*|| email_exists( $fields['email'] )*/ ) ) ) { $wpdb->insert ( "{$wpdb->prefix}easymail_recipients", $fields ); $added = $wpdb->insert_id; } } return $added; } /** * Delete Newsletter Recipients */ function alo_em_delete_newsletter_recipients ( $newsletter ) { global $wpdb; $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}easymail_recipients WHERE newsletter=%d", $newsletter ) ); $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}easymail_stats WHERE newsletter=%d", $newsletter ) ); } /** * Get Newsletter Recipients from db * @param bol if only recipients that have NOT yet received the newsletter */ function alo_em_get_newsletter_recipients ( $newsletter, $only_to_send=false ) { global $wpdb; $where_to_send = ( $only_to_send ) ? "AND r.result = 0" : ""; return $wpdb->get_results( $wpdb->prepare( "SELECT r.*, s.lang, s.unikey, s.name FROM {$wpdb->prefix}easymail_recipients AS r LEFT JOIN {$wpdb->prefix}easymail_subscribers AS s ON r.email = s.email WHERE newsletter=%d ". $where_to_send ." ORDER BY r.email ASC", $newsletter ) ); } /** * Count the Newsletter Recipients from db * @return int */ function alo_em_count_newsletter_recipients ( $newsletter, $only_to_send=false ) { return count( alo_em_get_newsletter_recipients ( $newsletter, $only_to_send ) ); } /** * Count the Newsletter Recipients from db already sent * @return int */ function alo_em_count_newsletter_recipients_already_sent ( $newsletter ) { global $wpdb; $sent = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM {$wpdb->prefix}easymail_recipients WHERE newsletter=%d AND result != 0 ", $newsletter ) ); return count( $sent ); } /** * Count the Newsletter Recipients from db already sent with Success * @return int */ function alo_em_count_newsletter_recipients_already_sent_with_success ( $newsletter ) { global $wpdb; $sent = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM {$wpdb->prefix}easymail_recipients WHERE newsletter=%d AND result = '1' ", $newsletter ) ); return count( $sent ); } /** * Count the Newsletter Recipients from db already sent with Error * @return int */ function alo_em_count_newsletter_recipients_already_sent_with_error ( $newsletter ) { global $wpdb; $sent = $wpdb->get_results( $wpdb->prepare( "SELECT ID FROM {$wpdb->prefix}easymail_recipients WHERE newsletter=%d AND result = '-1' ", $newsletter ) ); return count( $sent ); } /** * Count the Newsletter Recipients from db already sent * @return int */ function alo_em_newsletter_recipients_percentuage_already_sent ( $newsletter ) { $sent = alo_em_count_newsletter_recipients_already_sent ( $newsletter ); $total = alo_em_count_newsletter_recipients ( $newsletter ); $perc = ( $sent > 0 && $total > 0 ) ? number_format ( ( $sent * 100 / $total ), 1 ) : 0; return $perc; } /************************************************************************* * SUBSCRIPTION FUNCTIONS *************************************************************************/ /** * Count the n° of subscribers * return a array: total (active + not active), active, not active */ function alo_em_count_subscribers () { global $wpdb; $search = $wpdb->get_results( "SELECT active, COUNT(active) AS count FROM {$wpdb->prefix}easymail_subscribers GROUP BY active ORDER BY active ASC" ); $total = $noactive = $active = false; if ($search) { foreach ($search as $s) { switch ($s->active) { case 0: $noactive = $s->count; break; case 1: $active = $s->count; break; } } $total = $noactive + $active; } return array ( $total, $active, $noactive ); } /** * Check is there is already a subscriber with that email and return ID subscriber */ function alo_em_is_subscriber($email) { global $wpdb; $is_subscriber = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM {$wpdb->prefix}easymail_subscribers WHERE email='%s' LIMIT 1", $email) ); return (($is_subscriber)? $is_subscriber : 0); // ID in db tab subscribers } /** * Check is there is a subscriber with this ID and return true/false */ function alo_em_is_subscriber_by_id ( $id ) { global $wpdb; $is_subscriber = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM {$wpdb->prefix}easymail_subscribers WHERE ID=%d LIMIT 1", $id) ); return $is_subscriber; } /** * Check the state of a subscriber (active/not-active) */ function alo_em_check_subscriber_state($email) { global $wpdb; $is_activated = $wpdb->get_var( $wpdb->prepare("SELECT active FROM {$wpdb->prefix}easymail_subscribers WHERE email='%s' LIMIT 1", $email) ); return $is_activated; } /** * Modify the state of a subscriber (active/not-active) (BY ADMIN) */ function alo_em_edit_subscriber_state_by_id($id, $newstate) { global $wpdb; $output = $wpdb->update( "{$wpdb->prefix}easymail_subscribers", array ( 'active' => $newstate ), array ( 'ID' => $id) ); return $output; } /** * Modify the state of a subscriber (active/not-active) (BY SUBSCRIBER) */ function alo_em_edit_subscriber_state_by_email($email, $newstate="1", $unikey) { global $wpdb; $output = $wpdb->update( "{$wpdb->prefix}easymail_subscribers", array ( 'active' => $newstate ), array ( 'email' => $email, 'unikey' => $unikey ) ); return $output; } /** * Add a new subscriber * return bol/str: * false = generic error * "OK" = success * "NO-ALREADYACTIVATED" = not added because: email is already added and activated * "NO-ALREADYADDED" = not added because: email is already added but not activated; so send activation msg again */ function alo_em_add_subscriber($email, $name, $newstate=0, $lang="" ) { global $wpdb; $output = true; // if there is NOT a subscriber with this email address: add new subscriber and send activation email if (alo_em_is_subscriber($email) == false){ $unikey = substr(md5(uniqid(rand(), true)), 0,24); // a personal key to manage the subscription // try to send activation mail, otherwise will not add subscriber if ($newstate == 0) { $lang_actmail = ( !empty( $lang ) ) ? $lang : alo_em_short_langcode ( get_locale() ); if ( !alo_em_send_activation_email($email, $name, $unikey, $lang_actmail) ) $output = false; // DEBUG ON LOCALHOST: comment this line to avoid error on sending mail } if ( $output ) { $wpdb->insert ( "{$wpdb->prefix}easymail_subscribers", array( 'email' => $email, 'name' => $name, 'join_date' => get_date_from_gmt( date("Y-m-d H:i:s") ), 'active' => $newstate, 'unikey' => $unikey, 'lists' => "|", 'lang' => $lang ) ); $output = "OK"; //return true; } } else { // if there is ALREADY a subscriber with this email address, and if is NOT confirmed yet: re-send an activation email if ( alo_em_check_subscriber_state($email) == 0) { // retrieve existing unique key $exist_unikey = $wpdb->get_var( $wpdb->prepare("SELECT unikey FROM {$wpdb->prefix}easymail_subscribers WHERE ID='%d' LIMIT 1", alo_em_is_subscriber($email) ) ); if ( alo_em_send_activation_email($email, $name, $exist_unikey, $lang) ) { // update join date to today $output = $wpdb->update( "{$wpdb->prefix}easymail_subscribers", array ( 'join_date' => get_date_from_gmt( date("Y-m-d H:i:s") ), 'lang' => $lang ), array ( 'ID' => alo_em_is_subscriber($email) ) ); // tell that there is already added but not active: so it has sent another activation mail....... $output = "NO-ALREADYADDED"; } else { $output = false; //$output = "NO-ALREADYADDED"; // DEBUG ON LOCALHOST: comment the previous line and uncomment this one to avoid error on sending mail } } else { // tell that there is already an activated subscriber..... $output = "NO-ALREADYACTIVATED"; } } return $output; } /** * Delete a subscriber (BY ADMIN/REGISTERED-USER) */ function alo_em_delete_subscriber_by_id($id) { global $wpdb; $output = $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}easymail_subscribers WHERE ID=%d LIMIT 1", $id ) ); return $output; } /** * Update a subscriber (BY ADMIN/REGISTERED-USER) */ function alo_em_update_subscriber_by_email ( $old_email, $new_email, $name, $newstate=0, $lang="" ) { global $wpdb; $output = $wpdb->update( "{$wpdb->prefix}easymail_subscribers", array ( 'email' => $new_email, 'name' => $name, 'active' => $newstate, 'lang' => $lang ), array ( 'email' => $old_email ) ); return $output; } /** * Delete a subscriber (BY SUBSCRIBER) */ function alo_em_delete_subscriber_by_email($email, $unikey) { global $wpdb; $output = $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}easymail_subscribers WHERE email='%s' AND unikey='%s' LIMIT 1", $email, $unikey ) ); return $output; } /** * Check if can access subscription page (BY SUBSCRIBER) */ function alo_em_can_access_subscrpage ($email, $unikey) { global $wpdb; // check if email and unikey match $check = alo_em_check_subscriber_email_and_unikey ( $email, $unikey ); return $check; } /** * Check if subscriber email and unikey match (BY SUBSCRIBER) (check EMAIL<->UNIKEY) */ function alo_em_check_subscriber_email_and_unikey ( $email, $unikey ) { global $wpdb; $check = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM {$wpdb->prefix}easymail_subscribers WHERE email='%s' AND unikey='%s' LIMIT 1", $email, $unikey) ); return $check; } /** * Send email with activation link */ function alo_em_send_activation_email($email, $name, $unikey, $lang) { $blogname = html_entity_decode ( wp_kses_decode_entities ( get_option('blogname') ) ); // Headers $mail_sender = "noreply@". str_replace("www.","", $_SERVER['HTTP_HOST']); $headers = "";//"MIME-Version: 1.0\n"; $headers .= "From: ". $blogname ." <".$mail_sender.">\n"; $headers .= "Content-Type: text/plain; charset=\"". get_bloginfo('charset') . "\"\n"; /* // Subject // $subject = sprintf(__("Confirm your subscription to %s Newsletter", "alo-easymail"), $blogname ); $subject = alo_em_translate_option ( $lang, 'alo_em_txtpre_activationmail_subj', true ); $subject = str_replace ( "%BLOGNAME%", $blogname, $subject ); */ // Main content /* $div_email = explode("@", $email); // for link $arr_params = array ('ac' => 'activate', 'em1' => $div_email[0], 'em2' => $div_email[1], 'uk' => $unikey, 'lang' => $lang); $sub_link = add_query_arg( $arr_params, get_page_link (get_option('alo_em_subsc_page')) ); //$sub_link = alo_em_translate_url ( $sub_link, $lang ); */ /* $content = alo_em_translate_option ( $lang, 'alo_em_txtpre_activationmail_mail', true ); $content = str_replace ( "%BLOGNAME%", $blogname, $content ); $content = str_replace ( "%NAME%", $name, $content ); $content = str_replace ( "%ACTIVATIONLINK%", $sub_link, $content ); */ $content = "lang=$lang&email=$email&name=$name&unikey=$unikey"; //$content = "email=$email"; //echo "
". __("To read the newsletter online you can visit this link", "alo-easymail"); $content .= ": %NEWSLETTERLINK%
\r\n"; } $content = str_replace ( "%NEWSLETTERLINK%", " ". $viewonline_url ."", $content ); */ // Unsubscribe link and tracking, only if subscriber //if ( $recipient->unikey && $recipient->ID ) { /* $div_email = explode( "@", $recipient->email ); // for link $arr_params = array ('ac' => 'unsubscribe', 'em1' => $div_email[0], 'em2' => $div_email[1], 'uk' => $recipient->unikey ); $uns_link = add_query_arg( $arr_params, get_page_link (get_option('alo_em_subsc_page')) ); $uns_link = alo_em_translate_url ( $uns_link, $recipient->lang ); */ /* $uns_vars = $recipient->subscriber . "|" . $recipient->unikey; $uns_vars = urlencode( base64_encode( $uns_vars ) ); $uns_link = add_query_arg( 'emunsub', $uns_vars, trailingslashit( get_home_url() ) ); $uns_link = alo_em_translate_url ( $uns_link, $recipient->lang ); if ( $unsubfooter = alo_em_translate_option ( $recipient->lang, 'alo_em_custom_unsub_footer', true ) ) { $content .= $unsubfooter; } else { $content .= "\r\n". __("You have received this message because you subscribed to our newsletter. If you want to unsubscribe: ", "alo-easymail")." ";
$content .= __("visit this link", "alo-easymail") ."
\r\n%UNSUBSCRIBELINK%";
$content .= "
". print_r ( $recipient, true ) . ""; } */ $mail_sender = ( get_option('alo_em_sender_email') ) ? get_option('alo_em_sender_email') : "noreply@". str_replace("www.","", $_SERVER['HTTP_HOST']); $from_name = html_entity_decode ( wp_kses_decode_entities ( get_option('alo_em_sender_name') ) ); $headers = "From: ". $from_name ." <".$mail_sender.">\n"; $headers .= "Content-Type: text/html; charset=\"" . strtolower( get_option('blog_charset') ) . "\"\n"; // ---- Send MAIL (or DEBUG) ---- $send_mode = ( $force_send ) ? "" : get_option('alo_em_debug_newsletters'); switch ( $send_mode ) { case "to_author": $author = get_userdata( $newsletter->post_author ); $debug_subject = "( DEBUG - TO: ". $recipient->email ." ) " . $subject; $mail_engine = wp_mail( $author->user_email, $debug_subject, $content, $headers ); break; case "to_file": $log = fopen( WP_CONTENT_DIR . "/user_{$newsletter->post_author}_newsletter_{$newsletter->ID}.log", 'a+' ); $log_message = "\n------------------------------ ". date_i18n( __( 'j M Y @ G:i' ) ) ." ------------------------------\n\n"; $log_message .= "HEADERS:\n". $headers ."\n"; $log_message .= "TO:\t\t\t". $recipient->email ."\n"; $log_message .= "SUBJECT:\t". $subject ."\n\n"; $log_message .= "CONTENT:\n". $content ."\n\n"; $mail_engine = ( fwrite ( $log, $log_message ) ) ? true : false; fclose ( $log ); break; default: // no debug: send it! $mail_engine = wp_mail( $recipient->email, $subject, $content, $headers ); } $sent = ( $mail_engine ) ? "1" : "-1"; // If recipient is in db (eg. ID exists) update db if ( $recipient->ID ) { $wpdb->update( "{$wpdb->prefix}easymail_recipients", array ( 'result' => $sent ), array ( 'ID' => $recipient->ID ) ); } return ( $mail_engine ) ? true : false; } /* function alo_em_send_newsletter_to ( $recip, $force_send=false ) { global $wpdb; $defaults = array( 'email' => false, 'newsletter' => false, 'ID' => false, // if false, it's a test sending 'lang' => alo_em_get_language (), 'name' => false, 'firstname' => false, 'subscriber' => false, 'unikey' => false ); $args = wp_parse_args( (array)$recip, $defaults ); $recipient = (object)$args; if ( !is_email( $recipient->email ) ) return; // Get newsletter details $newsletter = alo_em_get_newsletter( $recipient->newsletter ); $subject = stripslashes ( alo_em_translate_text ( $recipient->lang, $newsletter->post_title ) ); $subject = apply_filters( 'alo_easymail_newsletter_title', $subject, $newsletter, $recipient ); $content = alo_em_translate_text( $recipient->lang, $newsletter->post_content ); // easymail standard and custom filters $content = apply_filters( 'alo_easymail_newsletter_content', $content, $newsletter, $recipient, false ); // general filters and shortcodes applied to 'the_content'? if ( get_option('alo_em_filter_the_content') != "no" ) { add_filter ( 'the_content', 'do_shortcode', 11 ); $content = apply_filters( "the_content", $content ); } $viewonline_url = alo_em_translate_url ( get_permalink( $recipient->newsletter ), $recipient->lang ); if ( $viewonline_msg = alo_em_translate_option ( $recipient->lang, 'alo_em_custom_viewonline_msg', true ) ) { $content .= $viewonline_msg; } else { $content .= "\r\n
". __("To read the newsletter online you can visit this link", "alo-easymail"); $content .= ": %NEWSLETTERLINK%
\r\n"; } $content = str_replace ( "%NEWSLETTERLINK%", " ". $viewonline_url ."", $content ); // Unsubscribe link and tracking, only if subscriber if ( $recipient->unikey && $recipient->ID ) { $uns_vars = $recipient->subscriber . "|" . $recipient->unikey; $uns_vars = urlencode( base64_encode( $uns_vars ) ); $uns_link = add_query_arg( 'emunsub', $uns_vars, trailingslashit( get_home_url() ) ); $uns_link = alo_em_translate_url ( $uns_link, $recipient->lang ); if ( $unsubfooter = alo_em_translate_option ( $recipient->lang, 'alo_em_custom_unsub_footer', true ) ) { $content .= $unsubfooter; } else { $content .= "\r\n". __("You have received this message because you subscribed to our newsletter. If you want to unsubscribe: ", "alo-easymail")." ";
$content .= __("visit this link", "alo-easymail") ."
\r\n%UNSUBSCRIBELINK%";
$content .= "
". print_r ( $recipient, true ) . ""; } $mail_sender = ( get_option('alo_em_sender_email') ) ? get_option('alo_em_sender_email') : "noreply@". str_replace("www.","", $_SERVER['HTTP_HOST']); $from_name = html_entity_decode ( wp_kses_decode_entities ( get_option('alo_em_sender_name') ) ); $headers = "From: ". $from_name ." <".$mail_sender.">\n"; $headers .= "Content-Type: text/html; charset=\"" . strtolower( get_option('blog_charset') ) . "\"\n"; // ---- Send MAIL (or DEBUG) ---- $send_mode = ( $force_send ) ? "" : get_option('alo_em_debug_newsletters'); switch ( $send_mode ) { case "to_author": $author = get_userdata( $newsletter->post_author ); $debug_subject = "( DEBUG - TO: ". $recipient->email ." ) " . $subject; $mail_engine = wp_mail( $author->user_email, $debug_subject, $content, $headers ); break; case "to_file": $log = fopen( WP_CONTENT_DIR . "/user_{$newsletter->post_author}_newsletter_{$newsletter->ID}.log", 'a+' ); $log_message = "\n------------------------------ ". date_i18n( __( 'j M Y @ G:i', "alo-easymail" ) ) ." ------------------------------\n\n"; $log_message .= "HEADERS:\n". $headers ."\n"; $log_message .= "TO:\t\t\t". $recipient->email ."\n"; $log_message .= "SUBJECT:\t". $subject ."\n\n"; $log_message .= "CONTENT:\n". $content ."\n\n"; $mail_engine = ( fwrite ( $log, $log_message ) ) ? true : false; fclose ( $log ); break; default: // no debug: send it! $mail_engine = wp_mail( $recipient->email, $subject, $content, $headers ); } $sent = ( $mail_engine ) ? "1" : "-1"; // If recipient is in db (eg. ID exists) update db if ( $recipient->ID ) { $wpdb->update( "{$wpdb->prefix}easymail_recipients", array ( 'result' => $sent ), array ( 'ID' => $recipient->ID ) ); } return ( $mail_engine ) ? true : false; } */ /** * When the newsletter has been sent, mark it as completed */ function alo_em_set_newsletter_as_completed ( $newsletter ) { global $wpdb; alo_em_edit_newsletter_status ( $newsletter, 'sent' ); add_post_meta ( $newsletter, "_easymail_completed", current_time( 'mysql', 0 ) ); $newsletter_obj = alo_em_get_newsletter ( $newsletter ); do_action ( 'alo_easymail_newsletter_delivered', $newsletter_obj ); } /** * Called by wp_cron: send the newsletter to a fraction of recipients every X minutes */ function alo_em_batch_sending () { global $wpdb; // search the interval between now and previous sending (or from default cron interval) $prev_time = ( get_option ( 'alo_em_last_cron' ) ) ? strtotime( get_option ( 'alo_em_last_cron' ) ) : current_time( 'timestamp', 0 ) - ALO_EM_INTERVAL_MIN * 60; $diff_time = current_time( 'timestamp', 0 ) - $prev_time; // so... how much recipients for this interval? // (86400 = seconds in a day) $day_rate = alo_em_get_dayrate(); $tot_recs = max ( floor( ( $day_rate * $diff_time / 86400 ) ) , 1 ); // not over the limit $limit_recs = min ( $tot_recs, alo_em_get_batchrate () ); // the recipients to whom send $recipients = alo_em_get_recipients_in_queue ( $limit_recs ); // update 'last cron time' option update_option ( 'alo_em_last_cron', current_time( 'mysql', 0 ) ); // if no recipients exit! if ( !$recipients ) return; foreach ( $recipients as $recipient ) { if ( alo_em_get_newsletter_status ( $recipient->newsletter ) != "sendable" ) continue; ob_start(); // Prepare and send the newsletter to this user! alo_em_send_newsletter_to ( $recipient ); // if no more recipient of this newsletter, it has been sent if ( count( alo_em_get_recipients_in_queue( 1, $recipient->newsletter ) ) == 0 ) { alo_em_set_newsletter_as_completed ( $recipient->newsletter ); } ob_end_flush(); if ( (int)get_option('alo_em_sleepvalue') > 0 ) usleep ( (int)get_option('alo_em_sleepvalue') * 1000 ); } } /** * alo newsletter custom email hooks */ function alo_em_zirkuss_custom_easymail_placeholders( $placeholders ) { $placeholders['easymail_subscriber']['tags']['[USER-UNSUBSCRIBE]'] = __ ( 'Text and URL to unsubscribe.', 'alo-easymail' ) . " (". __( 'You can customise this text in settings', 'alo-easymail' ) .".)"; $placeholders['easymail_subscriber']['tags']['[USER-UNSUBSCRIBE-URL]'] = __ ( 'URL to unsubscribe.', 'alo-easymail' ); $placeholders['easymail_newsletter']['title'] = __( "Newsletter tags", "alo-easymail" ); $placeholders['easymail_newsletter']['tags']['[READ-ONLINE]'] = __ ( 'Text and URL to the online version.', 'alo-easymail' ) . " (". __( 'You can customise this text in settings', 'alo-easymail' ) .".)"; $placeholders['easymail_newsletter']['tags']['[READ-ONLINE-URL]'] = __ ( 'URL to the online version.', 'alo-easymail' ); $placeholders['easymail_newsletter']['tags']['[TITLE]'] = __ ( 'Title of the newsletter.', 'alo-easymail' ); $placeholders['easymail_newsletter']['tags']['[DATE]'] = __ ( 'Date of the newsletter.', 'alo-easymail' ); $placeholders['easymail_newsletter']['tags']['[THUMB]'] = __ ( 'Post Thumbnail of newsletter', 'alo-easymail' ); $placeholders['easymail_newsletter']['tags']['[GALLERY]'] = __ ( 'Image gallery of newsletter', 'alo-easymail' ); $placeholders['easymail_site']['title'] = __( "Site tags", "alo-easymail" ); $placeholders['easymail_site']['tags']['[SITE-LINK]'] = __("The link to the site", "alo-easymail"); $placeholders['easymail_site']['tags']['[SITE-URL]'] = __ ( 'URL to the site.', 'alo-easymail' ); $placeholders['easymail_site']['tags']['[SITE-NAME]'] = __('Site Title'); $placeholders['easymail_site']['tags']['[SITE-DESCRIPTION]'] = __('Tagline'); $placeholders['easymail_post']['tags']['[POST-THUMB]'] = __("Post Thumbnail", "alo-easymail"); $placeholders['easymail_post']['tags']['[POST-GALLERY]'] = __("The image gallery of the post", "alo-easymail") ; return $placeholders; } add_filter ( 'alo_easymail_newsletter_placeholders_table', 'alo_em_zirkuss_custom_easymail_placeholders', 5 ); /** * alo newsletter content */ function alo_em_zirkuss_newsletter_content( $content, $newsletter, $recipient, $stop_recursive_the_content = false ) { if ( !is_object( $recipient ) ) $recipient = new stdClass(); if ( empty( $recipient->lang ) ) $recipient->lang = alo_em_short_langcode ( get_locale() ); // use the email theme only when emailing the // newsletter. otherwise use the default // wordpress theme to display the newsletter. if ( ( isset( $recipient->unikey ) && isset( $recipient->ID ) ) /* || ( isset( $_POST['email'] ) && isset( $_POST['newsletter'] ) )*/ ) { // Create the message to read the newsletter online $viewonline_url = alo_em_translate_url ( get_permalink( $recipient->newsletter ), $recipient->lang ); $viewonline_msg = alo_em_translate_option ( $recipient->lang, 'alo_em_custom_viewonline_msg', true ); if( empty( $viewonline_msg ) ) { $viewonline_msg = __('To read the newsletter online you can visit this link:', 'alo-easymail') . ' %NEWSLETTERLINK%'; } $viewonline_msg = str_replace( '%NEWSLETTERLINK%', ' '. $viewonline_url .'', $viewonline_msg ); $viewonline_msg = str_replace( '%NEWSLETTERURL%', $viewonline_url, $viewonline_msg ); $uns_vars = $recipient->subscriber . '|' . $recipient->unikey; $uns_vars = urlencode( base64_encode( $uns_vars ) ); $uns_link = add_query_arg( 'emunsub', $uns_vars, trailingslashit( get_home_url() ) ); $uns_link = alo_em_translate_url ( $uns_link, $recipient->lang ); $unsubfooter = alo_em_translate_option ( $recipient->lang, 'alo_em_custom_unsub_footer', true ); if ( empty( $unsubfooter ) ) { $unsubfooter = __('You have received this message because you subscribed to our newsletter. If you want to unsubscribe: ', 'alo-easymail').' %UNSUBSCRIBELINK%'; } $unsubfooter = str_replace ( '%UNSUBSCRIBELINK%', ' '. $uns_link .'', $unsubfooter ); $unsubfooter = str_replace ( '%UNSUBSCRIBEURL%', $uns_link, $unsubfooter ); // Tracking code $track_vars = $recipient->ID . '|' . $recipient->unikey; $track_vars = urlencode( base64_encode( $track_vars ) ); $tracking_view = '