From bce41a8435ab52559f4dce6d0dd9d098f05fb8f5 Mon Sep 17 00:00:00 2001 From: Dion Hulse Date: Tue, 21 Feb 2017 07:01:07 +0000 Subject: [PATCH] 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 --- src/wp-includes/class-wp-customize-manager.php | 8 +++++++- src/wp-includes/class-wp-customize-nav-menus.php | 2 +- tests/phpunit/tests/customize/manager.php | 8 ++++++-- tests/phpunit/tests/customize/nav-menus.php | 8 ++++++-- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/class-wp-customize-manager.php b/src/wp-includes/class-wp-customize-manager.php index 7f3e5a5b2d..1b2d29dc3b 100644 --- a/src/wp-includes/class-wp-customize-manager.php +++ b/src/wp-includes/class-wp-customize-manager.php @@ -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 ) { diff --git a/src/wp-includes/class-wp-customize-nav-menus.php b/src/wp-includes/class-wp-customize-nav-menus.php index e3e9233ade..dc5db7d5dd 100644 --- a/src/wp-includes/class-wp-customize-nav-menus.php +++ b/src/wp-includes/class-wp-customize-nav-menus.php @@ -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'] ) ) { diff --git a/tests/phpunit/tests/customize/manager.php b/tests/phpunit/tests/customize/manager.php index 033fa49731..4590772ddc 100644 --- a/tests/phpunit/tests/customize/manager.php +++ b/tests/phpunit/tests/customize/manager.php @@ -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'] ) ); diff --git a/tests/phpunit/tests/customize/nav-menus.php b/tests/phpunit/tests/customize/nav-menus.php index e68ab0539a..77116305e2 100644 --- a/tests/phpunit/tests/customize/nav-menus.php +++ b/tests/phpunit/tests/customize/nav-menus.php @@ -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 );