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 { |
46 | 46 | } |
47 | 47 | |
48 | 48 | /** |
| 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 | /** |
49 | 93 | * Registers a widget subclass. |
50 | 94 | * |
51 | 95 | * @since 2.8.0 |
… |
… |
class WP_Widget_Factory { |
56 | 100 | */ |
57 | 101 | public function register( $widget ) { |
58 | 102 | if ( $widget instanceof WP_Widget ) { |
59 | | $this->widgets[ spl_object_hash( $widget ) ] = $widget; |
| 103 | $this->widgets[ $this->hash_object( $widget ) ] = $widget; |
60 | 104 | } else { |
61 | 105 | $this->widgets[ $widget ] = new $widget(); |
62 | 106 | } |
… |
… |
class WP_Widget_Factory { |
73 | 117 | */ |
74 | 118 | public function unregister( $widget ) { |
75 | 119 | if ( $widget instanceof WP_Widget ) { |
76 | | unset( $this->widgets[ spl_object_hash( $widget ) ] ); |
| 120 | unset( $this->widgets[ $this->hash_object( $widget ) ] ); |
77 | 121 | } else { |
78 | 122 | unset( $this->widgets[ $widget ] ); |
79 | 123 | } |
diff --git tests/phpunit/tests/widgets.php tests/phpunit/tests/widgets.php
index 0d0234c..d0fe7ca 100644
|
|
class Tests_Widgets extends WP_UnitTestCase { |
78 | 78 | $widget_better_search->widget_options['classname'] = 'widget_' . $widget_better_search->id_base; |
79 | 79 | $widget_better_search->control_options['id_base'] = $widget_better_search->id_base; |
80 | 80 | 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 ); |
82 | 82 | |
83 | 83 | $widget_best_search = new WP_Widget_Search(); |
84 | 84 | $widget_best_search->id_base = 'best_search'; |
… |
… |
class Tests_Widgets extends WP_UnitTestCase { |
87 | 87 | $widget_best_search->widget_options['classname'] = 'widget_' . $widget_best_search->id_base; |
88 | 88 | $widget_best_search->control_options['id_base'] = $widget_best_search->id_base; |
89 | 89 | 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 ); |
91 | 91 | |
92 | 92 | $this->assertCount( 3, $wp_widget_factory->widgets ); |
93 | 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 ); |
| 94 | $this->assertContains( $widget_better_search, $wp_widget_factory->widgets ); |
| 95 | $this->assertContains( $widget_best_search, $wp_widget_factory->widgets ); |
96 | 96 | |
97 | 97 | $wp_widget_factory->_register_widgets(); |
98 | 98 | |
… |
… |
class Tests_Widgets extends WP_UnitTestCase { |
103 | 103 | $this->assertSame( $widget_better_search, $wp_registered_widgets['better_search-3']['callback'][0] ); |
104 | 104 | $this->assertSame( $widget_best_search, $wp_registered_widgets['best_search-4']['callback'][0] ); |
105 | 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 ); |
| 106 | $this->assertContains( $widget_better_search, $wp_widget_factory->widgets ); |
| 107 | $this->assertContains( $widget_best_search, $wp_widget_factory->widgets ); |
108 | 108 | $this->assertArrayHasKey( 'WP_Widget_Search', $wp_widget_factory->widgets ); |
109 | 109 | unregister_widget( 'WP_Widget_Search' ); |
110 | 110 | unregister_widget( $widget_better_search ); |
111 | 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 ); |
| 112 | $this->assertNotContains( $widget_better_search, $wp_widget_factory->widgets ); |
| 113 | $this->assertNotContains( $widget_best_search, $wp_widget_factory->widgets ); |
114 | 114 | $this->assertArrayNotHasKey( 'WP_Widget_Search', $wp_widget_factory->widgets ); |
115 | 115 | } |
116 | 116 | |