Skip to:
Content

BuddyPress.org

Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#6085 closed defect (bug) (no action required)

Possible Bug - Notice of Undefined index: position in bp-core-template.php

Reported by: espellcaste's profile espellcaste Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.1
Component: Core Keywords: reporter-feedback close
Cc:

Description

I'm not 100% sure if it is a bug in BuddyPress, but I'm posting here to give the core developers an opportunity to confirm it.

When I tried to unset a few $bp_options_nav, I started to get the current notice:

Notice: Undefined index: position in PATH\wp-content\plugins\buddypress\bp-core\bp-core-template.php on line 2512

Below is a function so that you can test it, it is similar to the one I used:

function test_remove_submenu_item() {
	global $bp;

	unset($bp->bp_options_nav['profile']['public']);
}
add_action('bp_setup_nav', 'test_remove_submenu_item', 201);

After taking a closer look at the problematic function _bp_nav_menu_sort(), I changed and solved the notice with the below change:

function _bp_nav_menu_sort( $a, $b ) {
	if ( !isset($a['position']) && !isset($b['position']) );
		return;

	if ( $a['position'] == $b['position'] ) {
		return 0;
	} elseif ( $a['position'] < $b['position'] ) {
		return -1;
	} else {
		return 1;
	}
}

Interesting Point:

  • The noticed only appeared to non logged in users, public users wouldn't get the notice.

Change History (4)

#1 @espellcaste
10 years ago

Correction in the end of the ticket.

Logged in users don't get the notice, I put "public users".

#2 @boonebgorges
10 years ago

  • Keywords reporter-feedback added; 2nd-opinion removed

Hi espellcaste. Happy new year!

I can't reproduce this using only the code you've provided here. The nav arrays will only be sorted using _bp_nav_menu_sort() if they contain two or more items (http://php.net/manual/en/array.sorting.php#112221). What are the nav items that you've got under $bp->bp_options_nav['profile']?

Have you checked whether this happens when using bp_core_remove_subnav_item()?

_bp_nav_menu_sort() is called from two different places in bp_get_nav_menu_items() - once for top-level items, and once for each set of submenus. Can you examine a bit more to see where it's happening in your case, and let us know exactly what value is being passed to usort()?

(I'm not necessarily opposed to the fix you've suggested, but I want to make sure this is not a symptom of a larger problem.)

#3 @espellcaste
10 years ago

Hi @boonebgorges, thanks a lot. Happy New Year for you too! =)

Firstly let me confirm that the problem wasn't in this particular function ('_bp_nav_menu_sort()'). After another calm and closer look, I found out the problem was in another function of mine that was changing the name of the subnav item, example below:

function test_submenu_name_item() {
	global $bp;

	$bp->bp_options_nav['forums']['assinaturas']['name'] = 'Tópicos Assinados';
}
add_action('bp_setup_nav', 'test_submenu_name_item', 201);

This function as it is, causes the underfined index: notice error for the non logged in user. My guess this happens because this particular item is not viewable for the public, so when it goes count the items, it shows the error.

I say this because to correct the notice, I added a check for the logged in user and the notice was gone, code below:

function test_submenu_name_item() {
	global $bp;

	if ( is_user_logged_in() )
	$bp->bp_options_nav['forums']['assinaturas']['name'] = 'Tópicos Assinados';
}
add_action('bp_setup_nav', 'test_submenu_name_item', 201);

have you checked whether this happens when using bp_core_remove_subnav_item()?

Yes, I tried to remove the items with this function and it works great, except in the situation where you are removing the first item of a "group". Example:

Profile -> View
Forums -> Topics
Settings -> General

If I remove the the sub item Edit, it works as expected, but if I try to remove the first item, View, the profile home page doesn't work anymore, the same happens to Topics, General and etc. Follows an example:

function test_6085_submenu_remove_item() {
	// Profile
	bp_core_remove_subnav_item( 'profile', 'public' );

	// Forums
	bp_core_remove_subnav_item( 'forums', 'topics' );

	// Setting
	bp_core_remove_subnav_item( 'setting', 'general' );
}
add_action('bp_setup_nav', 'test_6085_submenu_remove_item', 201);

It happens because bp_core_remove_subnav_item(), takes the parent item and the current slug, that in this case, it's the same as the parent.

In this case, the unset worked correctly. It removed the items and didn't interfered with the the index count.

function test_6085_submenu_remove_item() {

	// Profile
	unset($bp->bp_options_nav['profile']['public']);
}
add_action('bp_setup_nav', 'test_6085_submenu_remove_item', 201);

#4 @espellcaste
10 years ago

  • Keywords close added
  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

As the problem wasn't this ticket, I think I'm gonna close it as invalid. I spotted other problems but it'll need more testing and verification and I believe it is also best for another ticket.

Last edited 10 years ago by espellcaste (previous) (diff)
Note: See TracTickets for help on using tickets.