Skip to:
Content

BuddyPress.org

Changeset 13153


Ignore:
Timestamp:
11/19/2021 05:03:56 AM (3 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

Location:
trunk/src
Files:
3 added
9 edited

Legend:

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

    r11366 r13153  
    1212
    1313/**
     14 * Registers the Recent Posts Legacy Widget.
     15 *
     16 * @since 10.0.0
     17 */
     18function bp_blogs_register_recent_posts_widget() {
     19    register_widget( 'BP_Blogs_Recent_Posts_Widget' );
     20}
     21
     22/**
    1423 * Register the widgets for the Blogs component.
    1524 */
     
    1827
    1928    if ( bp_is_active( 'activity' ) && bp_is_root_blog( $wpdb->blogid ) ) {
    20         add_action( 'widgets_init', function() { register_widget( 'BP_Blogs_Recent_Posts_Widget' ); } );
     29        add_action( 'widgets_init', 'bp_blogs_register_recent_posts_widget' );
    2130    }
    2231}
  • 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 );
  • trunk/src/bp-friends/bp-friends-widgets.php

    r13092 r13153  
    1010// Exit if accessed directly.
    1111defined( 'ABSPATH' ) || exit;
     12
     13/**
     14 * Registers the Friends Legacy Widget.
     15 *
     16 * @since 10.0.0
     17 */
     18function bp_friends_register_friends_widget() {
     19    register_widget( 'BP_Core_Friends_Widget' );
     20}
    1221
    1322/**
     
    2837    }
    2938
    30     add_action(
    31         'widgets_init',
    32         function() {
    33             register_widget( 'BP_Core_Friends_Widget' );
    34         }
    35     );
     39    add_action( 'widgets_init', 'bp_friends_register_friends_widget' );
    3640}
    3741add_action( 'bp_register_widgets', 'bp_friends_register_widgets' );
  • trunk/src/bp-groups/bp-groups-widgets.php

    r12791 r13153  
    1212
    1313/**
     14 * Registers the Groups Legacy Widget.
     15 *
     16 * @since 10.0.0
     17 */
     18function bp_groups_register_groups_widget() {
     19    register_widget( 'BP_Groups_Widget' );
     20}
     21
     22/**
    1423 * Register widgets for groups component.
    1524 *
     
    1726 */
    1827function groups_register_widgets() {
    19     add_action( 'widgets_init', function() { register_widget( 'BP_Groups_Widget' ); } );
     28    add_action( 'widgets_init', 'bp_groups_register_groups_widget' );
    2029}
    2130add_action( 'bp_register_widgets', 'groups_register_widgets' );
  • trunk/src/bp-members/bp-members-widgets.php

    r13105 r13153  
    1212
    1313/**
     14 * Registers the Members Legacy Widget.
     15 *
     16 * @since 10.0.0
     17 */
     18function bp_members_register_members_widget() {
     19    register_widget( 'BP_Core_Members_Widget' );
     20}
     21
     22/**
     23 * Registers the "Who's online?" Legacy Widget.
     24 *
     25 * @since 10.0.0
     26 */
     27function bp_members_register_whos_online_widget() {
     28    register_widget( 'BP_Core_Whos_Online_Widget' );
     29}
     30
     31/**
     32 * Registers the "Recently Active" Legacy Widget.
     33 *
     34 * @since 10.0.0
     35 */
     36function bp_members_register_recently_active_widget() {
     37    register_widget( 'BP_Core_Recently_Active_Widget' );
     38}
     39
     40/**
    1441 * Register bp-members widgets.
    1542 *
     
    1946 */
    2047function bp_members_register_widgets() {
    21     add_action(
    22         'widgets_init',
    23         function() {
    24             return register_widget( 'BP_Core_Members_Widget' );
    25         }
    26     );
    27 
    28     add_action(
    29         'widgets_init',
    30         function() {
    31             return register_widget( 'BP_Core_Whos_Online_Widget' );
    32         }
    33     );
    34 
    35     add_action(
    36         'widgets_init',
    37         function() {
    38             return register_widget( 'BP_Core_Recently_Active_Widget' );
    39         }
    40     );
     48    add_action( 'widgets_init', 'bp_members_register_members_widget' );
     49    add_action( 'widgets_init', 'bp_members_register_whos_online_widget' );
     50    add_action( 'widgets_init', 'bp_members_register_recently_active_widget' );
    4151}
    4252add_action( 'bp_register_widgets', 'bp_members_register_widgets' );
  • trunk/src/bp-messages/bp-messages-widgets.php

    r13096 r13153  
    1212
    1313/**
     14 * Registers the Sitewide Notices Legacy Widget.
     15 *
     16 * @since 10.0.0
     17 */
     18function bp_messages_register_sitewide_notices_widget() {
     19    register_widget( 'BP_Messages_Sitewide_Notices_Widget' );
     20}
     21
     22/**
    1423 * Register widgets for the Messages component.
    1524 *
     
    1726 */
    1827function bp_messages_register_widgets() {
    19     add_action(
    20         'widgets_init',
    21         function() {
    22             register_widget( 'BP_Messages_Sitewide_Notices_Widget' );
    23         }
    24     );
     28    add_action( 'widgets_init', 'bp_messages_register_sitewide_notices_widget' );
    2529}
    2630add_action( 'bp_register_widgets', 'bp_messages_register_widgets' );
  • trunk/src/bp-templates/bp-nouveau/buddypress-functions.php

    r13145 r13153  
    218218
    219219        // Register the Primary Object nav widget.
    220         add_action( 'bp_widgets_init', array( 'BP_Nouveau_Object_Nav_Widget', 'register_widget' ) );
     220        if ( bp_core_retain_legacy_widgets() ) {
     221            add_action( 'bp_widgets_init', array( 'BP_Nouveau_Object_Nav_Widget', 'register_widget' ) );
     222        }
    221223
    222224        // Set the BP Uri for the Ajax customizer preview.
  • trunk/src/bp-templates/bp-nouveau/includes/activity/loader.php

    r12907 r13153  
    6161        require $this->dir . 'functions.php';
    6262        require $this->dir . 'template-tags.php';
    63         require $this->dir . 'widgets.php';
     63
     64        if ( bp_core_retain_legacy_widgets() ) {
     65            require $this->dir . 'widgets.php';
     66        }
    6467
    6568        // Test suite requires the AJAX functions early.
     
    8891    protected function setup_actions() {
    8992        add_action( 'bp_nouveau_enqueue_scripts', 'bp_nouveau_activity_enqueue_scripts' );
    90         add_action( 'bp_widgets_init', array( 'BP_Latest_Activities', 'register_widget' ) );
    9193        add_action( 'bp_nouveau_notifications_init_filters', 'bp_nouveau_activity_notification_filters' );
     94
     95        if ( bp_core_retain_legacy_widgets() ) {
     96            add_action( 'bp_widgets_init', array( 'BP_Latest_Activities', 'register_widget' ) );
     97        }
    9298
    9399        $bp = buddypress();
  • trunk/src/bp-templates/bp-nouveau/includes/classes.php

    r12983 r13153  
    44 *
    55 * @since 3.0.0
    6  * @version 9.0.0
     6 * @version 10.0.0
    77 */
    88
     
    1010defined( 'ABSPATH' ) || exit;
    1111
    12 /**
    13  * Builds a group of BP_Button
    14  *
    15  * @since 3.0.0
    16  */
    17 class BP_Buttons_Group {
     12require plugin_dir_path( __FILE__ ) . 'classes/class-bp-buttons-group.php';
    1813
    19     /**
    20      * The parameters of the Group of buttons
    21      *
    22      * @var array
    23      */
    24     protected $group = array();
    25 
    26     /**
    27      * Constructor
    28      *
    29      * @since 3.0.0
    30      *
    31      * @param array $args Optional array having the following parameters {
    32      *     @type string $id                A string to use as the unique ID for the button. Required.
    33      *     @type int    $position          Where to insert the Button. Defaults to 99.
    34      *     @type string $component         The Component's the button is build for (eg: Activity, Groups..). Required.
    35      *     @type bool   $must_be_logged_in Whether the button should only be displayed to logged in users. Defaults to True.
    36      *     @type bool   $block_self        Optional. True if the button should be hidden when a user is viewing his own profile.
    37      *                                     Defaults to False.
    38      *     @type string $parent_element    Whether to use a wrapper. Defaults to false.
    39      *     @type string $parent_attr       set an array of attributes for the parent element.
    40      *     @type string $button_element    Set this to 'button', 'img', or 'a', defaults to anchor.
    41      *     @type string $button_attr       Any attributes required for the button_element
    42      *     @type string $link_text         The text of the link. Required.
    43      * }
    44      */
    45     public function __construct( $args = array() ) {
    46         foreach ( $args as $arg ) {
    47             $this->add( $arg );
    48         }
    49     }
    50 
    51 
    52     /**
    53      * Sort the Buttons of the group according to their position attribute
    54      *
    55      * @since 3.0.0
    56      *
    57      * @param array the list of buttons to sort.
    58      *
    59      * @return array the list of buttons sorted.
    60      */
    61     public function sort( $buttons ) {
    62         $sorted = array();
    63 
    64         foreach ( $buttons as $button ) {
    65             $position = 99;
    66 
    67             if ( isset( $button['position'] ) ) {
    68                 $position = (int) $button['position'];
    69             }
    70 
    71             // If position is already taken, move to the first next available
    72             if ( isset( $sorted[ $position ] ) ) {
    73                 $sorted_keys = array_keys( $sorted );
    74 
    75                 do {
    76                     $position += 1;
    77                 } while ( in_array( $position, $sorted_keys, true ) );
    78             }
    79 
    80             $sorted[ $position ] = $button;
    81         }
    82 
    83         ksort( $sorted );
    84         return $sorted;
    85     }
    86 
    87     /**
    88      * Get the BuddyPress buttons for the group
    89      *
    90      * @since 3.0.0
    91      *
    92      * @param bool $sort whether to sort the buttons or not.
    93      *
    94      * @return array An array of HTML links.
    95      */
    96     public function get( $sort = true ) {
    97         $buttons = array();
    98 
    99         if ( empty( $this->group ) ) {
    100             return $buttons;
    101         }
    102 
    103         if ( true === $sort ) {
    104             $this->group = $this->sort( $this->group );
    105         }
    106 
    107         foreach ( $this->group as $key_button => $button ) {
    108             // Reindex with ids.
    109             if ( true === $sort ) {
    110                 $this->group[ $button['id'] ] = $button;
    111                 unset( $this->group[ $key_button ] );
    112             }
    113 
    114             $buttons[ $button['id'] ] = bp_get_button( $button );
    115         }
    116 
    117         return $buttons;
    118     }
    119 
    120     /**
    121      * Update the group of buttons
    122      *
    123      * @since 3.0.0
    124      *
    125      * @param array $args Optional. See the __constructor for a description of this argument.
    126      */
    127     public function update( $args = array() ) {
    128         foreach ( $args as $id => $params ) {
    129             if ( isset( $this->group[ $id ] ) ) {
    130                 $this->group[ $id ] = bp_parse_args(
    131                     $params,
    132                     $this->group[ $id ],
    133                     'buttons_group_update'
    134                 );
    135             } else {
    136                 $this->add( $params );
    137             }
    138         }
    139     }
    140 
    141     /**
    142      * Adds a button.
    143      *
    144      * @since 9.0.0
    145      *
    146      * @param array $args Required. See the __constructor for a description of this argument.
    147      * @return bool true on success, false on failure to add.
    148      */
    149     private function add( $args ) {
    150         $r = bp_parse_args(
    151             (array) $args,
    152             array(
    153                 'id'                => '',
    154                 'position'          => 99,
    155                 'component'         => '',
    156                 'must_be_logged_in' => true,
    157                 'block_self'        => false,
    158                 'parent_element'    => false,
    159                 'parent_attr'       => array(),
    160                 'button_element'    => 'a',
    161                 'button_attr'       => array(),
    162                 'link_text'         => '',
    163             ),
    164             'buttons_group_constructor'
    165         );
    166 
    167         // Just don't set the button if a param is missing
    168         if ( empty( $r['id'] ) || empty( $r['component'] ) || empty( $r['link_text'] ) ) {
    169             return false;
    170         }
    171 
    172         $r['id'] = sanitize_key( $r['id'] );
    173 
    174         // If the button already exist don't add it
    175         if ( isset( $this->group[ $r['id'] ] ) ) {
    176             return false;
    177         }
    178 
    179         /*
    180          * If, in bp_nouveau_get_*_buttons(), we pass through a false value for 'parent_element'
    181          * but we have attributtes for it in the array, let's default to setting a div.
    182          *
    183          * Otherwise, the original false value will be passed through to BP buttons.
    184          * @todo: this needs review, probably trying to be too clever
    185          */
    186         if ( ( ! empty( $r['parent_attr'] ) ) && false === $r['parent_element'] ) {
    187             $r['parent_element'] = 'div';
    188         }
    189 
    190         $this->group[ $r['id'] ] = $r;
    191         return true;
    192     }
     14if ( bp_core_retain_legacy_widgets() ) {
     15    require plugin_dir_path( __FILE__ ) . 'classes/class-bp-nouveau-object-nav-widget.php';
    19316}
    194 
    195 /**
    196  * BP Sidebar Item Nav_Widget
    197  *
    198  * Adds a widget to move avatar/item nav into the sidebar
    199  *
    200  * @since 3.0.0
    201  *
    202  * @uses WP_Widget
    203  */
    204 class BP_Nouveau_Object_Nav_Widget extends WP_Widget {
    205     /**
    206      * Constructor
    207      *
    208      * @since 3.0.0
    209      * @since 9.0.0 Adds the `show_instance_in_rest` property to Widget options.
    210      */
    211     public function __construct() {
    212         $widget_ops = array(
    213             'description'           => __( 'Displays BuddyPress primary nav in the sidebar of your site. Make sure to use it as the first widget of the sidebar and only once.', 'buddypress' ),
    214             'classname'             => 'widget_nav_menu buddypress_object_nav',
    215             'show_instance_in_rest' => true,
    216         );
    217 
    218         parent::__construct(
    219             'bp_nouveau_sidebar_object_nav_widget',
    220             __( '(BuddyPress) Primary navigation', 'buddypress' ),
    221             $widget_ops
    222         );
    223     }
    224 
    225     /**
    226      * Register the widget
    227      *
    228      * @since 3.0.0
    229      */
    230     public static function register_widget() {
    231         register_widget( 'BP_Nouveau_Object_Nav_Widget' );
    232     }
    233 
    234     /**
    235      * Displays the output, the button to post new support topics
    236      *
    237      * @since 3.0.0
    238      *
    239      * @param mixed   $args     Arguments
    240      * @param unknown $instance
    241      */
    242     public function widget( $args, $instance ) {
    243         if ( ! is_buddypress() || bp_is_group_create() ) {
    244             return;
    245         }
    246 
    247         /**
    248          * Filters the nav widget args for the BP_Nouveau_Object_Nav_Widget widget.
    249          *
    250          * @since 3.0.0
    251          *
    252          * @param array $value Array of arguments {
    253          *     @param bool $bp_nouveau_widget_title Whether or not to assign a title for the widget.
    254          * }
    255          */
    256         $item_nav_args = bp_parse_args(
    257             $instance,
    258             apply_filters(
    259                 'bp_nouveau_object_nav_widget_args',
    260                 array( 'bp_nouveau_widget_title' => true )
    261             ),
    262             'widget_object_nav'
    263         );
    264 
    265         $title = '';
    266 
    267         if ( ! empty( $item_nav_args['bp_nouveau_widget_title'] ) ) {
    268             if ( bp_is_group() ) {
    269                 $title = bp_get_current_group_name();
    270             } elseif ( bp_is_user() ) {
    271                 $title = bp_get_displayed_user_fullname();
    272             } elseif ( bp_get_directory_title( bp_current_component() ) ) {
    273                 $title = bp_get_directory_title( bp_current_component() );
    274             }
    275         }
    276 
    277         /**
    278          * Filters the BP_Nouveau_Object_Nav_Widget widget title.
    279          *
    280          * @since 3.0.0
    281          *
    282          * @param string $title    The widget title.
    283          * @param array  $instance The settings for the particular instance of the widget.
    284          * @param string $id_base  Root ID for all widgets of this type.
    285          */
    286         $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
    287 
    288         echo $args['before_widget'];
    289 
    290         if ( ! empty( $title ) ) {
    291             echo $args['before_title'] . $title . $args['after_title'];
    292         }
    293 
    294         if ( bp_is_user() ) {
    295             bp_get_template_part( 'members/single/parts/item-nav' );
    296         } elseif ( bp_is_group() ) {
    297             bp_get_template_part( 'groups/single/parts/item-nav' );
    298         } elseif ( bp_is_directory() ) {
    299             bp_get_template_part( 'common/nav/directory-nav' );
    300         }
    301 
    302         echo $args['after_widget'];
    303     }
    304 
    305     /**
    306      * Update the new support topic widget options (title)
    307      *
    308      * @since 3.0.0
    309      *
    310      * @param array $new_instance The new instance options
    311      * @param array $old_instance The old instance options
    312      *
    313      * @return array the instance
    314      */
    315     public function update( $new_instance, $old_instance ) {
    316         $instance                            = $old_instance;
    317         $instance['bp_nouveau_widget_title'] = (bool) $new_instance['bp_nouveau_widget_title'];
    318 
    319         return $instance;
    320     }
    321 
    322     /**
    323      * Output the new support topic widget options form
    324      *
    325      * @since 3.0.0
    326      *
    327      * @param $instance Instance
    328      *
    329      * @return string HTML Output
    330      */
    331     public function form( $instance ) {
    332         $defaults = array(
    333             'bp_nouveau_widget_title' => true,
    334         );
    335 
    336         $instance = bp_parse_args(
    337             (array) $instance,
    338             $defaults,
    339             'widget_object_nav_form'
    340         );
    341 
    342         $bp_nouveau_widget_title = (bool) $instance['bp_nouveau_widget_title'];
    343         ?>
    344 
    345         <p>
    346             <input class="checkbox" type="checkbox" <?php checked( $bp_nouveau_widget_title, true ); ?> id="<?php echo $this->get_field_id( 'bp_nouveau_widget_title' ); ?>" name="<?php echo $this->get_field_name( 'bp_nouveau_widget_title' ); ?>" />
    347             <label for="<?php echo $this->get_field_id( 'bp_nouveau_widget_title' ); ?>"><?php esc_html_e( 'Include navigation title', 'buddypress' ); ?></label>
    348         </p>
    349 
    350         <?php
    351     }
    352 }
Note: See TracChangeset for help on using the changeset viewer.