diff -r 490d5cc509ed -r cf61fcea0001 wp/wp-includes/class-wp-widget-factory.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wp/wp-includes/class-wp-widget-factory.php Mon Oct 14 17:39:30 2019 +0200 @@ -0,0 +1,144 @@ +_wp_widget_factory_hash_id ) ) { + if ( ! isset( $this->hashed_class_counts[ $class_name ] ) ) { + $this->hashed_class_counts[ $class_name ] = 0; + } + $this->hashed_class_counts[ $class_name ] += 1; + $widget->_wp_widget_factory_hash_id = $this->hashed_class_counts[ $class_name ]; + } + $hash .= ':' . $widget->_wp_widget_factory_hash_id; + return $hash; + } + } + + /** + * Registers a widget subclass. + * + * @since 2.8.0 + * @since 4.6.0 Updated the `$widget` parameter to also accept a WP_Widget instance object + * instead of simply a `WP_Widget` subclass name. + * + * @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass. + */ + public function register( $widget ) { + if ( $widget instanceof WP_Widget ) { + $this->widgets[ $this->hash_object( $widget ) ] = $widget; + } else { + $this->widgets[ $widget ] = new $widget(); + } + } + + /** + * Un-registers a widget subclass. + * + * @since 2.8.0 + * @since 4.6.0 Updated the `$widget` parameter to also accept a WP_Widget instance object + * instead of simply a `WP_Widget` subclass name. + * + * @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass. + */ + public function unregister( $widget ) { + if ( $widget instanceof WP_Widget ) { + unset( $this->widgets[ $this->hash_object( $widget ) ] ); + } else { + unset( $this->widgets[ $widget ] ); + } + } + + /** + * Serves as a utility method for adding widgets to the registered widgets global. + * + * @since 2.8.0 + * + * @global array $wp_registered_widgets + */ + public function _register_widgets() { + global $wp_registered_widgets; + $keys = array_keys($this->widgets); + $registered = array_keys($wp_registered_widgets); + $registered = array_map('_get_widget_id_base', $registered); + + foreach ( $keys as $key ) { + // don't register new widget if old widget with the same id is already registered + if ( in_array($this->widgets[$key]->id_base, $registered, true) ) { + unset($this->widgets[$key]); + continue; + } + + $this->widgets[$key]->_register(); + } + } +}