<!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>[39241] trunk/src/wp-includes: Customize: Allow starter content to apply in a new theme when switching from another theme containing changes.</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/39241">39241</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/39241","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-11-15 09:02:38 +0000 (Tue, 15 Nov 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: Allow starter content to apply in a new theme when switching from another theme containing changes.

* Ensure that starter content can apply from theme B after previewing starter content in theme A.
* Introduce new `starter_content` flag in changeset setting params which is used to capture whether a value is starter content and thus can be overridden.
* Create changeset up-front with `starter_content` flags instead of waiting for `AUTOSAVE_INTERVAL`.
* Eliminate instantiation of settings for widget instances in favor of directly calling `sanitize_widget_js_instance`. This eliminates issues with looking for widgets before they are registered.
* Ensure that non-placeholders (inline arrays instead of string references) can be supplied in starter content.
* Re-use auto-draft posts as starter content across theme switches.
* Introduce `starter_content` param for `WP_Customize_Manager::save_changeset_post()` which is `false` except when starter content is being loaded on a `fresh_site`.

See <a href="https://core.trac.wordpress.org/ticket/38114">#38114</a>.
Fixes <a href="https://core.trac.wordpress.org/ticket/38541">#38541</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpcustomizemanagerphp">trunk/src/wp-includes/class-wp-customize-manager.php</a></li>
<li><a href="#trunksrcwpincludesclasswpcustomizenavmenusphp">trunk/src/wp-includes/class-wp-customize-nav-menus.php</a></li>
<li><a href="#trunksrcwpincludesthemephp">trunk/src/wp-includes/theme.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<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-11-15 07:15:20 UTC (rev 39240)
+++ trunk/src/wp-includes/class-wp-customize-manager.php        2016-11-15 09:02:38 UTC (rev 39241)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -523,8 +523,12 @@
</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">-                // Import theme starter content for fresh installs when landing in the customizer and no existing changeset loaded.
-               if ( get_option( 'fresh_site' ) && 'customize.php' === $pagenow && ! $this->changeset_post_id() ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         /*
+                * Import theme starter content for fresh installs when landing in the customizer.
+                * Import starter content at after_setup_theme:100 so that any
+                * add_theme_support( 'starter-content' ) calls will have been made.
+                */
+               if ( get_option( 'fresh_site' ) && 'customize.php' === $pagenow ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         add_action( 'after_setup_theme', array( $this, 'import_theme_starter_content' ), 100 );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -885,9 +889,18 @@
</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">-         * Import theme starter content into post values.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Starter content setting IDs.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 4.7.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @access private
+        * @var array
+        */
+       protected $starter_content_settings_ids = array();
+
+       /**
+        * Import theme starter content into the customized state.
+        *
+        * @since 4.7.0
</ins><span class="cx" style="display: block; padding: 0 10px">          * @access public
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param array $starter_content Starter content. Defaults to `get_theme_starter_content()`.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -897,6 +910,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $starter_content = get_theme_starter_content();
</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">+                $changeset_data = array();
+               if ( $this->changeset_post_id() ) {
+                       $changeset_data = $this->get_changeset_post_data( $this->changeset_post_id() );
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $sidebars_widgets = isset( $starter_content['widgets'] ) && ! empty( $this->widgets ) ? $starter_content['widgets'] : array();
</span><span class="cx" style="display: block; padding: 0 10px">                $posts = isset( $starter_content['posts'] ) && ! empty( $this->nav_menus ) ? $starter_content['posts'] : array();
</span><span class="cx" style="display: block; padding: 0 10px">                $options = isset( $starter_content['options'] ) ? $starter_content['options'] : array();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -932,45 +950,113 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                $widget_id = sprintf( '%s-%d', $id_base, $max_widget_numbers[ $id_base ] );
</span><span class="cx" style="display: block; padding: 0 10px">                                $setting_id = sprintf( 'widget_%s[%d]', $id_base, $max_widget_numbers[ $id_base ] );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $class = 'WP_Customize_Setting';
-
-                               /** This filter is documented in wp-includes/class-wp-customize-manager.php */
-                               $args = apply_filters( 'customize_dynamic_setting_args', false, $setting_id );
-
-                               if ( false !== $args ) {
-
-                                       /** This filter is documented in wp-includes/class-wp-customize-manager.php */
-                                       $class = apply_filters( 'customize_dynamic_setting_class', $class, $setting_id, $args );
-
-                                       $setting = new $class( $this, $setting_id, $args );
-                                       $setting_value = call_user_func( $setting->sanitize_js_callback, $instance, $setting );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $setting_value = $this->widgets->sanitize_widget_js_instance( $instance );
+                               if ( empty( $changeset_data[ $setting_id ] ) || ! empty( $changeset_data[ $setting_id ]['starter_content'] ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                                         $this->set_post_value( $setting_id, $setting_value );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        $sidebar_widget_ids[] = $widget_id;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 $this->starter_content_settings_ids[] = $setting_id;
</ins><span class="cx" style="display: block; padding: 0 10px">                                 }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                $sidebar_widget_ids[] = $widget_id;
</ins><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">-                        $this->set_post_value( sprintf( 'sidebars_widgets[%s]', $sidebar_id ), $sidebar_widget_ids );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $setting_id = sprintf( 'sidebars_widgets[%s]', $sidebar_id );
+                       if ( empty( $changeset_data[ $setting_id ] ) || ! empty( $changeset_data[ $setting_id ]['starter_content'] ) ) {
+                               $this->set_post_value( $setting_id, $sidebar_widget_ids );
+                               $this->starter_content_settings_ids[] = $setting_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">                // Posts & pages.
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! empty( $posts ) ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        $nav_menus_created_posts = array();
+                       if ( ! empty( $changeset_data['nav_menus_created_posts']['value'] ) ) {
+                               $nav_menus_created_posts = $changeset_data['nav_menus_created_posts']['value'];
+                       }
+
+                       $existing_posts = array();
+                       if ( ! empty( $nav_menus_created_posts ) ) {
+                               $existing_posts_query = new WP_Query( array(
+                                       'post__in' => $nav_menus_created_posts,
+                                       'post_status' => 'auto-draft',
+                                       'post_type' => 'any',
+                                       'number' => -1,
+                               ) );
+                               foreach ( $existing_posts_query->posts as $existing_post ) {
+                                       $existing_posts[ $existing_post->post_type . ':' . $existing_post->post_name ] = $existing_post;
+                               }
+                       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         foreach ( array_keys( $posts ) as $post_symbol ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                if ( empty( $posts[ $post_symbol ]['post_type'] ) ) {
+                                       continue;
+                               }
+                               $post_type = $posts[ $post_symbol ]['post_type'];
+                               if ( ! empty( $posts[ $post_symbol ]['post_name'] ) ) {
+                                       $post_name = $posts[ $post_symbol ]['post_name'];
+                               } elseif ( ! empty( $posts[ $post_symbol ]['post_title'] ) ) {
+                                       $post_name = sanitize_title( $posts[ $post_symbol ]['post_title'] );
+                               } else {
+                                       continue;
+                               }
+
+                               // Use existing auto-draft post if one already exists with the same type and name.
+                               if ( isset( $existing_posts[ $post_type . ':' . $post_name ] ) ) {
+                                       $posts[ $post_symbol ]['ID'] = $existing_posts[ $post_type . ':' . $post_name ]->ID;
+                                       continue;
+                               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                                 $r = $this->nav_menus->insert_auto_draft_post( $posts[ $post_symbol ] );
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( $r instanceof WP_Post ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        $posts[ $post_symbol ]['ID'] = $r->ID;
</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">-                        $this->set_post_value( 'nav_menus_created_posts', wp_list_pluck( $posts, 'ID' ) ); // This is why nav_menus component is dependency for adding posts.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       // The nav_menus_created_posts setting is why nav_menus component is dependency for adding posts.
+                       $setting_id = 'nav_menus_created_posts';
+                       if ( empty( $changeset_data[ $setting_id ] ) || ! empty( $changeset_data[ $setting_id ]['starter_content'] ) ) {
+                               $nav_menus_created_posts = array_unique( array_merge( $nav_menus_created_posts, wp_list_pluck( $posts, 'ID' ) ) );
+                               $this->set_post_value( $setting_id, array_values( $nav_menus_created_posts ) );
+                               $this->starter_content_settings_ids[] = $setting_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">                // Nav menus.
</span><span class="cx" style="display: block; padding: 0 10px">                $placeholder_id = -1;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $reused_nav_menu_setting_ids = array();
</ins><span class="cx" style="display: block; padding: 0 10px">                 foreach ( $nav_menus as $nav_menu_location => $nav_menu ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $nav_menu_term_id = $placeholder_id--;
-                       $nav_menu_setting_id = sprintf( 'nav_menu[%d]', $nav_menu_term_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       $nav_menu_term_id = null;
+                       $nav_menu_setting_id = null;
+                       $matches = array();
+
+                       // Look for an existing placeholder menu with starter content to re-use.
+                       foreach ( $changeset_data as $setting_id => $setting_params ) {
+                               $can_reuse = (
+                                       ! empty( $setting_params['starter_content'] )
+                                       &&
+                                       ! in_array( $setting_id, $reused_nav_menu_setting_ids, true )
+                                       &&
+                                       preg_match( '#^nav_menu\[(?P<nav_menu_id>-?\d+)\]$#', $setting_id, $matches )
+                               );
+                               if ( $can_reuse ) {
+                                       $nav_menu_term_id = intval( $matches['nav_menu_id'] );
+                                       $nav_menu_setting_id = $setting_id;
+                                       $reused_nav_menu_setting_ids[] = $setting_id;
+                                       break;
+                               }
+                       }
+
+                       if ( ! $nav_menu_term_id ) {
+                               while ( isset( $changeset_data[ sprintf( 'nav_menu[%d]', $placeholder_id ) ] ) ) {
+                                       $placeholder_id--;
+                               }
+                               $nav_menu_term_id = $placeholder_id;
+                               $nav_menu_setting_id = sprintf( 'nav_menu[%d]', $placeholder_id );
+                       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         $this->set_post_value( $nav_menu_setting_id, array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'name' => isset( $nav_menu['name'] ) ? $nav_menu['name'] : $nav_menu_location,
</span><span class="cx" style="display: block; padding: 0 10px">                        ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        $this->starter_content_settings_ids[] = $nav_menu_setting_id;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        // @todo Add support for menu_item_parent.
</span><span class="cx" style="display: block; padding: 0 10px">                        $position = 0;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -994,10 +1080,18 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                } else {
</span><span class="cx" style="display: block; padding: 0 10px">                                        $nav_menu_item['object_id'] = 0;
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $this->set_post_value( $nav_menu_item_setting_id, $nav_menu_item );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                               if ( empty( $changeset_data[ $nav_menu_item_setting_id ] ) || ! empty( $changeset_data[ $nav_menu_item_setting_id ]['starter_content'] ) ) {
+                                       $this->set_post_value( $nav_menu_item_setting_id, $nav_menu_item );
+                                       $this->starter_content_settings_ids[] = $nav_menu_item_setting_id;
+                               }
</ins><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">-                        $this->set_post_value( sprintf( 'nav_menu_locations[%s]', $nav_menu_location ), $nav_menu_term_id );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $setting_id = sprintf( 'nav_menu_locations[%s]', $nav_menu_location );
+                       if ( empty( $changeset_data[ $setting_id ] ) || ! empty( $changeset_data[ $setting_id ]['starter_content'] ) ) {
+                               $this->set_post_value( $setting_id, $nav_menu_term_id );
+                               $this->starter_content_settings_ids[] = $setting_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">                // Options.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1005,7 +1099,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( preg_match( '/^{{(?P<symbol>.+)}}$/', $value, $matches ) && isset( $posts[ $matches['symbol'] ] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                $value = $posts[ $matches['symbol'] ]['ID'];
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $this->set_post_value( $name, $value );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       if ( empty( $changeset_data[ $name ] ) || ! empty( $changeset_data[ $name ]['starter_content'] ) ) {
+                               $this->set_post_value( $name, $value );
+                               $this->starter_content_settings_ids[] = $name;
+                       }
</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">                // Theme mods.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1013,11 +1111,41 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( preg_match( '/^{{(?P<symbol>.+)}}$/', $value, $matches ) && isset( $posts[ $matches['symbol'] ] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                $value = $posts[ $matches['symbol'] ]['ID'];
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $this->set_post_value( $name, $value );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       if ( empty( $changeset_data[ $name ] ) || ! empty( $changeset_data[ $name ]['starter_content'] ) ) {
+                               $this->set_post_value( $name, $value );
+                               $this->starter_content_settings_ids[] = $name;
+                       }
</ins><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( $this->starter_content_settings_ids ) ) {
+                       if ( did_action( 'customize_register' ) ) {
+                               $this->_save_starter_content_changeset();
+                       } else {
+                               add_action( 'customize_register', array( $this, '_save_starter_content_changeset' ), 1000 );
+                       }
+               }
</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><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Save starter content changeset.
+        *
+        * @since 4.7.0
+        * @access private
+        */
+       public function _save_starter_content_changeset() {
+
+               if ( empty( $this->starter_content_settings_ids ) ) {
+                       return;
+               }
+
+               $this->save_changeset_post( array(
+                       'data' => array_fill_keys( $this->starter_content_settings_ids, array( 'starter_content' => true ) ),
+                       'starter_content' => true,
+               ) );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Get dirty pre-sanitized setting values in the current customized state.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * The returned array consists of a merge of three sources:
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1823,11 +1951,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @param array $args {
</span><span class="cx" style="display: block; padding: 0 10px">         *     Args for changeset post.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         *     @type array  $data     Optional additional changeset data. Values will be merged on top of any existing post values.
-        *     @type string $status   Post status. Optional. If supplied, the save will be transactional and a post revision will be allowed.
-        *     @type string $title    Post title. Optional.
-        *     @type string $date_gmt Date in GMT. Optional.
-        *     @type int    $user_id  ID for user who is saving the changeset. Optional, defaults to the current user ID.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  *     @type array  $data            Optional additional changeset data. Values will be merged on top of any existing post values.
+        *     @type string $status          Post status. Optional. If supplied, the save will be transactional and a post revision will be allowed.
+        *     @type string $title           Post title. Optional.
+        *     @type string $date_gmt        Date in GMT. Optional.
+        *     @type int    $user_id         ID for user who is saving the changeset. Optional, defaults to the current user ID.
+        *     @type bool   $starter_content Whether the data is starter content. If false (default), then $starter_content will be cleared for any $data being saved.
</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">         * @return array|WP_Error Returns array on success and WP_Error with array data on error.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1841,6 +1970,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                'data' => array(),
</span><span class="cx" style="display: block; padding: 0 10px">                                'date_gmt' => null,
</span><span class="cx" style="display: block; padding: 0 10px">                                'user_id' => get_current_user_id(),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                'starter_content' => false,
</ins><span class="cx" style="display: block; padding: 0 10px">                         ),
</span><span class="cx" style="display: block; padding: 0 10px">                        $args
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1977,6 +2107,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( ! isset( $data[ $changeset_setting_id ] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        $data[ $changeset_setting_id ] = array();
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px">                                 $data[ $changeset_setting_id ] = array_merge(
</span><span class="cx" style="display: block; padding: 0 10px">                                        $data[ $changeset_setting_id ],
</span><span class="cx" style="display: block; padding: 0 10px">                                        $setting_params,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1985,6 +2116,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                                'user_id' => $args['user_id'],
</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">+
+                               // Clear starter_content flag in data if changeset is not explicitly being updated for starter content.
+                               if ( empty( $args['starter_content'] ) ) {
+                                       unset( $data[ $changeset_setting_id ]['starter_content'] );
+                               }
</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="trunksrcwpincludesclasswpcustomizenavmenusphp"></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-nav-menus.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-customize-nav-menus.php    2016-11-15 07:15:20 UTC (rev 39240)
+++ trunk/src/wp-includes/class-wp-customize-nav-menus.php      2016-11-15 09:02:38 UTC (rev 39241)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -696,6 +696,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->manager->add_setting( new WP_Customize_Filter_Setting( $this->manager, 'nav_menus_created_posts', array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'transport' => 'postMessage',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        'type' => 'option', // To prevent theme prefix in changeset.
</ins><span class="cx" style="display: block; padding: 0 10px">                         'default' => array(),
</span><span class="cx" style="display: block; padding: 0 10px">                        'sanitize_callback' => array( $this, 'sanitize_nav_menus_created_posts' ),
</span><span class="cx" style="display: block; padding: 0 10px">                ) ) );
</span></span></pre></div>
<a id="trunksrcwpincludesthemephp"></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/theme.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/theme.php   2016-11-15 07:15:20 UTC (rev 39240)
+++ trunk/src/wp-includes/theme.php     2016-11-15 09:02:38 UTC (rev 39241)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1824,26 +1824,26 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $config = array();
</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">-        $core_content = array (
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $core_content = array(
</ins><span class="cx" style="display: block; padding: 0 10px">                 'widgets' => array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'text_business_info' => array ( 'text', array (
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'text_business_info' => array( 'text', array(
</ins><span class="cx" style="display: block; padding: 0 10px">                                 'title' => _x( 'Find Us', 'Theme starter content' ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'text' => join( '', array (
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'text' => join( '', array(
</ins><span class="cx" style="display: block; padding: 0 10px">                                         '<p><strong>' . _x( 'Address', 'Theme starter content' ) . '</strong><br />',
</span><span class="cx" style="display: block; padding: 0 10px">                                        _x( '123 Main Street', 'Theme starter content' ) . '<br />' . _x( 'New York, NY 10001', 'Theme starter content' ) . '</p>',
</span><span class="cx" style="display: block; padding: 0 10px">                                        '<p><strong>' . _x( 'Hours', 'Theme starter content' ) . '</strong><br />',
</span><span class="cx" style="display: block; padding: 0 10px">                                        _x( 'Monday&mdash;Friday: 9:00AM&ndash;5:00PM', 'Theme starter content' ) . '<br />' . _x( 'Saturday &amp; Sunday: 11:00AM&ndash;3:00PM', 'Theme starter content' ) . '</p>'
</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">-                        'search' => array ( 'search', array (
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'search' => array( 'search', array(
</ins><span class="cx" style="display: block; padding: 0 10px">                                 'title' => _x( 'Site Search', 'Theme starter content' ),
</span><span class="cx" style="display: block; padding: 0 10px">                        ) ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'text_credits' => array ( 'text', array (
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'text_credits' => array( 'text', array(
</ins><span class="cx" style="display: block; padding: 0 10px">                                 'title' => _x( 'Site Credits', 'Theme starter content' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                'text' => sprintf( _x( 'This site was created on %s', 'Theme starter content' ), get_date_from_gmt( current_time( 'mysql', 1 ), 'c' ) ),
</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">-                'nav_menus' => array (
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         'nav_menus' => array(
</ins><span class="cx" style="display: block; padding: 0 10px">                         'page_home' => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'type' => 'post_type',
</span><span class="cx" style="display: block; padding: 0 10px">                                'object' => 'page',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1919,48 +1919,53 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        foreach ( $config as $type => $args ) {
</span><span class="cx" style="display: block; padding: 0 10px">                switch( $type ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Use options and theme_mods as-is
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Use options and theme_mods as-is.
</ins><span class="cx" style="display: block; padding: 0 10px">                         case 'options' :
</span><span class="cx" style="display: block; padding: 0 10px">                        case 'theme_mods' :
</span><span class="cx" style="display: block; padding: 0 10px">                                $content[ $type ] = $config[ $type ];
</span><span class="cx" style="display: block; padding: 0 10px">                                break;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Widgets are an extra level down due to groupings
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Widgets are grouped into sidebars.
</ins><span class="cx" style="display: block; padding: 0 10px">                         case 'widgets' :
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                foreach ( $config[ $type ] as $group => $items ) {
-                                       foreach ( $items as $id ) {
-                                               if ( ! empty( $core_content[ $type ] ) && ! empty( $core_content[ $type ][ $id ] ) ) {
-                                                       $content[ $type ][ $group ][ $id ] = $core_content[ $type ][ $id ];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         foreach ( $config[ $type ] as $sidebar_id => $widgets ) {
+                                       foreach ( $widgets as $widget ) {
+                                               if ( is_array( $widget ) ) {
+                                                       $content[ $type ][ $sidebar_id ][] = $widget;
+                                               } elseif ( is_string( $widget ) && ! empty( $core_content[ $type ] ) && ! empty( $core_content[ $type ][ $widget ] ) ) {
+                                                       $content[ $type ][ $sidebar_id ][] = $core_content[ $type ][ $widget ];
</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 class="cx" style="display: block; padding: 0 10px">                                break;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // And nav menus are yet another level down
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // And nav menu items are grouped into nav menus.
</ins><span class="cx" style="display: block; padding: 0 10px">                         case 'nav_menus' :
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                foreach ( $config[ $type ] as $group => $args2 ) {
-                                       // Menu groups need a name
-                                       if ( empty( $args['name'] ) ) {
-                                               $args2['name'] = $group;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         foreach ( $config[ $type ] as $nav_menu_location => $nav_menu ) {
+
+                                       // Ensure nav menus get a name.
+                                       if ( empty( $nav_menu['name'] ) ) {
+                                               $nav_menu['name'] = $nav_menu_location;
</ins><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">-                                        $content[ $type ][ $group ]['name'] = $args2['name'];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 $content[ $type ][ $nav_menu_location ]['name'] = $nav_menu['name'];
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        // Do we need to check if this is empty?
-                                       foreach ( $args2['items'] as $id ) {
-                                               if ( ! empty( $core_content[ $type ] ) && ! empty( $core_content[ $type ][ $id ] ) ) {
-                                                       $content[ $type ][ $group ]['items'][ $id ] = $core_content[ $type ][ $id ];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 foreach ( $nav_menu['items'] as $nav_menu_item ) {
+                                               if ( is_array( $nav_menu_item ) ) {
+                                                       $content[ $type ][ $nav_menu_location ]['items'][] = $nav_menu_item;
+                                               } elseif ( is_string( $nav_menu_item ) && ! empty( $core_content[ $type ] ) && ! empty( $core_content[ $type ][ $nav_menu_item ] ) ) {
+                                                       $content[ $type ][ $nav_menu_location ]['items'][] = $core_content[ $type ][ $nav_menu_item ];
</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 class="cx" style="display: block; padding: 0 10px">                                break;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-                       // Everything else should map at the next level
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Everything else should map at the next level.
</ins><span class="cx" style="display: block; padding: 0 10px">                         default :
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                foreach( $config[ $type ] as $id ) {
-                                       if ( ! empty( $core_content[ $type ] ) && ! empty( $core_content[ $type ][ $id ] ) ) {
-                                               $content[ $type ][ $id ] = $core_content[ $type ][ $id ];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         foreach( $config[ $type ] as $i => $item ) {
+                                       if ( is_array( $item ) ) {
+                                               $content[ $type ][ $i ] = $item;
+                                       } elseif ( is_string( $item ) && ! empty( $core_content[ $type ] ) && ! empty( $core_content[ $type ][ $item ] ) ) {
+                                               $content[ $type ][ $item ] = $core_content[ $type ][ $item ];
</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">                                break;
</span></span></pre>
</div>
</div>

</body>
</html>