Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
07/14/2021 01:50:38 AM (3 months 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

File:
1 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}
Note: See TracChangeset for help on using the changeset viewer.