Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
07/14/2021 06:45:11 PM (5 years ago)
Author:
imath
Message:

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

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

Fixes #8523

File:
1 edited

Legend:

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

    r12996 r13004  
    311311        return apply_filters( 'bp_groups_render_groups_block_output', $output, $block_args, $groups );
    312312}
     313
     314/**
     315 * Adds specific script data for the BP Groups blocks.
     316 *
     317 * Only used for the BP Dynamic Groups block.
     318 *
     319 * @since 9.0.0
     320 */
     321function bp_groups_blocks_add_script_data() {
     322        $dynamic_groups_blocks = array_filter( buddypress()->groups->block_globals['bp/dynamic-groups']->items );
     323
     324        if ( ! $dynamic_groups_blocks ) {
     325                return;
     326        }
     327
     328        // Include the common JS template.
     329        echo bp_get_dynamic_template_part( 'assets/widgets/dynamic-groups.php' );
     330
     331        // List the block specific props.
     332        wp_add_inline_script(
     333                'bp-dynamic-groups-script',
     334                sprintf( 'var bpDynamicGroupsBlocks = %s;', wp_json_encode( array_values( $dynamic_groups_blocks ) ) ),
     335                'before'
     336        );
     337}
     338
     339/**
     340 * Callback function to render the Dynamic Groups Block.
     341 *
     342 * @since 9.0.0
     343 *
     344 * @param array $attributes The block attributes.
     345 * @return string           HTML output.
     346 */
     347function bp_groups_render_dynamic_groups_block( $attributes = array() ) {
     348        $block_args = wp_parse_args(
     349                $attributes,
     350                array(
     351                        'title'        => __( 'Groups', 'buddypress' ),
     352                        'maxGroups'    => 5,
     353                        'groupDefault' => 'active',
     354                        'linkTitle'    => false,
     355                )
     356        );
     357
     358        $classnames         = 'widget_bp_groups_widget buddypress widget';
     359        $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) );
     360
     361        $max_groups = (int) $block_args['maxGroups'];
     362        $no_groups  = __( 'There are no groups to display.', 'buddypress' );
     363
     364        /** This filter is documented in buddypress/src/bp-groups/classes/class-bp-groups-widget.php */
     365        $separator = apply_filters( 'bp_groups_widget_separator', '|' );
     366
     367        // Make sure the widget ID is unique.
     368        $widget_id             = uniqid( 'groups-list-' );
     369        $groups_directory_link = bp_get_groups_directory_permalink();
     370
     371        // Set the Block's title.
     372        if ( true === $block_args['linkTitle'] ) {
     373                $widget_content = sprintf(
     374                        '<h2 class="widget-title"><a href="%1$s">%2$s</a></h2>',
     375                        esc_url( $groups_directory_link ),
     376                        esc_html( $block_args['title'] )
     377                );
     378        } else {
     379                $widget_content = sprintf( '<h2 class="widget-title">%s</h2>', esc_html( $block_args['title'] ) );
     380        }
     381
     382        $item_options = array(
     383                'newest'       => array(
     384                        'class' => '',
     385                        'label' => __( 'Newest', 'buddypress' ),
     386                ),
     387                'active'       => array(
     388                        'class' => '',
     389                        'label' => __( 'Active', 'buddypress' ),
     390                ),
     391                'popular'      => array(
     392                        'class' => '',
     393                        'label' => __( 'Popular', 'buddypress' ),
     394                ),
     395                'alphabetical' => array(
     396                        'class' => '',
     397                        'label' => __( 'Alphabetical', 'buddypress' ),
     398                ),
     399        );
     400
     401        $item_options_output = array();
     402        $separator_output    = sprintf( ' <span class="bp-separator" role="separator">%s</span> ', esc_html( $separator ) );
     403
     404        foreach ( $item_options as $item_type => $item_attr ) {
     405                if ( $block_args['groupDefault'] === $item_type ) {
     406                        $item_attr['class'] = ' class="selected"';
     407                }
     408
     409                $item_options_output[] = sprintf(
     410                        '<a href="%1$s" data-bp-sort="%2$s"%3$s>%4$s</a>',
     411                        esc_url( $groups_directory_link ),
     412                        esc_attr( $item_type ),
     413                        $item_attr['class'],
     414                        esc_html( $item_attr['label'] )
     415                );
     416        }
     417
     418        $preview      = '';
     419        $default_args = array(
     420                'type'            => $block_args['groupDefault'],
     421                'per_page'        => $max_groups,
     422                'populate_extras' => true,
     423        );
     424
     425        // Previewing the Block inside the editor.
     426        if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
     427                $bp_query = groups_get_groups( $default_args );
     428                $preview  = sprintf( '<div class="widget-error">%s</div>', $no_groups );
     429
     430                if ( is_array( $bp_query['groups'] ) && 0 < count( $bp_query['groups'] ) ) {
     431                        $preview = '';
     432                        foreach ( $bp_query['groups'] as $group ) {
     433                                if ( 'newest' === $block_args['groupDefault'] ) {
     434                                        /* translators: %s is time elapsed since the group was created */
     435                                        $extra = sprintf( __( 'Created %s', 'buddypress' ), bp_get_group_date_created( $group ) );
     436                                } elseif ( 'popular' === $block_args['groupDefault'] ) {
     437                                        $count = (int) $group->total_member_count;
     438
     439                                        /* translators: %s is the number of Group members */
     440                                        $extra = sprintf( _n( '%s member', '%s members', $count, 'buddypress' ), bp_core_number_format( $count ) );
     441                                } else {
     442                                        /* translators: %s: a human time diff. */
     443                                        $extra = sprintf( __( 'Active %s', 'buddypress' ), bp_get_group_last_active( $group ) );
     444                                }
     445
     446                                $preview .= bp_get_dynamic_template_part(
     447                                        'assets/widgets/dynamic-groups.php',
     448                                        'php',
     449                                        array(
     450                                                'data.link'              => bp_get_group_permalink( $group ),
     451                                                'data.name'              => bp_get_group_name( $group ),
     452                                                'data.avatar_urls.thumb' => bp_core_fetch_avatar(
     453                                                        array(
     454                                                                'item_id' => $group->id,
     455                                                                'html'    => false,
     456                                                                'object'  => 'group',
     457                                                        )
     458                                                ),
     459                                                'data.avatar_alt'        => esc_attr(
     460                                                        sprintf(
     461                                                                /* Translators: %s is the group's name. */
     462                                                                __( 'Group Profile photo of %s', 'buddypress' ),
     463                                                                $group->name
     464                                                        )
     465                                                ),
     466                                                'data.id'                => $group->id,
     467                                                'data.extra'             => $extra,
     468                                        )
     469                                );
     470                        }
     471                }
     472        } else {
     473                // Get corresponding members.
     474                $path = sprintf(
     475                        '/%1$s/%2$s/%3$s',
     476                        bp_rest_namespace(),
     477                        bp_rest_version(),
     478                        buddypress()->groups->id
     479                );
     480
     481                $default_path = add_query_arg(
     482                        $default_args,
     483                        $path
     484                );
     485
     486                $preloaded_groups = array();
     487                if ( bp_is_running_wp( '5.0.0' ) ) {
     488                        $preloaded_groups = rest_preload_api_request( '', $default_path );
     489                }
     490
     491                buddypress()->groups->block_globals['bp/dynamic-groups']->items[ $widget_id ] = (object) array(
     492                        'selector'   => $widget_id,
     493                        'query_args' => $default_args,
     494                        'preloaded'  => reset( $preloaded_groups ),
     495                );
     496
     497                // Only enqueue common/specific scripts and data once per page load.
     498                if ( ! has_action( 'wp_footer', 'bp_groups_blocks_add_script_data', 1 ) ) {
     499                        wp_set_script_translations( 'bp-dynamic-groups-script', 'buddypress' );
     500                        wp_enqueue_script( 'bp-dynamic-groups-script' );
     501                        wp_localize_script(
     502                                'bp-dynamic-groups-script',
     503                                'bpDynamicGroupsSettings',
     504                                array(
     505                                        'path'  => ltrim( $path, '/' ),
     506                                        'root'  => esc_url_raw( get_rest_url() ),
     507                                        'nonce' => wp_create_nonce( 'wp_rest' ),
     508                                )
     509                        );
     510
     511                        add_action( 'wp_footer', 'bp_groups_blocks_add_script_data', 1 );
     512                }
     513        }
     514
     515        $widget_content .= sprintf(
     516                '<div class="item-options">
     517                        %1$s
     518                </div>
     519                <ul id="%2$s" class="item-list" aria-live="polite" aria-relevant="all" aria-atomic="true">
     520                        %3$s
     521                </ul>',
     522                implode( $separator_output, $item_options_output ),
     523                esc_attr( $widget_id ),
     524                $preview
     525        );
     526
     527        // Adds a container to make sure the block is styled even when used into the Columns parent block.
     528        $widget_content = sprintf( '<div class="bp-dynamic-block-container">%s</div>', "\n" . $widget_content . "\n" );
     529
     530        // Only add a block wrapper if not loaded into a Widgets sidebar.
     531        if ( ! did_action( 'dynamic_sidebar_before' ) ) {
     532                return sprintf(
     533                        '<div %1$s>%2$s</div>',
     534                        $wrapper_attributes,
     535                        $widget_content
     536                );
     537        }
     538
     539        return $widget_content;
     540}
Note: See TracChangeset for help on using the changeset viewer.