/**
 * Displays the introduction for the group and loops through each item
 *
 * I've chosen to cache on an individual-activity basis, instead of a group-by-group basis. This
 * requires just a touch more overhead (in terms of looping through individual activity_ids), and
 * doesn't really have any added effect at the moment (since an activity item can only be associated
 * with a single group). But it provides the greatest amount of flexibility going forward, both in
 * terms of the possibility that activity items could be associated with more than one group, and
 * the possibility that users within a single group would want more highly-filtered digests.
 */
function ass_digest_format_item_group($group_id, $activity_ids, $type, $group_name, $group_slug, $user_id)
{
    global $bp, $ass_email_css;
    $group_permalink = apply_filters('bp_get_group_permalink', bp_get_root_domain() . '/' . bp_get_groups_root_slug() . '/' . $group_slug . '/');
    $group_name_link = '<a href="' . $group_permalink . '" name="' . $group_slug . '">' . $group_name . '</a>';
    $userdomain = ass_digest_get_user_domain($user_id);
    $unsubscribe_link = "{$userdomain}?bpass-action=unsubscribe&group={$group_id}&access_key=" . md5("{$group_id}{$user_id}unsubscribe" . wp_salt());
    $gnotifications_link = ass_get_login_redirect_url($group_permalink . 'notifications/');
    // add the group title bar
    if ($type == 'dig') {
        $group_message = "\n<div {$ass_email_css['group_title']}>" . sprintf(__('Group: %s', 'bp-ass'), $group_name_link) . "</div>\n\n";
    } elseif ($type == 'sum') {
        $group_message = "\n<div {$ass_email_css['group_title']}>" . sprintf(__('Group: %s weekly summary', 'bp-ass'), $group_name_link) . "</div>\n";
    }
    // add change email settings link
    $group_message .= "\n<div {$ass_email_css['change_email']}>";
    $group_message .= __('To disable these notifications for this group click ', 'bp-ass') . " <a href=\"{$unsubscribe_link}\">" . __('unsubscribe', 'bp-ass') . '</a> - ';
    $group_message .= __('change ', 'bp-ass') . '<a href="' . $gnotifications_link . '">' . __('email options', 'bp-ass') . '</a>';
    $group_message .= "</div>\n\n";
    $group_message = apply_filters('ass_digest_group_message_title', $group_message, $group_id, $type);
    // Finally, add the markup to the digest
    foreach ($activity_ids as $activity_id) {
        // Cache is set earlier in ass_digest_fire()
        $activity_item = !empty($bp->ass->items[$activity_id]) ? $bp->ass->items[$activity_id] : false;
        if (!empty($activity_item)) {
            $group_message .= ass_digest_format_item($activity_item, $type);
        }
        //$group_message .= '<pre>'. $item->id .'</pre>';
    }
    return apply_filters('ass_digest_format_item_group', $group_message, $group_id, $type);
}
/**
 * Displays the introduction for the group and loops through each item
 *
 * I've chosen to cache on an individual-activity basis, instead of a group-by-group basis. This
 * requires just a touch more overhead (in terms of looping through individual activity_ids), and
 * doesn't really have any added effect at the moment (since an activity item can only be associated
 * with a single group). But it provides the greatest amount of flexibility going forward, both in
 * terms of the possibility that activity items could be associated with more than one group, and
 * the possibility that users within a single group would want more highly-filtered digests.
 */
function ass_digest_format_item_group($group_id, $activity_ids, $type, $group_name, $group_slug, $user_id)
{
    global $bp, $ass_email_css;
    $group_permalink = bp_get_root_domain() . '/' . bp_get_groups_root_slug() . '/' . $group_slug . '/';
    $group_name_link = '<a href="' . $group_permalink . '" name="' . $group_slug . '">' . $group_name . '</a>';
    $userdomain = bp_core_get_user_domain($user_id);
    $unsubscribe_link = "{$userdomain}?bpass-action=unsubscribe&group={$group_id}&access_key=" . md5("{$group_id}{$user_id}unsubscribe" . wp_salt());
    // add the group title bar
    if ($type == 'dig') {
        $group_message = "\n<div {$ass_email_css['group_title']}>" . sprintf(__('Group: %s', 'bp-ass'), $group_name_link) . "</div>\n\n";
    } elseif ($type == 'sum') {
        $group_message = "\n<div {$ass_email_css['group_title']}>" . sprintf(__('Group: %s weekly summary', 'bp-ass'), $group_name_link) . "</div>\n";
    }
    // add change email settings link
    $group_message .= "\n<div {$ass_email_css['change_email']}>";
    $group_message .= __('To disable these notifications for this group click ', 'bp-ass') . " <a href=\"{$unsubscribe_link}\">" . __('unsubscribe', 'bp-ass') . '</a> - ';
    $group_message .= __('change ', 'bp-ass') . "<a href=\"" . $group_permalink . "notifications/\">" . __('email options', 'bp-ass') . "</a> ";
    $group_message .= "</div>\n\n";
    $group_message = apply_filters('ass_digest_group_message_title', $group_message, $group_id, $type);
    // Finally, add the markup to the digest
    foreach ($activity_ids as $activity_id) {
        // Cache is set earlier in ass_digest_fire()
        $activity_item = wp_cache_get('bp_activity_' . $activity_id, 'bp');
        if (!$activity_item) {
            // Try fetching it manually
            $activity_items = bp_activity_get_specific(array('sort' => 'ASC', 'activity_ids' => array($activity_item), 'show_hidden' => true));
            if (!empty($activity_items)) {
                $activity_item = $activity_items[0];
            }
        }
        if (!empty($activity_item)) {
            $group_message .= ass_digest_format_item($activity_item, $type);
        }
        //$group_message .= '<pre>'. $item->id .'</pre>';
    }
    return apply_filters('ass_digest_format_item_group', $group_message, $group_id, $type);
}