Skip to:
Content

BuddyPress.org

Opened 11 years ago

Closed 11 years ago

#5413 closed defect (bug) (fixed)

Don't query for item counts in profile nav when not needed

Reported by: r-a-y's profile r-a-y Owned by: r-a-y's profile r-a-y
Milestone: 2.0 Priority: normal
Severity: normal Version:
Component: Core Keywords: has-patch
Cc:

Description

When a component's setup_nav() method is fired, we currently do no checks for when an item count is fired.

This causes unnecessary queries to be fired when a user isn't logged in or when not on an appropriate BP page.

Attached is a quick patch before I forget to add one!

The only caveat with the patch is if a dev is manually calling the profile nav somewhere else that is not a profile page.

Attachments (1)

5413.01.patch (2.3 KB) - added by r-a-y 11 years ago.

Download all attachments as: .zip

Change History (5)

@r-a-y
11 years ago

#1 @lenasterg
11 years ago

Nice!!!

#2 @boonebgorges
11 years ago

Good idea.

The only caveat with the patch is if a dev is manually calling the profile nav somewhere else that is not a profile page.

I don't think we really support this anyway - our nav is riddled with calls to bp_displayed_user_id(). That said, one way to work around it would be to skip adding the counts in those cases:

if ( bp_is_user() ) {
    $count    = bp_get_total_group_count_for_user();  
    $class    = ( 0 === $count ) ? 'no-count' : 'count'; 
    $nav_name = sprintf( __( 'Groups <span class=%s">%s</span>', 'buddypress' ), esc_attr( $class ), number_format_i18n( $count ) ); 
} else {
    $nav_name = __( 'Groups', 'buddypress' );
}

Then, at least, we won't be outputting zeros where a zero would be technically incorrect.

#3 @r-a-y
11 years ago

I like this. Going to commit this now!

#4 @r-a-y
11 years ago

  • Owner set to r-a-y
  • Resolution set to fixed
  • Status changed from new to closed

In 7966:

Don't fetch item counts in profile navigation unnecessarily.

Previously, when a component's setup_nav() method is fired, the item
count for the component is fetched without any conditional checks.

This causes unneccessary queries to be fired when a user isn't logged
in or when we're not on a single user page.

This commit only fetches the item count in the profile nav if we're
currently on a single user page and/or if the current user has access
to the component nav item.

Fixes #5413

Props r-a-y, boonebgorges

Note: See TracTickets for help on using tickets.