diff --git src/wp-includes/class-wp-widget-factory.php src/wp-includes/class-wp-widget-factory.php
index 0616258..c4398b8 100644
|
|
|
class WP_Widget_Factory { |
| 23 | 23 | * @var array |
| 24 | 24 | */ |
| 25 | 25 | public $widgets = array(); |
| | 26 | |
| | 27 | |
| | 28 | /** |
| | 29 | * Base Ids for registered widgets. |
| | 30 | * |
| | 31 | * @since 4.9 |
| | 32 | * @access public |
| | 33 | * @var array |
| | 34 | */ |
| | 35 | public $widget_base_ids = array(); |
| 26 | 36 | |
| 27 | 37 | /** |
| 28 | 38 | * PHP5 constructor. |
| … |
… |
class WP_Widget_Factory { |
| 32 | 42 | */ |
| 33 | 43 | public function __construct() { |
| 34 | 44 | add_action( 'widgets_init', array( $this, '_register_widgets' ), 100 ); |
| | 45 | add_filter( 'sidebars_widgets', array( $this, '_purge_unregistered_widgets' ) ); |
| 35 | 46 | } |
| 36 | 47 | |
| 37 | 48 | /** |
| … |
… |
class WP_Widget_Factory { |
| 102 | 113 | public function register( $widget ) { |
| 103 | 114 | if ( $widget instanceof WP_Widget ) { |
| 104 | 115 | $this->widgets[ $this->hash_object( $widget ) ] = $widget; |
| | 116 | $this->widget_base_ids[] = $widget->id_base; |
| 105 | 117 | } else { |
| 106 | | $this->widgets[ $widget ] = new $widget(); |
| | 118 | $instance = new $widget(); |
| | 119 | $this->widgets[ $widget ] = $instance; |
| | 120 | $this->widget_base_ids[] = $instance->id_base; |
| 107 | 121 | } |
| 108 | 122 | } |
| 109 | 123 | |
| … |
… |
class WP_Widget_Factory { |
| 120 | 134 | public function unregister( $widget ) { |
| 121 | 135 | if ( $widget instanceof WP_Widget ) { |
| 122 | 136 | unset( $this->widgets[ $this->hash_object( $widget ) ] ); |
| | 137 | unset( $this->widget_base_ids[ $widget->id_base ] ); |
| 123 | 138 | } else { |
| 124 | 139 | unset( $this->widgets[ $widget ] ); |
| | 140 | unset( $this->widget_base_ids[ $this->widgets[ $widget ]->id_base ] ); |
| 125 | 141 | } |
| 126 | 142 | } |
| 127 | 143 | |
| … |
… |
class WP_Widget_Factory { |
| 149 | 165 | $this->widgets[$key]->_register(); |
| 150 | 166 | } |
| 151 | 167 | } |
| | 168 | |
| | 169 | |
| | 170 | /** |
| | 171 | * Utility method to purge any widget instances that are not registered. |
| | 172 | * |
| | 173 | * @since 4.9 |
| | 174 | * @access public |
| | 175 | * @param $sidebars_widgets |
| | 176 | * |
| | 177 | * @return array $sidebars_widgets |
| | 178 | */ |
| | 179 | public function _purge_unregistered_widgets( $sidebars_widgets ) { |
| | 180 | foreach ( $sidebars_widgets as $sidebar => $widgets ) { |
| | 181 | for( $i = 0; $i < count ( $widgets ); $i++ ) { |
| | 182 | $id_base = _get_widget_id_base( $widgets[ $i ] ); |
| | 183 | if ( ! in_array( $id_base, $this->widget_base_ids, true ) ) { |
| | 184 | unset( $sidebars_widgets[ $sidebar ][ $i ] ); |
| | 185 | } |
| | 186 | } |
| | 187 | } |
| | 188 | return $sidebars_widgets; |
| | 189 | } |
| 152 | 190 | } |