Skip to:
Content

BuddyPress.org

Changeset 8944


Ignore:
Timestamp:
08/18/2014 04:51:11 PM (6 years ago)
Author:
r-a-y
Message:

Add activity meta when recording a blog post or blog comment.

This commit:

  • Adds the post_title and post_url information as activity meta when recording a blog post or blog comment into the activity table.
  • Updates the post_title and post_url activity meta whenever a blog post is updated.
  • Adds a unit test.

This addresses issues with showing the correct blog post information in the
activity loop for blog post and blog comment activity items.

Props r-a-y, DJPaul, boonebgorges.

Fixes #5609.

Location:
trunk
Files:
2 edited

Legend:

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

    r8942 r8944  
    524524            $activity_content = $post->post_content;
    525525
    526             bp_blogs_record_activity( array(
     526            $activity_id = bp_blogs_record_activity( array(
    527527                'user_id'           => (int) $post->post_author,
    528528                'content'           => apply_filters( 'bp_blogs_activity_new_post_content',      $activity_content, $post, $post_permalink ),
     
    532532                'secondary_item_id' => $post_id,
    533533                'recorded_time'     => $post->post_date_gmt,
    534             ));
     534            ) );
     535
     536            // save post title in activity meta
     537            bp_activity_update_meta( $activity_id, 'post_title', $post->post_title );
     538            bp_activity_update_meta( $activity_id, 'post_url',   $post_permalink );
    535539        }
    536540
     
    584588    $activity->save();
    585589
     590    // update post title in activity meta
     591    $existing_title = bp_activity_get_meta( $activity_id, 'post_title' );
     592    if ( $post->post_title !== $existing_title ) {
     593        bp_activity_update_meta( $activity_id, 'post_title', $post->post_title );
     594
     595        // now update activity meta for post comments... sigh
     596        $comment_ids = get_comments( array( 'post_id' => $post->ID, 'fields' => 'ids' ) );
     597
     598        if ( ! empty( $comment_ids ) ) {
     599            $activity_ids = array();
     600
     601            // setup activity args
     602            $args = array(
     603                'update_meta_cache' => false,
     604                'show_hidden'       => true,
     605                'per_page'          => 99999,
     606            );
     607
     608            // query for old-style "new_blog_comment" activity items
     609            $args['filter'] = array(
     610                'object'     => buddypress()->blogs->id,
     611                'action'     => 'new_blog_comment',
     612                'secondary_id' => implode( ',', $comment_ids ),
     613            );
     614
     615            $activities = bp_activity_get( $args );
     616            if ( ! empty( $activities['activities'] ) ) {
     617                $activity_ids = (array) wp_list_pluck( $activities['activities'], 'id' );
     618            }
     619
     620            // query for activity comments connected to a blog post
     621            unset( $args['filter'] );
     622            $args['meta_query'] = array( array(
     623                'key'     => 'bp_blogs_post_comment_id',
     624                'value'   => $comment_ids,
     625                'compare' => 'IN',
     626            ) );
     627            $args['type'] = 'activity_comment';
     628            $args['display_comments'] = 'stream';
     629
     630            $activities = bp_activity_get( $args );
     631            if ( ! empty( $activities['activities'] ) ) {
     632                $activity_ids = array_merge( $activity_ids, (array) wp_list_pluck( $activities['activities'], 'id' ) );
     633            }
     634
     635            // update activity meta for all found activity items
     636            if ( ! empty( $activity_ids ) ) {
     637                foreach ( $activity_ids as $aid ) {
     638                    bp_activity_update_meta( $aid, 'post_title', $post->post_title );
     639                }
     640            }
     641
     642            unset( $activities, $activity_ids, $comment_ids );
     643        }
     644    }
     645
    586646    // add post comment status to activity meta if closed
    587647    if( 'closed' == $post->comment_status ) {
     
    681741
    682742            // record the activity entry
    683             bp_blogs_record_activity( $args );
     743            $activity_id = bp_blogs_record_activity( $args );
     744
     745            // add some post info in activity meta
     746            bp_activity_update_meta( $activity_id, 'post_title', $recorded_comment->post->post_title );
     747            bp_activity_update_meta( $activity_id, 'post_url',   add_query_arg( 'p', $recorded_comment->post->ID, home_url( '/' ) ) );
    684748
    685749        // record comment as BP activity comment under the parent 'new_blog_post'
     
    745809                    // add meta to activity comment
    746810                    bp_activity_update_meta( $comment_activity_id, 'bp_blogs_post_comment_id', $comment_id );
     811                    bp_activity_update_meta( $comment_activity_id, 'post_title', $recorded_comment->post->post_title );
     812                    bp_activity_update_meta( $comment_activity_id, 'post_url', add_query_arg( 'p', $recorded_comment->post->ID, home_url( '/' ) ) );
    747813
    748814                    // add meta to comment
  • trunk/tests/phpunit/testcases/blogs/functions.php

    r8491 r8944  
    395395    }
    396396
     397    /**
     398     * @group bp_blogs_catch_transition_post_status
     399     */
     400    public function test_update_blog_post_and_new_blog_comment_and_activity_comment_meta() {
     401        // save the current user and override logged-in user
     402        $old_user = get_current_user_id();
     403        $u = $this->create_user();
     404        $this->set_current_user( $u );
     405        $userdata = get_userdata( $u );
     406
     407        // create the blog post
     408        $post_id = $this->factory->post->create( array(
     409            'post_status' => 'publish',
     410            'post_type' => 'post',
     411            'post_title' => 'First title',
     412        ) );
     413
     414        // remove comment flood protection temporarily
     415        add_filter( 'comment_flood_filter', '__return_false' );
     416
     417        // let's use activity comments instead of single "new_blog_comment" activity items
     418        add_filter( 'bp_disable_blogforum_comments', '__return_false' );
     419        $c1 = wp_new_comment( array(
     420            'comment_post_ID'      => $post_id,
     421            'comment_author'       => $userdata->user_nicename,
     422            'comment_author_url'   => 'http://buddypress.org',
     423            'comment_author_email' => $userdata->user_email,
     424            'comment_content'      => 'this is a blog comment',
     425            'comment_type'         => '',
     426            'comment_parent'       => 0,
     427            'user_id'              => $u,
     428        ) );
     429        remove_filter( 'bp_disable_blogforum_comments', '__return_false' );
     430
     431        // let's also add a "new_blog_comment" activity entry
     432        $c2 = wp_new_comment( array(
     433            'comment_post_ID'      => $post_id,
     434            'comment_author'       => $userdata->user_nicename,
     435            'comment_author_url'   => 'http://buddypress.org',
     436            'comment_author_email' => $userdata->user_email,
     437            'comment_content'      => 'this is another blog comment',
     438            'comment_type'         => '',
     439            'comment_parent'       => 0,
     440            'user_id'              => $u,
     441        ) );
     442
     443        // bring back flood protection
     444        remove_filter( 'comment_flood_filter', '__return_false' );
     445
     446        // update the initial blog post
     447        wp_update_post( array(
     448            'ID'        => $post_id,
     449            'post_title' => 'Second title',
     450        ) );
     451
     452        // grab the activity ID for the activity comment
     453        $a1 = bp_activity_get_activity_id( array(
     454            'type'              => 'activity_comment',
     455            'display_comments'  => 'stream',
     456            'meta_query'        => array( array(
     457                'key'     => 'bp_blogs_post_comment_id',
     458                'value'   => $c1,
     459            ) )
     460        ) );
     461
     462        // grab the activity ID for the blog comment
     463        $a2 = bp_activity_get_activity_id( array(
     464            'component'         => buddypress()->blogs->id,
     465            'type'              => 'new_blog_comment',
     466            'secondary_item_id' => $c2,
     467        ) );
     468
     469        // see if blog comment activity meta matches the post items
     470        $this->assertEquals( 'Second title', bp_activity_get_meta( $a1, 'post_title' ) );
     471        $this->assertEquals( add_query_arg( 'p', $post_id, home_url( '/' ) ), bp_activity_get_meta( $a1, 'post_url' ) );
     472
     473        $this->assertEquals( 'Second title', bp_activity_get_meta( $a2, 'post_title' ) );
     474        $this->assertEquals( add_query_arg( 'p', $post_id, home_url( '/' ) ), bp_activity_get_meta( $a2, 'post_url' ) );
     475
     476        // reset
     477        $this->set_current_user( $old_user );
     478    }
     479
    397480    protected function activity_exists_for_post( $post_id ) {
    398481        $a = bp_activity_get( array(
Note: See TracChangeset for help on using the changeset viewer.