WordPress.org

Make WordPress Core

Changeset 37329


Ignore:
Timestamp:
04/29/2016 06:48:27 PM (4 years ago)
Author:
westonruter
Message:

Widgets: Allow WP_Widget subclass instances (objects) to be registered/unregistered in addition to WP_Widget subclass names (strings).

Allows widgets to be registered which rely on dependency injection. Also will allow for new widget types to be created dynamically (e.g. a Recent Posts widget for each registered post type).

See #35990.
Props mdwheele, PeterRKnight, westonruter.
Fixes #28216.

Location:
trunk
Files:
2 edited

Legend:

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

    r37063 r37329  
    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
     
    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
  • trunk/tests/phpunit/tests/widgets.php

    r36130 r37329  
    4242        unregister_widget( $widget_class );
    4343        $this->assertArrayNotHasKey( $widget_class, $wp_widget_factory->widgets );
     44    }
     45
     46    /**
     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 );
    44115    }
    45116
Note: See TracChangeset for help on using the changeset viewer.