WordPress.org

Make WordPress Core

Changeset 35100


Ignore:
Timestamp:
10/13/2015 01:12:12 AM (4 years ago)
Author:
wonderboymusic
Message:

Widgets: when getting settings, and none exist, set them to empty to avoid extraneous database queries on subsequent requests.

Adds unit tests.

Props kovshenin, MikeHansenMe, dlh.
Fixes #26876.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-widget.php

    r34782 r35100  
    540540        $settings = get_option( $this->option_name );
    541541
    542         if ( false === $settings && isset( $this->alt_option_name ) ) {
    543             $settings = get_option( $this->alt_option_name );
     542        if ( false === $settings ) {
     543            if ( isset( $this->alt_option_name ) ) {
     544                $settings = get_option( $this->alt_option_name );
     545            } else {
     546                // Save an option so it can be autoloaded next time.
     547                $this->save_settings( array() );
     548            }
    544549        }
    545550
  • trunk/tests/phpunit/tests/widgets.php

    r34817 r35100  
    466466        unset( $option_value['_multiwidget'] );
    467467
     468        // Pretend this widget is new.
     469        delete_option( 'widget_nav_menu' );
     470        $never_used = get_option( 'widget_nav_menu' );
     471        $this->assertFalse( $never_used );
     472
    468473        wp_widgets_init();
    469474        $wp_widget_search = $wp_registered_widgets['search-2']['callback'][0];
     
    476481            $this->assertEquals( $settings[ $widget_number ], $option_value[ $widget_number ] );
    477482        }
     483
     484        // After widgets_init(), get_settings() should create the widget option.
     485        $never_used = get_option( 'widget_nav_menu' );
     486        $this->assertEquals( 1, $never_used['_multiwidget'] );
     487        $this->assertArrayNotHasKey( 0, $never_used );
    478488    }
    479489
Note: See TracChangeset for help on using the changeset viewer.