Skip to:
Content

BuddyPress.org

Changeset 9732


Ignore:
Timestamp:
04/10/2015 11:05:19 PM (10 years ago)
Author:
johnjamesjacoby
Message:

Core: Improvements to bp_modify_page_title():

  • Better compatibility with core themes
  • Play more-nicely with plugins competing for ownership of competitive wp_title filter
  • In 2.2 branch, for 2.2.2 release

Props hnla, mercime, imath. Fixes #6107.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2/src/bp-core/bp-core-filters.php

    r9399 r9732  
    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.
    512  */
    513 function bp_modify_page_title( $title, $sep = '', $seplocation = '' ) {
    514     global $bp;
     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.
     513 */
     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
     
    529530    }
    530531
    531     $title = '';
     532    // Return WP's title if not a BuddyPress page
     533    if ( ! is_buddypress() ) {
     534        return $title;
     535    }
     536
     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
     543    /**
     544     * Strip span tags out of title part strings.
     545     *
     546     * This is a temporary function for compatibility with WordPress versions
     547     * less than 4.0, and should be removed at a later date.
     548     *
     549     * @param  string $title_part
     550     * @return string
     551     */
     552    function _bp_strip_spans_from_title( $title_part = '' ) {
     553        $title = $title_part;
     554        $span = strpos( $title, '<span' );
     555        if ( false !== $span ) {
     556            $title = substr( $title, 0, $span - 1 );
     557        }
     558        return $title;
     559    }
    532560
    533561    // Displayed user
    534     if ( bp_get_displayed_user_fullname() && ! is_404() ) {
     562    if ( ! empty( $displayed_user_name ) && ! is_404() ) {
     563
    535564        // Get the component's ID to try and get its name
    536565        $component_id = $component_name = bp_current_component();
    537566
     567        // Set empty subnav name
     568        $component_subnav_name = '';
     569
    538570        // Use the component nav name
    539571        if ( ! empty( $bp->bp_nav[$component_id] ) ) {
    540             // Remove counts that are added by the nav item
    541             $span = strpos( $bp->bp_nav[ $component_id ]['name'], '<span' );
    542             if ( false !== $span ) {
    543                 $component_name = substr( $bp->bp_nav[ $component_id ]['name'], 0, $span - 1 );
    544 
    545             } else {
    546                 $component_name = $bp->bp_nav[ $component_id ]['name'];
    547             }
     572            $component_name = _bp_strip_spans_from_title( $bp->bp_nav[ $component_id ]['name'] );
    548573
    549574        // Fall back on the component ID
     
    556581            $component_subnav_name = wp_filter_object_list( $bp->bp_options_nav[ $component_id ], array( 'slug' => bp_current_action() ), 'and', 'name' );
    557582
    558             if ( $component_subnav_name ) {
     583            if ( ! empty( $component_subnav_name ) ) {
    559584                $component_subnav_name = array_shift( $component_subnav_name );
    560             } else {
    561                 $component_subnav_name = '';
    562585            }
    563 
    564         } else {
    565             $component_subnav_name = '';
    566586        }
    567587
    568588        // If on the user profile's landing page, just use the fullname
    569         if ( bp_is_current_component( $bp->default_component ) && bp_get_requested_url() === bp_displayed_user_domain() ) {
    570             $title = bp_get_displayed_user_fullname();
     589        if ( bp_is_current_component( $bp->default_component ) && ( bp_get_requested_url() === bp_displayed_user_domain() ) ) {
     590            $title_parts[] = $displayed_user_name;
    571591
    572592        // Use component name on member pages
    573593        } else {
     594            $title_parts = array_merge( $title_parts, array_map( 'strip_tags', array(
     595                $displayed_user_name,
     596                $component_name,
     597            ) ) );
     598
    574599            // If we have a subnav name, add it separately for localization
    575600            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 ) );
     601                $title_parts[] = strip_tags( $component_subnav_name );
    582602            }
    583603        }
     
    585605    // A single group
    586606    } 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 );
     607        $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'] : '';
     608        $title_parts = array( $bp->bp_options_title, $subnav );
    590609
    591610    // A single item from a component other than groups
    592611    } 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 );
     612        $title_parts = array( $bp->bp_options_title, $bp->bp_options_nav[ bp_current_item() ][ bp_current_action() ]['name'] );
    595613
    596614    // An index or directory
     
    599617
    600618        // 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         }
     619        $title_parts = array( _x( 'Directory', 'component directory title', 'buddypress' ) );
     620
     621        if ( ! empty( $current_component ) ) {
     622            $title_parts = array( bp_get_directory_title( $current_component ) );
     623        }
    606624
    607625    // Sign up page
    608626    } elseif ( bp_is_register_page() ) {
    609         $title = __( 'Create an Account', 'buddypress' );
     627        $title_parts = array( __( 'Create an Account', 'buddypress' ) );
    610628
    611629    // Activation page
    612630    } elseif ( bp_is_activation_page() ) {
    613         $title = __( 'Activate your Account', 'buddypress' );
     631        $title_parts = array( __( 'Activate your Account', 'buddypress' ) );
    614632
    615633    // Group creation page
    616634    } elseif ( bp_is_group_create() ) {
    617         $title = __( 'Create a Group', 'buddypress' );
     635        $title_parts = array( __( 'Create a Group', 'buddypress' ) );
    618636
    619637    // Blog creation page
    620638    } elseif ( bp_is_create_blog() ) {
    621         $title = __( 'Create a Site', 'buddypress' );
    622     }
    623 
    624     // Some BP nav items contain item counts. Remove them
    625     $title = preg_replace( '|<span>[0-9]+</span>|', '', $title );
    626 
    627     return apply_filters( 'bp_modify_page_title', $title . ' ' . $sep . ' ', $title, $sep, $seplocation );
    628 }
    629 add_filter( 'wp_title', 'bp_modify_page_title', 10, 3 );
     639        $title_parts = array( __( 'Create a Site', 'buddypress' ) );
     640    }
     641
     642    // Strip spans
     643    $title_parts = array_map( '_bp_strip_spans_from_title', $title_parts );
     644
     645    // sep on right, so reverse the order
     646    if ( 'right' == $seplocation ) {
     647        $title_parts = array_reverse( $title_parts );
     648    }
     649
     650    // Get the blog name, so we can check if the original $title included it
     651    $blogname = get_bloginfo( 'name', 'display' );
     652
     653    /**
     654     * Are we going to fake 'title-tag' theme functionality?
     655     *
     656     * @link https://buddypress.trac.wordpress.org/ticket/6107
     657     * @see wp_title()
     658     */
     659    $title_tag_compatibility = (bool) ( ! empty( $_wp_theme_features['title-tag'] ) || strstr( $title, $blogname ) );
     660
     661    // Append the site title to title parts if theme supports title tag
     662    if ( true === $title_tag_compatibility ) {
     663        $title_parts[] = $blogname;
     664
     665        if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
     666            $title_parts[] = sprintf( __( 'Page %s' ), max( $paged, $page ) );
     667        }
     668    }
     669
     670    // Pad the separator with 1 space on each side
     671    $prefix = str_pad( $sep, strlen( $sep ) + 2, ' ', STR_PAD_BOTH );
     672
     673    // Join the parts together
     674    $new_title = join( $prefix, array_filter( $title_parts ) );
     675
     676    // Append the prefix for pre `title-tag` compatibility
     677    if ( false === $title_tag_compatibility ) {
     678        $new_title = $new_title . $prefix;
     679    }
     680
     681    /**
     682     * @param  string the BuddyPress page title
     683     * @param  string $title the original WordPress page title
     684     * @param  string $sep the title parts separator
     685     * @param  string $seplocation Location of the separator (left or right).
     686     */
     687    return apply_filters( 'bp_modify_page_title', $new_title, $title, $sep, $seplocation );
     688}
     689add_filter( 'wp_title', 'bp_modify_page_title', 20, 3 );
    630690add_filter( 'bp_modify_page_title', 'wptexturize'     );
    631691add_filter( 'bp_modify_page_title', 'convert_chars'   );
Note: See TracChangeset for help on using the changeset viewer.