Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
04/12/2023 10:12:37 PM (21 months ago)
Author:
imath
Message:

Administration: add a new settings tab to manage slugs customization

Compared to [13442], change the logic of Components user navigation
generation by introducing a BP_Component::register_nav() method to
globalize the nav items early (ie: the registration step) and make them
available for the new settings tab to manage slugs customization.

After a second thought, the BP_Component::setup_nav() should remain the
navigation generation step instead of playing the registration role. This
will maximize backward compatibility & third party plugins wishing their
slugs to be customizable will need to "opt-in" for BP Rewrites using the
BP_Component::register_nav() method.

This first version of the URLs settings tab does not handle slugs
customization yet, its first usage is to make sure all BP Components user
navigation slugs were registered & to put the Accordion UI in place.

Props r-a-y, johnjamesjacoby, boonebgorges

Closes https://github.com/buddypress/buddypress/pull/84
See #4954

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-notifications/classes/class-bp-notifications-component.php

    r13442 r13450  
    134134
    135135    /**
     136     * Register component navigation.
     137     *
     138     * @since 12.0.0
     139     *
     140     * @see `BP_Component::register_nav()` for a description of arguments.
     141     *
     142     * @param array $main_nav Optional. See `BP_Component::register_nav()` for
     143     *                        description.
     144     * @param array $sub_nav  Optional. See `BP_Component::register_nav()` for
     145     *                        description.
     146     */
     147    public function register_nav( $main_nav = array(), $sub_nav = array() ) {
     148        $slug   = bp_get_notifications_slug();
     149
     150        // Add 'Notifications' to the main navigation.
     151        $main_nav = array(
     152            'name'                     => _x( 'Notifications', 'Profile screen nav', 'buddypress' ),
     153            'slug'                     => $slug,
     154            'position'                 => 30,
     155            'show_for_displayed_user'  => false,
     156            'screen_function'          => 'bp_notifications_screen_unread',
     157            'default_subnav_slug'      => 'unread',
     158            'item_css_id'              => $this->id,
     159            'user_has_access_callback' => 'bp_core_can_edit_settings',
     160        );
     161
     162        // Add the subnav items to the notifications nav item.
     163        $sub_nav[] = array(
     164            'name'                     => _x( 'Unread', 'Notification screen nav', 'buddypress' ),
     165            'slug'                     => 'unread',
     166            'parent_slug'              => $slug,
     167            'screen_function'          => 'bp_notifications_screen_unread',
     168            'position'                 => 10,
     169            'item_css_id'              => 'notifications-my-notifications',
     170            'user_has_access'          => false,
     171            'user_has_access_callback' => 'bp_core_can_edit_settings',
     172        );
     173
     174        $sub_nav[] = array(
     175            'name'                     => _x( 'Read', 'Notification screen nav', 'buddypress' ),
     176            'slug'                     => 'read',
     177            'parent_slug'              => $slug,
     178            'screen_function'          => 'bp_notifications_screen_read',
     179            'position'                 => 20,
     180            'user_has_access'          => false,
     181            'user_has_access_callback' => 'bp_core_can_edit_settings',
     182        );
     183
     184        parent::register_nav( $main_nav, $sub_nav );
     185    }
     186
     187    /**
    136188     * Set up component navigation.
    137189     *
    138190     * @since 1.9.0
    139      *
    140      * @see BP_Component::setup_nav() for a description of arguments.
    141      *
    142      * @param array $main_nav Optional. See BP_Component::setup_nav() for
     191     * @since 12.0.0 Used to customize the main navigation name.
     192     *
     193     * @see `BP_Component::setup_nav()` for a description of arguments.
     194     *
     195     * @param array $main_nav Optional. See `BP_Component::setup_nav()` for
    143196     *                        description.
    144      * @param array $sub_nav  Optional. See BP_Component::setup_nav() for
     197     * @param array $sub_nav  Optional. See `BP_Component::setup_nav()` for
    145198     *                        description.
    146199     */
    147200    public function setup_nav( $main_nav = array(), $sub_nav = array() ) {
    148 
    149         // Stop if there is no user displayed or logged in.
    150         if ( ! is_user_logged_in() && ! bp_displayed_user_id() ) {
    151             return;
    152         }
    153 
    154         $access = bp_core_can_edit_settings();
    155         $slug   = bp_get_notifications_slug();
    156 
    157201        // Only grab count if we're on a user page and current user has access.
    158         if ( bp_is_user() && bp_user_has_access() ) {
    159             $count    = bp_notifications_get_unread_notification_count( bp_displayed_user_id() );
    160             $class    = ( 0 === $count ) ? 'no-count' : 'count';
    161             $nav_name = sprintf(
     202        if ( isset( $this->main_nav['name'] ) && bp_is_user() && bp_user_has_access() ) {
     203            $count                  = bp_notifications_get_unread_notification_count( bp_displayed_user_id() );
     204            $class                  = ( 0 === $count ) ? 'no-count' : 'count';
     205            $this->main_nav['name'] = sprintf(
    162206                /* translators: %s: Unread notification count for the current user */
    163207                _x( 'Notifications %s', 'Profile screen nav', 'buddypress' ),
     
    168212                )
    169213            );
    170         } else {
    171             $nav_name = _x( 'Notifications', 'Profile screen nav', 'buddypress' );
    172         }
    173 
    174         // Add 'Notifications' to the main navigation.
    175         $main_nav = array(
    176             'name'                    => $nav_name,
    177             'slug'                    => $slug,
    178             'position'                => 30,
    179             'show_for_displayed_user' => $access,
    180             'screen_function'         => 'bp_notifications_screen_unread',
    181             'default_subnav_slug'     => 'unread',
    182             'item_css_id'             => $this->id,
    183         );
    184 
    185         // Add the subnav items to the notifications nav item.
    186         $sub_nav[] = array(
    187             'name'            => _x( 'Unread', 'Notification screen nav', 'buddypress' ),
    188             'slug'            => 'unread',
    189             'parent_slug'     => $slug,
    190             'screen_function' => 'bp_notifications_screen_unread',
    191             'position'        => 10,
    192             'item_css_id'     => 'notifications-my-notifications',
    193             'user_has_access' => $access,
    194         );
    195 
    196         $sub_nav[] = array(
    197             'name'            => _x( 'Read', 'Notification screen nav', 'buddypress' ),
    198             'slug'            => 'read',
    199             'parent_slug'     => $slug,
    200             'screen_function' => 'bp_notifications_screen_read',
    201             'position'        => 20,
    202             'user_has_access' => $access,
    203         );
     214        }
    204215
    205216        parent::setup_nav( $main_nav, $sub_nav );
Note: See TracChangeset for help on using the changeset viewer.