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 {
25 public $widgets = array();
33 public function __construct() {
34 add_action( 'widgets_init', array( $this, '_register_widgets' ), 100 );
43 public function WP_Widget_Factory() {
44 _deprecated_constructor( 'WP_Widget_Factory', '4.2.0' );
49 * Memory for the number of times unique class instances have been hashed.
51 * This can be eliminated in favor of straight spl_object_hash() when 5.3
52 * is the minimum requirement for PHP.
58 * @see WP_Widget_Factory::hash_object()
60 private $hashed_class_counts = array();
63 * Hashes an object, doing fallback of `spl_object_hash()` if not available.
65 * This can be eliminated in favor of straight spl_object_hash() when 5.3
66 * is the minimum requirement for PHP.
71 * @param WP_Widget $widget Widget.
72 * @return string Object hash.
74 private function hash_object( $widget ) {
75 if ( function_exists( 'spl_object_hash' ) ) {
76 return spl_object_hash( $widget );
78 $class_name = get_class( $widget );
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;
84 $this->hashed_class_counts[ $class_name ] += 1;
85 $widget->_wp_widget_factory_hash_id = $this->hashed_class_counts[ $class_name ];
87 $hash .= ':' . $widget->_wp_widget_factory_hash_id;
93 * Registers a widget subclass.
96 * @since 4.6.0 Updated the `$widget` parameter to also accept a WP_Widget instance object
97 * instead of simply a `WP_Widget` subclass name.
100 * @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass.
102 public function register( $widget ) {
103 if ( $widget instanceof WP_Widget ) {
104 $this->widgets[ $this->hash_object( $widget ) ] = $widget;
106 $this->widgets[ $widget ] = new $widget();
111 * Un-registers a widget subclass.
114 * @since 4.6.0 Updated the `$widget` parameter to also accept a WP_Widget instance object
115 * instead of simply a `WP_Widget` subclass name.
118 * @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass.
120 public function unregister( $widget ) {
121 if ( $widget instanceof WP_Widget ) {
122 unset( $this->widgets[ $this->hash_object( $widget ) ] );
124 unset( $this->widgets[ $widget ] );
129 * Serves as a utility method for adding widgets to the registered widgets global.
134 * @global array $wp_registered_widgets
136 public function _register_widgets() {
137 global $wp_registered_widgets;
138 $keys = array_keys($this->widgets);
139 $registered = array_keys($wp_registered_widgets);
140 $registered = array_map('_get_widget_id_base', $registered);
142 foreach ( $keys as $key ) {
143 // don't register new widget if old widget with the same id is already registered
144 if ( in_array($this->widgets[$key]->id_base, $registered, true) ) {
145 unset($this->widgets[$key]);
149 $this->widgets[$key]->_register();