diff --git a/src/wp-admin/js/customize-nav-menus.js b/src/wp-admin/js/customize-nav-menus.js index 6dcdadc3fc..6731c34b67 100644 --- a/src/wp-admin/js/customize-nav-menus.js +++ b/src/wp-admin/js/customize-nav-menus.js @@ -104,6 +104,23 @@ deferred.resolve( response ); api.Menus.insertedAutoDrafts.push( response.post_id ); api( 'nav_menus_created_posts' ).set( _.clone( api.Menus.insertedAutoDrafts ) ); + + if ( 'page' === params.post_type ) { + + // Activate static front page controls as this could be the first page created. + if ( api.section.has( 'static_front_page' ) ) { + api.section( 'static_front_page' ).activate(); + } + + // Add new page to dropdown-pages controls. + api.control.each( function( control ) { + var select; + if ( 'dropdown-pages' === control.params.type ) { + select = control.container.find( 'select[name^="_customize-dropdown-pages-"]' ); + select.append( new Option( params.post_title, response.post_id ) ); + } + } ); + } } } ); diff --git a/src/wp-includes/class-wp-customize-control.php b/src/wp-includes/class-wp-customize-control.php index 3f4877fd41..e8482b2df0 100644 --- a/src/wp-includes/class-wp-customize-control.php +++ b/src/wp-includes/class-wp-customize-control.php @@ -533,15 +533,24 @@ class WP_Customize_Control { description; ?> - id; + $show_option_none = __( '— Select —' ); + $option_none_value = '0'; + $dropdown = wp_dropdown_pages( array( - 'name' => '_customize-dropdown-pages-' . $this->id, + 'name' => $dropdown_name, 'echo' => 0, - 'show_option_none' => __( '— Select —' ), - 'option_none_value' => '0', + 'show_option_none' => $show_option_none, + 'option_none_value' => $option_none_value, 'selected' => $this->value(), ) ); + if ( empty( $dropdown ) ) { + $dropdown = sprintf( ''; + } // Hackily add in the data link parameter. $dropdown = str_replace( 'get_link(), $dropdown ); diff --git a/src/wp-includes/class-wp-customize-manager.php b/src/wp-includes/class-wp-customize-manager.php index 668989b574..e56683da1b 100644 --- a/src/wp-includes/class-wp-customize-manager.php +++ b/src/wp-includes/class-wp-customize-manager.php @@ -2254,59 +2254,80 @@ final class WP_Customize_Manager { } } - /* Static Front Page */ - // #WP19627 + /* + * Static Front Page + * See also https://core.trac.wordpress.org/ticket/19627 which introduces the the static-front-page theme_support. + * The following replicates behavior from options-reading.php. + */ - // Replicate behavior from options-reading.php and hide front page options if there are no pages - if ( get_pages() ) { - $this->add_section( 'static_front_page', array( - 'title' => __( 'Static Front Page' ), - // 'theme_supports' => 'static-front-page', - 'priority' => 120, - 'description' => __( 'Your theme supports a static front page.' ), - ) ); + $this->add_section( 'static_front_page', array( + 'title' => __( 'Static Front Page' ), + 'priority' => 120, + 'description' => __( 'Your theme supports a static front page.' ), + 'active_callback' => array( $this, 'has_published_pages' ), + ) ); - $this->add_setting( 'show_on_front', array( - 'default' => get_option( 'show_on_front' ), - 'capability' => 'manage_options', - 'type' => 'option', - // 'theme_supports' => 'static-front-page', - ) ); + $this->add_setting( 'show_on_front', array( + 'default' => get_option( 'show_on_front' ), + 'capability' => 'manage_options', + 'type' => 'option', + ) ); - $this->add_control( 'show_on_front', array( - 'label' => __( 'Front page displays' ), - 'section' => 'static_front_page', - 'type' => 'radio', - 'choices' => array( - 'posts' => __( 'Your latest posts' ), - 'page' => __( 'A static page' ), - ), - ) ); + $this->add_control( 'show_on_front', array( + 'label' => __( 'Front page displays' ), + 'section' => 'static_front_page', + 'type' => 'radio', + 'choices' => array( + 'posts' => __( 'Your latest posts' ), + 'page' => __( 'A static page' ), + ), + ) ); - $this->add_setting( 'page_on_front', array( - 'type' => 'option', - 'capability' => 'manage_options', - // 'theme_supports' => 'static-front-page', - ) ); + $this->add_setting( 'page_on_front', array( + 'type' => 'option', + 'capability' => 'manage_options', + ) ); - $this->add_control( 'page_on_front', array( - 'label' => __( 'Front page' ), - 'section' => 'static_front_page', - 'type' => 'dropdown-pages', - ) ); + $this->add_control( 'page_on_front', array( + 'label' => __( 'Front page' ), + 'section' => 'static_front_page', + 'type' => 'dropdown-pages', + ) ); - $this->add_setting( 'page_for_posts', array( - 'type' => 'option', - 'capability' => 'manage_options', - // 'theme_supports' => 'static-front-page', - ) ); + $this->add_setting( 'page_for_posts', array( + 'type' => 'option', + 'capability' => 'manage_options', + ) ); - $this->add_control( 'page_for_posts', array( - 'label' => __( 'Posts page' ), - 'section' => 'static_front_page', - 'type' => 'dropdown-pages', - ) ); + $this->add_control( 'page_for_posts', array( + 'label' => __( 'Posts page' ), + 'section' => 'static_front_page', + 'type' => 'dropdown-pages', + ) ); + } + + /** + * Return whether there are published pages. + * + * Used as active callback for static front page section and controls. + * + * @access private + * @since 4.7.0 + * + * @returns bool Whether there are published (or to be published) pages. + */ + public function has_published_pages() { + + $setting = $this->get_setting( 'nav_menus_created_posts' ); + if ( $setting ) { + foreach ( $setting->value() as $post_id ) { + if ( 'page' === get_post_type( $post_id ) ) { + return true; + } + } } + + return 0 !== count( get_pages() ); } /** diff --git a/tests/phpunit/tests/customize/manager.php b/tests/phpunit/tests/customize/manager.php index b8fc4b5e46..f3e4846094 100644 --- a/tests/phpunit/tests/customize/manager.php +++ b/tests/phpunit/tests/customize/manager.php @@ -398,6 +398,48 @@ class Tests_WP_Customize_Manager extends WP_UnitTestCase { $this->assertEquals( 'dynamic_bar_default', $manager->get_setting( 'bar' )->default, 'Expected dynamic setting bar to have default providd by filter.' ); } + /** + * Test WP_Customize_Manager::has_published_pages(). + * + * @ticket 38013 + * @covers WP_Customize_Manager::has_published_pages() + */ + function test_has_published_pages() { + foreach ( get_pages() as $page ) { + wp_delete_post( $page->ID, true ); + } + $this->assertFalse( $this->manager->has_published_pages() ); + + $this->factory()->post->create( array( 'post_type' => 'page', 'post_status' => 'private' ) ); + $this->assertFalse( $this->manager->has_published_pages() ); + + $this->factory()->post->create( array( 'post_type' => 'page', 'post_status' => 'publish' ) ); + $this->assertTrue( $this->manager->has_published_pages() ); + } + + /** + * Ensure that page stubs created via nav menus will cause has_published_pages to return true. + * + * @ticket 38013 + * @covers WP_Customize_Manager::has_published_pages() + */ + function test_has_published_pages_when_nav_menus_created_posts() { + foreach ( get_pages() as $page ) { + wp_delete_post( $page->ID, true ); + } + $this->assertFalse( $this->manager->has_published_pages() ); + + wp_set_current_user( $this->factory()->user->create( array( 'role' => 'editor' ) ) ); + $this->manager->nav_menus->customize_register(); + $setting_id = 'nav_menus_created_posts'; + $setting = $this->manager->get_setting( $setting_id ); + $this->assertInstanceOf( 'WP_Customize_Filter_Setting', $setting ); + $auto_draft_page = $this->factory()->post->create( array( 'post_type' => 'page', 'post_status' => 'auto-draft' ) ); + $this->manager->set_post_value( $setting_id, array( $auto_draft_page ) ); + $setting->preview(); + $this->assertTrue( $this->manager->has_published_pages() ); + } + /** * Test the WP_Customize_Manager::register_dynamic_settings() method. *