diff --git src/wp-includes/class-wp-customize-manager.php src/wp-includes/class-wp-customize-manager.php
index 4862de4..f96a220 100644
|
|
final class WP_Customize_Manager { |
959 | 959 | // Posts & pages. |
960 | 960 | if ( ! empty( $posts ) ) { |
961 | 961 | foreach ( array_keys( $posts ) as $post_symbol ) { |
962 | | $posts[ $post_symbol ]['ID'] = wp_insert_post( wp_slash( array_merge( |
963 | | $posts[ $post_symbol ], |
964 | | array( 'post_status' => 'auto-draft' ) |
965 | | ) ) ); |
| 962 | $r = $this->nav_menus->insert_auto_draft_post( $posts[ $post_symbol ] ); |
| 963 | if ( $r instanceof WP_Post ) { |
| 964 | $posts[ $post_symbol ]['ID'] = $r->ID; |
| 965 | } |
966 | 966 | } |
967 | 967 | $this->set_post_value( 'nav_menus_created_posts', wp_list_pluck( $posts, 'ID' ) ); // This is why nav_menus component is dependency for adding posts. |
968 | 968 | } |
diff --git src/wp-includes/class-wp-customize-nav-menus.php src/wp-includes/class-wp-customize-nav-menus.php
index ec60c2f..66dd466 100644
|
|
final class WP_Customize_Nav_Menus { |
734 | 734 | * @since 4.7.0 |
735 | 735 | * |
736 | 736 | * @param array $postarr { |
737 | | * Abbreviated post array. |
| 737 | * Post array. Note that post_status is overridden to be `auto-draft`. |
738 | 738 | * |
739 | | * @var string $post_title Post title. |
740 | | * @var string $post_type Post type. |
| 739 | * @var string $post_title Post title. Required. |
| 740 | * @var string $post_type Post type. Required. |
| 741 | * @var string $post_name Post name. |
| 742 | * @var string $post_content Post content. |
741 | 743 | * } |
742 | 744 | * @return WP_Post|WP_Error Inserted auto-draft post object or error. |
743 | 745 | */ |
… |
… |
final class WP_Customize_Nav_Menus { |
745 | 747 | if ( ! isset( $postarr['post_type'] ) || ! post_type_exists( $postarr['post_type'] ) ) { |
746 | 748 | return new WP_Error( 'unknown_post_type', __( 'Unknown post type' ) ); |
747 | 749 | } |
748 | | if ( ! isset( $postarr['post_title'] ) ) { |
749 | | $postarr['post_title'] = ''; |
| 750 | if ( empty( $postarr['post_title'] ) ) { |
| 751 | return new WP_Error( 'empty_title', __( 'Empty title' ) ); |
| 752 | } |
| 753 | if ( ! empty( $postarr['post_status'] ) ) { |
| 754 | return new WP_Error( 'status_forbidden', __( 'Status is forbidden' ) ); |
| 755 | } |
| 756 | |
| 757 | $postarr['post_status'] = 'auto-draft'; |
| 758 | |
| 759 | // Auto-drafts are allowed to have empty post_names, so it has to be explicitly set. |
| 760 | if ( empty( $postarr['post_name'] ) ) { |
| 761 | $postarr['post_name'] = sanitize_title( $postarr['post_title'] ); |
750 | 762 | } |
751 | 763 | |
752 | 764 | add_filter( 'wp_insert_post_empty_content', '__return_false', 1000 ); |
753 | | $args = array( |
754 | | 'post_status' => 'auto-draft', |
755 | | 'post_type' => $postarr['post_type'], |
756 | | 'post_title' => $postarr['post_title'], |
757 | | 'post_name' => sanitize_title( $postarr['post_title'] ), // Auto-drafts are allowed to have empty post_names, so we need to explicitly set it. |
758 | | ); |
759 | | $r = wp_insert_post( wp_slash( $args ), true ); |
| 765 | $r = wp_insert_post( wp_slash( $postarr ), true ); |
760 | 766 | remove_filter( 'wp_insert_post_empty_content', '__return_false', 1000 ); |
761 | 767 | |
762 | 768 | if ( is_wp_error( $r ) ) { |
… |
… |
final class WP_Customize_Nav_Menus { |
785 | 791 | wp_send_json_error( 'missing_params', 400 ); |
786 | 792 | } |
787 | 793 | |
788 | | $params = wp_array_slice_assoc( |
789 | | array_merge( |
790 | | array( |
791 | | 'post_type' => '', |
792 | | 'post_title' => '', |
793 | | ), |
794 | | wp_unslash( $_POST['params'] ) |
| 794 | $params = wp_unslash( $_POST['params'] ); |
| 795 | $illegal_params = array_diff( array_keys( $params ), array( 'post_type', 'post_title' ) ); |
| 796 | if ( ! empty( $illegal_params ) ) { |
| 797 | wp_send_json_error( 'illegal_params', 400 ); |
| 798 | } |
| 799 | |
| 800 | $params = array_merge( |
| 801 | array( |
| 802 | 'post_type' => '', |
| 803 | 'post_title' => '', |
795 | 804 | ), |
796 | | array( 'post_type', 'post_title' ) |
| 805 | $params |
797 | 806 | ); |
798 | 807 | |
799 | 808 | if ( empty( $params['post_type'] ) || ! post_type_exists( $params['post_type'] ) ) { |
… |
… |
final class WP_Customize_Nav_Menus { |
1139 | 1148 | $post_ids = $setting->post_value(); |
1140 | 1149 | if ( ! empty( $post_ids ) ) { |
1141 | 1150 | foreach ( $post_ids as $post_id ) { |
1142 | | wp_publish_post( $post_id ); |
| 1151 | // Note that wp_publish_post() cannot be used because unique slugs need to be assigned. |
| 1152 | wp_update_post( array( 'ID' => $post_id, 'post_status' => 'publish' ) ); |
1143 | 1153 | } |
1144 | 1154 | } |
1145 | 1155 | } |
diff --git tests/phpunit/tests/ajax/CustomizeMenus.php tests/phpunit/tests/ajax/CustomizeMenus.php
index 28871b7..8614350 100644
|
|
class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase { |
547 | 547 | $this->assertTrue( $response['success'] ); |
548 | 548 | $this->assertArrayHasKey( 'post_id', $response['data'] ); |
549 | 549 | $this->assertArrayHasKey( 'url', $response['data'] ); |
| 550 | $post = get_post( $response['data']['post_id'] ); |
| 551 | $this->assertEquals( 'Hello World', $post->post_title ); |
| 552 | $this->assertEquals( 'post', $post->post_type ); |
| 553 | $this->assertEquals( 'hello-world', $post->post_name ); |
550 | 554 | } |
551 | 555 | |
552 | 556 | /** |
… |
… |
class Tests_Ajax_CustomizeMenus extends WP_Ajax_UnitTestCase { |
635 | 639 | $response = json_decode( $this->_last_response, true ); |
636 | 640 | $this->assertFalse( $response['success'] ); |
637 | 641 | $this->assertEquals( 'missing_post_title', $response['data'] ); |
| 642 | |
| 643 | // illegal_params. |
| 644 | $_POST = wp_slash( array( |
| 645 | 'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ), |
| 646 | 'params' => array( |
| 647 | 'post_type' => 'post', |
| 648 | 'post_title' => 'OK', |
| 649 | 'post_name' => 'bad', |
| 650 | 'post_content' => 'bad', |
| 651 | ), |
| 652 | ) ); |
| 653 | $this->_last_response = ''; |
| 654 | $this->make_ajax_call( 'customize-nav-menus-insert-auto-draft' ); |
| 655 | $response = json_decode( $this->_last_response, true ); |
| 656 | $this->assertFalse( $response['success'] ); |
| 657 | $this->assertEquals( 'illegal_params', $response['data'] ); |
638 | 658 | } |
639 | 659 | } |
diff --git tests/phpunit/tests/customize/nav-menus.php tests/phpunit/tests/customize/nav-menus.php
index 06e2333..f8b38a1 100644
|
|
class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { |
542 | 542 | $this->assertInstanceOf( 'WP_Error', $r ); |
543 | 543 | $this->assertEquals( 'unknown_post_type', $r->get_error_code() ); |
544 | 544 | |
| 545 | $r = $menus->insert_auto_draft_post( array( 'post_status' => 'publish', 'post_title' => 'Bad', 'post_type' => 'post' ) ); |
| 546 | $this->assertInstanceOf( 'WP_Error', $r ); |
| 547 | $this->assertEquals( 'status_forbidden', $r->get_error_code() ); |
| 548 | |
545 | 549 | $r = $menus->insert_auto_draft_post( array( 'post_title' => 'Hello World', 'post_type' => 'post' ) ); |
546 | 550 | $this->assertInstanceOf( 'WP_Post', $r ); |
547 | 551 | $this->assertEquals( 'Hello World', $r->post_title ); |
| 552 | $this->assertEquals( 'hello-world', $r->post_name ); |
548 | 553 | $this->assertEquals( 'post', $r->post_type ); |
549 | | $this->assertEquals( sanitize_title( $r->post_title ), $r->post_name ); |
| 554 | |
| 555 | $r = $menus->insert_auto_draft_post( array( 'post_title' => 'Hello World', 'post_type' => 'post', 'post_name' => 'greetings-world', 'post_content' => 'Hi World' ) ); |
| 556 | $this->assertInstanceOf( 'WP_Post', $r ); |
| 557 | $this->assertEquals( 'Hello World', $r->post_title ); |
| 558 | $this->assertEquals( 'post', $r->post_type ); |
| 559 | $this->assertEquals( 'greetings-world', $r->post_name ); |
| 560 | $this->assertEquals( 'Hi World', $r->post_content ); |
550 | 561 | } |
551 | 562 | |
552 | 563 | /** |
… |
… |
class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { |
731 | 742 | $post_ids = $this->factory()->post->create_many( 3, array( |
732 | 743 | 'post_status' => 'auto-draft', |
733 | 744 | 'post_type' => 'post', |
| 745 | 'post_name' => 'auto-draft', |
734 | 746 | ) ); |
735 | 747 | $pre_published_post_id = $this->factory()->post->create( array( 'post_status' => 'publish' ) ); |
736 | 748 | |
… |
… |
class Test_WP_Customize_Nav_Menus extends WP_UnitTestCase { |
750 | 762 | foreach ( $post_ids as $post_id ) { |
751 | 763 | $this->assertEquals( 'publish', get_post_status( $post_id ) ); |
752 | 764 | } |
| 765 | |
| 766 | // Ensure that unique slugs were assigned. |
| 767 | $posts = array_map( 'get_post', $post_ids ); |
| 768 | $post_names = wp_list_pluck( $posts, 'post_name' ); |
| 769 | $this->assertEqualSets( $post_names, array_unique( $post_names ) ); |
753 | 770 | } |
754 | 771 | |
755 | 772 | /** |