Make WordPress Core

Changeset 37333


Ignore:
Timestamp:
04/30/2016 10:35:27 PM (10 years ago)
Author:
westonruter
Message:

Widgets: Provide PHP 5.2 fallback for spl_object_hash() if disabled in logic for registering and unregistering pre-instantiated widgets.

Fixes #28216.

Location:
trunk
Files:
2 edited

Legend:

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

    r37329 r37333  
    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 Object hash.
     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         *
    … …  
    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();
    … …  
    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 ] );
  • trunk/tests/phpunit/tests/widgets.php

    r37329 r37333  
    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();
    … …  
    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();
    … …  
    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        }
Note: See TracChangeset for help on using the changeset viewer.