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). See #38615, #38114. Fixes #39610. git-svn-id: https://develop.svn.wordpress.org/trunk@39924 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
7d2c104068
commit
4e8e97983c
@ -997,13 +997,19 @@ final class WP_Customize_Manager {
|
|||||||
wp_list_pluck( $posts, 'post_name' )
|
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.
|
// Re-use auto-draft starter content posts referenced in the current customized state.
|
||||||
$existing_starter_content_posts = array();
|
$existing_starter_content_posts = array();
|
||||||
if ( ! empty( $starter_content_auto_draft_post_ids ) ) {
|
if ( ! empty( $starter_content_auto_draft_post_ids ) ) {
|
||||||
$existing_posts_query = new WP_Query( array(
|
$existing_posts_query = new WP_Query( array(
|
||||||
'post__in' => $starter_content_auto_draft_post_ids,
|
'post__in' => $starter_content_auto_draft_post_ids,
|
||||||
'post_status' => 'auto-draft',
|
'post_status' => 'auto-draft',
|
||||||
'post_type' => 'any',
|
'post_type' => $post_types,
|
||||||
'posts_per_page' => -1,
|
'posts_per_page' => -1,
|
||||||
) );
|
) );
|
||||||
foreach ( $existing_posts_query->posts as $existing_post ) {
|
foreach ( $existing_posts_query->posts as $existing_post ) {
|
||||||
|
@ -786,7 +786,7 @@ final class WP_Customize_Nav_Menus {
|
|||||||
* @return WP_Post|WP_Error Inserted auto-draft post object or error.
|
* @return WP_Post|WP_Error Inserted auto-draft post object or error.
|
||||||
*/
|
*/
|
||||||
public function insert_auto_draft_post( $postarr ) {
|
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', __( 'Invalid post type.' ) );
|
return new WP_Error( 'unknown_post_type', __( 'Invalid post type.' ) );
|
||||||
}
|
}
|
||||||
if ( empty( $postarr['post_title'] ) ) {
|
if ( empty( $postarr['post_title'] ) ) {
|
||||||
|
@ -373,6 +373,10 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase {
|
|||||||
'post_title' => 'Custom',
|
'post_title' => 'Custom',
|
||||||
'thumbnail' => '{{waffles}}',
|
'thumbnail' => '{{waffles}}',
|
||||||
),
|
),
|
||||||
|
'unknown_cpt' => array(
|
||||||
|
'post_type' => 'unknown_cpt',
|
||||||
|
'post_title' => 'Unknown CPT',
|
||||||
|
),
|
||||||
),
|
),
|
||||||
'attachments' => array(
|
'attachments' => array(
|
||||||
'waffles' => 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]'] );
|
$this->assertEquals( array( 'text-2', 'meta-3' ), $changeset_values['sidebars_widgets[sidebar-1]'] );
|
||||||
|
|
||||||
$posts_by_name = array();
|
$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_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->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.' );
|
$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;
|
$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( '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( 'sample-page-template.php', get_page_template_slug( $posts_by_name['about'] ) );
|
||||||
$this->assertEquals( '', get_page_template_slug( $posts_by_name['blog'] ) );
|
$this->assertEquals( '', get_page_template_slug( $posts_by_name['blog'] ) );
|
||||||
|
@ -538,9 +538,13 @@ class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase {
|
|||||||
$this->assertInstanceOf( 'WP_Error', $r );
|
$this->assertInstanceOf( 'WP_Error', $r );
|
||||||
$this->assertEquals( 'unknown_post_type', $r->get_error_code() );
|
$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->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' ) );
|
$r = $menus->insert_auto_draft_post( array( 'post_status' => 'publish', 'post_title' => 'Bad', 'post_type' => 'post' ) );
|
||||||
$this->assertInstanceOf( 'WP_Error', $r );
|
$this->assertInstanceOf( 'WP_Error', $r );
|
||||||
|
Loading…
Reference in New Issue
Block a user