bp_blogs_comment_sync_activity_comment( int|bool $activity_id, WP_Comment|null $comment = null, array $activity_args = array(), object|null $activity_post_object = null )

Update Activity and blogs meta and eventually sync comment with activity comment


Parameters Parameters

$activity_id

(int|bool) (Required) ID of recorded activity, or false if sync is active.

$comment

(WP_Comment|null) (Optional) The comment object.

Default value: null

$activity_args

(array) (Optional) Array of activity arguments.

Default value: array()

$activity_post_object

(object|null) (Optional) The post type tracking args object.

Default value: null


Top ↑

Return Return

(WP_Error|bool|int) Returns false if no activity, the activity id otherwise.


Source Source

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

function bp_blogs_comment_sync_activity_comment( &$activity_id, $comment = null, $activity_args = array(), $activity_post_object = null ) {
	if ( empty( $activity_args ) || empty( $comment->post->ID ) || empty( $activity_post_object->comment_action_id ) ) {
		return false;
	}

	// Set the current blog id.
	$blog_id = get_current_blog_id();

	// These activity metadatas are used to build the new_blog_comment action string.
	if ( ! empty( $activity_id ) && ! empty( $activity_args['item_id'] ) && 'new_blog_comment' === $activity_post_object->comment_action_id ) {
		// Add some post info in activity meta.
		bp_activity_update_meta( $activity_id, 'post_title', $comment->post->post_title );
		bp_activity_update_meta( $activity_id, 'post_url',   esc_url_raw( add_query_arg( 'p', $comment->post->ID, home_url( '/' ) ) ) );
	}

	// Sync comment - activity comment.
	if ( ! bp_disable_blogforum_comments() ) {

		if ( ! empty( $_REQUEST['action'] ) ) {
			$existing_activity_id = get_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', true );

			if ( ! empty( $existing_activity_id ) ) {
				$activity_args['id'] = $existing_activity_id;
			}
		}

		if ( empty( $activity_post_object ) ) {
			$activity_post_object = bp_activity_get_post_type_tracking_args( $comment->post->post_type );
		}

		if ( isset( $activity_post_object->action_id ) && isset( $activity_post_object->component_id ) ) {
			// Find the parent 'new_post_type' activity entry.
			$parent_activity_id = bp_activity_get_activity_id( array(
				'component'         => $activity_post_object->component_id,
				'type'              => $activity_post_object->action_id,
				'item_id'           => $blog_id,
				'secondary_item_id' => $comment->comment_post_ID
			) );

			// Try to create a new activity item for the parent blog post.
			if ( empty( $parent_activity_id ) ) {
				$parent_activity_id = bp_activity_post_type_publish( $comment->post->ID, $comment->post );
			}
		}

		// We found the parent activity entry
		// so let's go ahead and reconfigure some activity args.
		if ( ! empty( $parent_activity_id ) ) {
			// Set the parent activity entry ID.
			$activity_args['activity_id'] = $parent_activity_id;

			// Now see if the WP parent comment has a BP activity ID.
			$comment_parent = 0;
			if ( ! empty( $comment->comment_parent ) ) {
				$comment_parent = get_comment_meta( $comment->comment_parent, 'bp_activity_comment_id', true );
			}

			// WP parent comment does not have a BP activity ID
			// so set to 'new_' . post_type activity ID.
			if ( empty( $comment_parent ) ) {
				$comment_parent = $parent_activity_id;
			}

			$activity_args['parent_id']         = $comment_parent;
			$activity_args['skip_notification'] = true;

		// Could not find corresponding parent activity entry
		// so wipe out $args array.
		} else {
			$activity_args = array();
		}

		// Record in activity streams.
		if ( ! empty( $activity_args ) ) {
			$activity_id = bp_activity_new_comment( $activity_args );

			if ( empty( $activity_args['id'] ) ) {
				// The activity metadata to inform about the corresponding comment ID.
				bp_activity_update_meta( $activity_id, "bp_blogs_{$comment->post->post_type}_comment_id", $comment->comment_ID );

				// The comment metadata to inform about the corresponding activity ID.
				add_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', $activity_id );

				// These activity metadatas are used to build the new_blog_comment action string.
				if ( 'new_blog_comment' === $activity_post_object->comment_action_id ) {
					bp_activity_update_meta( $activity_id, 'post_title', $comment->post->post_title );
					bp_activity_update_meta( $activity_id, 'post_url', esc_url_raw( add_query_arg( 'p', $comment->post->ID, home_url( '/' ) ) ) );
				}
			}

			/**
			 * Fires after an activity comment is added from a WP post comment.
			 *
			 * @since 2.6.0
			 *
			 * @param int        $activity_id          The activity comment ID.
			 * @param WP_Comment $post_type_comment    WP Comment object.
			 * @param array      $activity_args        Activity comment arguments.
			 * @param object     $activity_post_object The post type tracking args object.
			 */
			do_action( 'bp_blogs_comment_sync_activity_comment', $activity_id, $comment, $activity_args, $activity_post_object );
		}
	}

	// Update the blogs last active date
	bp_blogs_update_blogmeta( $blog_id, 'last_activity', bp_core_current_time() );

	if ( 'new_blog_comment' === $activity_post_object->comment_action_id ) {
		/**
		 * Fires after BuddyPress has recorded metadata about a published blog post comment.
		 *
		 * @since 2.5.0
		 *
		 * @param int     $value    Comment ID of the blog post comment being recorded.
		 * @param WP_Post $post  WP_Comment object for the current blog post.
		 * @param string  $value ID of the user associated with the current blog post comment.
		 */
		do_action( 'bp_blogs_new_blog_comment', $comment->comment_ID, $comment, bp_loggedin_user_id() );
	}

	return $activity_id;
}


Top ↑

Changelog Changelog

Changelog
Version Description
2.5.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.