/**
  * Hook this extension's group tab into BuddyPress, if necessary.
  *
  * @since BuddyPress (1.8.0)
  */
 protected function setup_display_hooks()
 {
     // Bail if not a group
     if (!bp_is_group()) {
         return;
     }
     // Backward compatibility only
     if ('public' !== $this->visibility && !buddypress()->groups->current_group->user_has_access) {
         return;
     }
     // If the user can see the nav item, we create it.
     $user_can_see_nav_item = $this->user_can_see_nav_item();
     if ($user_can_see_nav_item) {
         $group_permalink = bp_get_group_permalink(groups_get_current_group());
         bp_core_create_subnav_link(array('name' => !$this->nav_item_name ? $this->name : $this->nav_item_name, 'slug' => $this->slug, 'parent_slug' => bp_get_current_group_slug(), 'parent_url' => $group_permalink, 'position' => $this->nav_item_position, 'item_css_id' => 'nav-' . $this->slug, 'screen_function' => array(&$this, '_display_hook'), 'user_has_access' => $user_can_see_nav_item, 'no_access_url' => $group_permalink));
     }
     // If the user can visit the screen, we register it.
     $user_can_visit = $this->user_can_visit();
     if ($user_can_visit) {
         $group_permalink = bp_get_group_permalink(groups_get_current_group());
         bp_core_register_subnav_screen_function(array('slug' => $this->slug, 'parent_slug' => bp_get_current_group_slug(), 'screen_function' => array(&$this, '_display_hook'), 'user_has_access' => $user_can_visit, 'no_access_url' => $group_permalink));
         // When we are viewing the extension display page, set the title and options title
         if (bp_is_current_action($this->slug)) {
             add_filter('bp_group_user_has_access', array($this, 'group_access_protection'), 10, 2);
             add_action('bp_template_content_header', create_function('', 'echo "' . esc_attr($this->name) . '";'));
             add_action('bp_template_title', create_function('', 'echo "' . esc_attr($this->name) . '";'));
         }
     }
     // Hook the group home widget
     if (!bp_current_action() && bp_is_current_action('home')) {
         add_action($this->display_hook, array(&$this, 'widget_display'));
     }
 }
示例#2
0
/**
 * Add a subnav item to the BuddyPress navigation.
 *
 * @param array|string $args {
 *     Array describing the new subnav item.
 *     @type string      $name              Display name for the subnav item.
 *     @type string      $slug              Unique URL slug for the subnav item.
 *     @type string      $parent_slug       Slug of the top-level nav item under which the new subnav item should
 *                                          be added.
 *     @type string      $parent_url        URL of the parent nav item.
 *     @type bool|string $item_css_id       Optional. 'id' attribute for the nav item. Default: the value of `$slug`.
 *     @type bool        $user_has_access   Optional. True if the logged-in user has access to the subnav item,
 *                                          otherwise false. Can be set dynamically when registering the subnav;
 *                                          eg, use `bp_is_my_profile()` to restrict access to profile owners only.
 *                                          Default: true.
 *     @type bool        $site_admin_only   Optional. Whether the nav item should be visible only to site admins
 *                                          (those with the 'bp_moderate' cap). Default: false.
 *     @type int         $position          Optional. Numerical index specifying where the item should appear in the
 *                                          subnav array. Default: 90.
 *     @type callable    $screen_function   The callback function that will run when the nav item is clicked.
 *     @type string      $link              Optional. The URL that the subnav item should point to. Defaults to a value
 *                                          generated from the `$parent_url` + `$slug`.
 *     @type bool        $show_in_admin_bar Optional. Whether the nav item should be added into the group's "Edit"
 *                                          Admin Bar menu for group admins. Default: false.
 * }
 * @return bool|null Returns false on failure.
 */
function bp_core_new_subnav_item($args = '')
{
    // First, add the subnav item link to the bp_options_nav array.
    $created = bp_core_create_subnav_link($args);
    // To mimic the existing behavior, if bp_core_create_subnav_link()
    // returns false, we make an early exit and don't attempt to register
    // the screen function.
    if (false === $created) {
        return false;
    }
    // Then, hook the screen function for the added subnav item.
    bp_core_register_subnav_screen_function($args);
}
/**
 * Add an item to secondary navigation of the specified component.
 *
 * @since 1.1.0
 * @since 2.6.0 Introduced the `$component` parameter.
 *
 * @param array|string $args {
 *     Array describing the new subnav item.
 *     @type string      $name              Display name for the subnav item.
 *     @type string      $slug              Unique URL slug for the subnav item.
 *     @type string      $parent_slug       Slug of the top-level nav item under which the new subnav item should
 *                                          be added.
 *     @type string      $parent_url        URL of the parent nav item.
 *     @type bool|string $item_css_id       Optional. 'id' attribute for the nav item. Default: the value of `$slug`.
 *     @type bool        $user_has_access   Optional. True if the logged-in user has access to the subnav item,
 *                                          otherwise false. Can be set dynamically when registering the subnav;
 *                                          eg, use `bp_is_my_profile()` to restrict access to profile owners only.
 *                                          Default: true.
 *     @type bool        $site_admin_only   Optional. Whether the nav item should be visible only to site admins
 *                                          (those with the 'bp_moderate' cap). Default: false.
 *     @type int         $position          Optional. Numerical index specifying where the item should appear in the
 *                                          subnav array. Default: 90.
 *     @type callable    $screen_function   The callback function that will run when the nav item is clicked.
 *     @type string      $link              Optional. The URL that the subnav item should point to. Defaults to a value
 *                                          generated from the `$parent_url` + `$slug`.
 *     @type bool        $show_in_admin_bar Optional. Whether the nav item should be added into the group's "Edit"
 *                                          Admin Bar menu for group admins. Default: false.
 * }
 * @param string       $component The component the navigation is attached to. Defaults to 'members'.
 * @return bool|null Returns false on failure.
 */
function bp_core_new_subnav_item($args, $component = null)
{
    // Backward compatibility for plugins using `bp_core_new_subnav_item()` without `$component`
    // to add group subnav items.
    if (null === $component && bp_is_active('groups') && bp_is_group() && isset($args['parent_slug'])) {
        /*
         * Assume that this item is intended to belong to the current group if:
         * a) the 'parent_slug' is the same as the slug of the current group, or
         * b) the 'parent_slug' starts with the slug of the current group, and the members nav doesn't have
         *    a primary item with that slug.
         */
        $group_slug = bp_get_current_group_slug();
        if ($group_slug === $args['parent_slug'] || 0 === strpos($args['parent_slug'], $group_slug) && !buddypress()->members->nav->get_primary(array('slug' => $args['parent_slug']), false)) {
            $component = 'groups';
        }
    }
    if (!$component) {
        $component = 'members';
    }
    if (!bp_is_active($component)) {
        return;
    }
    // First, register the subnav item in the nav.
    $subnav_item = bp_core_create_subnav_link($args, $component);
    /*
     * To mimic legacy behavior, if bp_core_create_subnav_link() returns false, we make an
     * early exit and don't attempt to register the screen function.
     */
    if (false === $subnav_item) {
        return false;
    }
    // Then, hook the screen function for the added subnav item.
    $hooked = bp_core_register_subnav_screen_function($subnav_item, $component);
    if (false === $hooked) {
        return false;
    }
}