<!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>[34269] trunk: Customize: Reduce peak memory usage by JSON-encoding settings and controls individually.</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/34269">34269</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/34269","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>2015-09-17 19:41:35 +0000 (Thu, 17 Sep 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'>Customize: Reduce peak memory usage by JSON-encoding settings and controls individually.

When there are hundreds of settings and controls (e.g. nav menu items and widget instances) the resulting object that is JSON-encoded can become very large, and `wp_json_encode()` can consume a lot of memory to serialize it. By breaking down the serialization into multiple calls the peak memory usage can be kept in line.

Moves logic out of `wp-admin/customize.php` into the `WP_Customize_Manager` class with new methods:

 * `is_ios()`
 * `get_document_title_template()`
 * `get_preview_url()`/`set_preview_url()`
 * `get_return_url()`/`set_return_url()`
 * `get_autofocus()`/`set_autofocus()`
 * `customize_pane_settings()`

Includes unit tests for these methods, for which the logic was formerly untestable in `customize.php`.

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

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadmincustomizephp">trunk/src/wp-admin/customize.php</a></li>
<li><a href="#trunksrcwpincludesclasswpcustomizemanagerphp">trunk/src/wp-includes/class-wp-customize-manager.php</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="trunksrcwpadmincustomizephp"></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/customize.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/customize.php  2015-09-17 19:29:46 UTC (rev 34268)
+++ trunk/src/wp-admin/customize.php    2015-09-17 19:41:35 UTC (rev 34269)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -20,23 +20,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"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-wp_reset_vars( array( 'url', 'return' ) );
-$url = wp_unslash( $url );
-$url = wp_validate_redirect( $url, home_url( '/' ) );
-if ( $return ) {
-       $return = wp_unslash( $return );
-       $return = remove_query_arg( wp_removable_query_args(), $return );
-       $return = wp_validate_redirect( $return );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+wp_reset_vars( array( 'url', 'return', 'autofocus' ) );
+if ( ! empty( $url ) ) {
+       $wp_customize->set_preview_url( wp_unslash( $url ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-if ( ! $return ) {
-       if ( $url ) {
-               $return = $url;
-       } elseif ( current_user_can( 'edit_theme_options' ) || current_user_can( 'switch_themes' ) ) {
-               $return = admin_url( 'themes.php' );
-       } else {
-               $return = admin_url();
-       }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+if ( ! empty( $return ) ) {
+       $wp_customize->set_return_url( wp_unslash( $return ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+if ( ! empty( $autofocus ) && is_array( $autofocus ) ) {
+       $wp_customize->set_autofocus( wp_unslash( $autofocus ) );
+}
</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">  * @global WP_Scripts           $wp_scripts
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -83,9 +76,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        ?><meta name="viewport" id="viewport-meta" content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=1.2" /><?php
</span><span class="cx" style="display: block; padding: 0 10px"> endif;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-$is_ios = wp_is_mobile() && preg_match( '/iPad|iPod|iPhone/', $_SERVER['HTTP_USER_AGENT'] );
-
-if ( $is_ios ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+if ( $wp_customize->is_ios() ) {
</ins><span class="cx" style="display: block; padding: 0 10px">         $body_class .= ' ios';
</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">@@ -94,18 +85,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> $body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_locale() ) ) );
</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 ( $wp_customize->is_theme_active() ) {
-       $document_title_tmpl = _x( 'Customize: %s', 'Placeholder is the document title from the preview' );
-} else {
-       $document_title_tmpl = _x( 'Live Preview: %s', 'Placeholder is the document title from the preview' );
-}
-$document_title_tmpl = html_entity_decode( $document_title_tmpl, ENT_QUOTES, 'UTF-8' ); // because exported to JS and assigned to document.title
-$admin_title = sprintf( $document_title_tmpl, __( 'Loading&hellip;' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+$admin_title = sprintf( $wp_customize->get_document_title_template(), __( 'Loading&hellip;' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> ?><title><?php echo $admin_title; ?></title>
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> <script type="text/javascript">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-var ajaxurl = '<?php echo admin_url( 'admin-ajax.php', 'relative' ); ?>';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+var ajaxurl = <?php echo wp_json_encode( admin_url( 'admin-ajax.php', 'relative' ) ); ?>;
</ins><span class="cx" style="display: block; padding: 0 10px"> </script>
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -137,7 +122,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                <span class="controls"><?php _e( 'Customize' ); ?></span>
</span><span class="cx" style="display: block; padding: 0 10px">                                <span class="preview"><?php _e( 'Preview' ); ?></span>
</span><span class="cx" style="display: block; padding: 0 10px">                        </a>
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        <a class="customize-controls-close" href="<?php echo esc_url( $return ); ?>">
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 <a class="customize-controls-close" href="<?php echo esc_url( $wp_customize->get_return_url() ); ?>">
</ins><span class="cx" style="display: block; padding: 0 10px">                                 <span class="screen-reader-text"><?php _e( 'Cancel' ); ?></span>
</span><span class="cx" style="display: block; padding: 0 10px">                        </a>
</span><span class="cx" style="display: block; padding: 0 10px">                </div>
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -172,134 +157,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">        <div id="customize-preview" class="wp-full-overlay-main"></div>
</span><span class="cx" style="display: block; padding: 0 10px">        <?php
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        // Render Panel, Section, and Control templates.
-       $wp_customize->render_panel_templates();
-       $wp_customize->render_section_templates();
-       $wp_customize->render_control_templates();
-
</del><span class="cx" style="display: block; padding: 0 10px">         /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * Print Customizer control scripts in the footer.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Print templates, control scripts, and settings in the footer.
</ins><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 class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        do_action( 'customize_controls_print_footer_scripts' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-       /*
-        * If the frontend and the admin are served from the same domain, load the
-        * preview over ssl if the Customizer is being loaded over ssl. This avoids
-        * insecure content warnings. This is not attempted if the admin and frontend
-        * are on different domains to avoid the case where the frontend doesn't have
-        * ssl certs. Domain mapping plugins can allow other urls in these conditions
-        * using the customize_allowed_urls filter.
-        */
-
-       $allowed_urls = array( home_url('/') );
-       $admin_origin = parse_url( admin_url() );
-       $home_origin  = parse_url( home_url() );
-       $cross_domain = ( strtolower( $admin_origin[ 'host' ] ) != strtolower( $home_origin[ 'host' ] ) );
-
-       if ( is_ssl() && ! $cross_domain )
-               $allowed_urls[] = home_url( '/', 'https' );
-
-       /**
-        * Filter the list of URLs allowed to be clicked and followed in the Customizer preview.
-        *
-        * @since 3.4.0
-        *
-        * @param array $allowed_urls An array of allowed URLs.
-        */
-       $allowed_urls = array_unique( apply_filters( 'customize_allowed_urls', $allowed_urls ) );
-
-       $login_url = add_query_arg( array(
-               'interim-login' => 1,
-               'customize-login' => 1
-       ), wp_login_url() );
-
-       // Prepare Customizer settings to pass to JavaScript.
-       $settings = array(
-               'theme'    => array(
-                       'stylesheet' => $wp_customize->get_stylesheet(),
-                       'active'     => $wp_customize->is_theme_active(),
-               ),
-               'url'      => array(
-                       'preview'       => esc_url_raw( $url ? $url : home_url( '/' ) ),
-                       'parent'        => esc_url_raw( admin_url() ),
-                       'activated'     => esc_url_raw( home_url( '/' ) ),
-                       'ajax'          => esc_url_raw( admin_url( 'admin-ajax.php', 'relative' ) ),
-                       'allowed'       => array_map( 'esc_url_raw', $allowed_urls ),
-                       'isCrossDomain' => $cross_domain,
-                       'home'          => esc_url_raw( home_url( '/' ) ),
-                       'login'         => esc_url_raw( $login_url ),
-               ),
-               'browser'  => array(
-                       'mobile' => wp_is_mobile(),
-                       'ios'    => $is_ios,
-               ),
-               'settings' => array(),
-               'controls' => array(),
-               'panels'   => array(),
-               'sections' => array(),
-               'nonce'    => array(
-                       'save'    => wp_create_nonce( 'save-customize_' . $wp_customize->get_stylesheet() ),
-                       'preview' => wp_create_nonce( 'preview-customize_' . $wp_customize->get_stylesheet() )
-               ),
-               'autofocus' => array(),
-               'documentTitleTmpl' => $document_title_tmpl,
-       );
-
-       // Prepare Customize Setting objects to pass to JavaScript.
-       foreach ( $wp_customize->settings() as $id => $setting ) {
-               if ( $setting->check_capabilities() ) {
-                       $settings['settings'][ $id ] = array(
-                               'value'     => $setting->js_value(),
-                               'transport' => $setting->transport,
-                               'dirty'     => $setting->dirty,
-                       );
-               }
-       }
-
-       // Prepare Customize Control objects to pass to JavaScript.
-       foreach ( $wp_customize->controls() as $id => $control ) {
-               if ( $control->check_capabilities() ) {
-                       $settings['controls'][ $id ] = $control->json();
-               }
-       }
-
-       // Prepare Customize Section objects to pass to JavaScript.
-       foreach ( $wp_customize->sections() as $id => $section ) {
-               if ( $section->check_capabilities() ) {
-                       $settings['sections'][ $id ] = $section->json();
-               }
-       }
-
-       // Prepare Customize Panel objects to pass to JavaScript.
-       foreach ( $wp_customize->panels() as $panel_id => $panel ) {
-               if ( $panel->check_capabilities() ) {
-                       $settings['panels'][ $panel_id ] = $panel->json();
-                       foreach ( $panel->sections as $section_id => $section ) {
-                               if ( $section->check_capabilities() ) {
-                                       $settings['sections'][ $section_id ] = $section->json();
-                               }
-                       }
-               }
-       }
-
-       // Pass to frontend the Customizer construct being deeplinked
-       if ( isset( $_GET['autofocus'] ) ) {
-               $autofocus = wp_unslash( $_GET['autofocus'] );
-               if ( is_array( $autofocus ) ) {
-                       foreach ( $autofocus as $type => $id ) {
-                               if ( isset( $settings[ $type . 's' ][ $id ] ) ) {
-                                       $settings['autofocus'][ $type ] = $id;
-                               }
-                       }
-               }
-       }
-
</del><span class="cx" style="display: block; padding: 0 10px">         ?>
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        <script type="text/javascript">
-               var _wpCustomizeSettings = <?php echo wp_json_encode( $settings ); ?>;
-       </script>
</del><span class="cx" style="display: block; padding: 0 10px"> </div>
</span><span class="cx" style="display: block; padding: 0 10px"> </body>
</span><span class="cx" style="display: block; padding: 0 10px"> </html>
</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-09-17 19:29:46 UTC (rev 34268)
+++ trunk/src/wp-includes/class-wp-customize-manager.php        2015-09-17 19:41:35 UTC (rev 34269)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -94,6 +94,33 @@
</span><span class="cx" style="display: block; padding: 0 10px">        protected $registered_control_types = array();
</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">+         * Initial URL being previewed.
+        *
+        * @since 4.4.0
+        * @access protected
+        * @var string
+        */
+       protected $preview_url;
+
+       /**
+        * URL to link the user to when closing the Customizer.
+        *
+        * @since 4.4.0
+        * @access protected
+        * @var string
+        */
+       protected $return_url;
+
+       /**
+        * Mapping of 'panel', 'section', 'control' to the ID which should be autofocused.
+        *
+        * @since 4.4.0
+        * @access protected
+        * @var array
+        */
+       protected $autofocus = array();
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Unsanitized values for Customize Settings parsed from $_POST['customized'].
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @var array
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -139,6 +166,14 @@
</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="cx" style="display: block; padding: 0 10px">                add_action( 'customize_controls_init',            array( $this, 'prepare_controls' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_control_scripts' ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               // Render Panel, Section, and Control templates.
+               add_action( 'customize_controls_print_footer_scripts', array( $this, 'render_panel_templates' ), 1 );
+               add_action( 'customize_controls_print_footer_scripts', array( $this, 'render_section_templates' ), 1 );
+               add_action( 'customize_controls_print_footer_scripts', array( $this, 'render_control_templates' ), 1 );
+
+               // Export the settings to JS via the _wpCustomizeSettings variable.
+               add_action( 'customize_controls_print_footer_scripts', array( $this, 'customize_pane_settings' ), 1000 );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -621,7 +656,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function customize_preview_settings() {
</span><span class="cx" style="display: block; padding: 0 10px">                $settings = array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        'values'  => array(),
</del><span class="cx" style="display: block; padding: 0 10px">                         'channel' => wp_unslash( $_POST['customize_messenger_channel'] ),
</span><span class="cx" style="display: block; padding: 0 10px">                        'activePanels' => array(),
</span><span class="cx" style="display: block; padding: 0 10px">                        'activeSections' => array(),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -638,11 +672,6 @@
</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">-                foreach ( $this->settings as $id => $setting ) {
-                       if ( $setting->check_capabilities() ) {
-                               $settings['values'][ $id ] = $setting->js_value();
-                       }
-               }
</del><span class="cx" style="display: block; padding: 0 10px">                 foreach ( $this->panels as $panel_id => $panel ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( $panel->check_capabilities() ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                $settings['activePanels'][ $panel_id ] = $panel->active();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -667,6 +696,25 @@
</span><span class="cx" style="display: block; padding: 0 10px">                ?>
</span><span class="cx" style="display: block; padding: 0 10px">                <script type="text/javascript">
</span><span class="cx" style="display: block; padding: 0 10px">                        var _wpCustomizeSettings = <?php echo wp_json_encode( $settings ); ?>;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        _wpCustomizeSettings.values = {};
+                       (function( v ) {
+                               <?php
+                               /*
+                                * Serialize settings separately from the initial _wpCustomizeSettings
+                                * serialization in order to avoid a peak memory usage spike.
+                                * @todo We may not even need to export the values at all since the pane syncs them anyway.
+                                */
+                               foreach ( $this->settings as $id => $setting ) {
+                                       if ( $setting->check_capabilities() ) {
+                                               printf(
+                                                       "v[%s] = %s;\n",
+                                                       wp_json_encode( $id ),
+                                                       wp_json_encode( $setting->js_value() )
+                                               );
+                                       }
+                               }
+                               ?>
+                       })( _wpCustomizeSettings.values );
</ins><span class="cx" style="display: block; padding: 0 10px">                 </script>
</span><span class="cx" style="display: block; padding: 0 10px">                <?php
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1265,6 +1313,279 @@
</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">+         * Return whether the user agent is iOS.
+        *
+        * @since 4.4.0
+        * @access public
+        *
+        * @return bool
+        */
+       public function is_ios() {
+               return wp_is_mobile() && preg_match( '/iPad|iPod|iPhone/', $_SERVER['HTTP_USER_AGENT'] );
+       }
+
+       /**
+        * Get the template string for the Customizer pane document title.
+        *
+        * @since 4.4.0
+        * @access public
+        *
+        * @return string
+        */
+       public function get_document_title_template() {
+               if ( $this->is_theme_active() ) {
+                       $document_title_tmpl = _x( 'Customize: %s', 'Placeholder is the document title from the preview' );
+               } else {
+                       $document_title_tmpl = _x( 'Live Preview: %s', 'Placeholder is the document title from the preview' );
+               }
+               $document_title_tmpl = html_entity_decode( $document_title_tmpl, ENT_QUOTES, 'UTF-8' ); // Because exported to JS and assigned to document.title.
+               return $document_title_tmpl;
+       }
+
+       /**
+        * Set the initial URL to be previewed.
+        *
+        * URL is validated.
+        *
+        * @since 4.4.0
+        * @access public
+        *
+        * @param string $preview_url  URL to be previewed.
+        */
+       public function set_preview_url( $preview_url ) {
+               $this->preview_url = wp_validate_redirect( $preview_url, home_url( '/' ) );
+       }
+
+       /**
+        * Get the initial URL to be previewed.
+        *
+        * @since 4.4.0
+        * @access public
+        *
+        * @return string
+        */
+       public function get_preview_url() {
+               if ( empty( $this->preview_url ) ) {
+                       $preview_url = home_url( '/' );
+               } else {
+                       $preview_url = $this->preview_url;
+               }
+               return $preview_url;
+       }
+
+       /**
+        * Set URL to link the user to when closing the Customizer.
+        *
+        * URL is validated.
+        *
+        * @since 4.4.0
+        * @access public
+        *
+        * @param string $return_url  URL for return link.
+        */
+       public function set_return_url( $return_url ) {
+               $return_url = remove_query_arg( wp_removable_query_args(), $return_url );
+               $return_url = wp_validate_redirect( $return_url );
+               $this->return_url = $return_url;
+       }
+
+       /**
+        * Get URL to link the user to when closing the Customizer.
+        *
+        * @since 4.4.0
+        * @access public
+        *
+        * @return string
+        */
+       public function get_return_url() {
+               if ( $this->return_url ) {
+                       $return_url = $this->return_url;
+               } else if ( $this->preview_url ) {
+                       $return_url = $this->preview_url;
+               } else if ( current_user_can( 'edit_theme_options' ) || current_user_can( 'switch_themes' ) ) {
+                       $return_url = admin_url( 'themes.php' );
+               } else {
+                       $return_url = admin_url();
+               }
+               return $return_url;
+       }
+
+       /**
+        * Set the autofocused constructs.
+        *
+        * @param array $autofocus {
+        *     Mapping of 'panel', 'section', 'control' to the ID which should be autofocused.
+        *
+        *     @type string [$control]  ID for control to be autofocused.
+        *     @type string [$section]  ID for section to be autofocused.
+        *     @type string [$panel]    ID for panel to be autofocused.
+        * }
+        */
+       public function set_autofocus( $autofocus ) {
+               $this->autofocus = array_filter( wp_array_slice_assoc( $autofocus, array( 'panel', 'section', 'control' ) ), 'is_string' );
+       }
+
+       /**
+        * Get the autofocused constructs.
+        *
+        * @since 4.4.0
+        * @access public
+        *
+        * @return array {
+        *     Mapping of 'panel', 'section', 'control' to the ID which should be autofocused.
+        *
+        *     @type string [$control]  ID for control to be autofocused.
+        *     @type string [$section]  ID for section to be autofocused.
+        *     @type string [$panel]    ID for panel to be autofocused.
+        * }
+        */
+       public function get_autofocus() {
+               return $this->autofocus;
+       }
+
+       /**
+        * Print JavaScript settings for parent window.
+        *
+        * @since 4.3.0
+        */
+       public function customize_pane_settings() {
+               /*
+                * If the frontend and the admin are served from the same domain, load the
+                * preview over ssl if the Customizer is being loaded over ssl. This avoids
+                * insecure content warnings. This is not attempted if the admin and frontend
+                * are on different domains to avoid the case where the frontend doesn't have
+                * ssl certs. Domain mapping plugins can allow other urls in these conditions
+                * using the customize_allowed_urls filter.
+                */
+
+               $allowed_urls = array( home_url( '/' ) );
+               $admin_origin = parse_url( admin_url() );
+               $home_origin  = parse_url( home_url() );
+               $cross_domain = ( strtolower( $admin_origin['host'] ) !== strtolower( $home_origin['host'] ) );
+
+               if ( is_ssl() && ! $cross_domain ) {
+                       $allowed_urls[] = home_url( '/', 'https' );
+               }
+
+               /**
+                * Filter the list of URLs allowed to be clicked and followed in the Customizer preview.
+                *
+                * @since 3.4.0
+                *
+                * @param array $allowed_urls An array of allowed URLs.
+                */
+               $allowed_urls = array_unique( apply_filters( 'customize_allowed_urls', $allowed_urls ) );
+
+               $login_url = add_query_arg( array(
+                       'interim-login' => 1,
+                       'customize-login' => 1,
+               ), wp_login_url() );
+
+               // Prepare Customizer settings to pass to JavaScript.
+               $settings = array(
+                       'theme'    => array(
+                               'stylesheet' => $this->get_stylesheet(),
+                               'active'     => $this->is_theme_active(),
+                       ),
+                       'url'      => array(
+                               'preview'       => esc_url_raw( $this->get_preview_url() ),
+                               'parent'        => esc_url_raw( admin_url() ),
+                               'activated'     => esc_url_raw( home_url( '/' ) ),
+                               'ajax'          => esc_url_raw( admin_url( 'admin-ajax.php', 'relative' ) ),
+                               'allowed'       => array_map( 'esc_url_raw', $allowed_urls ),
+                               'isCrossDomain' => $cross_domain,
+                               'home'          => esc_url_raw( home_url( '/' ) ),
+                               'login'         => esc_url_raw( $login_url ),
+                       ),
+                       'browser'  => array(
+                               'mobile' => wp_is_mobile(),
+                               'ios'    => $this->is_ios(),
+                       ),
+                       'panels'   => array(),
+                       'sections' => array(),
+                       'nonce'    => array(
+                               'save'    => wp_create_nonce( 'save-customize_' . $this->get_stylesheet() ),
+                               'preview' => wp_create_nonce( 'preview-customize_' . $this->get_stylesheet() ),
+                       ),
+                       'autofocus' => array(),
+                       'documentTitleTmpl' => $this->get_document_title_template(),
+               );
+
+               // Prepare Customize Section objects to pass to JavaScript.
+               foreach ( $this->sections() as $id => $section ) {
+                       if ( $section->check_capabilities() ) {
+                               $settings['sections'][ $id ] = $section->json();
+                       }
+               }
+
+               // Prepare Customize Panel objects to pass to JavaScript.
+               foreach ( $this->panels() as $panel_id => $panel ) {
+                       if ( $panel->check_capabilities() ) {
+                               $settings['panels'][ $panel_id ] = $panel->json();
+                               foreach ( $panel->sections as $section_id => $section ) {
+                                       if ( $section->check_capabilities() ) {
+                                               $settings['sections'][ $section_id ] = $section->json();
+                                       }
+                               }
+                       }
+               }
+
+               // Pass to frontend the Customizer construct being deeplinked.
+               foreach ( $this->get_autofocus() as $type => $id ) {
+                       $can_autofocus = (
+                               ( 'control' === $type && $this->get_control( $id ) && $this->get_control( $id )->check_capabilities() )
+                               ||
+                               ( 'section' === $type && isset( $settings['sections'][ $id ] ) )
+                               ||
+                               ( 'panel' === $type && isset( $settings['panels'][ $id ] ) )
+                       );
+                       if ( $can_autofocus ) {
+                               $settings['autofocus'][ $type ] = $id;
+                       }
+               }
+
+               ?>
+               <script type="text/javascript">
+                       var _wpCustomizeSettings = <?php echo wp_json_encode( $settings ); ?>;
+                       _wpCustomizeSettings.controls = {};
+                       _wpCustomizeSettings.settings = {};
+                       <?php
+
+                       // Serialize settings one by one to improve memory usage.
+                       echo "(function ( s ){\n";
+                       foreach ( $this->settings() as $setting ) {
+                               if ( $setting->check_capabilities() ) {
+                                       printf(
+                                               "s[%s] = %s;\n",
+                                               wp_json_encode( $setting->id ),
+                                               wp_json_encode( array(
+                                                       'value'     => $setting->js_value(),
+                                                       'transport' => $setting->transport,
+                                                       'dirty'     => $setting->dirty,
+                                               ) )
+                                       );
+                               }
+                       }
+                       echo "})( _wpCustomizeSettings.settings );\n";
+
+                       // Serialize controls one by one to improve memory usage.
+                       echo "(function ( c ){\n";
+                       foreach ( $this->controls() as $control ) {
+                               if ( $control->check_capabilities() ) {
+                                       printf(
+                                               "c[%s] = %s;\n",
+                                               wp_json_encode( $control->id ),
+                                               wp_json_encode( $control->json() )
+                                       );
+                               }
+                       }
+                       echo "})( _wpCustomizeSettings.controls );\n";
+               ?>
+               </script>
+               <?php
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Register some default controls.
</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="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   2015-09-17 19:29:46 UTC (rev 34268)
+++ trunk/tests/phpunit/tests/customize/manager.php     2015-09-17 19:41:35 UTC (rev 34269)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,4 +1,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+/**
+ * WP_Customize_Manager tests.
+ *
+ * @package WordPress
+ */
</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">  * Tests for the WP_Customize_Manager class.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -7,6 +12,23 @@
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> class Tests_WP_Customize_Manager extends WP_UnitTestCase {
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * Customize manager instance re-instantiated with each test.
+        *
+        * @var WP_Customize_Manager
+        */
+       public $manager;
+
+       /**
+        * Symbol.
+        *
+        * @var stdClass
+        */
+       public $undefined;
+
+       /**
+        * Set up test.
+        */
</ins><span class="cx" style="display: block; padding: 0 10px">         function setUp() {
</span><span class="cx" style="display: block; padding: 0 10px">                parent::setUp();
</span><span class="cx" style="display: block; padding: 0 10px">                require_once( ABSPATH . WPINC . '/class-wp-customize-manager.php' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -15,6 +37,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->undefined = new stdClass();
</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">+        /**
+        * Tear down test.
+        */
</ins><span class="cx" style="display: block; padding: 0 10px">         function tearDown() {
</span><span class="cx" style="display: block; padding: 0 10px">                $this->manager = null;
</span><span class="cx" style="display: block; padding: 0 10px">                unset( $GLOBALS['wp_customize'] );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -156,6 +181,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">         * In lieu of closures, callback for customize_dynamic_setting_args filter added for test_register_dynamic_settings().
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         *
+        * @param array  $setting_args Setting args.
+        * @param string $setting_id   Setting ID.
+        * @return array
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        function filter_customize_dynamic_setting_args_for_test_dynamic_settings( $setting_args, $setting_id ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( false, $setting_args, 'Expected $setting_args to be false by default.' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -168,6 +197,11 @@
</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">         * In lieu of closures, callback for customize_dynamic_setting_class filter added for test_register_dynamic_settings().
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         *
+        * @param string $setting_class Setting class.
+        * @param string $setting_id    Setting ID.
+        * @param array  $setting_args  Setting args.
+        * @return string
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        function filter_customize_dynamic_setting_class_for_test_dynamic_settings( $setting_class, $setting_id, $setting_args ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( 'WP_Customize_Setting', $setting_class );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -175,4 +209,122 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertInternalType( 'array', $setting_args );
</span><span class="cx" style="display: block; padding: 0 10px">                return $setting_class;
</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 is_ios() method.
+        *
+        * @see WP_Customize_Manager::is_ios()
+        */
+       function test_is_ios() {
+               $this->markTestSkipped( 'WP_Customize_Manager::is_ios() cannot be tested because it uses wp_is_mobile() which contains a static var.' );
+       }
+
+       /**
+        * Test get_document_title_template() method.
+        *
+        * @see WP_Customize_Manager::get_document_title_template()
+        */
+       function test_get_document_title_template() {
+               $tpl = $this->manager->get_document_title_template();
+               $this->assertContains( '%s', $tpl );
+       }
+
+       /**
+        * Test get_preview_url()/set_preview_url methods.
+        *
+        * @see WP_Customize_Manager::get_preview_url()
+        * @see WP_Customize_Manager::set_preview_url()
+        */
+       function test_preview_url() {
+               $this->assertEquals( home_url( '/' ), $this->manager->get_preview_url() );
+               $preview_url = home_url( '/foo/bar/baz/' );
+               $this->manager->set_preview_url( $preview_url );
+               $this->assertEquals( $preview_url, $this->manager->get_preview_url() );
+               $this->manager->set_preview_url( 'http://illegalsite.example.com/food/' );
+               $this->assertEquals( home_url( '/' ), $this->manager->get_preview_url() );
+       }
+
+       /**
+        * Test get_return_url()/set_return_url() methods.
+        *
+        * @see WP_Customize_Manager::get_return_url()
+        * @see WP_Customize_Manager::set_return_url()
+        */
+       function test_return_url() {
+               wp_set_current_user( $this->factory->user->create( array( 'role' => 'author' ) ) );
+               $this->assertEquals( get_admin_url(), $this->manager->get_return_url() );
+
+               wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
+               $this->assertTrue( current_user_can( 'edit_theme_options' ) );
+               $this->assertEquals( admin_url( 'themes.php' ), $this->manager->get_return_url() );
+
+               $preview_url = home_url( '/foo/' );
+               $this->manager->set_preview_url( $preview_url );
+               $this->assertEquals( $preview_url, $this->manager->get_return_url() );
+
+               $this->manager->set_return_url( admin_url( 'edit.php?trashed=1' ) );
+               $this->assertEquals( admin_url( 'edit.php' ), $this->manager->get_return_url() );
+       }
+
+       /**
+        * Test get_autofocus()/set_autofocus() methods.
+        *
+        * @see WP_Customize_Manager::get_autofocus()
+        * @see WP_Customize_Manager::set_autofocus()
+        */
+       function test_autofocus() {
+               $this->assertEmpty( $this->manager->get_autofocus() );
+
+               $this->manager->set_autofocus( array( 'unrecognized' => 'food' ) );
+               $this->assertEmpty( $this->manager->get_autofocus() );
+
+               $autofocus = array( 'control' => 'blogname' );
+               $this->manager->set_autofocus( $autofocus );
+               $this->assertEquals( $autofocus, $this->manager->get_autofocus() );
+
+               $autofocus = array( 'section' => 'colors' );
+               $this->manager->set_autofocus( $autofocus );
+               $this->assertEquals( $autofocus, $this->manager->get_autofocus() );
+
+               $autofocus = array( 'panel' => 'widgets' );
+               $this->manager->set_autofocus( $autofocus );
+               $this->assertEquals( $autofocus, $this->manager->get_autofocus() );
+
+               $autofocus = array( 'control' => array( 'blogname', 'blogdescription' ) );
+               $this->manager->set_autofocus( $autofocus );
+               $this->assertEmpty( $this->manager->get_autofocus() );
+       }
+
+       /**
+        * Test customize_pane_settings() method.
+        *
+        * @see WP_Customize_Manager::customize_pane_settings()
+        */
+       function test_customize_pane_settings() {
+               wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
+               $this->manager->register_controls();
+               $this->manager->prepare_controls();
+               $autofocus = array( 'control' => 'blogname' );
+               $this->manager->set_autofocus( $autofocus );
+
+               ob_start();
+               $this->manager->customize_pane_settings();
+               $content = ob_get_clean();
+
+               $this->assertContains( 'var _wpCustomizeSettings =', $content );
+               $this->assertContains( '"blogname"', $content );
+               $this->assertContains( '_wpCustomizeSettings.controls', $content );
+               $this->assertContains( '_wpCustomizeSettings.settings', $content );
+               $this->assertContains( '</script>', $content );
+
+               $this->assertNotEmpty( preg_match( '#var _wpCustomizeSettings\s*=\s*({.*?});\s*\n#', $content, $matches ) );
+               $json = $matches[1];
+               $data = json_decode( $json, true );
+               $this->assertNotEmpty( $data );
+
+               $this->assertEqualSets( array( 'theme', 'url', 'browser', 'panels', 'sections', 'nonce', 'autofocus', 'documentTitleTmpl' ), array_keys( $data ) );
+               $this->assertEquals( $autofocus, $data['autofocus'] );
+               $this->assertArrayHasKey( 'save', $data['nonce'] );
+               $this->assertArrayHasKey( 'preview', $data['nonce'] );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>