bp_activity_new_comment( array|string $args = '' )
Add an activity comment.
Description
Parameters
- $args
-
(Optional) An array of arguments.
- 'id'
(int) Optional. Pass an ID to update an existing comment. - 'content'
(string) The content of the comment. - 'user_id'
(int) Optional. The ID of the user making the comment. Defaults to the ID of the logged-in user. - 'activity_id'
(int) The ID of the "root" activity item, ie the oldest ancestor of the comment. - 'parent_id'
(int) Optional. The ID of the parent activity item, ie the item to which the comment is an immediate reply. If not provided, this value defaults to the $activity_id. - 'primary_link'
(string) Optional. the primary link for the comment. Defaults to an empty string. - 'skip_notification'
(bool) Optional. false to send a comment notification, false otherwise. Defaults to false. - 'error_type'
(string) Optional. Error type. Either 'bool' or 'wp_error'. Default: 'bool'.
Default value: ''
- 'id'
Return
(WP_Error|bool|int) The ID of the comment on success, otherwise false.
Source
File: bp-activity/bp-activity-functions.php
function bp_activity_new_comment( $args = '' ) {
$bp = buddypress();
$r = wp_parse_args( $args, array(
'id' => false,
'content' => false,
'user_id' => bp_loggedin_user_id(),
'activity_id' => false, // ID of the root activity item.
'parent_id' => false, // ID of a parent comment (optional).
'primary_link' => '',
'skip_notification' => false,
'error_type' => 'bool'
) );
// Error type is boolean; need to initialize some variables for backpat.
if ( 'bool' === $r['error_type'] ) {
if ( empty( $bp->activity->errors ) ) {
$bp->activity->errors = array();
}
}
// Default error message.
$feedback = __( 'There was an error posting your reply. Please try again.', 'buddyboss' );
// Bail if missing necessary data.
if ( empty( $r['content'] ) || empty( $r['user_id'] ) || empty( $r['activity_id'] ) ) {
$error = new WP_Error( 'missing_data', $feedback );
if ( 'wp_error' === $r['error_type'] ) {
return $error;
// Backpat.
} else {
$bp->activity->errors['new_comment'] = $error;
return false;
}
}
// Maybe set current activity ID as the parent.
if ( empty( $r['parent_id'] ) ) {
$r['parent_id'] = $r['activity_id'];
}
$activity_id = $r['activity_id'];
// Get the parent activity.
$activity = new BP_Activity_Activity( $activity_id );
// Bail if the parent activity does not exist.
if ( empty( $activity->date_recorded ) ) {
$error = new WP_Error( 'missing_activity', __( 'The item you were replying to no longer exists.', 'buddyboss' ) );
if ( 'wp_error' === $r['error_type'] ) {
return $error;
// Backpat.
} else {
$bp->activity->errors['new_comment'] = $error;
return false;
}
}
// Check to see if the parent activity is hidden, and if so, hide this comment publicly.
$is_hidden = $activity->hide_sitewide ? 1 : 0;
/**
* Filters the content of a new comment.
*
* @since BuddyPress 1.2.0
* @since BuddyPress 3.0.0 Added $context parameter to disambiguate from bp_get_activity_comment_content().
*
* @param string $r Content for the newly posted comment.
* @param string $context This filter's context ("new").
*/
$comment_content = apply_filters( 'bp_activity_comment_content', $r['content'], 'new' );
// Insert the activity comment.
$comment_id = bp_activity_add( array(
'id' => $r['id'],
'content' => $comment_content,
'component' => buddypress()->activity->id,
'type' => 'activity_comment',
'primary_link' => $r['primary_link'],
'user_id' => $r['user_id'],
'item_id' => $activity_id,
'secondary_item_id' => $r['parent_id'],
'hide_sitewide' => $is_hidden,
'error_type' => $r['error_type']
) );
// Bail on failure.
if ( false === $comment_id || is_wp_error( $comment_id ) ) {
return $comment_id;
}
// Comment caches are stored only with the top-level item.
wp_cache_delete( $activity_id, 'bp_activity_comments' );
// Walk the tree to clear caches for all parent items.
$clear_id = $r['parent_id'];
while ( $clear_id != $activity_id ) {
$clear_object = new BP_Activity_Activity( $clear_id );
wp_cache_delete( $clear_id, 'bp_activity' );
$clear_id = intval( $clear_object->secondary_item_id );
}
wp_cache_delete( $activity_id, 'bp_activity' );
if ( empty( $r[ 'skip_notification' ] ) ) {
/**
* Fires near the end of an activity comment posting, before the returning of the comment ID.
* Sends a notification to the user @see bp_activity_new_comment_notification_helper().
*
* @since BuddyPress 1.2.0
*
* @param int $comment_id ID of the newly posted activity comment.
* @param array $r Array of parsed comment arguments.
* @param BP_Activity_Activity $activity Activity item being commented on.
*/
do_action( 'bp_activity_comment_posted', $comment_id, $r, $activity );
} else {
/**
* Fires near the end of an activity comment posting, before the returning of the comment ID.
* without sending a notification to the user
*
* @since BuddyPress 2.5.0
*
* @param int $comment_id ID of the newly posted activity comment.
* @param array $r Array of parsed comment arguments.
* @param BP_Activity_Activity $activity Activity item being commented on.
*/
do_action( 'bp_activity_comment_posted_notification_skipped', $comment_id, $r, $activity );
}
if ( empty( $comment_id ) ) {
$error = new WP_Error( 'comment_failed', $feedback );
if ( 'wp_error' === $r['error_type'] ) {
return $error;
// Backpat.
} else {
$bp->activity->errors['new_comment'] = $error;
}
}
return $comment_id;
}
Changelog
| Version | Description |
|---|---|
| BuddyPress 2.6.0 Added 'error_type' parameter to $args. | BuddyPress 2.6.0 Added 'error_type' parameter to $args. |
| BuddyPress 1.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.