mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-11 20:30:23 +00:00
Posts, Post Types: Add support for post type templates.
WordPress has supported custom page templates for over 12 years, allowing developers to create various layouts for specific pages. While this feature is very helpful, it has always been limited to the 'page' post type and not was not available to other post types. By opening up the page template functionality to all post types, we continue to improve the template hierarchy's flexibility. In addition to the `Template Name` file header, the post types supported by a template can be specified using `Template Post Type: post, foo, bar`. When at least one template exists for a post type, the 'Post Attributes' meta box will be displayed in the back end, without the need to add post type support for `'page-attributes'`. 'Post Attributes' can be customized per post type using the `'attributes'` label when registering a post type. Props johnbillion, Mte90, dipesh.kakadiya, swissspidy. Fixes #18375. git-svn-id: https://develop.svn.wordpress.org/trunk@38951 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
/*
|
||||
Template Name: Sub Dir
|
||||
Template Post Type: foo, post
|
||||
*/
|
||||
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
/*
|
||||
Template Name: Top Level
|
||||
Template Post Type: foo, post
|
||||
*/
|
||||
@@ -48,21 +48,41 @@ class Tests_Admin_includesTheme extends WP_UnitTestCase {
|
||||
|
||||
switch_theme( $theme['Template'], $theme['Stylesheet'] );
|
||||
|
||||
$templates = get_page_templates();
|
||||
$this->assertCount( 3, $templates );
|
||||
$this->assertEquals( "template-top-level.php", $templates['Top Level'] );
|
||||
$this->assertEquals( "subdir/template-sub-dir.php", $templates['Sub Dir'] );
|
||||
$this->assertEquals( "template-header.php", $templates['This Template Header Is On One Line'] );
|
||||
$this->assertEqualSetsWithIndex( array(
|
||||
'Top Level' => 'template-top-level.php',
|
||||
'Sub Dir' => 'subdir/template-sub-dir.php',
|
||||
'This Template Header Is On One Line' => 'template-header.php',
|
||||
), get_page_templates() );
|
||||
|
||||
$theme = wp_get_theme( 'page-templates' );
|
||||
$this->assertNotEmpty( $theme );
|
||||
|
||||
switch_theme( $theme['Template'], $theme['Stylesheet'] );
|
||||
|
||||
$templates = get_page_templates();
|
||||
$this->assertCount( 3, $templates );
|
||||
$this->assertEquals( "template-top-level.php", $templates['Top Level'] );
|
||||
$this->assertEquals( "subdir/template-sub-dir.php", $templates['Sub Dir'] );
|
||||
$this->assertEquals( "template-header.php", $templates['This Template Header Is On One Line'] );
|
||||
$this->assertEqualSetsWithIndex( array(
|
||||
'Top Level' => 'template-top-level.php',
|
||||
'Sub Dir' => 'subdir/template-sub-dir.php',
|
||||
'This Template Header Is On One Line' => 'template-header.php',
|
||||
), get_page_templates() );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 18375
|
||||
*/
|
||||
function test_page_templates_different_post_types() {
|
||||
$theme = wp_get_theme( 'page-templates' );
|
||||
$this->assertNotEmpty( $theme );
|
||||
|
||||
switch_theme( $theme['Template'], $theme['Stylesheet'] );
|
||||
|
||||
$this->assertEqualSetsWithIndex( array(
|
||||
'Top Level' => 'template-top-level-post-types.php',
|
||||
'Sub Dir' => 'subdir/template-sub-dir-post-types.php',
|
||||
), get_page_templates( null, 'foo' ) );
|
||||
$this->assertEqualSetsWithIndex( array(
|
||||
'Top Level' => 'template-top-level-post-types.php',
|
||||
'Sub Dir' => 'subdir/template-sub-dir-post-types.php',
|
||||
), get_page_templates( null, 'post' ) );
|
||||
$this->assertEquals( array(), get_page_templates( null, 'bar' ) );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,4 +92,70 @@ class Tests_Post_GetBodyClass extends WP_UnitTestCase {
|
||||
$this->assertContains( "single-format-standard", $class );
|
||||
}
|
||||
|
||||
public function test_page_template_body_classes_no_template() {
|
||||
$post_id = self::factory()->post->create( array(
|
||||
'post_type' => 'page',
|
||||
) );
|
||||
$this->go_to( get_permalink( $post_id ) );
|
||||
|
||||
$class = get_body_class();
|
||||
|
||||
$this->assertNotContains( 'page-template', $class );
|
||||
$this->assertContains( 'page-template-default', $class );
|
||||
}
|
||||
|
||||
public function test_page_template_body_classes() {
|
||||
$post_id = self::factory()->post->create( array(
|
||||
'post_type' => 'page',
|
||||
) );
|
||||
|
||||
add_post_meta( $post_id, '_wp_page_template', 'templates/cpt.php' );
|
||||
|
||||
$this->go_to( get_permalink( $post_id ) );
|
||||
|
||||
$class = get_body_class();
|
||||
|
||||
$this->assertContains( 'page-template', $class );
|
||||
$this->assertContains( 'page-template-templates', $class );
|
||||
$this->assertContains( 'page-template-cpt', $class );
|
||||
$this->assertContains( 'page-template-templatescpt-php', $class );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 18375
|
||||
*/
|
||||
public function test_page_template_body_classes_attachment() {
|
||||
$post_id = self::factory()->post->create( array(
|
||||
'post_type' => 'attachment',
|
||||
) );
|
||||
|
||||
add_post_meta( $post_id, '_wp_page_template', 'templates/cpt.php' );
|
||||
|
||||
$this->go_to( get_permalink( $post_id ) );
|
||||
|
||||
$class = get_body_class();
|
||||
|
||||
$this->assertContains( 'attachment-template', $class );
|
||||
$this->assertContains( 'attachment-template-templates', $class );
|
||||
$this->assertContains( 'attachment-template-cpt', $class );
|
||||
$this->assertContains( 'attachment-template-templatescpt-php', $class );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 18375
|
||||
*/
|
||||
public function test_page_template_body_classes_post() {
|
||||
$post_id = self::factory()->post->create();
|
||||
|
||||
add_post_meta( $post_id, '_wp_page_template', 'templates/cpt.php' );
|
||||
|
||||
$this->go_to( get_permalink( $post_id ) );
|
||||
|
||||
$class = get_body_class();
|
||||
|
||||
$this->assertContains( 'post-template', $class );
|
||||
$this->assertContains( 'post-template-templates', $class );
|
||||
$this->assertContains( 'post-template-cpt', $class );
|
||||
$this->assertContains( 'post-template-templatescpt-php', $class );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,12 +145,6 @@ class Tests_Post_Objects extends WP_UnitTestCase {
|
||||
update_post_meta( $post_id, '_wp_page_template', 'foo.php' );
|
||||
$template = get_post_meta( $post->ID, '_wp_page_template', true );
|
||||
$this->assertEquals( 'foo.php', $template );
|
||||
// The post is not a page so the template is still empty
|
||||
$this->assertEquals( '', $post->page_template );
|
||||
|
||||
// Now the post is a page and should retrieve the template
|
||||
wp_update_post( array( 'ID' => $post->ID, 'post_type' => 'page' ) );
|
||||
$post = get_post( $post_id );
|
||||
$this->assertEquals( $template, $post->page_template );
|
||||
}
|
||||
|
||||
|
||||
@@ -271,16 +271,32 @@ NO;
|
||||
|
||||
/**
|
||||
* @ticket 31389
|
||||
* @ticket 18375
|
||||
*/
|
||||
public function test_get_page_template_slug_non_page() {
|
||||
$post_id = self::factory()->post->create( array(
|
||||
'post_type' => 'post',
|
||||
) );
|
||||
$post_id = self::factory()->post->create();
|
||||
|
||||
$this->assertFalse( get_page_template_slug( $post_id ) );
|
||||
$this->assertEquals( '', get_page_template_slug( $post_id ) );
|
||||
|
||||
update_post_meta( $post_id, '_wp_page_template', 'default' );
|
||||
|
||||
$this->assertEquals( '', get_page_template_slug( $post_id ) );
|
||||
|
||||
update_post_meta( $post_id, '_wp_page_template', 'example.php' );
|
||||
$this->assertEquals( 'example.php', get_page_template_slug( $post_id ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 18375
|
||||
*/
|
||||
public function test_get_page_template_slug_non_page_from_loop() {
|
||||
$post_id = self::factory()->post->create();
|
||||
|
||||
update_post_meta( $post_id, '_wp_page_template', 'example.php' );
|
||||
|
||||
$this->go_to( get_permalink( $post_id ) );
|
||||
$this->assertFalse( get_page_template_slug() );
|
||||
|
||||
$this->assertEquals( 'example.php', get_page_template_slug() );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1043,6 +1043,17 @@ class Tests_Query_Conditionals extends WP_UnitTestCase {
|
||||
$this->assertTrue( is_page_template( array('test.php', 'example.php') ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 18375
|
||||
*/
|
||||
function test_is_page_template_other_post_type() {
|
||||
$post_id = self::factory()->post->create( array( 'post_type' => 'post' ) );
|
||||
update_post_meta( $post_id, '_wp_page_template', 'example.php' );
|
||||
$this->go_to( get_post_permalink( $post_id ) );
|
||||
$this->assertFalse( is_page_template( array( 'test.php' ) ) );
|
||||
$this->assertTrue( is_page_template( array( 'test.php', 'example.php' ) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 35902
|
||||
*/
|
||||
|
||||
@@ -36,6 +36,7 @@ class Tests_Template extends WP_UnitTestCase {
|
||||
'post_date' => '1984-02-25 12:34:56',
|
||||
) );
|
||||
set_post_format( self::$post, 'quote' );
|
||||
add_post_meta( self::$post->ID, '_wp_page_template', 'templates/post.php' );
|
||||
}
|
||||
|
||||
public function setUp() {
|
||||
@@ -203,8 +204,12 @@ class Tests_Template extends WP_UnitTestCase {
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 18375
|
||||
*/
|
||||
public function test_single_template_hierarchy_for_post() {
|
||||
$this->assertTemplateHierarchy( get_permalink( self::$post ), array(
|
||||
'templates/post.php',
|
||||
'single-post-post-name-😀.php',
|
||||
'single-post-post-name-%f0%9f%98%80.php',
|
||||
'single-post.php',
|
||||
@@ -228,6 +233,26 @@ class Tests_Template extends WP_UnitTestCase {
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 18375
|
||||
*/
|
||||
public function test_single_template_hierarchy_for_custom_post_type_with_template() {
|
||||
$cpt = self::factory()->post->create_and_get( array(
|
||||
'post_type' => 'cpt',
|
||||
'post_name' => 'cpt-name-😀',
|
||||
) );
|
||||
add_post_meta( $cpt->ID, '_wp_page_template', 'templates/cpt.php' );
|
||||
|
||||
$this->assertTemplateHierarchy( get_permalink( $cpt ), array(
|
||||
'templates/cpt.php',
|
||||
'single-cpt-cpt-name-😀.php',
|
||||
'single-cpt-cpt-name-%f0%9f%98%80.php',
|
||||
'single-cpt.php',
|
||||
'single.php',
|
||||
'singular.php',
|
||||
) );
|
||||
}
|
||||
|
||||
public function test_attachment_template_hierarchy() {
|
||||
$attachment = self::factory()->attachment->create_and_get( array(
|
||||
'post_name' => 'attachment-name-😀',
|
||||
@@ -247,11 +272,37 @@ class Tests_Template extends WP_UnitTestCase {
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 18375
|
||||
*/
|
||||
public function test_attachment_template_hierarchy_with_template() {
|
||||
$attachment = self::factory()->attachment->create_and_get( array(
|
||||
'post_name' => 'attachment-name-😀',
|
||||
'file' => 'image.jpg',
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
) );
|
||||
|
||||
add_post_meta( $attachment, '_wp_page_template', 'templates/cpt.php' );
|
||||
|
||||
$this->assertTemplateHierarchy( get_permalink( $attachment ), array(
|
||||
'image-jpeg.php',
|
||||
'jpeg.php',
|
||||
'image.php',
|
||||
'attachment.php',
|
||||
'single-attachment-attachment-name-😀.php',
|
||||
'single-attachment-attachment-name-%f0%9f%98%80.php',
|
||||
'single-attachment.php',
|
||||
'single.php',
|
||||
'singular.php',
|
||||
) );
|
||||
}
|
||||
|
||||
public function test_embed_template_hierarchy_for_post() {
|
||||
$this->assertTemplateHierarchy( get_post_embed_url( self::$post ), array(
|
||||
'embed-post-quote.php',
|
||||
'embed-post.php',
|
||||
'embed.php',
|
||||
'templates/post.php',
|
||||
'single-post-post-name-😀.php',
|
||||
'single-post-post-name-%f0%9f%98%80.php',
|
||||
'single-post.php',
|
||||
|
||||
Reference in New Issue
Block a user