1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
<?php
/** * Handles registering Widgets Bundle widgets. * * Class SiteOrigin_Widgets_Widget_Manager */ class SiteOrigin_Widgets_Widget_Manager { /** * Regsitered widgets * * @var */ private $registered;
function __construct(){ $this->registered = array(); add_action( 'widgets_init', array( $this, 'widgets_init' ) ); }
/** * Get the single instance. * * @return SiteOrigin_Widgets_Widget_Manager */ static function single() { static $single;
if( empty($single) ) { $single = new self(); }
return $single; }
/** * @param $id * @param $path * @param bool|false $class * @return mixed */ public function register( $id, $path, $class = false ){ $path = wp_normalize_path( $path ); if ( empty( $class ) ) { $class = 'SiteOrigin_Widget_' . str_replace( ' ', '', ucwords( str_replace('-', ' ', $id) ) ) . '_Widget'; }
$this->registered[ $id ] = new stdClass(); $this->registered[ $id ]->path = $path; $this->registered[ $id ]->class = $class; $this->registered[ $id ]->registered = false;
return $this->registered[ $id ]; }
/** * Initialize all the widgets. */ public function widgets_init(){ foreach( $this->registered as $id => & $info ) { if( $info->registered ) continue; register_widget( $info->class ); $info->registered = true; } }
/** * Get the path of the widget * * @param $id * * @return bool */ public function get_plugin_path( $id ) { if( empty( $this->registered[ $id ] ) ) { // This call might be using the incorrect ID convention. if( substr($id, 0, 4) == 'sow-' ) $id = substr($id, 4); else $id = 'sow-' . $id; }
return !empty($this->registered[$id]) ? $this->registered[$id]->path : false; }
/** * @param $id * * @return string * * @todo examine this when using a widget in a theme folder. */ function get_plugin_dir_path( $id ){ return plugin_dir_path( $this->get_plugin_path( $id ) ); }
function get_plugin_dir_url( $id ){ return plugin_dir_url( $this->get_plugin_path( $id ) ); }
/** * Get a widget ID from a file path * * @param string $path The file path. * * @return string The ID. */ function get_id_from_path( $path ){ foreach( $this->registered as $id => $r ) { if( $r->path == $path ) return $id; } return false; }
/** * Get the class name of a widget from the * * @param $path * @return mixed */ function get_class_from_path( $path ) { foreach( $this->registered as $id => $r ) { if( $r->path == $path ) return $r->class; } return false; } /** * Get the list of registered widgets. * * @return array */ function get_registered_widgets() { return $this->registered; } /** * Get the registered widget instance by it's class name or the hash generated when it was registered. * * @param $class_or_hash * * @return array * */ static function get_widget_instance( $class_or_hash ) { global $wp_widget_factory; if ( isset( $wp_widget_factory->widgets[ $class_or_hash ] ) ) { return $wp_widget_factory->widgets[ $class_or_hash ]; } else { foreach ( $wp_widget_factory->widgets as $widget_instance ) { if ( $widget_instance instanceof $class_or_hash ) { return $widget_instance; } } } return null; } } SiteOrigin_Widgets_Widget_Manager::single();
/** * Register a widget * * @param string $id The ID of the widget * @param string $path The path of the widget * @param bool|string $class The name of the class */ function siteorigin_widget_register( $id, $path, $class = false ){ SiteOrigin_Widgets_Widget_Manager::single()->register( $id, $path, $class ); }
/** * Get the base file of a widget plugin * * @param $name * @return bool */ function siteorigin_widget_get_plugin_path($id){ return SiteOrigin_Widgets_Widget_Manager::single()->get_plugin_path( $id ); }
/** * Get the base path folder of a widget plugin. * * @param $id * @return string */ function siteorigin_widget_get_plugin_dir_path($id){ return SiteOrigin_Widgets_Widget_Manager::single()->get_plugin_dir_path( $id ); }
/** * Get the base path URL of a widget plugin. * * @param $id * @return string */ function siteorigin_widget_get_plugin_dir_url($id){ return SiteOrigin_Widgets_Widget_Manager::single()->get_plugin_dir_url( $id ); }
|