<!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>[39332] trunk: Customize: Remove iframe-specific behaviors from customize preview when previewing on frontend and not contained inside iframe.</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/39332">39332</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/39332","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>westonruter</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-11-21 16:55:20 +0000 (Mon, 21 Nov 2016)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Customize: Remove iframe-specific behaviors from customize preview when previewing on frontend and not contained inside iframe.
* Strip out `customize_messenger_channel` from preview window URL when not contained in iframe.
* Allow interacting with unpreviewable links and forms when previewing customized state on frontend.
See <a href="https://core.trac.wordpress.org/ticket/30937">#30937</a>.
Fixes <a href="https://core.trac.wordpress.org/ticket/38867">#38867</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="#trunksrcwpincludesjscustomizepreviewjs">trunk/src/wp-includes/js/customize-preview.js</a></li>
<li><a href="#trunktestsphpunittestscustomizemanagerphp">trunk/tests/phpunit/tests/customize/manager.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpcustomizemanagerphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-customize-manager.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-customize-manager.php 2016-11-21 16:14:01 UTC (rev 39331)
+++ trunk/src/wp-includes/class-wp-customize-manager.php 2016-11-21 16:55:20 UTC (rev 39332)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1353,6 +1353,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> wp_enqueue_script( 'customize-preview' );
</span><span class="cx" style="display: block; padding: 0 10px"> add_action( 'wp_head', array( $this, 'customize_preview_loading_style' ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ add_action( 'wp_head', array( $this, 'remove_frameless_preview_messenger_channel' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> add_action( 'wp_footer', array( $this, 'customize_preview_settings' ), 20 );
</span><span class="cx" style="display: block; padding: 0 10px"> add_filter( 'get_edit_post_link', '__return_empty_string' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1488,6 +1489,44 @@
</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">+ * Remove customize_messenger_channel query parameter from the preview window when it is not in an iframe.
+ *
+ * This ensures that the admin bar will be shown. It also ensures that link navigation will
+ * work as expected since the parent frame is not being sent the URL to navigate to.
+ *
+ * @since 4.7.0
+ * @access public
+ */
+ public function remove_frameless_preview_messenger_channel() {
+ if ( ! $this->messenger_channel ) {
+ return;
+ }
+ ?>
+ <script>
+ ( function() {
+ var urlParser, oldQueryParams, newQueryParams, i;
+ if ( parent !== window ) {
+ return;
+ }
+ urlParser = document.createElement( 'a' );
+ urlParser.href = location.href;
+ oldQueryParams = urlParser.search.substr( 1 ).split( /&/ );
+ newQueryParams = [];
+ for ( i = 0; i < oldQueryParams.length; i += 1 ) {
+ if ( ! /^customize_messenger_channel=/.test( oldQueryParams[ i ] ) ) {
+ newQueryParams.push( oldQueryParams[ i ] );
+ }
+ }
+ urlParser.search = newQueryParams.join( '&' );
+ if ( urlParser.search !== location.search ) {
+ location.replace( urlParser.href );
+ }
+ } )();
+ </script>
+ <?php
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Print JavaScript settings for preview frame.
</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="trunksrcwpincludesjscustomizepreviewjs"></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/js/customize-preview.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/js/customize-preview.js 2016-11-21 16:14:01 UTC (rev 39331)
+++ trunk/src/wp-includes/js/customize-preview.js 2016-11-21 16:55:20 UTC (rev 39332)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -106,18 +106,18 @@
</span><span class="cx" style="display: block; padding: 0 10px"> preview.add( 'scheme', urlParser.protocol.replace( /:$/, '' ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> preview.body = $( document.body );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ preview.window = $( window );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- preview.body.on( 'click.preview', 'a', function( event ) {
- preview.handleLinkClick( event );
- } );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( api.settings.channel ) {
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- preview.body.on( 'submit.preview', 'form', function( event ) {
- preview.handleFormSubmit( event );
- } );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // If in an iframe, then intercept the link clicks and form submissions.
+ preview.body.on( 'click.preview', 'a', function( event ) {
+ preview.handleLinkClick( event );
+ } );
+ preview.body.on( 'submit.preview', 'form', function( event ) {
+ preview.handleFormSubmit( event );
+ } );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- preview.window = $( window );
-
- if ( api.settings.channel ) {
</del><span class="cx" style="display: block; padding: 0 10px"> preview.window.on( 'scroll.preview', debounce( function() {
</span><span class="cx" style="display: block; padding: 0 10px"> preview.send( 'scroll', preview.window.scrollTop() );
</span><span class="cx" style="display: block; padding: 0 10px"> }, 200 ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -158,11 +158,6 @@
</span><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">- // If not in an iframe, then allow the link click to proceed normally since the state query params are added.
- if ( ! api.settings.channel ) {
- return;
- }
-
</del><span class="cx" style="display: block; padding: 0 10px"> // Prevent initiating navigating from click and instead rely on sending url message to pane.
</span><span class="cx" style="display: block; padding: 0 10px"> event.preventDefault();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -199,11 +194,6 @@
</span><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">- // If not in an iframe, then allow the form submission to proceed normally with the state inputs injected.
- if ( ! api.settings.channel ) {
- return;
- }
-
</del><span class="cx" style="display: block; padding: 0 10px"> /*
</span><span class="cx" style="display: block; padding: 0 10px"> * If the default wasn't prevented already (in which case the form
</span><span class="cx" style="display: block; padding: 0 10px"> * submission is already being handled by JS), and if it has a GET
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -348,12 +338,16 @@
</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"> // Make sure links in preview use HTTPS if parent frame uses HTTPS.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( 'https' === api.preview.scheme.get() && 'http:' === element.protocol && -1 !== api.settings.url.allowedHosts.indexOf( element.host ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( api.settings.channel && 'https' === api.preview.scheme.get() && 'http:' === element.protocol && -1 !== api.settings.url.allowedHosts.indexOf( element.host ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> element.protocol = 'https:';
</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"> if ( ! api.isLinkPreviewable( element ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $( element ).addClass( 'customize-unpreviewable' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ // Style link as unpreviewable only if previewing in iframe; if previewing on frontend, links will be allowed to work normally.
+ if ( api.settings.channel ) {
+ $( element ).addClass( 'customize-unpreviewable' );
+ }
</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"> $( element ).removeClass( 'customize-unpreviewable' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -496,13 +490,17 @@
</span><span class="cx" style="display: block; padding: 0 10px"> urlParser.href = form.action;
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Make sure forms in preview use HTTPS if parent frame uses HTTPS.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( 'https' === api.preview.scheme.get() && 'http:' === urlParser.protocol && -1 !== api.settings.url.allowedHosts.indexOf( urlParser.host ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( api.settings.channel && 'https' === api.preview.scheme.get() && 'http:' === urlParser.protocol && -1 !== api.settings.url.allowedHosts.indexOf( urlParser.host ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> urlParser.protocol = 'https:';
</span><span class="cx" style="display: block; padding: 0 10px"> form.action = urlParser.href;
</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"> if ( 'GET' !== form.method.toUpperCase() || ! api.isLinkPreviewable( urlParser ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $( form ).addClass( 'customize-unpreviewable' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ // Style form as unpreviewable only if previewing in iframe; if previewing on frontend, all forms will be allowed to work normally.
+ if ( api.settings.channel ) {
+ $( form ).addClass( 'customize-unpreviewable' );
+ }
</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"> $( form ).removeClass( 'customize-unpreviewable' );
</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-11-21 16:14:01 UTC (rev 39331)
+++ trunk/tests/phpunit/tests/customize/manager.php 2016-11-21 16:55:20 UTC (rev 39332)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -460,6 +460,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( $did_action_customize_preview_init + 1, did_action( 'customize_preview_init' ) );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( 10, has_action( 'wp_head', 'wp_no_robots' ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEquals( 10, has_action( 'wp_head', array( $wp_customize, 'remove_frameless_preview_messenger_channel' ) ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( 10, has_filter( 'wp_headers', array( $wp_customize, 'filter_iframe_security_headers' ) ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEquals( 10, has_filter( 'wp_redirect', array( $wp_customize, 'add_state_query_params' ) ) );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertTrue( wp_script_is( 'customize-preview', 'enqueued' ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2036,6 +2037,27 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Test remove_frameless_preview_messenger_channel.
+ *
+ * @ticket 38867
+ * @covers WP_Customize_Manager::remove_frameless_preview_messenger_channel()
+ */
+ function test_remove_frameless_preview_messenger_channel() {
+ wp_set_current_user( self::$admin_user_id );
+ $manager = new WP_Customize_Manager( array( 'messenger_channel' => null ) );
+ ob_start();
+ $manager->remove_frameless_preview_messenger_channel();
+ $output = ob_get_clean();
+ $this->assertEmpty( $output );
+
+ $manager = new WP_Customize_Manager( array( 'messenger_channel' => 'preview-0' ) );
+ ob_start();
+ $manager->remove_frameless_preview_messenger_channel();
+ $output = ob_get_clean();
+ $this->assertContains( '<script>', $output );
+ }
+
+ /**
</ins><span class="cx" style="display: block; padding: 0 10px"> * Test customize_preview_settings() method.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @see WP_Customize_Manager::customize_preview_settings()
</span></span></pre>
</div>
</div>
</body>
</html>