Changeset 37333
- Timestamp:
- 04/30/2016 10:35:27 PM (9 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-widget-factory.php
r37329 r37333 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 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 /** 49 93 * Registers a widget subclass. 50 94 * … … 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(); … … 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 ] ); -
trunk/tests/phpunit/tests/widgets.php
r37329 r37333 79 79 $widget_better_search->control_options['id_base'] = $widget_better_search->id_base; 80 80 register_widget( $widget_better_search ); 81 $this->assert ArrayHasKey( 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(); … … 88 88 $widget_best_search->control_options['id_base'] = $widget_best_search->id_base; 89 89 register_widget( $widget_best_search ); 90 $this->assert ArrayHasKey( 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->assert ArrayHasKey( spl_object_hash( $widget_better_search ), $wp_widget_factory->widgets );95 $this->assert ArrayHasKey( 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(); … … 104 104 $this->assertSame( $widget_best_search, $wp_registered_widgets['best_search-4']['callback'][0] ); 105 105 106 $this->assert ArrayHasKey( spl_object_hash( $widget_better_search ), $wp_widget_factory->widgets );107 $this->assert ArrayHasKey( 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->assert ArrayNotHasKey( spl_object_hash( $widget_better_search ), $wp_widget_factory->widgets );113 $this->assert ArrayNotHasKey( 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 }
Note: See TracChangeset
for help on using the changeset viewer.