Skip to:
Content

BuddyPress.org

Ticket #6675: 6675.03.patch

File 6675.03.patch, 5.8 KB (added by r-a-y, 6 years ago)
  • src/bp-core/bp-core-filters.php

     
    649649 * @return string              New page title.
    650650 */
    651651function bp_modify_page_title( $title = '', $sep = '»', $seplocation = 'right' ) {
    652         global $bp, $paged, $page, $_wp_theme_features;
     652        global $bp;
    653653
    654654        // If this is not a BP page, just return the title produced by WP.
    655655        if ( bp_is_blog_page() ) {
     
    766766                $title_parts = array_reverse( $title_parts );
    767767        }
    768768
    769         // Get the blog name, so we can check if the original $title included it.
    770         $blogname = get_bloginfo( 'name', 'display' );
    771 
    772769        /**
    773          * Are we going to fake 'title-tag' theme functionality?
     770         * Filter BuddyPress title parts before joining.
     771         *
     772         * @since 2.4.1 Perhaps move this to 2.5?
    774773         *
    775          * @link https://buddypress.trac.wordpress.org/ticket/6107
    776          * @see wp_title()
     774         * @param  array $title_parts Current BuddyPress title parts
     775         * @return array
    777776         */
    778         $title_tag_compatibility = (bool) ( ! empty( $_wp_theme_features['title-tag'] ) || strstr( $title, $blogname ) );
     777        $title_parts = (array) apply_filters( 'bp_title_parts', $title_parts );
     778
     779        // 'wp_title'
     780        if ( ! is_array( $title ) ) {
     781                global $paged, $page, $_wp_theme_features;
    779782
    780         // Append the site title to title parts if theme supports title tag.
    781         if ( true === $title_tag_compatibility ) {
    782                 $title_parts[] = $blogname;
     783                // Get the blog name, so we can check if the original $title included it.
     784                $blogname = get_bloginfo( 'name', 'display' );
    783785
    784                 if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
    785                         $title_parts[] = sprintf( __( 'Page %s', 'buddypress' ), max( $paged, $page ) );
     786                /**
     787                 * Are we going to fake 'title-tag' theme functionality?
     788                 *
     789                 * @link https://buddypress.trac.wordpress.org/ticket/6107
     790                 * @see wp_title()
     791                 */
     792                $title_tag_compatibility = (bool) ( ! empty( $_wp_theme_features['title-tag'] ) || strstr( $title, $blogname ) );
     793
     794                // Append the site title to title parts if theme supports title tag
     795                if ( true === $title_tag_compatibility ) {
     796                        $title_parts['site'] = $blogname;
     797
     798                        if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() && ! bp_is_single_activity() ) {
     799                                $title_parts['page'] = sprintf( __( 'Page %s', 'buddypress' ), max( $paged, $page ) );
     800                        }
    786801                }
    787         }
    788802
    789         // Pad the separator with 1 space on each side.
    790         $prefix = str_pad( $sep, strlen( $sep ) + 2, ' ', STR_PAD_BOTH );
     803                // Pad the separator with 1 space on each side
     804                $prefix = str_pad( $sep, strlen( $sep ) + 2, ' ', STR_PAD_BOTH );
     805
     806                // Join the parts together
     807                $new_title = join( $prefix, array_filter( $title_parts ) );
     808
     809                // Append the prefix for pre `title-tag` compatibility
     810                if ( false === $title_tag_compatibility ) {
     811                        $new_title = $new_title . $prefix;
     812                }
     813
     814                /**
     815                 * Filters the older 'wp_title' page title for BuddyPress pages.
     816                 *
     817                 * @since  1.5.0
     818                 *
     819                 * @param  string $new_title   The BuddyPress page title.
     820                 * @param  string $title       The original WordPress page title.
     821                 * @param  string $sep         The title parts separator.
     822                 * @param  string $seplocation Location of the separator (left or right).
     823                 */
     824                return apply_filters( 'bp_modify_page_title', $new_title, $title, $sep, $seplocation );
     825
     826        // 'document_title_parts' - WP 4.4+
     827        } else {
     828                // Get the separator used by wp_get_document_title()
     829                $sep = apply_filters( 'document_title_separator', '-' );
     830
     831                // Build the BuddyPress portion of the title.
     832                // We don't need to sanitize this as WordPress will take care of it.
     833                $bp_title = array(
     834                        // @todo Perhaps re-apply the 'bp_modify_page_title' filter? The problem with that
     835                        //       is the sitename is included in the older filter; it isn't included here...
     836                        'title' => join( " $sep ", $title_parts )
     837                );
     838
     839                // Add the pagination number if needed (not sure if this is necessary).
     840                if ( isset( $title['page'] ) && ! bp_is_single_activity() ) {
     841                        $bp_title['page'] = $title['page'];
     842                }
    791843
    792         // Join the parts together.
    793         $new_title = join( $prefix, array_filter( $title_parts ) );
     844                // Add the sitename if needed.
     845                if ( isset( $title['site'] ) ) {
     846                        $bp_title['site'] = $title['site'];
     847                }
    794848
    795         // Append the prefix for pre `title-tag` compatibility.
    796         if ( false === $title_tag_compatibility ) {
    797                 $new_title = $new_title . $prefix;
     849                return $bp_title;
    798850        }
     851}
    799852
    800         /**
    801          * Filters the page title for BuddyPress pages.
    802          *
    803          * @since  1.5.0
    804          *
    805          * @param  string $new_title   The BuddyPress page title.
    806          * @param  string $title       The original WordPress page title.
    807          * @param  string $sep         The title parts separator.
    808          * @param  string $seplocation Location of the separator (left or right).
    809          */
    810         return apply_filters( 'bp_modify_page_title', $new_title, $title, $sep, $seplocation );
     853/**
     854 * Set up the document title after theme set up.
     855 *
     856 * We do logic here to determine if we should use the newer title-tag or the
     857 * older 'wp_title'.
     858 *
     859 * @since 2.4.1
     860 */
     861function bp_setup_document_title() {
     862        // Use new WP 4.4 page title filter
     863        if ( function_exists( 'wp_get_document_title' ) && true === current_theme_supports( 'title-tag' ) ) {
     864                add_filter( 'document_title_parts', 'bp_modify_page_title', 20, 1 );
     865
     866        // Older 'wp_title' page title filter
     867        } else {
     868                add_filter( 'bp_modify_page_title', 'wptexturize'   );
     869                add_filter( 'bp_modify_page_title', 'convert_chars' );
     870                add_filter( 'bp_modify_page_title', 'esc_html'      );
     871
     872                add_filter( 'wp_title', 'bp_modify_page_title', 20, 3 );
     873        }
    811874}
    812 add_filter( 'wp_title', 'bp_modify_page_title', 20, 3 );
    813 add_filter( 'bp_modify_page_title', 'wptexturize'     );
    814 add_filter( 'bp_modify_page_title', 'convert_chars'   );
    815 add_filter( 'bp_modify_page_title', 'esc_html'        );
     875add_action( 'bp_after_setup_theme', 'bp_setup_document_title' );
    816876
    817877/**
    818878 * Strip span tags out of title part strings.