BP_Activity_Activity::get_activity_comments( int $activity_id, int $left, int $right, string $spam = 'ham_only', int $top_level_parent_id )
Get activity comments that are associated with a specific activity ID.
Description
Parameters
- $activity_id
-
(Required) Activity ID to fetch comments for.
- $left
-
(Required) Left-most node boundary.
- $right
-
(Required) Right-most node boundary.
- $spam
-
(Optional) 'ham_only' (default), 'spam_only' or 'all'.
Default value: 'ham_only'
- $top_level_parent_id
-
(Optional) The id of the root-level parent activity item.
Return
(array) The updated activities with nested comments.
Source
File: bp-activity/classes/class-bp-activity-activity.php
public static function get_activity_comments( $activity_id, $left, $right, $spam = 'ham_only', $top_level_parent_id = 0 ) {
global $wpdb;
if ( empty( $top_level_parent_id ) ) {
$top_level_parent_id = $activity_id;
}
$comments = wp_cache_get( $activity_id, 'bp_activity_comments' );
// We store the string 'none' to cache the fact that the
// activity item has no comments.
if ( 'none' === $comments ) {
$comments = false;
// A true cache miss.
} elseif ( empty( $comments ) ) {
$bp = buddypress();
// Select the user's fullname with the query.
if ( bp_is_active( 'xprofile' ) ) {
$fullname_select = ", pd.value as user_fullname";
$fullname_from = ", {$bp->profile->table_name_data} pd ";
$fullname_where = "AND pd.user_id = a.user_id AND pd.field_id = 1";
// Prevent debug errors.
} else {
$fullname_select = $fullname_from = $fullname_where = '';
}
// Don't retrieve activity comments marked as spam.
if ( 'ham_only' == $spam ) {
$spam_sql = 'AND a.is_spam = 0';
} elseif ( 'spam_only' == $spam ) {
$spam_sql = 'AND a.is_spam = 1';
} else {
$spam_sql = '';
}
// Legacy query - not recommended.
/**
* Filters if BuddyPress should use the legacy activity query.
*
* @since BuddyPress 2.0.0
*
* @param bool $value Whether or not to use the legacy query.
* @param BP_Activity_Activity $value Magic method referring to currently called method.
* @param array $func_args Array of the method's argument list.
*/
if ( apply_filters( 'bp_use_legacy_activity_query', false, __METHOD__, func_get_args() ) ) {
/**
* Filters the MySQL prepared statement for the legacy activity query.
*
* @since BuddyPress 1.5.0
*
* @param string $value Prepared statement for the activity query.
* @param int $activity_id Activity ID to fetch comments for.
* @param int $left Left-most node boundary.
* @param int $right Right-most node boundary.
* @param string $spam_sql SQL Statement portion to differentiate between ham or spam.
*/
$sql = apply_filters( 'bp_activity_comments_user_join_filter', $wpdb->prepare( "SELECT a.*, u.user_email, u.user_nicename, u.user_login, u.display_name{$fullname_select} FROM {$bp->activity->table_name} a, {$wpdb->users} u{$fullname_from} WHERE u.ID = a.user_id {$fullname_where} AND a.type = 'activity_comment' {$spam_sql} AND a.item_id = %d AND a.mptt_left > %d AND a.mptt_left < %d ORDER BY a.date_recorded ASC", $top_level_parent_id, $left, $right ), $activity_id, $left, $right, $spam_sql );
$descendants = $wpdb->get_results( $sql );
// We use the mptt BETWEEN clause to limit returned
// descendants to the correct part of the tree.
} else {
$sql = $wpdb->prepare( "SELECT id FROM {$bp->activity->table_name} a WHERE a.type = 'activity_comment' {$spam_sql} AND a.item_id = %d and a.mptt_left > %d AND a.mptt_left < %d ORDER BY a.date_recorded ASC", $top_level_parent_id, $left, $right );
$descendant_ids = $wpdb->get_col( $sql );
$descendants = self::get_activity_data( $descendant_ids );
$descendants = self::append_user_fullnames( $descendants );
}
$ref = array();
// Loop descendants and build an assoc array.
foreach ( (array) $descendants as $d ) {
$d->children = array();
// If we have a reference on the parent.
if ( isset( $ref[ $d->secondary_item_id ] ) ) {
$ref[ $d->secondary_item_id ]->children[ $d->id ] = $d;
$ref[ $d->id ] =& $ref[ $d->secondary_item_id ]->children[ $d->id ];
// If we don't have a reference on the parent, put in the root level.
} else {
$comments[ $d->id ] = $d;
$ref[ $d->id ] =& $comments[ $d->id ];
}
}
// Calculate depth for each item.
foreach ( $ref as &$r ) {
$depth = 1;
$parent_id = $r->secondary_item_id;
while ( $parent_id !== $r->item_id ) {
$depth++;
// When display_comments=stream, the parent comment may not be part of the
// returned results, so we manually fetch it.
if ( empty( $ref[ $parent_id ] ) ) {
$direct_parent = new BP_Activity_Activity( $parent_id );
if ( isset( $direct_parent->secondary_item_id ) ) {
// If the direct parent is not an activity update, that means we've reached
// the parent activity item (eg. new_blog_post).
if ( 'activity_update' !== $direct_parent->type ) {
$parent_id = $r->item_id;
} else {
$parent_id = $direct_parent->secondary_item_id;
}
} else {
// Something went wrong. Short-circuit the depth calculation.
$parent_id = $r->item_id;
}
} else {
$parent_id = $ref[ $parent_id ]->secondary_item_id;
}
}
$r->depth = $depth;
}
// If we cache a value of false, it'll count as a cache
// miss the next time the activity comments are fetched.
// Storing the string 'none' is a hack workaround to
// avoid unnecessary queries.
if ( false === $comments ) {
$cache_value = 'none';
} else {
$cache_value = $comments;
}
wp_cache_set( $activity_id, $cache_value, 'bp_activity_comments' );
}
return $comments;
}
Changelog
| Version | Description |
|---|---|
| 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.