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-friends/bp-friends-loader.php

    r9819 r9936  
    7676
    7777                // Define a slug, if necessary
    78                 if ( !defined( 'BP_FRIENDS_SLUG' ) )
     78                if ( ! defined( 'BP_FRIENDS_SLUG' ) ) {
    7979                        define( 'BP_FRIENDS_SLUG', $this->id );
     80                }
    8081
    8182                // Global tables for the friends component
     
    111112         */
    112113        public function setup_nav( $main_nav = array(), $sub_nav = array() ) {
    113                 $bp = buddypress();
    114 
    115                 // Add 'Friends' to the main navigation
    116                 $count    = friends_get_total_friend_count();
    117                 $class    = ( 0 === $count ) ? 'no-count' : 'count';
    118                 $main_nav = array(
    119                         'name'                => sprintf( __( 'Friends <span class="%s">%s</span>', 'buddypress' ), esc_attr( $class ), number_format_i18n( $count ) ),
    120                         'slug'                => $this->slug,
    121                         'position'            => 60,
    122                         'screen_function'     => 'friends_screen_my_friends',
    123                         'default_subnav_slug' => 'my-friends',
    124                         'item_css_id'         => $bp->friends->id
    125                 );
    126114
    127115                // Determine user to use
     
    134122                }
    135123
    136                 $friends_link = trailingslashit( $user_domain . bp_get_friends_slug() );
     124                $access       = bp_core_can_edit_settings();
     125                $slug         = bp_get_friends_slug();
     126                $friends_link = trailingslashit( $user_domain . $slug );
     127
     128                // Add 'Friends' to the main navigation
     129                $count    = friends_get_total_friend_count();
     130                $class    = ( 0 === $count ) ? 'no-count' : 'count';
     131                $main_nav = array(
     132                        'name'                => sprintf( __( 'Friends <span class="%s">%s</span>', 'buddypress' ), esc_attr( $class ), bp_core_number_format( $count ) ),
     133                        'slug'                => $slug,
     134                        'position'            => 60,
     135                        'screen_function'     => 'friends_screen_my_friends',
     136                        'default_subnav_slug' => 'my-friends',
     137                        'item_css_id'         => $this->id
     138                );
    137139
    138140                // Add the subnav items to the friends nav item
     
    141143                        'slug'            => 'my-friends',
    142144                        'parent_url'      => $friends_link,
    143                         'parent_slug'     => bp_get_friends_slug(),
     145                        'parent_slug'     => $slug,
    144146                        'screen_function' => 'friends_screen_my_friends',
    145147                        'position'        => 10,
     
    151153                        'slug'            => 'requests',
    152154                        'parent_url'      => $friends_link,
    153                         'parent_slug'     => bp_get_friends_slug(),
     155                        'parent_slug'     => $slug,
    154156                        'screen_function' => 'friends_screen_requests',
    155157                        'position'        => 20,
    156                         'user_has_access' => bp_core_can_edit_settings()
     158                        'user_has_access' => $access
    157159                );
    158160
     
    171173         */
    172174        public function setup_admin_bar( $wp_admin_nav = array() ) {
    173                 $bp = buddypress();
    174175
    175176                // Menus for logged in user
     
    177178
    178179                        // Setup the logged in user variables
    179                         $user_domain  = bp_loggedin_user_domain();
    180                         $friends_link = trailingslashit( $user_domain . $this->slug );
     180                        $friends_link = trailingslashit( bp_loggedin_user_domain() . bp_get_friends_slug() );
    181181
    182182                        // Pending friend requests
    183183                        $count = count( friends_get_friendship_request_user_ids( bp_loggedin_user_id() ) );
    184184                        if ( !empty( $count ) ) {
    185                                 $title   = sprintf( _x( 'Friends <span class="count">%s</span>', 'My Account Friends menu', 'buddypress' ), number_format_i18n( $count ) );
    186                                 $pending = sprintf( _x( 'Pending Requests <span class="count">%s</span>', 'My Account Friends menu sub nav', 'buddypress' ), number_format_i18n( $count ) );
     185                                $title   = sprintf( _x( 'Friends <span class="count">%s</span>',          'My Account Friends menu',         'buddypress' ), bp_core_number_format( $count ) );
     186                                $pending = sprintf( _x( 'Pending Requests <span class="count">%s</span>', 'My Account Friends menu sub nav', 'buddypress' ), bp_core_number_format( $count ) );
    187187                        } else {
    188                                 $title   = _x( 'Friends', 'My Account Friends menu', 'buddypress' );
     188                                $title   = _x( 'Friends',            'My Account Friends menu',        'buddypress' );
    189189                                $pending = _x( 'No Pending Requests','My Account Friends menu sub nav', 'buddypress' );
    190190                        }
     
    192192                        // Add the "My Account" sub menus
    193193                        $wp_admin_nav[] = array(
    194                                 'parent' => $bp->my_account_menu_id,
     194                                'parent' => buddypress()->my_account_menu_id,
    195195                                'id'     => 'my-account-' . $this->id,
    196196                                'title'  => $title,
    197                                 'href'   => trailingslashit( $friends_link )
     197                                'href'   => $friends_link
    198198                        );
    199199
     
    203203                                'id'     => 'my-account-' . $this->id . '-friendships',
    204204                                'title'  => _x( 'Friendships', 'My Account Friends menu sub nav', 'buddypress' ),
    205                                 'href'   => trailingslashit( $friends_link )
     205                                'href'   => $friends_link
    206206                        );
    207207
     
    222222         */
    223223        public function setup_title() {
    224                 $bp = buddypress();
    225224
    226225                // Adjust title
    227226                if ( bp_is_friends_component() ) {
     227                        $bp = buddypress();
     228
    228229                        if ( bp_is_my_profile() ) {
    229230                                $bp->bp_options_title = __( 'Friendships', 'buddypress' );
Note: See TracChangeset for help on using the changeset viewer.