Advertisement
Guest User

Activity feed email plugin

a guest
Mar 13th, 2013
892
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.05 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Add blog post to activity feed and email
  4. Plugin URI: http://becsrivett.com
  5. Description: Allows you to publish blog posts to the activity feed & email. Requires Buddypress group email subscription plugin
  6. Author: Becs Rivett
  7. Version: 1.0
  8. Tags: buddypress, groups, blog, edit
  9. Author URI: http://becsrivett,cin
  10. Network: true
  11. */
  12.  
  13. function bp_groups_record_post( $post_id, $post, $user_id = 0 ) {
  14.     global $bp, $wpdb;
  15.  
  16.     $post_id = (int)$post_id;
  17.     $blog_id = (int)$wpdb->blogid;
  18.    
  19.     //get all the category ID's for the post
  20.     $post_categories = wp_get_post_categories($post_id);
  21.    
  22.     foreach($post_categories as $c) {
  23.    
  24.     //get the slug for the current category
  25.     $cat = get_category($c);
  26.     $cat_slug = $cat->slug;
  27.  
  28.         //get group slug
  29.         $groupr_id = groups_get_id($cat_slug);
  30.    
  31.     }
  32.  
  33.     if ( !$user_id )
  34.         $user_id = (int) $post->post_author;
  35.  
  36.     // Stop infinite loops with WordPress MU Sitewide Tags.
  37.     // That plugin changed the way its settings were stored at some point. Thus the dual check.
  38.     if ( !empty( $bp->site_options['sitewide_tags_blog'] ) ) {
  39.         $st_options = maybe_unserialize( $bp->site_options['sitewide_tags_blog'] );
  40.         $tags_blog_id = isset( $st_options['tags_blog_id'] ) ? $st_options['tags_blog_id'] : 0;
  41.     } else {
  42.         $tags_blog_id = isset( $bp->site_options['tags_blog_id'] ) ? $bp->site_options['tags_blog_id'] : 0;
  43.     }
  44.  
  45.     if ( (int) $blog_id == $tags_blog_id && apply_filters( 'bp_blogs_block_sitewide_tags_activity', true ) )
  46.         return false;
  47.  
  48.     // Don't record this if it's not a post
  49.     if ( !in_array( $post->post_type, apply_filters( 'bp_blogs_record_post_post_types', array( 'post' ) ) ) )
  50.         return false;
  51.  
  52.     $is_blog_public = apply_filters( 'bp_is_blog_public', (int)get_blog_option( $blog_id, 'blog_public' ) );
  53.  
  54.     if ( 'publish' == $post->post_status && empty( $post->post_password ) ) {
  55.         if ( $is_blog_public || !is_multisite() ) {
  56.            
  57.             // Record this in activity streams
  58.             $post_permalink   = get_permalink( $post_id );
  59.  
  60.             if ( is_multisite() )
  61.                 $activity_action  = sprintf( __( '%1$s published a new post, %2$s, on the site %3$s', 'buddypress' ), bp_core_get_userlink( (int) $post->post_author ), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>', '<a href="' . get_blog_option( $blog_id, 'home' ) . '">' . get_blog_option( $blog_id, 'blogname' ) . '</a>' );
  62.             else
  63.                 $activity_action  = sprintf( __( '%1$s published a new post, %2$s', 'buddypress' ), bp_core_get_userlink( (int) $post->post_author ), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>' );
  64.  
  65.             // Make sure there's not an existing entry for this post (prevent bumping)
  66.             if ( bp_is_active( 'activity' ) ) {
  67.                 $existing = bp_activity_get( array(
  68.                     'filter' => array(
  69.                         'user_id'      => (int) $post->post_author,
  70.                         'action'       => 'new_blog_post',
  71.                         'primary_id'   => $blog_id,
  72.                         'secondary_id' => $post_id,
  73.                     )
  74.                 ) );
  75.                
  76.                 if ( !empty( $existing['activities'] ) ) {
  77.                     return;
  78.                 }
  79.             }
  80.  
  81.             $activity_content = $post->post_content;
  82.  
  83.             bp_groups_record_activity( array(
  84.                 'user_id'           => (int) $post->post_author,
  85.                 'action'            => apply_filters( 'bp_blogs_activity_new_post_action',       $activity_action,  $post, $post_permalink ),
  86.                 'content'           => apply_filters( 'bp_blogs_activity_new_post_content',      $activity_content, $post, $post_permalink ),
  87.                 'primary_link'      => apply_filters( 'bp_blogs_activity_new_post_primary_link', $post_permalink,   $post_id               ),
  88.                 'type'              => 'new_blog_post',
  89.                 'item_id'           => $groupr_id,
  90.                 'secondary_item_id' => $post_id,
  91.                 'recorded_time'     => $post->post_modified_gmt,
  92.                 'hide_sitewide'     => false
  93.             ));
  94.         }
  95.  
  96.         // Update the blogs last activity
  97.         bp_blogs_update_blogmeta( $blog_id, 'last_activity', bp_core_current_time() );
  98.         groups_update_last_activity ($groupr_id);
  99.        
  100.         add_filter( 'ass_this_activity_is_important', true, $type);
  101.        
  102.     } else {
  103.         bp_blogs_remove_post( $post_id, $blog_id, $user_id );
  104.     }
  105.    
  106.     do_action( 'bp_blogs_new_blog_post', $post_id, $post, $user_id );
  107. }
  108. add_action( 'save_post', 'bp_groups_record_post', 10, 2 );
  109.  
  110. /**
  111.  * Record the activity to the actvity stream
  112.  *
  113.  * @since BuddyPress (1.0)
  114.  * @package BuddyPress
  115.  * @subpackage BlogsActivity
  116.  * @global BuddyPress $bp
  117.  * @param array $args
  118.  * @return boolean
  119.  */
  120. function bp_groups_record_activity( $args = '' ) {
  121.     global $bp;
  122.  
  123.     // Bail if activity is not active
  124.     if ( ! bp_is_active( 'activity' ) )
  125.         return false;
  126.  
  127.     $defaults = array(
  128.         'user_id'           => bp_loggedin_user_id(),
  129.         'action'            => '',
  130.         'content'           => '',
  131.         'primary_link'      => '',
  132.         'component'         => 'groups',
  133.         'type'              => false,
  134.         'item_id'           => false,
  135.         'secondary_item_id' => false,
  136.         'recorded_time'     => bp_core_current_time(),
  137.         'hide_sitewide'     => false
  138.     );
  139.  
  140.     $r = wp_parse_args( $args, $defaults );
  141.     extract( $r, EXTR_SKIP );
  142.  
  143.     // Remove large images and replace them with just one image thumbnail
  144.     if ( !empty( $content ) )
  145.         $content = bp_activity_thumbnail_content_images( $content, $primary_link );
  146.  
  147.     if ( !empty( $action ) )
  148.         $action = apply_filters( 'bp_blogs_record_activity_action', $action );
  149.  
  150.     if ( !empty( $content ) )
  151.         $content = apply_filters( 'bp_blogs_record_activity_content', bp_create_excerpt( $content ), $content );
  152.  
  153.     // Check for an existing entry and update if one exists.
  154.     $id = bp_activity_get_activity_id( array(
  155.         'user_id'           => $user_id,
  156.         'component'         => $component,
  157.         'type'              => $type,
  158.         'item_id'           => $item_id,
  159.         'secondary_item_id' => $secondary_item_id
  160.     ) );
  161.  
  162.     return bp_activity_add( array( 'id' => $id, 'user_id' => $user_id, 'action' => $action, 'content' => $content, 'primary_link' => $primary_link, 'component' => $component, 'type' => $type, 'item_id' => $item_id, 'secondary_item_id' => $secondary_item_id, 'recorded_time' => $recorded_time, 'hide_sitewide' => $hide_sitewide ) );  
  163.  
  164. add_filter( 'ass_this_activity_is_important', true, $type);
  165. }
  166.  
  167. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement