wp/wp-includes/template.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    21  * @return string Full path to template file.
    21  * @return string Full path to template file.
    22  */
    22  */
    23 function get_query_template( $type, $templates = array() ) {
    23 function get_query_template( $type, $templates = array() ) {
    24 	$type = preg_replace( '|[^a-z0-9-]+|', '', $type );
    24 	$type = preg_replace( '|[^a-z0-9-]+|', '', $type );
    25 
    25 
    26 	if ( empty( $templates ) )
    26 	if ( empty( $templates ) ) {
    27 		$templates = array("{$type}.php");
    27 		$templates = array( "{$type}.php" );
       
    28 	}
    28 
    29 
    29 	/**
    30 	/**
    30 	 * Filters the list of template filenames that are searched for when retrieving a template to use.
    31 	 * Filters the list of template filenames that are searched for when retrieving a template to use.
    31 	 *
    32 	 *
    32 	 * The last element in the array should always be the fallback template for this query type.
    33 	 * The last element in the array should always be the fallback template for this query type.
    33 	 *
    34 	 *
    34 	 * Possible values for `$type` include: 'index', '404', 'archive', 'author', 'category', 'tag', 'taxonomy', 'date',
    35 	 * 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 	 * 'embed', 'home', 'frontpage', 'privacypolicy', 'page', 'paged', 'search', 'single', 'singular', and 'attachment'.
    36 	 *
    37 	 *
    37 	 * @since 4.7.0
    38 	 * @since 4.7.0
    38 	 *
    39 	 *
    39 	 * @param array $templates A list of template candidates, in descending order of priority.
    40 	 * @param array $templates A list of template candidates, in descending order of priority.
    40 	 */
    41 	 */
    48 	 * The dynamic portion of the hook name, `$type`, refers to the filename -- minus the file
    49 	 * 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 	 * 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 	 * This hook also applies to various types of files loaded as part of the Template Hierarchy.
    51 	 *
    52 	 *
    52 	 * Possible values for `$type` include: 'index', '404', 'archive', 'author', 'category', 'tag', 'taxonomy', 'date',
    53 	 * 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 	 * 'embed', 'home', 'frontpage', 'privacypolicy', 'page', 'paged', 'search', 'single', 'singular', and 'attachment'.
    54 	 *
    55 	 *
    55 	 * @since 1.5.0
    56 	 * @since 1.5.0
    56 	 * @since 4.8.0 The `$type` and `$templates` parameters were added.
    57 	 * @since 4.8.0 The `$type` and `$templates` parameters were added.
    57 	 *
    58 	 *
    58 	 * @param string $template  Path to the template. See locate_template().
    59 	 * @param string $template  Path to the template. See locate_template().
    59 	 * @param string $type      Filename without extension.
    60 	 * @param string $type      Sanitized filename without extension.
    60 	 * @param array  $templates A list of template candidates, in descending order of priority.
    61 	 * @param array  $templates A list of template candidates, in descending order of priority.
    61 	 */
    62 	 */
    62 	return apply_filters( "{$type}_template", $template, $type, $templates );
    63 	return apply_filters( "{$type}_template", $template, $type, $templates );
    63 }
    64 }
    64 
    65 
    73  * @see get_query_template()
    74  * @see get_query_template()
    74  *
    75  *
    75  * @return string Full path to index template file.
    76  * @return string Full path to index template file.
    76  */
    77  */
    77 function get_index_template() {
    78 function get_index_template() {
    78 	return get_query_template('index');
    79 	return get_query_template( 'index' );
    79 }
    80 }
    80 
    81 
    81 /**
    82 /**
    82  * Retrieve path of 404 template in current or parent template.
    83  * Retrieve path of 404 template in current or parent template.
    83  *
    84  *
    89  * @see get_query_template()
    90  * @see get_query_template()
    90  *
    91  *
    91  * @return string Full path to 404 template file.
    92  * @return string Full path to 404 template file.
    92  */
    93  */
    93 function get_404_template() {
    94 function get_404_template() {
    94 	return get_query_template('404');
    95 	return get_query_template( '404' );
    95 }
    96 }
    96 
    97 
    97 /**
    98 /**
    98  * Retrieve path of archive template in current or parent template.
    99  * Retrieve path of archive template in current or parent template.
    99  *
   100  *
   110 	$post_types = array_filter( (array) get_query_var( 'post_type' ) );
   111 	$post_types = array_filter( (array) get_query_var( 'post_type' ) );
   111 
   112 
   112 	$templates = array();
   113 	$templates = array();
   113 
   114 
   114 	if ( count( $post_types ) == 1 ) {
   115 	if ( count( $post_types ) == 1 ) {
   115 		$post_type = reset( $post_types );
   116 		$post_type   = reset( $post_types );
   116 		$templates[] = "archive-{$post_type}.php";
   117 		$templates[] = "archive-{$post_type}.php";
   117 	}
   118 	}
   118 	$templates[] = 'archive.php';
   119 	$templates[] = 'archive.php';
   119 
   120 
   120 	return get_query_template( 'archive', $templates );
   121 	return get_query_template( 'archive', $templates );
   132  *
   133  *
   133  * @return string Full path to archive template file.
   134  * @return string Full path to archive template file.
   134  */
   135  */
   135 function get_post_type_archive_template() {
   136 function get_post_type_archive_template() {
   136 	$post_type = get_query_var( 'post_type' );
   137 	$post_type = get_query_var( 'post_type' );
   137 	if ( is_array( $post_type ) )
   138 	if ( is_array( $post_type ) ) {
   138 		$post_type = reset( $post_type );
   139 		$post_type = reset( $post_type );
       
   140 	}
   139 
   141 
   140 	$obj = get_post_type_object( $post_type );
   142 	$obj = get_post_type_object( $post_type );
   141 	if ( ! ( $obj instanceof WP_Post_Type ) || ! $obj->has_archive ) {
   143 	if ( ! ( $obj instanceof WP_Post_Type ) || ! $obj->has_archive ) {
   142 		return '';
   144 		return '';
   143 	}
   145 	}
   333  * @see get_query_template()
   335  * @see get_query_template()
   334  *
   336  *
   335  * @return string Full path to date template file.
   337  * @return string Full path to date template file.
   336  */
   338  */
   337 function get_date_template() {
   339 function get_date_template() {
   338 	return get_query_template('date');
   340 	return get_query_template( 'date' );
   339 }
   341 }
   340 
   342 
   341 /**
   343 /**
   342  * Retrieve path of home template in current or parent template.
   344  * Retrieve path of home template in current or parent template.
   343  *
   345  *
   367  * @see get_query_template()
   369  * @see get_query_template()
   368  *
   370  *
   369  * @return string Full path to front page template file.
   371  * @return string Full path to front page template file.
   370  */
   372  */
   371 function get_front_page_template() {
   373 function get_front_page_template() {
   372 	$templates = array('front-page.php');
   374 	$templates = array( 'front-page.php' );
   373 
   375 
   374 	return get_query_template( 'front_page', $templates );
   376 	return get_query_template( 'frontpage', $templates );
       
   377 }
       
   378 
       
   379 /**
       
   380  * Retrieve path of Privacy Policy page template in current or parent template.
       
   381  *
       
   382  * The template hierarchy and template path are filterable via the {@see '$type_template_hierarchy'}
       
   383  * and {@see '$type_template'} dynamic hooks, where `$type` is 'privacypolicy'.
       
   384  *
       
   385  * @since 5.2.0
       
   386  *
       
   387  * @see get_query_template()
       
   388  *
       
   389  * @return string Full path to privacy policy template file.
       
   390  */
       
   391 function get_privacy_policy_template() {
       
   392 	$templates = array( 'privacy-policy.php' );
       
   393 
       
   394 	return get_query_template( 'privacypolicy', $templates );
   375 }
   395 }
   376 
   396 
   377 /**
   397 /**
   378  * Retrieve path of page template in current or parent template.
   398  * Retrieve path of page template in current or parent template.
   379  *
   399  *
   401  * @see get_query_template()
   421  * @see get_query_template()
   402  *
   422  *
   403  * @return string Full path to page template file.
   423  * @return string Full path to page template file.
   404  */
   424  */
   405 function get_page_template() {
   425 function get_page_template() {
   406 	$id = get_queried_object_id();
   426 	$id       = get_queried_object_id();
   407 	$template = get_page_template_slug();
   427 	$template = get_page_template_slug();
   408 	$pagename = get_query_var('pagename');
   428 	$pagename = get_query_var( 'pagename' );
   409 
   429 
   410 	if ( ! $pagename && $id ) {
   430 	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
   431 		// 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();
   432 		$post = get_queried_object();
   413 		if ( $post )
   433 		if ( $post ) {
   414 			$pagename = $post->post_name;
   434 			$pagename = $post->post_name;
   415 	}
   435 		}
   416 
   436 	}
   417 	$templates = array();
   437 
   418 	if ( $template && 0 === validate_file( $template ) )
   438 	$templates = array();
       
   439 	if ( $template && 0 === validate_file( $template ) ) {
   419 		$templates[] = $template;
   440 		$templates[] = $template;
       
   441 	}
   420 	if ( $pagename ) {
   442 	if ( $pagename ) {
   421 		$pagename_decoded = urldecode( $pagename );
   443 		$pagename_decoded = urldecode( $pagename );
   422 		if ( $pagename_decoded !== $pagename ) {
   444 		if ( $pagename_decoded !== $pagename ) {
   423 			$templates[] = "page-{$pagename_decoded}.php";
   445 			$templates[] = "page-{$pagename_decoded}.php";
   424 		}
   446 		}
   425 		$templates[] = "page-{$pagename}.php";
   447 		$templates[] = "page-{$pagename}.php";
   426 	}
   448 	}
   427 	if ( $id )
   449 	if ( $id ) {
   428 		$templates[] = "page-{$id}.php";
   450 		$templates[] = "page-{$id}.php";
       
   451 	}
   429 	$templates[] = 'page.php';
   452 	$templates[] = 'page.php';
   430 
   453 
   431 	return get_query_template( 'page', $templates );
   454 	return get_query_template( 'page', $templates );
   432 }
   455 }
   433 
   456 
   442  * @see get_query_template()
   465  * @see get_query_template()
   443  *
   466  *
   444  * @return string Full path to search template file.
   467  * @return string Full path to search template file.
   445  */
   468  */
   446 function get_search_template() {
   469 function get_search_template() {
   447 	return get_query_template('search');
   470 	return get_query_template( 'search' );
   448 }
   471 }
   449 
   472 
   450 /**
   473 /**
   451  * Retrieve path of single template in current or parent template. Applies to single Posts,
   474  * Retrieve path of single template in current or parent template. Applies to single Posts,
   452  * single Attachments, and single custom post types.
   475  * single Attachments, and single custom post types.
   470  *
   493  *
   471  * @since 1.5.0
   494  * @since 1.5.0
   472  * @since 4.4.0 `single-{post_type}-{post_name}.php` was added to the top of the template hierarchy.
   495  * @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
   496  * @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.
   497  *              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.
   498  * @since 4.7.0 `{Post Type Template}.php` was added to the top of the template hierarchy.
   476  *
   499  *
   477  * @see get_query_template()
   500  * @see get_query_template()
   478  *
   501  *
   479  * @return string Full path to single template file.
   502  * @return string Full path to single template file.
   480  */
   503  */
   496 
   519 
   497 		$templates[] = "single-{$object->post_type}-{$object->post_name}.php";
   520 		$templates[] = "single-{$object->post_type}-{$object->post_name}.php";
   498 		$templates[] = "single-{$object->post_type}.php";
   521 		$templates[] = "single-{$object->post_type}.php";
   499 	}
   522 	}
   500 
   523 
   501 	$templates[] = "single.php";
   524 	$templates[] = 'single.php';
   502 
   525 
   503 	return get_query_template( 'single', $templates );
   526 	return get_query_template( 'single', $templates );
   504 }
   527 }
   505 
   528 
   506 /**
   529 /**
   538 			$templates[] = "embed-{$object->post_type}-{$post_format}.php";
   561 			$templates[] = "embed-{$object->post_type}-{$post_format}.php";
   539 		}
   562 		}
   540 		$templates[] = "embed-{$object->post_type}.php";
   563 		$templates[] = "embed-{$object->post_type}.php";
   541 	}
   564 	}
   542 
   565 
   543 	$templates[] = "embed.php";
   566 	$templates[] = 'embed.php';
   544 
   567 
   545 	return get_query_template( 'embed', $templates );
   568 	return get_query_template( 'embed', $templates );
   546 }
   569 }
   547 
   570 
   548 /**
   571 /**
   624  * @param string|array $template_names Template file(s) to search for, in order.
   647  * @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.
   648  * @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.
   649  * @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.
   650  * @return string The template filename if one is located.
   628  */
   651  */
   629 function locate_template($template_names, $load = false, $require_once = true ) {
   652 function locate_template( $template_names, $load = false, $require_once = true ) {
   630 	$located = '';
   653 	$located = '';
   631 	foreach ( (array) $template_names as $template_name ) {
   654 	foreach ( (array) $template_names as $template_name ) {
   632 		if ( !$template_name )
   655 		if ( ! $template_name ) {
   633 			continue;
   656 			continue;
   634 		if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
   657 		}
       
   658 		if ( file_exists( STYLESHEETPATH . '/' . $template_name ) ) {
   635 			$located = STYLESHEETPATH . '/' . $template_name;
   659 			$located = STYLESHEETPATH . '/' . $template_name;
   636 			break;
   660 			break;
   637 		} elseif ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
   661 		} elseif ( file_exists( TEMPLATEPATH . '/' . $template_name ) ) {
   638 			$located = TEMPLATEPATH . '/' . $template_name;
   662 			$located = TEMPLATEPATH . '/' . $template_name;
   639 			break;
   663 			break;
   640 		} elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
   664 		} elseif ( file_exists( ABSPATH . WPINC . '/theme-compat/' . $template_name ) ) {
   641 			$located = ABSPATH . WPINC . '/theme-compat/' . $template_name;
   665 			$located = ABSPATH . WPINC . '/theme-compat/' . $template_name;
   642 			break;
   666 			break;
   643 		}
   667 		}
   644 	}
   668 	}
   645 
   669 
   646 	if ( $load && '' != $located )
   670 	if ( $load && '' != $located ) {
   647 		load_template( $located, $require_once );
   671 		load_template( $located, $require_once );
       
   672 	}
   648 
   673 
   649 	return $located;
   674 	return $located;
   650 }
   675 }
   651 
   676 
   652 /**
   677 /**
   675  */
   700  */
   676 function load_template( $_template_file, $require_once = true ) {
   701 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;
   702 	global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
   678 
   703 
   679 	if ( is_array( $wp_query->query_vars ) ) {
   704 	if ( is_array( $wp_query->query_vars ) ) {
       
   705 		/*
       
   706 		 * This use of extract() cannot be removed. There are many possible ways that
       
   707 		 * templates could depend on variables that it creates existing, and no way to
       
   708 		 * detect and deprecate it.
       
   709 		 *
       
   710 		 * Passing the EXTR_SKIP flag is the safest option, ensuring globals and
       
   711 		 * function variables cannot be overwritten.
       
   712 		 */
       
   713 		// phpcs:ignore WordPress.PHP.DontExtract.extract_extract
   680 		extract( $wp_query->query_vars, EXTR_SKIP );
   714 		extract( $wp_query->query_vars, EXTR_SKIP );
   681 	}
   715 	}
   682 
   716 
   683 	if ( isset( $s ) ) {
   717 	if ( isset( $s ) ) {
   684 		$s = esc_attr( $s );
   718 		$s = esc_attr( $s );