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

    r9898 r9936  
    144144         */
    145145        public function setup_nav( $main_nav = array(), $sub_nav = array() ) {
    146                 $bp = buddypress();
    147146
    148147                /**
     
    155154                }
    156155
    157                 // Add 'Sites' to the main navigation
    158                 $count    = (int) bp_get_total_blog_count_for_user();
    159                 $class    = ( 0 === $count ) ? 'no-count' : 'count';
    160                 $nav_text = sprintf( __( 'Sites <span class="%s">%s</span>', 'buddypress' ), esc_attr( $class ), number_format_i18n( $count )  );
    161                 $main_nav = array(
    162                         'name'                => $nav_text,
    163                         'slug'                => $this->slug,
    164                         'position'            => 30,
    165                         'screen_function'     => 'bp_blogs_screen_my_blogs',
    166                         'default_subnav_slug' => 'my-sites',
    167                         'item_css_id'         => $this->id
    168                 );
    169 
    170156                // Determine user to use
    171157                if ( bp_displayed_user_domain() ) {
     
    177163                }
    178164
    179                 $parent_url = trailingslashit( $user_domain . bp_get_blogs_slug() );
     165                $slug       = bp_get_blogs_slug();
     166                $parent_url = trailingslashit( $user_domain . $slug );
     167
     168                // Add 'Sites' to the main navigation
     169                $count    = (int) bp_get_total_blog_count_for_user();
     170                $class    = ( 0 === $count ) ? 'no-count' : 'count';
     171                $nav_text = sprintf( __( 'Sites <span class="%s">%s</span>', 'buddypress' ), esc_attr( $class ), bp_core_number_format( $count )  );
     172                $main_nav = array(
     173                        'name'                => $nav_text,
     174                        'slug'                => $slug,
     175                        'position'            => 30,
     176                        'screen_function'     => 'bp_blogs_screen_my_blogs',
     177                        'default_subnav_slug' => 'my-sites',
     178                        'item_css_id'         => $this->id
     179                );
    180180
    181181                $sub_nav[] = array(
     
    183183                        'slug'            => 'my-sites',
    184184                        'parent_url'      => $parent_url,
    185                         'parent_slug'     => $bp->blogs->slug,
     185                        'parent_slug'     => $slug,
    186186                        'screen_function' => 'bp_blogs_screen_my_blogs',
    187187                        'position'        => 10
     
    203203         */
    204204        public function setup_admin_bar( $wp_admin_nav = array() ) {
    205                 $bp = buddypress();
    206205
    207206                /**
     
    217216                if ( is_user_logged_in() ) {
    218217
    219                         $blogs_link = trailingslashit( bp_loggedin_user_domain() . $this->slug );
     218                        // Setup the logged in user variables
     219                        $blogs_link = trailingslashit( bp_loggedin_user_domain() . bp_get_blogs_slug() );
    220220
    221221                        // Add the "Sites" sub menu
    222222                        $wp_admin_nav[] = array(
    223                                 'parent' => $bp->my_account_menu_id,
     223                                'parent' => buddypress()->my_account_menu_id,
    224224                                'id'     => 'my-account-' . $this->id,
    225225                                'title'  => __( 'Sites', 'buddypress' ),
    226                                 'href'   => trailingslashit( $blogs_link )
     226                                'href'   => $blogs_link
    227227                        );
    228228
     
    232232                                'id'     => 'my-account-' . $this->id . '-my-sites',
    233233                                'title'  => __( 'My Sites', 'buddypress' ),
    234                                 'href'   => trailingslashit( $blogs_link )
     234                                'href'   => $blogs_link
    235235                        );
    236236
     
    253253         */
    254254        public function setup_title() {
    255                 $bp = buddypress();
    256255
    257256                // Set up the component options navigation for Site
    258257                if ( bp_is_blogs_component() ) {
     258                        $bp = buddypress();
     259
    259260                        if ( bp_is_my_profile() ) {
    260261                                if ( bp_is_active( 'xprofile' ) ) {
     
    300301         */
    301302        public function post_tracking_args( $params = null, $post_type = 0 ) {
     303
    302304                /**
    303305                 * Filters the post types to track for the Blogs component.
Note: See TracChangeset for help on using the changeset viewer.