WordPress 4.5
[autoinstalls/wordpress.git] / wp-includes / class-wp-widget-factory.php
1 <?php
2 /**
3  * Widget API: WP_Widget_Factory class
4  *
5  * @package WordPress
6  * @subpackage Widgets
7  * @since 4.4.0
8  */
9
10 /**
11  * Singleton that registers and instantiates WP_Widget classes.
12  *
13  * @since 2.8.0
14  * @since 4.4.0 Moved to its own file from wp-includes/widgets.php
15  */
16 class WP_Widget_Factory {
17
18         /**
19          * Widgets array.
20          *
21          * @since 2.8.0
22          * @access public
23          * @var array
24          */
25         public $widgets = array();
26
27         /**
28          * PHP5 constructor.
29          *
30          * @since 4.3.0
31          * @access public
32          */
33         public function __construct() {
34                 add_action( 'widgets_init', array( $this, '_register_widgets' ), 100 );
35         }
36
37         /**
38          * PHP4 constructor.
39          *
40          * @since 2.8.0
41          * @access public
42          */
43         public function WP_Widget_Factory() {
44                 _deprecated_constructor( 'WP_Widget_Factory', '4.2.0' );
45                 self::__construct();
46         }
47
48         /**
49          * Registers a widget subclass.
50          *
51          * @since 2.8.0
52          * @access public
53          *
54          * @param string $widget_class The name of a WP_Widget subclass.
55          */
56         public function register( $widget_class ) {
57                 $this->widgets[$widget_class] = new $widget_class();
58         }
59
60         /**
61          * Un-registers a widget subclass.
62          *
63          * @since 2.8.0
64          * @access public
65          *
66          * @param string $widget_class The name of a WP_Widget subclass.
67          */
68         public function unregister( $widget_class ) {
69                 unset( $this->widgets[ $widget_class ] );
70         }
71
72         /**
73          * Serves as a utility method for adding widgets to the registered widgets global.
74          *
75          * @since 2.8.0
76          * @access public
77          *
78          * @global array $wp_registered_widgets
79          */
80         public function _register_widgets() {
81                 global $wp_registered_widgets;
82                 $keys = array_keys($this->widgets);
83                 $registered = array_keys($wp_registered_widgets);
84                 $registered = array_map('_get_widget_id_base', $registered);
85
86                 foreach ( $keys as $key ) {
87                         // don't register new widget if old widget with the same id is already registered
88                         if ( in_array($this->widgets[$key]->id_base, $registered, true) ) {
89                                 unset($this->widgets[$key]);
90                                 continue;
91                         }
92
93                         $this->widgets[$key]->_register();
94                 }
95         }
96 }