Make WordPress Core


Ignore:
Timestamp:
09/17/2015 07:41:35 PM (11 years ago)
Author:
westonruter
Message:

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 #33898.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/customize.php

    r34093 r34269  
    2121}
    2222
    23 wp_reset_vars( array( 'url', 'return' ) );
    24 $url = wp_unslash( $url );
    25 $url = wp_validate_redirect( $url, home_url( '/' ) );
    26 if ( $return ) {
    27         $return = wp_unslash( $return );
    28         $return = remove_query_arg( wp_removable_query_args(), $return );
    29         $return = wp_validate_redirect( $return );
     23wp_reset_vars( array( 'url', 'return', 'autofocus' ) );
     24if ( ! empty( $url ) ) {
     25        $wp_customize->set_preview_url( wp_unslash( $url ) );
    3026}
    31 if ( ! $return ) {
    32         if ( $url ) {
    33                 $return = $url;
    34         } elseif ( current_user_can( 'edit_theme_options' ) || current_user_can( 'switch_themes' ) ) {
    35                 $return = admin_url( 'themes.php' );
    36         } else {
    37                 $return = admin_url();
    38         }
     27if ( ! empty( $return ) ) {
     28        $wp_customize->set_return_url( wp_unslash( $return ) );
     29}
     30if ( ! empty( $autofocus ) && is_array( $autofocus ) ) {
     31        $wp_customize->set_autofocus( wp_unslash( $autofocus ) );
    3932}
    4033
    … …  
    8477endif;
    8578
    86 $is_ios = wp_is_mobile() && preg_match( '/iPad|iPod|iPhone/', $_SERVER['HTTP_USER_AGENT'] );
    87 
    88 if ( $is_ios ) {
     79if ( $wp_customize->is_ios() ) {
    8980        $body_class .= ' ios';
    9081}
    … …  
    9586$body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_locale() ) ) );
    9687
    97 if ( $wp_customize->is_theme_active() ) {
    98         $document_title_tmpl = _x( 'Customize: %s', 'Placeholder is the document title from the preview' );
    99 } else {
    100         $document_title_tmpl = _x( 'Live Preview: %s', 'Placeholder is the document title from the preview' );
    101 }
    102 $document_title_tmpl = html_entity_decode( $document_title_tmpl, ENT_QUOTES, 'UTF-8' ); // because exported to JS and assigned to document.title
    103 $admin_title = sprintf( $document_title_tmpl, __( 'Loading…' ) );
     88$admin_title = sprintf( $wp_customize->get_document_title_template(), __( 'Loading…' ) );
    10489
    10590?><title><?php echo $admin_title; ?></title>
    10691
    10792<script type="text/javascript">
    108 var ajaxurl = '<?php echo admin_url( 'admin-ajax.php', 'relative' ); ?>';
     93var ajaxurl = <?php echo wp_json_encode( admin_url( 'admin-ajax.php', 'relative' ) ); ?>;
    10994</script>
    11095
    … …  
    138123                                <span class="preview"><?php _e( 'Preview' ); ?></span>
    139124                        </a>
    140                         <a class="customize-controls-close" href="<?php echo esc_url( $return ); ?>">
     125                        <a class="customize-controls-close" href="<?php echo esc_url( $wp_customize->get_return_url() ); ?>">
    141126                                <span class="screen-reader-text"><?php _e( 'Cancel' ); ?></span>
    142127                        </a>
    … …  
    173158        <?php
    174159
    175         // Render Panel, Section, and Control templates.
    176         $wp_customize->render_panel_templates();
    177         $wp_customize->render_section_templates();
    178         $wp_customize->render_control_templates();
    179 
    180160        /**
    181          * Print Customizer control scripts in the footer.
     161         * Print templates, control scripts, and settings in the footer.
    182162         *
    183163         * @since 3.4.0
    184164         */
    185165        do_action( 'customize_controls_print_footer_scripts' );
    186 
    187         /*
    188          * If the frontend and the admin are served from the same domain, load the
    189          * preview over ssl if the Customizer is being loaded over ssl. This avoids
    190          * insecure content warnings. This is not attempted if the admin and frontend
    191          * are on different domains to avoid the case where the frontend doesn't have
    192          * ssl certs. Domain mapping plugins can allow other urls in these conditions
    193          * using the customize_allowed_urls filter.
    194          */
    195 
    196         $allowed_urls = array( home_url('/') );
    197         $admin_origin = parse_url( admin_url() );
    198         $home_origin  = parse_url( home_url() );
    199         $cross_domain = ( strtolower( $admin_origin[ 'host' ] ) != strtolower( $home_origin[ 'host' ] ) );
    200 
    201         if ( is_ssl() && ! $cross_domain )
    202                 $allowed_urls[] = home_url( '/', 'https' );
    203 
    204         /**
    205          * Filter the list of URLs allowed to be clicked and followed in the Customizer preview.
    206          *
    207          * @since 3.4.0
    208          *
    209          * @param array $allowed_urls An array of allowed URLs.
    210          */
    211         $allowed_urls = array_unique( apply_filters( 'customize_allowed_urls', $allowed_urls ) );
    212 
    213         $login_url = add_query_arg( array(
    214                 'interim-login' => 1,
    215                 'customize-login' => 1
    216         ), wp_login_url() );
    217 
    218         // Prepare Customizer settings to pass to JavaScript.
    219         $settings = array(
    220                 'theme'    => array(
    221                         'stylesheet' => $wp_customize->get_stylesheet(),
    222                         'active'     => $wp_customize->is_theme_active(),
    223                 ),
    224                 'url'      => array(
    225                         'preview'       => esc_url_raw( $url ? $url : home_url( '/' ) ),
    226                         'parent'        => esc_url_raw( admin_url() ),
    227                         'activated'     => esc_url_raw( home_url( '/' ) ),
    228                         'ajax'          => esc_url_raw( admin_url( 'admin-ajax.php', 'relative' ) ),
    229                         'allowed'       => array_map( 'esc_url_raw', $allowed_urls ),
    230                         'isCrossDomain' => $cross_domain,
    231                         'home'          => esc_url_raw( home_url( '/' ) ),
    232                         'login'         => esc_url_raw( $login_url ),
    233                 ),
    234                 'browser'  => array(
    235                         'mobile' => wp_is_mobile(),
    236                         'ios'    => $is_ios,
    237                 ),
    238                 'settings' => array(),
    239                 'controls' => array(),
    240                 'panels'   => array(),
    241                 'sections' => array(),
    242                 'nonce'    => array(
    243                         'save'    => wp_create_nonce( 'save-customize_' . $wp_customize->get_stylesheet() ),
    244                         'preview' => wp_create_nonce( 'preview-customize_' . $wp_customize->get_stylesheet() )
    245                 ),
    246                 'autofocus' => array(),
    247                 'documentTitleTmpl' => $document_title_tmpl,
    248         );
    249 
    250         // Prepare Customize Setting objects to pass to JavaScript.
    251         foreach ( $wp_customize->settings() as $id => $setting ) {
    252                 if ( $setting->check_capabilities() ) {
    253                         $settings['settings'][ $id ] = array(
    254                                 'value'     => $setting->js_value(),
    255                                 'transport' => $setting->transport,
    256                                 'dirty'     => $setting->dirty,
    257                         );
    258                 }
    259         }
    260 
    261         // Prepare Customize Control objects to pass to JavaScript.
    262         foreach ( $wp_customize->controls() as $id => $control ) {
    263                 if ( $control->check_capabilities() ) {
    264                         $settings['controls'][ $id ] = $control->json();
    265                 }
    266         }
    267 
    268         // Prepare Customize Section objects to pass to JavaScript.
    269         foreach ( $wp_customize->sections() as $id => $section ) {
    270                 if ( $section->check_capabilities() ) {
    271                         $settings['sections'][ $id ] = $section->json();
    272                 }
    273         }
    274 
    275         // Prepare Customize Panel objects to pass to JavaScript.
    276         foreach ( $wp_customize->panels() as $panel_id => $panel ) {
    277                 if ( $panel->check_capabilities() ) {
    278                         $settings['panels'][ $panel_id ] = $panel->json();
    279                         foreach ( $panel->sections as $section_id => $section ) {
    280                                 if ( $section->check_capabilities() ) {
    281                                         $settings['sections'][ $section_id ] = $section->json();
    282                                 }
    283                         }
    284                 }
    285         }
    286 
    287         // Pass to frontend the Customizer construct being deeplinked
    288         if ( isset( $_GET['autofocus'] ) ) {
    289                 $autofocus = wp_unslash( $_GET['autofocus'] );
    290                 if ( is_array( $autofocus ) ) {
    291                         foreach ( $autofocus as $type => $id ) {
    292                                 if ( isset( $settings[ $type . 's' ][ $id ] ) ) {
    293                                         $settings['autofocus'][ $type ] = $id;
    294                                 }
    295                         }
    296                 }
    297         }
    298 
    299166        ?>
    300         <script type="text/javascript">
    301                 var _wpCustomizeSettings = <?php echo wp_json_encode( $settings ); ?>;
    302         </script>
    303167</div>
    304168</body>
Note: See TracChangeset for help on using the changeset viewer.