Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
10/11/2020 11:51:42 AM (4 years ago)
Author:
imath
Message:

BP Blocks: introduce the BP Members Block 👫

Unlike what we are doing in BP Members widgets (filtering a max amount of members according to their popularity, last activity, alphabetically, ...), the BP Members block lets you select as many members as you wish to feature them into your post or page.

Members are selected one by one using the BP Auto-completer React component.

Just like the BP Member block you can choose the Profile photo size (or disable it), whether to show or hide the mention name.

You can also choose to show or hide the user display name and some extra information generated by BuddyPress about the user:

  • Last time the user was active,
  • Latest activity the user posted (if the Activity component is active).

Finally you can choose to display members in a grid of 2 to 4 columns or in a list.

Props espellcaste, imath

See #8369

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-members/bp-members-blocks.php

    r12591 r12748  
    193193    return apply_filters( 'bp_members_render_member_block_output', $output, $params );
    194194}
     195
     196/**
     197 * Callback function to render the BP Members Block.
     198 *
     199 * @since 7.0.0
     200 *
     201 * @param array $attributes The block attributes.
     202 * @return string           HTML output.
     203 */
     204function bp_members_render_members_block( $attributes = array() ) {
     205    $bp = buddypress();
     206
     207    $block_args = wp_parse_args(
     208        $attributes,
     209        array(
     210            'itemIDs'            => array(),
     211            'avatarSize'         => 'full',
     212            'displayMentionSlug' => true,
     213            'displayUserName'    => true,
     214            'extraData'          => 'none',
     215            'layoutPreference'   => 'list',
     216            'columns'            => '2',
     217        )
     218    );
     219
     220    $member_ids = wp_parse_id_list( $block_args['itemIDs'] );
     221    if ( ! array_filter( $member_ids ) ) {
     222        return '';
     223    }
     224
     225    $container_classes = sprintf( 'bp-block-members avatar-%s', $block_args['avatarSize'] );
     226    if ( 'grid' === $block_args['layoutPreference'] ) {
     227        $container_classes .= sprintf( ' is-grid columns-%d', (int) $block_args['columns'] );
     228    }
     229
     230    $query_args = array(
     231        'user_ids' => $member_ids,
     232    );
     233
     234    if ( 'none' !== $block_args['extraData'] ) {
     235        $query_args['populate_extras'] = true;
     236    }
     237
     238    $query = bp_core_get_users( $query_args );
     239
     240    // Initialize the output and the members.
     241    $output  = '';
     242    $members = $query['users'];
     243
     244    foreach ( $members as $member ) {
     245        $has_activity        = false;
     246        $member_item_classes = 'member-content';
     247
     248        if ( 'list' === $block_args['layoutPreference'] && 'latest_update' === $block_args['extraData'] && isset( $member->latest_update ) && $member->latest_update ) {
     249            $has_activity        = true;
     250            $member_item_classes = 'member-content has-activity';
     251        }
     252
     253        $output .= sprintf( '<div class="%s">', $member_item_classes );
     254
     255        // Get Member link.
     256        $member_link = bp_core_get_user_domain( $member->ID );
     257
     258        // Set the Avatar output.
     259        if ( $bp->avatar && $bp->avatar->show_avatars && 'none' !== $block_args['avatarSize'] ) {
     260            $output .= sprintf(
     261                '<div class="item-header-avatar">
     262                    <a href="%1$s">
     263                        <img class="avatar" alt="%2$s" src="%3$s" />
     264                    </a>
     265                </div>',
     266                esc_url( $member_link ),
     267                /* translators: %s: the member display name */
     268                sprintf( esc_attr__( 'Profile photo of %s', 'buddypress' ), $member->display_name ),
     269                esc_url(
     270                    bp_core_fetch_avatar(
     271                        array(
     272                            'item_id' => $member->ID,
     273                            'object'  => 'user',
     274                            'type'    => $block_args['avatarSize'],
     275                            'html'    => false,
     276                        )
     277                    )
     278                )
     279            );
     280        }
     281
     282        $output .= '<div class="member-description">';
     283
     284        // Add the latest activity the member posted.
     285        if ( $has_activity ) {
     286            $activity_content = '';
     287            $activity_data    = maybe_unserialize( $member->latest_update );
     288
     289            if ( isset( $activity_data['content'] ) ) {
     290                $activity_content = apply_filters( 'bp_get_activity_content', $activity_data['content'] );
     291            }
     292
     293            $display_name = '';
     294            if ( $block_args['displayUserName'] ) {
     295                $display_name = $member->display_name;
     296            }
     297
     298            $mention_name = '';
     299            if ( bp_is_active( 'activity' ) && bp_activity_do_mentions() && $block_args['displayMentionSlug'] ) {
     300                $mention_name = '(@' . $member->user_nicename . ')';
     301            }
     302
     303            $output .= sprintf(
     304                '<blockquote class="wp-block-quote">
     305                    %1$s
     306                    <cite>
     307                        <span>%2$s</span>
     308                        %3$s
     309                    </cite>
     310                </blockquote>',
     311                $activity_content,
     312                esc_html( $display_name ),
     313                esc_html( $mention_name )
     314            );
     315        } else {
     316            if ( $block_args['displayUserName'] ) {
     317                $output .= sprintf(
     318                    '<strong><a href="%1$s">%2$s</a></strong>',
     319                    esc_url( $member_link ),
     320                    esc_html( $member->display_name )
     321                );
     322            }
     323
     324            if ( bp_is_active( 'activity' ) && bp_activity_do_mentions() && $block_args['displayMentionSlug'] ) {
     325                $output .= sprintf(
     326                    '<span class="user-nicename">@%s</span>',
     327                    esc_html( $member->user_nicename )
     328                );
     329            }
     330
     331            if ( 'last_activity' === $block_args['extraData'] ) {
     332                $output .= sprintf(
     333                    '<time datetime="%1$s">%2$s</time>',
     334                    esc_attr( bp_core_get_iso8601_date( $member->last_activity ) ),
     335                    /* translators: %s: a human time diff */
     336                    sprintf( esc_html__( 'Active %s', 'buddypress' ), bp_core_time_since( $member->last_activity ) )
     337                );
     338            }
     339        }
     340
     341        $output .= '</div></div>';
     342    }
     343
     344    // Set the final output.
     345    $output = sprintf( '<div class="%1$s">%2$s</div>', $container_classes, $output );
     346
     347    /**
     348     * Filter here to edit the output of the members block.
     349     *
     350     * @since 7.0.0
     351     *
     352     * @param string $output     The HTML output of the block.
     353     * @param array  $block_args The block arguments.
     354     * @param array  $members    The list of WP_User objects.
     355     */
     356    return apply_filters( 'bp_members_render_members_block_output', $output, $block_args, $members );
     357}
Note: See TracChangeset for help on using the changeset viewer.