Index: branches/2.2/src/bp-core/bp-core-filters.php
===================================================================
--- branches/2.2/src/bp-core/bp-core-filters.php	(revision 9440)
+++ branches/2.2/src/bp-core/bp-core-filters.php	(revision 9732)
@@ -506,11 +506,12 @@
  * @global object $bp BuddyPress global settings.
  *
- * @param string $title Original page title.
- * @param string $sep How to separate the various items within the page title.
- * @param string $seplocation Direction to display title.
- * @return string New page title.
- */
-function bp_modify_page_title( $title, $sep = '', $seplocation = '' ) {
-	global $bp;
+ * @param  string $title       Original page title.
+ * @param  string $sep         How to separate the various items within the page title.
+ * @param  string $seplocation Direction to display title.
+ *
+ * @return string              New page title.
+ */
+function bp_modify_page_title( $title = '', $sep = '&raquo;', $seplocation = 'right' ) {
+	global $bp, $paged, $page, $_wp_theme_features;
 
 	// If this is not a BP page, just return the title produced by WP
@@ -529,21 +530,45 @@
 	}
 
-	$title = '';
+	// Return WP's title if not a BuddyPress page
+	if ( ! is_buddypress() ) {
+		return $title;
+	}
+
+	// Setup an empty title parts array
+	$title_parts = array();
+
+	// Is there a displayed user, and do they have a name?
+	$displayed_user_name = bp_get_displayed_user_fullname();
+
+	/**
+	 * Strip span tags out of title part strings.
+	 *
+	 * This is a temporary function for compatibility with WordPress versions
+	 * less than 4.0, and should be removed at a later date.
+	 *
+	 * @param  string $title_part
+	 * @return string
+	 */
+	function _bp_strip_spans_from_title( $title_part = '' ) {
+		$title = $title_part;
+		$span = strpos( $title, '<span' );
+		if ( false !== $span ) {
+			$title = substr( $title, 0, $span - 1 );
+		}
+		return $title;
+	}
 
 	// Displayed user
-	if ( bp_get_displayed_user_fullname() && ! is_404() ) {
+	if ( ! empty( $displayed_user_name ) && ! is_404() ) {
+
 		// Get the component's ID to try and get its name
 		$component_id = $component_name = bp_current_component();
 
+		// Set empty subnav name
+		$component_subnav_name = '';
+
 		// Use the component nav name
 		if ( ! empty( $bp->bp_nav[$component_id] ) ) {
-			// Remove counts that are added by the nav item
-			$span = strpos( $bp->bp_nav[ $component_id ]['name'], '<span' );
-			if ( false !== $span ) {
-				$component_name = substr( $bp->bp_nav[ $component_id ]['name'], 0, $span - 1 );
-
-			} else {
-				$component_name = $bp->bp_nav[ $component_id ]['name'];
-			}
+			$component_name = _bp_strip_spans_from_title( $bp->bp_nav[ $component_id ]['name'] );
 
 		// Fall back on the component ID
@@ -556,28 +581,23 @@
 			$component_subnav_name = wp_filter_object_list( $bp->bp_options_nav[ $component_id ], array( 'slug' => bp_current_action() ), 'and', 'name' );
 
-			if ( $component_subnav_name ) {
+			if ( ! empty( $component_subnav_name ) ) {
 				$component_subnav_name = array_shift( $component_subnav_name );
-			} else {
-				$component_subnav_name = '';
 			}
-
-		} else {
-			$component_subnav_name = '';
 		}
 
 		// If on the user profile's landing page, just use the fullname
-		if ( bp_is_current_component( $bp->default_component ) && bp_get_requested_url() === bp_displayed_user_domain() ) {
-			$title = bp_get_displayed_user_fullname();
+		if ( bp_is_current_component( $bp->default_component ) && ( bp_get_requested_url() === bp_displayed_user_domain() ) ) {
+			$title_parts[] = $displayed_user_name;
 
 		// Use component name on member pages
 		} else {
+			$title_parts = array_merge( $title_parts, array_map( 'strip_tags', array(
+				$displayed_user_name,
+				$component_name,
+			) ) );
+
 			// If we have a subnav name, add it separately for localization
 			if ( ! empty( $component_subnav_name ) ) {
-				// translators: construct the page title. 1 = user name, 2 = component name, 3 = separator, 4 = component subnav name
-				$title = strip_tags( sprintf( __( '%1$s %3$s %2$s %3$s %4$s', 'buddypress' ), bp_get_displayed_user_fullname(), $component_name, $sep, $component_subnav_name ) );
-
-			} else {
-				// translators: construct the page title. 1 = user name, 2 = component name, 3 = separator
-				$title = strip_tags( sprintf( __( '%1$s %3$s %2$s', 'buddypress' ), bp_get_displayed_user_fullname(), $component_name, $sep ) );
+				$title_parts[] = strip_tags( $component_subnav_name );
 			}
 		}
@@ -585,12 +605,10 @@
 	// A single group
 	} elseif ( bp_is_active( 'groups' ) && ! empty( $bp->groups->current_group ) && ! empty( $bp->bp_options_nav[ $bp->groups->current_group->slug ] ) ) {
-		$subnav = isset( $bp->bp_options_nav[ $bp->groups->current_group->slug ][ bp_current_action() ]['name'] ) ? $bp->bp_options_nav[ $bp->groups->current_group->slug ][ bp_current_action() ]['name'] : '';
-		// translators: 1 = group name, 2 = group nav section name, 3 = separator
-		$title = sprintf( __( '%1$s %3$s %2$s', 'buddypress' ), $bp->bp_options_title, $subnav, $sep );
+		$subnav      = isset( $bp->bp_options_nav[ $bp->groups->current_group->slug ][ bp_current_action() ]['name'] ) ? $bp->bp_options_nav[ $bp->groups->current_group->slug ][ bp_current_action() ]['name'] : '';
+		$title_parts = array( $bp->bp_options_title, $subnav );
 
 	// A single item from a component other than groups
 	} elseif ( bp_is_single_item() ) {
-		// translators: 1 = component item name, 2 = component nav section name, 3 = separator
-		$title = sprintf( __( '%1$s %3$s %2$s', 'buddypress' ), $bp->bp_options_title, $bp->bp_options_nav[ bp_current_item() ][ bp_current_action() ]['name'], $sep );
+		$title_parts = array( $bp->bp_options_title, $bp->bp_options_nav[ bp_current_item() ][ bp_current_action() ]['name'] );
 
 	// An index or directory
@@ -599,33 +617,75 @@
 
 		// No current component (when does this happen?)
-		if ( empty( $current_component ) ) {
-			$title = _x( 'Directory', 'component directory title', 'buddypress' );
-		} else {
-			$title = bp_get_directory_title( $current_component );
-		}
+		$title_parts = array( _x( 'Directory', 'component directory title', 'buddypress' ) );
+
+		if ( ! empty( $current_component ) ) {
+			$title_parts = array( bp_get_directory_title( $current_component ) );
+ 		}
 
 	// Sign up page
 	} elseif ( bp_is_register_page() ) {
-		$title = __( 'Create an Account', 'buddypress' );
+		$title_parts = array( __( 'Create an Account', 'buddypress' ) );
 
 	// Activation page
 	} elseif ( bp_is_activation_page() ) {
-		$title = __( 'Activate your Account', 'buddypress' );
+		$title_parts = array( __( 'Activate your Account', 'buddypress' ) );
 
 	// Group creation page
 	} elseif ( bp_is_group_create() ) {
-		$title = __( 'Create a Group', 'buddypress' );
+		$title_parts = array( __( 'Create a Group', 'buddypress' ) );
 
 	// Blog creation page
 	} elseif ( bp_is_create_blog() ) {
-		$title = __( 'Create a Site', 'buddypress' );
-	}
-
-	// Some BP nav items contain item counts. Remove them
-	$title = preg_replace( '|<span>[0-9]+</span>|', '', $title );
-
-	return apply_filters( 'bp_modify_page_title', $title . ' ' . $sep . ' ', $title, $sep, $seplocation );
-}
-add_filter( 'wp_title', 'bp_modify_page_title', 10, 3 );
+		$title_parts = array( __( 'Create a Site', 'buddypress' ) );
+	}
+
+	// Strip spans
+	$title_parts = array_map( '_bp_strip_spans_from_title', $title_parts );
+
+	// sep on right, so reverse the order
+	if ( 'right' == $seplocation ) {
+		$title_parts = array_reverse( $title_parts );
+	}
+
+	// Get the blog name, so we can check if the original $title included it
+	$blogname = get_bloginfo( 'name', 'display' );
+
+	/**
+	 * Are we going to fake 'title-tag' theme functionality?
+	 *
+	 * @link https://buddypress.trac.wordpress.org/ticket/6107
+	 * @see wp_title()
+	 */
+	$title_tag_compatibility = (bool) ( ! empty( $_wp_theme_features['title-tag'] ) || strstr( $title, $blogname ) );
+
+	// Append the site title to title parts if theme supports title tag
+	if ( true === $title_tag_compatibility ) {
+		$title_parts[] = $blogname;
+
+		if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
+			$title_parts[] = sprintf( __( 'Page %s' ), max( $paged, $page ) );
+		}
+	}
+
+	// Pad the separator with 1 space on each side
+	$prefix = str_pad( $sep, strlen( $sep ) + 2, ' ', STR_PAD_BOTH );
+
+	// Join the parts together
+	$new_title = join( $prefix, array_filter( $title_parts ) );
+
+	// Append the prefix for pre `title-tag` compatibility
+	if ( false === $title_tag_compatibility ) {
+		$new_title = $new_title . $prefix;
+	}
+
+	/**
+	 * @param  string the BuddyPress page title
+	 * @param  string $title the original WordPress page title
+	 * @param  string $sep the title parts separator
+	 * @param  string $seplocation Location of the separator (left or right).
+	 */
+	return apply_filters( 'bp_modify_page_title', $new_title, $title, $sep, $seplocation );
+}
+add_filter( 'wp_title', 'bp_modify_page_title', 20, 3 );
 add_filter( 'bp_modify_page_title', 'wptexturize'     );
 add_filter( 'bp_modify_page_title', 'convert_chars'   );
