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

mod/invitefriends/start.php (3 issues)

1
<?php
2
/**
3
 * Elgg invite friends
4
 *
5
 * @package ElggInviteFriends
6
 */
7
8
/**
9
 * Invite friend init
10
 *
11
 * @return void
12
 */
13
function invitefriends_init() {
14 31
	elgg_register_page_handler('invite', 'invitefriends_page_handler');
15
16 31
	elgg_register_plugin_hook_handler('register', 'user', 'invitefriends_add_friends');
17
18 31
	elgg_register_plugin_hook_handler('register', 'menu:page', 'invitefriends_register_page_menu');
19 31
}
20
21
/**
22
 * Page handler function
23
 *
24
 * @param array $page Page URL segments
25
 * @return bool
26
 */
27
function invitefriends_page_handler($page) {
28
	echo elgg_view_resource('invitefriends/invite');
29
	return true;
30
}
31
32
/**
33
 * Adds menu items to the page menu
34
 *
35
 * @param string         $hook   'register'
36
 * @param string         $type   'menu:page'
37
 * @param ElggMenuItem[] $result Current items
38
 * @param array          $params Hook params
39
 *
40
 * @return void|ElggMenuItem[]
41
 */
42
function invitefriends_register_page_menu($hook, $type, $result, $params) {
3 ignored issues
show
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

42
function invitefriends_register_page_menu($hook, $type, $result, /** @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...
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

42
function invitefriends_register_page_menu($hook, /** @scrutinizer ignore-unused */ $type, $result, $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

42
function invitefriends_register_page_menu(/** @scrutinizer ignore-unused */ $hook, $type, $result, $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...
43 1
	if (!elgg_is_logged_in()) {
44 1
		return;
45
	}
46
	
47
	if (!elgg_get_config('allow_registration')) {
48
		return;
49
	}
50
	
51
	$result[] = \ElggMenuItem::factory([
52
		'name' => 'invite',
53
		'text' => elgg_echo('friends:invite'),
54
		'href' => 'invite',
55
		'contexts' => ['friends'],
56
	]);
57
	
58
	return $result;
59
}
60
61
/**
62
 * Add friends if invite code was set
63
 *
64
 * @param string $hook   'register'
65
 * @param string $type   'user'
66
 * @param bool   $result Whether to allow registration
67
 * @param array  $params Hook params
68
 *
69
 * @return void
70
 */
71
function invitefriends_add_friends($hook, $type, $result, $params) {
72
	$user = elgg_extract('user', $params);
73
	if (!($user instanceof \ElggUser)) {
74
		return;
75
	}
76
	
77
	$friend_guid = elgg_extract('friend_guid', $params);
78
	$invite_code = elgg_extract('invitecode', $params);
79
	
80
	if (!$friend_guid) {
81
		return;
82
	}
83
	
84
	$friend_user = get_user($friend_guid);
85
	if (!($friend_user instanceof \ElggUser)) {
86
		return;
87
	}
88
89
	if (!elgg_validate_invite_code($friend_user->username, $invite_code)) {
90
		return;
91
	}
92
	
93
	// Make mutual friends
94
	$user->addFriend($friend_guid, true);
95
	$friend_user->addFriend($user->guid, true);
96
}
97
98
return function() {
99 18
	elgg_register_event_handler('init', 'system', 'invitefriends_init');
100
};
101