<!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>[32054] trunk/src: Customizer: Refresh nonces when a session expires and the user logs in again.</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/32054">32054</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/32054","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>ocean90</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2015-04-06 15:09:21 +0000 (Mon, 06 Apr 2015)</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'>Customizer: Refresh nonces when a session expires and the user logs in again.

This was broken since 4.0 and the introduction of user session tokens. The nonces are now tied to session tokens as opposed to user IDs, and thus they change with each re-login.
Custom nonces can be added through the `customize_refresh_nonces` filter. On a successful refresh request the JavaScript API will trigger a `nonce-refresh` event. See widget's update nonce as an example.

props westonruter for initial patch.
fixes <a href="https://core.trac.wordpress.org/ticket/31294">#31294</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminjscustomizecontrolsjs">trunk/src/wp-admin/js/customize-controls.js</a></li>
<li><a href="#trunksrcwpadminjscustomizewidgetsjs">trunk/src/wp-admin/js/customize-widgets.js</a></li>
<li><a href="#trunksrcwpincludesclasswpcustomizemanagerphp">trunk/src/wp-includes/class-wp-customize-manager.php</a></li>
<li><a href="#trunksrcwpincludesclasswpcustomizewidgetsphp">trunk/src/wp-includes/class-wp-customize-widgets.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<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       2015-04-06 01:20:23 UTC (rev 32053)
+++ trunk/src/wp-admin/js/customize-controls.js 2015-04-06 15:09:21 UTC (rev 32054)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2358,11 +2358,23 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        messenger.targetWindow( iframe[0].contentWindow );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        messenger.bind( 'login', function() {
-                               iframe.remove();
-                               messenger.destroy();
-                               delete previewer._login;
-                               deferred.resolve();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 messenger.bind( 'login', function () {
+                               var refreshNonces = previewer.refreshNonces();
+
+                               refreshNonces.always( function() {
+                                       iframe.remove();
+                                       messenger.destroy();
+                                       delete previewer._login;
+                               });
+
+                               refreshNonces.done( function() {
+                                       deferred.resolve();
+                               });
+
+                               refreshNonces.fail( function() {
+                                       previewer.cheatin();
+                                       deferred.reject();
+                               });
</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 this._login;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2370,6 +2382,28 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                cheatin: function() {
</span><span class="cx" style="display: block; padding: 0 10px">                        $( document.body ).empty().addClass('cheatin').append( '<p>' + api.l10n.cheatin + '</p>' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                },
+
+               refreshNonces: function() {
+                       var request, deferred = $.Deferred();
+
+                       deferred.promise();
+
+                       request = wp.ajax.post( 'customize_refresh_nonces', {
+                               wp_customize: 'on',
+                               theme: api.settings.theme.stylesheet
+                       });
+
+                       request.done( function( response ) {
+                               api.trigger( 'nonce-refresh', response );
+                               deferred.resolve();
+                       });
+
+                       request.fail( function() {
+                               deferred.reject();
+                       });
+
+                       return deferred;
</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="lines" style="display: block; padding: 0 10px; color: #888">@@ -2538,6 +2572,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $.extend( this.nonce, nonce );
</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">+                // Refresh the nonces if login sends updated nonces over.
+               api.bind( 'nonce-refresh', function( nonce ) {
+                       $.extend( api.settings.nonce, nonce );
+                       $.extend( api.previewer.nonce, nonce );
+               });
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Create Settings
</span><span class="cx" style="display: block; padding: 0 10px">                $.each( api.settings.settings, function( id, data ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        api.create( id, id, data.value, {
</span></span></pre></div>
<a id="trunksrcwpadminjscustomizewidgetsjs"></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-widgets.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/js/customize-widgets.js        2015-04-06 01:20:23 UTC (rev 32053)
+++ trunk/src/wp-admin/js/customize-widgets.js  2015-04-06 15:09:21 UTC (rev 32054)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1898,6 +1898,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                sidebar_widgets: api.Widgets.SidebarControl
</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">+        // Refresh the nonce if login sends updated nonces over.
+       api.bind( 'nonce-refresh', function( nonces ) {
+               api.Widgets.data.nonce = nonces['update-widget'];
+       });
+
</ins><span class="cx" style="display: block; padding: 0 10px">         /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Init Customizer for widgets.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpcustomizemanagerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-customize-manager.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-customize-manager.php      2015-04-06 01:20:23 UTC (rev 32053)
+++ trunk/src/wp-includes/class-wp-customize-manager.php        2015-04-06 15:09:21 UTC (rev 32054)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -91,8 +91,8 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                add_filter( 'wp_die_handler', array( $this, 'wp_die_handler' ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                add_action( 'setup_theme',  array( $this, 'setup_theme' ) );
-               add_action( 'wp_loaded',    array( $this, 'wp_loaded' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         add_action( 'setup_theme', array( $this, 'setup_theme' ) );
+               add_action( 'wp_loaded',   array( $this, 'wp_loaded' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Run wp_redirect_status late to make sure we override the status last.
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( 'wp_redirect_status', array( $this, 'wp_redirect_status' ), 1000 );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -105,7 +105,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                remove_action( 'admin_init', '_maybe_update_plugins' );
</span><span class="cx" style="display: block; padding: 0 10px">                remove_action( 'admin_init', '_maybe_update_themes' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                add_action( 'wp_ajax_customize_save', array( $this, 'save' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         add_action( 'wp_ajax_customize_save',           array( $this, 'save' ) );
+               add_action( 'wp_ajax_customize_refresh_nonces', array( $this, 'refresh_nonces' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( 'customize_register',                 array( $this, 'register_controls' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( 'customize_register',                 array( $this, 'register_dynamic_settings' ), 11 ); // allow code to create settings first
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -784,6 +785,34 @@
</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">+         * Refresh nonces for the current preview.
+        *
+        * @since 4.2.0
+        */
+       public function refresh_nonces() {
+               if ( ! $this->is_preview() ) {
+                       wp_send_json_error( 'not_preview' );
+               }
+
+               $nonces = array(
+                       'save'    => wp_create_nonce( 'save-customize_' . $this->get_stylesheet() ),
+                       'preview' => wp_create_nonce( 'preview-customize_' . $this->get_stylesheet() ),
+               );
+
+               /**
+                * Filter nonces for a customize_refresh_nonces AJAX request.
+                *
+                * @since 4.2.0
+                *
+                * @param array                $nonces Array of refreshed nonces for save and
+                *                                     preview actions.
+                * @param WP_Customize_Manager $this   WP_Customize_Manager instance.
+                */
+               $nonces = apply_filters( 'customize_refresh_nonces', $nonces, $this );
+               wp_send_json_success( $nonces );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Add a customize setting.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 3.4.0
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpcustomizewidgetsphp"></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-widgets.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-customize-widgets.php      2015-04-06 01:20:23 UTC (rev 32053)
+++ trunk/src/wp-includes/class-wp-customize-widgets.php        2015-04-06 15:09:21 UTC (rev 32054)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -95,6 +95,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( 'customize_controls_print_footer_scripts', array( $this, 'print_footer_scripts' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( 'customize_controls_print_footer_scripts', array( $this, 'output_widget_control_templates' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( 'customize_preview_init',                  array( $this, 'customize_preview_init' ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                add_filter( 'customize_refresh_nonces',                array( $this, 'refresh_nonces' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( 'dynamic_sidebar',                         array( $this, 'tally_rendered_widgets' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                add_filter( 'is_active_sidebar',                       array( $this, 'tally_sidebars_via_is_active_sidebar_calls' ), 10, 2 );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -889,6 +890,20 @@
</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">+         * Refresh nonce for widget updates.
+        *
+        * @since 4.2.0
+        * @access public
+        *
+        * @param  array $nonces Array of nonces.
+        * @return array $nonces Array of nonces.
+        */
+       public function refresh_nonces( $nonces ) {
+               $nonces['update-widget'] = wp_create_nonce( 'update-widget' );
+               return $nonces;
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * When previewing, make sure the proper previewing widgets are used.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * Because wp_get_sidebars_widgets() gets called early at init
</span></span></pre>
</div>
</div>

</body>
</html>