Skip to:
Content

BuddyPress.org


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

    r13448 r13450  
    3434     */
    3535    public $current_group;
    36 
    37     /**
    38      * Default group extension.
    39      *
    40      * @since 1.6.0
    41      * @todo Is this used anywhere? Is this a duplicate of $default_extension?
    42      * @var string
    43      */
    44     var $default_component;
    4536
    4637    /**
     
    564555
    565556    /**
     557     * Register component navigation.
     558     *
     559     * @since 12.0.0
     560     *
     561     * @see `BP_Component::register_nav()` for a description of arguments.
     562     *
     563     * @param array $main_nav Optional. See `BP_Component::register_nav()` for description.
     564     * @param array $sub_nav  Optional. See `BP_Component::register_nav()` for description.
     565     */
     566    public function register_nav( $main_nav = array(), $sub_nav = array() ) {
     567        $slug = bp_get_groups_slug();
     568
     569        // Add 'Groups' to the main navigation.
     570        $main_nav = array(
     571            'name'                => _x( 'Groups', 'Group screen nav without counter', 'buddypress' ),
     572            'slug'                => $slug,
     573            'position'            => 70,
     574            'screen_function'     => 'groups_screen_my_groups',
     575            'default_subnav_slug' => 'my-groups',
     576            'item_css_id'         => $this->id
     577        );
     578
     579        // Add the My Groups nav item.
     580        $sub_nav[] = array(
     581            'name'            => __( 'Memberships', 'buddypress' ),
     582            'slug'            => 'my-groups',
     583            'parent_slug'     => $slug,
     584            'screen_function' => 'groups_screen_my_groups',
     585            'position'        => 10,
     586            'item_css_id'     => 'groups-my-groups'
     587        );
     588
     589        if ( bp_is_active( 'groups', 'invitations' ) ) {
     590            // Add the Group Invites nav item.
     591            $sub_nav[] = array(
     592                'name'                     => __( 'Invitations', 'buddypress' ),
     593                'slug'                     => 'invites',
     594                'parent_slug'              => $slug,
     595                'screen_function'          => 'groups_screen_group_invites',
     596                'position'                 => 30,
     597                'user_has_access'          => false,
     598                'user_has_access_callback' => 'bp_core_can_edit_settings',
     599            );
     600        }
     601
     602        parent::register_nav( $main_nav, $sub_nav );
     603    }
     604
     605    /**
    566606     * Set up component navigation.
    567607     *
    568608     * @since 1.5.0
    569      *
    570      * @see BP_Component::setup_nav() for a description of arguments.
    571      *
    572      * @param array $main_nav Optional. See BP_Component::setup_nav() for description.
    573      * @param array $sub_nav  Optional. See BP_Component::setup_nav() for description.
     609     * @since 12.0.0 Used to customize the main navigation name and set
     610     *               a Groups single item navigation.
     611     *
     612     * @see `BP_Component::setup_nav()` for a description of arguments.
     613     *
     614     * @param array $main_nav Optional. See `BP_Component::setup_nav()` for
     615     *                        description.
     616     * @param array $sub_nav  Optional. See `BP_Component::setup_nav()` for
     617     *                        description.
    574618     */
    575619    public function setup_nav( $main_nav = array(), $sub_nav = array() ) {
    576 
    577         if ( is_user_logged_in() || bp_displayed_user_id() ) {
    578             // Only grab count if we're on a user page.
    579             if ( bp_is_user() ) {
    580                 $class = ( 0 === groups_total_groups_for_user( bp_displayed_user_id() ) ) ? 'no-count' : 'count';
    581 
    582                 $nav_name = sprintf(
    583                     /* translators: %s: Group count for the current user */
    584                     _x( 'Groups %s', 'Group screen nav with counter', 'buddypress' ),
    585                     sprintf(
    586                         '<span class="%s">%s</span>',
    587                         esc_attr( $class ),
    588                         bp_get_total_group_count_for_user()
    589                     )
    590                 );
    591             } else {
    592                 $nav_name = _x( 'Groups', 'Group screen nav without counter', 'buddypress' );
    593             }
    594 
    595             $slug   = bp_get_groups_slug();
    596             $access = bp_core_can_edit_settings();
    597 
    598             // Add 'Groups' to the main navigation.
    599             $main_nav = array(
    600                 'name'                => $nav_name,
    601                 'slug'                => $slug,
    602                 'position'            => 70,
    603                 'screen_function'     => 'groups_screen_my_groups',
    604                 'default_subnav_slug' => 'my-groups',
    605                 'item_css_id'         => $this->id
     620        // Only grab count if we're on a user page.
     621        if ( isset( $this->main_nav['name'] ) && bp_is_user() ) {
     622            $class                  = ( 0 === groups_total_groups_for_user( bp_displayed_user_id() ) ) ? 'no-count' : 'count';
     623            $this->main_nav['name'] = sprintf(
     624                /* translators: %s: Group count for the current user */
     625                _x( 'Groups %s', 'Group screen nav with counter', 'buddypress' ),
     626                sprintf(
     627                    '<span class="%s">%s</span>',
     628                    esc_attr( $class ),
     629                    bp_get_total_group_count_for_user()
     630                )
    606631            );
    607 
    608             // Add the My Groups nav item.
    609             $sub_nav[] = array(
    610                 'name'            => __( 'Memberships', 'buddypress' ),
    611                 'slug'            => 'my-groups',
    612                 'parent_slug'     => $slug,
    613                 'screen_function' => 'groups_screen_my_groups',
    614                 'position'        => 10,
    615                 'item_css_id'     => 'groups-my-groups'
    616             );
    617 
    618             if ( bp_is_active( 'groups', 'invitations' ) ) {
    619                 // Add the Group Invites nav item.
    620                 $sub_nav[] = array(
    621                     'name'            => __( 'Invitations', 'buddypress' ),
    622                     'slug'            => 'invites',
    623                     'parent_slug'     => $slug,
    624                     'screen_function' => 'groups_screen_group_invites',
    625                     'user_has_access' => $access,
    626                     'position'        => 30
    627                 );
    628             }
    629 
    630             parent::setup_nav( $main_nav, $sub_nav );
    631632        }
    632633
    633634        if ( bp_is_groups_component() && bp_is_single_item() ) {
    634 
    635             // Reset sub nav.
    636             $sub_nav = array();
    637 
    638635            /*
    639636             * The top-level Groups item is called 'Memberships' for legacy reasons.
     
    661658            );
    662659
    663             // If this is a private group, and the user is not a
    664             // member and does not have an outstanding invitation,
    665             // show a "Request Membership" nav item.
     660            /*
     661             * If this is a private group, and the user is not a member and does not
     662             * have an outstanding invitation, how a "Request Membership" nav item.
     663             */
    666664            if ( bp_current_user_can( 'groups_request_membership', array( 'group_id' => $this->current_group->id ) ) ) {
    667 
    668665                $sub_nav[] = array(
    669666                    'name'            => _x( 'Request Membership','Group screen nav', 'buddypress' ),
     
    676673
    677674            if ( $this->current_group->front_template || bp_is_active( 'activity' ) ) {
    678                 /**
    679                  * If the theme is using a custom front, create activity subnav.
    680                  */
     675                // If the theme is using a custom front, create activity subnav.
    681676                if ( $this->current_group->front_template && bp_is_active( 'activity' ) ) {
    682677                    $sub_nav[] = array(
     
    692687                }
    693688
    694                 /**
    695                  * Only add the members subnav if it's not the home's nav.
    696                  */
     689                // Only add the members subnav if it's not the home's nav.
    697690                $sub_nav[] = array(
    698691                    'name'            => sprintf(
     
    804797                bp_core_new_subnav_item( $nav, 'groups' );
    805798            }
    806         }
    807 
    808         if ( isset( $this->current_group->user_has_access ) ) {
    809 
    810             /**
    811              * Fires at the end of the groups navigation setup if user has access.
    812              *
    813              * @since 1.0.2
    814              *
    815              * @param bool $user_has_access Whether or not user has access.
    816              */
    817             do_action( 'groups_setup_nav', $this->current_group->user_has_access );
    818         } else {
    819 
    820             /** This action is documented in bp-groups/bp-groups-loader.php */
    821             do_action( 'groups_setup_nav');
    822         }
     799
     800            if ( isset( $this->current_group->user_has_access ) ) {
     801
     802                /**
     803                 * Fires at the end of the groups navigation setup if user has access.
     804                 *
     805                 * @since 1.0.2
     806                 *
     807                 * @param bool $user_has_access Whether or not user has access.
     808                 */
     809                do_action( 'groups_setup_nav', $this->current_group->user_has_access );
     810            } else {
     811
     812                /** This action is documented in bp-groups/bp-groups-loader.php */
     813                do_action( 'groups_setup_nav');
     814            }
     815        }
     816
     817        parent::setup_nav( $main_nav, $sub_nav );
    823818    }
    824819
Note: See TracChangeset for help on using the changeset viewer.