Customize: Allow custom post types to be used in starter content.

Changes `WP_Customize_Nav_Menus::insert_auto_draft_post()` so it can be invoked for a `post_type` that is not registered (yet).

Props westonruter.
Merges [39924] to the 4.7 branch.
See #38615, #38114.
Fixes #39610.


git-svn-id: https://develop.svn.wordpress.org/branches/4.7@40098 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Dion Hulse 2017-02-21 07:01:07 +00:00
parent cba5d45c90
commit bce41a8435
4 changed files with 20 additions and 6 deletions

View File

@ -997,13 +997,19 @@ final class WP_Customize_Manager {
wp_list_pluck( $posts, 'post_name' )
);
/*
* Obtain all post types referenced in starter content to use in query.
* This is needed because 'any' will not account for post types not yet registered.
*/
$post_types = array_filter( array_merge( array( 'attachment' ), wp_list_pluck( $posts, 'post_type' ) ) );
// Re-use auto-draft starter content posts referenced in the current customized state.
$existing_starter_content_posts = array();
if ( ! empty( $starter_content_auto_draft_post_ids ) ) {
$existing_posts_query = new WP_Query( array(
'post__in' => $starter_content_auto_draft_post_ids,
'post_status' => 'auto-draft',
'post_type' => 'any',
'post_type' => $post_types,
'posts_per_page' => -1,
) );
foreach ( $existing_posts_query->posts as $existing_post ) {

View File

@ -786,7 +786,7 @@ final class WP_Customize_Nav_Menus {
* @return WP_Post|WP_Error Inserted auto-draft post object or error.
*/
public function insert_auto_draft_post( $postarr ) {
if ( ! isset( $postarr['post_type'] ) || ! post_type_exists( $postarr['post_type'] ) ) {
if ( ! isset( $postarr['post_type'] ) ) {
return new WP_Error( 'unknown_post_type', __( 'Unknown post type' ) );
}
if ( empty( $postarr['post_title'] ) ) {

View File

@ -373,6 +373,10 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase {
'post_title' => 'Custom',
'thumbnail' => '{{waffles}}',
),
'unknown_cpt' => array(
'post_type' => 'unknown_cpt',
'post_title' => 'Unknown CPT',
),
),
'attachments' => array(
'waffles' => array(
@ -441,7 +445,7 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase {
$this->assertEquals( array( 'text-2', 'meta-3' ), $changeset_values['sidebars_widgets[sidebar-1]'] );
$posts_by_name = array();
$this->assertCount( 6, $changeset_values['nav_menus_created_posts'] );
$this->assertCount( 7, $changeset_values['nav_menus_created_posts'] );
$this->assertContains( $existing_published_home_page_id, $changeset_values['nav_menus_created_posts'], 'Expected reuse of non-auto-draft posts.' );
$this->assertContains( $existing_canola_attachment_id, $changeset_values['nav_menus_created_posts'], 'Expected reuse of non-auto-draft attachment.' );
$this->assertNotContains( $existing_auto_draft_about_page_id, $changeset_values['nav_menus_created_posts'], 'Expected non-reuse of auto-draft posts.' );
@ -461,7 +465,7 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase {
}
$posts_by_name[ $post_name ] = $post->ID;
}
$this->assertEquals( array( 'waffles', 'canola', 'home', 'about', 'blog', 'custom' ), array_keys( $posts_by_name ) );
$this->assertEquals( array( 'waffles', 'canola', 'home', 'about', 'blog', 'custom', 'unknown-cpt' ), array_keys( $posts_by_name ) );
$this->assertEquals( 'Custom', get_post( $posts_by_name['custom'] )->post_title );
$this->assertEquals( 'sample-page-template.php', get_page_template_slug( $posts_by_name['about'] ) );
$this->assertEquals( '', get_page_template_slug( $posts_by_name['blog'] ) );

View File

@ -538,9 +538,13 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
$this->assertInstanceOf( 'WP_Error', $r );
$this->assertEquals( 'unknown_post_type', $r->get_error_code() );
$r = $menus->insert_auto_draft_post( array( 'post_type' => 'fake' ) );
// Non-existent post types allowed as of #39610.
$r = $menus->insert_auto_draft_post( array( 'post_title' => 'Non-existent', 'post_type' => 'nonexistent' ) );
$this->assertInstanceOf( 'WP_Post', $r );
$r = $menus->insert_auto_draft_post( array( 'post_type' => 'post' ) );
$this->assertInstanceOf( 'WP_Error', $r );
$this->assertEquals( 'unknown_post_type', $r->get_error_code() );
$this->assertEquals( 'empty_title', $r->get_error_code() );
$r = $menus->insert_auto_draft_post( array( 'post_status' => 'publish', 'post_title' => 'Bad', 'post_type' => 'post' ) );
$this->assertInstanceOf( 'WP_Error', $r );