Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
08/18/2014 04:51:11 PM (10 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.

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.