Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
06/11/2015 06:53:59 AM (9 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-messages/bp-messages-loader.php

    r9934 r9936  
    9191
    9292        // Define a slug, if necessary
    93         if ( !defined( 'BP_MESSAGES_SLUG' ) ) {
     93        if ( ! defined( 'BP_MESSAGES_SLUG' ) ) {
    9494            define( 'BP_MESSAGES_SLUG', $this->id );
    9595        }
     
    130130    public function setup_nav( $main_nav = array(), $sub_nav = array() ) {
    131131
    132         // Only grab count if we're on a user page and current user has access
    133         if ( bp_is_user() && bp_user_has_access() ) {
    134             $count    = bp_get_total_unread_messages_count();
    135             $class    = ( 0 === $count ) ? 'no-count' : 'count';
    136             $nav_name = sprintf( __( 'Messages <span class="%s">%s</span>', 'buddypress' ), esc_attr( $class ), number_format_i18n( $count ) );
    137         } else {
    138             $nav_name = __( 'Messages', 'buddypress' );
    139         }
    140 
    141         // Add 'Messages' to the main navigation
    142         $main_nav = array(
    143             'name'                    => $nav_name,
    144             'slug'                    => $this->slug,
    145             'position'                => 50,
    146             'show_for_displayed_user' => bp_core_can_edit_settings(),
    147             'screen_function'         => 'messages_screen_inbox',
    148             'default_subnav_slug'     => 'inbox',
    149             'item_css_id'             => $this->id
    150         );
    151 
    152132        // Determine user to use
    153133        if ( bp_displayed_user_domain() ) {
     
    159139        }
    160140
    161         // Link to user messages
    162         $messages_link = trailingslashit( $user_domain . $this->slug );
     141        $access        = bp_core_can_edit_settings();
     142        $slug          = bp_get_messages_slug();
     143        $messages_link = trailingslashit( $user_domain . $slug );
     144
     145        // Only grab count if we're on a user page and current user has access
     146        if ( bp_is_user() && bp_user_has_access() ) {
     147            $count    = bp_get_total_unread_messages_count();
     148            $class    = ( 0 === $count ) ? 'no-count' : 'count';
     149            $nav_name = sprintf( __( 'Messages <span class="%s">%s</span>', 'buddypress' ), esc_attr( $class ), bp_core_number_format( $count ) );
     150        } else {
     151            $nav_name = __( 'Messages', 'buddypress' );
     152        }
     153
     154        // Add 'Messages' to the main navigation
     155        $main_nav = array(
     156            'name'                    => $nav_name,
     157            'slug'                    => $slug,
     158            'position'                => 50,
     159            'show_for_displayed_user' => $access,
     160            'screen_function'         => 'messages_screen_inbox',
     161            'default_subnav_slug'     => 'inbox',
     162            'item_css_id'             => $this->id
     163        );
    163164
    164165        // Add the subnav items to the profile
     
    167168            'slug'            => 'inbox',
    168169            'parent_url'      => $messages_link,
    169             'parent_slug'     => $this->slug,
     170            'parent_slug'     => $slug,
    170171            'screen_function' => 'messages_screen_inbox',
    171172            'position'        => 10,
    172             'user_has_access' => bp_core_can_edit_settings()
     173            'user_has_access' => $access
    173174        );
    174175
     
    178179                'slug'            => bp_get_messages_starred_slug(),
    179180                'parent_url'      => $messages_link,
    180                 'parent_slug'     => $this->slug,
     181                'parent_slug'     => $slug,
    181182                'screen_function' => 'bp_messages_star_screen',
    182183                'position'        => 11,
    183                 'user_has_access' => bp_core_can_edit_settings()
     184                'user_has_access' => $access
    184185            );
    185186        }
     
    189190            'slug'            => 'sentbox',
    190191            'parent_url'      => $messages_link,
    191             'parent_slug'     => $this->slug,
     192            'parent_slug'     => $slug,
    192193            'screen_function' => 'messages_screen_sentbox',
    193194            'position'        => 20,
    194             'user_has_access' => bp_core_can_edit_settings()
     195            'user_has_access' => $access
    195196        );
    196197
     
    199200            'slug'            => 'compose',
    200201            'parent_url'      => $messages_link,
    201             'parent_slug'     => $this->slug,
     202            'parent_slug'     => $slug,
    202203            'screen_function' => 'messages_screen_compose',
    203204            'position'        => 30,
    204             'user_has_access' => bp_core_can_edit_settings()
     205            'user_has_access' => $access
    205206        );
    206207
     
    210211                'slug'            => 'notices',
    211212                'parent_url'      => $messages_link,
    212                 'parent_slug'     => $this->slug,
     213                'parent_slug'     => $slug,
    213214                'screen_function' => 'messages_screen_notices',
    214215                'position'        => 90,
    215                 'user_has_access' => bp_current_user_can( 'bp_moderate' )
     216                'user_has_access' => true
    216217            );
    217218        }
     
    227228     */
    228229    public function setup_admin_bar( $wp_admin_nav = array() ) {
    229         $bp = buddypress();
    230230
    231231        // Menus for logged in user
     
    233233
    234234            // Setup the logged in user variables
    235             $user_domain   = bp_loggedin_user_domain();
    236             $messages_link = trailingslashit( $user_domain . $this->slug );
     235            $messages_link = trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() );
    237236
    238237            // Unread message count
    239238            $count = messages_get_unread_count();
    240239            if ( !empty( $count ) ) {
    241                 $title = sprintf( __( 'Messages <span class="count">%s</span>', 'buddypress' ), number_format_i18n( $count ) );
    242                 $inbox = sprintf( __( 'Inbox <span class="count">%s</span>',    'buddypress' ), number_format_i18n( $count ) );
     240                $title = sprintf( __( 'Messages <span class="count">%s</span>', 'buddypress' ), bp_core_number_format( $count ) );
     241                $inbox = sprintf( __( 'Inbox <span class="count">%s</span>',    'buddypress' ), bp_core_number_format( $count ) );
    243242            } else {
    244243                $title = __( 'Messages', 'buddypress' );
     
    248247            // Add main Messages menu
    249248            $wp_admin_nav[] = array(
    250                 'parent' => $bp->my_account_menu_id,
     249                'parent' => buddypress()->my_account_menu_id,
    251250                'id'     => 'my-account-' . $this->id,
    252251                'title'  => $title,
    253                 'href'   => trailingslashit( $messages_link )
     252                'href'   => $messages_link
    254253            );
    255254
     
    259258                'id'     => 'my-account-' . $this->id . '-inbox',
    260259                'title'  => $inbox,
    261                 'href'   => trailingslashit( $messages_link . 'inbox' )
     260                'href'   => $messages_link
    262261            );
    263262
     
    306305     */
    307306    public function setup_title() {
    308         $bp = buddypress();
    309307
    310308        if ( bp_is_messages_component() ) {
     309            $bp = buddypress();
     310
    311311            if ( bp_is_my_profile() ) {
    312312                $bp->bp_options_title = __( 'My Messages', 'buddypress' );
Note: See TracChangeset for help on using the changeset viewer.