function ass_digest_fire($type) { global $bp, $wpdb, $groups_template, $ass_email_css; // If safe mode isn't on, then let's set the execution time to unlimited if (!ini_get('safe_mode')) { set_time_limit(0); } if (!is_string($type)) { $type = 'sum'; } // HTML emails only work with inline CSS styles. Here we setup the styles to be used in various functions below. $ass_email_css['wrapper'] = 'style="color:#333;clear:both;'; // use this to style the body $ass_email_css['title'] = 'style="font-size:130%;"'; $ass_email_css['summary'] = ''; $ass_email_css['summary_ul'] = 'style="padding:12px 0 5px; list-style-type:circle; list-style-position:inside;"'; //$ass_email_css['summary'] = 'style="display:list-item;"'; $ass_email_css['follow_topic'] = 'style="padding:15px 0 0; color: #888;clear:both;"'; $ass_email_css['group_title'] = 'style="font-size:120%; background-color:#F5F5F5; padding:3px; margin:20px 0 0; border-top: 1px #eee solid;"'; $ass_email_css['change_email'] = 'style="font-size:12px; margin-left:10px; color:#888;"'; $ass_email_css['item_div'] = 'style="padding: 10px; border-top: 1px #eee solid;"'; $ass_email_css['item_action'] = 'style="color:#888;"'; $ass_email_css['item_date'] = 'style="font-size:85%; color:#bbb; margin-left:8px;"'; $ass_email_css['item_content'] = 'style="color:#333;"'; $ass_email_css['item_weekly'] = 'style="color:#888; padding:4px 10px 0"'; // used in weekly in place of other item_ above $ass_email_css['footer'] = 'class="ass-footer" style="margin:25px 0 0; padding-top:5px; border-top:1px #bbb solid;"'; // Allow plugins to filter the CSS $ass_email_css = apply_filters('ass_email_css', $ass_email_css); if ($type == 'dig') { $title = sprintf(__('Your daily digest of group activity', 'bp-ass')); } else { $title = sprintf(__('Your weekly summary of group topics', 'bp-ass')); } $title = apply_filters('ass_digest_title', $title, $type); $blogname = get_blog_option(BP_ROOT_BLOG, 'blogname'); $subject = apply_filters('ass_digest_subject', "{$title} [{$blogname}]", $blogname, $title, $type); $footer = "\n\n<div {$ass_email_css['footer']}>"; $footer .= sprintf(__("You have received this message because you are subscribed to receive a digest of activity in some of your groups on %s.", 'bp-ass'), $blogname); $footer = apply_filters('ass_digest_footer', $footer, $type); // get all user subscription data $user_subscriptions = $wpdb->get_results("SELECT user_id, meta_value FROM {$wpdb->usermeta} WHERE meta_key = 'ass_digest_items' AND meta_value != ''"); // no subscription data? stop now! if (empty($user_subscriptions)) { return; } // get all activity IDs for everyone subscribed to a digest $all_activity_items = array(); foreach ((array) $user_subscriptions as $us) { $subs = maybe_unserialize($us->meta_value); foreach ((array) $subs as $digest_type => $group_subs) { foreach ((array) $group_subs as $group_id => $sub_ids) { foreach ((array) $sub_ids as $sid) { // note: activity ID is added as the key for performance reasons if (!isset($all_activity_items[$sid]) && !empty($sid)) { $all_activity_items[$sid] = 1; } } } } } // no activity IDs? stop now! if (empty($all_activity_items)) { return; } // setup the IN query $in = implode(",", array_keys($all_activity_items)); // get only the activity data we need // // we're not using bp_activity_get_specific() to avoid an extra query with the // xprofile table // // @todo MySQL IN query doesn't scale well when querying a ton of IDs $items = $wpdb->get_results("\n\t\tSELECT id, type, action, content, primary_link, secondary_item_id, date_recorded\n\t\t\tFROM {$bp->activity->table_name}\n\t\t\tWHERE id IN ({$in})\n\t"); // cache some stuff $bp->ass = new stdClass(); $bp->ass->massdata = ass_get_mass_userdata(wp_list_pluck($user_subscriptions, 'user_id')); $bp->ass->activity_ids = array_flip((array) wp_list_pluck($items, 'id')); $bp->ass->items = array(); // cache activity items foreach ($items as $item) { $bp->ass->items[$item->id] = $item; } // get list of all groups so we can look them up quickly in the foreach loop below $all_groups = $wpdb->get_results("SELECT id, name, slug FROM {$bp->groups->table_name}"); // setup group info array that we'll reference later $groups_info = array(); foreach ($all_groups as $group) { $groups_info[$group->id] = array('name' => ass_digest_filter($group->name), 'slug' => $group->slug); } // start the digest loop for each user foreach ((array) $user_subscriptions as $user) { $user_id = $user->user_id; // get the group subscriptions for the user $group_activity_ids_array = unserialize($user->meta_value); // initialize some strings $summary = $activity_message = ''; // We only want the weekly or daily ones if (empty($group_activity_ids_array[$type]) || !($group_activity_ids = (array) $group_activity_ids_array[$type])) { continue; } // get userdata // @see ass_get_mass_userdata() $userdata = $bp->ass->massdata[$user_id]; // sanity check! if (empty($userdata)) { continue; } // email address of user $to = $userdata['email']; $userdomain = ass_digest_get_user_domain($user_id); // filter the list - can be used to sort the groups $group_activity_ids = apply_filters('ass_digest_group_activity_ids', @$group_activity_ids); $header = "<div {$ass_email_css['title']}>{$title} " . __('at', 'bp-ass') . " <a href='" . $bp->root_domain . "'>{$blogname}</a></div>\n\n"; $message = apply_filters('ass_digest_header', $header, $title, $ass_email_css['title']); // loop through each group for this user foreach ($group_activity_ids as $group_id => $activity_ids) { // check to see if our activity IDs exist // intersect against our master activity IDs array $activity_ids = array_intersect_key(array_flip($activity_ids), $bp->ass->activity_ids); $activity_ids = array_keys($activity_ids); $group_name = $groups_info[$group_id]['name']; $group_slug = $groups_info[$group_id]['slug']; if ('dig' == $type) { // might be nice here to link to anchor tags in the message $summary .= apply_filters('ass_digest_summary', "<li {$ass_email_css['summary']}><a href='#{$group_slug}'>{$group_name}</a> " . sprintf(__('(%s items)', 'bp-ass'), count($activity_ids)) . "</li>\n", $ass_email_css['summary'], $group_slug, $group_name, $activity_ids); } $activity_message .= ass_digest_format_item_group($group_id, $activity_ids, $type, $group_name, $group_slug, $user_id); unset($group_activity_ids[$group_id]); } // reset the user's sub array removing those sent $group_activity_ids_array[$type] = $group_activity_ids; // show group summary for digest, and follow help text for weekly summary if ('dig' == $type) { $message .= apply_filters('ass_digest_summary_full', "\n<ul {$ass_email_css['summary_ul']}>" . __('Group Summary', 'bp-ass') . ":\n" . $summary . "</ul>\n", $ass_email_css['summary_ul'], $summary); } // the meat of the message which we generated above goes here $message .= $activity_message; // user is subscribed to "New Topics" // add follow help text only if bundled forums are enabled if ('sum' == $type && class_exists('BP_Forums_Component')) { $message .= apply_filters('ass_summary_follow_topic', "<div {$ass_email_css['follow_topic']}>" . __("How to follow a topic: to get email updates for a specific topic, click the topic title - then on the webpage click the <i>Follow this topic</i> button. (If you don't see the button you need to login first.)", 'bp-ass') . "</div>\n", $ass_email_css['follow_topic']); } $message .= $footer; $unsubscribe_message = "\n\n<br><br>" . sprintf(__("To disable these notifications per group please login and go to: %s where you can change your email settings for each group.", 'bp-ass'), "<a href=\"{$userdomain}{$bp->groups->slug}/\">" . __('My Groups', 'bp-ass') . "</a>"); if (get_option('ass-global-unsubscribe-link') == 'yes') { $unsubscribe_link = "{$userdomain}?bpass-action=unsubscribe&access_key=" . md5($user_id . 'unsubscribe' . wp_salt()); $unsubscribe_message .= "\n\n<br><br><a href=\"{$unsubscribe_link}\">" . __('Disable these notifications for all my groups at once.', 'bp-ass') . '</a>'; } $message .= apply_filters('ass_digest_disable_notifications', $unsubscribe_message, $userdomain . $bp->groups->slug); $message .= "</div>"; $message_plaintext = ass_convert_html_to_plaintext($message); if (isset($_GET['sum'])) { // test mode run from the browser, dont send the emails, just show them on screen using domain.com?sum=1 echo '<div style="background-color:white; width:75%;padding:20px 10px;">'; echo '<p>======================== to: <b>' . $to . '</b> ========================</p>'; echo $message; //echo '<br>PLAIN TEXT PART:<br><pre>'; echo $message_plaintext ; echo '</pre>'; echo '</div>'; } else { // send out the email ass_send_multipart_email($to, $subject, $message_plaintext, $message); // update the subscriber's digest list bp_update_user_meta($user_id, 'ass_digest_items', $group_activity_ids_array); } unset($message, $message_plaintext, $message, $to, $userdata, $userdomain, $activity_message, $summary, $group_activity_ids_array, $group_activity_ids); } }
function ass_digest_fire($type) { global $bp, $wpdb, $groups_template, $ass_email_css, $current_user; if (!is_string($type)) { $type = 'sum'; } // HTML emails only work with inline CSS styles. Here we setup the styles to be used in various functions below. $ass_email_css['wrapper'] = 'style="color:#333;clear:both;'; // use this to style the body $ass_email_css['title'] = 'style="font-size:130%;"'; $ass_email_css['summary'] = ''; $ass_email_css['summary_ul'] = 'style="padding:12px 0 5px; list-style-type:circle; list-style-position:inside;"'; //$ass_email_css['summary'] = 'style="display:list-item;"'; $ass_email_css['follow_topic'] = 'style="padding:15px 0 0; color: #888;clear:both;"'; $ass_email_css['group_title'] = 'style="font-size:120%; background-color:#F5F5F5; padding:3px; margin:20px 0 0; border-top: 1px #eee solid;"'; $ass_email_css['change_email'] = 'style="font-size:12px; margin-left:10px; color:#888;"'; $ass_email_css['item_div'] = 'style="padding: 10px; border-top: 1px #eee solid;"'; $ass_email_css['item_action'] = 'style="color:#888;"'; $ass_email_css['item_date'] = 'style="font-size:85%; color:#bbb; margin-left:8px;"'; $ass_email_css['item_content'] = 'style="color:#333;"'; $ass_email_css['item_weekly'] = 'style="color:#888; padding:4px 10px 0"'; // used in weekly in place of other item_ above $ass_email_css['footer'] = 'class="ass-footer" style="margin:25px 0 0; padding-top:5px; border-top:1px #bbb solid;"'; // Allow plugins to filter the CSS $ass_email_css = apply_filters('ass_email_css', $ass_email_css); if ($type == 'dig') { $title = sprintf(__('Your daily digest of group activity', 'bp-ass')); } else { $title = sprintf(__('Your weekly summary of group topics', 'bp-ass')); } $title = apply_filters('ass_digest_title', $title, $type); $blogname = get_blog_option(BP_ROOT_BLOG, 'blogname'); $subject = apply_filters('ass_digest_subject', "{$title} [{$blogname}]", $blogname, $title, $type); $footer = "\n\n<div {$ass_email_css['footer']}>"; $footer .= sprintf(__("You have received this message because you are subscribed to receive a digest of activity in some of your groups on %s.", 'bp-ass'), $blogname); $footer = apply_filters('ass_digest_footer', $footer, $type); // get list of all groups so we can look them up quickly in the foreach loop below $all_groups = $wpdb->get_results("SELECT id, name, slug FROM {$bp->groups->table_name}"); foreach ($all_groups as $group) { $group_name = ass_digest_filter($group->name); $groups_info[$group->id] = array('name' => $group_name, 'slug' => $group->slug); } $user_subscriptions = $wpdb->get_results("SELECT user_id, meta_value FROM {$wpdb->usermeta} WHERE meta_key = 'ass_digest_items' AND meta_value != ''"); // Do all activity lookups in one single query, and cache the results // Todo: in_array() is slow; isset( array_flip( $array[$id] ) ) is better // but it will take a flip every time $all_activity_items = array(); foreach ((array) $user_subscriptions as $us) { $subs = maybe_unserialize($us->meta_value); foreach ((array) $subs as $digest_type => $group_subs) { foreach ((array) $group_subs as $group_id => $sub_ids) { foreach ((array) $sub_ids as $sid) { if (!in_array($sid, $all_activity_items)) { $all_activity_items[] = $sid; } } } } } $items = bp_activity_get_specific(array('sort' => 'ASC', 'activity_ids' => $all_activity_items, 'show_hidden' => true)); foreach ((array) $items['activities'] as $activity) { $key = 'bp_activity_' . $activity->id; if (!wp_cache_get($key, 'bp')) { wp_cache_set($key, $activity, 'bp', 60 * 60); } } foreach ((array) $user_subscriptions as $user) { $user_id = $user->user_id; $group_activity_ids_array = unserialize($user->meta_value); $summary = $activity_message = ''; // We only want the weekly or daily ones if (empty($group_activity_ids_array[$type]) || !($group_activity_ids = (array) $group_activity_ids_array[$type])) { continue; } // Get the details for the user if (!($userdata = bp_core_get_core_userdata($user_id))) { continue; } if (!($to = $userdata->user_email)) { continue; } $userdomain = bp_core_get_user_domain($user_id); // filter the list - can be used to sort the groups $group_activity_ids = apply_filters('ass_digest_group_activity_ids', @$group_activity_ids); $header = "<div {$ass_email_css['title']}>{$title} " . __('at', 'bp-ass') . " <a href='" . $bp->root_domain . "'>{$blogname}</a></div>\n\n"; $message = apply_filters('ass_digest_header', $header, $title, $ass_email_css['title']); // loop through each group for this user foreach ($group_activity_ids as $group_id => $activity_ids) { $group_name = $groups_info[$group_id]['name']; $group_slug = $groups_info[$group_id]['slug']; if ('dig' == $type) { // might be nice here to link to anchor tags in the message $summary .= apply_filters('ass_digest_summary', "<li {$ass_email_css['summary']}><a href='#{$group_slug}'>{$group_name}</a> " . sprintf(__('(%s items)', 'bp-ass'), count($activity_ids)) . "</li>\n", $ass_email_css['summary'], $group_slug, $group_name, $activity_ids); } $activity_message .= ass_digest_format_item_group($group_id, $activity_ids, $type, $group_name, $group_slug, $user_id); unset($group_activity_ids[$group_id]); } // reset the user's sub array removing those sent sent $group_activity_ids_array[$type] = $group_activity_ids; // show group summary for digest, and follow help text for weekly summary if ('dig' == $type) { $message .= apply_filters('ass_digest_summary_full', "\n<ul {$ass_email_css['summary_ul']}>" . __('Group Summary', 'bp-ass') . ":\n" . $summary . "</ul>\n", $ass_email_css['summary_ul'], $summary); } $message .= $activity_message; // the meat of the message which we generated above goes here // user is subscribed to "New Topics" // add follow help text only if bundled forums are enabled if ('sum' == $type && class_exists('BP_Forums_Component')) { $message .= apply_filters('ass_summary_follow_topic', "<div {$ass_email_css['follow_topic']}>" . __("How to follow a topic: to get email updates for a specific topic, click the topic title - then on the webpage click the <i>Follow this topic</i> button. (If you don't see the button you need to login first.)", 'bp-ass') . "</div>\n", $ass_email_css['follow_topic']); } $message .= $footer; $unsubscribe_message = "\n\n<br><br>" . sprintf(__("To disable these notifications per group please login and go to: %s where you can change your email settings for each group.", 'bp-ass'), "<a href=\"{$userdomain}{$bp->groups->slug}/\">" . __('My Groups', 'bp-ass') . "</a>"); if (get_option('ass-global-unsubscribe-link') == 'yes') { $unsubscribe_link = "{$userdomain}?bpass-action=unsubscribe&access_key=" . md5($user_id . 'unsubscribe' . wp_salt()); $unsubscribe_message .= "\n\n<br><br><a href=\"{$unsubscribe_link}\">" . __('Disable these notifications for all my groups at once.', 'bp_ass') . '</a>'; } $message .= apply_filters('ass_digest_disable_notifications', $unsubscribe_message, $userdomain . $bp->groups->slug); $message .= "</div>"; $message_plaintext = ass_convert_html_to_plaintext($message); if (isset($_GET['sum'])) { // test mode run from the browser, dont send the emails, just show them on screen using domain.com?sum=1 echo '<div style="background-color:white; width:75%;padding:20px 10px;">'; echo '<p>======================== to: <b>' . $to . '</b> ========================</p>'; echo $message; //echo '<br>PLAIN TEXT PART:<br><pre>'; echo $message_plaintext ; echo '</pre>'; echo '</div>'; } else { // send out the email ass_send_multipart_email($to, $subject, $message_plaintext, $message); // update the subscriber's digest list update_user_meta($user_id, 'ass_digest_items', $group_activity_ids_array); } unset($message, $message_plaintext, $message, $to, $userdata, $userdomain, $activity_message, $summary, $group_activity_ids_array, $group_activity_ids); } }