Make WordPress Core


Ignore:
Timestamp:
09/25/2015 09:01:46 PM (6 years ago)
Author:
westonruter
Message:

Customizer: Defer embedding widget controls to improve DOM performance and initial load time.

The Menu Customizer feature includes a performance technique whereby the controls for nav menu items are only embedded into the DOM once the containing menu section is expanded. This commit implements the same DOM deferral for widgets but goes a step further than just embedding the controls once the widget area's Customizer section is expanded: it also defers the embedding of the widget control's form until the widget is expanded, at which point the widget-added event also fires to allow any additional widget initialization to be done. The deferred DOM embedding can speed up initial load time by 10x or more. This DOM deferral also yields a reduction in overall memory usage in the browser process.

Includes changes to wp_widget_control() to facilitate separating out the widget form from the surrounding accordion container; also includes unit tests for this previously-untested function. Also included are initial QUnit tests (finally) for widgets in the Customizer.

Fixes #33901.

File:
1 edited

Legend:

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

    r34017 r34563  
    182182    $add_new = isset($sidebar_args['_add']) ? $sidebar_args['_add'] : '';
    183183
     184    $before_form = isset( $sidebar_args['before_form'] ) ? $sidebar_args['before_form'] : '<form method="post">';
     185    $after_form = isset( $sidebar_args['after_form'] ) ? $sidebar_args['after_form'] : '</form>';
     186    $before_widget_content = isset( $sidebar_args['before_widget_content'] ) ? $sidebar_args['before_widget_content'] : '<div class="widget-content">';
     187    $after_widget_content = isset( $sidebar_args['after_widget_content'] ) ? $sidebar_args['after_widget_content'] : '</div>';
     188
    184189    $query_arg = array( 'editwidget' => $widget['id'] );
    185190    if ( $add_new ) {
     
    226231
    227232    <div class="widget-inside">
    228     <form method="post">
    229     <div class="widget-content">
    230 <?php
    231     if ( isset($control['callback']) )
     233    <?php echo $before_form; ?>
     234    <?php echo $before_widget_content; ?>
     235    <?php
     236    if ( isset( $control['callback'] ) ) {
    232237        $has_form = call_user_func_array( $control['callback'], $control['params'] );
    233     else
    234         echo "\t\t<p>" . __('There are no options for this widget.') . "</p>\n"; ?>
    235     </div>
     238    } else {
     239        echo "\t\t<p>" . __('There are no options for this widget.') . "</p>\n";
     240    }
     241    ?>
     242    <?php echo $after_widget_content; ?>
    236243    <input type="hidden" name="widget-id" class="widget-id" value="<?php echo esc_attr($id_format); ?>" />
    237244    <input type="hidden" name="id_base" class="id_base" value="<?php echo esc_attr($id_base); ?>" />
     
    253260        <br class="clear" />
    254261    </div>
    255     </form>
     262    <?php echo $after_form; ?>
    256263    </div>
    257264
Note: See TracChangeset for help on using the changeset viewer.