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

    r9843 r9936  
    4646     */
    4747    public function includes( $includes = array() ) {
     48
    4849        // Files to include
    4950        $includes = array(
     
    9091
    9192        // Define a slug, if necessary
    92         if ( !defined( 'BP_ACTIVITY_SLUG' ) )
     93        if ( ! defined( 'BP_ACTIVITY_SLUG' ) ) {
    9394            define( 'BP_ACTIVITY_SLUG', $this->id );
     95        }
    9496
    9597        // Global tables for activity component
     
    136138    public function setup_nav( $main_nav = array(), $sub_nav = array() ) {
    137139
    138         // Add 'Activity' to the main navigation
    139         $main_nav = array(
    140             'name'                => _x( 'Activity', 'Profile activity screen nav', 'buddypress' ),
    141             'slug'                => $this->slug,
    142             'position'            => 10,
    143             'screen_function'     => 'bp_activity_screen_my_activity',
    144             'default_subnav_slug' => 'just-me',
    145             'item_css_id'         => $this->id
    146         );
    147 
    148140        // Stop if there is no user displayed or logged in
    149         if ( !is_user_logged_in() && !bp_displayed_user_id() )
     141        if ( ! is_user_logged_in() && ! bp_displayed_user_id() ) {
    150142            return;
     143        }
    151144
    152145        // Determine user to use
     
    159152        }
    160153
    161         // User link
    162         $activity_link = trailingslashit( $user_domain . $this->slug );
     154        $slug          = bp_get_activity_slug();
     155        $activity_link = trailingslashit( $user_domain . $slug );
     156
     157        // Add 'Activity' to the main navigation
     158        $main_nav = array(
     159            'name'                => _x( 'Activity', 'Profile activity screen nav', 'buddypress' ),
     160            'slug'                => $slug,
     161            'position'            => 10,
     162            'screen_function'     => 'bp_activity_screen_my_activity',
     163            'default_subnav_slug' => 'just-me',
     164            'item_css_id'         => $this->id
     165        );
    163166
    164167        // Add the subnav items to the activity nav item if we are using a theme that supports this
     
    167170            'slug'            => 'just-me',
    168171            'parent_url'      => $activity_link,
    169             'parent_slug'     => $this->slug,
     172            'parent_slug'     => $slug,
    170173            'screen_function' => 'bp_activity_screen_my_activity',
    171174            'position'        => 10
     
    178181                'slug'            => 'mentions',
    179182                'parent_url'      => $activity_link,
    180                 'parent_slug'     => $this->slug,
     183                'parent_slug'     => $slug,
    181184                'screen_function' => 'bp_activity_screen_mentions',
    182185                'position'        => 20,
     
    191194                'slug'            => 'favorites',
    192195                'parent_url'      => $activity_link,
    193                 'parent_slug'     => $this->slug,
     196                'parent_slug'     => $slug,
    194197                'screen_function' => 'bp_activity_screen_favorites',
    195198                'position'        => 30,
     
    204207                'slug'            => bp_get_friends_slug(),
    205208                'parent_url'      => $activity_link,
    206                 'parent_slug'     => $this->slug,
     209                'parent_slug'     => $slug,
    207210                'screen_function' => 'bp_activity_screen_friends',
    208211                'position'        => 40,
     
    217220                'slug'            => bp_get_groups_slug(),
    218221                'parent_url'      => $activity_link,
    219                 'parent_slug'     => $this->slug,
     222                'parent_slug'     => $slug,
    220223                'screen_function' => 'bp_activity_screen_groups',
    221224                'position'        => 50,
     
    246249     */
    247250    public function setup_admin_bar( $wp_admin_nav = array() ) {
    248         $bp = buddypress();
    249251
    250252        // Menus for logged in user
     
    252254
    253255            // Setup the logged in user variables
    254             $user_domain   = bp_loggedin_user_domain();
    255             $activity_link = trailingslashit( $user_domain . $this->slug );
     256            $activity_link = trailingslashit( bp_loggedin_user_domain() . bp_get_activity_slug() );
    256257
    257258            // Unread message count
     
    259260                $count = bp_get_total_mention_count_for_user( bp_loggedin_user_id() );
    260261                if ( !empty( $count ) ) {
    261                     $title = sprintf( _x( 'Mentions <span class="count">%s</span>', 'Toolbar Mention logged in user', 'buddypress' ), number_format_i18n( $count ) );
     262                    $title = sprintf( _x( 'Mentions <span class="count">%s</span>', 'Toolbar Mention logged in user', 'buddypress' ), bp_core_number_format( $count ) );
    262263                } else {
    263264                    $title = _x( 'Mentions', 'Toolbar Mention logged in user', 'buddypress' );
     
    267268            // Add the "Activity" sub menu
    268269            $wp_admin_nav[] = array(
    269                 'parent' => $bp->my_account_menu_id,
     270                'parent' => buddypress()->my_account_menu_id,
    270271                'id'     => 'my-account-' . $this->id,
    271272                'title'  => _x( 'Activity', 'My Account Activity sub nav', 'buddypress' ),
    272                 'href'   => trailingslashit( $activity_link )
     273                'href'   => $activity_link
    273274            );
    274275
     
    278279                'id'     => 'my-account-' . $this->id . '-personal',
    279280                'title'  => _x( 'Personal', 'My Account Activity sub nav', 'buddypress' ),
    280                 'href'   => trailingslashit( $activity_link )
     281                'href'   => $activity_link
    281282            );
    282283
     
    336337     */
    337338    public function setup_title() {
    338         $bp = buddypress();
    339339
    340340        // Adjust title based on view
    341341        if ( bp_is_activity_component() ) {
     342            $bp = buddypress();
     343
    342344            if ( bp_is_my_profile() ) {
    343345                $bp->bp_options_title = _x( 'My Activity', 'Page and <title>', 'buddypress' );
     
    361363     */
    362364    public function setup_actions() {
     365
    363366        // Spam prevention
    364367        add_action( 'bp_include', 'bp_activity_setup_akismet' );
Note: See TracChangeset for help on using the changeset viewer.