Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
11/19/2021 05:03:56 AM (2 years ago)
Author:
imath
Message:

Only load BP Legacy Widgets when the Widgets Block editor is disabled

To improve the Widgets Block editor's users experience, we are making a new step towards deprecating BP Legacy Widgets by not loading them by default. As a result, into this editor, only BP Widget Blocks will be loaded and listed into the Block inserter of the Widgets screen.

If users want to keep the Legacy Widgets into the Widgets Block editor, they can do so returning true to this filter 'bp_core_retain_legacy_widgets'.

If users are using the Classic Widgets Editor plugin or if advanced users are directly filtering 'use_widgets_block_editor' to return false: Legacy Widgets will still be available into the Classic Widgets Editor.

Fixes #8594

File:
1 edited

Legend:

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

    r11366 r13153  
    1212
    1313/**
     14 * Should BuddyPress load Legacy Widgets?
     15 *
     16 * @since 10.0.0
     17 *
     18 * @return bool False if BuddyPress shouldn't load Legacy Widgets. True otherwise.
     19 */
     20function bp_core_retain_legacy_widgets() {
     21    $theme_supports = current_theme_supports( 'widgets-block-editor' );
     22    $wp_supports    = bp_is_running_wp( '5.8.0' );
     23
     24    /** This filter is documented in wp-includes/widgets.php */
     25    $block_widgets_enabled = $theme_supports && apply_filters( 'use_widgets_block_editor', $wp_supports );
     26
     27    $retain_legacy_widgets = true;
     28    if ( $block_widgets_enabled ) {
     29        $retain_legacy_widgets = false;
     30    }
     31
     32    /**
     33     * Filter here to force Legacy Widgets to be retained or not.
     34     *
     35     * @since 10.0.0
     36     *
     37     * @param bool $retain_legacy_widgets False if BuddyPress shouldn't load Legacy Widgets. True otherwise.
     38     */
     39    return apply_filters( 'bp_core_retain_legacy_widgets', $retain_legacy_widgets );
     40}
     41
     42/**
     43 * Registers the Login widget.
     44 *
     45 * @since 10.0.0
     46 */
     47function bp_core_register_login_widget() {
     48    register_widget( 'BP_Core_Login_Widget' );
     49}
     50
     51/**
    1452 * Register bp-core widgets.
    1553 *
     
    1755 */
    1856function bp_core_register_widgets() {
    19     add_action( 'widgets_init', function() { register_widget( 'BP_Core_Login_Widget' ); } );
     57    add_action( 'widgets_init', 'bp_core_register_login_widget' );
    2058}
    2159add_action( 'bp_register_widgets', 'bp_core_register_widgets' );
     60
     61/**
     62 * Checks whether BuddyPress should unhook Legacy Widget registrations.
     63 *
     64 * @since 10.0.0
     65 */
     66function bp_core_maybe_unhook_legacy_widgets() {
     67    if ( bp_core_retain_legacy_widgets() ) {
     68        return;
     69    }
     70
     71    $callbacks = array(
     72        'bp_core_register_login_widget',
     73        'bp_members_register_members_widget',
     74        'bp_members_register_whos_online_widget',
     75        'bp_members_register_recently_active_widget',
     76    );
     77
     78    if ( bp_is_active( 'friends' ) ) {
     79        $callbacks[] = 'bp_friends_register_friends_widget';
     80    }
     81
     82    if ( bp_is_active( 'groups' ) ) {
     83        $callbacks[] = 'bp_groups_register_groups_widget';
     84    }
     85
     86    if ( bp_is_active( 'messages' ) ) {
     87        $callbacks[] = 'bp_messages_register_sitewide_notices_widget';
     88    }
     89
     90    if ( bp_is_active( 'blogs' ) && bp_is_active( 'activity' ) && bp_is_root_blog() ) {
     91        $callbacks[] = 'bp_blogs_register_recent_posts_widget';
     92    }
     93
     94    foreach ( $callbacks as $callback ) {
     95        remove_action( 'widgets_init', $callback );
     96    }
     97}
     98add_action( 'widgets_init', 'bp_core_maybe_unhook_legacy_widgets', 0 );
Note: See TracChangeset for help on using the changeset viewer.