Make WordPress Core

Ticket #28216: 28216.6.diff

File 28216.6.diff, 4.4 KB (added by westonruter, 8 years ago)

Fix docs

  • 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..1b6bab4 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(
     60                                'title' => '',
     61                        ),
     62                        '_multiwidget' => 1,
     63                ) );
     64                update_option( 'widget_better_search', array(
     65                        3 => array(
     66                                'title' => '',
     67                        ),
     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                $this->assertCount( 2, $wp_widget_factory->widgets );
     84                $this->assertArrayHasKey( 'WP_Widget_Search', $wp_widget_factory->widgets );
     85                $this->assertArrayHasKey( spl_object_hash( $widget_better_search ), $wp_widget_factory->widgets );
     86
     87                $wp_widget_factory->_register_widgets();
     88
     89                $this->assertArrayHasKey( 'search-2', $wp_registered_widgets );
     90                $this->assertArrayHasKey( 'better_search-3', $wp_registered_widgets );
     91                $this->assertInstanceOf( 'WP_Widget_Search', $wp_registered_widgets['search-2']['callback'][0] );
     92                $this->assertSame( $widget_better_search, $wp_registered_widgets['better_search-3']['callback'][0] );
     93
     94                $this->assertArrayHasKey( spl_object_hash( $widget_better_search ), $wp_widget_factory->widgets );
     95                $this->assertArrayHasKey( 'WP_Widget_Search', $wp_widget_factory->widgets );
     96                unregister_widget( 'WP_Widget_Search' );
     97                unregister_widget( $widget_better_search );
     98                $this->assertArrayNotHasKey( spl_object_hash( $widget_better_search ), $wp_widget_factory->widgets );
     99                $this->assertArrayNotHasKey( 'WP_Widget_Search', $wp_widget_factory->widgets );
     100        }
     101
     102        /**
    47103         * @group sidebar
    48104         */
    49105        function test_register_sidebars_single() {