Advertisement
Guest User

r-a-y

a guest
May 13th, 2010
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.51 KB | None | 0 0
  1. function ass_digest_fire( $type ) {
  2.     global $bp;
  3.  
  4.     if ( !$type )
  5.         $type = 'dig';
  6.        
  7.     // This is done with bp_has_groups because user subscription status is stored in groupmeta. Therefore you can't simply pull up a list of all members on the site and run through them one by one. The bp_has_groups method will result in far fewer db hits
  8.    
  9.     if ( bp_has_groups( 'per_page=100000' ) ) {
  10.        
  11.         if ( $type == 'dig' )
  12.             $subject = sprintf( __( 'Your daily digest of group activity', 'bp-ass' ) );
  13.         else
  14.             $subject = sprintf( __( 'Your weekly summary of group topics', 'bp-ass' ) );
  15.  
  16.         $blogname = get_blog_option( BP_ROOT_BLOG, 'blogname' );           
  17.         $subject .= ' [' . $blogname . '] ';
  18.    
  19.         $footer = "\n-----------\n";
  20.         $footer .= sprintf( __( "You have received this message because you are subscribed to receive a digest of activity in some of your groups on %s. To change your notification settings for a given group, click on the group\'s link above and visit the Email Options page.", 'bp-ass' ), $blogname );
  21.            
  22.         $member_sent = array();
  23.        
  24.         while ( bp_groups() ) {
  25.             bp_the_group();
  26.            
  27.             $group_id = bp_get_group_id();
  28.            
  29.             $subscribers = groups_get_groupmeta( $group_id , 'ass_subscribed_users' );
  30.                        
  31.             // number of sent emails
  32.             $s = 0;
  33.  
  34.             foreach ( (array)$subscribers as $subscriber => $email_status ) {          
  35.                
  36.                 // Each user only gets one digest each time around
  37.                 if ( in_array( $subscriber, $member_sent ) )
  38.                     continue;
  39.                
  40.                 // Get the activity items the user needs to receive. If there are none, move on to the next member
  41.                 if ( !$group_activity_ids_array = get_usermeta( $subscriber, 'ass_digest_items' ) )
  42.                     continue;
  43.                
  44.                 // We only want the weekly or daily ones
  45.                 if ( !$group_activity_ids = (array)$group_activity_ids_array[$type] )
  46.                     continue;
  47.        
  48.                 $message = $subject . "\n\n----------------------\n";
  49.                 $summary = __( 'Group Summary', 'bp-ass');
  50.                
  51.                 foreach ( $group_activity_ids as $group_id => $activity_ids ) {
  52.                     // get group name and add it to this
  53.                     $group = new BP_Groups_Group( $group_id );
  54.                     $group_name = bp_get_group_name( $group );
  55.                     $act_count = count( $activity_ids );
  56.                     $summary .= "\n- " . $group_name . ' ' . sprintf( __( '(%s items)', 'bp-ass' ), $act_count );
  57.                    
  58.                     $activity_message .= ass_digest_format_item_group( $group_id, $activity_ids, $type );
  59.                     unset( $group_activity_ids[$group_id] );
  60.                 }
  61.                
  62.                 $summary .= "\n----------------------\n\n";
  63.                
  64.                 if ( $type == 'dig' )
  65.                     $message .= $summary;
  66.                    
  67.                 $message .= $activity_message;
  68.                 $message .= $footer;
  69.                
  70.                 $message = strip_tags(stripslashes( $message ) );
  71.                
  72.                 // Get the details for the user
  73.                 $ud = bp_core_get_core_userdata( $subscriber );
  74.        
  75.                 // Set up and send the message
  76.                 $to = $ud->user_email;
  77.                
  78.                 //echo '<br><br>========================================================<br><br>';// For testing only
  79.                 //echo '<pre> To: '.$to . '</pre>'; // For testing only
  80.                 //echo '<pre>'; print_r( $message ); echo '</pre>'; //die(); // For testing only
  81.                
  82.                 wp_mail( $to, $subject, $message );
  83.                 unset( $message, $to );        
  84.        
  85.                 $group_activity_ids_array[$type] = $group_activity_ids;
  86.                 update_usermeta( $subscriber, 'ass_digest_items', $group_activity_ids_array );  // comment this out for helpful testing
  87.                 $member_sent[] = $subscriber;
  88.  
  89.                 $s++;
  90.    
  91.                 if ( $s % 50 == 0 )
  92.                     sleep(10); // to help server load, delay 10 seconds for every 50 messages sent                 
  93.             }
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement