Skip to:
Content

BuddyPress.org

Ticket #4656: 4656-port.01.patch

File 4656-port.01.patch, 7.0 KB (added by r-a-y, 12 years ago)

First patch - bbPress port of template stack functionality

  • bp-core/bp-core-filters.php

    add_filter( 'bp_template_include', 'bp_template_include_theme_supports', 2, 1 ); 
    6262add_filter( 'bp_template_include', 'bp_template_include_theme_compat',   4, 2 );
    6363
    6464// Run all template parts through additional template locations
    65 add_filter( 'bp_locate_template',   'bp_add_template_locations' );
    6665add_filter( 'bp_get_template_part', 'bp_add_template_locations' );
    6766
    6867// Turn comments off for BuddyPress pages
  • bp-core/bp-core-template-loader.php

    function bp_get_template_part( $slug, $name = null ) { 
    3232
    3333        // Setup possible parts
    3434        $templates = array();
     35
    3536        if ( isset( $name ) )
    3637                $templates[] = $slug . '-' . $name . '.php';
     38
    3739        $templates[] = $slug . '.php';
    3840
    3941        // Allow template parst to be filtered
    function bp_get_template_part( $slug, $name = null ) { 
    6163function bp_locate_template( $template_names, $load = false, $require_once = true ) {
    6264
    6365        // No file found yet
    64         $located        = false;
    65         $child_theme    = get_stylesheet_directory();
    66         $parent_theme   = get_template_directory();
    67         $fallback_theme = bp_get_theme_compat_dir();
    68 
    69         // Allow templates to be filtered
    70         // BuddyPress core automatically adds bp_add_template_locations()
    71         $template_names = apply_filters( 'bp_locate_template', $template_names );
     66        $located            = false;
     67        $template_locations = bp_get_template_stack();
    7268
    73         // Try to find a template file
    74         // Check the parent and child theme directories first
    75         foreach ( (array) $template_names as $template_name ) {
     69        // Loop through template stack
     70        foreach ( (array) $template_locations as $template_location ) {
    7671
    77                 // Continue if template is empty
    78                 if ( empty( $template_name ) )
     72                // Continue if $template_location is empty
     73                if ( empty( $template_location ) )
    7974                        continue;
    8075
    81                 // Trim off any slashes from the template name
    82                 $template_name = ltrim( $template_name, '/' );
    83 
    84                 // Check child theme first
    85                 if ( file_exists( trailingslashit( $child_theme ) . $template_name ) ) {
    86                         $located = trailingslashit( $child_theme ) . $template_name;
    87                         break;
    88 
    89                 // Check parent theme next
    90                 } elseif ( file_exists( trailingslashit( $parent_theme ) . $template_name ) ) {
    91                         $located = trailingslashit( $parent_theme ) . $template_name;
    92                         break;
    93                 }
    94         }
    95 
    96         // Check theme compatibility last if no template is found in the current theme
    97         if ( empty( $located ) ) {
     76                // Try to find a template file
    9877                foreach ( (array) $template_names as $template_name ) {
    99                         if ( file_exists( trailingslashit( $fallback_theme ) . $template_name ) ) {
    100                                 $located = trailingslashit( $fallback_theme ) . $template_name;
    101                                 break;
     78       
     79                        // Continue if template is empty
     80                        if ( empty( $template_name ) )
     81                                continue;
     82       
     83                        // Trim off any slashes from the template name
     84                        $template_name  = ltrim( $template_name, '/' );
     85
     86                        // Check child theme first
     87                        if ( file_exists( trailingslashit( $template_location ) . $template_name ) ) {
     88                                $located = trailingslashit( $template_location ) . $template_name;
     89                                break 2;
    10290                        }
    10391                }
    10492        }
    10593
     94        // Maybe load the template if one was located
    10695        if ( ( true == $load ) && !empty( $located ) )
    10796                load_template( $located, $require_once );
    10897
    function bp_buffer_template_part( $slug, $name = null, $echo = true ) { 
    144133}
    145134
    146135/**
     136 * This is really cool. This function registers a new template stack location,
     137 * using WordPress's built in filters API.
     138 *
     139 * This allows for templates to live in places beyond just the parent/child
     140 * relationship, to allow for custom template locations. Used in conjunction
     141 * with bp_locate_template(), this allows for easy template overrides.
     142 *
     143 * @since BuddyPress (1.7)
     144 *
     145 * @param string $location Callback function that returns the
     146 * @param int $priority
     147 */
     148function bp_register_template_stack( $location_callback = '', $priority = 10 ) {
     149
     150        // Bail if no location, or function does not exist
     151        if ( empty( $location_callback ) || ! function_exists( $location_callback ) )
     152                return false;
     153
     154        // Add location callback to template stack
     155        add_filter( 'bp_template_stack', $location_callback, (int) $priority );
     156}
     157
     158/**
     159 * Call the functions added to the 'bp_template_stack' filter hook, and return
     160 * an array of the template locations.
     161 *
     162 * @see bp_register_template_stack()
     163 *
     164 * @since BuddyPress (1.7)
     165 *
     166 * @global array $wp_filter Stores all of the filters
     167 * @global array $merged_filters Merges the filter hooks using this function.
     168 * @global array $wp_current_filter stores the list of current filters with the current one last
     169 *
     170 * @return array The filtered value after all hooked functions are applied to it.
     171 */
     172function bp_get_template_stack() {
     173        global $wp_filter, $merged_filters, $wp_current_filter;
     174
     175        // Setup some default variables
     176        $tag  = 'bp_template_stack';
     177        $args = $stack = array();
     178
     179        // Add 'bp_template_stack' to the current filter array
     180        $wp_current_filter[] = $tag;
     181
     182        // Sort
     183        if ( ! isset( $merged_filters[ $tag ] ) ) {
     184                ksort( $wp_filter[$tag] );
     185                $merged_filters[ $tag ] = true;
     186        }
     187
     188        // Ensure we're always at the beginning of the filter array
     189        reset( $wp_filter[ $tag ] );
     190
     191        // Loop through 'bp_template_stack' filters, and call callback functions
     192        while ( next( $wp_filter[$tag] ) !== false ) {
     193                foreach( (array) current( $wp_filter[$tag] ) as $the_ ) {
     194                        if ( ! is_null( $the_['function'] ) ) {
     195                                $args[1] = $stack;
     196                                $stack[] = call_user_func_array( $the_['function'], array_slice( $args, 1, (int) $the_['accepted_args'] ) );
     197                        }
     198                }
     199        };
     200
     201        // Remove 'bp_template_stack' from the current filter array
     202        array_pop( $wp_current_filter );
     203
     204        // Remove empties and duplicates
     205        $stack = array_unique( array_filter( $stack ) );
     206
     207        return (array) apply_filters( 'bp_get_template_stack', $stack ) ;
     208}
     209
     210/**
    147211 * Retrieve path to a template
    148212 *
    149213 * Used to quickly retrieve the path of a template without including the file
  • bp-loader.php

    class BuddyPress { 
    527527         * @since BuddyPress (1.7)
    528528         */
    529529        public function register_theme_packages() {
     530                // Register the default theme compatibility package
    530531                bp_register_theme_package( array(
    531532                        'id'      => 'legacy',
    532533                        'name'    => __( 'BuddyPress Default', 'buddypress' ),
    class BuddyPress { 
    534535                        'dir'     => trailingslashit( $this->themes_dir . '/bp-legacy' ),
    535536                        'url'     => trailingslashit( $this->themes_url . '/bp-legacy' )
    536537                ) );
     538
     539                // Register the basic theme stack. This is really dope.
     540                bp_register_template_stack( 'get_stylesheet_directory', 10 );
     541                bp_register_template_stack( 'get_template_directory',   12 );
     542                bp_register_template_stack( 'bp_get_theme_compat_dir',  14 );
    537543        }
    538544
    539545        /**