Skip to:
Content

BuddyPress.org

Changeset 12997


Ignore:
Timestamp:
07/14/2021 01:50:38 AM (4 years ago)
Author:
imath
Message:

Add a new function to check if a Widget/Block is active into a sidebar

Some of our Legacy Widgets are being displayed only if active they have been included into a Sidebar Widget. We are using the is_active_widget() WordPress function to check whether it is the case or not. This function doesn't fit in the Widget Blocks case as all Blocks are being inserted into the specific WP_Widget_Block widget.

As WordPress 5.8 doesn't provide any equivalent (so far?), we are introducing a new function to do this check: bp_is_widget_block_active(). It takes 2 optional arguments.

  • $block_name: the name of the block you need to check (eg: 'bp/members').
  • $widget_id_base: the Base ID of the widget (eg: 'bp_messages_sitewide_notices_widget').

You can use it to check a block, a widget or both.

Fixes #8515

Location:
trunk/src
Files:
6 edited

Legend:

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

    r12934 r12997  
    38573857        return $salutation;
    38583858    }
     3859
     3860/**
     3861 * Checks if a Widget/Block is active.
     3862 *
     3863 * @since 9.0.0
     3864 *
     3865 * @param string $block_name     The Block name to check (eg: 'bp/sitewide-notices'). Optional.
     3866 * @param string $widget_id_base The Widget ID base to check (eg: 'bp_messages_sitewide_notices_widget' ). Optional.
     3867 * @return boolean True if the Widget/Block is active. False otherwise.
     3868 */
     3869function bp_is_widget_block_active( $block_name = '', $widget_id_base = '' ) {
     3870    $is_active = array(
     3871        'widget' => false,
     3872        'block'  => false,
     3873    );
     3874
     3875    if ( $block_name ) {
     3876        $widget_blocks = get_option( 'widget_block', array() );
     3877        $sidebars      = wp_get_sidebars_widgets();
     3878
     3879        if ( ! $widget_blocks || ! $sidebars ) {
     3880            return false;
     3881        }
     3882
     3883        // Neutralize inactive sidebar.
     3884        unset( $sidebars['wp_inactive_widgets'] );
     3885
     3886        $widgets_content = '';
     3887        foreach ( $widget_blocks as $key => $widget_block ) {
     3888            $widget_block_reference = 'block-' . $key;
     3889
     3890            if ( ! isset( $widget_block['content'] ) || ! $widget_block['content'] ) {
     3891                continue;
     3892            }
     3893
     3894            foreach ( $sidebars as $sidebar ) {
     3895                if ( is_array( $sidebar ) && in_array( $widget_block_reference, $sidebar, true ) ) {
     3896                    $widgets_content .= $widget_block['content'] . "\n";
     3897                }
     3898            }
     3899        }
     3900
     3901        $is_active['block'] = has_block( $block_name, $widgets_content );
     3902    }
     3903
     3904    if ( $widget_id_base ) {
     3905        $is_active['widget'] = is_active_widget( false, false, $widget_id_base, true );
     3906    }
     3907
     3908    return 0 !== count( array_filter( $is_active ) );
     3909}
  • trunk/src/bp-friends/classes/class-bp-core-friends-widget.php

    r12991 r12997  
    3333        parent::__construct( false, $name = _x( '(BuddyPress) Friends', 'widget name', 'buddypress' ), $widget_ops );
    3434
    35         if ( is_customize_preview() || is_active_widget( false, false, $this->id_base ) ) {
     35        if ( is_customize_preview() || bp_is_widget_block_active( '', $this->id_base ) ) {
    3636            add_action( 'bp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
    3737        }
  • trunk/src/bp-groups/classes/class-bp-groups-widget.php

    r12976 r12997  
    3333        parent::__construct( false, _x( '(BuddyPress) Groups', 'widget name', 'buddypress' ), $widget_ops );
    3434
    35         if ( is_customize_preview() || is_active_widget( false, false, $this->id_base ) ) {
     35        if ( is_customize_preview() || bp_is_widget_block_active( '', $this->id_base ) ) {
    3636            add_action( 'bp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
    3737        }
  • trunk/src/bp-members/classes/class-bp-core-members-widget.php

    r12976 r12997  
    3838        ) );
    3939
    40         if ( is_customize_preview() || is_active_widget( false, false, $this->id_base ) ) {
     40        if ( is_customize_preview() || bp_is_widget_block_active( '', $this->id_base ) ) {
    4141            add_action( 'bp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
    4242        }
  • trunk/src/bp-templates/bp-legacy/buddypress-functions.php

    r12989 r12997  
    125125        // Only hook the 'sitewide_notices' overlay if the Sitewide
    126126        // Notices widget is not in use (to avoid duplicate content).
    127         if ( bp_is_active( 'messages' ) && ! is_active_widget( false, false, 'bp_messages_sitewide_notices_widget', true ) ) {
     127        if ( bp_is_active( 'messages' ) && ! bp_is_widget_block_active( 'bp/sitewide-notices', 'bp_messages_sitewide_notices_widget', true ) ) {
    128128            add_action( 'wp_footer', array( $this, 'sitewide_notices' ), 9999 );
    129129        }
  • trunk/src/bp-templates/bp-nouveau/includes/functions.php

    r12947 r12997  
    376376 */
    377377function bp_nouveau_is_object_nav_in_sidebar() {
    378     return is_active_widget( false, false, 'bp_nouveau_sidebar_object_nav_widget', true );
     378    return bp_is_widget_block_active( 'bp/primary-nav', 'bp_nouveau_sidebar_object_nav_widget' );
    379379}
    380380
Note: See TracChangeset for help on using the changeset viewer.