Make WordPress Core


Ignore:
Timestamp:
09/09/2022 02:17:33 AM (18 months ago)
Author:
peterwilsoncc
Message:

Widgets: Store default options for uninitialized widgets.

Prevent unnecessary database queries on page load by initializing widget options. On sites with uninitialized widgets, this prevents one or two database queries per uninitialized widget on each page load.

Props Chouby, mvraghavan, costdev, peterwilsoncc, spacedmonkey, mukesh27.
Fixes #54677.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/widgets.php

    r52259 r54112  
    685685        $this->assertSame( 1, $never_used['_multiwidget'] );
    686686        $this->assertArrayNotHasKey( 0, $never_used );
     687    }
     688
     689    /**
     690     * @ticket 54677
     691     *
     692     * @covers WP_Widget::get_settings
     693     */
     694    public function test_wp_widget_initializes_widget_with_alt_option() {
     695        /*
     696         * Emulate a new the recent posts widget.
     697         *
     698         * The widget contains an alternative (legacy) option so both the
     699         * current and the alternative option need to be deleted.
     700         */
     701        delete_option( 'widget_recent-posts' );
     702        delete_option( 'widget_recent_entries' );
     703
     704        $this->assertFalse( get_option( 'widget_recent-posts' ), 'The option widget_recent-posts was not deleted.' );
     705        $this->assertFalse( get_option( 'widget_recent_entries' ), 'The option widget_recent_entries was not deleted.' );
     706
     707        wp_widgets_init();
     708        $this->assertSameSetsWithIndex( array( '_multiwidget' => 1 ), get_option( 'widget_recent-posts' ), 'Option failed to be initialized.' );
     709        $this->assertFalse( get_option( 'widget_recent_entries' ), 'Alternative option is set.' );
     710    }
     711
     712    /**
     713     * @ticket 54677
     714     *
     715     * @covers WP_Widget::get_settings
     716     */
     717    public function test_wp_widget_migrates_widget_with_alt_option() {
     718        $option = array(
     719            2              => array(
     720                'title'     => 'Recent Posts',
     721                'number'    => 5,
     722                'show_date' => false,
     723            ),
     724            '_multiwidget' => 1,
     725        );
     726
     727        /*
     728         * Emulate the recent posts widget with an alternative option.
     729         *
     730         * The widget contains an alternative (legacy) option so the
     731         * current option is deleted while the alternative option is created.
     732         */
     733        delete_option( 'widget_recent-posts' );
     734        update_option( 'widget_recent_entries', $option );
     735
     736        $this->assertFalse( get_option( 'widget_recent-posts' ), 'The option widget_recent-posts was not deleted.' );
     737        $this->assertSameSetsWithIndex( $option, get_option( 'widget_recent_entries' ), 'The option widget_recent_entries was not set to the default.' );
     738
     739        wp_widgets_init();
     740        $this->assertSameSetsWithIndex( $option, get_option( 'widget_recent-posts' ), 'Option failed to be converted to new name.' );
     741        $this->assertFalse( get_option( 'widget_recent_entries' ), 'Alternative option was not deleted.' );
    687742    }
    688743
Note: See TracChangeset for help on using the changeset viewer.