Passed
Push — master ( c2d8e3...289151 )
by Jeroen
06:06
created

_elgg_entity_menu_setup()   C

Complexity

Conditions 8
Paths 13

Size

Total Lines 51
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 42.6203

Importance

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

796
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 Cash Costello
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

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

845
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 Cash Costello
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

845
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 Cash Costello
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

845
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...
846 1
847 1
	if (_elgg_config()->allow_registration) {
848 1
		$return[] = \ElggMenuItem::factory([
849 1
			'name' => 'register',
850 1
			'href' => elgg_get_registration_url(),
851
			'text' => elgg_echo('register'),
852
			'link_class' => 'registration_link',
853 1
		]);
854
	}
855
856
	$return[] = \ElggMenuItem::factory([
857
		'name' => 'forgotpassword',
858
		'href' => elgg_generate_url('account:password:reset'),
859
		'text' => elgg_echo('user:password:lost'),
860
		'link_class' => 'forgot_link',
861
	]);
862
863
	return $return;
864
}
865
866
/**
867
 * Add the RSS link to the menu
868
 *
869
 * @param string         $hook   'register'
870 1
 * @param string         $type   'menu:footer'
871 1
 * @param ElggMenuItem[] $return current return value
872
 * @param array          $params supplied params
873
 *
874
 * @return void|ElggMenuItem[]
875
 *
876
 * @access private
877
 */
878
function _elgg_rss_menu_setup($hook, $type, $return, $params) {
3 ignored issues
show
Unused Code introduced by Jeroen Dalsem
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

878
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...
Unused Code introduced by Jeroen Dalsem
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

878
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 Jeroen Dalsem
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

878
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...
879
880
	if (!elgg_is_logged_in()) {
881
		return;
882
	}
883
	
884
	if (!_elgg_has_rss_link()) {
885
		return;
886
	}
887
	
888
	if (_elgg_config()->disable_rss) {
889
		return;
890
	}
891
892
	$return[] = ElggMenuItem::factory([
893
		'name' => 'rss',
894
		'text' => elgg_echo('feed:rss'),
895
		'icon' => 'rss',
896
		'href' => elgg_http_add_url_query_elements(current_page_url(), [
897
			'view' => 'rss',
898
		]),
899
		'title' => elgg_echo('feed:rss:title'),
900
	]);
901
902
	return $return;
903 31
}
904
905 31
/**
906 31
 * Navigation initialization
907
 *
908 31
 * @return void
909
 *
910 31
 * @access private
911 31
 */
912 31
function _elgg_nav_init() {
913 31
	elgg_register_plugin_hook_handler('prepare', 'breadcrumbs', 'elgg_prepare_breadcrumbs');
914 31
915 31
	elgg_register_plugin_hook_handler('prepare', 'menu:site', '_elgg_site_menu_setup', 999);
916 31
	elgg_register_plugin_hook_handler('register', 'menu:site', '_elgg_site_menu_init');
917
918 31
	elgg_register_plugin_hook_handler('prepare', 'menu:page', '_elgg_page_menu_setup', 999);
919
920 31
	elgg_register_plugin_hook_handler('prepare', 'menu:entity', '_elgg_menu_transform_to_dropdown');
921 31
	elgg_register_plugin_hook_handler('prepare', 'menu:river', '_elgg_menu_transform_to_dropdown');
922 31
	elgg_register_plugin_hook_handler('register', 'menu:entity', '_elgg_entity_menu_setup');
923 31
	elgg_register_plugin_hook_handler('register', 'menu:widget', '_elgg_widget_menu_setup');
924 31
	elgg_register_plugin_hook_handler('register', 'menu:login', '_elgg_login_menu_setup');
925 31
	elgg_register_plugin_hook_handler('register', 'menu:footer', '_elgg_rss_menu_setup');
926 31
	elgg_register_plugin_hook_handler('register', 'menu:entity_navigation', '_elgg_entity_navigation_menu_setup');
927 31
928
	elgg_register_plugin_hook_handler('public_pages', 'walled_garden', '_elgg_nav_public_pages');
929
930
	if (!_elgg_config()->remove_branding) {
931 31
		elgg_register_menu_item('footer', \ElggMenuItem::factory([
932
			'name' => 'powered',
933
			'text' => elgg_echo("elgg:powered"),
934
			'href' => 'http://elgg.org',
935 31
			'title' => 'Elgg ' . elgg_get_version(true),
1 ignored issue
show
Bug introduced by Jeroen Dalsem
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

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

960
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 Jeroen Dalsem
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

960
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...
Unused Code introduced by Jeroen Dalsem
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

960
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...
961
	if (is_array($return_value)) {
962 18
		$return_value[] = 'navigation/menu/user_hover/contents';
963 18
	}
964
965
	return $return_value;
966
}
967
968
/**
969
 * @see \Elgg\Application::loadCore Do not do work here. Just register for events.
970
 */
971
return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
1 ignored issue
show
Unused Code introduced by Steve Clay
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

971
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...
972
	$events->registerHandler('init', 'system', '_elgg_nav_init');