"; 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 $user_id = ( email_exists( $email ) ) ? email_exists( $email ) : false; $args = array( 'newsletter' => $newsletter, 'email' => $email, 'user_id' => $user_id ); $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 = $rec->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 = $rec->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 (skip if registered user) *@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', 'user_id' => '' ); $fields = wp_parse_args( $args, $defaults ); $added = false; if ( $fields['email'] && $fields['newsletter'] ) { if ( !$only_if_active || !alo_em_is_subscriber( $fields['email'] ) || ( $only_if_active && alo_em_is_subscriber( $fields['email'] ) && alo_em_check_subscriber_state( $fields['email'] ) == 1 ) ) { $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, $offset=0, $limit=0 ) { global $wpdb; $where_to_send = ( $only_to_send ) ? "AND r.result = 0" : ""; $limit = ( $offset || $limit ) ? " LIMIT $offset, $limit " : ""; 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". $limit, $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, false, false ) ); } /** * 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 = ( get_option('alo_em_sender_email') ) ? get_option('alo_em_sender_email') : "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 "