bp_blogs_format_activity_action_new_blog_comment( string $action, object $activity )

Format ‘new_blog_comment’ activity actions.

Description

Parameters

$action

(Required) Static activity action.

$activity

(Required) Activity data object.

Return

(string) Constructed activity action.

Source

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

function bp_blogs_format_activity_action_new_blog_comment( $action, $activity ) {
	/**
	 * When the comment is published we are faking an activity object
	 * to which we add 4 properties :
	 * - the post url
	 * - the post title
	 * - the blog url
	 * - the blog name
	 * This is done to build the 'post link' part of the activity
	 * action string.
	 * NB: in this case the activity has not yet been created.
	 */

	$blog_url = false;

	// Try to get the blog url from the activity object
	if ( isset( $activity->blog_url ) ) {
		$blog_url = $activity->blog_url;
	} else {
		$blog_url = bp_blogs_get_blogmeta( $activity->item_id, 'url' );
	}

	$blog_name = false;

	// Try to get the blog name from the activity object
	if ( isset( $activity->blog_name ) ) {
		$blog_name = $activity->blog_name;
	} else {
		$blog_name = bp_blogs_get_blogmeta( $activity->item_id, 'name' );
	}

	if ( empty( $blog_url ) || empty( $blog_name ) ) {
		$blog_url  = get_home_url( $activity->item_id );
		$blog_name = get_blog_option( $activity->item_id, 'blogname' );

		bp_blogs_update_blogmeta( $activity->item_id, 'url', $blog_url );
		bp_blogs_update_blogmeta( $activity->item_id, 'name', $blog_name );
	}

	$post_url = false;

	// Try to get the post url from the activity object
	if ( isset( $activity->post_url ) ) {
		$post_url = $activity->post_url;

	/**
	 * The post_url property is not set, we need to build the url
	 * thanks to the post id which is also saved as the secondary
	 * item id property of the activity object.
	 */
	} elseif ( ! empty( $activity->id ) ) {
		$post_url = bp_activity_get_meta( $activity->id, 'post_url' );
	}

	$post_title = false;

	// Should be the case when the comment has just been published
	if ( isset( $activity->post_title ) ) {
		$post_title = $activity->post_title;

	// If activity already exists try to get the post title from activity meta
	} elseif ( ! empty( $activity->id ) ) {
		$post_title = bp_activity_get_meta( $activity->id, 'post_title' );
	}

	// Should only be empty at the time of post creation.
	if ( empty( $post_url ) || empty( $post_title ) ) {
		switch_to_blog( $activity->item_id );

		$comment = get_comment( $activity->secondary_item_id );

		if ( ! empty( $comment->comment_post_ID ) ) {
			$post_url = add_query_arg( 'p', $comment->comment_post_ID, trailingslashit( $blog_url ) );
			bp_activity_update_meta( $activity->id, 'post_url', $post_url );

			$post = get_post( $comment->comment_post_ID );

			if ( is_a( $post, 'WP_Post' ) ) {
				$post_title = $post->post_title;
				bp_activity_update_meta( $activity->id, 'post_title', $post_title );
			}
		}

		restore_current_blog();
	}

	$post_link = '<a href="' . esc_url( $post_url ) . '">' . $post_title . '</a>';
	$user_link = bp_core_get_userlink( $activity->user_id );

	if ( is_multisite() ) {
		$action  = sprintf( __( '%1$s commented on the post, %2$s, on the site %3$s', 'buddyboss' ), $user_link, $post_link, '<a href="' . esc_url( $blog_url ) . '">' . esc_html( $blog_name ) . '</a>' );
	} else {
		$action  = sprintf( __( '%1$s commented on the post, %2$s', 'buddyboss' ), $user_link, $post_link );
	}

	// Legacy filter - requires the comment object.
	if ( has_filter( 'bp_blogs_activity_new_comment_action' ) ) {
		switch_to_blog( $activity->item_id );
		$comment = get_comment( $activity->secondary_item_id );
		restore_current_blog();

		if ( ! empty( $comment ) && ! is_wp_error( $comment ) ) {
			$action = apply_filters( 'bp_blogs_activity_new_comment_action', $action, $comment, $post_url . '#' . $activity->secondary_item_id );
		}
	}

	/**
	 * Filters the new blog comment action for the new blog.
	 *
	 * @since BuddyPress 2.0.0
	 *
	 * @param string $action   Constructed activity action.
	 * @param object $activity Activity data object.
	 */
	return apply_filters( 'bp_blogs_format_activity_action_new_blog_comment', $action, $activity );
}

Changelog

Changelog
Version Description
BuddyPress 2.0.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.