Skip to:
Content

BuddyPress.org

Changeset 12288


Ignore:
Timestamp:
11/15/2018 08:14:01 PM (6 years ago)
Author:
boonebgorges
Message:

Nav: Dynamically load component screen file when changing nav default.

BP 3.0.0 moved most screen controllers into files that are loaded only when
viewing the component. This introduced a regression into
bp_core_new_nav_default(), since the screen_function would not be available
(ie ! is_callable()) at the time that the nav item is modified.

We resolve this by loading the component's screen functions in
bp_core_new_nav_default() when we detect that they're not present. To work
around cases where component slugs don't match the official component ID,
which corresponds to the file path, we introduce a new 'component_id' parameter
to be used when registering a new nav item.

Props imath, r-a-y.
Fixes #7935.

Location:
trunk/src/bp-core
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/bp-core-buddybar.php

    r12195 r12288  
    1818 * @since 1.1.0
    1919 * @since 2.6.0 Introduced the `$component` parameter.
     20 * @since 4.0.0 Introduced the `$component_id` argument.
    2021 *
    2122 * @param array|string $args {
     
    2324 *     @type string      $name                    Display name for the nav item.
    2425 *     @type string      $slug                    Unique URL slug for the nav item.
     26 *     @type string      $component_id            Optional. The ID of the component registering the nav item. Defaults to slug.
    2527 *     @type bool|string $item_css_id             Optional. 'id' attribute for the nav item. Default: the value of `$slug`.
    2628 *     @type bool        $show_for_displayed_user Optional. Whether the nav item should be visible when viewing a
     
    4547        'name'                    => false, // Display name for the nav item.
    4648        'slug'                    => false, // URL slug for the nav item.
     49        'component_id'            => '',    // ID of the component registering the nav item.
    4750        'item_css_id'             => false, // The CSS ID to apply to the HTML of the nav item.
    4851        'show_for_displayed_user' => true,  // When viewing another user does this nav item show up?
     
    9194 *
    9295 * @since 2.4.0
    93  * @since 2.6.0 Introduced the `$component` parameter. Began returning a BP_Core_Nav_Item object on success.
     96 * @since 2.6.0 Introduced the `$component` parameter. Began returning a BP_Core_Nav_Item
     97 *              object on success.
     98 * @since 4.0.0 Introduced `$component_id` argument.
    9499 *
    95100 * @param array|string $args {
    96101 *     Array describing the new nav item.
     102 *     @type string      $component_id            Optional. The ID of the component registering this nav item. Defaults to the
     103 *                                                the value of `$slug`.
    97104 *     @type string      $name                    Display name for the nav item.
    98105 *     @type string      $slug                    Unique URL slug for the nav item.
     
    115122
    116123    $defaults = array(
     124        'component_id'            => '',    // The component ID registering this nav item.
    117125        'name'                    => false, // Display name for the nav item.
    118126        'slug'                    => false, // URL slug for the nav item.
     
    137145    }
    138146
     147    if ( empty( $r['component_id'] ) ) {
     148        $r['component_id'] = $r['slug'];
     149    }
     150
    139151    if ( empty( $r['item_css_id'] ) ) {
    140152        $r['item_css_id'] = $r['slug'];
     
    142154
    143155    $nav_item = array(
     156        'component_id'            => $r['component_id'],
    144157        'name'                    => $r['name'],
    145158        'slug'                    => $r['slug'],
     
    343356        if ( empty( $unfiltered_action ) ) {
    344357            if ( ! bp_is_current_action( $r['subnav_slug'] ) ) {
     358                /*
     359                 * If the screen function isn't available, attempt to find it.
     360                 *
     361                 * This is due to our conditional-loading code since v3.0.0.
     362                 */
     363                if ( ! is_callable( $r['screen_function'] ) && ! empty( $parent_nav->component_id ) ) {
     364                    $file      = $bp->core->path . 'bp-' . $parent_nav->component_id . '/screens/' . $r['subnav_slug'] . '.php';
     365                    $file_path = realpath( $file );
     366
     367                    // Found the file, so require it.
     368                    if ( $file === $file_path && file_exists( $file ) ) {
     369                        require_once $file;
     370                    }
     371                }
     372
    345373                if ( is_callable( $r['screen_function'] ) ) {
    346374                    add_action( 'bp_screens', $r['screen_function'], 3 );
  • trunk/src/bp-core/classes/class-bp-component.php

    r12180 r12288  
    499499        // No sub nav items without a main nav item.
    500500        if ( !empty( $main_nav ) ) {
     501            // Always set the component ID.
     502            $main_nav['component_id'] = $this->id;
     503
    501504            bp_core_new_nav_item( $main_nav, 'members' );
    502505
Note: See TracChangeset for help on using the changeset viewer.