Skip to:
Content

BuddyPress.org

Changeset 6537


Ignore:
Timestamp:
11/29/2012 09:15:07 PM (12 years ago)
Author:
johnjamesjacoby
Message:

Theme Compat:

  • Literal merge of template stack code from bbPress 2.2.
  • See #4656.
  • Does not include additional bp_template_stack_add_custom_locations() function in attached patch. This needs more testing.
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-core/bp-core-template-loader.php

    r6490 r6537  
    6262
    6363    // 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 );
     64    $located            = false;
     65    $template_locations = bp_get_template_stack();
    7266
    7367    // Try to find a template file
    74     // Check the parent and child theme directories first
    7568    foreach ( (array) $template_names as $template_name ) {
    7669
     
    8073
    8174        // 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 ) ) {
    98         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;
     75        $template_name  = ltrim( $template_name, '/' );
     76
     77        // Loop through template stack
     78        foreach ( (array) $template_locations as $template_location ) {
     79
     80            // Continue if $template_location is empty
     81            if ( empty( $template_location ) )
     82                continue;
     83
     84            // Check child theme first
     85            if ( file_exists( trailingslashit( $template_location ) . $template_name ) ) {
     86                $located = trailingslashit( $template_location ) . $template_name;
     87                break 2;
    10288            }
    10389        }
    10490    }
    10591
     92    // Maybe load the template if one was located
    10693    if ( ( true == $load ) && !empty( $located ) )
    10794        load_template( $located, $require_once );
    10895
    10996    return $located;
     97}
     98
     99/**
     100 * This is really cool. This function registers a new template stack location,
     101 * using WordPress's built in filters API.
     102 *
     103 * This allows for templates to live in places beyond just the parent/child
     104 * relationship, to allow for custom template locations. Used in conjunction
     105 * with bp_locate_template(), this allows for easy template overrides.
     106 *
     107 * @since BuddyPress (1.7)
     108 *
     109 * @param string $location Callback function that returns the
     110 * @param int $priority
     111 */
     112function bp_register_template_stack( $location_callback = '', $priority = 10 ) {
     113
     114    // Bail if no location, or function does not exist
     115    if ( empty( $location_callback ) || ! function_exists( $location_callback ) )
     116        return false;
     117
     118    // Add location callback to template stack
     119    add_filter( 'bp_template_stack', $location_callback, (int) $priority );
     120}
     121
     122/**
     123 * Call the functions added to the 'bp_template_stack' filter hook, and return
     124 * an array of the template locations.
     125 *
     126 * @see bp_register_template_stack()
     127 *
     128 * @since BuddyPress (1.7)
     129 *
     130 * @global array $wp_filter Stores all of the filters
     131 * @global array $merged_filters Merges the filter hooks using this function.
     132 * @global array $wp_current_filter stores the list of current filters with the current one last
     133 *
     134 * @return array The filtered value after all hooked functions are applied to it.
     135 */
     136function bp_get_template_stack() {
     137    global $wp_filter, $merged_filters, $wp_current_filter;
     138
     139    // Setup some default variables
     140    $tag  = 'bp_template_stack';
     141    $args = $stack = array();
     142
     143    // Add 'bp_template_stack' to the current filter array
     144    $wp_current_filter[] = $tag;
     145
     146    // Sort
     147    if ( ! isset( $merged_filters[ $tag ] ) ) {
     148        ksort( $wp_filter[$tag] );
     149        $merged_filters[ $tag ] = true;
     150    }
     151
     152    // Ensure we're always at the beginning of the filter array
     153    reset( $wp_filter[ $tag ] );
     154
     155    // Loop through 'bp_template_stack' filters, and call callback functions
     156    do {
     157        foreach( (array) current( $wp_filter[$tag] ) as $the_ ) {
     158            if ( ! is_null( $the_['function'] ) ) {
     159                $args[1] = $stack;
     160                $stack[] = call_user_func_array( $the_['function'], array_slice( $args, 1, (int) $the_['accepted_args'] ) );
     161            }
     162        }
     163    } while ( next( $wp_filter[$tag] ) !== false );
     164
     165    // Remove 'bp_template_stack' from the current filter array
     166    array_pop( $wp_current_filter );
     167
     168    // Remove empties and duplicates
     169    $stack = array_unique( array_filter( $stack ) );
     170
     171    return (array) apply_filters( 'bp_get_template_stack', $stack ) ;
    110172}
    111173
  • trunk/bp-loader.php

    r6509 r6537  
    539539     */
    540540    public function register_theme_packages() {
     541
     542        // Register the default theme compatibility package
    541543        bp_register_theme_package( array(
    542544            'id'      => 'legacy',
     
    546548            'url'     => trailingslashit( $this->themes_url . '/bp-legacy' )
    547549        ) );
     550
     551        // Register the basic theme stack. This is really dope.
     552        bp_register_template_stack( 'get_stylesheet_directory', 10 );
     553        bp_register_template_stack( 'get_template_directory',   12 );
     554        bp_register_template_stack( 'bp_get_theme_compat_dir',  14 );
    548555    }
    549556
Note: See TracChangeset for help on using the changeset viewer.