Issues (4967)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/wp-includes/template.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Template loading functions.
4
 *
5
 * @package WordPress
6
 * @subpackage Template
7
 */
8
9
/**
10
 * Retrieve path to a template
11
 *
12
 * Used to quickly retrieve the path of a template without including the file
13
 * extension. It will also check the parent theme, if the file exists, with
14
 * the use of locate_template(). Allows for more generic template location
15
 * without the use of the other get_*_template() functions.
16
 *
17
 * @since 1.5.0
18
 *
19
 * @param string $type      Filename without extension.
20
 * @param array  $templates An optional list of template candidates
21
 * @return string Full path to template file.
22
 */
23
function get_query_template( $type, $templates = array() ) {
24
	$type = preg_replace( '|[^a-z0-9-]+|', '', $type );
25
26
	if ( empty( $templates ) )
27
		$templates = array("{$type}.php");
28
29
	/**
30
	 * Filters the list of template filenames that are searched for when retrieving a template to use.
31
	 *
32
	 * The last element in the array should always be the fallback template for this query type.
33
	 *
34
	 * Possible values for `$type` include: 'index', '404', 'archive', 'author', 'category', 'tag', 'taxonomy', 'date',
35
	 * 'embed', 'home', 'frontpage', 'page', 'paged', 'search', 'single', 'singular', and 'attachment'.
36
	 *
37
	 * @since 4.7.0
38
	 *
39
	 * @param array $templates A list of template candidates, in descending order of priority.
40
	 */
41
	$templates = apply_filters( "{$type}_template_hierarchy", $templates );
42
43
	$template = locate_template( $templates );
44
45
	/**
46
	 * Filters the path of the queried template by type.
47
	 *
48
	 * The dynamic portion of the hook name, `$type`, refers to the filename -- minus the file
49
	 * extension and any non-alphanumeric characters delimiting words -- of the file to load.
50
	 * This hook also applies to various types of files loaded as part of the Template Hierarchy.
51
	 *
52
	 * Possible values for `$type` include: 'index', '404', 'archive', 'author', 'category', 'tag', 'taxonomy', 'date',
53
	 * 'embed', 'home', 'frontpage', 'page', 'paged', 'search', 'single', 'singular', and 'attachment'.
54
	 *
55
	 * @since 1.5.0
56
	 * @since 4.8.0 The `$type` and `$templates` parameters were added.
57
	 *
58
	 * @param string $template  Path to the template. See locate_template().
59
	 * @param string $type      Filename without extension.
60
	 * @param array  $templates A list of template candidates, in descending order of priority.
61
	 */
62
	return apply_filters( "{$type}_template", $template, $type, $templates );
63
}
64
65
/**
66
 * Retrieve path of index template in current or parent template.
67
 *
68
 * The template hierarchy is filterable via the {@see 'index_template_hierarchy'} hook.
69
 * The template path is filterable via the {@see 'index_template'} hook.
70
 *
71
 * @since 3.0.0
72
 *
73
 * @see get_query_template()
74
 *
75
 * @return string Full path to index template file.
76
 */
77
function get_index_template() {
78
	return get_query_template('index');
79
}
80
81
/**
82
 * Retrieve path of 404 template in current or parent template.
83
 *
84
 * The template hierarchy is filterable via the {@see '404_template_hierarchy'} hook.
85
 * The template path is filterable via the {@see '404_template'} hook.
86
 *
87
 * @since 1.5.0
88
 *
89
 * @see get_query_template()
90
 *
91
 * @return string Full path to 404 template file.
92
 */
93
function get_404_template() {
94
	return get_query_template('404');
95
}
96
97
/**
98
 * Retrieve path of archive template in current or parent template.
99
 *
100
 * The template hierarchy is filterable via the {@see 'archive_template_hierarchy'} hook.
101
 * The template path is filterable via the {@see 'archive_template'} hook.
102
 *
103
 * @since 1.5.0
104
 *
105
 * @see get_query_template()
106
 *
107
 * @return string Full path to archive template file.
108
 */
109
function get_archive_template() {
110
	$post_types = array_filter( (array) get_query_var( 'post_type' ) );
111
112
	$templates = array();
113
114
	if ( count( $post_types ) == 1 ) {
115
		$post_type = reset( $post_types );
116
		$templates[] = "archive-{$post_type}.php";
117
	}
118
	$templates[] = 'archive.php';
119
120
	return get_query_template( 'archive', $templates );
121
}
122
123
/**
124
 * Retrieve path of post type archive template in current or parent template.
125
 *
126
 * The template hierarchy is filterable via the {@see 'archive_template_hierarchy'} hook.
127
 * The template path is filterable via the {@see 'archive_template'} hook.
128
 *
129
 * @since 3.7.0
130
 *
131
 * @see get_archive_template()
132
 *
133
 * @return string Full path to archive template file.
134
 */
135
function get_post_type_archive_template() {
136
	$post_type = get_query_var( 'post_type' );
137
	if ( is_array( $post_type ) )
138
		$post_type = reset( $post_type );
139
140
	$obj = get_post_type_object( $post_type );
141
	if ( ! ( $obj instanceof WP_Post_Type ) || ! $obj->has_archive ) {
142
		return '';
143
	}
144
145
	return get_archive_template();
146
}
147
148
/**
149
 * Retrieve path of author template in current or parent template.
150
 *
151
 * The hierarchy for this template looks like:
152
 *
153
 * 1. author-{nicename}.php
154
 * 2. author-{id}.php
155
 * 3. author.php
156
 *
157
 * An example of this is:
158
 *
159
 * 1. author-john.php
160
 * 2. author-1.php
161
 * 3. author.php
162
 *
163
 * The template hierarchy is filterable via the {@see 'author_template_hierarchy'} hook.
164
 * The template path is filterable via the {@see 'author_template'} hook.
165
 *
166
 * @since 1.5.0
167
 *
168
 * @see get_query_template()
169
 *
170
 * @return string Full path to author template file.
171
 */
172
function get_author_template() {
173
	$author = get_queried_object();
174
175
	$templates = array();
176
177
	if ( $author instanceof WP_User ) {
178
		$templates[] = "author-{$author->user_nicename}.php";
179
		$templates[] = "author-{$author->ID}.php";
180
	}
181
	$templates[] = 'author.php';
182
183
	return get_query_template( 'author', $templates );
184
}
185
186
/**
187
 * Retrieve path of category template in current or parent template.
188
 *
189
 * The hierarchy for this template looks like:
190
 *
191
 * 1. category-{slug}.php
192
 * 2. category-{id}.php
193
 * 3. category.php
194
 *
195
 * An example of this is:
196
 *
197
 * 1. category-news.php
198
 * 2. category-2.php
199
 * 3. category.php
200
 *
201
 * The template hierarchy is filterable via the {@see 'category_template_hierarchy'} hook.
202
 * The template path is filterable via the {@see 'category_template'} hook.
203
 *
204
 * @since 1.5.0
205
 * @since 4.7.0 The decoded form of `category-{slug}.php` was added to the top of the
206
 *              template hierarchy when the category slug contains multibyte characters.
207
 *
208
 * @see get_query_template()
209
 *
210
 * @return string Full path to category template file.
211
 */
212 View Code Duplication
function get_category_template() {
0 ignored issues
show
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
213
	$category = get_queried_object();
214
215
	$templates = array();
216
217
	if ( ! empty( $category->slug ) ) {
218
219
		$slug_decoded = urldecode( $category->slug );
220
		if ( $slug_decoded !== $category->slug ) {
221
			$templates[] = "category-{$slug_decoded}.php";
222
		}
223
224
		$templates[] = "category-{$category->slug}.php";
225
		$templates[] = "category-{$category->term_id}.php";
226
	}
227
	$templates[] = 'category.php';
228
229
	return get_query_template( 'category', $templates );
230
}
231
232
/**
233
 * Retrieve path of tag template in current or parent template.
234
 *
235
 * The hierarchy for this template looks like:
236
 *
237
 * 1. tag-{slug}.php
238
 * 2. tag-{id}.php
239
 * 3. tag.php
240
 *
241
 * An example of this is:
242
 *
243
 * 1. tag-wordpress.php
244
 * 2. tag-3.php
245
 * 3. tag.php
246
 *
247
 * The template hierarchy is filterable via the {@see 'tag_template_hierarchy'} hook.
248
 * The template path is filterable via the {@see 'tag_template'} hook.
249
 *
250
 * @since 2.3.0
251
 * @since 4.7.0 The decoded form of `tag-{slug}.php` was added to the top of the
252
 *              template hierarchy when the tag slug contains multibyte characters.
253
 *
254
 * @see get_query_template()
255
 *
256
 * @return string Full path to tag template file.
257
 */
258 View Code Duplication
function get_tag_template() {
0 ignored issues
show
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
259
	$tag = get_queried_object();
260
261
	$templates = array();
262
263
	if ( ! empty( $tag->slug ) ) {
264
265
		$slug_decoded = urldecode( $tag->slug );
266
		if ( $slug_decoded !== $tag->slug ) {
267
			$templates[] = "tag-{$slug_decoded}.php";
268
		}
269
270
		$templates[] = "tag-{$tag->slug}.php";
271
		$templates[] = "tag-{$tag->term_id}.php";
272
	}
273
	$templates[] = 'tag.php';
274
275
	return get_query_template( 'tag', $templates );
276
}
277
278
/**
279
 * Retrieve path of custom taxonomy term template in current or parent template.
280
 *
281
 * The hierarchy for this template looks like:
282
 *
283
 * 1. taxonomy-{taxonomy_slug}-{term_slug}.php
284
 * 2. taxonomy-{taxonomy_slug}.php
285
 * 3. taxonomy.php
286
 *
287
 * An example of this is:
288
 *
289
 * 1. taxonomy-location-texas.php
290
 * 2. taxonomy-location.php
291
 * 3. taxonomy.php
292
 *
293
 * The template hierarchy is filterable via the {@see 'taxonomy_template_hierarchy'} hook.
294
 * The template path is filterable via the {@see 'taxonomy_template'} hook.
295
 *
296
 * @since 2.5.0
297
 * @since 4.7.0 The decoded form of `taxonomy-{taxonomy_slug}-{term_slug}.php` was added to the top of the
298
 *              template hierarchy when the term slug contains multibyte characters.
299
 *
300
 * @see get_query_template()
301
 *
302
 * @return string Full path to custom taxonomy term template file.
303
 */
304
function get_taxonomy_template() {
305
	$term = get_queried_object();
306
307
	$templates = array();
308
309
	if ( ! empty( $term->slug ) ) {
310
		$taxonomy = $term->taxonomy;
311
312
		$slug_decoded = urldecode( $term->slug );
313
		if ( $slug_decoded !== $term->slug ) {
314
			$templates[] = "taxonomy-$taxonomy-{$slug_decoded}.php";
315
		}
316
317
		$templates[] = "taxonomy-$taxonomy-{$term->slug}.php";
318
		$templates[] = "taxonomy-$taxonomy.php";
319
	}
320
	$templates[] = 'taxonomy.php';
321
322
	return get_query_template( 'taxonomy', $templates );
323
}
324
325
/**
326
 * Retrieve path of date template in current or parent template.
327
 *
328
 * The template hierarchy is filterable via the {@see 'date_template_hierarchy'} hook.
329
 * The template path is filterable via the {@see 'date_template'} hook.
330
 *
331
 * @since 1.5.0
332
 *
333
 * @see get_query_template()
334
 *
335
 * @return string Full path to date template file.
336
 */
337
function get_date_template() {
338
	return get_query_template('date');
339
}
340
341
/**
342
 * Retrieve path of home template in current or parent template.
343
 *
344
 * The template hierarchy is filterable via the {@see 'home_template_hierarchy'} hook.
345
 * The template path is filterable via the {@see 'home_template'} hook.
346
 *
347
 * @since 1.5.0
348
 *
349
 * @see get_query_template()
350
 *
351
 * @return string Full path to home template file.
352
 */
353
function get_home_template() {
354
	$templates = array( 'home.php', 'index.php' );
355
356
	return get_query_template( 'home', $templates );
357
}
358
359
/**
360
 * Retrieve path of front page template in current or parent template.
361
 *
362
 * The template hierarchy is filterable via the {@see 'frontpage_template_hierarchy'} hook.
363
 * The template path is filterable via the {@see 'frontpage_template'} hook.
364
 *
365
 * @since 3.0.0
366
 *
367
 * @see get_query_template()
368
 *
369
 * @return string Full path to front page template file.
370
 */
371
function get_front_page_template() {
372
	$templates = array('front-page.php');
373
374
	return get_query_template( 'front_page', $templates );
375
}
376
377
/**
378
 * Retrieve path of page template in current or parent template.
379
 *
380
 * The hierarchy for this template looks like:
381
 *
382
 * 1. {Page Template}.php
383
 * 2. page-{page_name}.php
384
 * 3. page-{id}.php
385
 * 4. page.php
386
 *
387
 * An example of this is:
388
 *
389
 * 1. page-templates/full-width.php
390
 * 2. page-about.php
391
 * 3. page-4.php
392
 * 4. page.php
393
 *
394
 * The template hierarchy is filterable via the {@see 'page_template_hierarchy'} hook.
395
 * The template path is filterable via the {@see 'page_template'} hook.
396
 *
397
 * @since 1.5.0
398
 * @since 4.7.0 The decoded form of `page-{page_name}.php` was added to the top of the
399
 *              template hierarchy when the page name contains multibyte characters.
400
 *
401
 * @see get_query_template()
402
 *
403
 * @return string Full path to page template file.
404
 */
405
function get_page_template() {
406
	$id = get_queried_object_id();
407
	$template = get_page_template_slug();
408
	$pagename = get_query_var('pagename');
409
410
	if ( ! $pagename && $id ) {
411
		// If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object
412
		$post = get_queried_object();
413
		if ( $post )
414
			$pagename = $post->post_name;
415
	}
416
417
	$templates = array();
418
	if ( $template && 0 === validate_file( $template ) )
0 ignored issues
show
Bug Best Practice introduced by
The expression $template of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
419
		$templates[] = $template;
420
	if ( $pagename ) {
421
		$pagename_decoded = urldecode( $pagename );
422
		if ( $pagename_decoded !== $pagename ) {
423
			$templates[] = "page-{$pagename_decoded}.php";
424
		}
425
		$templates[] = "page-{$pagename}.php";
426
	}
427
	if ( $id )
428
		$templates[] = "page-{$id}.php";
429
	$templates[] = 'page.php';
430
431
	return get_query_template( 'page', $templates );
432
}
433
434
/**
435
 * Retrieve path of search template in current or parent template.
436
 *
437
 * The template hierarchy is filterable via the {@see 'search_template_hierarchy'} hook.
438
 * The template path is filterable via the {@see 'search_template'} hook.
439
 *
440
 * @since 1.5.0
441
 *
442
 * @see get_query_template()
443
 *
444
 * @return string Full path to search template file.
445
 */
446
function get_search_template() {
447
	return get_query_template('search');
448
}
449
450
/**
451
 * Retrieve path of single template in current or parent template. Applies to single Posts,
452
 * single Attachments, and single custom post types.
453
 *
454
 * The hierarchy for this template looks like:
455
 *
456
 * 1. {Post Type Template}.php
457
 * 2. single-{post_type}-{post_name}.php
458
 * 3. single-{post_type}.php
459
 * 4. single.php
460
 *
461
 * An example of this is:
462
 *
463
 * 1. templates/full-width.php
464
 * 2. single-post-hello-world.php
465
 * 3. single-post.php
466
 * 4. single.php
467
 *
468
 * The template hierarchy is filterable via the {@see 'single_template_hierarchy'} hook.
469
 * The template path is filterable via the {@see 'single_template'} hook.
470
 *
471
 * @since 1.5.0
472
 * @since 4.4.0 `single-{post_type}-{post_name}.php` was added to the top of the template hierarchy.
473
 * @since 4.7.0 The decoded form of `single-{post_type}-{post_name}.php` was added to the top of the
474
 *              template hierarchy when the post name contains multibyte characters.
475
 * @since 4.7.0 {Post Type Template}.php was added to the top of the template hierarchy.
476
 *
477
 * @see get_query_template()
478
 *
479
 * @return string Full path to single template file.
480
 */
481
function get_single_template() {
482
	$object = get_queried_object();
483
484
	$templates = array();
485
486
	if ( ! empty( $object->post_type ) ) {
487
		$template = get_page_template_slug( $object );
488
		if ( $template && 0 === validate_file( $template ) ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $template of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
489
			$templates[] = $template;
490
		}
491
492
		$name_decoded = urldecode( $object->post_name );
493
		if ( $name_decoded !== $object->post_name ) {
494
			$templates[] = "single-{$object->post_type}-{$name_decoded}.php";
495
		}
496
497
		$templates[] = "single-{$object->post_type}-{$object->post_name}.php";
498
		$templates[] = "single-{$object->post_type}.php";
499
	}
500
501
	$templates[] = "single.php";
502
503
	return get_query_template( 'single', $templates );
504
}
505
506
/**
507
 * Retrieves an embed template path in the current or parent template.
508
 *
509
 * The hierarchy for this template looks like:
510
 *
511
 * 1. embed-{post_type}-{post_format}.php
512
 * 2. embed-{post_type}.php
513
 * 3. embed.php
514
 *
515
 * An example of this is:
516
 *
517
 * 1. embed-post-audio.php
518
 * 2. embed-post.php
519
 * 3. embed.php
520
 *
521
 * The template hierarchy is filterable via the {@see 'embed_template_hierarchy'} hook.
522
 * The template path is filterable via the {@see 'embed_template'} hook.
523
 *
524
 * @since 4.5.0
525
 *
526
 * @see get_query_template()
527
 *
528
 * @return string Full path to embed template file.
529
 */
530
function get_embed_template() {
531
	$object = get_queried_object();
532
533
	$templates = array();
534
535
	if ( ! empty( $object->post_type ) ) {
536
		$post_format = get_post_format( $object );
537
		if ( $post_format ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $post_format of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
538
			$templates[] = "embed-{$object->post_type}-{$post_format}.php";
539
		}
540
		$templates[] = "embed-{$object->post_type}.php";
541
	}
542
543
	$templates[] = "embed.php";
544
545
	return get_query_template( 'embed', $templates );
546
}
547
548
/**
549
 * Retrieves the path of the singular template in current or parent template.
550
 *
551
 * The template hierarchy is filterable via the {@see 'singular_template_hierarchy'} hook.
552
 * The template path is filterable via the {@see 'singular_template'} hook.
553
 *
554
 * @since 4.3.0
555
 *
556
 * @see get_query_template()
557
 *
558
 * @return string Full path to singular template file
559
 */
560
function get_singular_template() {
561
	return get_query_template( 'singular' );
562
}
563
564
/**
565
 * Retrieve path of attachment template in current or parent template.
566
 *
567
 * The hierarchy for this template looks like:
568
 *
569
 * 1. {mime_type}-{sub_type}.php
570
 * 2. {sub_type}.php
571
 * 3. {mime_type}.php
572
 * 4. attachment.php
573
 *
574
 * An example of this is:
575
 *
576
 * 1. image-jpeg.php
577
 * 2. jpeg.php
578
 * 3. image.php
579
 * 4. attachment.php
580
 *
581
 * The template hierarchy is filterable via the {@see 'attachment_template_hierarchy'} hook.
582
 * The template path is filterable via the {@see 'attachment_template'} hook.
583
 *
584
 * @since 2.0.0
585
 * @since 4.3.0 The order of the mime type logic was reversed so the hierarchy is more logical.
586
 *
587
 * @see get_query_template()
588
 *
589
 * @global array $posts
590
 *
591
 * @return string Full path to attachment template file.
592
 */
593
function get_attachment_template() {
594
	$attachment = get_queried_object();
595
596
	$templates = array();
597
598
	if ( $attachment ) {
599 View Code Duplication
		if ( false !== strpos( $attachment->post_mime_type, '/' ) ) {
600
			list( $type, $subtype ) = explode( '/', $attachment->post_mime_type );
601
		} else {
602
			list( $type, $subtype ) = array( $attachment->post_mime_type, '' );
603
		}
604
605
		if ( ! empty( $subtype ) ) {
606
			$templates[] = "{$type}-{$subtype}.php";
607
			$templates[] = "{$subtype}.php";
608
		}
609
		$templates[] = "{$type}.php";
610
	}
611
	$templates[] = 'attachment.php';
612
613
	return get_query_template( 'attachment', $templates );
614
}
615
616
/**
617
 * Retrieve the name of the highest priority template file that exists.
618
 *
619
 * Searches in the STYLESHEETPATH before TEMPLATEPATH and wp-includes/theme-compat
620
 * so that themes which inherit from a parent theme can just overload one file.
621
 *
622
 * @since 2.7.0
623
 *
624
 * @param string|array $template_names Template file(s) to search for, in order.
625
 * @param bool         $load           If true the template file will be loaded if it is found.
626
 * @param bool         $require_once   Whether to require_once or require. Default true. Has no effect if $load is false.
627
 * @return string The template filename if one is located.
628
 */
629
function locate_template($template_names, $load = false, $require_once = true ) {
630
	$located = '';
631
	foreach ( (array) $template_names as $template_name ) {
632
		if ( !$template_name )
633
			continue;
634
		if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
635
			$located = STYLESHEETPATH . '/' . $template_name;
636
			break;
637
		} elseif ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
638
			$located = TEMPLATEPATH . '/' . $template_name;
639
			break;
640
		} elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
641
			$located = ABSPATH . WPINC . '/theme-compat/' . $template_name;
642
			break;
643
		}
644
	}
645
646
	if ( $load && '' != $located )
647
		load_template( $located, $require_once );
648
649
	return $located;
650
}
651
652
/**
653
 * Require the template file with WordPress environment.
654
 *
655
 * The globals are set up for the template file to ensure that the WordPress
656
 * environment is available from within the function. The query variables are
657
 * also available.
658
 *
659
 * @since 1.5.0
660
 *
661
 * @global array      $posts
662
 * @global WP_Post    $post
663
 * @global bool       $wp_did_header
664
 * @global WP_Query   $wp_query
665
 * @global WP_Rewrite $wp_rewrite
666
 * @global wpdb       $wpdb
667
 * @global string     $wp_version
668
 * @global WP         $wp
669
 * @global int        $id
670
 * @global WP_Comment $comment
671
 * @global int        $user_ID
672
 *
673
 * @param string $_template_file Path to template file.
674
 * @param bool   $require_once   Whether to require_once or require. Default true.
675
 */
676
function load_template( $_template_file, $require_once = true ) {
677
	global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
678
679
	if ( is_array( $wp_query->query_vars ) ) {
680
		extract( $wp_query->query_vars, EXTR_SKIP );
681
	}
682
683
	if ( isset( $s ) ) {
684
		$s = esc_attr( $s );
685
	}
686
687
	if ( $require_once ) {
688
		require_once( $_template_file );
689
	} else {
690
		require( $_template_file );
691
	}
692
}
693