Make WordPress Core

Ticket #22355: 22355.2.patch

File 22355.2.patch, 4.0 KB (added by johnjamesjacoby, 12 years ago)

Reverse lookup (name to stack) and break out of both loops

  • wp-includes/template.php

     
    364364 * @return string The template filename if one is located.
    365365 */
    366366function locate_template($template_names, $load = false, $require_once = true ) {
    367         $located = '';
     367        $located        = '';
     368        $template_stack = get_template_stack();
     369
    368370        foreach ( (array) $template_names as $template_name ) {
    369                 if ( !$template_name )
     371
     372                // Continue if $template_name is empty
     373                if ( empty( $template_name ) )
    370374                        continue;
    371                 if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
    372                         $located = STYLESHEETPATH . '/' . $template_name;
    373                         break;
    374                 } else if ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
    375                         $located = TEMPLATEPATH . '/' . $template_name;
    376                         break;
     375
     376                $template_name = ltrim( $template_name, '/' );
     377
     378                foreach ( (array) $template_stack as $template_location ) {
     379
     380                        // Continue if $template_location is empty
     381                        if ( empty( $template_location ) )
     382                                continue;
     383
     384                        if ( file_exists( trailingslashit( $template_location ) . $template_name ) ) {
     385                                $located = trailingslashit( $template_location ) . $template_name;
     386                                break 2;
     387                        }
    377388                }
    378389        }
    379390
     
    407418                require( $_template_file );
    408419}
    409420
     421
     422/**
     423 * This function registers a new template stack location, using WordPress's
     424 * built-in filters API.
     425 *
     426 * This allows for templates to live in places beyond just the parent/child
     427 * relationship, to allow for custom template locations. Used in conjunction
     428 * with locate_template(), this allows for easy template overrides.
     429 *
     430 * @package WordPress
     431 * @since 3.6
     432 *
     433 * @param string $location Callback function that returns the
     434 * @param int $priority
     435 */
     436function register_template_stack( $location_callback = '', $priority = 10 ) {
     437
     438        // Bail if no location, or function does not exist
     439        if ( empty( $location_callback ) || ! function_exists( $location_callback ) )
     440                return false;
     441
     442        // Add location callback to template stack
     443        add_filter( 'template_stack', $location_callback, (int) $priority );
     444}
     445
     446/**
     447 * Call the functions added to the 'template_stack' filter hook, and return
     448 * an array of the template locations.
     449 *
     450 * @see register_template_stack()
     451 *
     452 * @package WordPress
     453 * @since 3.6
     454 *
     455 * @global array $wp_filter Stores all of the filters
     456 * @global array $merged_filters Merges the filter hooks using this function.
     457 * @global array $wp_current_filter stores the list of current filters with the current one last
     458 *
     459 * @return array The filtered value after all hooked functions are applied to it.
     460 */
     461function get_template_stack() {
     462        global $wp_filter, $merged_filters, $wp_current_filter;
     463
     464        // Setup some default variables
     465        $tag  = 'template_stack';
     466        $args = $stack = array();
     467
     468        // Add 'template_stack' to the current filter array
     469        $wp_current_filter[] = $tag;
     470
     471        // Sort
     472        if ( ! isset( $merged_filters[ $tag ] ) ) {
     473                ksort( $wp_filter[$tag] );
     474                $merged_filters[ $tag ] = true;
     475        }
     476
     477        // Ensure we're always at the beginning of the filter array
     478        reset( $wp_filter[ $tag ] );
     479
     480        // Loop through 'template_stack' filters, and call callback functions
     481        while ( next( $wp_filter[$tag] ) !== false ) {
     482                foreach( (array) current( $wp_filter[$tag] ) as $the_ ) {
     483                        if ( ! is_null( $the_['function'] ) ) {
     484                                $args[1] = $stack;
     485                                $stack[] = call_user_func_array( $the_['function'], array_slice( $args, 1, (int) $the_['accepted_args'] ) );
     486                        }
     487                }
     488        };
     489
     490        // Remove 'template_stack' from the current filter array
     491        array_pop( $wp_current_filter );
     492
     493        // Remove empties and duplicates
     494        $stack = array_unique( array_filter( $stack ) );
     495
     496        return (array) apply_filters( 'get_template_stack', $stack ) ;
     497}
  • wp-settings.php

     
    268268// Define the template related constants.
    269269wp_templating_constants(  );
    270270
     271// Register the parent/child template stack
     272register_template_stack( 'get_stylesheet_directory', 10 );
     273register_template_stack( 'get_template_directory',   12 );
     274
    271275// Load the default text localization domain.
    272276load_default_textdomain();
    273277