Skip to:
Content

BuddyPress.org

Ticket #6107: 6107.06.patch

File 6107.06.patch, 7.6 KB (added by johnjamesjacoby, 5 years ago)
  • src/bp-core/bp-core-filters.php

     
    505505 * @see wp_title()
    506506 * @global object $bp BuddyPress global settings.
    507507 *
    508  * @param string $title Original page title.
    509  * @param string $sep How to separate the various items within the page title.
    510  * @param string $seplocation Direction to display title.
    511  * @return string New page title.
     508 * @param  string $title       Original page title.
     509 * @param  string $sep         How to separate the various items within the page title.
     510 * @param  string $seplocation Direction to display title.
     511 *
     512 * @return string              New page title.
    512513 */
    513 function bp_modify_page_title( $title, $sep = '', $seplocation = '' ) {
    514         global $bp;
     514function bp_modify_page_title( $title = '', $sep = '»', $seplocation = 'right' ) {
     515        global $bp, $paged, $page, $_wp_theme_features;
    515516
    516517        // If this is not a BP page, just return the title produced by WP
    517518        if ( bp_is_blog_page() ) {
     
    528529                return $title;
    529530        }
    530531
    531         $title = '';
     532        // Return WP's title if not a BuddyPress page
     533        if ( ! is_buddypress() ) {
     534                return $title;
     535        }
    532536
     537        // Setup an empty title parts array
     538        $title_parts = array();
     539
     540        // Is there a displayed user, and do they have a name?
     541        $displayed_user_name = bp_get_displayed_user_fullname();
     542
    533543        // Displayed user
    534         if ( bp_get_displayed_user_fullname() && ! is_404() ) {
     544        if ( ! empty( $displayed_user_name ) && ! is_404() ) {
     545
    535546                // Get the component's ID to try and get its name
    536547                $component_id = $component_name = bp_current_component();
    537548
     
    567578
    568579                // If on the user profile's landing page, just use the fullname
    569580                if ( bp_is_current_component( $bp->default_component ) && bp_get_requested_url() === bp_displayed_user_domain() ) {
    570                         $title = bp_get_displayed_user_fullname();
     581                        $title_parts[] = $displayed_user_name;
    571582
    572583                // Use component name on member pages
    573584                } else {
     585                        $title_parts = array_merge( $title_parts, array_map( 'strip_tags', array(
     586                                $displayed_user_name,
     587                                $component_name,
     588                        ) ) );
     589
    574590                        // If we have a subnav name, add it separately for localization
    575591                        if ( ! empty( $component_subnav_name ) ) {
    576                                 // translators: construct the page title. 1 = user name, 2 = component name, 3 = separator, 4 = component subnav name
    577                                 $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 ) );
    578 
    579                         } else {
    580                                 // translators: construct the page title. 1 = user name, 2 = component name, 3 = separator
    581                                 $title = strip_tags( sprintf( __( '%1$s %3$s %2$s', 'buddypress' ), bp_get_displayed_user_fullname(), $component_name, $sep ) );
     592                                $title_parts[] = strip_tags( $component_subnav_name );
    582593                        }
    583594                }
    584595
    585596        // A single group
    586597        } elseif ( bp_is_active( 'groups' ) && ! empty( $bp->groups->current_group ) && ! empty( $bp->bp_options_nav[ $bp->groups->current_group->slug ] ) ) {
    587                 $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'] : '';
    588                 // translators: 1 = group name, 2 = group nav section name, 3 = separator
    589                 $title = sprintf( __( '%1$s %3$s %2$s', 'buddypress' ), $bp->bp_options_title, $subnav, $sep );
     598                $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'] : '';
     599                $title_parts = array( $bp->bp_options_title, $subnav );
    590600
    591601        // A single item from a component other than groups
    592602        } elseif ( bp_is_single_item() ) {
    593                 // translators: 1 = component item name, 2 = component nav section name, 3 = separator
    594                 $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 );
     603                $title_parts = array( $bp->bp_options_title, $bp->bp_options_nav[ bp_current_item() ][ bp_current_action() ]['name'] );
    595604
    596605        // An index or directory
    597606        } elseif ( bp_is_directory() ) {
    598607                $current_component = bp_current_component();
    599608
    600609                // No current component (when does this happen?)
    601                 if ( empty( $current_component ) ) {
    602                         $title = _x( 'Directory', 'component directory title', 'buddypress' );
    603                 } else {
    604                         $title = bp_get_directory_title( $current_component );
    605                 }
     610                $title_parts = array( _x( 'Directory', 'component directory title', 'buddypress' ) );
    606611
     612                if ( ! empty( $current_component ) ) {
     613                        $title_parts = array( bp_get_directory_title( $current_component ) );
     614                }
     615
    607616        // Sign up page
    608617        } elseif ( bp_is_register_page() ) {
    609                 $title = __( 'Create an Account', 'buddypress' );
     618                $title_parts = array( __( 'Create an Account', 'buddypress' ) );
    610619
    611620        // Activation page
    612621        } elseif ( bp_is_activation_page() ) {
    613                 $title = __( 'Activate your Account', 'buddypress' );
     622                $title_parts = array( __( 'Activate your Account', 'buddypress' ) );
    614623
    615624        // Group creation page
    616625        } elseif ( bp_is_group_create() ) {
    617                 $title = __( 'Create a Group', 'buddypress' );
     626                $title_parts = array( __( 'Create a Group', 'buddypress' ) );
    618627
    619628        // Blog creation page
    620629        } elseif ( bp_is_create_blog() ) {
    621                 $title = __( 'Create a Site', 'buddypress' );
     630                $title_parts = array( __( 'Create a Site', 'buddypress' ) );
    622631        }
    623632
    624         // Some BP nav items contain item counts. Remove them
    625         $title = preg_replace( '|<span>[0-9]+</span>|', '', $title );
     633        /**
     634         * Strip span tags out of title part strings.
     635         *
     636         * This is a temporary function for compatibility with WordPress versions
     637         * less than 4.0, and should be removed at a later date.
     638         *
     639         * @param  string $title_part
     640         * @return string
     641         */
     642        function _bp_strip_spans_from_title( $title_part = '' ) {
     643                return preg_replace( '|<span>[0-9]+</span>|' , '', $title_part );
     644        }
    626645
    627         return apply_filters( 'bp_modify_page_title', $title . ' ' . $sep . ' ', $title, $sep, $seplocation );
     646        // Strip spans
     647        $title_parts = array_map( '_bp_strip_spans_from_title', $title_parts );
     648
     649        // Get the blog name, so we can check if the original $title included it
     650        $blogname = get_bloginfo( 'name', 'display' );
     651
     652        /**
     653         * Are we going to fake 'title-tag' theme functionality?
     654         *
     655         * @link https://buddypress.trac.wordpress.org/ticket/6107
     656         * @see wp_title()
     657         */
     658        $title_tag_compatibility = (bool) ( ! empty( $_wp_theme_features['title-tag'] ) || strstr( $title, $blogname ) );
     659
     660        // Append the site title to title parts if theme supports title tag
     661        if ( true === $title_tag_compatibility ) {
     662                $title_parts[]    = $blogname;
     663                $site_description = get_bloginfo( 'description', 'display' );
     664                if ( ! empty( $site_description ) && ( is_home() || is_front_page() ) ) {
     665                        $title_parts[] = $site_description;
     666                }
     667
     668                if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
     669                        $title_parts[] = sprintf( __( 'Page %s' ), max( $paged, $page ) );
     670                }
     671        }
     672
     673        // Join the parts together
     674        $new_title = join( ' ' . $sep . ' ', array_filter( $title_parts ) );
     675
     676        /**
     677         * @param  string the BuddyPress page title
     678         * @param  string $title the original WordPress page title
     679         * @param  string $sep the title parts separator
     680         * @param  string $seplocation Location of the separator (left or right).
     681         */
     682        return apply_filters( 'bp_modify_page_title', $new_title, $title, $sep, $seplocation );
    628683}
    629 add_filter( 'wp_title', 'bp_modify_page_title', 10, 3 );
     684add_filter( 'wp_title', 'bp_modify_page_title', 20, 3 );
    630685add_filter( 'bp_modify_page_title', 'wptexturize'     );
    631686add_filter( 'bp_modify_page_title', 'convert_chars'   );
    632687add_filter( 'bp_modify_page_title', 'esc_html'        );