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

mod/invitefriends/start.php (1 issue)

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');
0 ignored issues
show
Deprecated Code introduced by Evan Winslow
The function elgg_register_page_handler() has been deprecated: 3.0 ( Ignorable by Annotation )

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

14
	/** @scrutinizer ignore-deprecated */ elgg_register_page_handler('invite', 'invitefriends_page_handler');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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) {
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