Skip to:
Content

BuddyPress.org

Changeset 1388


Ignore:
Timestamp:
04/22/2009 06:17:08 PM (17 years ago)
Author:
apeatling
Message:

Fixing problems with blog comment activity stream recording when setting comment status from approved/unapproved/spam.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-blogs.php

    r1366 r1388  
    77if ( !defined( 'BP_BLOGS_SLUG' ) )
    88        define ( 'BP_BLOGS_SLUG', 'blogs' );
    9 
    10 /* Define the total number of posts to keep track of for each user. */
    11 if ( !defined( 'TOTAL_RECORDED_POSTS' ) )
    12         define ( 'TOTAL_RECORDED_POSTS', 150 );
    13 
    14 /* Define the total number of comments to keep track of for each user. */       
    15 if ( !defined( 'TOTAL_RECORDED_COMMENTS' ) )   
    16         define ( 'TOTAL_RECORDED_COMMENTS', 500 );
    179
    1810require ( 'bp-blogs/bp-blogs-classes.php' );
     
    428420                if ( 'publish' == $post->post_status && '' == $post->post_password ) {
    429421                       
    430                         /**
    431                          * Check how many recorded posts there are for the user. If we are
    432                          * at the max, then delete the oldest recorded post first.
    433                          */
    434                         if ( BP_Blogs_Post::get_total_recorded_for_user( $user_id ) >= TOTAL_RECORDED_POSTS )
    435                                 BP_Blogs_Post::delete_oldest( $user_id );
    436                        
    437422                        $recorded_post = new BP_Blogs_Post;
    438423                        $recorded_post->user_id = $user_id;
     
    474459
    475460                /* Delete and re-add the activity stream item to reflect potential content changes. */
    476                 if ( strtotime( $recorded_post->date_created ) >= strtotime( "-24 hours" ) ) {
    477                         bp_blogs_delete_activity( array( 'item_id' => $recorded_post->id, 'component_name' => 'blogs', 'component_action' => 'new_blog_post', 'user_id' => $recorded_post->user_id ) );
    478                         bp_blogs_record_activity( array( 'item_id' => $recorded_post->id, 'component_name' => 'blogs', 'component_action' => 'new_blog_post', 'is_private' => bp_blogs_is_blog_hidden( $recorded_post->blog_id ), 'user_id' => $recorded_post->user_id ) );
    479                 }
     461                bp_blogs_delete_activity( array( 'item_id' => $recorded_post->id, 'component_name' => 'blogs', 'component_action' => 'new_blog_post', 'user_id' => $recorded_post->user_id ) );
     462                bp_blogs_record_activity( array( 'item_id' => $recorded_post->id, 'component_name' => 'blogs', 'component_action' => 'new_blog_post', 'is_private' => bp_blogs_is_blog_hidden( $recorded_post->blog_id ), 'user_id' => $recorded_post->user_id, 'recorded_time' => strtotime( $post->post_date ) ) );
    480463        }
    481464
     
    483466}
    484467add_action( 'publish_post', 'bp_blogs_record_post' );
    485 add_action( 'edit_post', 'bp_blogs_record_post' );
    486 
    487 
    488 function bp_blogs_record_comment( $comment_id, $post_id = false, $blog_id = false, $from_ajax = false ) {
    489         global $bp, $wpdb, $current_user;
    490 
     468
     469function bp_blogs_record_comment( $comment_id, $is_approved ) {
     470        global $wpdb;
     471       
     472        if ( !$is_approved )
     473                return false;
     474               
     475        $comment = get_comment($comment_id);
     476       
     477        /* Get the user_id from the author email. */
     478        $user = get_user_by_email( $comment->comment_author_email );
     479        $user_id = (int)$user->ID;
     480       
     481        if ( !$user_id )
     482                return false;
     483
     484        $recorded_comment = new BP_Blogs_Comment;
     485        $recorded_comment->user_id = $user_id;
     486        $recorded_comment->blog_id = $wpdb->blogid;
     487        $recorded_comment->comment_id = $comment_id;
     488        $recorded_comment->comment_post_id = $comment->comment_post_ID;
     489        $recorded_comment->date_created = strtotime( $comment->comment_date );
     490
     491        $recorded_commment_id = $recorded_comment->save();
     492       
     493        bp_blogs_update_blogmeta( $recorded_comment->blog_id, 'last_activity', time() );
     494        bp_blogs_record_activity( array( 'item_id' => $recorded_comment->blog_id, 'secondary_item_id' => $recorded_commment_id, 'component_name' => 'blogs', 'component_action' => 'new_blog_comment', 'is_private' => $is_private, 'user_id' => $recorded_comment->user_id, 'recorded_time' => $recorded_comment->date_created ) );   
     495}
     496add_action( 'comment_post', 'bp_blogs_record_comment', 10, 2 );
     497
     498function bp_blogs_approve_comment( $comment_id, $comment ) {
     499        global $bp, $wpdb;
     500
     501        $recorded_comment = bp_blogs_record_comment( $comment_id, true );
     502
     503        bp_blogs_delete_activity( array( 'item_id' => $recorded_comment->blog_id, 'secondary_item_id' => $recorded_commment_id, 'component_name' => 'blogs', 'component_action' => 'new_blog_comment', 'user_id' => $recorded_comment->user_id ) );
     504        bp_blogs_record_activity( array( 'item_id' => $recorded_comment->blog_id, 'secondary_item_id' => $recorded_commment_id, 'component_name' => 'blogs', 'component_action' => 'new_blog_comment', 'is_private' => $is_private, 'user_id' => $recorded_comment->user_id, 'recorded_time' => $recorded_comment->date_created ) );   
     505}
     506add_action( 'comment_approved_', 'bp_blogs_approve_comment', 10, 2 );
     507
     508function bp_blogs_unapprove_comment( $comment_id, $status = false ) {
     509        if ( 'spam' == $status || !$status )
     510                bp_blogs_remove_comment( $comment_id );         
     511}
     512add_action( 'comment_unapproved_', 'bp_blogs_unapprove_comment' );
     513add_action( 'wp_set_comment_status', 'bp_blogs_unapprove_comment', 10, 2 );
     514
     515function bp_blogs_add_user_to_blog( $user_id, $role, $blog_id ) {
     516        if ( $role != 'subscriber' ) {
     517                bp_blogs_record_blog( $blog_id, $user_id );
     518        }
     519}
     520add_action( 'add_user_to_blog', 'bp_blogs_add_user_to_blog', 10, 3 );
     521
     522function bp_blogs_remove_user_from_blog( $user_id, $blog_id ) {
     523        bp_blogs_remove_blog_for_user( $user_id, $blog_id );
     524}
     525add_action( 'remove_user_from_blog', 'bp_blogs_remove_user_from_blog', 10, 2 );
     526
     527function bp_blogs_remove_blog( $blog_id ) {
     528        global $bp;
     529       
    491530        if ( !$bp ) {
    492531                bp_core_setup_globals();
    493532                bp_blogs_setup_globals();
    494533        }
    495 
    496         $comment = get_comment($comment_id);
    497        
    498         /* Get the user_id from the author email. */
    499         $user = get_user_by_email( $comment->comment_author_email );
    500         $user_id = (int)$user->ID;
    501 
    502         /* Only record a comment if it is by a registered user. */
    503         if ( $user_id ) {
    504                 $comment_id = (int) $comment_id;
    505                
    506                 if ( !$post_id )
    507                         $post_id = (int) $comment->comment_post_ID;
    508 
    509                 if ( !$blog_id )
    510                         $blog_id = (int) $wpdb->blogid;
    511                        
    512                 /**
    513                  * Check how many recorded posts there are for the user. If we are
    514                  * at the max, then delete the oldest recorded post first.
    515                  */
    516                 if ( BP_Blogs_Comment::get_total_recorded_for_user() >= TOTAL_RECORDED_COMMENTS )
    517                         BP_Blogs_Comment::delete_oldest();
    518 
    519                 if ( !$is_recorded = BP_Blogs_Comment::is_recorded( $comment_id, $post_id, $blog_id ) ) {
    520                         if ( !$comment->comment_approved || 'spam' == $comment->comment_approved )
    521                                 return false;
    522 
    523                         $recorded_comment = new BP_Blogs_Comment;
    524                         $recorded_comment->user_id = $user_id;
    525                         $recorded_comment->blog_id = $blog_id;
    526                         $recorded_comment->comment_id = $comment_id;
    527                         $recorded_comment->comment_post_id = $post_id;
    528                         $recorded_comment->date_created = strtotime( $comment->comment_date );
    529 
    530                         $recorded_commment_id = $recorded_comment->save();
    531                        
    532                         bp_blogs_update_blogmeta( $recorded_comment->blog_id, 'last_activity', time() );
    533                        
    534                         $is_private = bp_blogs_is_blog_hidden( $recorded_comment->blog_id );
    535                        
    536                         // Record in activity streams
    537                         bp_blogs_record_activity( array( 'item_id' => $recorded_comment->blog_id, 'secondary_item_id' => $recorded_commment_id, 'component_name' => 'blogs', 'component_action' => 'new_blog_comment', 'is_private' => $is_private, 'user_id' => $recorded_comment->user_id, 'recorded_time' => $recorded_comment->date_created ) );
    538                 } else {
    539                         /**
    540                          * Check to see if the post have previously been recorded.
    541                          * If the post status has changed from public to private then we need
    542                          * to remove the record of the post.
    543                          */
    544                         if ( !$comment->comment_approved || 'spam' == $comment->comment_approved )
    545                                 BP_Blogs_Comment::delete( $comment_id, $blog_id );     
    546                 }
    547         }
    548 
    549         do_action( 'bp_blogs_new_blog_comment', $recorded_comment, $is_private, $is_recorded );
    550 }
    551 add_action( 'comment_post', 'bp_blogs_record_comment', 10, 2 );
    552 add_action( 'edit_comment', 'bp_blogs_record_comment', 10, 2 );
    553 
    554 function bp_blogs_add_user_to_blog( $user_id, $role, $blog_id ) {
    555         if ( $role != 'subscriber' ) {
    556                 bp_blogs_record_blog( $blog_id, $user_id );
    557         }
    558 }
    559 add_action( 'add_user_to_blog', 'bp_blogs_add_user_to_blog', 10, 3 );
    560 
    561 function bp_blogs_remove_user_from_blog( $user_id, $blog_id ) {
    562         bp_blogs_remove_blog_for_user( $user_id, $blog_id );
    563 }
    564 add_action( 'remove_user_from_blog', 'bp_blogs_remove_user_from_blog', 10, 2 );
    565        
    566 function bp_blogs_modify_comment( $comment_id, $comment_status ) {
    567         global $bp;
     534       
     535        $blog_id = (int)$blog_id;
     536
     537        BP_Blogs_Blog::delete_blog_for_all( $blog_id );
     538       
     539        // Delete activity stream item
     540        bp_blogs_delete_activity( array( 'item_id' => $blog_id, 'component_name' => 'blogs', 'component_action' => 'new_blog', 'user_id' => $bp->loggedin_user->id ) );
     541       
     542        do_action( 'bp_blogs_remove_blog', $blog_id );
     543}
     544add_action( 'delete_blog', 'bp_blogs_remove_blog' );
     545
     546function bp_blogs_remove_blog_for_user( $user_id, $blog_id ) {
     547        global $current_user;
     548       
     549        $blog_id = (int)$blog_id;
     550        $user_id = (int)$user_id;
     551
     552        BP_Blogs_Blog::delete_blog_for_user( $blog_id, $user_id );
     553
     554        // Delete activity stream item
     555        bp_blogs_delete_activity( array( 'item_id' => $blog_id, 'component_name' => 'blogs', 'component_action' => 'new_blog', 'user_id' => $current_user->ID ) );
     556
     557        do_action( 'bp_blogs_remove_blog_for_user', $blog_id, $user_id );
     558}
     559add_action( 'remove_user_from_blog', 'bp_blogs_remove_blog_for_user', 10, 2 );
     560
     561function bp_blogs_remove_post( $post_id ) {
     562        global $current_blog, $bp;
    568563       
    569564        if ( !$bp ) {
     
    572567        }
    573568       
    574         $comment = get_comment($comment_id);
    575        
    576         // This is backwards, but it's just the way things work with WP AJAX.
    577         if ( $comment->comment_approved ) {
    578                 bp_blogs_remove_comment( $comment_id );
    579         } else {
    580                 bp_blogs_record_comment( $comment_id, false, false, true );             
    581         }
    582 }
    583 add_action( 'wp_set_comment_status', 'bp_blogs_modify_comment', 10, 2 );
    584 
    585 function bp_blogs_remove_blog( $blog_id ) {
    586         global $bp;
     569        $post_id = (int)$post_id;
     570        $blog_id = (int)$current_blog->blog_id;
     571       
     572        $post = new BP_Blogs_Post( null, $blog_id, $post_id );
     573
     574        // Delete post from the bp_blogs table
     575        BP_Blogs_Post::delete( $post_id, $blog_id );
     576               
     577        // Delete activity stream item
     578        bp_blogs_delete_activity( array( 'item_id' => $post->blog_id, 'secondary_item_id' => $post->id, 'component_name' => 'blogs', 'component_action' => 'new_blog_post', 'user_id' => $post->user_id ) );
     579
     580        do_action( 'bp_blogs_remove_post', $blog_id, $post_id, $post->user_id );
     581}
     582add_action( 'delete_post', 'bp_blogs_remove_post' );
     583
     584function bp_blogs_remove_comment( $comment_id ) {
     585        global $wpdb, $bp;
    587586       
    588587        if ( !$bp ) {
     
    591590        }
    592591       
    593         $blog_id = (int)$blog_id;
    594 
    595         BP_Blogs_Blog::delete_blog_for_all( $blog_id );
    596        
     592        $recorded_comment = new BP_Blogs_Comment( false, $wpdb->blogid, $comment_id );
     593        BP_Blogs_Comment::delete( $comment_id, $wpdb->blogid );
     594
    597595        // Delete activity stream item
    598         bp_blogs_delete_activity( array( 'item_id' => $blog_id, 'component_name' => 'blogs', 'component_action' => 'new_blog', 'user_id' => $bp->loggedin_user->id ) );
    599        
    600         do_action( 'bp_blogs_remove_blog', $blog_id );
    601 }
    602 add_action( 'delete_blog', 'bp_blogs_remove_blog' );
    603 
    604 function bp_blogs_remove_blog_for_user( $user_id, $blog_id ) {
    605         global $current_user;
    606        
    607         $blog_id = (int)$blog_id;
    608         $user_id = (int)$user_id;
    609 
    610         BP_Blogs_Blog::delete_blog_for_user( $blog_id, $user_id );
    611 
    612         // Delete activity stream item
    613         bp_blogs_delete_activity( array( 'item_id' => $blog_id, 'component_name' => 'blogs', 'component_action' => 'new_blog', 'user_id' => $current_user->ID ) );
    614 
    615         do_action( 'bp_blogs_remove_blog_for_user', $blog_id, $user_id );
    616 }
    617 add_action( 'remove_user_from_blog', 'bp_blogs_remove_blog_for_user', 10, 2 );
    618 
    619 function bp_blogs_remove_post( $post_id ) {
    620         global $current_blog, $bp;
    621        
    622         if ( !$bp ) {
    623                 bp_core_setup_globals();
    624                 bp_blogs_setup_globals();
    625         }
    626        
    627         $post_id = (int)$post_id;
    628         $blog_id = (int)$current_blog->blog_id;
    629        
    630         $post = new BP_Blogs_Post( null, $blog_id, $post_id );
    631 
    632         // Delete post from the bp_blogs table
    633         BP_Blogs_Post::delete( $post_id, $blog_id );
    634                
    635         // Delete activity stream item
    636         bp_blogs_delete_activity( array( 'item_id' => $post->blog_id, 'secondary_item_id' => $post->id, 'component_name' => 'blogs', 'component_action' => 'new_blog_post', 'user_id' => $post->user_id ) );
    637 
    638         do_action( 'bp_blogs_remove_post', $blog_id, $post_id, $post->user_id );
    639 }
    640 add_action( 'delete_post', 'bp_blogs_remove_post' );
    641 
    642 function bp_blogs_remove_comment( $comment_id ) {
    643         global $current_blog, $bp;
    644        
    645         if ( !$bp ) {
    646                 bp_core_setup_globals();
    647                 bp_blogs_setup_globals();
    648         }
    649        
    650         $comment_id = (int)$comment_id;
    651         $blog_id = (int)$current_blog->blog_id;
    652        
    653         BP_Blogs_Comment::delete( $comment_id, $blog_id );     
    654 
    655         // Delete activity stream item
    656         bp_blogs_delete_activity( array( 'item_id' => $blog_id, 'secondary_item_id' => $comment_id, 'component_name' => 'blogs', 'component_action' => 'new_blog_comment', 'user_id' => $bp->loggedin_user->id ) );
     596        bp_blogs_delete_activity( array( 'item_id' => $recorded_comment->blog_id, 'secondary_item_id' => $recorded_comment->id, 'component_name' => 'blogs', 'component_action' => 'new_blog_comment', 'user_id' => $recorded_comment->user_id ) );
    657597
    658598        do_action( 'bp_blogs_remove_comment', $blog_id, $comment_id, $bp->loggedin_user->id );
  • trunk/bp-blogs/bp-blogs-classes.php

    r1366 r1388  
    486486        var $date_created;
    487487       
    488         function bp_blogs_comment( $id = null ) {
     488        function bp_blogs_comment( $id = false, $blog_id = false, $comment_id = false ) {
    489489                global $bp, $wpdb;
    490490
     
    492492                        $user_id = $bp->displayed_user->id;
    493493                       
    494                 if ( $id ) {
     494                if ( $id || ( !$id && $blog_id && $comment_id ) ) {
    495495                        $this->id = $id;
    496                         $this->populate( $id );
    497                 }
    498         }
    499 
    500         function populate( $id ) {
    501                 global $wpdb, $bp;
    502                
    503                 $comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->blogs->table_name_blog_comments} WHERE id = %d", $this->id ) );
    504 
     496                        $this->blog_id = $blog_id;
     497                        $this->comment_id = $comment_id;
     498                        $this->populate();
     499                }
     500        }
     501
     502        function populate() {
     503                global $wpdb, $bp;
     504               
     505                if ( $this->id )
     506                        $comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->blogs->table_name_blog_comments} WHERE id = %d", $this->id ) );
     507                else
     508                        $comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->blogs->table_name_blog_comments} WHERE blog_id = %d AND comment_id = %d", (int)$this->blog_id, (int)$this->comment_id ) );
     509               
    505510                $this->comment_id = $comment->comment_id;
    506511                $this->user_id = $comment->user_id;
Note: See TracChangeset for help on using the changeset viewer.