Make WordPress Core

Ticket #28216: 28216.8.fallback.diff

File 28216.8.fallback.diff, 5.2 KB (added by westonruter, 8 years ago)

Provide fallback if spl_object_hash() is not available

  • 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 9b9214e..ce37708 100644
    class WP_Widget_Factory { 
    4646        }
    4747
    4848        /**
     49         * Memory for the number of times unique class instances have been hashed.
     50         *
     51         * This can be eliminated in favor of straight spl_object_hash() when 5.3
     52         * is the minimum requirement for PHP.
     53         *
     54         * @since 4.6.0
     55         * @access private
     56         * @see WP_Widget_Factory::hash_object()
     57         *
     58         * @var array
     59         */
     60        private $hashed_class_counts = array();
     61
     62        /**
     63         * Hash an object, doing fallback of `spl_object_hash()` if not available.
     64         *
     65         * This can be eliminated in favor of straight spl_object_hash() when 5.3
     66         * is the minimum requirement for PHP.
     67         *
     68         * @since 4.6.0
     69         * @access private
     70         *
     71         * @param WP_Widget $widget Widget.
     72         * @return string
     73         */
     74        private function hash_object( $widget ) {
     75                if ( function_exists( 'spl_object_hash' ) ) {
     76                        return spl_object_hash( $widget );
     77                } else {
     78                        $class_name = get_class( $widget );
     79                        $hash = $class_name;
     80                        if ( ! isset( $widget->_wp_widget_factory_hash_id ) ) {
     81                                if ( ! isset( $this->hashed_class_counts[ $class_name ] ) ) {
     82                                        $this->hashed_class_counts[ $class_name ] = 0;
     83                                }
     84                                $this->hashed_class_counts[ $class_name ] += 1;
     85                                $widget->_wp_widget_factory_hash_id = $this->hashed_class_counts[ $class_name ];
     86                        }
     87                        $hash .= ':' . $widget->_wp_widget_factory_hash_id;
     88                        return $hash;
     89                }
     90        }
     91
     92        /**
    4993         * Registers a widget subclass.
    5094         *
    5195         * @since 2.8.0
    class WP_Widget_Factory { 
    56100         */
    57101        public function register( $widget ) {
    58102                if ( $widget instanceof WP_Widget ) {
    59                         $this->widgets[ spl_object_hash( $widget ) ] = $widget;
     103                        $this->widgets[ $this->hash_object( $widget ) ] = $widget;
    60104                } else {
    61105                        $this->widgets[ $widget ] = new $widget();
    62106                }
    class WP_Widget_Factory { 
    73117         */
    74118        public function unregister( $widget ) {
    75119                if ( $widget instanceof WP_Widget ) {
    76                         unset( $this->widgets[ spl_object_hash( $widget ) ] );
     120                        unset( $this->widgets[ $this->hash_object( $widget ) ] );
    77121                } else {
    78122                        unset( $this->widgets[ $widget ] );
    79123                }
  • tests/phpunit/tests/widgets.php

    diff --git tests/phpunit/tests/widgets.php tests/phpunit/tests/widgets.php
    index 0d0234c..d0fe7ca 100644
    class Tests_Widgets extends WP_UnitTestCase { 
    7878                $widget_better_search->widget_options['classname'] = 'widget_' . $widget_better_search->id_base;
    7979                $widget_better_search->control_options['id_base'] = $widget_better_search->id_base;
    8080                register_widget( $widget_better_search );
    81                 $this->assertArrayHasKey( spl_object_hash( $widget_better_search ), $wp_widget_factory->widgets );
     81                $this->assertContains( $widget_better_search, $wp_widget_factory->widgets );
    8282
    8383                $widget_best_search = new WP_Widget_Search();
    8484                $widget_best_search->id_base = 'best_search';
    class Tests_Widgets extends WP_UnitTestCase { 
    8787                $widget_best_search->widget_options['classname'] = 'widget_' . $widget_best_search->id_base;
    8888                $widget_best_search->control_options['id_base'] = $widget_best_search->id_base;
    8989                register_widget( $widget_best_search );
    90                 $this->assertArrayHasKey( spl_object_hash( $widget_best_search ), $wp_widget_factory->widgets );
     90                $this->assertContains( $widget_best_search, $wp_widget_factory->widgets );
    9191
    9292                $this->assertCount( 3, $wp_widget_factory->widgets );
    9393                $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 );
     94                $this->assertContains( $widget_better_search, $wp_widget_factory->widgets );
     95                $this->assertContains( $widget_best_search, $wp_widget_factory->widgets );
    9696
    9797                $wp_widget_factory->_register_widgets();
    9898
    class Tests_Widgets extends WP_UnitTestCase { 
    103103                $this->assertSame( $widget_better_search, $wp_registered_widgets['better_search-3']['callback'][0] );
    104104                $this->assertSame( $widget_best_search, $wp_registered_widgets['best_search-4']['callback'][0] );
    105105
    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 );
     106                $this->assertContains( $widget_better_search, $wp_widget_factory->widgets );
     107                $this->assertContains( $widget_best_search, $wp_widget_factory->widgets );
    108108                $this->assertArrayHasKey( 'WP_Widget_Search', $wp_widget_factory->widgets );
    109109                unregister_widget( 'WP_Widget_Search' );
    110110                unregister_widget( $widget_better_search );
    111111                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 );
     112                $this->assertNotContains( $widget_better_search, $wp_widget_factory->widgets );
     113                $this->assertNotContains( $widget_best_search, $wp_widget_factory->widgets );
    114114                $this->assertArrayNotHasKey( 'WP_Widget_Search', $wp_widget_factory->widgets );
    115115        }
    116116