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

_elgg_entity_navigation_menu_setup()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 53
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 6

Importance

Changes 0
Metric Value
cc 4
eloc 39
nc 5
nop 1
dl 0
loc 53
ccs 19
cts 38
cp 0.5
crap 6
rs 8.9849
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Elgg navigation library
4
 * Functions for managing menus and other navigational elements
5
 *
6
 * Breadcrumbs
7
 * Elgg uses a breadcrumb stack. The page handlers (controllers in MVC terms)
8
 * push the breadcrumb links onto the stack. @see elgg_push_breadcrumb()
9
 *
10
 *
11
 * Pagination
12
 * Automatically handled by Elgg when using elgg_list_entities* functions.
13
 * @see elgg_list_entities()
14
 *
15
 *
16
 * Tabs
17
 * @see navigation/tabs view
18
 *
19
 *
20
 * Menus
21
 * Elgg uses a single interface to manage its menus. Menu items are added with
22
 * {@link elgg_register_menu_item()}. This is generally used for menus that
23
 * appear only once per page. For dynamic menus (such as the hover
24
 * menu for user's avatar), a plugin hook is emitted when the menu is being
25
 * created. The hook is 'register', 'menu:<menu_name>'. For more details on this,
26
 * @see elgg_view_menu().
27
 *
28
 * Menus supported by the Elgg core
29
 * Standard menus:
30
 *     site   Site navigation shown on every page.
31
 *     page   Page menu usually shown in a sidebar. Uses Elgg's context.
32
 *     topbar Topbar menu shown on every page. The default has two sections.
33
 *     footer Like the topbar but in the footer.
34
 *
35
 * Dynamic menus (also called just-in-time menus):
36
 *     user_hover  Avatar hover menu. The user entity is passed as a parameter.
37
 *     entity      The set of links shown in the summary of an entity.
38
 *     river       Links shown on river items.
39
 *     owner_block Links shown for a user or group in their owner block.
40
 *     filter      The tab filter for content (all, mine, friends)
41
 *     title       The buttons shown next to a content title.
42
 *     longtext    The links shown above the input/longtext view.
43
 *     login       Menu of links at bottom of login box
44
 *
45
 * @package    Elgg.Core
46
 * @subpackage Navigation
47
 */
48
49
/**
50
 * Register an item for an Elgg menu
51
 *
52
 * @warning Generally you should not use this in response to the plugin hook:
53
 * 'register', 'menu:<menu_name>'. If you do, you may end up with many incorrect
54
 * links on a dynamic menu.
55
 *
56
 * @warning A menu item's name must be unique per menu. If more than one menu
57
 * item with the same name are registered, the last menu item takes priority.
58
 *
59
 * @see elgg_view_menu() for the plugin hooks available for modifying a menu as
60
 * it is being rendered.
61
 *
62
 * @see ElggMenuItem::factory() is used to turn an array value of $menu_item into an
63
 * ElggMenuItem object.
64
 *
65
 * @param string $menu_name The name of the menu: site, page, userhover,
66
 *                          userprofile, groupprofile, or any custom menu
67
 * @param mixed  $menu_item A \ElggMenuItem object or an array of options in format:
68
 *                          name        => STR  Menu item identifier (required)
69
 *                          text        => STR  Menu item display text as HTML (required)
70
 *                          href        => STR  Menu item URL (required)
71
 *                                              false = do not create a link.
72
 *                                              null = current URL.
73
 *                                              "" = current URL.
74
 *                                              "/" = site home page.
75
 *                                              @warning If href is false, the <a> tag will
76
 *                                              not appear, so the link_class will not apply. If you
77
 *                                              put <a> tags in manually through the 'text' option
78
 *                                              the default CSS selector .elgg-menu-$menu > li > a
79
 *                                              may affect formatting. Wrap in a <span> if it does.)
80
 *                          contexts    => ARR  Page context strings
81
 *                          section     => STR  Menu section identifier
82
 *                          title       => STR  Menu item tooltip
83
 *                          selected    => BOOL Is this menu item currently selected
84
 *                          parent_name => STR  Identifier of the parent menu item
85
 *                          link_class  => STR  A class or classes for the <a> tag
86
 *                          item_class  => STR  A class or classes for the <li> tag
87
 *                          deps     => STR  One or more AMD modules to require
88
 *
89
 *                          Additional options that the view output/url takes can be
90
 *							passed in the array. Custom options can be added by using
91
 *							the 'data' key with the	value being an associative array.
92
 *
93
 * @return bool False if the item could not be added
94
 * @since 1.8.0
95
 */
96
function elgg_register_menu_item($menu_name, $menu_item) {
97 34
	if (is_array($menu_item)) {
98 34
		$options = $menu_item;
99 34
		$menu_item = \ElggMenuItem::factory($options);
100 34
		if (!$menu_item) {
101
			$menu_item_name = elgg_extract('name', $options, 'MISSING NAME');
102
			elgg_log("Unable to add menu item '{$menu_item_name}' to '$menu_name' menu", 'WARNING');
103
			return false;
104
		}
105
	}
106
107 34
	if (!$menu_item instanceof ElggMenuItem) {
108
		elgg_log('Second argument of elgg_register_menu_item() must be an instance of '
109
			. 'ElggMenuItem or an array of menu item factory options', 'ERROR');
110
		return false;
111
	}
112
113 34
	$menus = _elgg_config()->menus;
114 34
	if (!$menus) {
115 21
		$menus = [];
116
	}
117
118 34
	$menus[$menu_name][] = $menu_item;
119 34
	_elgg_config()->menus = $menus;
120
121 34
	return true;
122
}
123
124
/**
125
 * Remove an item from a menu
126
 *
127
 * @param string $menu_name The name of the menu
128
 * @param string $item_name The unique identifier for this menu item
129
 *
130
 * @return \ElggMenuItem|null
131
 * @since 1.8.0
132
 */
133
function elgg_unregister_menu_item($menu_name, $item_name) {
134
	$menus = _elgg_config()->menus;
135
	if (!$menus) {
136
		return null;
137
	}
138
139
	foreach ($menus[$menu_name] as $index => $menu_object) {
140
		/* @var \ElggMenuItem $menu_object */
141
		if ($menu_object instanceof ElggMenuItem && $menu_object->getName() == $item_name) {
142
			$item = $menus[$menu_name][$index];
143
			unset($menus[$menu_name][$index]);
144
			elgg_set_config('menus', $menus);
145
			return $item;
146
		}
147
	}
148
149
	return null;
150
}
151
152
/**
153
 * Check if a menu item has been registered
154
 *
155
 * @param string $menu_name The name of the menu
156
 * @param string $item_name The unique identifier for this menu item
157
 *
158
 * @return bool
159
 * @since 1.8.0
160
 */
161
function elgg_is_menu_item_registered($menu_name, $item_name) {
162
	$menus = _elgg_config()->menus;
163
	if (!$menus) {
164
		return false;
165
	}
166
167
	if (!isset($menus[$menu_name])) {
168
		return false;
169
	}
170
171
	foreach ($menus[$menu_name] as $menu_object) {
172
		/* @var \ElggMenuItem $menu_object */
173
		if ($menu_object->getName() == $item_name) {
174
			return true;
175
		}
176
	}
177
178
	return false;
179
}
180
181
/**
182
 * Get a menu item registered for a menu
183
 *
184
 * @param string $menu_name The name of the menu
185
 * @param string $item_name The unique identifier for this menu item
186
 *
187
 * @return ElggMenuItem|null
188
 * @since 1.9.0
189
 */
190
function elgg_get_menu_item($menu_name, $item_name) {
191
	$menus = _elgg_config()->menus;
192
	if (!$menus) {
193
		return null;
194
	}
195
196
	if (!isset($menus[$menu_name])) {
197
		return null;
198
	}
199
200
	foreach ($menus[$menu_name] as $index => $menu_object) {
201
		/* @var \ElggMenuItem $menu_object */
202
		if ($menu_object->getName() == $item_name) {
203
			return $menus[$menu_name][$index];
204
		}
205
	}
206
207
	return null;
208
}
209
210
/**
211
 * Convenience function for registering a button to the title menu
212
 *
213
 * The URL must be $handler/$name/$guid where $guid is the guid of the page owner.
214
 * The label of the button is "$handler:$name" so that must be defined in a
215
 * language file.
216
 *
217
 * This is used primarily to support adding an add content button
218
 *
219
 * @param string $handler        The handler to use or null to autodetect from context
220
 * @param string $name           Name of the button (defaults to 'add')
221
 * @param string $entity_type    Optional entity type to be added (used to verify canWriteToContainer permission)
222
 * @param string $entity_subtype Optional entity subtype to be added (used to verify canWriteToContainer permission)
223
 * @return void
224
 * @since 1.8.0
225
 */
226
function elgg_register_title_button($handler = null, $name = 'add', $entity_type = 'all', $entity_subtype = 'all') {
227
	
228
	if (!$handler) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $handler of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
229
		$handler = elgg_get_context();
230
	}
231
232
	$owner = elgg_get_page_owner_entity();
233
	if (!$owner) {
234
		// noone owns the page so this is probably an all site list page
235
		$owner = elgg_get_logged_in_user_entity();
236
	}
237
	
238
	if (($name === 'add') && ($owner instanceof ElggUser)) {
239
		// make sure the add link goes to the current logged in user, not the page owner
240
		$logged_in_user = elgg_get_logged_in_user_entity();
241
		if (!empty($logged_in_user) && ($logged_in_user->guid !== $owner->guid)) {
242
			// change the 'owner' for the link to the current logged in user
243
			$owner = $logged_in_user;
244
		}
245
	}
246
	
247
	// do we have an owner and is the current user allowed to create content here
248
	if (!$owner || !$owner->canWriteToContainer(0, $entity_type, $entity_subtype)) {
249
		return;
250
	}
251
252
	// register the title menu item
253
	elgg_register_menu_item('title', [
254
		'name' => $name,
255
		'icon' => $name === 'add' ? 'plus' : '',
256
		'href' => "$handler/$name/$owner->guid",
257
		'text' => elgg_echo("$handler:$name"),
258
		'link_class' => 'elgg-button elgg-button-action',
259
	]);
260
}
261
262
/**
263
 * Adds a breadcrumb to the breadcrumbs stack.
264
 *
265
 * See elgg_get_breadcrumbs() and the navigation/breadcrumbs view.
266
 *
267
 * @param string $text The title to display. During rendering this is HTML encoded.
268
 * @param string $href Optional. The href for the title. During rendering links are
269
 *                     normalized via elgg_normalize_url().
270
 *
271
 * @return void
272
 * @since 1.8.0
273
 * @see elgg_get_breadcrumbs()
274
 */
275
function elgg_push_breadcrumb($text, $href = null) {
276
	$breadcrumbs = (array) _elgg_config()->breadcrumbs;
277
	
278
	$breadcrumbs[] = [
279
		'text' => $text,
280
		'href' => $href,
281
	];
282
	
283
	elgg_set_config('breadcrumbs', $breadcrumbs);
284
}
285
286
/**
287
 * Removes last breadcrumb entry.
288
 *
289
 * @return array popped breadcrumb array or empty array
290
 * @since 1.8.0
291
 */
292
function elgg_pop_breadcrumb() {
293
	$breadcrumbs = (array) _elgg_config()->breadcrumbs;
294
295
	if (empty($breadcrumbs)) {
296
		return [];
297
	}
298
299
	$popped = array_pop($breadcrumbs);
300
	elgg_set_config('breadcrumbs', $breadcrumbs);
301
302
	return $popped;
303
}
304
305
/**
306
 * Returns all breadcrumbs as an array
307
 * <code>
308
 * [
309
 *    [
310
 *       'text' => 'Breadcrumb title',
311
 *       'href' => '/path/to/page',
312
 *    ]
313
 * ]
314
 * </code>
315
 *
316
 * Breadcrumbs are filtered through the plugin hook [prepare, breadcrumbs] before
317
 * being returned.
318
 *
319
 * @param array $breadcrumbs An array of breadcrumbs
320
 *                           If set, will override breadcrumbs in the stack
321
 * @return array
322
 * @since 1.8.0
323
 * @see elgg_prepare_breadcrumbs()
324
 */
325
function elgg_get_breadcrumbs(array $breadcrumbs = null) {
326
	if (!isset($breadcrumbs)) {
327
		// if no crumbs set, still allow hook to populate it
328
		$breadcrumbs = (array) _elgg_config()->breadcrumbs;
329
	}
330
331
	if (!is_array($breadcrumbs)) {
332
		_elgg_services()->logger->error(__FUNCTION__ . ' expects breadcrumbs as an array');
333
		$breadcrumbs = [];
334
	}
335
	
336
	$params = [
337
		'breadcrumbs' => $breadcrumbs,
338
	];
339
340
	$params['identifier'] = _elgg_services()->request->getFirstUrlSegment();
341
	$params['segments'] = _elgg_services()->request->getUrlSegments();
342
	array_shift($params['segments']);
343
344
	$breadcrumbs = elgg_trigger_plugin_hook('prepare', 'breadcrumbs', $params, $breadcrumbs);
345
	if (!is_array($breadcrumbs)) {
346
		_elgg_services()->logger->error('"prepare, breadcrumbs" hook must return an array of breadcrumbs');
347
		return [];
348
	}
349
	
350
	foreach ($breadcrumbs as $key => $breadcrumb) {
351
		$text = elgg_extract('text', $breadcrumb, elgg_extract('title', $breadcrumb));
352
		if (isset($breadcrumb['link'])) {
353
			elgg_deprecated_notice("Breadcrumb [{$text}] requires 'href' instead of 'link' set in the configuration", '3.0');
354
			
355
			$breadcrumbs[$key]['href'] = $breadcrumb['link'];
356
			unset($breadcrumbs[$key]['link']);
357
		}
358
		if (isset($breadcrumb['title'])) {
359
			elgg_deprecated_notice("Breadcrumb [{$text}] requires 'text' instead of 'title' set in the configuration", '3.0');
360
			
361
			$breadcrumbs[$key]['text'] = $breadcrumb['title'];
362
			unset($breadcrumbs[$key]['title']);
363
		}
364
		
365
		// adds name for usage in menu items
366
		if (!isset($breadcrumb['name'])) {
367
			$breadcrumbs[$key]['name'] = $key;
368
		}
369
	}
370
371
	return $breadcrumbs;
372
}
373
374
/**
375
 * Prepare breadcrumbs before display. This turns titles into 100-character excerpts, and also
376
 * removes the last crumb if it's not a link.
377
 *
378
 * @param string $hook        "prepare"
379
 * @param string $type        "breadcrumbs"
380
 * @param array  $breadcrumbs Breadcrumbs to be altered
381
 * @param array  $params      Hook parameters
382
 *
383
 * @return array
384
 * @since 1.11
385
 */
386
function elgg_prepare_breadcrumbs($hook, $type, $breadcrumbs, $params) {
3 ignored issues
show
Unused Code introduced by
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

386
function elgg_prepare_breadcrumbs(/** @scrutinizer ignore-unused */ $hook, $type, $breadcrumbs, $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...
Unused Code introduced by
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

386
function elgg_prepare_breadcrumbs($hook, /** @scrutinizer ignore-unused */ $type, $breadcrumbs, $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...
Unused Code introduced by
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

386
function elgg_prepare_breadcrumbs($hook, $type, $breadcrumbs, /** @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...
387
	// remove last crumb if not a link
388
	$last_crumb = end($breadcrumbs);
389
	if (empty($last_crumb['href'])) {
390
		array_pop($breadcrumbs);
391
	}
392
393
	// apply excerpt to text
394
	foreach (array_keys($breadcrumbs) as $i) {
395
		$breadcrumbs[$i]['text'] = elgg_get_excerpt($breadcrumbs[$i]['text'], 100);
396
	}
397
	return $breadcrumbs;
398
}
399
400
/**
401
 * Returns default filter tabs (All, Mine, Friends) for the user
402
 *
403
 * @param string   $context  Context to be used to prefix tab URLs
404
 * @param string   $selected Name of the selected tab
405
 * @param ElggUser $user     User who owns the layout (defaults to logged in user)
406
 * @param array    $vars     Additional vars
407
 * @return ElggMenuItem[]
408
 * @since 2.3
409
 */
410
function elgg_get_filter_tabs($context = null, $selected = null, ElggUser $user = null, array $vars = []) {
411
412
	if (!isset($selected)) {
413
		$selected = 'all';
414
	}
415
416
	if (!$user) {
417
		$user = elgg_get_logged_in_user_entity();
418
	}
419
420
	$items = [];
421
	if ($user) {
422
		$items[] = ElggMenuItem::factory([
423
			'name' => 'all',
424
			'text' => elgg_echo('all'),
425
			'href' => (isset($vars['all_link'])) ? $vars['all_link'] : "$context/all",
426
			'selected' => ($selected == 'all'),
427
			'priority' => 200,
428
		]);
429
		$items[] = ElggMenuItem::factory([
430
			'name' => 'mine',
431
			'text' => elgg_echo('mine'),
432
			'href' => (isset($vars['mine_link'])) ? $vars['mine_link'] : "$context/owner/{$user->username}",
433
			'selected' => ($selected == 'mine'),
434
			'priority' => 300,
435
		]);
436
	}
437
438
	$params = [
439
		'selected' => $selected,
440
		'user' => $user,
441
		'vars' => $vars,
442
	];
443
	$items = _elgg_services()->hooks->trigger('filter_tabs', $context, $params, $items);
444
445
	return $items;
446
}
447
448
/**
449
 * Init site menu
450
 *
451
 * Registers custom menu items
452
 *
453
 * @param string         $hook   'register'
454
 * @param string         $type   'menu:site'
455
 * @param ElggMenuItem[] $return Menu
456
 * @param array          $params Hook params
457
 *
458
 * @return ElggMenuItem[]
459
 *
460
 * @access private
461
 */
462
function _elgg_site_menu_init($hook, $type, $return, $params) {
3 ignored issues
show
Unused Code introduced by
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

462
function _elgg_site_menu_init(/** @scrutinizer ignore-unused */ $hook, $type, $return, $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...
Unused Code introduced by
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

462
function _elgg_site_menu_init($hook, /** @scrutinizer ignore-unused */ $type, $return, $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...
Unused Code introduced by
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

462
function _elgg_site_menu_init($hook, $type, $return, /** @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...
463 1
	$custom_menu_items = elgg_get_config('site_custom_menu_items');
464
465 1
	if ($custom_menu_items) {
466
		// add custom menu items
467
		$n = 1;
468
		foreach ($custom_menu_items as $title => $url) {
469
			$item = new ElggMenuItem("custom$n", $title, $url);
470
			$return[] = $item;
471
			$n++;
472
		}
473
	}
474
475 1
	if (elgg_is_logged_in() && elgg_is_active_plugin('dashboard')) {
476
		$return[] = ElggMenuItem::factory([
477
			'name' => 'dashboard',
478
			'text' => elgg_echo('dashboard'),
479
			'href' => 'dashboard',
480
		]);
481
	}
482
483 1
	return $return;
484
}
485
486
/**
487
 * Set up the site menu
488
 *
489
 * Handles default, featured, and custom menu items
490
 *
491
 * @param string         $hook   'prepare'
492
 * @param string         $type   'menu:site'
493
 * @param ElggMenuItem[] $return current return value
494
 * @param array          $params supplied params
495
 *
496
 * @return ElggMenuItem[]
497
 *
498
 * @access private
499
 */
500
function _elgg_site_menu_setup($hook, $type, $return, $params) {
3 ignored issues
show
Unused Code introduced by
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

500
function _elgg_site_menu_setup($hook, $type, $return, /** @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...
Unused Code introduced by
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

500
function _elgg_site_menu_setup(/** @scrutinizer ignore-unused */ $hook, $type, $return, $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...
Unused Code introduced by
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

500
function _elgg_site_menu_setup($hook, /** @scrutinizer ignore-unused */ $type, $return, $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...
501
502 1
	$featured_menu_names = array_values((array) elgg_get_config('site_featured_menu_names'));
503
504 1
	$registered = $return['default'];
505
	/* @var ElggMenuItem[] $registered */
506
507 1
	$has_selected = false;
508 1
	$priority = 500;
509 1
	foreach ($registered as &$item) {
510 1
		if (in_array($item->getName(), $featured_menu_names)) {
511
			$featured_index = array_search($item->getName(), $featured_menu_names);
512
			$item->setPriority($featured_index);
0 ignored issues
show
Bug introduced by
It seems like $featured_index can also be of type string and false; however, parameter $priority of ElggMenuItem::setPriority() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

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

512
			$item->setPriority(/** @scrutinizer ignore-type */ $featured_index);
Loading history...
513
		} else {
514 1
			$item->setPriority($priority);
515 1
			$priority++;
516
		}
517 1
		if ($item->getSelected()) {
518 1
			$has_selected = true;
519
		}
520
	}
521
522 1
	if (!$has_selected) {
523
		$is_selected = function ($item) {
524 1
			$current_url = current_page_url();
525 1
			if (strpos($item->getHref(), elgg_get_site_url()) === 0) {
526 1
				if ($item->getName() == elgg_get_context()) {
527
					return true;
528
				}
529 1
				if ($item->getHref() == $current_url) {
530
					return true;
531
				}
532
			}
533
534 1
			return false;
535 1
		};
536 1
		foreach ($registered as &$item) {
537 1
			if ($is_selected($item)) {
538
				$item->setSelected(true);
539 1
				break;
540
			}
541
		}
542
	}
543
544 1
	usort($registered, [\ElggMenuBuilder::class, 'compareByPriority']);
545
546 1
	$max_display_items = 5;
547
548 1
	$num_menu_items = count($registered);
549
550 1
	$more = [];
551 1
	if ($max_display_items && $num_menu_items > ($max_display_items + 1)) {
552 1
		$more = array_splice($registered, $max_display_items);
553
	}
554
555 1
	if (!empty($more)) {
556 1
		$dropdown = ElggMenuItem::factory([
557 1
			'name' => 'more',
558 1
			'href' => 'javascript:void(0);',
559 1
			'text' => elgg_echo('more'),
560 1
			'icon_alt' => 'angle-down',
561 1
			'priority' => 999,
562
		]);
563
564 1
		foreach ($more as &$item) {
565 1
			$item->setParentName('more');
566
		}
567
568 1
		$dropdown->setChildren($more);
569
570 1
		$registered[] = $dropdown;
571
	}
572
573 1
	$return['default'] = $registered;
574
575 1
	return $return;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $return returns an array which contains values of type ElggMenuItem[] which are incompatible with the documented value type ElggMenuItem.
Loading history...
576
}
577
578
/**
579
 * Prepare page menu
580
 * Sets the display child menu option to "toggle" if not set
581
 * Recursively marks parents of the selected item as selected (expanded)
582
 *
583
 * @param \Elgg\Hook $hook 'prepare', 'menu:page'
584
 *
585
 * @return ElggMenuItem[]
586
 *
587
 * @access private
588
 */
589
function _elgg_page_menu_setup(\Elgg\Hook $hook) {
590 2
	$menu = $hook->getValue();
591
592 2
	foreach ($menu as $section => $menu_items) {
593 1
		foreach ($menu_items as $menu_item) {
594 1
			if ($menu_item instanceof ElggMenuItem) {
595 1
				$child_menu_vars = $menu_item->getChildMenuOptions();
596 1
				if (empty($child_menu_vars['display'])) {
597 1
					$child_menu_vars['display'] = 'toggle';
598
				}
599 1
				$menu_item->setChildMenuOptions($child_menu_vars);
600
			}
601
		}
602
	}
603
604 2
	$selected_item = $hook->getParam('selected_item');
605 2
	if ($selected_item instanceof \ElggMenuItem) {
606
		$parent = $selected_item->getParent();
607
		while ($parent instanceof \ElggMenuItem) {
608
			$parent->setSelected();
609
			$parent = $parent->getParent();
610
		}
611
	}
612
613 2
	return $menu;
614
}
615
616
/**
617
 * Entity menu is list of links and info on any entity
618
 *
619
 * @param string         $hook   'register'
620
 * @param string         $type   'menu:entity'
621
 * @param ElggMenuItem[] $return current return value
622
 * @param array          $params supplied params
623
 *
624
 * @return void|ElggMenuItem[]
625
 *
626
 * @access private
627
 */
628
function _elgg_entity_menu_setup($hook, $type, $return, $params) {
2 ignored issues
show
Unused Code introduced by
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

628
function _elgg_entity_menu_setup(/** @scrutinizer ignore-unused */ $hook, $type, $return, $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...
Unused Code introduced by
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

628
function _elgg_entity_menu_setup($hook, /** @scrutinizer ignore-unused */ $type, $return, $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...
629 1
	$entity = elgg_extract('entity', $params);
630 1
	if (!($entity instanceof \ElggEntity)) {
631
		return;
632
	}
633
634 1
	$handler = elgg_extract('handler', $params, false);
635 1
	if (!$handler) {
636 1
		return;
637
	}
638
	
639
	if ($entity->canEdit()) {
640
		$return[] = \ElggMenuItem::factory([
641
			'name' => 'edit',
642
			'icon' => 'edit',
643
			'text' => elgg_echo('edit'),
644
			'title' => elgg_echo('edit:this'),
645
			'href' => "$handler/edit/{$entity->getGUID()}",
646
			'priority' => 900,
647
		]);
648
	}
649
650
	if ($entity->canDelete()) {
651
		if (elgg_action_exists("$handler/delete")) {
652
			$action = "action/$handler/delete";
653
		} else {
654
			$action = "action/entity/delete";
655
		}
656
		
657
		$return[] = \ElggMenuItem::factory([
658
			'name' => 'delete',
659
			'icon' => 'delete',
660
			'text' => elgg_echo('delete'),
661
			'title' => elgg_echo('delete:this'),
662
			'href' => "$action?guid={$entity->getGUID()}",
663
			'confirm' => elgg_echo('deleteconfirm'),
664
			'priority' => 950,
665
		]);
666
	}
667
668
	return $return;
669
}
670
671
/**
672
 * Moves default menu items into a dropdown
673
 *
674
 * @param \Elgg\Hook $hook 'prepare', 'menu:entity'|'menu:river'
675
 *
676
 * @return void|ElggMenuItem[]
677
 *
678
 * @access private
679
 */
680
function _elgg_menu_transform_to_dropdown(\Elgg\Hook $hook) {
681 2
	$result = $hook->getValue();
682
	
683 2
	$items = elgg_extract('default', $result);
684 2
	if (empty($items)) {
685
		return;
686
	}
687
		
688 2
	$result['default'] = [
689 2
		\ElggMenuItem::factory([
690 2
			'name' => 'entity-menu-toggle',
691 2
			'icon' => 'ellipsis-v',
692
			'href' => false,
693 2
			'text' => '',
694
			'child_menu' => [
695 2
				'display' => 'dropdown',
696 2
				'data-position' => json_encode([
697 2
					'at' => 'right bottom',
698
					'my' => 'right top',
699
					'collision' => 'fit fit',
700
				]),
701 2
				'class' => "elgg-{$hook->getParam('name')}-dropdown-menu",
702
			],
703 2
			'children' => $items,
704
		]),
705
	];
706
	
707 2
	return $result;
708
}
709
710
/**
711
 * Entity navigation menu is previous/next link for an entity
712
 *
713
 * @param \Elgg\Hook $hook 'register' 'menu:entity_navigation'
714
 *
715
 * @return void|ElggMenuItem[]
716
 *
717
 * @access private
718
 */
719
function _elgg_entity_navigation_menu_setup(\Elgg\Hook $hook) {
720 1
	$entity = $hook->getEntityParam();
721 1
	if (!$entity) {
722
		return;
723
	}
724
725 1
	$return = $hook->getValue();
726
727
	$options = [
728 1
		'type' => $entity->getType(),
729 1
		'subtype' => $entity->getSubtype(),
730 1
		'container_guid' => $entity->container_guid,
731 1
		'wheres' => ["e.guid != {$entity->guid}"],
732 1
		'limit' => 1,
733
	];
734
735 1
	$previous_options = $options;
736 1
	$previous_options['created_time_upper'] = $entity->time_created;
737 1
	$previous_options['order_by'] = 'e.time_created DESC, e.guid DESC';
738
739 1
	$previous = elgg_get_entities($previous_options);
740 1
	if ($previous) {
741
		$previous = $previous[0];
742
		$return[] = \ElggMenuItem::factory([
743
			'name' => 'previous',
744
			'text' => elgg_echo('previous'),
745
			'href' => $previous->getUrl(),
746
			'title' => $previous->getDisplayName(),
747
			'icon' => 'angle-double-left',
748
			'link_class' => 'elgg-button elgg-button-outline',
749
			'priority' => 100,
750
		]);
751
	}
752
	
753 1
	$next_options = $options;
754 1
	$next_options['created_time_lower'] = $entity->time_created;
755 1
	$next_options['order_by'] = 'e.time_created ASC, e.guid ASC';
756
	
757 1
	$next = elgg_get_entities($next_options);
758 1
	if ($next) {
759
		$next = $next[0];
760
		$return[] = \ElggMenuItem::factory([
761
			'name' => 'next',
762
			'text' => elgg_echo('next'),
763
			'href' => $next->getUrl(),
764
			'title' => $next->getDisplayName(),
765
			'icon_alt' => 'angle-double-right',
766
			'link_class' => 'elgg-button elgg-button-outline',
767
			'priority' => 800,
768
		]);
769
	}
770
	
771 1
	return $return;
772
}
773
774
/**
775
 * Widget menu is a set of widget controls
776
 *
777
 * @param string         $hook   'register'
778
 * @param string         $type   'menu:widget'
779
 * @param ElggMenuItem[] $return current return value
780
 * @param array          $params supplied params
781
 *
782
 * @return void|ElggMenuItem[]
783
 *
784
 * @access private
785
 */
786
function _elgg_widget_menu_setup($hook, $type, $return, $params) {
2 ignored issues
show
Unused Code introduced by
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

786
function _elgg_widget_menu_setup($hook, /** @scrutinizer ignore-unused */ $type, $return, $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...
Unused Code introduced by
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

786
function _elgg_widget_menu_setup(/** @scrutinizer ignore-unused */ $hook, $type, $return, $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...
787
788
	$widget = elgg_extract('entity', $params);
789
	if (!($widget instanceof \ElggWidget)) {
790
		return;
791
	}
792
	
793
	if ($widget->canDelete()) {
794
		$return[] = \ElggMenuItem::factory([
795
			'name' => 'delete',
796
			'text' => elgg_view_icon('delete-alt'),
797
			'title' => elgg_echo('widget:delete', [$widget->getTitle()]),
798
			'href' => "action/widgets/delete?widget_guid=$widget->guid",
799
			'is_action' => true,
800
			'link_class' => 'elgg-widget-delete-button',
801
			'id' => "elgg-widget-delete-button-$widget->guid",
802
			'data-elgg-widget-type' => $widget->handler,
803
			'priority' => 900,
804
		]);
805
	}
806
	
807
	$show_edit = elgg_extract('show_edit', $params, $widget->canEdit());
808
	if ($show_edit) {
809
		$return[] = \ElggMenuItem::factory([
810
			'name' => 'settings',
811
			'text' => elgg_view_icon('settings-alt'),
812
			'title' => elgg_echo('widget:edit'),
813
			'href' => "#widget-edit-$widget->guid",
814
			'link_class' => "elgg-widget-edit-button",
815
			'rel' => 'toggle',
816
			'priority' => 800,
817
		]);
818
	}
819
820
	return $return;
821
}
822
823
/**
824
 * Add the register and forgot password links to login menu
825
 *
826
 * @param string         $hook   'register'
827
 * @param string         $type   'menu:login'
828
 * @param ElggMenuItem[] $return current return value
829
 * @param array          $params supplied params
830
 *
831
 * @return ElggMenuItem[]
832
 *
833
 * @access private
834
 */
835
function _elgg_login_menu_setup($hook, $type, $return, $params) {
3 ignored issues
show
Unused Code introduced by
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

835
function _elgg_login_menu_setup(/** @scrutinizer ignore-unused */ $hook, $type, $return, $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...
Unused Code introduced by
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

835
function _elgg_login_menu_setup($hook, $type, $return, /** @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...
Unused Code introduced by
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

835
function _elgg_login_menu_setup($hook, /** @scrutinizer ignore-unused */ $type, $return, $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...
836
837 1
	if (_elgg_config()->allow_registration) {
838 1
		$return[] = \ElggMenuItem::factory([
839 1
			'name' => 'register',
840 1
			'href' => elgg_get_registration_url(),
841 1
			'text' => elgg_echo('register'),
842 1
			'link_class' => 'registration_link',
843
		]);
844
	}
845
846 1
	$return[] = \ElggMenuItem::factory([
847 1
		'name' => 'forgotpassword',
848 1
		'href' => 'forgotpassword',
849 1
		'text' => elgg_echo('user:password:lost'),
850 1
		'link_class' => 'forgot_link',
851
	]);
852
853 1
	return $return;
854
}
855
856
/**
857
 * Add the RSS link to the menu
858
 *
859
 * @param string         $hook   'register'
860
 * @param string         $type   'menu:footer'
861
 * @param ElggMenuItem[] $return current return value
862
 * @param array          $params supplied params
863
 *
864
 * @return void|ElggMenuItem[]
865
 *
866
 * @access private
867
 */
868
function _elgg_rss_menu_setup($hook, $type, $return, $params) {
3 ignored issues
show
Unused Code introduced by
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

868
function _elgg_rss_menu_setup($hook, /** @scrutinizer ignore-unused */ $type, $return, $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...
Unused Code introduced by
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

868
function _elgg_rss_menu_setup(/** @scrutinizer ignore-unused */ $hook, $type, $return, $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...
Unused Code introduced by
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

868
function _elgg_rss_menu_setup($hook, $type, $return, /** @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...
869
870 1
	if (!elgg_is_logged_in()) {
871 1
		return;
872
	}
873
	
874
	if (!_elgg_has_rss_link()) {
875
		return;
876
	}
877
	
878
	if (_elgg_config()->disable_rss) {
879
		return;
880
	}
881
882
	$return[] = ElggMenuItem::factory([
883
		'name' => 'rss',
884
		'text' => elgg_echo('feed:rss'),
885
		'icon' => 'rss',
886
		'href' => elgg_http_add_url_query_elements(current_page_url(), [
887
			'view' => 'rss',
888
		]),
889
		'title' => elgg_echo('feed:rss:title'),
890
	]);
891
892
	return $return;
893
}
894
895
/**
896
 * Navigation initialization
897
 *
898
 * @return void
899
 *
900
 * @access private
901
 */
902
function _elgg_nav_init() {
903 31
	elgg_register_plugin_hook_handler('prepare', 'breadcrumbs', 'elgg_prepare_breadcrumbs');
904
905 31
	elgg_register_plugin_hook_handler('prepare', 'menu:site', '_elgg_site_menu_setup', 999);
906 31
	elgg_register_plugin_hook_handler('register', 'menu:site', '_elgg_site_menu_init');
907
908 31
	elgg_register_plugin_hook_handler('prepare', 'menu:page', '_elgg_page_menu_setup', 999);
909
910 31
	elgg_register_plugin_hook_handler('prepare', 'menu:entity', '_elgg_menu_transform_to_dropdown');
911 31
	elgg_register_plugin_hook_handler('prepare', 'menu:river', '_elgg_menu_transform_to_dropdown');
912 31
	elgg_register_plugin_hook_handler('register', 'menu:entity', '_elgg_entity_menu_setup');
913 31
	elgg_register_plugin_hook_handler('register', 'menu:widget', '_elgg_widget_menu_setup');
914 31
	elgg_register_plugin_hook_handler('register', 'menu:login', '_elgg_login_menu_setup');
915 31
	elgg_register_plugin_hook_handler('register', 'menu:footer', '_elgg_rss_menu_setup');
916 31
	elgg_register_plugin_hook_handler('register', 'menu:entity_navigation', '_elgg_entity_navigation_menu_setup');
917
918 31
	elgg_register_plugin_hook_handler('public_pages', 'walled_garden', '_elgg_nav_public_pages');
919
920 31
	if (!_elgg_config()->remove_branding) {
921 31
		elgg_register_menu_item('footer', \ElggMenuItem::factory([
922 31
			'name' => 'powered',
923 31
			'text' => elgg_echo("elgg:powered"),
924 31
			'href' => 'http://elgg.org',
925 31
			'title' => 'Elgg ' . elgg_get_version(true),
1 ignored issue
show
Bug introduced by
Are you sure elgg_get_version(true) of type false can be used in concatenation? ( Ignorable by Annotation )

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

925
			'title' => 'Elgg ' . /** @scrutinizer ignore-type */ elgg_get_version(true),
Loading history...
926 31
			'section' => 'meta',
927 31
			'priority' => 600,
928
		]));
929
	}
930
	
931 31
	elgg_register_ajax_view('navigation/menu/user_hover/contents');
932
933
	// Using a view extension to ensure that themes that have replaced the item view
934
	// still load the required AMD modules
935 31
	elgg_extend_view('navigation/menu/elements/item', 'navigation/menu/elements/item_deps');
936 31
}
937
938
/**
939
 * Extend public pages
940
 *
941
 * @param string   $hook_name    "public_pages"
942
 * @param string   $entity_type  "walled_garden"
943
 * @param string[] $return_value Paths accessible outside the "walled garden"
944
 * @param mixed    $params       unused
945
 *
946
 * @return string[]
947
 * @access private
948
 * @since 1.11.0
949
 */
950
function _elgg_nav_public_pages($hook_name, $entity_type, $return_value, $params) {
3 ignored issues
show
Unused Code introduced by
The parameter $hook_name 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

950
function _elgg_nav_public_pages(/** @scrutinizer ignore-unused */ $hook_name, $entity_type, $return_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...
Unused Code introduced by
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

950
function _elgg_nav_public_pages($hook_name, $entity_type, $return_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...
Unused Code introduced by
The parameter $entity_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

950
function _elgg_nav_public_pages($hook_name, /** @scrutinizer ignore-unused */ $entity_type, $return_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...
951
	if (is_array($return_value)) {
952
		$return_value[] = 'navigation/menu/user_hover/contents';
953
	}
954
955
	return $return_value;
956
}
957
958
/**
959
 * @see \Elgg\Application::loadCore Do not do work here. Just register for events.
960
 */
961
return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
1 ignored issue
show
Unused Code introduced by
The parameter $hooks 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

961
return function(\Elgg\EventsService $events, /** @scrutinizer ignore-unused */ \Elgg\HooksRegistrationService $hooks) {

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...
962 18
	$events->registerHandler('init', 'system', '_elgg_nav_init');
963
};
964