WordPress.org

Make WordPress Core

Ticket #22355: 22355.patch

File 22355.patch, 3.9 KB (added by johnjamesjacoby, 6 years ago)
  • wp-includes/template.php

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