Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
05/12/2016 05:19:06 PM (8 years 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/bpCoreMaybeHookNewSubnavScreenFunction.php

    r9988 r10745  
    8383        $old_default_component = buddypress()->default_component;
    8484        buddypress()->default_component = 'foo';
    85         buddypress()->bp_nav = array(
    86             'foo' => array(
    87                 'show_for_displayed_user' => true,
    88             ),
    89         );
     85
     86        bp_core_new_nav_item( array(
     87            'slug' => 'foo',
     88            'name' => 'Foo',
     89            'screen_function' => 'foo',
     90            'default_subnav_item' => 'bar',
     91        ) );
    9092
    9193        $subnav_item = array(
     
    9597        // Just test relevant info
    9698        $found = bp_core_maybe_hook_new_subnav_screen_function( $subnav_item );
    97         $this->assertSame( 'failure', $found['status'] );
    98         $this->assertSame( bp_core_get_user_domain( $u2 ), $found['redirect_args']['root'] );
    9999
    100100        // Clean up
     
    102102        buddypress()->default_component = $old_default_component;
    103103        buddypress()->bp_nav = $old_bp_nav;
     104
     105        $this->assertSame( 'failure', $found['status'] );
     106        $this->assertSame( bp_core_get_user_domain( $u2 ), $found['redirect_args']['root'] );
    104107    }
    105108
     
    115118        $old_default_component = buddypress()->default_component;
    116119        buddypress()->default_component = 'foo';
    117         buddypress()->bp_nav = array(
    118             'foo' => array(
    119                 'show_for_displayed_user' => false,
    120             ),
    121         );
     120
     121        bp_core_new_nav_item( array(
     122            'slug' => 'foo',
     123            'name' => 'Foo',
     124            'screen_function' => 'foo',
     125            'default_subnav_item' => 'bar',
     126            'show_for_displayed_user' => false,
     127        ) );
    122128
    123129        $subnav_item = array(
     
    127133        // Just test relevant info
    128134        $found = bp_core_maybe_hook_new_subnav_screen_function( $subnav_item );
    129         $this->assertSame( 'failure', $found['status'] );
    130         $this->assertSame( bp_core_get_user_domain( $u2 ) . bp_get_activity_slug() . '/', $found['redirect_args']['root'] );
    131135
    132136        // Clean up
     
    134138        buddypress()->default_component = $old_default_component;
    135139        buddypress()->bp_nav = $old_bp_nav;
     140
     141        $this->assertSame( 'failure', $found['status'] );
     142        $this->assertSame( bp_core_get_user_domain( $u2 ) . bp_get_activity_slug() . '/', $found['redirect_args']['root'] );
    136143    }
    137144
Note: See TracChangeset for help on using the changeset viewer.