WordPress.org

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/tests/phpunit/tests/widgets.php

    r34465 r34563  
    307307        $result = dynamic_sidebar( 'Sidebar 1' );
    308308        ob_end_clean();
    309          
     309
    310310        $this->assertFalse( $result );
    311311    }
    312312
     313    /**
     314     * @see wp_widget_control()
     315     */
     316    function test_wp_widget_control() {
     317        global $wp_registered_widgets;
     318
     319        wp_widgets_init();
     320        require_once ABSPATH . '/wp-admin/includes/widgets.php';
     321        $widget_id = 'search-2';
     322        $widget = $wp_registered_widgets[ $widget_id ];
     323        $params = array(
     324            'widget_id' => $widget['id'],
     325            'widget_name' => $widget['name'],
     326        );
     327        $args = wp_list_widget_controls_dynamic_sidebar( array( 0 => $params, 1 => $widget['params'][0] ) );
     328
     329        ob_start();
     330        call_user_func_array( 'wp_widget_control', $args );
     331        $control = ob_get_clean();
     332        $this->assertNotEmpty( $control );
     333
     334        $this->assertContains( '<div class="widget-top">', $control );
     335        $this->assertContains( '<div class="widget-title-action">', $control );
     336        $this->assertContains( '<div class="widget-title">', $control );
     337        $this->assertContains( '<form method="post">', $control );
     338        $this->assertContains( '<div class="widget-content">', $control );
     339        $this->assertContains( '<input class="widefat"', $control );
     340        $this->assertContains( '<input type="hidden" name="id_base" class="id_base" value="search"', $control );
     341        $this->assertContains( '<div class="widget-control-actions">', $control );
     342        $this->assertContains( '<div class="alignleft">', $control );
     343        $this->assertContains( 'widget-control-remove', $control );
     344        $this->assertContains( 'widget-control-close', $control );
     345        $this->assertContains( '<div class="alignright">', $control );
     346        $this->assertContains( '<input type="submit" name="savewidget"', $control );
     347
     348        $param_overrides = array(
     349            'before_form' => '<!-- before_form -->',
     350            'after_form' => '<!-- after_form -->',
     351            'before_widget_content' => '<!-- before_widget_content -->',
     352            'after_widget_content' => '<!-- after_widget_content -->',
     353        );
     354        $params = array_merge( $params, $param_overrides );
     355        $args = wp_list_widget_controls_dynamic_sidebar( array( 0 => $params, 1 => $widget['params'][0] ) );
     356
     357        ob_start();
     358        call_user_func_array( 'wp_widget_control', $args );
     359        $control = ob_get_clean();
     360        $this->assertNotEmpty( $control );
     361        $this->assertNotContains( '<form method="post">', $control );
     362        $this->assertNotContains( '<div class="widget-content">', $control );
     363
     364        foreach ( $param_overrides as $contained ) {
     365            $this->assertContains( $contained, $control );
     366        }
     367    }
    313368}
Note: See TracChangeset for help on using the changeset viewer.