Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
04/25/2015 09:04:00 AM (6 years ago)
Author:
djpaul
Message:

Core: Improvements to bp_modify_page_title (trunk)

  • 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

Ports r9732, r9734, r9741 from the 2.2 branch to trunk.

Props hnla, mercime, imath, johnjamesjacoby. Fixes #6107.

File:
1 edited

Legend:

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

    r9664 r9801  
    644644 *
    645645 * @see wp_title()
    646  *
    647  * @param string $title Original page title.
    648  * @param string $sep How to separate the various items within the page title.
    649  * @param string $seplocation Direction to display title.
    650  * @return string New page title.
    651  */
    652 function bp_modify_page_title( $title, $sep = '', $seplocation = '' ) {
     646 * @global object $bp BuddyPress global settings.
     647 *
     648 * @param  string $title       Original page title.
     649 * @param  string $sep         How to separate the various items within the page title.
     650 * @param  string $seplocation Direction to display title.
     651 *
     652 * @return string              New page title.
     653 */
     654function bp_modify_page_title( $title = '', $sep = '»', $seplocation = 'right' ) {
     655    global $bp, $paged, $page, $_wp_theme_features;
    653656
    654657    // If this is not a BP page, just return the title produced by WP
     
    667670    }
    668671
    669     $bp    = buddypress();
    670     $title = '';
     672    // Return WP's title if not a BuddyPress page
     673    if ( ! is_buddypress() ) {
     674        return $title;
     675    }
     676
     677    // Setup an empty title parts array
     678    $title_parts = array();
     679
     680    // Is there a displayed user, and do they have a name?
     681    $displayed_user_name = bp_get_displayed_user_fullname();
    671682
    672683    // Displayed user
    673     if ( bp_get_displayed_user_fullname() && ! is_404() ) {
     684    if ( ! empty( $displayed_user_name ) && ! is_404() ) {
     685
    674686        // Get the component's ID to try and get its name
    675687        $component_id = $component_name = bp_current_component();
    676688
     689        // Set empty subnav name
     690        $component_subnav_name = '';
     691
    677692        // Use the component nav name
    678693        if ( ! empty( $bp->bp_nav[$component_id] ) ) {
    679             // Remove counts that are added by the nav item
    680             $span = strpos( $bp->bp_nav[ $component_id ]['name'], '<span' );
    681             if ( false !== $span ) {
    682                 $component_name = substr( $bp->bp_nav[ $component_id ]['name'], 0, $span - 1 );
    683 
    684             } else {
    685                 $component_name = $bp->bp_nav[ $component_id ]['name'];
    686             }
     694            $component_name = _bp_strip_spans_from_title( $bp->bp_nav[ $component_id ]['name'] );
    687695
    688696        // Fall back on the component ID
     
    695703            $component_subnav_name = wp_filter_object_list( $bp->bp_options_nav[ $component_id ], array( 'slug' => bp_current_action() ), 'and', 'name' );
    696704
    697             if ( $component_subnav_name ) {
     705            if ( ! empty( $component_subnav_name ) ) {
    698706                $component_subnav_name = array_shift( $component_subnav_name );
    699             } else {
    700                 $component_subnav_name = '';
    701707            }
    702 
    703         } else {
    704             $component_subnav_name = '';
    705708        }
    706709
    707710        // If on the user profile's landing page, just use the fullname
    708         if ( bp_is_current_component( $bp->default_component ) && bp_get_requested_url() === bp_displayed_user_domain() ) {
    709             $title = bp_get_displayed_user_fullname();
     711        if ( bp_is_current_component( $bp->default_component ) && ( bp_get_requested_url() === bp_displayed_user_domain() ) ) {
     712            $title_parts[] = $displayed_user_name;
    710713
    711714        // Use component name on member pages
    712715        } else {
     716            $title_parts = array_merge( $title_parts, array_map( 'strip_tags', array(
     717                $displayed_user_name,
     718                $component_name,
     719            ) ) );
     720
    713721            // If we have a subnav name, add it separately for localization
    714722            if ( ! empty( $component_subnav_name ) ) {
    715                 // translators: construct the page title. 1 = user name, 2 = component name, 3 = separator, 4 = component subnav name
    716                 $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 ) );
    717 
    718             } else {
    719                 // translators: construct the page title. 1 = user name, 2 = component name, 3 = separator
    720                 $title = strip_tags( sprintf( __( '%1$s %3$s %2$s', 'buddypress' ), bp_get_displayed_user_fullname(), $component_name, $sep ) );
     723                $title_parts[] = strip_tags( $component_subnav_name );
    721724            }
    722725        }
     
    724727    // A single group
    725728    } elseif ( bp_is_active( 'groups' ) && ! empty( $bp->groups->current_group ) && ! empty( $bp->bp_options_nav[ $bp->groups->current_group->slug ] ) ) {
    726         $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'] : '';
    727         // translators: 1 = group name, 2 = group nav section name, 3 = separator
    728         $title = sprintf( __( '%1$s %3$s %2$s', 'buddypress' ), $bp->bp_options_title, $subnav, $sep );
     729        $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'] : '';
     730        $title_parts = array( $bp->bp_options_title, $subnav );
    729731
    730732    // A single item from a component other than groups
    731733    } elseif ( bp_is_single_item() ) {
    732         // translators: 1 = component item name, 2 = component nav section name, 3 = separator
    733         $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 );
     734        $title_parts = array( $bp->bp_options_title, $bp->bp_options_nav[ bp_current_item() ][ bp_current_action() ]['name'] );
    734735
    735736    // An index or directory
     
    738739
    739740        // No current component (when does this happen?)
    740         if ( empty( $current_component ) ) {
    741             $title = _x( 'Directory', 'component directory title', 'buddypress' );
    742         } else {
    743             $title = bp_get_directory_title( $current_component );
    744         }
     741        $title_parts = array( _x( 'Directory', 'component directory title', 'buddypress' ) );
     742
     743        if ( ! empty( $current_component ) ) {
     744            $title_parts = array( bp_get_directory_title( $current_component ) );
     745        }
    745746
    746747    // Sign up page
    747748    } elseif ( bp_is_register_page() ) {
    748         $title = __( 'Create an Account', 'buddypress' );
     749        $title_parts = array( __( 'Create an Account', 'buddypress' ) );
    749750
    750751    // Activation page
    751752    } elseif ( bp_is_activation_page() ) {
    752         $title = __( 'Activate your Account', 'buddypress' );
     753        $title_parts = array( __( 'Activate your Account', 'buddypress' ) );
    753754
    754755    // Group creation page
    755756    } elseif ( bp_is_group_create() ) {
    756         $title = __( 'Create a Group', 'buddypress' );
     757        $title_parts = array( __( 'Create a Group', 'buddypress' ) );
    757758
    758759    // Blog creation page
    759760    } elseif ( bp_is_create_blog() ) {
    760         $title = __( 'Create a Site', 'buddypress' );
    761     }
    762 
    763     // Some BP nav items contain item counts. Remove them
    764     $title = preg_replace( '|<span>[0-9]+</span>|', '', $title );
    765 
    766     /**
    767      * Filters the page title for BuddyPress pages.
    768      *
    769      * @since BuddyPress (1.5.0)
    770      *
    771      * @param string $value       Determined title for the current BuddyPress page.
    772      * @param string $sep         Separator used for the BuddyPress page title.
    773      * @param string $seplocation Location to place the separator value.
    774      */
    775     return apply_filters( 'bp_modify_page_title', $title . ' ' . $sep . ' ', $title, $sep, $seplocation );
    776 }
    777 add_filter( 'wp_title', 'bp_modify_page_title', 10, 3 );
     761        $title_parts = array( __( 'Create a Site', 'buddypress' ) );
     762    }
     763
     764    // Strip spans
     765    $title_parts = array_map( '_bp_strip_spans_from_title', $title_parts );
     766
     767    // sep on right, so reverse the order
     768    if ( 'right' == $seplocation ) {
     769        $title_parts = array_reverse( $title_parts );
     770    }
     771
     772    // Get the blog name, so we can check if the original $title included it
     773    $blogname = get_bloginfo( 'name', 'display' );
     774
     775    /**
     776     * Are we going to fake 'title-tag' theme functionality?
     777     *
     778     * @link https://buddypress.trac.wordpress.org/ticket/6107
     779     * @see wp_title()
     780     */
     781    $title_tag_compatibility = (bool) ( ! empty( $_wp_theme_features['title-tag'] ) || strstr( $title, $blogname ) );
     782
     783    // Append the site title to title parts if theme supports title tag
     784    if ( true === $title_tag_compatibility ) {
     785        $title_parts[] = $blogname;
     786
     787        if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
     788            $title_parts[] = sprintf( __( 'Page %s', 'buddypress' ), max( $paged, $page ) );
     789        }
     790    }
     791
     792    // Pad the separator with 1 space on each side
     793    $prefix = str_pad( $sep, strlen( $sep ) + 2, ' ', STR_PAD_BOTH );
     794
     795    // Join the parts together
     796    $new_title = join( $prefix, array_filter( $title_parts ) );
     797
     798    // Append the prefix for pre `title-tag` compatibility
     799    if ( false === $title_tag_compatibility ) {
     800        $new_title = $new_title . $prefix;
     801    }
     802
     803    /**
     804     * @param  string the BuddyPress page title
     805     * @param  string $title the original WordPress page title
     806     * @param  string $sep the title parts separator
     807     * @param  string $seplocation Location of the separator (left or right).
     808     */
     809    return apply_filters( 'bp_modify_page_title', $new_title, $title, $sep, $seplocation );
     810}
     811add_filter( 'wp_title', 'bp_modify_page_title', 20, 3 );
    778812add_filter( 'bp_modify_page_title', 'wptexturize'     );
    779813add_filter( 'bp_modify_page_title', 'convert_chars'   );
    780814add_filter( 'bp_modify_page_title', 'esc_html'        );
     815
     816/**
     817 * Strip span tags out of title part strings.
     818 *
     819 * This is a temporary function for compatibility with WordPress versions
     820 * less than 4.0, and should be removed at a later date.
     821 *
     822 * @param  string $title_part
     823 * @return string
     824 */
     825function _bp_strip_spans_from_title( $title_part = '' ) {
     826    $title = $title_part;
     827    $span = strpos( $title, '<span' );
     828    if ( false !== $span ) {
     829        $title = substr( $title, 0, $span - 1 );
     830    }
     831    return $title;
     832}
    781833
    782834/**
Note: See TracChangeset for help on using the changeset viewer.