<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[38624] trunk: Customize: Let `static_front_page` section be contextually active based on whether there are any published pages.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/38624">38624</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/38624","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>westonruter</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-09-20 00:46:54 +0000 (Tue, 20 Sep 2016)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/ticket/34923">#34923</a>.
Fixes <a href="https://core.trac.wordpress.org/ticket/38013">#38013</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminjscustomizenavmenusjs">trunk/src/wp-admin/js/customize-nav-menus.js</a></li>
<li><a href="#trunksrcwpincludesclasswpcustomizecontrolphp">trunk/src/wp-includes/class-wp-customize-control.php</a></li>
<li><a href="#trunksrcwpincludesclasswpcustomizemanagerphp">trunk/src/wp-includes/class-wp-customize-manager.php</a></li>
<li><a href="#trunktestsphpunittestscustomizemanagerphp">trunk/tests/phpunit/tests/customize/manager.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminjscustomizenavmenusjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/js/customize-nav-menus.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/js/customize-nav-menus.js      2016-09-19 16:45:44 UTC (rev 38623)
+++ trunk/src/wp-admin/js/customize-nav-menus.js        2016-09-20 00:46:54 UTC (rev 38624)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -104,6 +104,23 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                deferred.resolve( response );
</span><span class="cx" style="display: block; padding: 0 10px">                                api.Menus.insertedAutoDrafts.push( response.post_id );
</span><span class="cx" style="display: block; padding: 0 10px">                                api( 'nav_menus_created_posts' ).set( _.clone( api.Menus.insertedAutoDrafts ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                               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 ) );
+                                               }
+                                       } );
+                               }
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><span class="cx" style="display: block; padding: 0 10px">                } );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpcustomizecontrolphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-customize-control.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-customize-control.php      2016-09-19 16:45:44 UTC (rev 38623)
+++ trunk/src/wp-includes/class-wp-customize-control.php        2016-09-20 00:46:54 UTC (rev 38624)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -533,15 +533,24 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                        <span class="description customize-control-description"><?php echo $this->description; ?></span>
</span><span class="cx" style="display: block; padding: 0 10px">                                <?php endif; ?>
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                <?php $dropdown = wp_dropdown_pages(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         <?php
+                               $dropdown_name = '_customize-dropdown-pages-' . $this->id;
+                               $show_option_none = __( '&mdash; Select &mdash;' );
+                               $option_none_value = '0';
+                               $dropdown = wp_dropdown_pages(
</ins><span class="cx" style="display: block; padding: 0 10px">                                         array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                'name'              => '_customize-dropdown-pages-' . $this->id,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                         'name'              => $dropdown_name,
</ins><span class="cx" style="display: block; padding: 0 10px">                                                 'echo'              => 0,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                'show_option_none'  => __( '&mdash; Select &mdash;' ),
-                                               'option_none_value' => '0',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                         'show_option_none'  => $show_option_none,
+                                               'option_none_value' => $option_none_value,
</ins><span class="cx" style="display: block; padding: 0 10px">                                                 'selected'          => $this->value(),
</span><span class="cx" style="display: block; padding: 0 10px">                                        )
</span><span class="cx" style="display: block; padding: 0 10px">                                );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                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>';
+                               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                // Hackily add in the data link parameter.
</span><span class="cx" style="display: block; padding: 0 10px">                                $dropdown = str_replace( '<select', '<select ' . $this->get_link(), $dropdown );
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpcustomizemanagerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-customize-manager.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-customize-manager.php      2016-09-19 16:45:44 UTC (rev 38623)
+++ trunk/src/wp-includes/class-wp-customize-manager.php        2016-09-20 00:46:54 UTC (rev 38624)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2254,59 +2254,80 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                /* Static Front Page */
-               // #WP19627
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         /*
+                * 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.
+                */
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // 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.' ),
-                       ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $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' ),
+               ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $this->add_setting( 'show_on_front', array(
-                               'default'        => get_option( 'show_on_front' ),
-                               'capability'     => 'manage_options',
-                               'type'           => 'option',
-                       //      'theme_supports' => 'static-front-page',
-                       ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->add_setting( 'show_on_front', array(
+                       'default' => get_option( 'show_on_front' ),
+                       'capability' => 'manage_options',
+                       'type' => 'option',
+               ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $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' ),
-                               ),
-                       ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $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' ),
+                       ),
+               ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $this->add_setting( 'page_on_front', array(
-                               'type'       => 'option',
-                               'capability' => 'manage_options',
-                       //      'theme_supports' => 'static-front-page',
-                       ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->add_setting( 'page_on_front', array(
+                       'type'       => 'option',
+                       'capability' => 'manage_options',
+               ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $this->add_control( 'page_on_front', array(
-                               'label'      => __( 'Front page' ),
-                               'section'    => 'static_front_page',
-                               'type'       => 'dropdown-pages',
-                       ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->add_control( 'page_on_front', array(
+                       'label' => __( 'Front page' ),
+                       'section' => 'static_front_page',
+                       'type' => 'dropdown-pages',
+               ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $this->add_setting( 'page_for_posts', array(
-                               'type'           => 'option',
-                               'capability'     => 'manage_options',
-                       //      'theme_supports' => 'static-front-page',
-                       ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->add_setting( 'page_for_posts', array(
+                       'type' => 'option',
+                       'capability' => 'manage_options',
+               ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $this->add_control( 'page_for_posts', array(
-                               'label'      => __( 'Posts page' ),
-                               'section'    => 'static_front_page',
-                               'type'       => 'dropdown-pages',
-                       ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $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;
+                               }
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               return 0 !== count( get_pages() );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span></span></pre></div>
<a id="trunktestsphpunittestscustomizemanagerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/customize/manager.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/customize/manager.php   2016-09-19 16:45:44 UTC (rev 38623)
+++ trunk/tests/phpunit/tests/customize/manager.php     2016-09-20 00:46:54 UTC (rev 38624)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -399,6 +399,48 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * 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() );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Test the WP_Customize_Manager::register_dynamic_settings() method.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * This is similar to test_add_dynamic_settings, except the settings are passed via $_POST['customized'].
</span></span></pre>
</div>
</div>

</body>
</html>