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.


git-svn-id: https://develop.svn.wordpress.org/trunk@35100 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2015-10-13 01:12:12 +00:00
parent 32b604de00
commit d936110f7c
2 changed files with 17 additions and 2 deletions

View File

@ -539,8 +539,13 @@ class WP_Widget {
$settings = get_option( $this->option_name );
if ( false === $settings && isset( $this->alt_option_name ) ) {
$settings = get_option( $this->alt_option_name );
if ( false === $settings ) {
if ( isset( $this->alt_option_name ) ) {
$settings = get_option( $this->alt_option_name );
} else {
// Save an option so it can be autoloaded next time.
$this->save_settings( array() );
}
}
if ( ! is_array( $settings ) && ! ( $settings instanceof ArrayObject || $settings instanceof ArrayIterator ) ) {

View File

@ -465,6 +465,11 @@ class Tests_Widgets extends WP_UnitTestCase {
$this->assertArrayHasKey( 'title', $instance );
unset( $option_value['_multiwidget'] );
// Pretend this widget is new.
delete_option( 'widget_nav_menu' );
$never_used = get_option( 'widget_nav_menu' );
$this->assertFalse( $never_used );
wp_widgets_init();
$wp_widget_search = $wp_registered_widgets['search-2']['callback'][0];
@ -475,6 +480,11 @@ class Tests_Widgets extends WP_UnitTestCase {
foreach ( $option_value as $widget_number => $instance ) {
$this->assertEquals( $settings[ $widget_number ], $option_value[ $widget_number ] );
}
// After widgets_init(), get_settings() should create the widget option.
$never_used = get_option( 'widget_nav_menu' );
$this->assertEquals( 1, $never_used['_multiwidget'] );
$this->assertArrayNotHasKey( 0, $never_used );
}
/**