Skip to:
Content

BuddyPress.org


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

    r9819 r9936  
    5050
    5151        // Define the parent forum ID
    52         if ( !defined( 'BP_FORUMS_PARENT_FORUM_ID' ) )
     52        if ( ! defined( 'BP_FORUMS_PARENT_FORUM_ID' ) ) {
    5353            define( 'BP_FORUMS_PARENT_FORUM_ID', 1 );
     54        }
    5455
    5556        // Define a slug, if necessary
    56         if ( !defined( 'BP_FORUMS_SLUG' ) )
     57        if ( ! defined( 'BP_FORUMS_SLUG' ) ) {
    5758            define( 'BP_FORUMS_SLUG', $this->id );
     59        }
    5860
    5961        // The location of the bbPress stand-alone config file
    6062        $bbconfig = bp_core_get_root_option( 'bb-config-location' );
    61         if ( '' !== $bbconfig )
     63        if ( '' !== $bbconfig ) {
    6264            $this->bbconfig = $bbconfig;
     65        }
    6366
    6467        // All globals for messaging component.
     
    9598
    9699        // bbPress stand-alone
    97         if ( !defined( 'BB_PATH' ) )
     100        if ( ! defined( 'BB_PATH' ) ) {
    98101            $includes[] = 'bbpress-sa';
     102        }
    99103
    100104        // Admin-specific code
     
    122126
    123127        // Stop if forums haven't been set up yet
    124         if ( !bp_forums_is_installed_correctly() )
     128        if ( ! bp_forums_is_installed_correctly() ) {
    125129            return;
     130        }
    126131
    127132        // Stop if there is no user displayed or logged in
    128         if ( !is_user_logged_in() && !bp_displayed_user_id() )
     133        if ( ! is_user_logged_in() && ! bp_displayed_user_id() ) {
    129134            return;
    130 
    131         // Add 'Forums' to the main navigation
    132         $main_nav = array(
    133             'name'                => __( 'Forums', 'buddypress' ),
    134             'slug'                => $this->slug,
    135             'position'            => 80,
    136             'screen_function'     => 'bp_member_forums_screen_topics',
    137             'default_subnav_slug' => 'topics',
    138             'item_css_id'         => $this->id
    139         );
     135        }
    140136
    141137        // Determine user to use
     
    149145
    150146        // User link
    151         $forums_link = trailingslashit( $user_domain . $this->slug );
    152 
    153         // Additional menu if friends is active
     147        $slug        = bp_get_forums_slug();
     148        $forums_link = trailingslashit( $user_domain . $slug );
     149
     150        // Add 'Forums' to the main navigation
     151        $main_nav = array(
     152            'name'                => __( 'Forums', 'buddypress' ),
     153            'slug'                => $slug,
     154            'position'            => 80,
     155            'screen_function'     => 'bp_member_forums_screen_topics',
     156            'default_subnav_slug' => 'topics',
     157            'item_css_id'         => $this->id
     158        );
     159
     160        // Topics started
    154161        $sub_nav[] = array(
    155162            'name'            => __( 'Topics Started', 'buddypress' ),
    156163            'slug'            => 'topics',
    157164            'parent_url'      => $forums_link,
    158             'parent_slug'     => $this->slug,
     165            'parent_slug'     => $slug,
    159166            'screen_function' => 'bp_member_forums_screen_topics',
    160167            'position'        => 20,
     
    162169        );
    163170
    164         // Additional menu if friends is active
     171        // Topics replied to
    165172        $sub_nav[] = array(
    166173            'name'            => __( 'Replied To', 'buddypress' ),
    167174            'slug'            => 'replies',
    168175            'parent_url'      => $forums_link,
    169             'parent_slug'     => $this->slug,
     176            'parent_slug'     => $slug,
    170177            'screen_function' => 'bp_member_forums_screen_replies',
    171178            'position'        => 40,
     
    192199
    193200            // Setup the logged in user variables
    194             $forums_link = trailingslashit( bp_loggedin_user_domain() . $this->slug );
     201            $forums_link = trailingslashit( bp_loggedin_user_domain() . bp_get_forums_slug() );
    195202
    196203            // Add the "My Account" sub menus
     
    199206                'id'     => 'my-account-' . $this->id,
    200207                'title'  => __( 'Forums', 'buddypress' ),
    201                 'href'   => trailingslashit( $forums_link )
     208                'href'   => $forums_link
    202209            );
    203210
     
    207214                'id'     => 'my-account-' . $this->id . '-topics-started',
    208215                'title'  => __( 'Topics Started', 'buddypress' ),
    209                 'href'   => trailingslashit( $forums_link . 'topics' )
     216                'href'   => $forums_link
    210217            );
    211218
     
    234241     */
    235242    public function setup_title() {
    236         $bp = buddypress();
    237243
    238244        // Adjust title based on view
    239245        if ( bp_is_forums_component() ) {
     246            $bp = buddypress();
     247
    240248            if ( bp_is_my_profile() ) {
    241249                $bp->bp_options_title = __( 'Forums', 'buddypress' );
Note: See TracChangeset for help on using the changeset viewer.