These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Elgg plugins library |
||
4 | * Contains functions for managing plugins |
||
5 | */ |
||
6 | |||
7 | use Elgg\Filesystem\Directory; |
||
0 ignored issues
–
show
|
|||
8 | |||
9 | |||
10 | /** |
||
11 | * Tells \ElggPlugin::start() to include the start.php file. |
||
12 | */ |
||
13 | define('ELGG_PLUGIN_INCLUDE_START', 1); |
||
14 | |||
15 | /** |
||
16 | * Tells \ElggPlugin::start() to automatically register the plugin's views. |
||
17 | */ |
||
18 | define('ELGG_PLUGIN_REGISTER_VIEWS', 2); |
||
19 | |||
20 | /** |
||
21 | * Tells \ElggPlugin::start() to automatically register the plugin's languages. |
||
22 | */ |
||
23 | define('ELGG_PLUGIN_REGISTER_LANGUAGES', 4); |
||
24 | |||
25 | /** |
||
26 | * Tells \ElggPlugin::start() to automatically register the plugin's classes. |
||
27 | */ |
||
28 | define('ELGG_PLUGIN_REGISTER_CLASSES', 8); |
||
29 | |||
30 | /** |
||
31 | * Tells \ElggPlugin::start() to automatically register the plugin's actions. |
||
32 | */ |
||
33 | define('ELGG_PLUGIN_REGISTER_ACTIONS', 16); |
||
34 | |||
35 | /** |
||
36 | * Tells \ElggPlugin::start() to automatically register the plugin's actions. |
||
37 | */ |
||
38 | define('ELGG_PLUGIN_REGISTER_WIDGETS', 32); |
||
39 | |||
40 | /** |
||
41 | * Prefix for plugin setting names |
||
42 | * |
||
43 | * @todo Can't namespace these because many plugins directly call |
||
44 | * private settings via $entity->$name. |
||
45 | */ |
||
46 | //define('ELGG_PLUGIN_SETTING_PREFIX', 'plugin:setting:'); |
||
47 | |||
48 | /** |
||
49 | * Prefix for plugin user setting names |
||
50 | */ |
||
51 | define('ELGG_PLUGIN_USER_SETTING_PREFIX', 'plugin:user_setting:'); |
||
52 | |||
53 | /** |
||
54 | * Internal settings prefix |
||
55 | * |
||
56 | * @todo This could be resolved by promoting \ElggPlugin to a 5th type. |
||
57 | */ |
||
58 | define('ELGG_PLUGIN_INTERNAL_PREFIX', 'elgg:internal:'); |
||
59 | |||
60 | /** |
||
61 | * Discovers plugins in the plugins_path setting and creates \ElggPlugin |
||
62 | * entities for them if they don't exist. If there are plugins with entities |
||
63 | * but not actual files, will disable the \ElggPlugin entities and mark as inactive. |
||
64 | * The \ElggPlugin object holds config data, so don't delete. |
||
65 | * |
||
66 | * @return bool |
||
67 | * @since 1.8.0 |
||
68 | * @access private |
||
69 | */ |
||
70 | function _elgg_generate_plugin_entities() { |
||
71 | return _elgg_services()->plugins->generateEntities(); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Cache a reference to this plugin by its ID |
||
76 | * |
||
77 | * @param \ElggPlugin $plugin |
||
78 | * |
||
79 | * @access private |
||
80 | */ |
||
81 | function _elgg_cache_plugin_by_id(\ElggPlugin $plugin) { |
||
82 | return _elgg_services()->plugins->cache($plugin); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Returns an \ElggPlugin object with the path $path. |
||
87 | * |
||
88 | * @param string $plugin_id The id (dir name) of the plugin. NOT the guid. |
||
89 | * @return \ElggPlugin|null |
||
90 | * @since 1.8.0 |
||
91 | */ |
||
92 | function elgg_get_plugin_from_id($plugin_id) { |
||
93 | return _elgg_services()->plugins->get($plugin_id); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Returns if a plugin exists in the system. |
||
98 | * |
||
99 | * @warning This checks only plugins that are registered in the system! |
||
100 | * If the plugin cache is outdated, be sure to regenerate it with |
||
101 | * {@link _elgg_generate_plugin_objects()} first. |
||
102 | * |
||
103 | * @param string $id The plugin ID. |
||
104 | * @since 1.8.0 |
||
105 | * @return bool |
||
106 | */ |
||
107 | function elgg_plugin_exists($id) { |
||
108 | return _elgg_services()->plugins->exists($id); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Returns the highest priority of the plugins |
||
113 | * |
||
114 | * @return int |
||
115 | * @since 1.8.0 |
||
116 | * @access private |
||
117 | */ |
||
118 | function _elgg_get_max_plugin_priority() { |
||
119 | return _elgg_services()->plugins->getMaxPriority(); |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Returns if a plugin is active for a current site. |
||
124 | * |
||
125 | * @param string $plugin_id The plugin ID |
||
126 | * @since 1.8.0 |
||
127 | * @return bool |
||
128 | */ |
||
129 | function elgg_is_active_plugin($plugin_id) { |
||
130 | return _elgg_services()->plugins->isActive($plugin_id); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Returns an ordered list of plugins |
||
135 | * |
||
136 | * @param string $status The status of the plugins. active, inactive, or all. |
||
137 | * @return \ElggPlugin[] |
||
138 | * @since 1.8.0 |
||
139 | */ |
||
140 | function elgg_get_plugins($status = 'active') { |
||
141 | return _elgg_services()->plugins->find($status); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Namespaces a string to be used as a private setting name for a plugin. |
||
146 | * |
||
147 | * For user_settings, two namespaces are added: a user setting namespace and the |
||
148 | * plugin id. |
||
149 | * |
||
150 | * For internal (plugin priority), there is a single internal namespace added. |
||
151 | * |
||
152 | * @param string $type The type of setting: user_setting or internal. |
||
153 | * @param string $name The name to namespace. |
||
154 | * @param string $id The plugin's ID to namespace with. Required for user_setting. |
||
155 | * @return string |
||
156 | * @since 1.8.0 |
||
157 | * @access private |
||
158 | */ |
||
159 | function _elgg_namespace_plugin_private_setting($type, $name, $id = null) { |
||
160 | return _elgg_services()->plugins->namespacePrivateSetting($type, $name, $id); |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Deletes all cached data on plugins being provided. |
||
165 | * |
||
166 | * @return boolean |
||
167 | * @since 1.9.0 |
||
168 | * @access private |
||
169 | */ |
||
170 | function _elgg_invalidate_plugins_provides_cache() { |
||
171 | return _elgg_services()->plugins->invalidateProvidesCache(); |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Checks if a plugin is currently providing $type and $name, and optionally |
||
176 | * checking a version. |
||
177 | * |
||
178 | * @param string $type The type of the provide |
||
179 | * @param string $name The name of the provide |
||
180 | * @param string $version A version to check against |
||
181 | * @param string $comparison The comparison operator to use in version_compare() |
||
182 | * |
||
183 | * @return array An array in the form array( |
||
184 | * 'status' => bool Does the provide exist?, |
||
185 | * 'value' => string The version provided |
||
186 | * ) |
||
187 | * @since 1.8.0 |
||
188 | * @access private |
||
189 | */ |
||
190 | function _elgg_check_plugins_provides($type, $name, $version = null, $comparison = 'ge') { |
||
191 | return _elgg_services()->plugins->checkProvides($type, $name, $version, $comparison); |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Returns an array of parsed strings for a dependency in the |
||
196 | * format: array( |
||
197 | * 'type' => requires, conflicts, or provides. |
||
198 | * 'name' => The name of the requirement / conflict |
||
199 | * 'value' => A string representing the expected value: <1, >=3, !=enabled |
||
200 | * 'local_value' => The current value, ("Not installed") |
||
201 | * 'comment' => Free form text to help resovle the problem ("Enable / Search for plugin <link>") |
||
202 | * ) |
||
203 | * |
||
204 | * @param array $dep An \ElggPluginPackage dependency array |
||
205 | * @return array |
||
206 | * @since 1.8.0 |
||
207 | * @access private |
||
208 | */ |
||
209 | function _elgg_get_plugin_dependency_strings($dep) { |
||
210 | return _elgg_services()->plugins->getDependencyStrings($dep); |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * Returns an array of all plugin user settings for a user. |
||
215 | * |
||
216 | * @param int $user_guid The user GUID or 0 for the currently logged in user. |
||
217 | * @param string $plugin_id The plugin ID (Required) |
||
218 | * @param bool $return_obj Return settings as an object? This can be used to in reusable |
||
219 | * views where the settings are passed as $vars['entity']. |
||
220 | * @return array |
||
221 | * @since 1.8.0 |
||
222 | * @see \ElggPlugin::getAllUserSettings() |
||
223 | */ |
||
224 | function elgg_get_all_plugin_user_settings($user_guid = 0, $plugin_id = null, $return_obj = false) { |
||
225 | return _elgg_services()->plugins->getAllUserSettings($user_guid, $plugin_id, $return_obj); |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * Set a user specific setting for a plugin. |
||
230 | * |
||
231 | * @param string $name The name. Note: cannot be "title". |
||
232 | * @param mixed $value The value. |
||
233 | * @param int $user_guid The user GUID or 0 for the currently logged in user. |
||
234 | * @param string $plugin_id The plugin ID (Required) |
||
235 | * |
||
236 | * @return bool |
||
237 | * @since 1.8.0 |
||
238 | * @see \ElggPlugin::setUserSetting() |
||
239 | */ |
||
240 | function elgg_set_plugin_user_setting($name, $value, $user_guid = 0, $plugin_id = null) { |
||
241 | return _elgg_services()->plugins->setUserSetting($name, $value, $user_guid, $plugin_id); |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * Unsets a user-specific plugin setting |
||
246 | * |
||
247 | * @param string $name Name of the setting |
||
248 | * @param int $user_guid The user GUID or 0 for the currently logged in user. |
||
249 | * @param string $plugin_id The plugin ID (Required) |
||
250 | * |
||
251 | * @return bool |
||
252 | * @since 1.8.0 |
||
253 | * @see \ElggPlugin::unsetUserSetting() |
||
254 | */ |
||
255 | function elgg_unset_plugin_user_setting($name, $user_guid = 0, $plugin_id = null) { |
||
256 | return _elgg_services()->plugins->unsetUserSetting($name, $user_guid, $plugin_id); |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Get a user specific setting for a plugin. |
||
261 | * |
||
262 | * @param string $name The name of the setting. |
||
263 | * @param int $user_guid The user GUID or 0 for the currently logged in user. |
||
264 | * @param string $plugin_id The plugin ID (Required) |
||
265 | * @param mixed $default The default value to return if none is set |
||
266 | * |
||
267 | * @return mixed |
||
268 | * @since 1.8.0 |
||
269 | * @see \ElggPlugin::getUserSetting() |
||
270 | */ |
||
271 | function elgg_get_plugin_user_setting($name, $user_guid = 0, $plugin_id = null, $default = null) { |
||
272 | return _elgg_services()->plugins->getUserSetting($name, $user_guid, $plugin_id, $default); |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Set a setting for a plugin. |
||
277 | * |
||
278 | * @param string $name The name of the setting - note, can't be "title". |
||
279 | * @param mixed $value The value. |
||
280 | * @param string $plugin_id The plugin ID (Required) |
||
281 | * |
||
282 | * @return bool |
||
283 | * @since 1.8.0 |
||
284 | * @see \ElggPlugin::setSetting() |
||
285 | */ |
||
286 | function elgg_set_plugin_setting($name, $value, $plugin_id) { |
||
287 | return _elgg_services()->plugins->setSetting($name, $value, $plugin_id); |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Get setting for a plugin. |
||
292 | * |
||
293 | * @param string $name The name of the setting. |
||
294 | * @param string $plugin_id The plugin ID (Required) |
||
295 | * @param mixed $default The default value to return if none is set |
||
296 | * |
||
297 | * @return mixed |
||
298 | * @since 1.8.0 |
||
299 | * @see \ElggPlugin::getSetting() |
||
300 | */ |
||
301 | function elgg_get_plugin_setting($name, $plugin_id, $default = null) { |
||
302 | return _elgg_services()->plugins->getSetting($name, $plugin_id, $default); |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * Unsets a plugin setting. |
||
307 | * |
||
308 | * @param string $name The name of the setting. |
||
309 | * @param string $plugin_id The plugin ID (Required) |
||
310 | * |
||
311 | * @return bool |
||
312 | * @since 1.8.0 |
||
313 | * @see \ElggPlugin::unsetSetting() |
||
314 | */ |
||
315 | function elgg_unset_plugin_setting($name, $plugin_id) { |
||
316 | return _elgg_services()->plugins->unsetSetting($name, $plugin_id); |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * Unsets all plugin settings for a plugin. |
||
321 | * |
||
322 | * @param string $plugin_id The plugin ID (Required) |
||
323 | * |
||
324 | * @return bool |
||
325 | * @since 1.8.0 |
||
326 | * @see \ElggPlugin::unsetAllSettings() |
||
327 | */ |
||
328 | function elgg_unset_all_plugin_settings($plugin_id) { |
||
329 | return _elgg_services()->plugins->unsetAllSettings($plugin_id); |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * Returns entities based upon plugin user settings. |
||
334 | * Takes all the options for {@link elgg_get_entities_from_private_settings()} |
||
335 | * in addition to the ones below. |
||
336 | * |
||
337 | * @param array $options Array in the format: |
||
338 | * |
||
339 | * plugin_id => STR The plugin id. Required. |
||
340 | * |
||
341 | * plugin_user_setting_names => null|ARR private setting names |
||
342 | * |
||
343 | * plugin_user_setting_values => null|ARR metadata values |
||
344 | * |
||
345 | * plugin_user_setting_name_value_pairs => null|ARR ( |
||
346 | * name => 'name', |
||
347 | * value => 'value', |
||
348 | * 'operand' => '=', |
||
349 | * ) |
||
350 | * Currently if multiple values are sent via |
||
351 | * an array (value => array('value1', 'value2') |
||
352 | * the pair's operand will be forced to "IN". |
||
353 | * |
||
354 | * plugin_user_setting_name_value_pairs_operator => null|STR The operator to use for combining |
||
355 | * (name = value) OPERATOR (name = value); default AND |
||
356 | * |
||
357 | * @return mixed int If count, int. If not count, array. false on errors. |
||
358 | * @since 1.8.0 |
||
359 | */ |
||
360 | function elgg_get_entities_from_plugin_user_settings(array $options = []) { |
||
361 | return _elgg_services()->plugins->getEntitiesFromUserSettings($options); |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * Runs unit tests for plugin API. |
||
366 | * |
||
367 | * @param string $hook unit_test |
||
368 | * @param string $type system |
||
369 | * @param mixed $value Array of tests |
||
370 | * @param mixed $params Params |
||
371 | * |
||
372 | * @return array |
||
373 | * @access private |
||
374 | */ |
||
375 | function _elgg_plugins_test($hook, $type, $value, $params) { |
||
376 | global $CONFIG; |
||
377 | $value[] = $CONFIG->path . 'engine/tests/ElggCorePluginsAPITest.php'; |
||
378 | return $value; |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * Initialize the plugin system |
||
383 | * |
||
384 | * @return void |
||
385 | * @access private |
||
386 | */ |
||
387 | function _elgg_plugins_init() { |
||
388 | |||
389 | if (elgg_is_admin_logged_in()) { |
||
390 | elgg_register_ajax_view('object/plugin/full'); |
||
391 | elgg_register_ajax_view('object/plugin/details'); |
||
392 | } |
||
393 | |||
394 | elgg_register_plugin_hook_handler('unit_test', 'system', '_elgg_plugins_test'); |
||
395 | |||
396 | /** |
||
397 | * @see \Elgg\Database\Plugins::invalidateIsActiveCache |
||
398 | */ |
||
399 | $svc = _elgg_services()->plugins; |
||
400 | elgg_register_event_handler('deactivate', 'plugin', [$svc, 'invalidateIsActiveCache']); |
||
401 | elgg_register_event_handler('activate', 'plugin', [$svc, 'invalidateIsActiveCache']); |
||
402 | |||
403 | elgg_register_action("plugins/settings/save", '', 'admin'); |
||
404 | elgg_register_action("plugins/usersettings/save"); |
||
405 | |||
406 | elgg_register_action('admin/plugins/activate', '', 'admin'); |
||
407 | elgg_register_action('admin/plugins/deactivate', '', 'admin'); |
||
408 | elgg_register_action('admin/plugins/activate_all', '', 'admin'); |
||
409 | elgg_register_action('admin/plugins/deactivate_all', '', 'admin'); |
||
410 | |||
411 | elgg_register_action('admin/plugins/set_priority', '', 'admin'); |
||
412 | } |
||
413 | |||
414 | return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) { |
||
415 | $events->registerHandler('init', 'system', '_elgg_plugins_init'); |
||
416 | }; |
||
417 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: