Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
03/28/2014 10:52:14 PM (10 years ago)
Author:
r-a-y
Message:

Blogs: Support post comment synchronization in the activity stream.

Previously, if a reply to a blog post was made in the activity stream,
the reply would only exist in the activity stream and not as a comment
for the blog post.

This commit:

  • Adds a blog comment whenever an activity reply is made under a blog post activity entry
  • Adds an activity comment to the blog post's activity entry whenever a blog comment is made
  • Checks to see if the blog post is open for comments (whether manually closed or due to age) (see r8189) to determine whether the activity item can be replied to
  • Handles content edit synchronization to either the post comment or the activity comment
  • Handles deletion synchronization to a degree

See #5130

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-blogs/bp-blogs-functions.php

    r8189 r8190  
    539539        $comment_link   = get_comment_link( $recorded_comment->comment_ID );
    540540
    541         // Prepare to record in activity streams
    542         if ( is_multisite() )
    543             $activity_action = sprintf( __( '%1$s commented on the post, %2$s, on the site %3$s', 'buddypress' ), bp_core_get_userlink( $user_id ), '<a href="' . $post_permalink . '">' . apply_filters( 'the_title', $recorded_comment->post->post_title ) . '</a>', '<a href="' . get_blog_option( $blog_id, 'home' ) . '">' . get_blog_option( $blog_id, 'blogname' ) . '</a>' );
    544         else
    545             $activity_action = sprintf( __( '%1$s commented on the post, %2$s', 'buddypress' ), bp_core_get_userlink( $user_id ), '<a href="' . $post_permalink . '">' . apply_filters( 'the_title', $recorded_comment->post->post_title ) . '</a>' );
    546 
    547         $activity_content   = $recorded_comment->comment_content;
    548 
    549         // Record in activity streams
    550         bp_blogs_record_activity( array(
    551             'user_id'           => $user_id,
    552             'content'           => apply_filters_ref_array( 'bp_blogs_activity_new_comment_content',      array( $activity_content, &$recorded_comment, $comment_link ) ),
    553             'primary_link'      => apply_filters_ref_array( 'bp_blogs_activity_new_comment_primary_link', array( $comment_link,     &$recorded_comment                ) ),
    554             'type'              => 'new_blog_comment',
    555             'item_id'           => $blog_id,
    556             'secondary_item_id' => $comment_id,
    557             'recorded_time'     => $recorded_comment->comment_date_gmt
    558         ) );
     541        // Setup activity args
     542        $args = array();
     543
     544        $args['user_id']       = $user_id;
     545        $args['content']       = apply_filters_ref_array( 'bp_blogs_activity_new_comment_content', array( $recorded_comment->comment_content, &$recorded_comment, $comment_link ) );
     546        $args['primary_link']  = apply_filters_ref_array( 'bp_blogs_activity_new_comment_primary_link', array( $comment_link,     &$recorded_comment ) );
     547        $args['recorded_time'] = $recorded_comment->comment_date_gmt;
     548
     549        // Setup some different activity args depending if activity commenting is
     550        // enabled or not
     551
     552        // if cannot comment, record separate activity entry
     553        // this is the old way of doing things
     554        if ( bp_disable_blogforum_comments() ) {
     555            $args['type']              = 'new_blog_comment';
     556            $args['item_id']           = $blog_id;
     557            $args['secondary_item_id'] = $comment_id;
     558
     559            // record the activity entry
     560            bp_blogs_record_activity( $args );
     561
     562        // record comment as BP activity comment under the parent 'new_blog_post'
     563        // activity item
     564        } else {
     565            // this is a comment edit
     566            // check to see if corresponding activity entry already exists
     567            if ( ! empty( $_REQUEST['action'] ) ) {
     568                $existing_activity_id = get_comment_meta( $comment_id, 'bp_activity_comment_id', true );
     569
     570                if ( ! empty( $existing_activity_id ) ) {
     571                    $args['id'] = $existing_activity_id;
     572                }
     573            }
     574
     575            // find the parent 'new_blog_post' activity entry
     576            $parent_activity_id = bp_activity_get_activity_id( array(
     577                'user_id'           => $user_id,
     578                'component'         => 'blogs',
     579                'type'              => 'new_blog_post',
     580                'item_id'           => $blog_id,
     581                'secondary_item_id' => $recorded_comment->comment_post_ID
     582            ) );
     583
     584            // we found the parent activity entry
     585            // so let's go ahead and reconfigure some activity args
     586            if ( ! empty( $parent_activity_id ) ) {
     587                // set the 'item_id' with the parent activity entry ID
     588                $args['item_id'] = $parent_activity_id;
     589
     590                // now see if the WP parent comment has a BP activity ID
     591                $comment_parent = 0;
     592                if ( ! empty( $recorded_comment->comment_parent ) ) {
     593                    $comment_parent = get_comment_meta( $recorded_comment->comment_parent, 'bp_activity_comment_id', true );
     594                }
     595
     596                // WP parent comment does not have a BP activity ID
     597                // so set to 'new_blog_post' activity ID
     598                if ( empty( $comment_parent ) ) {
     599                    $comment_parent = $parent_activity_id;
     600                }
     601
     602                $args['secondary_item_id'] = $comment_parent;
     603                $args['component']         = 'activity';
     604                $args['type']              = 'activity_comment';
     605
     606            // could not find corresponding parent activity entry
     607            // so wipe out $args array
     608            } else {
     609                $args = array();
     610            }
     611
     612            // Record in activity streams
     613            if ( ! empty( $args ) ) {
     614                // @todo should we use bp_activity_new_comment()? that function will also send
     615                // an email to people in the activity comment thread
     616                //
     617                // what if a site already has some comment email notification plugin setup?
     618                // this is why I decided to go with bp_activity_add() to avoid any conflict
     619                // with existing comment email notification plugins
     620                $comment_activity_id = bp_activity_add( $args );
     621
     622                if ( empty( $args['id'] ) ) {
     623                    // add meta to activity comment
     624                    bp_activity_update_meta( $comment_activity_id, 'bp_blogs_post_comment_id', $comment_id );
     625
     626                    // add meta to comment
     627                    add_comment_meta( $comment_id, 'bp_activity_comment_id', $comment_activity_id );
     628                }
     629            }
     630        }
    559631
    560632        // Update the blogs last active date
     
    730802    global $wpdb;
    731803
    732     // Delete activity stream item
    733     bp_blogs_delete_activity( array( 'item_id' => $wpdb->blogid, 'secondary_item_id' => $comment_id, 'type' => 'new_blog_comment' ) );
     804    // activity comments are disabled for blog posts
     805    // which means that individual activity items exist for blog comments
     806    if ( bp_disable_blogforum_comments() ) {
     807        // Delete the individual activity stream item
     808        bp_blogs_delete_activity( array(
     809            'item_id'           => $wpdb->blogid,
     810            'secondary_item_id' => $comment_id,
     811            'type'              => 'new_blog_comment'
     812        ) );
     813
     814    // activity comments are enabled for blog posts
     815    // remove the associated activity item
     816    } else {
     817        // get associated activity ID from comment meta
     818        $activity_id = get_comment_meta( $comment_id, 'bp_activity_comment_id', true );
     819
     820        // delete the associated activity comment
     821        //
     822        // also removes child post comments and associated activity comments
     823        if ( ! empty( $activity_id ) && bp_is_active( 'activity' ) ) {
     824            // fetch the activity comments for the activity item
     825            $activity = bp_activity_get( array(
     826                'in'               => $activity_id,
     827                'display_comments' => 'stream',
     828            ) );
     829
     830            // get all activity comment IDs for the pending deleted item
     831            if ( ! empty( $activity['activities'] ) ) {
     832                $activity_ids   = bp_activity_recurse_comments_activity_ids( $activity );
     833                $activity_ids[] = $activity_id;
     834
     835                // delete activity items
     836                foreach ( $activity_ids as $activity_id ) {
     837                    bp_activity_delete( array(
     838                        'id' => $activity_id
     839                    ) );
     840                }
     841
     842                // remove associated blog comments
     843                bp_blogs_remove_associated_blog_comments( $activity_ids );
     844
     845                // rebuild activity comment tree
     846                BP_Activity_Activity::rebuild_activity_comment_tree( $activity['activities'][0]->item_id );
     847            }
     848        }
     849    }
    734850
    735851    do_action( 'bp_blogs_remove_comment', $wpdb->blogid, $comment_id, bp_loggedin_user_id() );
    736852}
    737853add_action( 'delete_comment', 'bp_blogs_remove_comment' );
     854
     855/**
     856 * Removes blog comments that are associated with activity comments.
     857 *
     858 * @since BuddyPress (2.0.0)
     859 *
     860 * @see bp_blogs_remove_comment()
     861 * @see bp_blogs_sync_delete_from_activity_comment()
     862 *
     863 * @param array $activity_ids The activity IDs to check association with blog
     864 *              comments.
     865 * @param bool $force_delete Whether to force delete the comments. If false,
     866 *             comments are trashed instead.
     867 */
     868function bp_blogs_remove_associated_blog_comments( $activity_ids = array(), $force_delete = true ) {
     869    // query args
     870    $query_args = array(
     871        'meta_query' => array(
     872            array(
     873                'key'     => 'bp_activity_comment_id',
     874                'value'   => implode( ',', (array) $activity_ids ),
     875                'compare' => 'IN',
     876            )
     877        )
     878    );
     879
     880    // get comment
     881    $comment_query = new WP_Comment_Query;
     882    $comments = $comment_query->query( $query_args );
     883
     884    // found the corresponding comments
     885    // let's delete them!
     886    foreach ( $comments as $comment ) {
     887        wp_delete_comment( $comment->comment_ID, $force_delete );
     888
     889        // if we're trashing the comment, remove the meta key as well
     890        if ( empty( $force_delete ) ) {
     891            delete_comment_meta( $comment->comment_ID, 'bp_activity_comment_id' );
     892        }
     893    }
     894}
    738895
    739896/**
     
    765922
    766923    // This clause was moved in from bp_blogs_remove_comment() in BuddyPress 1.6. It handles delete/hold.
    767     if ( in_array( $new_status, array( 'delete', 'hold' ) ) )
     924    if ( in_array( $new_status, array( 'delete', 'hold' ) ) ) {
    768925        return bp_blogs_remove_comment( $comment->comment_ID );
    769926
    770927    // These clauses handle trash, spam, and un-spams.
    771     elseif ( in_array( $new_status, array( 'trash', 'spam' ) ) )
     928    } elseif ( in_array( $new_status, array( 'trash', 'spam' ) ) ) {
    772929        $action = 'spam_activity';
    773     elseif ( 'approved' == $new_status )
     930    } elseif ( 'approved' == $new_status ) {
    774931        $action = 'ham_activity';
     932    }
    775933
    776934    // Get the activity
    777     $activity_id = bp_activity_get_activity_id( array( 'component' => $bp->blogs->id, 'item_id' => get_current_blog_id(), 'secondary_item_id' => $comment->comment_ID, 'type' => 'new_blog_comment', ) );
     935    if ( bp_disable_blogforum_comments() ) {
     936        $activity_id = bp_activity_get_activity_id( array( 'component' => $bp->blogs->id, 'item_id' => get_current_blog_id(), 'secondary_item_id' => $comment->comment_ID, 'type' => 'new_blog_comment', ) );
     937    } else {
     938        $activity_id = get_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', true );
     939    }
    778940
    779941    // Check activity item exists
    780     if ( ! $activity_id ) {
    781 
     942    if ( empty( $activity_id ) ) {
    782943        // If no activity exists, but the comment has been approved, record it into the activity table.
    783         if ( 'approved' == $new_status )
     944        if ( 'approved' == $new_status ) {
    784945            return bp_blogs_record_comment( $comment->comment_ID, true );
     946        }
    785947
    786948        return;
Note: See TracChangeset for help on using the changeset viewer.