Skip to:
Content

BuddyPress.org

Ticket #4304: bp-blogs-functions.diff

File bp-blogs-functions.diff, 3.7 KB (added by imath, 14 years ago)
  • bp-blogs/bp-blogs-functions.php

     
    201201                        }
    202202
    203203                        $activity_content = $post->post_content;
     204                       
     205                        // if the post was in trash then we take the post_date_gmt to make sure the comments will not be older than the post ;)
     206                        $post_date = isset( $bp->blogs->post_restored ) ? $post->post_date_gmt : $post->post_modified_gmt ;
    204207
    205208                        bp_blogs_record_activity( array(
    206209                                'user_id'           => (int) $post->post_author,
     
    210213                                'type'              => 'new_blog_post',
    211214                                'item_id'           => $blog_id,
    212215                                'secondary_item_id' => $post_id,
    213                                 'recorded_time'     => $post->post_modified_gmt
     216                                'recorded_time'     => $post_date
    214217                        ));
    215218                }
    216219
    217                 // Update the blogs last activity
    218                 bp_blogs_update_blogmeta( $blog_id, 'last_activity', bp_core_current_time() );
     220                // Update the blogs last activity only if the post wasnt in trash
     221                if( !$bp->blogs->post_restored )
     222                        bp_blogs_update_blogmeta( $blog_id, 'last_activity', bp_core_current_time() );
     223                       
     224                $bp->blogs->post_restored = false;
    219225        } else {
    220226                bp_blogs_remove_post( $post_id, $blog_id, $user_id );
    221227        }
     
    423429}
    424430add_action( 'delete_post', 'bp_blogs_remove_post' );
    425431
     432/***** beginning of enhancement *****/
     433
     434/**
     435* If the admin move the post to trash, then the activity is deleted
     436* but if this post had been commented, then the new_blog_comment activities are still there
     437* If someone clicks on the action link of one of these activities, then we get a 404
     438* by hooking trashed_post_comments with this function we avoid this.
     439*/
     440function bp_blogs_handle_trashed_post_comments( $post_id ) {
     441        global $wpdb;
     442       
     443        $comments = $wpdb->get_results( $wpdb->prepare("SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id) );
     444        if ( empty($comments) )
     445                return;
     446       
     447        foreach ( $comments as $comment ) {
     448               
     449                bp_blogs_delete_activity( array( 'item_id' => $wpdb->blogid, 'secondary_item_id' => $comment->comment_ID, 'type' => 'new_blog_comment' ) );
     450
     451                do_action( 'bp_blogs_handle_trashed_post_comment', $wpdb->blogid, $post_id, $comment->comment_ID );
     452               
     453        }
     454               
     455        do_action( 'bp_blogs_handle_trashed_post_comments', $wpdb->blogid, $post_id, $comments );
     456       
     457}
     458add_action( 'trashed_post_comments', 'bp_blogs_handle_trashed_post_comments', 10, 1 );
     459
     460/**
     461* Now if the admin changes his mind, and move back the post from trash to published
     462* then we can record the comment activities we deleted earlier
     463*/
     464function bp_blogs_handle_untrashed_post_comments( $post_id ) {
     465        global $wpdb;
     466       
     467        $comments = $wpdb->get_results( $wpdb->prepare("SELECT comment_ID, comment_approved FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id) );
     468        if ( empty($comments) )
     469                return;
     470       
     471        foreach ( $comments as $comment ) {
     472               
     473                if( $comment->comment_approved == 1 )
     474                        bp_blogs_record_comment( $comment->comment_ID, true );
     475
     476                do_action( 'bp_blogs_handle_untrashed_post_comment', $wpdb->blogid, $post_id, $comment->comment_ID );
     477               
     478        }
     479               
     480        do_action( 'bp_blogs_handle_untrashed_post_comments', $wpdb->blogid, $post_id, $comments );
     481       
     482}
     483
     484add_action('untrashed_post_comments', 'bp_blogs_handle_untrashed_post_comments', 10, 1 );
     485
     486/**
     487* now if the post is restored, the activity is taking the
     488*/
     489function bp_blogs_post_restored( $post_id ) {
     490        global $bp;
     491       
     492        if( 'publish' == get_post_meta($post_id, '_wp_trash_meta_status', true) )
     493                $bp->blogs->post_restored = true;
     494}
     495
     496add_action('untrash_post', 'bp_blogs_post_restored', 10, 1);
     497
     498/***** end of enhancement *****/
     499
    426500function bp_blogs_remove_comment( $comment_id ) {
    427501        global $wpdb;
    428502