bp_blogs_update_post_activity_meta( WP_Post $post, BP_Activity_Activity $activity, object $activity_post_object )

Updates a blog post’s activity meta entry during a post edit.

Description

Parameters

$post

(Required) Post object.

$activity

(Required) Activity object.

$activity_post_object

(Required) The post type tracking args object.

Source

File: bp-blogs/bp-blogs-functions.php

function bp_blogs_update_post_activity_meta( $post, $activity, $activity_post_object ) {
	if ( empty( $activity->id ) || empty( $activity_post_object->action_id ) ) {
		return;
	}

	// Update post title in activity meta.
	$existing_title = bp_activity_get_meta( $activity->id, 'post_title' );
	if ( $post->post_title !== $existing_title ) {
		bp_activity_update_meta( $activity->id, 'post_title', $post->post_title );

		if ( ! empty( $activity_post_object->comments_tracking->action_id ) ) {
			// Now update activity meta for post comments... sigh.
			add_filter( 'comments_clauses', 'bp_blogs_comments_clauses_select_by_id' );
			$comments = get_comments( array( 'post_id' => $post->ID ) );
			remove_filter( 'comments_clauses', 'bp_blogs_comments_clauses_select_by_id' );

			if ( ! empty( $comments ) ) {
				$activity_ids = array();
				$comment_ids  = wp_list_pluck( $comments, 'comment_ID' );

				// Set up activity args.
				$args = array(
					'update_meta_cache' => false,
					'show_hidden'       => true,
					'per_page'          => 99999,
				);

				// Query for old-style "new_blog_comment" activity items.
				$args['filter'] = array(
					'object'       => $activity_post_object->comments_tracking->component_id,
					'action'       => $activity_post_object->comments_tracking->action_id,
					'primary_id'   => get_current_blog_id(),
					'secondary_id' => implode( ',', $comment_ids ),
				);

				$activities = bp_activity_get( $args );
				if ( ! empty( $activities['activities'] ) ) {
					$activity_ids = (array) wp_list_pluck( $activities['activities'], 'id' );
				}

				// Query for activity comments connected to a blog post.
				unset( $args['filter'] );
				$args['meta_query'] = array( array(
					'key'     => 'bp_blogs_' . $post->post_type . '_comment_id',
					'value'   => $comment_ids,
					'compare' => 'IN',
				) );
				$args['type'] = 'activity_comment';
				$args['display_comments'] = 'stream';

				$activities = bp_activity_get( $args );
				if ( ! empty( $activities['activities'] ) ) {
					$activity_ids = array_merge( $activity_ids, (array) wp_list_pluck( $activities['activities'], 'id' ) );
				}

				// Update activity meta for all found activity items.
				if ( ! empty( $activity_ids ) ) {
					foreach ( $activity_ids as $aid ) {
						bp_activity_update_meta( $aid, 'post_title', $post->post_title );
					}
				}

				unset( $activities, $activity_ids, $comment_ids, $comments );
			}
		}
	}

	// Add post comment status to activity meta if closed.
	if( 'closed' == $post->comment_status ) {
		bp_activity_update_meta( $activity->id, 'post_comment_status', $post->comment_status );
	} else {
		bp_activity_delete_meta( $activity->id, 'post_comment_status' );
	}
}

Changelog

Changelog
Version Description
BuddyPress 2.5.0 Add the post type tracking args object parameter BuddyPress 2.5.0 Add the post type tracking args object parameter
BuddyPress 2.2.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.