3 * Widget API: WP_Widget_Factory class
11 * Singleton that registers and instantiates WP_Widget classes.
14 * @since 4.4.0 Moved to its own file from wp-includes/widgets.php
16 class WP_Widget_Factory {
17 public $widgets = array();
22 public function __construct() {
23 add_action( 'widgets_init', array( $this, '_register_widgets' ), 100 );
29 public function WP_Widget_Factory() {
30 _deprecated_constructor( 'WP_Widget_Factory', '4.2.0' );
35 * Register a widget subclass.
40 * @param string $widget_class The name of a {@see WP_Widget} subclass.
42 public function register( $widget_class ) {
43 $this->widgets[$widget_class] = new $widget_class();
47 * Un-register a widget subclass.
52 * @param string $widget_class The name of a {@see WP_Widget} subclass.
54 public function unregister( $widget_class ) {
55 unset( $this->widgets[ $widget_class ] );
59 * Utility method for adding widgets to the registered widgets global.
64 * @global array $wp_registered_widgets
66 public function _register_widgets() {
67 global $wp_registered_widgets;
68 $keys = array_keys($this->widgets);
69 $registered = array_keys($wp_registered_widgets);
70 $registered = array_map('_get_widget_id_base', $registered);
72 foreach ( $keys as $key ) {
73 // don't register new widget if old widget with the same id is already registered
74 if ( in_array($this->widgets[$key]->id_base, $registered, true) ) {
75 unset($this->widgets[$key]);
79 $this->widgets[$key]->_register();