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

mod/profile/start.php (7 issues)

1
<?php
2
/**
3
 * Elgg profile plugin
4
 *
5
 * @package ElggProfile
6
 */
7
8
/**
9
 * Profile init function
10
 *
11
 * @return void
12
 */
13
function profile_init() {
14
15
	// Register a URL handler for users
16 31
	elgg_register_plugin_hook_handler('entity:url', 'user', 'profile_set_url');
17
18 31
	elgg_register_page_handler('profile', 'profile_page_handler');
19
	
20
	// unregister default core page handler for displaying users
21 31
	elgg_unregister_page_handler('user');
22
23 31
	elgg_extend_view('elgg.css', 'profile/css');
24
	
25 31
	elgg_register_ajax_view('forms/profile/fields/add');
26
27
	// allow ECML in parts of the profile
28 31
	elgg_register_plugin_hook_handler('get_views', 'ecml', 'profile_ecml_views_hook');
29
30
	// allow admins to set default widgets for users on profiles
31 31
	elgg_register_plugin_hook_handler('get_list', 'default_widgets', 'profile_default_widgets_hook');
32
	
33 31
	elgg_register_plugin_hook_handler('register', 'menu:topbar', '_profile_topbar_menu');
34 31
	elgg_register_plugin_hook_handler('register', 'menu:title', '_profile_title_menu');
35 31
	elgg_register_plugin_hook_handler('register', 'menu:page', '_profile_admin_page_menu');
36 31
	elgg_register_plugin_hook_handler('register', 'menu:page', '_profile_user_page_menu');
37 31
	elgg_register_plugin_hook_handler('register', 'menu:user_hover', '_profile_user_hover_menu');
38 31
}
39
40
/**
41
 * Profile page handler
42
 *
43
 * @param array $page Array of URL segments passed by the page handling mechanism
44
 * @return bool
45
 */
46
function profile_page_handler($page) {
47
48
	if (isset($page[0])) {
49
		$username = $page[0];
50
		$user = get_user_by_username($username);
51
		elgg_set_page_owner_guid($user->guid);
52
	} elseif (elgg_is_logged_in()) {
53
		forward(elgg_get_logged_in_user_entity()->getURL());
54
	}
55
56
	// short circuit if invalid or banned username
57
	if (!$user || ($user->isBanned() && !elgg_is_admin_logged_in())) {
58
		register_error(elgg_echo('profile:notfound'));
59
		forward();
60
	}
61
62
	$action = null;
63
	if (isset($page[1])) {
64
		$action = $page[1];
65
	}
66
	
67
	if ($action == 'edit') {
68
		// use the core profile edit page
69
		echo elgg_view_resource('profile/edit');
70
		return true;
71
	}
72
73
	echo elgg_view_resource('profile/view', [
74
		'username' => $page[0],
75
	]);
76
	return true;
77
}
78
79
/**
80
 * Profile URL generator for $user->getUrl();
81
 *
82
 * @param string $hook   'entity:url'
83
 * @param string $type   'user'
84
 * @param string $url    current return value
85
 * @param array  $params supplied params
86
 *
87
 * @return void|string
88
 */
89
function profile_set_url($hook, $type, $url, $params) {
2 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

89
function profile_set_url($hook, /** @scrutinizer ignore-unused */ $type, $url, $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 $url 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

89
function profile_set_url($hook, $type, /** @scrutinizer ignore-unused */ $url, $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

89
function profile_set_url(/** @scrutinizer ignore-unused */ $hook, $type, $url, $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...
90
	
91 1
	$user = elgg_extract('entity', $params);
92 1
	if (!$user instanceof ElggUser) {
93
		return;
94
	}
95
	
96 1
	return "profile/{$user->username}";
97
}
98
99
/**
100
 * Parse ECML on parts of the profile
101
 *
102
 * @param string $hook         'get_views'
103
 * @param string $type         'ecml'
104
 * @param array  $return_value current return value
105
 *
106
 * @return array
107
 */
108
function profile_ecml_views_hook($hook, $type, $return_value) {
2 ignored issues
show
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

108
function profile_ecml_views_hook(/** @scrutinizer ignore-unused */ $hook, $type, $return_value) {

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 $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

108
function profile_ecml_views_hook($hook, /** @scrutinizer ignore-unused */ $type, $return_value) {

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...
109
	$return_value['profile/profile_content'] = elgg_echo('profile');
110
111
	return $return_value;
112
}
113
114
/**
115
 * Register profile widgets with default widgets
116
 *
117
 * @param string $hook   'get_list'
118
 * @param string $type   'default_widgets'
119
 * @param array  $return current return value
120
 *
121
 * @return array
122
 */
123
function profile_default_widgets_hook($hook, $type, $return) {
2 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

123
function profile_default_widgets_hook($hook, /** @scrutinizer ignore-unused */ $type, $return) {

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

123
function profile_default_widgets_hook(/** @scrutinizer ignore-unused */ $hook, $type, $return) {

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...
124 18
	$return[] = [
125 18
		'name' => elgg_echo('profile'),
126 18
		'widget_context' => 'profile',
127 18
		'widget_columns' => 2,
128
129 18
		'event' => 'create',
130 18
		'entity_type' => 'user',
131
		'entity_subtype' => ELGG_ENTITIES_ANY_VALUE,
132
	];
133
134 18
	return $return;
135
}
136
137
/**
138
 * This function loads a set of default fields into the profile, then triggers a hook letting other plugins to edit
139
 * add and delete fields.
140
 *
141
 * Note: This is a secondary system:init call and is run at a super low priority to guarantee that it is called after all
142
 * other plugins have initialised.
143
 *
144
 * @return void
145
 *
146
 * @access private
147
 */
148
function _profile_fields_setup() {
149
	$profile_defaults =  [
150 31
		'description' => 'longtext',
151
		'briefdescription' => 'text',
152
		'location' => 'location',
153
		'interests' => 'tags',
154
		'skills' => 'tags',
155
		'contactemail' => 'email',
156
		'phone' => 'text',
157
		'mobile' => 'text',
158
		'website' => 'url',
159
		'twitter' => 'text',
160
	];
161
162 31
	$loaded_defaults = [];
163 31
	$fieldlist = elgg_get_config('profile_custom_fields');
164 31
	if ($fieldlist || $fieldlist === '0') {
165
		$fieldlistarray = explode(',', $fieldlist);
166
		foreach ($fieldlistarray as $listitem) {
167
			if ($translation = elgg_get_config("admin_defined_profile_{$listitem}")) {
168
				$type = elgg_get_config("admin_defined_profile_type_{$listitem}");
169
				$loaded_defaults["admin_defined_profile_{$listitem}"] = $type;
170
				add_translation(get_current_language(), ["profile:admin_defined_profile_{$listitem}" => $translation]);
171
			}
172
		}
173
	}
174
175 31
	if (count($loaded_defaults)) {
176
		elgg_set_config('profile_using_custom', true);
177
		$profile_defaults = $loaded_defaults;
178
	}
179
	
180 31
	$profile_fields = elgg_trigger_plugin_hook('profile:fields', 'profile', null, $profile_defaults);
181 31
	elgg_set_config('profile_fields', $profile_fields);
182
183
	// register any tag metadata names
184 31
	foreach ($profile_fields as $name => $type) {
185 31
		if ($type == 'tags' || $type == 'location' || $type == 'tag') {
186 31
			elgg_register_tag_metadata_name($name);
187
			// register a tag name translation
188 31
			add_translation(get_current_language(), ["tag_names:$name" => elgg_echo("profile:$name")]);
189
		}
190
	}
191 31
}
192
193
/**
194
 * Register menu items for the topbar menu
195
 *
196
 * @param \Elgg\Hook $hook 'register' 'menu:topbar'
197
 *
198
 * @return void|ElggMenuItem[]
199
 *
200
 * @access private
201
 * @since 3.0
202
 */
203
function _profile_topbar_menu(\Elgg\Hook $hook) {
204
205 1
	$viewer = elgg_get_logged_in_user_entity();
206 1
	if (!$viewer) {
207 1
		 return;
208
	}
209
	$return = $hook->getValue();
210
	
211
	$return[] = \ElggMenuItem::factory([
212
		'name' => 'profile',
213
		'href' => $viewer->getURL(),
214
		'text' => elgg_echo('profile'),
215
		'icon' => 'user',
216
		'parent_name' => 'account',
217
		'section' => 'alt',
218
		'priority' => 100,
219
	]);
220
	
221
	return $return;
222
}
223
224
/**
225
 * Register menu items for the user hover menu
226
 *
227
 * @param \Elgg\Hook $hook 'register' 'menu:user_hover'
228
 *
229
 * @return void|ElggMenuItem[]
230
 *
231
 * @access private
232
 * @since 3.0
233
 */
234
function _profile_user_hover_menu(\Elgg\Hook $hook) {
235
236 1
	if (!elgg_is_logged_in()) {
237 1
		return;
238
	}
239
	
240
	$user = $hook->getEntityParam();
241
	if (!($user instanceof \ElggUser) || !$user->canEdit()) {
242
		return;
243
	}
244
	
245
	$return = $hook->getValue();
246
	$return[] = ElggMenuItem::factory([
247
		'name' => 'profile:edit',
248
		'text' => elgg_echo('profile:edit'),
249
		'icon' => 'address-card',
250
		'href' => "profile/$user->username/edit",
251
		'section' => (elgg_get_logged_in_user_guid() == $user->guid)? 'action' : 'admin',
252
	]);
253
	
254
	return $return;
255
}
256
257
/**
258
 * Register menu items for the admin page menu
259
 *
260
 * @param \Elgg\Hook $hook 'register' 'menu:page'
261
 *
262
 * @return void|ElggMenuItem[]
263
 *
264
 * @access private
265
 * @since 3.0
266
 */
267
function _profile_admin_page_menu(\Elgg\Hook $hook) {
268
269 1
	if (!elgg_in_context('admin') || !elgg_is_admin_logged_in()) {
270 1
		return;
271
	}
272
	
273
	$return = $hook->getValue();
274
	
275
	$return[] = \ElggMenuItem::factory([
276
		'name' => 'configure_utilities:profile_fields',
277
		'text' => elgg_echo('admin:configure_utilities:profile_fields'),
278
		'href' => 'admin/configure_utilities/profile_fields',
279
		'section' => 'configure',
280
		'parent_name' => 'configure_utilities',
281
	]);
282
	
283
	return $return;
284
}
285
286
/**
287
 * Register menu items for the page menu
288
 *
289
 * @param \Elgg\Hook $hook 'register' 'menu:page'
290
 *
291
 * @return void|ElggMenuItem
292
 *
293
 * @access private
294
 * @since 3.0
295
 */
296
function _profile_user_page_menu(\Elgg\Hook $hook) {
297
298 1
	$owner = elgg_get_page_owner_entity();
299 1
	if (!$owner instanceof \ElggUser) {
300
		return;
301
	}
302
	
303 1
	$return = $hook->getValue();
304
	
305 1
	$return[] = \ElggMenuItem::factory([
306 1
		'name' => 'edit_profile',
307 1
		'href' => "profile/{$owner->username}/edit",
308 1
		'text' => elgg_echo('profile:edit'),
309 1
		'section' => '1_profile',
310
		'contexts' => ['settings'],
311
	]);
312
	
313 1
	return $return;
314
}
315
316
/**
317
 * Register menu items for the title menu
318
 *
319
 * @param string $hook   'register'
320
 * @param string $type   'menu:title'
321
 * @param array  $return Current return value
322
 * @param array  $params Hook parameters
323
 *
324
 * @return void|ElggMenuItem[]
325
 *
326
 * @access private
327
 * @since 3.0
328
 */
329
function _profile_title_menu($hook, $type, $return, $params) {
330
331 1
	if (!elgg_in_context('profile') || elgg_in_context('profile_edit')) {
332 1
		return;
333
	}
334
	
335
	$user = elgg_get_page_owner_entity();
336
	
337
	// grab the actions and admin menu items from user hover
338
	$menu = elgg()->menus->getMenu('user_hover', [
339
		'entity' => $user,
340
		'username' => $user->username,
341
	]);
342
	
343
	$actions = $menu->getSection('action', []);
344
	foreach ($actions as $action) {
345
		$action->addLinkClass('elgg-button elgg-button-action');
346
		$return[] = $action;
347
	}
348
	
349
	return $return;
350
}
351
352
return function() {
353 18
	elgg_register_event_handler('init', 'system', 'profile_init', 1);
354 18
	elgg_register_event_handler('init', 'system', '_profile_fields_setup', 10000); // Ensure this runs after other plugins
355
};
356