Customize: Allow page stubs to be created via dropdown-pages controls in the Static Front Page section.

This ability was previously added to nav menus via the available page items panel. The "Add New Page" button only appears when the `allow_addition` control param is supplied as `true`. Code is adapted from the Customize Posts feature plugin.

Props celloexpressions, westonruter.
See #38013, #34923.
Fixes #38164.


git-svn-id: https://develop.svn.wordpress.org/trunk@38906 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Weston Ruter
2016-10-25 06:30:27 +00:00
parent 32fce2093c
commit 4e8410a886
8 changed files with 243 additions and 42 deletions

View File

@@ -26,6 +26,7 @@ class Test_WP_Customize_Control extends WP_UnitTestCase {
*/
function setUp() {
parent::setUp();
wp_set_current_user( $this->factory()->user->create( array( 'role' => 'administrator' ) ) );
require_once( ABSPATH . WPINC . '/class-wp-customize-manager.php' );
// @codingStandardsIgnoreStart
$GLOBALS['wp_customize'] = new WP_Customize_Manager();
@@ -39,7 +40,6 @@ class Test_WP_Customize_Control extends WP_UnitTestCase {
* @see WP_Customize_Control::check_capabilities()
*/
function test_check_capabilities() {
wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
do_action( 'customize_register', $this->wp_customize );
$control = new WP_Customize_Control( $this->wp_customize, 'blogname', array(
'settings' => array( 'blogname' ),
@@ -77,6 +77,67 @@ class Test_WP_Customize_Control extends WP_UnitTestCase {
$this->assertTrue( $control->check_capabilities() );
}
/**
* @ticket 38164
*/
function test_dropdown_pages() {
do_action( 'customize_register', $this->wp_customize );
$this->assertInstanceOf( 'WP_Customize_Nav_Menus', $this->wp_customize->nav_menus );
$nav_menus_created_posts_setting = $this->wp_customize->get_setting( 'nav_menus_created_posts' );
$this->assertInstanceOf( 'WP_Customize_Filter_Setting', $nav_menus_created_posts_setting );
$page_on_front_control = $this->wp_customize->get_control( 'page_on_front' );
// Ensure the add-new-toggle is absent if allow_addition param is not set.
$page_on_front_control->allow_addition = false;
ob_start();
$page_on_front_control->maybe_render();
$content = ob_get_clean();
$this->assertNotContains( 'add-new-toggle', $content );
// Ensure the add-new-toggle is absent if allow_addition param is set.
$page_on_front_control->allow_addition = true;
ob_start();
$page_on_front_control->maybe_render();
$content = ob_get_clean();
$this->assertContains( 'add-new-toggle', $content );
// Ensure that dropdown-pages delect is rendered even if there are no pages published (yet).
foreach ( get_pages() as $page ) {
wp_delete_post( $page->ID );
}
$page_on_front_control->allow_addition = true;
ob_start();
$page_on_front_control->maybe_render();
$content = ob_get_clean();
$this->assertContains( '<option value="0">', $content, 'Dropdown-pages renders select even without any pages published.' );
// Ensure that auto-draft pages are included if they are among the nav_menus_created_posts.
$auto_draft_page_id = $this->factory()->post->create( array(
'post_type' => 'page',
'post_status' => 'auto-draft',
'post_title' => 'Auto Draft Page',
) );
$this->factory()->post->create( array(
'post_type' => 'page',
'post_status' => 'auto-draft',
'post_title' => 'Orphan Auto Draft Page',
) );
$auto_draft_post_id = $this->factory()->post->create( array(
'post_type' => 'post',
'post_status' => 'auto-draft',
'post_title' => 'Auto Draft Post',
) );
$this->wp_customize->set_post_value( $nav_menus_created_posts_setting->id, array( $auto_draft_page_id, $auto_draft_post_id ) );
$nav_menus_created_posts_setting->preview();
ob_start();
$page_on_front_control->maybe_render();
$content = ob_get_clean();
$this->assertContains( sprintf( '<option value="%d">Auto Draft Page</option>', $auto_draft_page_id ), $content );
$this->assertNotContains( 'Auto Draft Post', $content );
$this->assertNotContains( 'Orphan Auto Draft Page', $content );
}
/**
* Tear down.
*/