Skip to:
Content

Ignore:
Timestamp:
05/12/16 17:19:06 (21 months ago)
Author:
boonebgorges
Message:

Introduce new API for BuddyPress navigation.

The new BP_Core_Nav overhauls the way that BuddyPress registers, stores, and
renders navigation items. Navigations are now component-specific, eliminating
the potential for confusion and conflict between navigation items with similar
names in different components, and opening the possibility of generating navs
for separate objects of the same type on a single pageload.

The new nav API replaces the old bp_nav and bp_options_nav system, which
dates from the earliest days of BuddyPress. These global properties were
responsible for handling nav and subnav across all of BP's components. The data
structure of bp_nav and bp_options_nav was simultaneously too opaque (in the
sense of being difficult to approach for developers and not having a complete
interface for programmatic modification) and too transparent (forcing devs to
manipulate the global arrays directly in order to customize navigation). The
new system eliminates most of these problems, by removing direct access to the
underlying navigation data, while providing a full-fledged API for accessing
and modifying that data.

An abstraction layer provides backward compatibility for most legacy uses of
bp_nav and bp_options_nav. Plugins that read data from the globals, modify
the data (eg $bp->bp_nav['foo']['name'] = 'Bar'), and unset nav items via
bp_nav and bp_options_nav should all continue to work as before. Anyone
accessing the globals in this way will see a _doing_it_wrong() notice. This
backward compatibility layer requires SPL (Standard PHP Library), which means
that it will not be enabled on certain configurations running PHP 5.2.x. (SPL
cannot be disabled in PHP 5.3+, and is on by default for earlier versions.)

The new system breaks backward compatibility in a number of small ways. Our
research suggests that these breaks will affect very few customizations, but
we list them here for posterity:

  • Some array functions, such as sort(), will no longer work to modify the nav globals.
  • Subnav items added to nonexistent parents can no longer be successfully registered. Previously, they could be loaded into the global, but were never displayed on the front end.
  • Manual management of group navigation items previously worked by passing the group slug as the parent_slug parameter to the bp_core_*_subnav_item() functions. The new API requires specifying a $component ('members', 'groups') when accessing nav items. To provide compatibility with legacy use - where $component was not required - we make some educated guesses about whether a nav item is "meant" to be attached to a group. This could result in unpredictable behavior in cases where a group slug clashes with a Members navigation item. This has always been broken - see #5103 - but may break in different ways after this changeset.

Props imath, boonebgorges, r-a-y.
Fixes #5103. Fixes #6534.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/testcases/core/nav/bpCoreNewNavItem.php

    r10326 r10745  
    66class BP_Tests_Core_Nav_BpCoreNewNavItem extends BP_UnitTestCase { 
    77 
     8    /** 
     9     * @expectedIncorrectUsage bp_nav 
     10     */ 
    811    public function test_user_nav() { 
    912        $bp_nav = buddypress()->bp_nav; 
     
    3437        ); 
    3538 
    36         $this->assertSame( buddypress()->bp_nav['foo'], $expected ); 
    37  
    38         // Clean up 
    39         buddypress()->bp_nav = $bp_nav; 
    40         $this->set_current_user( $old_current_user ); 
    41     } 
    42  
     39        foreach ( $expected as $k => $v ) { 
     40            $this->assertEquals( $v, buddypress()->bp_nav['foo'][ $k ] ); 
     41        } 
     42 
     43        // Clean up 
     44        buddypress()->bp_nav = $bp_nav; 
     45        $this->set_current_user( $old_current_user ); 
     46    } 
     47 
     48    /** 
     49     * @expectedIncorrectUsage bp_nav 
     50     */ 
    4351    public function test_group_nav() { 
    4452        $bp_nav = buddypress()->bp_nav; 
     
    9098    } 
    9199 
     100    /** 
     101     * @expectedIncorrectUsage bp_nav 
     102     */ 
    92103    public function test_css_id_should_fall_back_on_slug() { 
    93104        $args = array( 
     
    100111    } 
    101112 
     113    /** 
     114     * @expectedIncorrectUsage bp_nav 
     115     */ 
    102116    public function test_css_id_should_be_respected() { 
    103117        $args = array( 
     
    125139    } 
    126140 
     141    /** 
     142     * @expectedIncorrectUsage bp_nav 
     143     */ 
    127144    public function test_existence_of_access_protected_user_nav() { 
    128145        $bp_nav = buddypress()->bp_nav; 
     
    146163        ); 
    147164 
    148         $this->assertSame( buddypress()->bp_nav['settings'], $expected ); 
    149  
    150         // Clean up 
    151         buddypress()->bp_nav = $bp_nav; 
    152         $this->set_current_user( $old_current_user ); 
    153     } 
    154  
     165        foreach ( $expected as $k => $v ) { 
     166            $this->assertEquals( $v, buddypress()->bp_nav['settings'][ $k ] ); 
     167        } 
     168 
     169        // Clean up 
     170        buddypress()->bp_nav = $bp_nav; 
     171        $this->set_current_user( $old_current_user ); 
     172    } 
     173 
     174    /** 
     175     * @expectedIncorrectUsage bp_nav 
     176     */ 
    155177    public function test_creation_of_access_protected_user_nav() { 
    156178        // The nav item must be added to bp_nav, even if the current user 
     
    185207        ); 
    186208 
    187         $this->assertSame( buddypress()->bp_nav['woof'], $expected ); 
     209        foreach ( $expected as $k => $v ) { 
     210            $this->assertEquals( $v, buddypress()->bp_nav['woof'][ $k ] ); 
     211        } 
    188212 
    189213        // Clean up 
Note: See TracChangeset for help on using the changeset viewer.