Changeset 4324 for trunk/includes/core/template-functions.php
- Timestamp:
- 11/03/2012 09:20:34 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/includes/core/template-functions.php
r4321 r4324 62 62 63 63 // 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 ) ) 78 71 continue; 79 72 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 } 97 88 } 98 89 } 99 90 91 // Maybe load the template if one was located 100 92 if ( ( true == $load ) && !empty( $located ) ) 101 93 load_template( $located, $require_once ); 102 94 103 95 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 */ 111 function 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 */ 135 function 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 ) ; 104 171 } 105 172
Note: See TracChangeset
for help on using the changeset viewer.