Make WordPress Core

Ticket #28216: 28216.7.diff

File 28216.7.diff, 5.5 KB (added by westonruter, 8 years ago)

Test registering multiple instances of the same class

  • src/wp-includes/class-wp-widget-factory.php

    diff --git src/wp-includes/class-wp-widget-factory.php src/wp-includes/class-wp-widget-factory.php
    index 12b4553..9b9214e 100644
    class WP_Widget_Factory { 
    4949         * Registers a widget subclass.
    5050         *
    5151         * @since 2.8.0
     52         * @since 4.6.0 The `$widget` param can also be an instance object of `WP_Widget` instead of just a `WP_Widget` subclass name.
    5253         * @access public
    5354         *
    54          * @param string $widget_class The name of a WP_Widget subclass.
     55         * @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass.
    5556         */
    56         public function register( $widget_class ) {
    57                 $this->widgets[$widget_class] = new $widget_class();
     57        public function register( $widget ) {
     58                if ( $widget instanceof WP_Widget ) {
     59                        $this->widgets[ spl_object_hash( $widget ) ] = $widget;
     60                } else {
     61                        $this->widgets[ $widget ] = new $widget();
     62                }
    5863        }
    5964
    6065        /**
    6166         * Un-registers a widget subclass.
    6267         *
    6368         * @since 2.8.0
     69         * @since 4.6.0 The `$widget` param can also be an instance object of `WP_Widget` instead of just a `WP_Widget` subclass name.
    6470         * @access public
    6571         *
    66          * @param string $widget_class The name of a WP_Widget subclass.
     72         * @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass.
    6773         */
    68         public function unregister( $widget_class ) {
    69                 unset( $this->widgets[ $widget_class ] );
     74        public function unregister( $widget ) {
     75                if ( $widget instanceof WP_Widget ) {
     76                        unset( $this->widgets[ spl_object_hash( $widget ) ] );
     77                } else {
     78                        unset( $this->widgets[ $widget ] );
     79                }
    7080        }
    7181
    7282        /**
  • tests/phpunit/tests/widgets.php

    diff --git tests/phpunit/tests/widgets.php tests/phpunit/tests/widgets.php
    index 0ac6d92..0d0234c 100644
    class Tests_Widgets extends WP_UnitTestCase { 
    4444        }
    4545
    4646        /**
     47         * Test that registering a widget class and registering a widget instance work together.
     48         *
     49         * @see register_widget()
     50         * @see unregister_widget()
     51         * @ticket 28216
     52         */
     53        function test_register_and_unregister_widget_instance() {
     54                global $wp_widget_factory, $wp_registered_widgets;
     55                $this->assertEmpty( $wp_widget_factory->widgets );
     56                $this->assertEmpty( $wp_registered_widgets );
     57
     58                update_option( 'widget_search', array(
     59                        2 => array( 'title' => '' ),
     60                        '_multiwidget' => 1,
     61                ) );
     62                update_option( 'widget_better_search', array(
     63                        3 => array( 'title' => '' ),
     64                        '_multiwidget' => 1,
     65                ) );
     66                update_option( 'widget_best_search', array(
     67                        4 => array( 'title' => '' ),
     68                        '_multiwidget' => 1,
     69                ) );
     70
     71                register_widget( 'WP_Widget_Search' );
     72                $this->assertArrayHasKey( 'WP_Widget_Search', $wp_widget_factory->widgets );
     73
     74                $widget_better_search = new WP_Widget_Search();
     75                $widget_better_search->id_base = 'better_search';
     76                $widget_better_search->name = 'Better Search';
     77                $widget_better_search->option_name = 'widget_' . $widget_better_search->id_base;
     78                $widget_better_search->widget_options['classname'] = 'widget_' . $widget_better_search->id_base;
     79                $widget_better_search->control_options['id_base'] = $widget_better_search->id_base;
     80                register_widget( $widget_better_search );
     81                $this->assertArrayHasKey( spl_object_hash( $widget_better_search ), $wp_widget_factory->widgets );
     82
     83                $widget_best_search = new WP_Widget_Search();
     84                $widget_best_search->id_base = 'best_search';
     85                $widget_best_search->name = 'Best Search';
     86                $widget_best_search->option_name = 'widget_' . $widget_best_search->id_base;
     87                $widget_best_search->widget_options['classname'] = 'widget_' . $widget_best_search->id_base;
     88                $widget_best_search->control_options['id_base'] = $widget_best_search->id_base;
     89                register_widget( $widget_best_search );
     90                $this->assertArrayHasKey( spl_object_hash( $widget_best_search ), $wp_widget_factory->widgets );
     91
     92                $this->assertCount( 3, $wp_widget_factory->widgets );
     93                $this->assertArrayHasKey( 'WP_Widget_Search', $wp_widget_factory->widgets );
     94                $this->assertArrayHasKey( spl_object_hash( $widget_better_search ), $wp_widget_factory->widgets );
     95                $this->assertArrayHasKey( spl_object_hash( $widget_best_search ), $wp_widget_factory->widgets );
     96
     97                $wp_widget_factory->_register_widgets();
     98
     99                $this->assertArrayHasKey( 'search-2', $wp_registered_widgets );
     100                $this->assertArrayHasKey( 'better_search-3', $wp_registered_widgets );
     101                $this->assertArrayHasKey( 'best_search-4', $wp_registered_widgets );
     102                $this->assertInstanceOf( 'WP_Widget_Search', $wp_registered_widgets['search-2']['callback'][0] );
     103                $this->assertSame( $widget_better_search, $wp_registered_widgets['better_search-3']['callback'][0] );
     104                $this->assertSame( $widget_best_search, $wp_registered_widgets['best_search-4']['callback'][0] );
     105
     106                $this->assertArrayHasKey( spl_object_hash( $widget_better_search ), $wp_widget_factory->widgets );
     107                $this->assertArrayHasKey( spl_object_hash( $widget_best_search ), $wp_widget_factory->widgets );
     108                $this->assertArrayHasKey( 'WP_Widget_Search', $wp_widget_factory->widgets );
     109                unregister_widget( 'WP_Widget_Search' );
     110                unregister_widget( $widget_better_search );
     111                unregister_widget( $widget_best_search );
     112                $this->assertArrayNotHasKey( spl_object_hash( $widget_better_search ), $wp_widget_factory->widgets );
     113                $this->assertArrayNotHasKey( spl_object_hash( $widget_best_search ), $wp_widget_factory->widgets );
     114                $this->assertArrayNotHasKey( 'WP_Widget_Search', $wp_widget_factory->widgets );
     115        }
     116
     117        /**
    47118         * @group sidebar
    48119         */
    49120        function test_register_sidebars_single() {