Guest User

Buddypress record custom post type to activity stream

a guest
Mar 7th, 2012
669
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. function custom_record_activity( $post_id, $post, $user_id = false ) {
  4.    
  5.     global $bp, $wpdb;
  6.    
  7.     //check for multisite install
  8.     if(!is_multisite()) return false;
  9.    
  10.     $post_id = (int)$post_id;
  11.     $blog_id = (int)$wpdb->blogid;
  12.    
  13.     if ( !$user_id )
  14.         $user_id = (int)$post->post_author;
  15.    
  16.     /* This is to stop infinite loops with Donncha's sitewide tags plugin */
  17.     if ( (int)$bp->site_options['tags_blog_id'] == (int)$blog_id ) return false;
  18.    
  19.     /* Don't record this if it's a regular post */
  20.     if ( $post->post_type == 'post' ) return false;
  21.    
  22.     if ( ( 'publish' == $post->post_status ) && ( '' == $post->post_password ) ) {
  23.        
  24.         if ( (int)get_blog_option( $blog_id, 'blog_public' ) || !bp_core_is_multisite() ) {
  25.            
  26.             /* Record this in activity streams */
  27.             $post_permalink = get_permalink( $post_id );
  28.    
  29.             $activity_action = sprintf( __( '%1$s wrote 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>', get_blog_option($blog_id, 'blogname') );
  30.             $activity_content = $post->post_content;
  31.    
  32.             bp_blogs_record_activity( array(
  33.                 'user_id' => (int)$post->post_author,
  34.                 'action' => apply_filters( 'bp_blogs_activity_new_post_action', $activity_action, &$post, $post_permalink ),
  35.                 'content' => apply_filters( 'bp_blogs_activity_new_post_content', $activity_content, &$post, $post_permalink ),
  36.                 'primary_link' => apply_filters( 'bp_blogs_activity_new_post_primary_link', $post_permalink, $post_id ),
  37.                 'type' => 'new_blog_post',
  38.                 'item_id' => $blog_id,
  39.                 'secondary_item_id' => $post_id,
  40.                 'recorded_time' => $post->post_date_gmt
  41.             ));
  42.         }
  43.     } else
  44.         bp_blogs_remove_post( $post_id, $blog_id );
  45.    
  46.     bp_blogs_update_blogmeta( $blog_id, 'last_activity', bp_core_current_time() );
  47.    
  48.     do_action( 'bp_blogs_new_blog_post', $post_id, $post, $user_id );
  49. }
  50.      
  51. add_action( 'save_post', 'custom_record_activity', 10, 2 );
RAW Paste Data