Skip to:
Content

bbPress.org

Changeset 4324


Ignore:
Timestamp:
11/03/2012 09:20:34 PM (9 years ago)
Author:
johnjamesjacoby
Message:

Theme Compat:

  • Introduce bbp_get_template_stack() and bbp_register_template_stack().
  • Used to provide an accurate hierarchy to individual template locations.
  • Also enables an unlimited number of parent/child template relationships to be registered.
  • See #1968.
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/bbpress.php

    r4321 r4324  
    385385     */
    386386    public function register_theme_packages() {
     387
     388        // Register the default theme compatibility package
    387389        bbp_register_theme_package( array(
    388390            'id'      => 'default',
     
    392394            'url'     => trailingslashit( $this->themes_url . 'bbp-default' )
    393395        ) );
     396
     397        // Register the basic theme stack. This is really dope.
     398        bbp_register_template_stack( 'get_stylesheet_directory', 10 );
     399        bbp_register_template_stack( 'get_template_directory',   12 );
     400        bbp_register_template_stack( 'bbp_get_theme_compat_dir', 14 );
    394401    }
    395402
     
    803810
    804811    /**
    805      * Register the bbPress capabilities
    806      *
    807      * @since bbPress (r3031)
    808      *
    809      * @uses BBP_Capabilities
    810      */
    811     public function register_capabilities() {
    812         $this->capabilities = new BBP_Capabilities();
    813     }
    814 
    815     /**
    816812     * Setup the currently logged-in user
    817813     *
  • trunk/includes/core/template-functions.php

    r4321 r4324  
    6262
    6363    // No file found yet
    64     $located        = false;
    65     $child_theme    = get_stylesheet_directory();
    66     $parent_theme   = get_template_directory();
    67     $fallback_theme = bbp_get_theme_compat_dir();
    68 
    69     // Allow templates to be filtered
    70     // Note: if you do this, be excellent to each other
    71     $template_names = apply_filters( 'bbp_locate_template', $template_names );
    72 
    73     // Try to find a template file
    74     foreach ( (array) $template_names as $template_name ) {
    75 
    76         // Continue if template is empty
    77         if ( empty( $template_name ) )
     64    $located = false;
     65
     66    // Loop through template stack
     67    foreach ( (array) bbp_get_template_stack() as $template_location ) {
     68
     69        // Continue if $template_location is empty
     70        if ( empty( $template_location ) )
    7871            continue;
    7972
    80         // Trim off any slashes from the template name
    81         $template_name  = ltrim( $template_name, '/' );
    82 
    83         // Check child theme first
    84         if ( file_exists( trailingslashit( $child_theme ) . $template_name ) ) {
    85             $located = trailingslashit( $child_theme ) . $template_name;
    86             break;
    87 
    88         // Check parent theme next
    89         } elseif ( file_exists( trailingslashit( $parent_theme ) . $template_name ) ) {
    90             $located = trailingslashit( $parent_theme ) . $template_name;
    91             break;
    92 
    93         // Check theme compatibility last
    94         } elseif ( file_exists( trailingslashit( $fallback_theme ) . $template_name ) ) {
    95             $located = trailingslashit( $fallback_theme ) . $template_name;
    96             break;
     73        // Try to find a template file
     74        foreach ( (array) $template_names as $template_name ) {
     75
     76            // Continue if template is empty
     77            if ( empty( $template_name ) )
     78                continue;
     79
     80            // Trim off any slashes from the template name
     81            $template_name  = ltrim( $template_name, '/' );
     82
     83            // Check child theme first
     84            if ( file_exists( trailingslashit( $template_location ) . $template_name ) ) {
     85                $located = trailingslashit( $template_location ) . $template_name;
     86                break;
     87            }
    9788        }
    9889    }
    9990
     91    // Maybe load the template if one was located
    10092    if ( ( true == $load ) && !empty( $located ) )
    10193        load_template( $located, $require_once );
    10294
    10395    return $located;
     96}
     97
     98/**
     99 * This is really cool. This function registers a new template stack location,
     100 * using WordPress's built in filters API.
     101 *
     102 * This allows for templates to live in places beyond just the parent/child
     103 * relationship, to allow for custom template locations. Used in conjunction
     104 * with bbp_locate_template(), this allows for easy template overrides.
     105 *
     106 * @since bbPress (r4323)
     107 *
     108 * @param string $location Callback function that returns the
     109 * @param int $priority
     110 */
     111function bbp_register_template_stack( $location_callback = '', $priority = 10 ) {
     112
     113    // Bail if no location, or function does not exist
     114    if ( empty( $location_callback ) || ! function_exists( $location_callback ) )
     115        return false;
     116
     117    // Add location callback to template stack
     118    add_filter( 'bbp_template_stack', $location_callback, (int) $priority );
     119}
     120
     121/**
     122 * Call the functions added to the 'bbp_template_stack' filter hook, and return
     123 * an array of the template locations.
     124 *
     125 * @see bbp_register_template_stack()
     126 *
     127 * @since bbPress (r4323)
     128 *
     129 * @global array $wp_filter Stores all of the filters
     130 * @global array $merged_filters Merges the filter hooks using this function.
     131 * @global array $wp_current_filter stores the list of current filters with the current one last
     132 *
     133 * @return array The filtered value after all hooked functions are applied to it.
     134 */
     135function bbp_get_template_stack() {
     136    global $wp_filter, $merged_filters, $wp_current_filter;
     137
     138    // Setup some default variables
     139    $tag  = 'bbp_template_stack';
     140    $args = $stack = array();
     141
     142    // Add 'bbp_template_stack' to the current filter array
     143    $wp_current_filter[] = $tag;
     144
     145    // Sort
     146    if ( ! isset( $merged_filters[ $tag ] ) ) {
     147        ksort( $wp_filter[$tag] );
     148        $merged_filters[ $tag ] = true;
     149    }
     150
     151    // Ensure we're always at the beginning of the filter array
     152    reset( $wp_filter[ $tag ] );
     153
     154    // Loop through 'bbp_template_stack' filters, and call callback functions
     155    while ( next( $wp_filter[$tag] ) !== false ) {
     156        foreach( (array) current( $wp_filter[$tag] ) as $the_ ) {
     157            if ( ! is_null( $the_['function'] ) ) {
     158                $args[1] = $stack;
     159                $stack[] = call_user_func_array( $the_['function'], array_slice( $args, 1, (int) $the_['accepted_args'] ) );
     160            }
     161        }
     162    };
     163
     164    // Remove 'bbp_template_stack' from the current filter array
     165    array_pop( $wp_current_filter );
     166
     167    // Remove empties and duplicates
     168    $stack = array_unique( array_filter( $stack ) );
     169
     170    return (array) apply_filters( 'bbp_get_template_stack', $stack ) ;
    104171}
    105172
Note: See TracChangeset for help on using the changeset viewer.