Skip to:
Content

BuddyPress.org

Changeset 13000


Ignore:
Timestamp:
07/14/2021 10:52:40 AM (5 years ago)
Author:
imath
Message:

Merge the BP Blocks plugin's 'bp/primary-nav' Block into Core/Nouveau

  • Adapt Grunt sass task.
  • Add the Block JavaScript source files into src/js/bp-core/js/blocks.
  • Add the Block Scss source file into src/bp-templates/bp-nouveau/sass.
  • Generate the development files to ease testing.
  • Add filters to register the Block and its globals into the BP Core component.
  • Limit the Widget Block to the Widget Block Editor context.

Fixes #8518

Location:
trunk
Files:
8 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Gruntfile.js

    r12999 r13000  
    3434                        '!bp-templates/bp-nouveau/css/buddypress.css',
    3535                        '!bp-templates/bp-nouveau/css/twenty*.css',
     36                        '!bp-templates/bp-nouveau/css/primary-nav.css',
    3637                        '!bp-core/admin/css/hello.css',
    3738                        '!bp-members/css/blocks/member.css',
     
    120121                                ext: '.css',
    121122                                flatten: true,
    122                                 src: ['bp-templates/bp-nouveau/sass/buddypress.scss', 'bp-templates/bp-nouveau/sass/twenty*.scss'],
     123                                src: ['bp-templates/bp-nouveau/sass/buddypress.scss', 'bp-templates/bp-nouveau/sass/twenty*.scss', 'bp-templates/bp-nouveau/sass/primary-nav.scss'],
    123124                                dest: SOURCE_DIR + 'bp-templates/bp-nouveau/css/'
    124125                        },
  • trunk/src/bp-core/classes/class-bp-core.php

    r12999 r13000  
    296296                                'block_globals' => array(
    297297                                        'bp/login-form' => array(
    298                                                 'widget_classnames' => array ( 'widget_bp_core_login_widget', 'buddypress' ),
     298                                                'widget_classnames' => array( 'widget_bp_core_login_widget', 'buddypress' ),
    299299                                        )
    300300                                )
  • trunk/src/bp-templates/bp-nouveau/includes/functions.php

    r12997 r13000  
    44 *
    55 * @since 3.0.0
    6  * @version 8.0.0
     6 * @version 9.0.0
    77 */
    88
     
    14901490        return apply_filters( 'bp_nouveau_get_component_slug', $slug, $component_id );
    14911491}
     1492
     1493/**
     1494 * Registers the 'bp/primary-nav' Widget Block.
     1495 *
     1496 * @since 9.0.0
     1497 *
     1498 * @param array $blocks The Core Blocks list.
     1499 * @return array The Core Blocks list.
     1500 */
     1501function bp_nouveau_register_primary_nav_widget_block( $blocks = array() ) {
     1502        $editor_style = bp_locate_template_asset( 'css/primary-nav.css' );
     1503
     1504        $blocks['bp/primary-nav'] = array(
     1505                'name'               => 'bp/primary-nav',
     1506                'editor_script'      => 'bp-primary-nav-block',
     1507                'editor_script_url'  => trailingslashit( buddypress()->plugin_url . 'bp-core' ) . 'js/blocks/primary-nav.js',
     1508                'editor_script_deps' => array(
     1509                        'wp-blocks',
     1510                        'wp-element',
     1511                        'wp-components',
     1512                        'wp-i18n',
     1513                        'wp-block-editor',
     1514                        'bp-block-data',
     1515                        'bp-block-components',
     1516                ),
     1517                'editor_style'       => 'bp-primary-nav-block',
     1518                'editor_style_url'   => $editor_style['uri'],
     1519                'attributes'         => array(
     1520                        'displayTitle' => array(
     1521                                'type'    => 'boolean',
     1522                                'default' => true,
     1523                        ),
     1524                ),
     1525                'render_callback'    => 'bp_nouveau_render_primary_nav_block',
     1526        );
     1527
     1528        return $blocks;
     1529}
     1530add_filter( 'bp_core_register_blocks', 'bp_nouveau_register_primary_nav_widget_block', 20, 1 );
     1531
     1532/**
     1533 * Registers the 'bp/primary-nav' Widget Block classnames.
     1534 *
     1535 * @since 9.0.0
     1536 *
     1537 * @param array $block_globals The list of global properties for Core blocks.
     1538 * @return array               The list of global properties for Core blocks.
     1539 */
     1540function bp_nouveau_register_core_block_globals( $block_globals = array() ) {
     1541        $block_globals['bp/primary-nav'] = array(
     1542                'widget_classnames' => array( 'widget_nav_menu', 'buddypress_object_nav', 'buddypress' ),
     1543        );
     1544
     1545        return $block_globals;
     1546}
     1547add_filter( 'bp_core_block_globals', 'bp_nouveau_register_core_block_globals', 10, 1 );
     1548
     1549/**
     1550 * Unregister the 'bp/primary-nav' Block from the post context.
     1551 *
     1552 * @since 9.0.0
     1553 */
     1554function bp_nouveau_unregister_blocks_for_post_context() {
     1555        unregister_block_type( 'bp/primary-nav' );
     1556}
     1557add_action( 'load-post.php', 'bp_nouveau_unregister_blocks_for_post_context' );
     1558add_action( 'load-post-new.php', 'bp_nouveau_unregister_blocks_for_post_context' );
     1559
     1560/**
     1561 * Callback function to render the BP Primary Nav Block.
     1562 *
     1563 * @since 9.0.0
     1564 *
     1565 * @param array $attributes The block attributes.
     1566 * @return string           HTML output.
     1567 */
     1568function bp_nouveau_render_primary_nav_block( $attributes = array() ) {
     1569        $widget_content = '';
     1570        $widget_title   = '';
     1571        $block_args     = bp_parse_args(
     1572                $attributes,
     1573                array(
     1574                        'displayTitle' => true,
     1575                ),
     1576                'widget_object_nav'
     1577        );
     1578
     1579        // Previewing the Block inside the editor.
     1580        if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
     1581                $widget_title = bp_get_loggedin_user_fullname();
     1582
     1583                ob_start();
     1584
     1585                // Temporary override the displayed user by the logged in one.
     1586                add_filter( 'bp_displayed_user_id', 'bp_loggedin_user_id' );
     1587
     1588                bp_get_template_part( 'members/single/parts/item-nav' );
     1589                $widget_content = ob_get_clean();
     1590
     1591                // Remove the temporary override.
     1592                remove_filter( 'bp_displayed_user_id', 'bp_loggedin_user_id' );
     1593        } else {
     1594                ob_start();
     1595
     1596                if ( bp_is_user() ) {
     1597                        $widget_title = bp_get_displayed_user_fullname();
     1598                        bp_get_template_part( 'members/single/parts/item-nav' );
     1599                } elseif ( bp_is_group() ) {
     1600                        $widget_title = bp_get_current_group_name();
     1601                        bp_get_template_part( 'groups/single/parts/item-nav' );
     1602                } elseif ( bp_is_directory() ) {
     1603                        $widget_title = bp_get_directory_title( bp_current_component() );
     1604                        bp_get_template_part( 'common/nav/directory-nav' );
     1605                }
     1606
     1607                $widget_content = ob_get_clean();
     1608        }
     1609
     1610        if ( ! $widget_content ) {
     1611                return '';
     1612        }
     1613
     1614        // Set the Block's title.
     1615        if ( true === $block_args['displayTitle'] ) {
     1616                $widget_content = sprintf(
     1617                        '<h2 class="widget-title">%1$s</h2>
     1618                        %2$s',
     1619                        esc_html( $widget_title ),
     1620                        $widget_content
     1621                );
     1622        }
     1623
     1624        // Only add a block wrapper if not loaded into a Widgets sidebar.
     1625        if ( ! did_action( 'dynamic_sidebar_before' ) ) {
     1626                $classnames         = 'widget_nav_menu buddypress_object_nav buddypress widget';
     1627                $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) );
     1628
     1629                return sprintf(
     1630                        '<div %1$s>%2$s</div>',
     1631                        $wrapper_attributes,
     1632                        $widget_content
     1633                );
     1634        }
     1635
     1636        return $widget_content;
     1637}
Note: See TracChangeset for help on using the changeset viewer.