Permalink
Browse files

Customize: Let `static_front_page` section be contextually active bas…

…ed 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.

Built from https://develop.svn.wordpress.org/trunk@38624


git-svn-id: http://core.svn.wordpress.org/trunk@38567 1a063a9b-81f0-0310-95a4-ce76da25c4cd
  • Loading branch information...
1 parent 94ff571 commit d84c343cc6194950373678678a2020a070d8ed72 @westonruter westonruter committed Sep 20, 2016
@@ -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 ) );
+ }
+ } );
+ }
}
} );
Oops, something went wrong.
@@ -533,15 +533,24 @@ protected function render_content() {
<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 = __( '&mdash; Select &mdash;' );
+ $option_none_value = '0';
+ $dropdown = wp_dropdown_pages(
array(
- 'name' => '_customize-dropdown-pages-' . $this->id,
+ 'name' => $dropdown_name,
'echo' => 0,
- 'show_option_none' => __( '&mdash; Select &mdash;' ),
- '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 @@ public function register_controls() {
}
}
- /* Static Front Page */
- // #WP19627
-
- // 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.' ),
- ) );
+ /*
+ * 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.
+ */
- $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_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_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( 'show_on_front', array(
+ 'default' => get_option( 'show_on_front' ),
+ 'capability' => 'manage_options',
+ 'type' => 'option',
+ ) );
- $this->add_setting( 'page_on_front', array(
- 'type' => 'option',
- 'capability' => 'manage_options',
- // 'theme_supports' => 'static-front-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_control( 'page_on_front', array(
- 'label' => __( 'Front page' ),
- 'section' => 'static_front_page',
- 'type' => 'dropdown-pages',
- ) );
+ $this->add_setting( 'page_on_front', array(
+ 'type' => 'option',
+ 'capability' => 'manage_options',
+ ) );
- $this->add_setting( 'page_for_posts', array(
- 'type' => 'option',
- 'capability' => 'manage_options',
- // 'theme_supports' => 'static-front-page',
- ) );
+ $this->add_control( 'page_on_front', array(
+ 'label' => __( 'Front 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',
- ) );
+ $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',
+ ) );
+ }
+
+ /**
+ * 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() );
}
/**
@@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
-$wp_version = '4.7-alpha-38623';
+$wp_version = '4.7-alpha-38624';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

0 comments on commit d84c343

Please sign in to comment.