From ae8af5d0dd10b5bc9f871e7bb41e05c4c63235a0 Mon Sep 17 00:00:00 2001 From: nacin Date: Fri, 2 Mar 2012 18:56:54 +0000 Subject: [PATCH] Introduce get_page_template_slug( $id = null ) to return a page's template (like "showcase.php"). Returns false if post ID is not a page, and an empty string for the default page template. Use the function across core. props billerickson for initial patch. fixes #18750. git-svn-id: http://svn.automattic.com/wordpress/trunk@20075 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-xmlrpc-server.php | 2 +- wp-includes/post-template.php | 44 +++++++++++++++++--------- wp-includes/post.php | 3 +- wp-includes/template.php | 9 ++---- 4 files changed, 34 insertions(+), 24 deletions(-) diff --git a/wp-includes/class-wp-xmlrpc-server.php b/wp-includes/class-wp-xmlrpc-server.php index 7449c39866..9ed3c31493 100644 --- a/wp-includes/class-wp-xmlrpc-server.php +++ b/wp-includes/class-wp-xmlrpc-server.php @@ -1177,7 +1177,7 @@ class wp_xmlrpc_server extends IXR_Server { // Get the author info. $author = get_userdata($page->post_author); - $page_template = get_post_meta( $page->ID, '_wp_page_template', true ); + $page_template = get_page_template_slug( $page->ID ); if ( empty( $page_template ) ) $page_template = 'default'; diff --git a/wp-includes/post-template.php b/wp-includes/post-template.php index 3eed04df48..7a60672d25 100644 --- a/wp-includes/post-template.php +++ b/wp-includes/post-template.php @@ -487,7 +487,7 @@ function get_body_class( $class = '' ) { } if ( is_page_template() ) { $classes[] = 'page-template'; - $classes[] = 'page-template-' . sanitize_html_class( str_replace( '.', '-', get_post_meta( $page_id, '_wp_page_template', true ) ), '' ); + $classes[] = 'page-template-' . sanitize_html_class( str_replace( '.', '-', get_page_template_slug( $page_id ) ) ); } else { $classes[] = 'page-template-default'; } @@ -1241,29 +1241,43 @@ function get_the_password_form() { * @param string $template The specific template name if specific matching is required. * @return bool False on failure, true if success. */ -function is_page_template($template = '') { - if (!is_page()) { +function is_page_template( $template = '' ) { + if ( ! is_page() ) return false; - } - global $wp_query; + $page_template = get_page_template_slug( get_queried_object_id() ); - $page = $wp_query->get_queried_object(); - $custom_fields = get_post_custom_values('_wp_page_template',$page->ID); - $page_template = $custom_fields[0]; + if ( empty( $template ) ) + return (bool) $page_template; - // We have no argument passed so just see if a page_template has been specified - if ( empty( $template ) ) { - if ( !empty( $page_template ) and ( 'default' != $page_template ) ) { - return true; - } - } elseif ( $template == $page_template) { + if ( $template == $page_template ) + return true; + + if ( 'default' == $template && ! $page_template ) return true; - } return false; } +/** + * Get the specific template name for a page. + * + * @since 3.4.0 + * + * @param int $id The page ID to check. Defaults to the current post, when used in the loop. + * @return string|bool Page template filename. Returns an empty string when the default page template + * is in use. Returns false if the post is not a page. + */ +function get_page_template_slug( $post_id = null ) { + $post = get_post( $post_id ); + if ( 'page' != $post->post_type ) + return false; + $template = get_post_meta( $post->ID, '_wp_page_template', true ); + if ( ! $template || 'default' == $template ) + return ''; + return $template; +} + /** * Retrieve formatted date timestamp of a revision (linked to that revisions's page). * diff --git a/wp-includes/post.php b/wp-includes/post.php index 076bd9c323..3fcaa46edc 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -5338,5 +5338,4 @@ function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache ); } -} - +} \ No newline at end of file diff --git a/wp-includes/template.php b/wp-includes/template.php index bcf9a2e5b5..6f3e252ae3 100644 --- a/wp-includes/template.php +++ b/wp-includes/template.php @@ -225,20 +225,17 @@ function get_front_page_template() { */ function get_page_template() { $id = get_queried_object_id(); - $template = get_post_meta($id, '_wp_page_template', true); + $template = get_page_template_slug(); $pagename = get_query_var('pagename'); - if ( !$pagename && $id > 0 ) { + if ( ! $pagename && $id ) { // If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object $post = get_queried_object(); $pagename = $post->post_name; } - if ( 'default' == $template ) - $template = ''; - $templates = array(); - if ( !empty($template) && !validate_file($template) ) + if ( $template && 0 === validate_file( $template ) ) $templates[] = $template; if ( $pagename ) $templates[] = "page-$pagename.php";