Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
07/14/2021 05:45:37 PM (4 years ago)
Author:
imath
Message:

Merge the BP Blocks plugin's 'bp/dynamic-members' Block

  • Adds a new assets/widgets/dynamic-members.php JavaScript template to our two template packs.
  • Add a front-end script to the Members component to manage Widget Block sort filters dynamically.
  • Adapt Grunt sass tasks.
  • Add the Block JavaScript source files into src/js/bp-members/js/blocks.
  • Add the Block Scss source file into src/bp-members/sass.
  • Generate the development files to ease testing.

Fixes #8522

File:
1 edited

Legend:

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

    r12996 r13003  
    332332    return apply_filters( 'bp_members_render_members_block_output', $output, $block_args, $members );
    333333}
     334
     335/**
     336 * Adds specific script data for the BP Members blocks.
     337 *
     338 * Only used for the BP Dynamic Members block.
     339 *
     340 * @since 9.0.0
     341 */
     342function bp_members_blocks_add_script_data() {
     343    $dynamic_members_blocks = array_filter( buddypress()->members->block_globals['bp/dynamic-members']->items );
     344
     345    if ( ! $dynamic_members_blocks ) {
     346        return;
     347    }
     348
     349    // Include the common JS template.
     350    echo bp_get_dynamic_template_part( 'assets/widgets/dynamic-members.php' );
     351
     352    // List the block specific props.
     353    wp_add_inline_script(
     354        'bp-dynamic-members-script',
     355        sprintf( 'var bpDynamicMembersBlocks = %s;', wp_json_encode( array_values( $dynamic_members_blocks ) ) ),
     356        'before'
     357    );
     358}
     359
     360/**
     361 * Callback function to render the Dynamic Members Block.
     362 *
     363 * @since 9.0.0
     364 *
     365 * @param array $attributes The block attributes.
     366 * @return string           HTML output.
     367 */
     368function bp_members_render_dynamic_members_block( $attributes = array() ) {
     369    $block_args = wp_parse_args(
     370        $attributes,
     371        array(
     372            'title'         => __( 'Members', 'buddypress' ),
     373            'maxMembers'    => 5,
     374            'memberDefault' => 'active',
     375            'linkTitle'     => false,
     376        )
     377    );
     378
     379    $classnames         = 'widget_bp_core_members_widget buddypress widget';
     380    $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) );
     381
     382    $max_members = (int) $block_args['maxMembers'];
     383    $no_members  = __( 'No members found.', 'buddypress' );
     384
     385    /** This filter is documented in buddypress/src/bp-members/classes/class-bp-core-members-widget.php */
     386    $separator = apply_filters( 'bp_members_widget_separator', '|' );
     387
     388    // Make sure the widget ID is unique.
     389    $widget_id              = uniqid( 'members-list-' );
     390    $members_directory_link = bp_get_members_directory_permalink();
     391
     392    // Set the Block's title.
     393    if ( true === $block_args['linkTitle'] ) {
     394        $widget_content = sprintf(
     395            '<h2 class="widget-title"><a href="%1$s">%2$s</a></h2>',
     396            esc_url( $members_directory_link ),
     397            esc_html( $block_args['title'] )
     398        );
     399    } else {
     400        $widget_content = sprintf( '<h2 class="widget-title">%s</h2>', esc_html( $block_args['title'] ) );
     401    }
     402
     403    $item_options = array(
     404        'newest' => array(
     405            'class' => '',
     406            'label' => __( 'Newest', 'buddypress' ),
     407        ),
     408        'active' => array(
     409            'class' => '',
     410            'label' => __( 'Active', 'buddypress' ),
     411        ),
     412    );
     413
     414    if ( bp_is_active( 'friends' ) ) {
     415        $item_options['popular'] = array(
     416            'class' => '',
     417            'label' => __( 'Popular', 'buddypress' ),
     418        );
     419    }
     420
     421    $item_options_output = array();
     422    $separator_output    = sprintf( ' <span class="bp-separator" role="separator">%s</span> ', esc_html( $separator ) );
     423
     424    foreach ( $item_options as $item_type => $item_attr ) {
     425        if ( $block_args['memberDefault'] === $item_type ) {
     426            $item_attr['class'] = ' class="selected"';
     427        }
     428
     429        $item_options_output[] = sprintf(
     430            '<a href="%1$s" data-bp-sort="%2$s"%3$s>%4$s</a>',
     431            esc_url( $members_directory_link ),
     432            esc_attr( $item_type ),
     433            $item_attr['class'],
     434            esc_html( $item_attr['label'] )
     435        );
     436    }
     437
     438    $preview      = '';
     439    $default_args = array(
     440        'type'            => $block_args['memberDefault'],
     441        'per_page'        => $max_members,
     442        'populate_extras' => true,
     443    );
     444
     445    // Previewing the Block inside the editor.
     446    if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
     447        $bp_query = bp_core_get_users( $default_args );
     448        $preview  = sprintf( '<div class="widget-error">%s</div>', $no_members );
     449
     450        if ( is_array( $bp_query['users'] ) && 0 < count( $bp_query['users'] ) ) {
     451            $preview = '';
     452            foreach ( $bp_query['users'] as $user ) {
     453                if ( 'newest' === $block_args['memberDefault'] ) {
     454                    /* translators: %s is time elapsed since the registration date happened */
     455                    $extra = sprintf( _x( 'Registered %s', 'The timestamp when the user registered', 'buddypress' ), bp_core_time_since( $user->user_registered ) );
     456                } elseif ( 'popular' === $block_args['memberDefault'] && isset( $item_options['popular'] ) && isset( $user->total_friend_count ) ) {
     457                    /* translators: %s: total friend count */
     458                    $extra = sprintf( _n( '%s friend', '%s friends', $user->total_friend_count, 'buddypress' ), number_format_i18n( $user->total_friend_count ) );
     459                } else {
     460                    /* translators: %s: a human time diff. */
     461                    $extra = sprintf( __( 'Active %s', 'buddypress' ), bp_core_time_since( $user->last_activity ) );
     462                }
     463
     464                $preview .= bp_get_dynamic_template_part(
     465                    'assets/widgets/dynamic-members.php',
     466                    'php',
     467                    array(
     468                        'data.link'              => bp_core_get_user_domain( $user->ID, $user->user_nicename, $user->user_login ),
     469                        'data.name'              => $user->display_name,
     470                        'data.avatar_urls.thumb' => bp_core_fetch_avatar(
     471                            array(
     472                                'item_id' => $user->ID,
     473                                'html'    => false,
     474                            )
     475                        ),
     476                        'data.avatar_alt'        => esc_html(
     477                            sprintf(
     478                                /* translators: %s: member name */
     479                                __( 'Profile picture of %s', 'buddypress' ),
     480                                $user->display_name
     481                            )
     482                        ),
     483                        'data.id'                => $user->ID,
     484                        'data.extra'             => $extra,
     485                    )
     486                );
     487            }
     488        }
     489    } else {
     490        // Get corresponding members.
     491        $path = sprintf(
     492            '/%1$s/%2$s/%3$s',
     493            bp_rest_namespace(),
     494            bp_rest_version(),
     495            buddypress()->members->id
     496        );
     497
     498        $default_path = add_query_arg(
     499            $default_args,
     500            $path
     501        );
     502
     503        $preloaded_members = array();
     504        if ( bp_is_running_wp( '5.0.0' ) ) {
     505            $preloaded_members = rest_preload_api_request( '', $default_path );
     506        }
     507
     508        buddypress()->members->block_globals['bp/dynamic-members']->items[ $widget_id ] = (object) array(
     509            'selector'   => $widget_id,
     510            'query_args' => $default_args,
     511            'preloaded'  => reset( $preloaded_members ),
     512        );
     513
     514        // Only enqueue common/specific scripts and data once per page load.
     515        if ( ! has_action( 'wp_footer', 'bp_members_blocks_add_script_data', 1 ) ) {
     516            wp_set_script_translations( 'bp-dynamic-members-script', 'buddypress' );
     517            wp_enqueue_script( 'bp-dynamic-members-script' );
     518            wp_localize_script(
     519                'bp-dynamic-members-script',
     520                'bpDynamicMembersSettings',
     521                array(
     522                    'path'  => ltrim( $path, '/' ),
     523                    'root'  => esc_url_raw( get_rest_url() ),
     524                    'nonce' => wp_create_nonce( 'wp_rest' ),
     525                )
     526            );
     527
     528            add_action( 'wp_footer', 'bp_members_blocks_add_script_data', 1 );
     529        }
     530    }
     531
     532    $widget_content .= sprintf(
     533        '<div class="item-options">
     534            %1$s
     535        </div>
     536        <ul id="%2$s" class="item-list" aria-live="polite" aria-relevant="all" aria-atomic="true">
     537            %3$s
     538        </ul>',
     539        implode( $separator_output, $item_options_output ),
     540        esc_attr( $widget_id ),
     541        $preview
     542    );
     543
     544    // Adds a container to make sure the block is styled even when used into the Columns parent block.
     545    $widget_content = sprintf( '<div class="bp-dynamic-block-container">%s</div>', "\n" . $widget_content . "\n" );
     546
     547    // Only add a block wrapper if not loaded into a Widgets sidebar.
     548    if ( ! did_action( 'dynamic_sidebar_before' ) ) {
     549        return sprintf(
     550            '<div %1$s>%2$s</div>',
     551            $wrapper_attributes,
     552            $widget_content
     553        );
     554    }
     555
     556    return $widget_content;
     557}
Note: See TracChangeset for help on using the changeset viewer.