<!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>[39411] trunk: Customize: Reuse existing non-auto-draft posts and existing auto-draft posts in the customized state with matching slugs when applying starter content.</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/39411">39411</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/39411","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-12-02 00:24:19 +0000 (Fri, 02 Dec 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: Reuse existing non-auto-draft posts and existing auto-draft posts in the customized state with matching slugs when applying starter content.

* Updates `wp_unique_post_slug()` to ignore `auto-draft` posts. Prevents publishing multiple posts that have the same slugs from starter content.
* Fixes fatal error when attempting to save an header_image setting from a non-admin context.
* Fixes substituting attachment symbols in options and theme mods.
* Fixes applying starter content for header images and background images.

See <a href="https://core.trac.wordpress.org/ticket/38114">#38114</a>.
Fixes <a href="https://core.trac.wordpress.org/ticket/38928">#38928</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="#trunksrcwpincludescustomizeclasswpcustomizeheaderimagesettingphp">trunk/src/wp-includes/customize/class-wp-customize-header-image-setting.php</a></li>
<li><a href="#trunksrcwpincludespostphp">trunk/src/wp-includes/post.php</a></li>
<li><a href="#trunktestsphpunittestscustomizemanagerphp">trunk/tests/phpunit/tests/customize/manager.php</a></li>
<li><a href="#trunktestsphpunittestspostwpUniquePostSlugphp">trunk/tests/phpunit/tests/post/wpUniquePostSlug.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-12-02 00:06:59 UTC (rev 39410)
+++ trunk/src/wp-includes/class-wp-customize-manager.php        2016-12-02 00:24:19 UTC (rev 39411)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -971,6 +971,32 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $starter_content_auto_draft_post_ids = array_merge( $starter_content_auto_draft_post_ids, $changeset_data['nav_menus_created_posts']['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">+                // Make an index of all the posts needed and what their slugs are.
+               $needed_posts = array();
+               $attachments = $this->prepare_starter_content_attachments( $attachments );
+               foreach ( $attachments as $attachment ) {
+                       $key = 'attachment:' . $attachment['post_name'];
+                       $needed_posts[ $key ] = true;
+               }
+               foreach ( array_keys( $posts ) as $post_symbol ) {
+                       if ( empty( $posts[ $post_symbol ]['post_name'] ) && empty( $posts[ $post_symbol ]['post_title'] ) ) {
+                               unset( $posts[ $post_symbol ] );
+                               continue;
+                       }
+                       if ( empty( $posts[ $post_symbol ]['post_name'] ) ) {
+                               $posts[ $post_symbol ]['post_name'] = sanitize_title( $posts[ $post_symbol ]['post_title'] );
+                       }
+                       if ( empty( $posts[ $post_symbol ]['post_type'] ) ) {
+                               $posts[ $post_symbol ]['post_type'] = 'post';
+                       }
+                       $needed_posts[ $posts[ $post_symbol ]['post_type'] . ':' . $posts[ $post_symbol ]['post_name'] ] = true;
+               }
+               $all_post_slugs = array_merge(
+                       wp_list_pluck( $attachments, 'post_name' ),
+                       wp_list_pluck( $posts, 'post_name' )
+               );
+
+               // Re-use auto-draft starter content posts referenced in the current customized state.
</ins><span class="cx" style="display: block; padding: 0 10px">                 $existing_starter_content_posts = array();
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! empty( $starter_content_auto_draft_post_ids ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $existing_posts_query = new WP_Query( array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -984,50 +1010,32 @@
</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">+                // Re-use non-auto-draft posts.
+               if ( ! empty( $all_post_slugs ) ) {
+                       $existing_posts_query = new WP_Query( array(
+                               'post_name__in' => $all_post_slugs,
+                               'post_status' => array_diff( get_post_stati(), array( 'auto-draft' ) ),
+                               'post_type' => 'any',
+                               'number' => -1,
+                       ) );
+                       foreach ( $existing_posts_query->posts as $existing_post ) {
+                               $key = $existing_post->post_type . ':' . $existing_post->post_name;
+                               if ( isset( $needed_posts[ $key ] ) && ! isset( $existing_starter_content_posts[ $key ] ) ) {
+                                       $existing_starter_content_posts[ $key ] = $existing_post;
+                               }
+                       }
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Attachments are technically posts but handled differently.
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! empty( $attachments ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Such is The WordPress Way.
-                       require_once( ABSPATH . 'wp-admin/includes/file.php' );
-                       require_once( ABSPATH . 'wp-admin/includes/media.php' );
-                       require_once( ABSPATH . 'wp-admin/includes/image.php' );
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        $attachment_ids = array();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        foreach ( $attachments as $symbol => $attachment ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-                               // A file is required and URLs to files are not currently allowed.
-                               if ( empty( $attachment['file'] ) || preg_match( '#^https?://$#', $attachment['file'] ) ) {
-                                       continue;
-                               }
-
-                               $file_array = array();
-                               $file_path = null;
-                               if ( file_exists( $attachment['file'] ) ) {
-                                       $file_path = $attachment['file']; // Could be absolute path to file in plugin.
-                               } elseif ( is_child_theme() && file_exists( get_stylesheet_directory() . '/' . $attachment['file'] ) ) {
-                                       $file_path = get_stylesheet_directory() . '/' . $attachment['file'];
-                               } elseif ( file_exists( get_template_directory() . '/' . $attachment['file'] ) ) {
-                                       $file_path = get_template_directory() . '/' . $attachment['file'];
-                               } else {
-                                       continue;
-                               }
-                               $file_array['name'] = basename( $attachment['file'] );
-
-                               // Skip file types that are not recognized.
-                               $checked_filetype = wp_check_filetype( $file_array['name'] );
-                               if ( empty( $checked_filetype['type'] ) ) {
-                                       continue;
-                               }
-
-                               // Ensure post_name is set since not automatically derived from post_title for new auto-draft posts.
-                               if ( empty( $attachment['post_name'] ) ) {
-                                       if ( ! empty( $attachment['post_title'] ) ) {
-                                               $attachment['post_name'] = sanitize_title( $attachment['post_title'] );
-                                       } else {
-                                               $attachment['post_name'] = sanitize_title( preg_replace( '/\.\w+$/', '', $file_array['name'] ) );
-                                       }
-                               }
-
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $file_array = array(
+                                       'name' => $attachment['file_name'],
+                               );
+                               $file_path = $attachment['file_path'];
</ins><span class="cx" style="display: block; padding: 0 10px">                                 $attachment_id = null;
</span><span class="cx" style="display: block; padding: 0 10px">                                $attached_file = null;
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( isset( $existing_starter_content_posts[ 'attachment:' . $attachment['post_name'] ] ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1080,14 +1088,14 @@
</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">                                $attachment_ids[ $symbol ] = $attachment_id;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $starter_content_auto_draft_post_ids = array_merge( $starter_content_auto_draft_post_ids, array_values( $attachment_ids ) );
</del><span class="cx" style="display: block; padding: 0 10px">                         }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        $starter_content_auto_draft_post_ids = array_merge( $starter_content_auto_draft_post_ids, array_values( $attachment_ids ) );
</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><span class="cx" style="display: block; padding: 0 10px">                        foreach ( array_keys( $posts ) as $post_symbol ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                if ( empty( $posts[ $post_symbol ]['post_type'] ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         if ( empty( $posts[ $post_symbol ]['post_type'] ) || empty( $posts[ $post_symbol ]['post_name'] ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                                         continue;
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><span class="cx" style="display: block; padding: 0 10px">                                $post_type = $posts[ $post_symbol ]['post_type'];
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1209,8 +1217,14 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Options.
</span><span class="cx" style="display: block; padding: 0 10px">                foreach ( $options as $name => $value ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        if ( preg_match( '/^{{(?P<symbol>.+)}}$/', $value, $matches ) && isset( $posts[ $matches['symbol'] ] ) ) {
-                               $value = $posts[ $matches['symbol'] ]['ID'];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( preg_match( '/^{{(?P<symbol>.+)}}$/', $value, $matches ) ) {
+                               if ( isset( $posts[ $matches['symbol'] ] ) ) {
+                                       $value = $posts[ $matches['symbol'] ]['ID'];
+                               } elseif ( isset( $attachment_ids[ $matches['symbol'] ] ) ) {
+                                       $value = $attachment_ids[ $matches['symbol'] ];
+                               } else {
+                                       continue;
+                               }
</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">                        if ( empty( $changeset_data[ $name ] ) || ! empty( $changeset_data[ $name ]['starter_content'] ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1221,10 +1235,33 @@
</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="cx" style="display: block; padding: 0 10px">                foreach ( $theme_mods as $name => $value ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        if ( preg_match( '/^{{(?P<symbol>.+)}}$/', $value, $matches ) && isset( $posts[ $matches['symbol'] ] ) ) {
-                               $value = $posts[ $matches['symbol'] ]['ID'];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( preg_match( '/^{{(?P<symbol>.+)}}$/', $value, $matches ) ) {
+                               if ( isset( $posts[ $matches['symbol'] ] ) ) {
+                                       $value = $posts[ $matches['symbol'] ]['ID'];
+                               } elseif ( isset( $attachment_ids[ $matches['symbol'] ] ) ) {
+                                       $value = $attachment_ids[ $matches['symbol'] ];
+                               } else {
+                                       continue;
+                               }
</ins><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">+                        // Handle header image as special case since setting has a legacy format.
+                       if ( 'header_image' === $name ) {
+                               $name = 'header_image_data';
+                               $metadata = wp_get_attachment_metadata( $value );
+                               if ( empty( $metadata ) ) {
+                                       continue;
+                               }
+                               $value = array(
+                                       'attachment_id' => $value,
+                                       'url' => wp_get_attachment_url( $value ),
+                                       'height' => $metadata['height'],
+                                       'width' => $metadata['width'],
+                               );
+                       } elseif ( 'background_image' === $name ) {
+                               $value = wp_get_attachment_url( $value );
+                       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( empty( $changeset_data[ $name ] ) || ! empty( $changeset_data[ $name ]['starter_content'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                $this->set_post_value( $name, $value );
</span><span class="cx" style="display: block; padding: 0 10px">                                $this->pending_starter_content_settings_ids[] = $name;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1241,6 +1278,69 @@
</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">+         * Prepare starter content attachments.
+        *
+        * Ensure that the attachments are valid and that they have slugs and file name/path.
+        *
+        * @since 4.7.0
+        * @access private
+        *
+        * @param array $attachments Attachments.
+        * @return array Prepared attachments.
+        */
+       protected function prepare_starter_content_attachments( $attachments ) {
+               $prepared_attachments = array();
+               if ( empty( $attachments ) ) {
+                       return $prepared_attachments;
+               }
+
+               // Such is The WordPress Way.
+               require_once( ABSPATH . 'wp-admin/includes/file.php' );
+               require_once( ABSPATH . 'wp-admin/includes/media.php' );
+               require_once( ABSPATH . 'wp-admin/includes/image.php' );
+
+               foreach ( $attachments as $symbol => $attachment ) {
+
+                       // A file is required and URLs to files are not currently allowed.
+                       if ( empty( $attachment['file'] ) || preg_match( '#^https?://$#', $attachment['file'] ) ) {
+                               continue;
+                       }
+
+                       $file_path = null;
+                       if ( file_exists( $attachment['file'] ) ) {
+                               $file_path = $attachment['file']; // Could be absolute path to file in plugin.
+                       } elseif ( is_child_theme() && file_exists( get_stylesheet_directory() . '/' . $attachment['file'] ) ) {
+                               $file_path = get_stylesheet_directory() . '/' . $attachment['file'];
+                       } elseif ( file_exists( get_template_directory() . '/' . $attachment['file'] ) ) {
+                               $file_path = get_template_directory() . '/' . $attachment['file'];
+                       } else {
+                               continue;
+                       }
+                       $file_name = basename( $attachment['file'] );
+
+                       // Skip file types that are not recognized.
+                       $checked_filetype = wp_check_filetype( $file_name );
+                       if ( empty( $checked_filetype['type'] ) ) {
+                               continue;
+                       }
+
+                       // Ensure post_name is set since not automatically derived from post_title for new auto-draft posts.
+                       if ( empty( $attachment['post_name'] ) ) {
+                               if ( ! empty( $attachment['post_title'] ) ) {
+                                       $attachment['post_name'] = sanitize_title( $attachment['post_title'] );
+                               } else {
+                                       $attachment['post_name'] = sanitize_title( preg_replace( '/\.\w+$/', '', $file_name ) );
+                               }
+                       }
+
+                       $attachment['file_name'] = $file_name;
+                       $attachment['file_path'] = $file_path;
+                       $prepared_attachments[ $symbol ] = $attachment;
+               }
+               return $prepared_attachments;
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Save starter content changeset.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 4.7.0
</span></span></pre></div>
<a id="trunksrcwpincludescustomizeclasswpcustomizeheaderimagesettingphp"></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/customize/class-wp-customize-header-image-setting.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/customize/class-wp-customize-header-image-setting.php       2016-12-02 00:06:59 UTC (rev 39410)
+++ trunk/src/wp-includes/customize/class-wp-customize-header-image-setting.php 2016-12-02 00:24:19 UTC (rev 39411)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -29,6 +29,15 @@
</span><span class="cx" style="display: block; padding: 0 10px">        public function update( $value ) {
</span><span class="cx" style="display: block; padding: 0 10px">                global $custom_image_header;
</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 _custom_header_background_just_in_time() fails to initialize $custom_image_header when not is_admin().
+               if ( empty( $custom_image_header ) ) {
+                       require_once( ABSPATH . 'wp-admin/custom-header.php' );
+                       $args = get_theme_support( 'custom-header' );
+                       $admin_head_callback = isset( $args[0]['admin-head-callback'] ) ? $args[0]['admin-head-callback'] : null;
+                       $admin_preview_callback = isset( $args[0]['admin-preview-callback'] ) ? $args[0]['admin-preview-callback'] : null;
+                       $custom_image_header = new Custom_Image_Header( $admin_head_callback, $admin_preview_callback );
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // If the value doesn't exist (removed or random),
</span><span class="cx" style="display: block; padding: 0 10px">                // use the header_image value.
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! $value )
</span></span></pre></div>
<a id="trunksrcwpincludespostphp"></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/post.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/post.php    2016-12-02 00:06:59 UTC (rev 39410)
+++ trunk/src/wp-includes/post.php      2016-12-02 00:24:19 UTC (rev 39411)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3673,7 +3673,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( 'attachment' == $post_type ) {
</span><span class="cx" style="display: block; padding: 0 10px">                // Attachment slugs must be unique across all types.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_status != 'auto-draft' AND post_name = %s AND ID != %d LIMIT 1";
</ins><span class="cx" style="display: block; padding: 0 10px">                 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID ) );
</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">@@ -3701,7 +3701,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                 * Page slugs must be unique within their own trees. Pages are in a separate
</span><span class="cx" style="display: block; padding: 0 10px">                 * namespace than posts so page slugs are allowed to overlap post slugs.
</span><span class="cx" style="display: block; padding: 0 10px">                 */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( %s, 'attachment' ) AND ID != %d AND post_parent = %d LIMIT 1";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_status != 'auto-draft' AND post_name = %s AND post_type IN ( %s, 'attachment' ) AND ID != %d AND post_parent = %d LIMIT 1";
</ins><span class="cx" style="display: block; padding: 0 10px">                 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID, $post_parent ) );
</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">@@ -3725,7 +3725,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">        } else {
</span><span class="cx" style="display: block; padding: 0 10px">                // Post slugs must be unique across all posts.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_status != 'auto-draft' AND post_name = %s AND post_type = %s AND ID != %d LIMIT 1";
</ins><span class="cx" style="display: block; padding: 0 10px">                 $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Prevent new post slugs that could result in URLs that conflict with date archives.
</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-12-02 00:06:59 UTC (rev 39410)
+++ trunk/tests/phpunit/tests/customize/manager.php     2016-12-02 00:24:19 UTC (rev 39411)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -314,7 +314,27 @@
</span><span class="cx" style="display: block; padding: 0 10px">        function test_import_theme_starter_content() {
</span><span class="cx" style="display: block; padding: 0 10px">                wp_set_current_user( self::$admin_user_id );
</span><span class="cx" style="display: block; padding: 0 10px">                register_nav_menu( 'top', 'Top' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                add_theme_support( 'custom-logo' );
+               add_theme_support( 'custom-header' );
+               add_theme_support( 'custom-background' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $canola_file = DIR_TESTDATA . '/images/canola.jpg';
+               $existing_canola_attachment_id = self::factory()->attachment->create_object( $canola_file, 0, array(
+                       'post_mime_type' => 'image/jpeg',
+                       'post_type' => 'attachment',
+                       'post_name' => 'canola',
+               ) );
+               $existing_published_home_page_id = $this->factory()->post->create( array(
+                       'post_name' => 'home',
+                       'post_type' => 'page',
+                       'post_status' => 'publish'
+               ) );
+               $existing_auto_draft_about_page_id = $this->factory()->post->create( array(
+                       'post_name' => 'about',
+                       'post_type' => 'page',
+                       'post_status' => 'auto-draft'
+               ) );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 global $wp_customize;
</span><span class="cx" style="display: block; padding: 0 10px">                $wp_customize = new WP_Customize_Manager();
</span><span class="cx" style="display: block; padding: 0 10px">                $starter_content_config = array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -351,16 +371,22 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                'custom' => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                        'post_type' => 'post',
</span><span class="cx" style="display: block; padding: 0 10px">                                        'post_title' => 'Custom',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        'thumbnail' => '{{featured-image-logo}}',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 'thumbnail' => '{{waffles}}',
</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">                        'attachments' => array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'featured-image-logo' => array(
-                                       'post_title' => 'Featured Image',
-                                       'post_content' => 'Attachment Description',
-                                       'post_excerpt' => 'Attachment Caption',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'waffles' => array(
+                                       'post_title' => 'Waffles',
+                                       'post_content' => 'Waffles Attachment Description',
+                                       'post_excerpt' => 'Waffles Attachment Caption',
</ins><span class="cx" style="display: block; padding: 0 10px">                                         'file' => DIR_TESTDATA . '/images/waffles.jpg',
</span><span class="cx" style="display: block; padding: 0 10px">                                ),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                'canola' => array(
+                                       'post_title' => 'Canola',
+                                       'post_content' => 'Canola Attachment Description',
+                                       'post_excerpt' => 'Canola Attachment Caption',
+                                       'file' => DIR_TESTDATA . '/images/canola.jpg',
+                               ),
</ins><span class="cx" style="display: block; padding: 0 10px">                         ),
</span><span class="cx" style="display: block; padding: 0 10px">                        'options' => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'blogname' => 'Starter Content Title',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -369,6 +395,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                'page_on_front'  => '{{home}}',
</span><span class="cx" style="display: block; padding: 0 10px">                                'page_for_posts' => '{{blog}}',
</span><span class="cx" style="display: block; padding: 0 10px">                        ),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        'theme_mods' => array(
+                               'custom_logo' => '{{canola}}',
+                               'header_image' => '{{waffles}}',
+                               'background_image' => '{{waffles}}',
+                       ),
</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">                add_theme_support( 'starter-content', $starter_content_config );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -378,6 +409,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $expected_setting_ids = array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'blogname',
</span><span class="cx" style="display: block; padding: 0 10px">                        'blogdescription',
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        'custom_logo',
+                       'header_image_data',
+                       'background_image',
</ins><span class="cx" style="display: block; padding: 0 10px">                         'widget_text[2]',
</span><span class="cx" style="display: block; padding: 0 10px">                        'widget_meta[3]',
</span><span class="cx" style="display: block; padding: 0 10px">                        'sidebars_widgets[sidebar-1]',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -406,20 +440,29 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( array( 'text-2', 'meta-3' ), $changeset_values['sidebars_widgets[sidebar-1]'] );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $posts_by_name = array();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertCount( 5, $changeset_values['nav_menus_created_posts'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertCount( 6, $changeset_values['nav_menus_created_posts'] );
+               $this->assertContains( $existing_published_home_page_id, $changeset_values['nav_menus_created_posts'], 'Expected reuse of non-auto-draft posts.' );
+               $this->assertContains( $existing_canola_attachment_id, $changeset_values['nav_menus_created_posts'], 'Expected reuse of non-auto-draft attachment.' );
+               $this->assertNotContains( $existing_auto_draft_about_page_id, $changeset_values['nav_menus_created_posts'], 'Expected non-reuse of auto-draft posts.' );
</ins><span class="cx" style="display: block; padding: 0 10px">                 foreach ( $changeset_values['nav_menus_created_posts'] as $post_id ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $post = get_post( $post_id );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $this->assertEquals( 'auto-draft', $post->post_status );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( $post->ID === $existing_published_home_page_id ) {
+                               $this->assertEquals( 'publish', $post->post_status );
+                       } elseif ( $post->ID === $existing_canola_attachment_id ) {
+                               $this->assertEquals( 'inherit', $post->post_status );
+                       } else {
+                               $this->assertEquals( 'auto-draft', $post->post_status );
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px">                         $posts_by_name[ $post->post_name ] = $post->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->assertEquals( array( 'featured-image', 'home', 'about', 'blog', 'custom' ), array_keys( $posts_by_name ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertEquals( array( 'waffles', 'canola', 'home', 'about', 'blog', 'custom' ), array_keys( $posts_by_name ) );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertEquals( 'Custom', get_post( $posts_by_name['custom'] )->post_title );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( 'sample-page-template.php', get_page_template_slug( $posts_by_name['about'] ) );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( '', get_page_template_slug( $posts_by_name['blog'] ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertEquals( $posts_by_name['featured-image'], get_post_thumbnail_id( $posts_by_name['custom'] ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertEquals( $posts_by_name['waffles'], get_post_thumbnail_id( $posts_by_name['custom'] ) );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertEquals( '', get_post_thumbnail_id( $posts_by_name['blog'] ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $attachment_metadata = wp_get_attachment_metadata( $posts_by_name['featured-image'] );
-               $this->assertEquals( 'Featured Image', get_post( $posts_by_name['featured-image'] )->post_title );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $attachment_metadata = wp_get_attachment_metadata( $posts_by_name['waffles'] );
+               $this->assertEquals( 'Waffles', get_post( $posts_by_name['waffles'] )->post_title );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertArrayHasKey( 'file', $attachment_metadata );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertContains( 'waffles', $attachment_metadata['file'] );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -472,14 +515,29 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertArrayHasKey( 'starter_content', $changeset_data['blogdescription'] );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Publish.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $this->assertEmpty( get_custom_logo() );
+               $this->assertEmpty( get_header_image() );
+               $this->assertEmpty( get_background_image() );
+               $this->assertEmpty( get_theme_mod( 'custom_logo' ) );
+               $this->assertEmpty( get_theme_mod( 'header_image' ) );
+               $this->assertEmpty( get_theme_mod( 'background_image' ) );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertEquals( 'auto-draft', get_post( $posts_by_name['about'] )->post_status );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertEquals( 'auto-draft', get_post( $posts_by_name['featured-image'] )->post_status );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertEquals( 'auto-draft', get_post( $posts_by_name['waffles'] )->post_status );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertNotEquals( $changeset_data['blogname']['value'], get_option( 'blogname' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                $r = $wp_customize->save_changeset_post( array( 'status' => 'publish' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertInternalType( 'array', $r );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( 'publish', get_post( $posts_by_name['about'] )->post_status );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertEquals( 'inherit', get_post( $posts_by_name['featured-image'] )->post_status );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertEquals( 'inherit', get_post( $posts_by_name['waffles'] )->post_status );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertEquals( $changeset_data['blogname']['value'], get_option( 'blogname' ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $this->assertNotEmpty( get_theme_mod( 'custom_logo' ) );
+               $this->assertNotEmpty( get_theme_mod( 'header_image' ) );
+               $this->assertNotEmpty( get_theme_mod( 'background_image' ) );
+               $this->assertNotEmpty( get_custom_logo() );
+               $this->assertNotEmpty( get_header_image() );
+               $this->assertNotEmpty( get_background_image() );
+               $this->assertContains( 'canola', get_custom_logo() );
+               $this->assertContains( 'waffles', get_header_image() );
+               $this->assertContains( 'waffles', get_background_image() );
</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="trunktestsphpunittestspostwpUniquePostSlugphp"></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/post/wpUniquePostSlug.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/post/wpUniquePostSlug.php       2016-12-02 00:06:59 UTC (rev 39410)
+++ trunk/tests/phpunit/tests/post/wpUniquePostSlug.php 2016-12-02 00:24:19 UTC (rev 39411)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -347,4 +347,46 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $found = wp_unique_post_slug( 'embed', $p, 'publish', 'attachment', 0 );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( 'embed-2', $found );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * @ticket 38928
+        */
+       public function test_non_unique_slugs_for_existing_auto_draft_posts() {
+               $auto_draft_post_id = self::factory()->post->create( array(
+                       'post_type' => 'post',
+                       'post_name' => 'existing-post',
+                       'post_status' => 'auto-draft',
+               ) );
+               $auto_draft_page_id = self::factory()->post->create( array(
+                       'post_type' => 'page',
+                       'post_name' => 'existing-page',
+                       'post_status' => 'auto-draft',
+               ) );
+               $auto_draft_attachment_id = self::factory()->attachment->create_object( 'image.jpg', $auto_draft_page_id, array(
+                       'post_mime_type' => 'image/jpeg',
+                       'post_type' => 'attachment',
+                       'post_name' => 'existing-attachment',
+                       'post_status' => 'auto-draft',
+               ) );
+
+               $post_id = self::factory()->post->create( array( 'post_type' => 'post' ) );
+               $page_id = self::factory()->post->create( array( 'post_type' => 'page' ) );
+               $attachment_id = self::factory()->attachment->create_object( 'image2.jpg', $page_id, array(
+                       'post_mime_type' => 'image/jpeg',
+                       'post_type' => 'attachment',
+                       'post_name' => 'existing-image',
+               ) );
+
+               $this->assertEquals( 'existing-post', wp_unique_post_slug( 'existing-post', $post_id, 'publish', get_post_type( $post_id ), 0 ) );
+               wp_publish_post( $auto_draft_post_id );
+               $this->assertEquals( 'existing-post-2', wp_unique_post_slug( 'existing-post', $post_id, 'publish', get_post_type( $post_id ), 0 ) );
+
+               $this->assertEquals( 'existing-page', wp_unique_post_slug( 'existing-page', $page_id, 'publish', get_post_type( $page_id ), 0 ) );
+               wp_publish_post( $auto_draft_page_id );
+               $this->assertEquals( 'existing-page-2', wp_unique_post_slug( 'existing-page', $page_id, 'publish', get_post_type( $page_id ), 0 ) );
+
+               $this->assertEquals( 'existing-attachment', wp_unique_post_slug( 'existing-attachment', $attachment_id, 'publish', get_post_type( $attachment_id ), 0 ) );
+               wp_publish_post( $auto_draft_attachment_id );
+               $this->assertEquals( 'existing-attachment-2', wp_unique_post_slug( 'existing-attachment', $attachment_id, 'publish', get_post_type( $attachment_id ), 0 ) );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>