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
Description
Parameters
- $activity_id
-
(Required) ID of recorded activity, or false if sync is active.
- $comment
-
(Optional) The comment object.
Default value: null
- $activity_args
-
(Optional) Array of activity arguments.
Default value: array()
- $activity_post_object
-
(Optional) The post type tracking args object.
Default value: null
Return
(WP_Error|bool|int) Returns false if no activity, the activity id otherwise.
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 feeds
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 BuddyPress 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 BuddyPress 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;
}
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.