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

    r9819 r9936  
    6868
    6969                // Define a slug, if necessary
    70                 if ( !defined( 'BP_NOTIFICATIONS_SLUG' ) ) {
     70                if ( ! defined( 'BP_NOTIFICATIONS_SLUG' ) ) {
    7171                        define( 'BP_NOTIFICATIONS_SLUG', $this->id );
    7272                }
     
    104104        public function setup_nav( $main_nav = array(), $sub_nav = array() ) {
    105105
    106                 // Only grab count if we're on a user page and current user has access
    107                 if ( bp_is_user() && bp_user_has_access() ) {
    108                         $count    = bp_notifications_get_unread_notification_count( bp_displayed_user_id() );
    109                         $class    = ( 0 === $count ) ? 'no-count' : 'count';
    110                         $nav_name = sprintf( _x( 'Notifications <span class="%s">%s</span>', 'Profile screen nav', 'buddypress' ), esc_attr( $class ), number_format_i18n( $count ) );
    111                 } else {
    112                         $nav_name = _x( 'Notifications', 'Profile screen nav', 'buddypress' );
    113                 }
    114 
    115                 // Add 'Notifications' to the main navigation
    116                 $main_nav = array(
    117                         'name'                    => $nav_name,
    118                         'slug'                    => $this->slug,
    119                         'position'                => 30,
    120                         'show_for_displayed_user' => bp_core_can_edit_settings(),
    121                         'screen_function'         => 'bp_notifications_screen_unread',
    122                         'default_subnav_slug'     => 'unread',
    123                         'item_css_id'             => $this->id,
    124                 );
    125 
    126106                // Determine user to use
    127107                if ( bp_displayed_user_domain() ) {
     
    133113                }
    134114
    135                 $notifications_link = trailingslashit( $user_domain . bp_get_notifications_slug() );
     115                $access             = bp_core_can_edit_settings();
     116                $slug               = bp_get_notifications_slug();
     117                $notifications_link = trailingslashit( $user_domain . $slug );
     118
     119                // Only grab count if we're on a user page and current user has access
     120                if ( bp_is_user() && bp_user_has_access() ) {
     121                        $count    = bp_notifications_get_unread_notification_count( bp_displayed_user_id() );
     122                        $class    = ( 0 === $count ) ? 'no-count' : 'count';
     123                        $nav_name = sprintf( _x( 'Notifications <span class="%s">%s</span>', 'Profile screen nav', 'buddypress' ), esc_attr( $class ), bp_core_number_format( $count ) );
     124                } else {
     125                        $nav_name = _x( 'Notifications', 'Profile screen nav', 'buddypress' );
     126                }
     127
     128                // Add 'Notifications' to the main navigation
     129                $main_nav = array(
     130                        'name'                    => $nav_name,
     131                        'slug'                    => $slug,
     132                        'position'                => 30,
     133                        'show_for_displayed_user' => $access,
     134                        'screen_function'         => 'bp_notifications_screen_unread',
     135                        'default_subnav_slug'     => 'unread',
     136                        'item_css_id'             => $this->id,
     137                );
    136138
    137139                // Add the subnav items to the notifications nav item
     
    140142                        'slug'            => 'unread',
    141143                        'parent_url'      => $notifications_link,
    142                         'parent_slug'     => bp_get_notifications_slug(),
     144                        'parent_slug'     => $slug,
    143145                        'screen_function' => 'bp_notifications_screen_unread',
    144146                        'position'        => 10,
    145147                        'item_css_id'     => 'notifications-my-notifications',
    146                         'user_has_access' => bp_core_can_edit_settings(),
     148                        'user_has_access' => $access,
    147149                );
    148150
     
    151153                        'slug'            => 'read',
    152154                        'parent_url'      => $notifications_link,
    153                         'parent_slug'     => bp_get_notifications_slug(),
     155                        'parent_slug'     => $slug,
    154156                        'screen_function' => 'bp_notifications_screen_read',
    155157                        'position'        => 20,
    156                         'user_has_access' => bp_core_can_edit_settings(),
     158                        'user_has_access' => $access,
    157159                );
    158160
     
    177179
    178180                        // Setup the logged in user variables
    179                         $notifications_link = trailingslashit( bp_loggedin_user_domain() . $this->slug );
     181                        $notifications_link = trailingslashit( bp_loggedin_user_domain() . bp_get_notifications_slug() );
    180182
    181183                        // Pending notification requests
    182184                        $count = bp_notifications_get_unread_notification_count( bp_loggedin_user_id() );
    183185                        if ( ! empty( $count ) ) {
    184                                 $title  = sprintf( _x( 'Notifications <span class="count">%s</span>', 'My Account Notification pending', 'buddypress' ), number_format_i18n( $count ) );
    185                                 $unread = sprintf( _x( 'Unread <span class="count">%s</span>', 'My Account Notification pending', 'buddypress' ), number_format_i18n( $count ) );
     186                                $title  = sprintf( _x( 'Notifications <span class="count">%s</span>', 'My Account Notification pending', 'buddypress' ), bp_core_number_format( $count ) );
     187                                $unread = sprintf( _x( 'Unread <span class="count">%s</span>',        'My Account Notification pending', 'buddypress' ), bp_core_number_format( $count ) );
    186188                        } else {
    187                                 $title  = _x( 'Notifications', 'My Account Notification', 'buddypress' );
    188                                 $unread = _x( 'Unread', 'My Account Notification sub nav', 'buddypress' );
     189                                $title  = _x( 'Notifications', 'My Account Notification',         'buddypress' );
     190                                $unread = _x( 'Unread',        'My Account Notification sub nav', 'buddypress' );
    189191                        }
    190192
     
    194196                                'id'     => 'my-account-' . $this->id,
    195197                                'title'  => $title,
    196                                 'href'   => trailingslashit( $notifications_link ),
     198                                'href'   => $notifications_link
    197199                        );
    198200
     
    202204                                'id'     => 'my-account-' . $this->id . '-unread',
    203205                                'title'  => $unread,
    204                                 'href'   => trailingslashit( $notifications_link ),
     206                                'href'   => $notifications_link
    205207                        );
    206208
     
    223225         */
    224226        public function setup_title() {
    225                 $bp = buddypress();
    226227
    227228                // Adjust title
    228229                if ( bp_is_notifications_component() ) {
     230                        $bp = buddypress();
     231
    229232                        if ( bp_is_my_profile() ) {
    230233                                $bp->bp_options_title = __( 'Notifications', 'buddypress' );
Note: See TracChangeset for help on using the changeset viewer.