Skip to:
Content

BuddyPress.org

Changeset 12998


Ignore:
Timestamp:
07/14/2021 08:52:00 AM (4 years ago)
Author:
imath
Message:

Introduce a new BP_Component property for block globals

This new property will help us to manage Widget Block occurrences globals and use a single filter on 'widget_block_dynamic_classname' to make sure Widget Blocks will include Legacy Widget classnames and inherit their styles.

This commit also makes sure WordPress is >= 5.0 before using the has_block function.

See #8515
Fixes #8516

Location:
trunk
Files:
1 added
4 edited

Legend:

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

    r12995 r12998  
    163163    return new BP_Block( $args );
    164164}
     165
     166/**
     167 * Gets a Widget Block list of classnames.
     168 *
     169 * @since 9.0.0
     170 *
     171 * @param string $block_name The Block name.
     172 * @return array The list of widget classnames for the Block.
     173 */
     174function bp_blocks_get_widget_block_classnames( $block_name = '' ) {
     175    $components         = bp_core_get_active_components( array(), 'objects' );
     176    $components['core'] = buddypress()->core;
     177    $classnames         = array();
     178
     179    foreach ( $components as $component ) {
     180        if ( isset( $component->block_globals[ $block_name ] ) ) {
     181            $block_props = $component->block_globals[ $block_name ]->props;
     182
     183            if ( isset( $block_props['widget_classnames'] ) && $block_props['widget_classnames'] ) {
     184                $classnames = (array) $block_props['widget_classnames'];
     185                break;
     186            }
     187        }
     188    }
     189
     190    return $classnames;
     191}
     192
     193/**
     194 * Make sure the BP Widget Block classnames are included into Widget Blocks.
     195 *
     196 * @since 9.0.0
     197 *
     198 * @param string $classname The classname to be used in the block widget's container HTML.
     199 * @param string $block_name The name of the block.
     200 * @return string The classname to be used in the block widget's container HTML.
     201 */
     202function bp_widget_block_dynamic_classname( $classname, $block_name ) {
     203    $bp_classnames = bp_blocks_get_widget_block_classnames( $block_name );
     204
     205    if ( $bp_classnames ) {
     206        $bp_classnames = array_map( 'sanitize_html_class', $bp_classnames );
     207        $classname    .= ' ' . implode( ' ', $bp_classnames );
     208    }
     209
     210    return $classname;
     211}
     212add_filter( 'widget_block_dynamic_classname', 'bp_widget_block_dynamic_classname', 10, 2 );
  • trunk/src/bp-core/bp-core-template.php

    r12997 r12998  
    38733873    );
    38743874
    3875     if ( $block_name ) {
     3875    if ( $block_name && bp_is_running_wp( '5.0.0', '>=' ) ) {
    38763876        $widget_blocks = get_option( 'widget_block', array() );
    38773877        $sidebars      = wp_get_sidebars_widgets();
  • trunk/src/bp-core/classes/class-bp-component.php

    r12994 r12998  
    140140     */
    141141    public $search_query_arg = 's';
     142
     143    /**
     144     * An array of globalized data for BP Blocks.
     145     *
     146     * @since 9.0.0
     147     *
     148     * @var array
     149     */
     150    public $block_globals = array();
    142151
    143152    /** Methods ***************************************************************/
     
    206215     *
    207216     * @since 1.5.0
    208      *
     217     * @since 9.0.0 Adds the `$block_globals` argument to the `$args` parameter.
    209218     *
    210219     * @param array $args {
     
    222231     *     @type array    $global_tables         Optional. An array of database table names.
    223232     *     @type array    $meta_tables           Optional. An array of metadata table names.
     233     *     @type array    $block_globals         Optional. An array of globalized data for BP Blocks.
    224234     * }
    225235     */
     
    242252            'global_tables'         => '',
    243253            'meta_tables'           => '',
     254            'block_globals'         => array(),
    244255        ) );
    245256
     
    306317        if ( ! empty( $r['meta_tables'] ) ) {
    307318            $this->register_meta_tables( $r['meta_tables'] );
     319        }
     320
     321        /**
     322         * Filters the $blocks global value.
     323         *
     324         * @since 9.0.0
     325         *
     326         * @param array $blocks a list of global properties for blocks keyed
     327         *                      by their corresponding block name.
     328         */
     329        $block_globals = apply_filters( 'bp_' . $this->id . '_block_globals', $r['block_globals'] );
     330        if ( is_array( $block_globals ) && array_filter( $block_globals ) ) {
     331            foreach ( $block_globals as $block_name => $block_props ) {
     332                $this->block_globals[ $block_name ] = new stdClass();
     333
     334                // Initialize an `items` property for Widget Block occurrences.
     335                $this->block_globals[ $block_name ]->items = array();
     336
     337                // Set the global properties for the Block.
     338                $this->block_globals[ $block_name ]->props = (array) $block_props;
     339            }
    308340        }
    309341
  • trunk/tests/phpunit/testcases/core/class-bp-component.php

    r12994 r12998  
    22
    33include_once BP_TESTS_DIR . '/assets/bp-rest-api-controllers.php';
     4include_once BP_TESTS_DIR . '/assets/class-bptest-component.php';
    45
    56/**
     
    6970        ) );
    7071    }
     72
     73    /**
     74     * @group bp_blocks
     75     */
     76    public function test_component_block_globals() {
     77        $expected = array(
     78            'dynamic_widget_classname' => 'widget_example_classname',
     79        );
     80
     81        $example = new BPTest_Component(
     82            array(
     83                'globals' => array(
     84                    'block_globals' => array(
     85                        'bp/example-block' => $expected,
     86                    )
     87                ),
     88            )
     89        );
     90
     91        do_action( 'bp_setup_globals' );
     92
     93        $this->assertEquals( $expected, $example->block_globals['bp/example-block']->props );
     94    }
    7195}
Note: See TracChangeset for help on using the changeset viewer.