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
1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 | 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.