Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
03/27/2023 06:19:06 PM (13 months ago)
Author:
imath
Message:

Improve Components Member's single item navigation generation

  • Edit the BP_Component Class so that it globalize navigation items before registering it.
  • Introduce bp_get_component_navigations(), a new function that will be used to get Member's single navigation customizable slugs within the BuddyPress settings area.
  • Perform all remaining bp_loggedin_user_domain() replacements (55) in favor of the bp_loggedin_user_url() function which uses BP Rewrites to build URLs.
  • Improve bp_loggedin_user_link() by adding a new $chunks array of arguments to output escaped URL in templates.
  • Adapt some PHPUnit testcases.

Props r-a-y, johnjamesjacoby, boonebgorges

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

File:
1 edited

Legend:

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

    r13432 r13442  
    187187     */
    188188    public $directory_title = '';
     189
     190    /**
     191     * Component's main nav items.
     192     *
     193     * @since 12.0.0
     194     * @var array
     195     */
     196    public $main_nav = array();
     197
     198    /**
     199     * Component's main nav sub items.
     200     *
     201     * @since 12.0.0
     202     * @var array
     203     */
     204    public $sub_nav  = array();
    189205
    190206    /** Methods ***************************************************************/
     
    529545
    530546        // Setup navigation.
    531         add_action( 'bp_setup_nav',              array( $this, 'setup_nav'              ), 10 );
     547        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 );
    532551
    533552        // Setup WP Toolbar menus.
     
    598617     *
    599618     * @since 1.5.0
    600      *
    601      * @see bp_core_new_nav_item() For a description of the $main_nav
    602      *      parameter formatting.
    603      * @see bp_core_new_subnav_item() For a description of how each item
    604      *      in the $sub_nav parameter array should be formatted.
     619     * @since 12.0.0 Uses `BP_Component::$main_nav` && `BP_Component::$sub_nav` to globalize nav items.
    605620     *
    606621     * @param array $main_nav Optional. Passed directly to bp_core_new_nav_item().
     
    611626     */
    612627    public function setup_nav( $main_nav = array(), $sub_nav = array() ) {
    613 
     628        if ( isset( $main_nav['slug'] ) ) {
     629            // Always set the component ID.
     630            $this->main_nav['component_id'] = $this->id;
     631
     632            if ( ! isset( $main_nav['rewrite_id'] ) ) {
     633                $this->main_nav['rewrite_id'] = 'bp_member_' . str_replace( '-', '_', $main_nav['slug'] );
     634            } elseif ( ! $main_nav['rewrite_id'] ) {
     635                unset( $main_nav['rewrite_id'] );
     636            }
     637
     638            $this->main_nav = array_merge( $this->main_nav, $main_nav );
     639
     640            // Sub nav items are not required.
     641            if ( ! empty( $sub_nav ) ) {
     642                foreach( (array) $sub_nav as $nav ) {
     643                    if ( ! isset( $nav['slug'], $nav['parent_slug'] ) ) {
     644                        continue;
     645                    }
     646
     647                    if ( ! isset( $nav['rewrite_id'] ) ) {
     648                        $nav['rewrite_id'] = 'bp_member_' . str_replace( '-', '_', $nav['parent_slug'] ) . '_' . str_replace( '-', '_', $nav['slug'] );
     649                    } elseif ( ! $nav['rewrite_id'] ) {
     650                        unset( $nav['rewrite_id'] );
     651                    }
     652
     653                    $this->sub_nav[] = $nav;
     654                }
     655            }
     656        }
     657    }
     658
     659    /**
     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() {
    614670        // No sub nav items without a main nav item.
    615         if ( !empty( $main_nav ) ) {
    616             // Always set the component ID.
    617             $main_nav['component_id'] = $this->id;
    618 
    619             bp_core_new_nav_item( $main_nav, 'members' );
     671        if ( $this->main_nav ) {
     672            bp_core_new_nav_item( $this->main_nav, 'members' );
    620673
    621674            // Sub nav items are not required.
    622             if ( !empty( $sub_nav ) ) {
    623                 foreach( (array) $sub_nav as $nav ) {
     675            if ( $this->sub_nav ) {
     676                foreach( (array) $this->sub_nav as $nav ) {
    624677                    bp_core_new_subnav_item( $nav, 'members' );
    625678                }
Note: See TracChangeset for help on using the changeset viewer.