Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
04/12/2023 10:12:37 PM (17 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-core/classes/class-bp-component.php

    r13442 r13450  
    544544        add_action( 'bp_late_include',           array( $this, 'late_includes'          ) );
    545545
     546        // Generate navigation.
     547        add_action( 'bp_setup_nav',              array( $this, 'register_nav'           ),  7 );
     548
    546549        // Setup navigation.
    547550        add_action( 'bp_setup_nav',              array( $this, 'setup_nav'              ),  9 );
    548 
    549         // Generate navigation.
    550         add_action( 'bp_setup_nav',              array( $this, 'generate_nav'           ), 10, 0 );
    551551
    552552        // Setup WP Toolbar menus.
     
    614614
    615615    /**
    616      * Set up component navigation.
    617      *
    618      * @since 1.5.0
    619      * @since 12.0.0 Uses `BP_Component::$main_nav` && `BP_Component::$sub_nav` to globalize nav items.
     616     * Registers nav items globalizing them into `BP_Component::$main_nav` & `BP_Component::$sub_nav` properties.
     617     *
     618     * @since 12.0.0
    620619     *
    621620     * @param array $main_nav Optional. Passed directly to bp_core_new_nav_item().
     
    625624     *                        function for a description.
    626625     */
    627     public function setup_nav( $main_nav = array(), $sub_nav = array() ) {
     626    public function register_nav( $main_nav = array(), $sub_nav = array() ) {
    628627        if ( isset( $main_nav['slug'] ) ) {
    629628            // Always set the component ID.
     
    658657
    659658    /**
    660      * Generate component navigation using the nav/subnav set up in `BP_Component::setup_nav()`.
    661      *
    662      * @since 12.0.0
    663      *
    664      * @see bp_core_new_nav_item() For a description of the $main_nav
    665      *      parameter formatting.
    666      * @see bp_core_new_subnav_item() For a description of how each item
    667      *      in the $sub_nav parameter array should be formatted.
    668      */
    669     public function generate_nav() {
    670         // No sub nav items without a main nav item.
    671         if ( $this->main_nav ) {
    672             bp_core_new_nav_item( $this->main_nav, 'members' );
     659     * Set up component navigation.
     660     *
     661     * @since 1.5.0
     662     * @since 12.0.0 Uses the registered navigations to generate it.
     663     *
     664     * @param array $main_nav Optional. Passed directly to bp_core_new_nav_item().
     665     *                        See that function for a description.
     666     * @param array $sub_nav  Optional. Multidimensional array, each item in
     667     *                        which is passed to bp_core_new_subnav_item(). See that
     668     *                        function for a description.
     669     */
     670    public function setup_nav( $main_nav = array(), $sub_nav = array() ) {
     671        // Use the registered navigations if available.
     672        if ( empty( $main_nav ) && $this->main_nav ) {
     673            // Don't generate navigation if there's no member.
     674            if ( ! is_user_logged_in() && ! bp_is_user() ) {
     675                return;
     676            }
     677
     678            $generate = true;
     679            if ( isset( $this->main_nav['generate'] ) ) {
     680                $generate = is_callable( $this->main_nav['generate'] ) ? call_user_func( $this->main_nav['generate'] ) : (bool) $this->main_nav['generate'];
     681                unset( $this->main_nav['generate'] );
     682            }
     683
     684            if ( bp_displayed_user_has_front_template() ) {
     685                bp_core_new_nav_item(
     686                    array(
     687                        'name'                => _x( 'Home', 'Member Home page', 'buddypress' ),
     688                        'slug'                => 'front',
     689                        'position'            => 5,
     690                        'screen_function'     => 'bp_members_screen_display_profile',
     691                        'default_subnav_slug' => 'public',
     692                    ),
     693                    'members'
     694                );
     695            }
     696
     697            if ( 'xprofile' === $this->id ) {
     698                $extra_subnavs = wp_list_filter(
     699                    buddypress()->members->sub_nav,
     700                    array(
     701                        'slug'            => 'change-avatar',
     702                        'screen_function' => 'bp_members_screen_change_cover_image',
     703                    ),
     704                    'OR'
     705                );
     706
     707                $this->sub_nav = array_merge( $this->sub_nav, $extra_subnavs );
     708            }
     709
     710            // No sub nav items without a main nav item.
     711            if ( $this->main_nav && $generate) {
     712                if ( isset( $this->main_nav['user_has_access_callback'] ) && is_callable( $this->main_nav['user_has_access_callback'] ) ) {
     713                    $this->main_nav['show_for_displayed_user'] = call_user_func( $this->main_nav['user_has_access_callback'] );
     714                    unset( $this->main_nav['user_has_access_callback'] );
     715                }
     716
     717                bp_core_new_nav_item( $this->main_nav, 'members' );
     718
     719                // Sub nav items are not required.
     720                if ( $this->sub_nav ) {
     721                    foreach( (array) $this->sub_nav as $nav ) {
     722                        if ( isset( $nav['user_has_access_callback'] ) && is_callable( $nav['user_has_access_callback'] ) ) {
     723                            $nav['user_has_access'] = call_user_func( $nav['user_has_access_callback'] );
     724                            unset( $nav['user_has_access_callback'] );
     725                        }
     726
     727                        if ( isset( $nav['generate'] ) ) {
     728                            if ( is_callable( $nav['generate'] ) ) {
     729                                $generate_sub = call_user_func( $nav['generate'] );
     730                            } else {
     731                                $generate_sub = (bool) $nav['generate'];
     732                            }
     733
     734                            unset( $nav['generate'] );
     735
     736                            if ( ! $generate_sub ) {
     737                                continue;
     738                            }
     739                        }
     740
     741                        bp_core_new_subnav_item( $nav, 'members' );
     742                    }
     743                }
     744            }
     745
     746            /*
     747             * If the `$main_nav` is populated, it means a plugin is not registering its navigation using
     748             * `BP_Component::register_nav()` to enjoy the BP Rewrites API slug customization. Let's simply
     749             * preverve backward compatibility in this case.
     750             */
     751        } elseif ( ! empty( $main_nav ) && ! $this->main_nav ) {
     752            // Always set the component ID.
     753            $main_nav['component_id'] = $this->id;
     754            $this->main_nav           = $main_nav;
     755
     756            bp_core_new_nav_item( $main_nav, 'members' );
    673757
    674758            // Sub nav items are not required.
    675             if ( $this->sub_nav ) {
    676                 foreach( (array) $this->sub_nav as $nav ) {
     759            if ( ! empty( $sub_nav ) ) {
     760                $this->sub_nav = $sub_nav;
     761
     762                foreach( (array) $sub_nav as $nav ) {
    677763                    bp_core_new_subnav_item( $nav, 'members' );
    678764                }
Note: See TracChangeset for help on using the changeset viewer.