Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
06/11/2015 06:53:59 AM (11 years ago)
Author:
johnjamesjacoby
Message:

Loaders: Micro-optimizations:

  • Add brackets for improved readability
  • Only instantiate $bp from buddypress() when used more than once, and after all early return conditions have executed
  • Use _slug_ functions where appropriate, and avoid $this->slug references as they do not run filters
  • Reduce mulitple calls to the same functions into 1 function call: I.E. bp_core_can_edit_settings()
  • Remove BP_Core_Component::setup_nav() method - it's been handled and duplicated by BP_Members_Component for several years now, and only when XProfile is disabled
  • Use new bp_get_profile_slug() functions where appropriate
  • Use bp_core_number_format() where appropriate
  • Avoid duplicate trailingslashit() calls on the same variables
  • Rely on canonical menu URLs for all components

This change touches each component's -loader.php file and makes several small optimizations that avoid executing hundreds of function calls that return data (specifically gettext) that never gets used on logged-out users. This results in an approximate 20% reduction in time spent running our full unit test suite in my testing, and also improves site performance for logged-out users.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-groups/bp-groups-loader.php

    r9906 r9936  
    121121                );
    122122
    123                 if ( is_admin() )
     123                if ( is_admin() ) {
    124124                        $includes[] = 'admin';
     125                }
    125126
    126127                parent::includes( $includes );
     
    143144
    144145                // Define a slug, if necessary
    145                 if ( !defined( 'BP_GROUPS_SLUG' ) )
     146                if ( ! defined( 'BP_GROUPS_SLUG' ) ) {
    146147                        define( 'BP_GROUPS_SLUG', $this->id );
     148                }
    147149
    148150                // Global tables for groups component
     
    214216
    215217                        // Using "item" not "group" for generic support in other components.
    216                         if ( bp_current_user_can( 'bp_moderate' ) )
     218                        if ( bp_current_user_can( 'bp_moderate' ) ) {
    217219                                bp_update_is_item_admin( true, 'groups' );
    218                         else
     220                        } else {
    219221                                bp_update_is_item_admin( groups_is_user_admin( bp_loggedin_user_id(), $this->current_group->id ), 'groups' );
     222                        }
    220223
    221224                        // If the user is not an admin, check if they are a moderator
    222                         if ( !bp_is_item_admin() )
     225                        if ( ! bp_is_item_admin() ) {
    223226                                bp_update_is_item_mod  ( groups_is_user_mod  ( bp_loggedin_user_id(), $this->current_group->id ), 'groups' );
     227                        }
    224228
    225229                        // Is the logged in user a member of the group?
    226                         if ( ( is_user_logged_in() && groups_is_user_member( bp_loggedin_user_id(), $this->current_group->id ) ) )
     230                        if ( ( is_user_logged_in() && groups_is_user_member( bp_loggedin_user_id(), $this->current_group->id ) ) ) {
    227231                                $this->current_group->is_user_member = true;
    228                         else
     232                        } else {
    229233                                $this->current_group->is_user_member = false;
     234                        }
    230235
    231236                        // Should this group be visible to the logged in user?
    232                         if ( 'public' == $this->current_group->status || $this->current_group->is_user_member )
     237                        if ( 'public' == $this->current_group->status || $this->current_group->is_user_member ) {
    233238                                $this->current_group->is_visible = true;
    234                         else
     239                        } else {
    235240                                $this->current_group->is_visible = false;
     241                        }
    236242
    237243                        // If this is a private or hidden group, does the user have access?
    238244                        if ( 'private' == $this->current_group->status || 'hidden' == $this->current_group->status ) {
    239                                 if ( $this->current_group->is_user_member && is_user_logged_in() || bp_current_user_can( 'bp_moderate' ) )
     245                                if ( $this->current_group->is_user_member && is_user_logged_in() || bp_current_user_can( 'bp_moderate' ) ) {
    240246                                        $this->current_group->user_has_access = true;
    241                                 else
     247                                } else {
    242248                                        $this->current_group->user_has_access = false;
     249                                }
    243250                        } else {
    244251                                $this->current_group->user_has_access = true;
     
    346353                }
    347354
    348 
    349355                /**
    350356                 * Filters the default groups extension.
     
    357363                $this->default_extension = apply_filters( 'bp_groups_default_extension', defined( 'BP_GROUPS_DEFAULT_EXTENSION' ) ? BP_GROUPS_DEFAULT_EXTENSION : 'home' );
    358364
    359                 if ( !bp_current_action() ) {
    360                         buddypress()->current_action = $this->default_extension;
     365                $bp = buddypress();
     366
     367                if ( ! bp_current_action() ) {
     368                        $bp->current_action = $this->default_extension;
    361369                }
    362370
    363371                // Prepare for a redirect to the canonical URL
    364                 buddypress()->canonical_stack['base_url'] = bp_get_group_permalink( $this->current_group );
     372                $bp->canonical_stack['base_url'] = bp_get_group_permalink( $this->current_group );
    365373
    366374                if ( bp_current_action() ) {
    367                         buddypress()->canonical_stack['action'] = bp_current_action();
    368                 }
    369 
    370                 if ( !empty( buddypress()->action_variables ) ) {
    371                         buddypress()->canonical_stack['action_variables'] = bp_action_variables();
     375                        $bp->canonical_stack['action'] = bp_current_action();
     376                }
     377
     378                if ( ! empty( $bp->action_variables ) ) {
     379                        $bp->canonical_stack['action_variables'] = bp_action_variables();
    372380                }
    373381
     
    375383                // that extension's slug, unless more has been tacked onto the URL via
    376384                // action variables
    377                 if ( bp_is_current_action( $this->default_extension ) && empty( buddypress()->action_variables ) )  {
    378                         unset( buddypress()->canonical_stack['action'] );
     385                if ( bp_is_current_action( $this->default_extension ) && empty( $bp->action_variables ) )  {
     386                        unset( $bp->canonical_stack['action'] );
    379387                }
    380388        }
     
    391399         */
    392400        public function setup_nav( $main_nav = array(), $sub_nav = array() ) {
    393 
    394                 // Only grab count if we're on a user page
    395                 if ( bp_is_user() ) {
    396                         $count    = bp_get_total_group_count_for_user();
    397                         $class    = ( 0 === $count ) ? 'no-count' : 'count';
    398                         $nav_name = sprintf( _x( 'Groups <span class="%s">%s</span>', 'Group screen nav with counter', 'buddypress' ), esc_attr( $class ), number_format_i18n( $count ) );
    399                 } else {
    400                         $nav_name = _x( 'Groups', 'Group screen nav without counter', 'buddypress' );
    401                 }
    402 
    403                 // Add 'Groups' to the main navigation
    404                 $main_nav = array(
    405                         'name'                => $nav_name,
    406                         'slug'                => $this->slug,
    407                         'position'            => 70,
    408                         'screen_function'     => 'groups_screen_my_groups',
    409                         'default_subnav_slug' => 'my-groups',
    410                         'item_css_id'         => $this->id
    411                 );
    412401
    413402                // Determine user to use
     
    420409                }
    421410
    422                 if ( !empty( $user_domain ) ) {
    423                         $groups_link = trailingslashit( $user_domain . $this->slug );
     411                // Only grab count if we're on a user page
     412                if ( bp_is_user() ) {
     413                        $count    = bp_get_total_group_count_for_user();
     414                        $class    = ( 0 === $count ) ? 'no-count' : 'count';
     415                        $nav_name = sprintf( _x( 'Groups <span class="%s">%s</span>', 'Group screen nav with counter', 'buddypress' ), esc_attr( $class ), bp_core_number_format( $count ) );
     416                } else {
     417                        $nav_name = _x( 'Groups', 'Group screen nav without counter', 'buddypress' );
     418                }
     419
     420                $slug = bp_get_groups_slug();
     421
     422                // Add 'Groups' to the main navigation
     423                $main_nav = array(
     424                        'name'                => $nav_name,
     425                        'slug'                => $slug,
     426                        'position'            => 70,
     427                        'screen_function'     => 'groups_screen_my_groups',
     428                        'default_subnav_slug' => 'my-groups',
     429                        'item_css_id'         => $this->id
     430                );
     431
     432                if ( ! empty( $user_domain ) ) {
     433                        $access      = bp_core_can_edit_settings();
     434                        $groups_link = trailingslashit( $user_domain . $slug );
    424435
    425436                        // Add the My Groups nav item
     
    428439                                'slug'            => 'my-groups',
    429440                                'parent_url'      => $groups_link,
    430                                 'parent_slug'     => $this->slug,
     441                                'parent_slug'     => $slug,
    431442                                'screen_function' => 'groups_screen_my_groups',
    432443                                'position'        => 10,
     
    439450                                'slug'            => 'invites',
    440451                                'parent_url'      => $groups_link,
    441                                 'parent_slug'     => $this->slug,
     452                                'parent_slug'     => $slug,
    442453                                'screen_function' => 'groups_screen_group_invites',
    443                                 'user_has_access' => bp_core_can_edit_settings(),
     454                                'user_has_access' => $access,
    444455                                'position'        => 30
    445456                        );
     
    487498
    488499                                $sub_nav[] = array(
    489                                         'name'               => _x( 'Request Membership','Group screen nav', 'buddypress' ),
    490                                         'slug'               => 'request-membership',
    491                                         'parent_url'         => $group_link,
    492                                         'parent_slug'        => $this->current_group->slug,
    493                                         'screen_function'    => 'groups_screen_group_request_membership',
    494                                         'position'           => 30
     500                                        'name'            => _x( 'Request Membership','Group screen nav', 'buddypress' ),
     501                                        'slug'            => 'request-membership',
     502                                        'parent_url'      => $group_link,
     503                                        'parent_slug'     => $this->current_group->slug,
     504                                        'screen_function' => 'groups_screen_group_request_membership',
     505                                        'position'        => 30
    495506                                );
    496507                        }
     
    562573
    563574                                $sub_nav[] = array_merge( array(
    564                                         'name'            => __( 'Details', 'buddypress' ),
    565                                         'slug'            => 'edit-details',
    566                                         'position'        => 0,
     575                                        'name'     => __( 'Details', 'buddypress' ),
     576                                        'slug'     => 'edit-details',
     577                                        'position' => 0,
    567578                                ), $default_params );
    568579
    569580                                $sub_nav[] = array_merge( array(
    570                                         'name'            => __( 'Settings', 'buddypress' ),
    571                                         'slug'            => 'group-settings',
    572                                         'position'        => 10,
     581                                        'name'     => __( 'Settings', 'buddypress' ),
     582                                        'slug'     => 'group-settings',
     583                                        'position' => 10,
    573584                                ), $default_params );
    574585
    575586                                if ( ! bp_disable_group_avatar_uploads() && buddypress()->avatar->show_avatars ) {
    576587                                        $sub_nav[] = array_merge( array(
    577                                                 'name'        => __( 'Photo', 'buddypress' ),
    578                                                 'slug'        => 'group-avatar',
    579                                                 'position'    => 20,
     588                                                'name'     => __( 'Photo', 'buddypress' ),
     589                                                'slug'     => 'group-avatar',
     590                                                'position' => 20,
    580591                                        ), $default_params );
    581592                                }
    582593
    583594                                $sub_nav[] = array_merge( array(
    584                                         'name'            => __( 'Members', 'buddypress' ),
    585                                         'slug'            => 'manage-members',
    586                                         'position'        => 30,
     595                                        'name'     => __( 'Members', 'buddypress' ),
     596                                        'slug'     => 'manage-members',
     597                                        'position' => 30,
    587598                                ), $default_params );
    588599
    589600                                if ( 'private' == $this->current_group->status ) {
    590601                                        $sub_nav[] = array_merge( array(
    591                                                 'name'            => __( 'Requests', 'buddypress' ),
    592                                                 'slug'            => 'membership-requests',
    593                                                 'position'        => 40,
     602                                                'name'     => __( 'Requests', 'buddypress' ),
     603                                                'slug'     => 'membership-requests',
     604                                                'position' => 40,
    594605                                        ), $default_params );
    595606                                }
    596607
    597608                                $sub_nav[] = array_merge( array(
    598                                         'name'            => __( 'Delete', 'buddypress' ),
    599                                         'slug'            => 'delete-group',
    600                                         'position'        => 1000,
     609                                        'name'     => __( 'Delete', 'buddypress' ),
     610                                        'slug'     => 'delete-group',
     611                                        'position' => 1000,
    601612                                ), $default_params );
    602613                        }
     
    631642         */
    632643        public function setup_admin_bar( $wp_admin_nav = array() ) {
    633                 $bp = buddypress();
    634644
    635645                // Menus for logged in user
     
    637647
    638648                        // Setup the logged in user variables
    639                         $user_domain = bp_loggedin_user_domain();
    640                         $groups_link = trailingslashit( $user_domain . $this->slug );
     649                        $groups_link = trailingslashit( bp_loggedin_user_domain() . bp_get_groups_slug() );
    641650
    642651                        // Pending group invites
     
    645654                        $pending = _x( 'No Pending Invites', 'My Account Groups sub nav', 'buddypress' );
    646655
    647                         if ( !empty( $count['total'] ) ) {
    648                                 $title   = sprintf( _x( 'Groups <span class="count">%s</span>', 'My Account Groups nav', 'buddypress' ), $count );
    649                                 $pending = sprintf( _x( 'Pending Invites <span class="count">%s</span>', 'My Account Groups sub nav', 'buddypress' ), $count );
     656                        if ( ! empty( $count['total'] ) ) {
     657                                $title   = sprintf( _x( 'Groups <span class="count">%s</span>',          'My Account Groups nav',     'buddypress' ), bp_core_number_format( $count ) );
     658                                $pending = sprintf( _x( 'Pending Invites <span class="count">%s</span>', 'My Account Groups sub nav', 'buddypress' ), bp_core_number_format( $count ) );
    650659                        }
    651660
    652661                        // Add the "My Account" sub menus
    653662                        $wp_admin_nav[] = array(
    654                                 'parent' => $bp->my_account_menu_id,
     663                                'parent' => buddypress()->my_account_menu_id,
    655664                                'id'     => 'my-account-' . $this->id,
    656665                                'title'  => $title,
    657                                 'href'   => trailingslashit( $groups_link )
     666                                'href'   => $groups_link
    658667                        );
    659668
     
    663672                                'id'     => 'my-account-' . $this->id . '-memberships',
    664673                                'title'  => _x( 'Memberships', 'My Account Groups sub nav', 'buddypress' ),
    665                                 'href'   => trailingslashit( $groups_link )
     674                                'href'   => $groups_link
    666675                        );
    667676
     
    692701         */
    693702        public function setup_title() {
    694                 $bp = buddypress();
    695703
    696704                if ( bp_is_groups_component() ) {
     705                        $bp = buddypress();
    697706
    698707                        if ( bp_is_my_profile() && !bp_is_single_item() ) {
Note: See TracChangeset for help on using the changeset viewer.