Customize: Let static_front_page
section be contextually active based on whether there are any published pages.
If there are no pages when the customizer is opened, the `static_front_page` section will be hidden. As soon as a page is created in the customizer session, the `static_front_page` section will be revealed. Previously the section would not be registered if there were no pages. Page stubs created via nav menus will appear in the `dropdown-pages` controls for `page_for_posts` and `page_on_front`, and such page stubs will thus cause the `static_front_page` section to appear. Plugins that facilitate page creation in the customizer by filtering `get_pages` will also cause the section to appear. See #34923. Fixes #38013. git-svn-id: https://develop.svn.wordpress.org/trunk@38624 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
849c5de6d5
commit
7079459b77
@ -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 ) );
|
||||
}
|
||||
} );
|
||||
}
|
||||
}
|
||||
} );
|
||||
|
||||
|
@ -533,15 +533,24 @@ class WP_Customize_Control {
|
||||
<span class="description customize-control-description"><?php echo $this->description; ?></span>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php $dropdown = wp_dropdown_pages(
|
||||
<?php
|
||||
$dropdown_name = '_customize-dropdown-pages-' . $this->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( '<select id="%1$s" name="%1$s">', esc_attr( $dropdown_name ) );
|
||||
$dropdown .= sprintf( '<option value="%1$s">%2$s</option>', esc_attr( $option_none_value ), esc_html( $show_option_none ) );
|
||||
$dropdown .= '</select>';
|
||||
}
|
||||
|
||||
// Hackily add in the data link parameter.
|
||||
$dropdown = str_replace( '<select', '<select ' . $this->get_link(), $dropdown );
|
||||
|
@ -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() );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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.
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user