<!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>[41389] trunk/src: Customize: Show notification error with "Your homepage displays" control when homepage and posts page are set to be the same (but not empty).</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/41389">41389</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/41389","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>2017-09-19 00:45:23 +0000 (Tue, 19 Sep 2017)</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: Show notification error with "Your homepage displays" control when homepage and posts page are set to be the same (but not empty).

* Show global error notiafication when saving is blocked due to client-side setting invalidity.
* Refactor `wp.customize.Notifications#render()` to ensure a notification re-renders if its `message` or data changes but its `code` does not.

Props MatheusGimenez, sixhours, westonruter, karmatosed, aocean90, zoonini, michelleweber, melchoyce.
See <a href="https://core.trac.wordpress.org/ticket/35210">#35210</a>.
Fixes <a href="https://core.trac.wordpress.org/ticket/21492">#21492</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadmincsscustomizecontrolscss">trunk/src/wp-admin/css/customize-controls.css</a></li>
<li><a href="#trunksrcwpadminjscustomizecontrolsjs">trunk/src/wp-admin/js/customize-controls.js</a></li>
<li><a href="#trunksrcwpincludesscriptloaderphp">trunk/src/wp-includes/script-loader.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadmincsscustomizecontrolscss"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/css/customize-controls.css</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/css/customize-controls.css     2017-09-18 23:03:06 UTC (rev 41388)
+++ trunk/src/wp-admin/css/customize-controls.css       2017-09-19 00:45:23 UTC (rev 41389)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -841,6 +841,17 @@
</span><span class="cx" style="display: block; padding: 0 10px"> /* Style for custom settings */
</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">+ * Static front page
+ */
+
+#customize-control-show_on_front.has-error {
+       margin-bottom: 0;
+}
+#customize-control-show_on_front.has-error .customize-control-notifications-container {
+       margin-top:12px;
+}
+
+/**
</ins><span class="cx" style="display: block; padding: 0 10px">  * Dropdowns
</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="trunksrcwpadminjscustomizecontrolsjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/js/customize-controls.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/js/customize-controls.js       2017-09-18 23:03:06 UTC (rev 41388)
+++ trunk/src/wp-admin/js/customize-controls.js 2017-09-19 00:45:23 UTC (rev 41389)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -146,64 +146,45 @@
</span><span class="cx" style="display: block; padding: 0 10px">                render: function() {
</span><span class="cx" style="display: block; padding: 0 10px">                        var collection = this,
</span><span class="cx" style="display: block; padding: 0 10px">                                notifications,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                renderedNotificationContainers,
-                               prevRenderedCodes,
-                               nextRenderedCodes,
-                               addedCodes,
-                               removedCodes,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         previousNotificationsByCode = {},
</ins><span class="cx" style="display: block; padding: 0 10px">                                 listElement;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        // Short-circuit if there are no container to render into.
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( ! collection.container || ! collection.container.length ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                return;
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        listElement = collection.container.children( 'ul' ).first();
-                       if ( ! listElement.length ) {
-                               listElement = $( '<ul></ul>' );
-                               collection.container.append( listElement );
-                       }
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        notifications = collection.get( { sort: true } );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-                       renderedNotificationContainers = {};
-                       listElement.find( '> [data-code]' ).each( function() {
-                               renderedNotificationContainers[ $( this ).data( 'code' ) ] = $( this );
-                       });
-
</del><span class="cx" style="display: block; padding: 0 10px">                         collection.container.toggle( 0 !== notifications.length );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        nextRenderedCodes = _.pluck( notifications, 'code' );
-                       prevRenderedCodes = _.keys( renderedNotificationContainers );
-
-                       // Short-circuit if there are no notifications added.
-                       if ( _.isEqual( nextRenderedCodes, prevRenderedCodes ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Short-circuit if there are no changes to the notifications.
+                       if ( _.isEqual( notifications, collection.previousNotifications ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                                 return;
</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">-                        addedCodes = _.difference( nextRenderedCodes, prevRenderedCodes );
-                       removedCodes = _.difference( prevRenderedCodes, nextRenderedCodes );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Make sure list is part of the container.
+                       listElement = collection.container.children( 'ul' ).first();
+                       if ( ! listElement.length ) {
+                               listElement = $( '<ul></ul>' );
+                               collection.container.append( listElement );
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Remove notifications that have been removed.
-                       _.each( renderedNotificationContainers, function( renderedContainer, code ) {
-                               if ( -1 !== _.indexOf( removedCodes, code ) ) {
-                                       renderedContainer.remove(); // @todo Consider slideUp as enhancement.
-                               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Remove all notifications prior to re-rendering.
+                       listElement.find( '> [data-code]' ).remove();
+
+                       _.each( collection.previousNotifications, function( notification ) {
+                               previousNotificationsByCode[ notification.code ] = notification;
</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 all notifications in the sorted order.
</span><span class="cx" style="display: block; padding: 0 10px">                        _.each( notifications, function( notification ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                var notificationContainer = renderedNotificationContainers[ notification.code ];
-                               if ( notificationContainer ) {
-                                       listElement.append( notificationContainer );
-                               } else {
-                                       notificationContainer = $( notification.render() );
-                                       listElement.append( notificationContainer ); // @todo Consider slideDown() as enhancement.
-                                       if ( wp.a11y ) {
-                                               wp.a11y.speak( notification.message, 'assertive' );
-                                       }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         if ( wp.a11y && ( ! previousNotificationsByCode[ notification.code ] || ! _.isEqual( notification.message, previousNotificationsByCode[ notification.code ].message ) ) ) {
+                                       wp.a11y.speak( notification.message, 'assertive' );
</ins><span class="cx" style="display: block; padding: 0 10px">                                 }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                listElement.append( $( notification.render() ) ); // @todo Consider slideDown() as enhancement.
</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">+                        collection.previousNotifications = notifications;
</ins><span class="cx" style="display: block; padding: 0 10px">                         collection.trigger( 'rendered' );
</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">@@ -4644,9 +4625,10 @@
</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">                                submit = function () {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        var request, query, settingInvalidities = {}, latestRevision = api._latestRevision;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 var request, query, settingInvalidities = {}, latestRevision = api._latestRevision, errorCode = 'client_side_error';
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                        api.bind( 'change', captureSettingModifiedDuringSave );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                        api.notifications.remove( errorCode );
</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">                                         * Block saving if there are any settings that are marked as
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -4668,6 +4650,14 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                        if ( ! _.isEmpty( invalidControls ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                                _.values( invalidControls )[0][0].focus();
</span><span class="cx" style="display: block; padding: 0 10px">                                                api.unbind( 'change', captureSettingModifiedDuringSave );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                                               api.notifications.add( errorCode, new api.Notification( errorCode, {
+                                                       message: ( 1 === invalidSettings.length ? api.l10n.saveBlockedError.singular : api.l10n.saveBlockedError.plural ).replace( /%s/g, String( invalidSettings.length ) ),
+                                                       type: 'error',
+                                                       dismissible: true,
+                                                       saveFailure: true
+                                               } ) );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                                                 deferred.rejectWith( previewer, [
</span><span class="cx" style="display: block; padding: 0 10px">                                                        { setting_invalidities: settingInvalidities }
</span><span class="cx" style="display: block; padding: 0 10px">                                                ] );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5610,24 +5600,46 @@
</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">-                // Change previewed URL to the homepage when changing the page_on_front.
-               api( 'show_on_front', 'page_on_front', function( showOnFront, pageOnFront ) {
-                       var updatePreviewUrl = function() {
-                               if ( showOnFront() === 'page' && parseInt( pageOnFront(), 10 ) > 0 ) {
-                                       api.previewer.previewUrl.set( api.settings.url.home );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         // Add behaviors to the static front page controls.
+               api( 'show_on_front', 'page_on_front', 'page_for_posts', function( showOnFront, pageOnFront, pageForPosts ) {
+                       var handleChange = function() {
+                               var setting = this, pageOnFrontId, pageForPostsId, errorCode = 'show_on_front_page_collision';
+                               pageOnFrontId = parseInt( pageOnFront(), 10 );
+                               pageForPostsId = parseInt( pageForPosts(), 10 );
+
+                               if ( 'page' === showOnFront() ) {
+
+                                       // Change previewed URL to the homepage when changing the page_on_front.
+                                       if ( setting === pageOnFront && pageOnFrontId > 0 ) {
+                                               api.previewer.previewUrl.set( api.settings.url.home );
+                                       }
+
+                                       // Change the previewed URL to the selected page when changing the page_for_posts.
+                                       if ( setting === pageForPosts && pageForPostsId > 0 ) {
+                                               api.previewer.previewUrl.set( api.settings.url.home + '?page_id=' + pageForPostsId );
+                                       }
</ins><span class="cx" style="display: block; padding: 0 10px">                                 }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                               // Toggle notification when the homepage and posts page are both set and the same.
+                               if ( 'page' === showOnFront() && pageOnFrontId && pageForPostsId && pageOnFrontId === pageForPostsId ) {
+                                       showOnFront.notifications.add( errorCode, new api.Notification( errorCode, {
+                                               type: 'error',
+                                               message: api.l10n.pageOnFrontError
+                                       } ) );
+                               } else {
+                                       showOnFront.notifications.remove( errorCode );
+                               }
</ins><span class="cx" style="display: block; padding: 0 10px">                         };
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        showOnFront.bind( updatePreviewUrl );
-                       pageOnFront.bind( updatePreviewUrl );
-               });
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 showOnFront.bind( handleChange );
+                       pageOnFront.bind( handleChange );
+                       pageForPosts.bind( handleChange );
+                       handleChange.call( showOnFront, showOnFront() ); // Make sure initial notification is added after loading existing changeset.
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Change the previewed URL to the selected page when changing the page_for_posts.
-               api( 'page_for_posts', function( setting ) {
-                       setting.bind(function( pageId ) {
-                               pageId = parseInt( pageId, 10 );
-                               if ( pageId > 0 ) {
-                                       api.previewer.previewUrl.set( api.settings.url.home + '?page_id=' + pageId );
-                               }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Move notifications container to the bottom.
+                       api.control( 'show_on_front', function( showOnFrontControl ) {
+                               showOnFrontControl.deferred.embedded.done( function() {
+                                       showOnFrontControl.container.append( showOnFrontControl.getNotificationsContainerElement() );
+                               });
</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="trunksrcwpincludesscriptloaderphp"></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/script-loader.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/script-loader.php   2017-09-18 23:03:06 UTC (rev 41388)
+++ trunk/src/wp-includes/script-loader.php     2017-09-19 00:45:23 UTC (rev 41389)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -570,6 +570,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        _n_noop( 'There is %d error which must be fixed before you can save.', 'There are %d errors which must be fixed before you can save.' ),
</span><span class="cx" style="display: block; padding: 0 10px">                        array( 'singular', 'plural' )
</span><span class="cx" style="display: block; padding: 0 10px">                ),
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                'pageOnFrontError' => __( 'Homepage and posts page must be different.' ),
+               'saveBlockedError' => wp_array_slice_assoc(
+                       /* translators: placeholder is error count */
+                       _n_noop( 'Unable to save due to %s invalid setting.', 'Unable to save due to %s invalid settings.' ),
+                       array( 'singular', 'plural' )
+               ),
</ins><span class="cx" style="display: block; padding: 0 10px">         ) );
</span><span class="cx" style="display: block; padding: 0 10px">        $scripts->add( 'customize-selective-refresh', "/wp-includes/js/customize-selective-refresh$suffix.js", array( 'jquery', 'wp-util', 'customize-preview' ), false, 1 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre>
</div>
</div>

</body>
</html>