| | 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 | */ |
| | 440 | function 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 | } |
| | 458 | add_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 | */ |
| | 464 | function 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 | |
| | 484 | add_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 | */ |
| | 489 | function 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 | |
| | 496 | add_action('untrash_post', 'bp_blogs_post_restored', 10, 1); |
| | 497 | |
| | 498 | /***** end of enhancement *****/ |
| | 499 | |