Skip to:
Content

BuddyPress.org

Changeset 1794


Ignore:
Timestamp:
09/04/2009 10:42:52 PM (15 years ago)
Author:
apeatling
Message:

Fixed all comment/post/blog recording and deletion functions. Fixes #772

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-blogs.php

    r1790 r1794  
    264264
    265265function bp_blogs_delete_activity( $args = true ) {
    266     if ( function_exists('bp_activity_delete') ) {
     266    if ( function_exists('bp_activity_delete_by_item_id') ) {
    267267        extract($args);
    268         bp_activity_delete_by_item_id( $item_id, $component_name, $component_action, $user_id, $secondary_item_id );
     268       
     269        bp_activity_delete_by_item_id( array(
     270            'item_id' => $item_id,
     271            'component_name' => $component_name,
     272            'component_action' => $component_action,
     273            'user_id' => $user_id,
     274            'secondary_item_id' => $secondary_item_id
     275        ) );
    269276    }
    270277}
     
    300307}
    301308
    302 function bp_blogs_record_blog( $blog_id, $user_id ) {
     309function bp_blogs_record_blog( $blog_id, $user_id, $no_activity = false ) {
    303310    global $bp;
    304311   
     
    312319    $recorded_blog->user_id = $user_id;
    313320    $recorded_blog->blog_id = $blog_id;
    314    
     321
    315322    $recorded_blog_id = $recorded_blog->save();
    316323   
     
    319326    bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'last_activity', time() );
    320327   
    321     if ( (int)$_POST['blog_public'] )
    322         $is_private = 0;
    323     else
    324         $is_private = 1;
    325    
    326     /* Record this in activity streams */
    327     bp_blogs_record_activity( array(
    328         'user_id' => $recorded_blog->user_id,
    329         'content' => sprintf( __( '%s created the blog %s', 'buddypress'), bp_core_get_userlink( $recorded_blog->user_id ), '<a href="' . get_blog_option( $recorded_blog->blog_id, 'siteurl' ) . '">' . attribute_escape( $name ) . '</a>' ),
    330         'primary_link' => get_blog_option( $recorded_blog->blog_id, 'siteurl' ),
    331         'component_action' => 'new_blog',
    332         'item_id' => $recorded_blog_id
    333     ) );
    334    
    335     do_action( 'bp_blogs_new_blog', $recorded_blog, $is_private, $is_recorded );
     328    /* Only record this activity if the blog is public */
     329    if ( (int)$_POST['blog_public'] && !$no_activity ) {   
     330        /* Record this in activity streams */
     331        bp_blogs_record_activity( array(
     332            'user_id' => $recorded_blog->user_id,
     333            'content' => sprintf( __( '%s created the blog %s', 'buddypress'), bp_core_get_userlink( $recorded_blog->user_id ), '<a href="' . get_blog_option( $recorded_blog->blog_id, 'siteurl' ) . '">' . attribute_escape( $name ) . '</a>' ),
     334            'primary_link' => get_blog_option( $recorded_blog->blog_id, 'siteurl' ),
     335            'component_action' => 'new_blog',
     336            'item_id' => $recorded_blog->blog_id
     337        ) );
     338    }
     339   
     340    do_action( 'bp_blogs_new_blog', &$recorded_blog, $is_private, $is_recorded );
    336341}
    337342add_action( 'wpmu_new_blog', 'bp_blogs_record_blog', 10, 2 );
    338343
    339 function bp_blogs_record_post( $post_id, $blog_id = false, $user_id = false ) {
     344function bp_blogs_record_post( $post_id, $post, $user_id = false ) {
    340345    global $bp, $wpdb;
    341346   
    342347    $post_id = (int)$post_id;
    343     $post = get_post($post_id);
     348    $blog_id = (int)$wpdb->blogid;
    344349   
    345350    if ( !$user_id )
    346351        $user_id = (int)$post->post_author;
    347        
    348     if ( !$blog_id )
    349         $blog_id = (int)$wpdb->blogid;
     352
    350353   
    351354    /* This is to stop infinate loops with Donncha's sitewide tags plugin */
     
    364367            $recorded_post->blog_id = $blog_id;
    365368            $recorded_post->post_id = $post_id;
    366             $recorded_post->date_created = strtotime( $post->post_date_gmt );
     369            $recorded_post->date_created = strtotime( $post->post_date );
    367370           
    368371            $recorded_post_id = $recorded_post->save();
     
    372375            $post_permalink = bp_post_get_permalink( $post, $blog_id );
    373376
     377            $activity_content = sprintf( __( '%s wrote a new blog post: %s', 'buddypress' ), bp_core_get_userlink( (int)$post->post_author ), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>' );
     378            $activity_content .= "<blockquote>" . bp_create_excerpt( $post->post_content ) . "</blockquote>";
     379           
    374380            /* Record this in activity streams */
    375381            bp_blogs_record_activity( array(
    376382                'user_id' => (int)$post->post_author,
    377                 'content' => sprintf( __( '%s wrote a new blog post: %s', 'buddypress' ), bp_core_get_userlink( (int)$post->post_author ), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>' ),
     383                'content' => $activity_content,
    378384                'primary_link' => $post_permalink,
    379385                'component_action' => 'new_blog_post',
    380                 'item_id' => $recorded_post->id,
    381                 'recorded_time' => $recorded_post->date_created
     386                'item_id' => $recorded_post_id,
     387                'recorded_time' => strtotime( $post->post_date )
    382388            ) );
    383389        }
     
    385391        $existing_post = new BP_Blogs_Post( null, $blog_id, $post_id );
    386392
    387         /**
    388          *  Delete the recorded post if:
    389          *  - The status is no longer "published"
    390          *  - The post is password protected
    391          */
    392         if ( 'publish' != $post->post_status || '' != $post->post_password )
    393             bp_blogs_remove_post( $post_id, $blog_id );
    394        
    395         // Check to see if the post author has changed.
    396         if ( (int)$existing_post->user_id != (int)$post->post_author ) {
     393        /* Delete and the activity stream item as we are probably going to re-add it later with new info. */
     394        bp_blogs_delete_activity( array( 'item_id' => $existing_post->id, 'component_name' => $bp->blogs->slug, 'component_action' => 'new_blog_post' ) );
     395
     396        /* Delete the recorded post if the status is not published or it is password protected */
     397        if ( 'publish' != $post->post_status || '' != $post->post_password ) {
     398            return bp_blogs_remove_post( $post_id, $blog_id, $existing_post );         
     399       
     400        /* If the post author has changed, delete the post and re-add it. */
     401        } else if ( (int)$existing_post->user_id != (int)$post->post_author ) {
    397402            // Delete the existing recorded post
    398             bp_blogs_remove_post( $post_id, $blog_id );
     403            bp_blogs_remove_post( $post_id, $blog_id, $existing_post );
    399404           
    400405            // Re-record the post with the new author.
    401             bp_blogs_record_post( $post_id );
     406            bp_blogs_record_post( $post_id );               
    402407        }
    403408       
     409        /* Now re-record the post in the activity streams */       
    404410        $post_permalink = bp_post_get_permalink( $post, $blog_id );
    405411       
    406         /* Delete and re-add the activity stream item to reflect potential content changes. */
    407         bp_blogs_delete_activity( array( 'item_id' => $recorded_post->id, 'component_name' => $bp->blogs->slug, 'component_action' => 'new_blog_post', 'user_id' => $recorded_post->user_id ) );
    408 
     412
     413        $activity_content = sprintf( __( '%s wrote a new blog post: %s', 'buddypress' ), bp_core_get_userlink( (int)$post->post_author ), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>' );
     414        $activity_content .= "<blockquote>" . bp_create_excerpt( $post->post_content ) . "</blockquote>";
     415       
    409416        /* Record this in activity streams */
    410417        bp_blogs_record_activity( array(
    411418            'user_id' => (int)$post->post_author,
    412             'content' => sprintf( __( '%s wrote a new blog post: %s', 'buddypress' ), bp_core_get_userlink( (int)$post->post_author ), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>' ),
     419            'content' => $activity_content,
    413420            'primary_link' => $post_permalink,
    414421            'component_action' => 'new_blog_post',
    415422            'item_id' => $existing_post->id,
    416             'recorded_time' => $existing_post->date_created
     423            'recorded_time' => strtotime( $post->post_date )
    417424        ) );
    418425    }
     
    420427    do_action( 'bp_blogs_new_blog_post', $existing_post, $is_private, $is_recorded );
    421428}
    422 add_action( 'publish_post', 'bp_blogs_record_post' );
    423 add_action( 'edit_post', 'bp_blogs_record_post' );
     429add_action( 'save_post', 'bp_blogs_record_post', 10, 2 );
    424430
    425431function bp_blogs_record_comment( $comment_id, $is_approved ) {
     
    430436       
    431437    $comment = get_comment($comment_id);
     438    $comment->post = get_post( $comment->comment_post_ID );
    432439   
    433440    /* Get the user_id from the author email. */
     
    443450    $recorded_comment->comment_id = $comment_id;
    444451    $recorded_comment->comment_post_id = $comment->comment_post_ID;
    445     $recorded_comment->date_created = strtotime( $comment->comment_date_gmt );
     452    $recorded_comment->date_created = strtotime( $comment->comment_date );
    446453
    447454    $recorded_commment_id = $recorded_comment->save();
     
    450457
    451458    $comment_link = bp_post_get_permalink( $comment->post, $recorded_comment->blog_id );
    452     $content = sprintf( __( '%s commented on the blog post %s', 'buddypress' ), bp_core_get_userlink( $user_id ), '<a href="' . $comment_link . '#comment-' . $comment->comment_ID . '">' . $comment->post->post_title . '</a>' ) . ' <span class="time-since">%s</span>';         
     459    $activity_content = sprintf( __( '%s commented on the blog post %s', 'buddypress' ), bp_core_get_userlink( $user_id ), '<a href="' . $comment_link . '#comment-' . $comment->comment_ID . '">' . $comment->post->post_title . '</a>' );         
     460    $activity_content .= '<blockquote>' . bp_create_excerpt( $comment->comment_content ) . '</blockquote>';
     461
     462    /* Record this in activity streams */
     463    bp_blogs_record_activity( array(
     464        'user_id' => $recorded_comment->user_id,
     465        'content' => $activity_content,
     466        'primary_link' => $comment_link,
     467        'component_action' => 'new_blog_comment',
     468        'item_id' => $comment_id,
     469        'secondary_item_id' => $recorded_comment->blog_id,
     470        'recorded_time' =>  $recorded_comment->date_created
     471    ) );
     472   
     473    return $recorded_comment;
     474}
     475add_action( 'comment_post', 'bp_blogs_record_comment', 10, 2 );
     476
     477function bp_blogs_approve_comment( $comment_id, $comment ) {
     478    global $bp, $wpdb;
     479
     480    $recorded_comment = bp_blogs_record_comment( $comment_id, true );
     481    $comment = get_comment($comment_id);
     482    $comment->post = get_post( $comment->comment_post_ID );
     483   
     484    bp_blogs_delete_activity( array( 'item_id' => $comment_id, 'secondary_item_id' => $recorded_comment->blog_id, 'component_name' => $bp->blogs->slug, 'component_action' => 'new_blog_comment' ) );
     485
     486    $comment_link = bp_post_get_permalink( $comment->post, $comment->blog_id );
     487    $content = sprintf( __( '%s commented on the blog post %s', 'buddypress' ), bp_core_get_userlink( $recorded_comment->user_id ), '<a href="' . $comment_link . '#comment-' . $comment->comment_ID . '">' . $comment->post->post_title . '</a>' );           
    453488    $content .= '<blockquote>' . bp_create_excerpt( $comment->comment_content ) . '</blockquote>';
    454489
     
    459494        'primary_link' => $comment_link,
    460495        'component_action' => 'new_blog_comment',
    461         'item_id' => $recorded_comment->blog_id,
    462         'recorded_time' =>  $recorded_comment->date_created
    463     ) );
    464 }
    465 add_action( 'comment_post', 'bp_blogs_record_comment', 10, 2 );
    466 
    467 function bp_blogs_approve_comment( $comment_id, $comment ) {
    468     global $bp, $wpdb;
    469 
    470     $recorded_comment = bp_blogs_record_comment( $comment_id, true );
    471     $comment = get_comment($comment_id);
    472 
    473     bp_blogs_delete_activity( array( 'item_id' => $recorded_comment->blog_id, 'secondary_item_id' => $recorded_commment_id, 'component_name' => $bp->blogs->slug, 'component_action' => 'new_blog_comment', 'user_id' => $recorded_comment->user_id ) );
    474 
    475     $comment_link = bp_post_get_permalink( $comment->post, $comment->blog_id );
    476     $content = sprintf( __( '%s commented on the blog post %s', 'buddypress' ), bp_core_get_userlink( $user_id ), '<a href="' . $comment_link . '#comment-' . $comment->comment_ID . '">' . $comment->post->post_title . '</a>' ) . ' <span class="time-since">%s</span>';         
    477     $content .= '<blockquote>' . bp_create_excerpt( $comment->comment_content ) . '</blockquote>';
    478 
    479     /* Record this in activity streams */
    480     bp_blogs_record_activity( array(
    481         'user_id' => $recorded_comment->user_id,
    482         'content' => $content,
    483         'primary_link' => $comment_link,
    484         'component_action' => 'new_blog_comment',
    485         'item_id' => $recorded_comment->blog_id,
     496        'item_id' => $comment_id,
     497        'secondary_item_id' => $recorded_comment->blog_id,
    486498        'recorded_time' =>  $recorded_comment->date_created
    487499    ) );
     
    498510function bp_blogs_add_user_to_blog( $user_id, $role, $blog_id ) {
    499511    if ( $role != 'subscriber' ) {
    500         bp_blogs_record_blog( $blog_id, $user_id );
     512        bp_blogs_record_blog( $blog_id, $user_id, $no_activity = true );
    501513    }
    502514}
     
    516528   
    517529    // Delete activity stream item
    518     bp_blogs_delete_activity( array( 'item_id' => $blog_id, 'component_name' => $bp->blogs->slug, 'component_action' => 'new_blog', 'user_id' => $bp->loggedin_user->id ) );
     530    bp_blogs_delete_activity( array( 'item_id' => $blog_id, 'component_name' => $bp->blogs->slug, 'component_action' => 'new_blog' ) );
    519531   
    520532    do_action( 'bp_blogs_remove_blog', $blog_id );
     
    531543
    532544    // Delete activity stream item
    533     bp_blogs_delete_activity( array( 'item_id' => $blog_id, 'component_name' => $bp->blogs->slug, 'component_action' => 'new_blog', 'user_id' => $current_user->ID ) );
     545    bp_blogs_delete_activity( array( 'item_id' => $blog_id, 'component_name' => $bp->blogs->slug, 'component_action' => 'new_blog' ) );
    534546
    535547    do_action( 'bp_blogs_remove_blog_for_user', $blog_id, $user_id );
     
    537549add_action( 'remove_user_from_blog', 'bp_blogs_remove_blog_for_user', 10, 2 );
    538550
    539 function bp_blogs_remove_post( $post_id ) {
     551function bp_blogs_remove_post( $post_id, $blog_id = false, $existing_post = false ) {
    540552    global $current_blog, $bp;
    541553
    542554    $post_id = (int)$post_id;
    543     $blog_id = (int)$current_blog->blog_id;
    544    
    545     $post = new BP_Blogs_Post( null, $blog_id, $post_id );
     555   
     556    if ( !$blog_id )
     557        $blog_id = (int)$current_blog->blog_id;
     558   
     559    if ( !$existing_post )
     560        $existing_post = new BP_Blogs_Post( null, $blog_id, $post_id );
    546561
    547562    // Delete post from the bp_blogs table
     
    549564       
    550565    // Delete activity stream item
    551     bp_blogs_delete_activity( array( 'item_id' => $post->id, 'component_name' => $bp->blogs->slug, 'component_action' => 'new_blog_post', 'user_id' => $post->user_id ) );
     566    bp_blogs_delete_activity( array( 'item_id' => $existing_post->id, 'component_name' => $bp->blogs->slug, 'component_action' => 'new_blog_post' ) );
    552567
    553568    do_action( 'bp_blogs_remove_post', $blog_id, $post_id, $post->user_id );
     
    562577
    563578    // Delete activity stream item
    564     bp_blogs_delete_activity( array( 'item_id' => $recorded_comment->blog_id, 'secondary_item_id' => $recorded_comment->id, 'component_name' => $bp->blogs->slug, 'component_action' => 'new_blog_comment', 'user_id' => $recorded_comment->user_id ) );
     579    bp_blogs_delete_activity( array( 'item_id' => $comment_id, 'secondary_item_id' => $recorded_comment->blog_id, 'component_name' => $bp->blogs->slug, 'component_action' => 'new_blog_comment' ) );
    565580
    566581    do_action( 'bp_blogs_remove_comment', $blog_id, $comment_id, $bp->loggedin_user->id );
     
    577592
    578593    // Delete activity stream item
    579     bp_blogs_delete_activity( array( 'item_id' => $blog_id, 'component_name' => $bp->blogs->slug, 'component_action' => false, 'user_id' => $bp->loggedin_user->id ) );
     594    bp_blogs_delete_activity( array( 'item_id' => $blog_id, 'component_name' => $bp->blogs->slug, 'component_action' => false ) );
    580595
    581596    do_action( 'bp_blogs_remove_data_for_blog', $blog_id );
Note: See TracChangeset for help on using the changeset viewer.