Skip to:
Content

BuddyPress.org

Changeset 6501


Ignore:
Timestamp:
11/09/2012 03:19:23 PM (13 years ago)
Author:
djpaul
Message:

Add template functions to get an activity item's comments' author IDs

  • bp_activity_get_comments_user_ids() returns the user IDs of everyone who's written an activity comment on the current activity item.
  • bp_activity_comments_user_avatars() uses the above to output the users' avatars, linked to their profile page, into <LI> tags.
  • Fixes #4655

An example use case is where you want to show a list of all users participating in a discussion on a particular activity, above or before the activity comments themselves.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-activity/bp-activity-template.php

    r6489 r6501  
    24802480    }
    24812481
     2482/**
     2483 * Looks at all the activity comments on the current activity item, and prints the comments' authors's avatar wrapped in <LI> tags.
     2484 *
     2485 * Use this function to easily output activity comment authors' avatars.
     2486 *
     2487 * @param array $args See {@link bp_core_fetch_avatar} for accepted values
     2488 * @since BuddyPress (1.7)
     2489 */
     2490function bp_activity_comments_user_avatars( $args = array() ) {
     2491    $defaults = array(
     2492        'height' => false,
     2493        'html'   => true,
     2494        'type'   => 'thumb',
     2495        'width'  => false,
     2496    );
     2497
     2498    $args = wp_parse_args( $args, $defaults );
     2499    extract( $args, EXTR_SKIP );
     2500
     2501    // Get the user IDs of everyone who has left a comment to the current activity item
     2502    $user_ids = bp_activity_get_comments_user_ids();
     2503
     2504    $output = array();
     2505    foreach ( (array) $user_ids as $user_id ) {
     2506        $profile_link = bp_core_get_user_domain( $user_id );
     2507        $image_html   = bp_core_fetch_avatar( array( 'item_id' => $user_id, 'height' => $height, 'html' => $html, 'type' => $type, 'width' => $width, ) );
     2508
     2509        $output[] = sprintf( '<a href="%1$s">%2$s</a>', esc_url( $profile_link ), $image_html );
     2510    }
     2511
     2512    echo apply_filters( 'bp_activity_comments_user_avatars', '<li>' . implode( '</li><li>', $output ) . '</li>', $args, $output );
     2513}
     2514
     2515/**
     2516 * Returns the user IDs of everyone who's written an activity comment on the current activity item.
     2517 *
     2518 * @return bool|array Returns false if there is no current activity items
     2519 * @since BuddyPress (1.7)
     2520 */
     2521function bp_activity_get_comments_user_ids() {
     2522    if ( empty( $GLOBALS['activities_template']->activity ) || empty( $GLOBALS['activities_template']->activity->children ) )
     2523        return false;
     2524
     2525    $user_ids = (array) bp_activity_recurse_comments_user_ids( $GLOBALS['activities_template']->activity->children );
     2526    return apply_filters( 'bp_activity_get_comments_user_ids', array_unique( $user_ids ) );
     2527}
     2528
     2529    /**
     2530     * Recurse through all activity comments and collect the IDs of the users who wrote them.
     2531     *
     2532     * @param array $comments Array of {@link BP_Activity_Activity} items
     2533     * @return array Array of user IDs
     2534     * @since BuddyPress (1.7)
     2535     */
     2536    function bp_activity_recurse_comments_user_ids( array $comments ) {
     2537        $user_ids = array();
     2538
     2539        foreach ( $comments as $comment ) {
     2540            // If a user is a spammer, their activity items will have been automatically marked as spam. Skip these.
     2541            if ( $comment->is_spam )
     2542                continue;
     2543
     2544            $user_ids[] = $comment->user_id;
     2545
     2546            // Check for commentception
     2547            if ( ! empty( $comment->children ) )
     2548                $user_ids = array_merge( $user_ids, bp_activity_recurse_comments_user_ids( $comment->children ) );
     2549        }
     2550
     2551        return $user_ids;
     2552    }
     2553
     2554
    24822555/* RSS Feed Template Tags ****************************************************/
    24832556
Note: See TracChangeset for help on using the changeset viewer.