Make WordPress Core

Ticket #28216: 28216.spl_object_hash-approach.patch

File 28216.spl_object_hash-approach.patch, 2.2 KB (added by PeterRKnight, 8 years ago)

mdwheele's patch using spl_object_hash instead of get_class allowing for multiple instances of class

  • tests/phpunit/tests/widgets.php

     
    4141        }
    4242
    4343        /**
     44         * @see register_widget()
     45         * @see unregister_widget()
     46         * @group 28216
     47         */
     48        function test_register_and_unregister_widget_instance() {
     49                global $wp_widget_factory;
     50
     51                $widget = new WP_Widget_Search();
     52                $widspl_object_hash = spl_object_hash( $widget );
     53
     54                register_widget( $widget );
     55                $this->assertArrayHasKey( $widspl_object_hash, $wp_widget_factory->widgets );
     56
     57                unregister_widget( $widget );
     58                $this->assertArrayNotHasKey( $widspl_object_hash, $wp_widget_factory->widgets );
     59        }
     60
     61        /**
    4462         * @see register_sidebars()
    4563         */
    4664        function test_register_sidebars_single() {
  • src/wp-includes/widgets.php

     
    573573         * @since 2.8.0
    574574         * @access public
    575575         *
    576          * @param string $widspl_object_hash The name of a {@see WP_Widget} subclass.
     576         * @param string | WP_Widget $widgetish Either the name of a {@see WP_Widget} subclass or an instance of a {@see WP_Widget} subclass.
    577577         */
    578         public function register( $widspl_object_hash ) {
    579                 $this->widgets[$widspl_object_hash] = new $widspl_object_hash();
     578        public function register( $widgetish ) {
     579                if ( $widgetish instanceof WP_Widget ) {
     580                        $this->widgets[ spl_object_hash( $widgetish ) ] = $widgetish;
     581                } else {
     582                        $this->widgets[ $widgetish ] = new $widgetish();
     583                }
    580584        }
    581585
    582586        /**
     
    585589         * @since 2.8.0
    586590         * @access public
    587591         *
    588          * @param string $widspl_object_hash The name of a {@see WP_Widget} subclass.
     592         * @param string | WP_Widget $widgetish Either the name of a {@see WP_Widget} subclass or an instance of a {@see WP_Widget} subclass.
    589593         */
    590         public function unregister( $widspl_object_hash ) {
    591                 unset( $this->widgets[ $widspl_object_hash ] );
     594        public function unregister( $widgetish ) {
     595                if ( $widgetish instanceof WP_Widget ) {
     596                        unset( $this->widgets[ spl_object_hash( $widgetish ) ] );
     597                } else {
     598                        unset( $this->widgets[ $widgetish ] );
     599                }
    592600        }
    593601
    594602        /**