Skip to:
Content

BuddyPress.org

Ticket #8516: 8516.patch

File 8516.patch, 6.7 KB (added by imath, 5 years ago)
  • src/bp-core/bp-core-blocks.php

    diff --git src/bp-core/bp-core-blocks.php src/bp-core/bp-core-blocks.php
    index 96bb0aa32..85217e4ce 100644
    add_filter( 'block_editor_rest_api_preload_paths', 'bp_blocks_preload_paths' ); 
    162162function bp_register_block( $args = array() ) {
    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 );
  • src/bp-core/bp-core-template.php

    diff --git src/bp-core/bp-core-template.php src/bp-core/bp-core-template.php
    index f5655279e..0961c0805 100644
    function bp_is_widget_block_active( $block_name = '', $widget_id_base = '' ) { 
    38723872                'block'  => false,
    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();
    38783878
  • src/bp-core/classes/class-bp-component.php

    diff --git src/bp-core/classes/class-bp-component.php src/bp-core/classes/class-bp-component.php
    index 23ef1cbc5..b1b13341f 100644
    class BP_Component { 
    140140         */
    141141        public $search_query_arg = 's';
    142142
     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();
     151
    143152        /** Methods ***************************************************************/
    144153
    145154        /**
    class BP_Component { 
    205214         * Set up component global variables.
    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 {
    211220         *     All values are optional.
    class BP_Component { 
    221230         *                                           'Search Groups...'.
    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         */
    226236        public function setup_globals( $args = array() ) {
    class BP_Component { 
    241251                        'search_string'         => '',
    242252                        'global_tables'         => '',
    243253                        'meta_tables'           => '',
     254                        'block_globals'         => array(),
    244255                ) );
    245256
    246257                /**
    class BP_Component { 
    307318                        $this->register_meta_tables( $r['meta_tables'] );
    308319                }
    309320
     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                        }
     340                }
     341
    310342                /** BuddyPress *******************************************************
    311343                 */
    312344
  • new file tests/phpunit/assets/class-bptest-component.php

    diff --git tests/phpunit/assets/class-bptest-component.php tests/phpunit/assets/class-bptest-component.php
    new file mode 100644
    index 000000000..1b6c006a6
    - +  
     1<?php
     2
     3// Testing Component Class.
     4class BPTest_Component extends BP_Component {
     5        /**
     6         * Globals to test.
     7         *
     8         * @var array
     9         */
     10        public $globals = array();
     11
     12        // Start the `test` component setup process.
     13        public function __construct( $args = array() ) {
     14                $r = wp_parse_args(
     15                        $args,
     16                        array(
     17                                'id'      => 'example',
     18                                'name'    => 'Example Component',
     19                                'globals' => array(
     20                                        'slug' => 'example',
     21                                ),
     22                        )
     23                );
     24
     25                $this->globals = $r['globals'];
     26
     27                parent::start(
     28                        $r['id'],
     29                        $r['name']
     30                );
     31        }
     32
     33        // Setup Test Globals.
     34        public function setup_globals( $args = array() ) {
     35                parent::setup_globals( $this->globals );
     36        }
     37}
  • tests/phpunit/testcases/core/class-bp-component.php

    diff --git tests/phpunit/testcases/core/class-bp-component.php tests/phpunit/testcases/core/class-bp-component.php
    index 29d226f37..4f249eca9 100644
     
    11<?php
    22
    33include_once BP_TESTS_DIR . '/assets/bp-rest-api-controllers.php';
     4include_once BP_TESTS_DIR . '/assets/class-bptest-component.php';
    45
    56/**
    67 * @group core
    class BP_Tests_BP_Component_TestCases extends BP_UnitTestCase { 
    6869                        'BP_REST_Attachments_Member_Cover_Endpoint',
    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}