Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

engine/lib/plugins.php (3 issues)

1
<?php
2
/**
3
 * Elgg plugins library
4
 * Contains functions for managing plugins
5
 */
6
7
/**
8
 * Discovers plugins in the plugins_path setting and creates \ElggPlugin
9
 * entities for them if they don't exist.  If there are plugins with entities
10
 * but not actual files, will disable the \ElggPlugin entities and mark as inactive.
11
 * The \ElggPlugin object holds config data, so don't delete.
12
 *
13
 * @return bool
14
 * @since 1.8.0
15
 * @access private
16
 */
17
function _elgg_generate_plugin_entities() {
18 2
	return _elgg_services()->plugins->generateEntities();
19
}
20
21
/**
22
 * Returns an \ElggPlugin object with the path $path.
23
 *
24
 * @param string $plugin_id The id (dir name) of the plugin. NOT the guid.
25
 * @return \ElggPlugin|null
26
 * @since 1.8.0
27
 */
28
function elgg_get_plugin_from_id($plugin_id) {
29 334
	return _elgg_services()->plugins->get($plugin_id);
30
}
31
32
/**
33
 * Returns if a plugin exists in the system.
34
 *
35
 * @warning This checks only plugins that are registered in the system!
36
 * If the plugin cache is outdated, be sure to regenerate it with
37
 * {@link _elgg_generate_plugin_objects()} first.
38
 *
39
 * @param string $id The plugin ID.
40
 * @since 1.8.0
41
 * @return bool
42
 */
43
function elgg_plugin_exists($id) {
44
	return _elgg_services()->plugins->exists($id);
45
}
46
47
/**
48
 * Returns the highest priority of the plugins
49
 *
50
 * @return int
51
 * @since 1.8.0
52
 * @access private
53
 */
54
function _elgg_get_max_plugin_priority() {
55 7
	return _elgg_services()->plugins->getMaxPriority();
56
}
57
58
/**
59
 * Returns if a plugin is active for a current site.
60
 *
61
 * @param string $plugin_id The plugin ID
62
 * @since 1.8.0
63
 * @return bool
64
 */
65
function elgg_is_active_plugin($plugin_id) {
66 5
	return _elgg_services()->plugins->isActive($plugin_id);
67
}
68
69
/**
70
 * Returns an ordered list of plugins
71
 *
72
 * @param string $status The status of the plugins. active, inactive, or all.
73
 * @return \ElggPlugin[]
74
 * @since 1.8.0
75
 */
76
function elgg_get_plugins($status = 'active') {
77 5
	return _elgg_services()->plugins->find($status);
78
}
79
80
/**
81
 * Returns an array of all plugin user settings for a user.
82
 *
83
 * @param int    $user_guid  The user GUID or 0 for the currently logged in user.
84
 * @param string $plugin_id  The plugin ID (Required)
85
 * @param bool   $return_obj Return settings as an object? This can be used to in reusable
86
 *                           views where the settings are passed as $vars['entity'].
87
 *
88
 * @return array|object
89
 * @since 1.8.0
90
 * @see   \ElggPlugin::getAllUserSettings()
91
 */
92
function elgg_get_all_plugin_user_settings($user_guid = 0, $plugin_id = null, $return_obj = false) {
93
	$plugin = elgg_get_plugin_from_id($plugin_id);
94
	if (!$plugin) {
95
		return [];
96
	}
97
98
	$settings = $plugin->getAllUserSettings($user_guid);
99
100
	return $return_obj ? (object) $settings : $settings;
101
}
102
103
/**
104
 * Set a user specific setting for a plugin.
105
 *
106
 * @param string $name      The name. Note: cannot be "title".
107
 * @param mixed  $value     The value.
108
 * @param int    $user_guid The user GUID or 0 for the currently logged in user.
109
 * @param string $plugin_id The plugin ID (Required)
110
 *
111
 * @return bool
112
 * @since 1.8.0
113
 * @see \ElggPlugin::setUserSetting()
114
 */
115
function elgg_set_plugin_user_setting($name, $value, $user_guid = 0, $plugin_id = null) {
116
	return _elgg_services()->plugins->setUserSetting($name, $value, $user_guid, $plugin_id);
117
}
118
119
/**
120
 * Unsets a user-specific plugin setting
121
 *
122
 * @param string $name      Name of the setting
123
 * @param int    $user_guid The user GUID or 0 for the currently logged in user.
124
 * @param string $plugin_id The plugin ID (Required)
125
 *
126
 * @return bool
127
 * @since 1.8.0
128
 * @see \ElggPlugin::unsetUserSetting()
129
 */
130
function elgg_unset_plugin_user_setting($name, $user_guid = 0, $plugin_id = null) {
131
	return _elgg_services()->plugins->unsetUserSetting($name, $user_guid, $plugin_id);
132
}
133
134
/**
135
 * Get a user specific setting for a plugin.
136
 *
137
 * @param string $name      The name of the setting.
138
 * @param int    $user_guid The user GUID or 0 for the currently logged in user.
139
 * @param string $plugin_id The plugin ID (Required)
140
 * @param mixed  $default   The default value to return if none is set
141
 *
142
 * @return mixed
143
 * @since 1.8.0
144
 * @see \ElggPlugin::getUserSetting()
145
 */
146
function elgg_get_plugin_user_setting($name, $user_guid = 0, $plugin_id = null, $default = null) {
147
	return _elgg_services()->plugins->getUserSetting($name, $user_guid, $plugin_id, $default);
148
}
149
150
/**
151
 * Set a setting for a plugin.
152
 *
153
 * @param string $name      The name of the setting - note, can't be "title".
154
 * @param mixed  $value     The value.
155
 * @param string $plugin_id The plugin ID (Required)
156
 *
157
 * @return bool
158
 * @since 1.8.0
159
 * @see \ElggPlugin::setSetting()
160
 */
161
function elgg_set_plugin_setting($name, $value, $plugin_id) {
162
	return _elgg_services()->plugins->setSetting($name, $value, $plugin_id);
163
}
164
165
/**
166
 * Get setting for a plugin.
167
 *
168
 * @param string $name      The name of the setting.
169
 * @param string $plugin_id The plugin ID (Required)
170
 * @param mixed  $default   The default value to return if none is set
171
 *
172
 * @return mixed
173
 * @since 1.8.0
174
 * @see \ElggPlugin::getSetting()
175
 */
176
function elgg_get_plugin_setting($name, $plugin_id, $default = null) {
177 31
	return _elgg_services()->plugins->getSetting($name, $plugin_id, $default);
178
}
179
180
/**
181
 * Unsets a plugin setting.
182
 *
183
 * @param string $name      The name of the setting.
184
 * @param string $plugin_id The plugin ID (Required)
185
 *
186
 * @return bool
187
 * @since 1.8.0
188
 * @see \ElggPlugin::unsetSetting()
189
 */
190
function elgg_unset_plugin_setting($name, $plugin_id) {
191
	return _elgg_services()->plugins->unsetSetting($name, $plugin_id);
192
}
193
194
/**
195
 * Unsets all plugin settings for a plugin.
196
 *
197
 * @param string $plugin_id The plugin ID (Required)
198
 *
199
 * @return bool
200
 * @since 1.8.0
201
 * @see \ElggPlugin::unsetAllSettings()
202
 */
203
function elgg_unset_all_plugin_settings($plugin_id) {
204
	return _elgg_services()->plugins->unsetAllSettings($plugin_id);
205
}
206
207
/**
208
 * Returns entities based upon plugin user settings.
209
 * Takes all the options for {@link elgg_get_entities_from_private_settings()}
210
 * in addition to the ones below.
211
 *
212
 * @param array $options Array in the format:
213
 *
214
 * 	plugin_id => STR The plugin id. Required.
215
 *
216
 * 	plugin_user_setting_names => null|ARR private setting names
217
 *
218
 * 	plugin_user_setting_values => null|ARR metadata values
219
 *
220
 * 	plugin_user_setting_name_value_pairs => null|ARR (
221
 *                                         name => 'name',
222
 *                                         value => 'value',
223
 *                                         'operand' => '=',
224
 *                                        )
225
 * 	                             Currently if multiple values are sent via
226
 *                               an array (value => array('value1', 'value2')
227
 *                               the pair's operand will be forced to "IN".
228
 *
229
 * 	plugin_user_setting_name_value_pairs_operator => null|STR The operator to use for combining
230
 *                                        (name = value) OPERATOR (name = value); default AND
231
 *
232
 * @return mixed int If count, int. If not count, array. false on errors.
233
 * @since 1.8.0
234
 */
235
function elgg_get_entities_from_plugin_user_settings(array $options = []) {
236
	return _elgg_services()->plugins->getEntitiesFromUserSettings($options);
237
}
238
239
/**
240
 * Runs unit tests for plugin API.
241
 *
242
 * @param string $hook   unit_test
243
 * @param string $type   system
244
 * @param mixed  $value  Array of tests
245
 * @param mixed  $params Params
246
 *
247
 * @return array
248
 * @access private
249
 * @codeCoverageIgnore
250
 */
251
function _elgg_plugins_test($hook, $type, $value, $params) {
3 ignored issues
show
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

251
function _elgg_plugins_test($hook, /** @scrutinizer ignore-unused */ $type, $value, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $hook is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

251
function _elgg_plugins_test(/** @scrutinizer ignore-unused */ $hook, $type, $value, $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

251
function _elgg_plugins_test($hook, $type, $value, /** @scrutinizer ignore-unused */ $params) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
252
	$value[] = ElggCorePluginsAPITest::class;
253
	return $value;
254
}
255
256
/**
257
 * Initialize the plugin system
258
 *
259
 * @return void
260
 * @access private
261
 */
262
function _elgg_plugins_init() {
263
264 31
	if (elgg_is_admin_logged_in()) {
265
		elgg_register_ajax_view('object/plugin/full');
266
		elgg_register_ajax_view('object/plugin/details');
267
	}
268
269 31
	elgg_register_plugin_hook_handler('unit_test', 'system', '_elgg_plugins_test');
270
271
	/**
272
	 * @see \Elgg\Database\Plugins::invalidateIsActiveCache
273
	 */
274 31
	$svc = _elgg_services()->plugins;
275 31
	elgg_register_event_handler('deactivate', 'plugin', [$svc, 'invalidateIsActiveCache']);
276 31
	elgg_register_event_handler('activate', 'plugin', [$svc, 'invalidateIsActiveCache']);
277
278 31
	elgg_register_action("plugins/settings/save", '', 'admin');
279 31
	elgg_register_action("plugins/usersettings/save");
280
281 31
	elgg_register_action('admin/plugins/activate', '', 'admin');
282 31
	elgg_register_action('admin/plugins/deactivate', '', 'admin');
283 31
	elgg_register_action('admin/plugins/activate_all', '', 'admin');
284 31
	elgg_register_action('admin/plugins/deactivate_all', '', 'admin');
285
286 31
	elgg_register_action('admin/plugins/set_priority', '', 'admin');
287 31
}
288
289
/**
290
 * @see \Elgg\Application::loadCore Do not do work here. Just register for events.
291
 */
292
return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
293 18
	$events->registerHandler('init', 'system', '_elgg_plugins_init');
294
};
295