Skip to:
Content

BuddyPress.org

Changeset 10403


Ignore:
Timestamp:
12/13/2015 04:54:45 PM (9 years ago)
Author:
imath
Message:

Make sure document/page title tags are correctly generated for BuddyPress pages

  • Remove all the BuddyPress process to create BuddyPress title parts from bp_modify_page_title() and create the new function bp_get_title_parts() and put this process here. This allowes plugin/theme to directly use it if needed.
  • Keep the filter on wp_title so that themes not supporting the title-tag feature or breadcrumb plugins can still easily get BuddyPress title parts.
  • Filter document_title_parts with a new function bp_modify_document_title_parts() to build the BuddyPress title parts if needed.
  • Both functions bp_modify_page_title() and bp_modify_document_title_parts() are using bp_get_title_parts().

Props slaFFik, mercime, r-a-y, DJPaul

Fixes #6675 (trunk)

Location:
trunk/src/bp-core
Files:
3 edited

Legend:

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

    r10356 r10403  
    650650 */
    651651function bp_modify_page_title( $title = '', $sep = '»', $seplocation = 'right' ) {
    652     global $bp, $paged, $page, $_wp_theme_features;
    653 
    654     // If this is not a BP page, just return the title produced by WP.
    655     if ( bp_is_blog_page() ) {
     652    global $paged, $page, $_wp_theme_features;
     653
     654    // Get the BuddyPress title parts
     655    $bp_title_parts = bp_get_title_parts( $seplocation );
     656
     657    // If not set, simply return the original title
     658    if ( ! $bp_title_parts ) {
    656659        return $title;
    657     }
    658 
    659     // If this is a 404, let WordPress handle it.
    660     if ( is_404() ) {
    661         return $title;
    662     }
    663 
    664     // If this is the front page of the site, return WP's title.
    665     if ( is_front_page() || is_home() ) {
    666         return $title;
    667     }
    668 
    669     // Return WP's title if not a BuddyPress page.
    670     if ( ! is_buddypress() ) {
    671         return $title;
    672     }
    673 
    674     // Setup an empty title parts array.
    675     $title_parts = array();
    676 
    677     // Is there a displayed user, and do they have a name?
    678     $displayed_user_name = bp_get_displayed_user_fullname();
    679 
    680     // Displayed user.
    681     if ( ! empty( $displayed_user_name ) && ! is_404() ) {
    682 
    683         // Get the component's ID to try and get its name.
    684         $component_id = $component_name = bp_current_component();
    685 
    686         // Set empty subnav name.
    687         $component_subnav_name = '';
    688 
    689         // Use the component nav name.
    690         if ( ! empty( $bp->bp_nav[$component_id] ) ) {
    691             $component_name = _bp_strip_spans_from_title( $bp->bp_nav[ $component_id ]['name'] );
    692 
    693         // Fall back on the component ID.
    694         } elseif ( ! empty( $bp->{$component_id}->id ) ) {
    695             $component_name = ucwords( $bp->{$component_id}->id );
    696         }
    697 
    698         // Append action name if we're on a member component sub-page.
    699         if ( ! empty( $bp->bp_options_nav[ $component_id ] ) && ! empty( $bp->canonical_stack['action'] ) ) {
    700             $component_subnav_name = wp_filter_object_list( $bp->bp_options_nav[ $component_id ], array( 'slug' => bp_current_action() ), 'and', 'name' );
    701 
    702             if ( ! empty( $component_subnav_name ) ) {
    703                 $component_subnav_name = array_shift( $component_subnav_name );
    704             }
    705         }
    706 
    707         // 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_parts[] = $displayed_user_name;
    710 
    711         // Use component name on member pages.
    712         } else {
    713             $title_parts = array_merge( $title_parts, array_map( 'strip_tags', array(
    714                 $displayed_user_name,
    715                 $component_name,
    716             ) ) );
    717 
    718             // If we have a subnav name, add it separately for localization.
    719             if ( ! empty( $component_subnav_name ) ) {
    720                 $title_parts[] = strip_tags( $component_subnav_name );
    721             }
    722         }
    723 
    724     // A single group.
    725     } 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         $title_parts = array( $bp->bp_options_title, $subnav );
    728 
    729     // A single item from a component other than groups.
    730     } elseif ( bp_is_single_item() ) {
    731         $title_parts = array( $bp->bp_options_title, $bp->bp_options_nav[ bp_current_item() ][ bp_current_action() ]['name'] );
    732 
    733     // An index or directory.
    734     } elseif ( bp_is_directory() ) {
    735         $current_component = bp_current_component();
    736 
    737         // No current component (when does this happen?).
    738         $title_parts = array( _x( 'Directory', 'component directory title', 'buddypress' ) );
    739 
    740         if ( ! empty( $current_component ) ) {
    741             $title_parts = array( bp_get_directory_title( $current_component ) );
    742         }
    743 
    744     // Sign up page.
    745     } elseif ( bp_is_register_page() ) {
    746         $title_parts = array( __( 'Create an Account', 'buddypress' ) );
    747 
    748     // Activation page.
    749     } elseif ( bp_is_activation_page() ) {
    750         $title_parts = array( __( 'Activate Your Account', 'buddypress' ) );
    751 
    752     // Group creation page.
    753     } elseif ( bp_is_group_create() ) {
    754         $title_parts = array( __( 'Create a Group', 'buddypress' ) );
    755 
    756     // Blog creation page.
    757     } elseif ( bp_is_create_blog() ) {
    758         $title_parts = array( __( 'Create a Site', 'buddypress' ) );
    759     }
    760 
    761     // Strip spans.
    762     $title_parts = array_map( '_bp_strip_spans_from_title', $title_parts );
    763 
    764     // Sep on right, so reverse the order.
    765     if ( 'right' == $seplocation ) {
    766         $title_parts = array_reverse( $title_parts );
    767660    }
    768661
     
    778671    $title_tag_compatibility = (bool) ( ! empty( $_wp_theme_features['title-tag'] ) || strstr( $title, $blogname ) );
    779672
    780     // Append the site title to title parts if theme supports title tag.
     673    // Append the site title to title parts if theme supports title tag
    781674    if ( true === $title_tag_compatibility ) {
    782         $title_parts[] = $blogname;
    783 
    784         if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() ) {
    785             $title_parts[] = sprintf( __( 'Page %s', 'buddypress' ), max( $paged, $page ) );
     675        $bp_title_parts['site'] = $blogname;
     676
     677        if ( ( $paged >= 2 || $page >= 2 ) && ! is_404() && ! bp_is_single_activity() ) {
     678            $bp_title_parts['page'] = sprintf( __( 'Page %s', 'buddypress' ), max( $paged, $page ) );
    786679        }
    787680    }
    788681
    789     // Pad the separator with 1 space on each side.
     682    // Pad the separator with 1 space on each side
    790683    $prefix = str_pad( $sep, strlen( $sep ) + 2, ' ', STR_PAD_BOTH );
    791684
    792     // Join the parts together.
    793     $new_title = join( $prefix, array_filter( $title_parts ) );
    794 
    795     // Append the prefix for pre `title-tag` compatibility.
     685    // Join the parts together
     686    $new_title = join( $prefix, array_filter( $bp_title_parts ) );
     687
     688    // Append the prefix for pre `title-tag` compatibility
    796689    if ( false === $title_tag_compatibility ) {
    797690        $new_title = $new_title . $prefix;
     
    799692
    800693    /**
    801      * Filters the page title for BuddyPress pages.
     694     * Filters the older 'wp_title' page title for BuddyPress pages.
    802695     *
    803696     * @since  1.5.0
     
    810703    return apply_filters( 'bp_modify_page_title', $new_title, $title, $sep, $seplocation );
    811704}
    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'        );
    816 
    817 /**
    818  * Strip span tags out of title part strings.
    819  *
    820  * This is a temporary function for compatibility with WordPress versions
    821  * less than 4.0, and should be removed at a later date.
    822  *
    823  * @param string $title_part Title part to clean up.
    824  * @return string
    825  */
    826 function _bp_strip_spans_from_title( $title_part = '' ) {
    827     $title = $title_part;
    828     $span = strpos( $title, '<span' );
    829     if ( false !== $span ) {
    830         $title = substr( $title, 0, $span - 1 );
    831     }
    832     return $title;
    833 }
     705add_filter( 'wp_title',             'bp_modify_page_title', 20, 3 );
     706add_filter( 'bp_modify_page_title', 'wptexturize'                 );
     707add_filter( 'bp_modify_page_title', 'convert_chars'               );
     708add_filter( 'bp_modify_page_title', 'esc_html'                    );
     709
     710/**
     711 * Filter the document title for BuddyPress pages.
     712 *
     713 * @since 2.4.3
     714 *
     715 * @param array $title The WordPress document title parts
     716 * @return array the unchanged title parts or the BuddyPress ones
     717 */
     718function bp_modify_document_title_parts( $title = array() ) {
     719    // Get the BuddyPress title parts
     720    $bp_title_parts = bp_get_title_parts();
     721
     722    // If not set, simply return the original title
     723    if ( ! $bp_title_parts ) {
     724        return $title;
     725    }
     726
     727    // Get the separator used by wp_get_document_title()
     728    $sep = apply_filters( 'document_title_separator', '-' );
     729
     730    // Build the BuddyPress portion of the title.
     731    // We don't need to sanitize this as WordPress will take care of it.
     732    $bp_title = array(
     733        'title' => join( " $sep ", $bp_title_parts )
     734    );
     735
     736    // Add the pagination number if needed (not sure if this is necessary).
     737    if ( isset( $title['page'] ) && ! bp_is_single_activity() ) {
     738        $bp_title['page'] = $title['page'];
     739    }
     740
     741    // Add the sitename if needed.
     742    if ( isset( $title['site'] ) ) {
     743        $bp_title['site'] = $title['site'];
     744    }
     745
     746    /**
     747     * Filters BuddyPress title parts that will be used into the document title.
     748     *
     749     * @since  2.4.3
     750     *
     751     * @param  array $bp_title   The BuddyPress page title parts.
     752     * @param  array $title      The original WordPress title parts.
     753     */
     754    return apply_filters( 'bp_modify_document_title_parts', $bp_title, $title );
     755}
     756add_filter( 'document_title_parts', 'bp_modify_document_title_parts', 20, 1 );
    834757
    835758/**
  • trunk/src/bp-core/bp-core-functions.php

    r10365 r10403  
    22832283add_action( 'bp_init', 'bp_remove_adjacent_posts_rel_link' );
    22842284
     2285/**
     2286 * Strip the span count of a menu item or of a title part.
     2287 *
     2288 * @since 2.2.2
     2289 *
     2290 * @param string $title_part Title part to clean up.
     2291 * @return string
     2292 */
     2293function _bp_strip_spans_from_title( $title_part = '' ) {
     2294    $title = $title_part;
     2295    $span = strpos( $title, '<span' );
     2296    if ( false !== $span ) {
     2297        $title = substr( $title, 0, $span - 1 );
     2298    }
     2299    return trim( $title );
     2300}
     2301
    22852302/** Nav Menu ******************************************************************/
    22862303
     
    23342351
    23352352        // Remove <span>number</span>.
    2336         $item_name = preg_replace( '/([.0-9]+)/', '', $bp_item['name'] );
    2337         $item_name = trim( strip_tags( $item_name ) );
     2353        $item_name = _bp_strip_spans_from_title( $bp_item['name'] );
    23382354
    23392355        $page_args[ $bp_item['slug'] ] = (object) array(
  • trunk/src/bp-core/bp-core-template.php

    r10356 r10403  
    27802780function bp_is_register_page() {
    27812781    return (bool) bp_is_current_component( 'register' );
     2782}
     2783
     2784/**
     2785 * Get the title parts of the BuddyPress displayed page
     2786 *
     2787 * @since 2.4.3
     2788 *
     2789 * @param string $seplocation
     2790 * @return array the title parts
     2791 */
     2792function bp_get_title_parts( $seplocation = 'right' ) {
     2793    $bp = buddypress();
     2794
     2795    // Defaults to an empty array
     2796    $bp_title_parts = array();
     2797
     2798    // If this is not a BP page, return the empty array.
     2799    if ( bp_is_blog_page() ) {
     2800        return $bp_title_parts;
     2801    }
     2802
     2803    // If this is a 404, return the empty array.
     2804    if ( is_404() ) {
     2805        return $bp_title_parts;
     2806    }
     2807
     2808    // If this is the front page of the site, return the empty array.
     2809    if ( is_front_page() || is_home() ) {
     2810        return $bp_title_parts;
     2811    }
     2812
     2813    // Return the empty array if not a BuddyPress page.
     2814    if ( ! is_buddypress() ) {
     2815        return $bp_title_parts;
     2816    }
     2817
     2818    // Now we can build the BP Title Parts
     2819    // Is there a displayed user, and do they have a name?
     2820    $displayed_user_name = bp_get_displayed_user_fullname();
     2821
     2822    // Displayed user.
     2823    if ( ! empty( $displayed_user_name ) && ! is_404() ) {
     2824
     2825        // Get the component's ID to try and get its name.
     2826        $component_id = $component_name = bp_current_component();
     2827
     2828        // Set empty subnav name.
     2829        $component_subnav_name = '';
     2830
     2831        // Use the component nav name.
     2832        if ( ! empty( $bp->bp_nav[$component_id] ) ) {
     2833            $component_name = _bp_strip_spans_from_title( $bp->bp_nav[ $component_id ]['name'] );
     2834
     2835        // Fall back on the component ID.
     2836        } elseif ( ! empty( $bp->{$component_id}->id ) ) {
     2837            $component_name = ucwords( $bp->{$component_id}->id );
     2838        }
     2839
     2840        // Append action name if we're on a member component sub-page.
     2841        if ( ! empty( $bp->bp_options_nav[ $component_id ] ) && ! empty( $bp->canonical_stack['action'] ) ) {
     2842            $component_subnav_name = wp_filter_object_list( $bp->bp_options_nav[ $component_id ], array( 'slug' => bp_current_action() ), 'and', 'name' );
     2843
     2844            if ( ! empty( $component_subnav_name ) ) {
     2845                $component_subnav_name = array_shift( $component_subnav_name );
     2846            }
     2847        }
     2848
     2849        // If on the user profile's landing page, just use the fullname.
     2850        if ( bp_is_current_component( $bp->default_component ) && ( bp_get_requested_url() === bp_displayed_user_domain() ) ) {
     2851            $bp_title_parts[] = $displayed_user_name;
     2852
     2853        // Use component name on member pages.
     2854        } else {
     2855            $bp_title_parts = array_merge( $bp_title_parts, array_map( 'strip_tags', array(
     2856                $displayed_user_name,
     2857                $component_name,
     2858            ) ) );
     2859
     2860            // If we have a subnav name, add it separately for localization.
     2861            if ( ! empty( $component_subnav_name ) ) {
     2862                $bp_title_parts[] = strip_tags( $component_subnav_name );
     2863            }
     2864        }
     2865
     2866    // A single group.
     2867    } elseif ( bp_is_active( 'groups' ) && ! empty( $bp->groups->current_group ) && ! empty( $bp->bp_options_nav[ $bp->groups->current_group->slug ] ) ) {
     2868        $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'] : '';
     2869        $bp_title_parts = array( $bp->bp_options_title, $subnav );
     2870
     2871    // A single item from a component other than groups.
     2872    } elseif ( bp_is_single_item() ) {
     2873        $bp_title_parts = array( $bp->bp_options_title, $bp->bp_options_nav[ bp_current_item() ][ bp_current_action() ]['name'] );
     2874
     2875    // An index or directory.
     2876    } elseif ( bp_is_directory() ) {
     2877        $current_component = bp_current_component();
     2878
     2879        // No current component (when does this happen?).
     2880        $bp_title_parts = array( _x( 'Directory', 'component directory title', 'buddypress' ) );
     2881
     2882        if ( ! empty( $current_component ) ) {
     2883            $bp_title_parts = array( bp_get_directory_title( $current_component ) );
     2884        }
     2885
     2886    // Sign up page.
     2887    } elseif ( bp_is_register_page() ) {
     2888        $bp_title_parts = array( __( 'Create an Account', 'buddypress' ) );
     2889
     2890    // Activation page.
     2891    } elseif ( bp_is_activation_page() ) {
     2892        $bp_title_parts = array( __( 'Activate Your Account', 'buddypress' ) );
     2893
     2894    // Group creation page.
     2895    } elseif ( bp_is_group_create() ) {
     2896        $bp_title_parts = array( __( 'Create a Group', 'buddypress' ) );
     2897
     2898    // Blog creation page.
     2899    } elseif ( bp_is_create_blog() ) {
     2900        $bp_title_parts = array( __( 'Create a Site', 'buddypress' ) );
     2901    }
     2902
     2903    // Strip spans.
     2904    $bp_title_parts = array_map( '_bp_strip_spans_from_title', $bp_title_parts );
     2905
     2906    // Sep on right, so reverse the order.
     2907    if ( 'right' === $seplocation ) {
     2908        $bp_title_parts = array_reverse( $bp_title_parts );
     2909    }
     2910
     2911    /**
     2912     * Filter BuddyPress title parts before joining.
     2913     *
     2914     * @since 2.4.3
     2915     *
     2916     * @param  array $bp_title_parts Current BuddyPress title parts
     2917     * @return array
     2918     */
     2919    return (array) apply_filters( 'bp_get_title_parts', $bp_title_parts );
    27822920}
    27832921
Note: See TracChangeset for help on using the changeset viewer.