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

mod/friends/start.php (1 issue)

1
<?php
2
3
/**
4
 * Friends init
5
 *
6
 * @return void
7
 */
8
function elgg_friends_plugin_init() {
9 31
	elgg_register_plugin_hook_handler('filter_tabs', 'all', '_elgg_friends_filter_tabs', 1);
10
11 31
	elgg_register_event_handler('create', 'relationship', '_elgg_send_friend_notification');
12
13 31
	elgg_register_page_handler('friends', '_elgg_friends_page_handler');
14 31
	elgg_register_page_handler('friendsof', '_elgg_friends_page_handler');
15
16 31
	elgg_register_plugin_hook_handler('entity:url', 'object', '_elgg_friends_widget_urls');
17
	
18 31
	elgg_register_plugin_hook_handler('register', 'menu:page', '_elgg_friends_page_menu');
19 31
	elgg_register_plugin_hook_handler('register', 'menu:topbar', '_elgg_friends_topbar_menu');
20 31
	elgg_register_plugin_hook_handler('register', 'menu:user_hover', '_elgg_friends_setup_user_hover_menu');
21 31
}
22
23
/**
24
 * Adds friending to user hover menu
25
 *
26
 * @param string         $hook   'register'
27
 * @param string         $type   'menu:user_hover'
28
 * @param ElggMenuItem[] $return current return value
29
 * @param array          $params supplied params
30
 *
31
 * @return void|ElggMenuItem[]
32
 *
33
 * @access private
34
 */
35
function _elgg_friends_setup_user_hover_menu($hook, $type, $return, $params) {
36
	
37 1
	$user = elgg_extract('entity', $params);
38 1
	if (!$user instanceof ElggUser || !elgg_is_logged_in()) {
39 1
		return;
40
	}
41
42
	if (elgg_get_logged_in_user_guid() === $user->guid) {
43
		return;
44
	}
45
	
46
	$isFriend = $user->isFriend();
47
48
	// Always emit both to make it super easy to toggle with ajax
49
	$return[] = \ElggMenuItem::factory([
50
		'name' => 'remove_friend',
51
		'href' => "action/friends/remove?friend={$user->guid}",
52
		'is_action' => true,
53
		'text' => elgg_echo('friend:remove'),
54
		'icon' => 'user-times',
55
		'section' => 'action',
56
		'item_class' => $isFriend ? '' : 'hidden',
57
		'data-toggle' => 'add_friend',
58
	]);
59
60
	$return[] = \ElggMenuItem::factory([
61
		'name' => 'add_friend',
62
		'href' => "action/friends/add?friend={$user->guid}",
63
		'is_action' => true,
64
		'text' => elgg_echo('friend:add'),
65
		'icon' => 'user-plus',
66
		'section' => 'action',
67
		'item_class' => $isFriend ? 'hidden' : '',
68
		'data-toggle' => 'remove_friend',
69
	]);
70
71
	return $return;
72
}
73
74
/**
75
 * Page handler for friends-related pages
76
 *
77
 * @param array  $segments URL segments
78
 * @param string $handler  The first segment in URL used for routing
79
 *
80
 * @return bool
81
 * @access private
82
 */
83
function _elgg_friends_page_handler($segments, $handler) {
84
	elgg_set_context('friends'); // needed because pagehandler friendsof is also using this handler
85
86
	if (isset($segments[0]) && $user = get_user_by_username($segments[0])) {
87
		elgg_set_page_owner_guid($user->getGUID());
88
	}
89
90
	if (!elgg_get_page_owner_guid()) {
91
		return false;
92
	}
93
94
	switch ($handler) {
95
		case 'friends':
96
			echo elgg_view_resource("friends/index");
97
			break;
98
		case 'friendsof':
99
			echo elgg_view_resource("friends/of");
100
			break;
101
		default:
102
			return false;
103
	}
104
	return true;
105
}
106
107
/**
108
 * Register menu items for the topbar menu
109
 *
110
 * @param string         $hook   'register'
111
 * @param string         $type   'menu:topbar'
112
 * @param ElggMenuItem[] $return current return value
113
 * @param array          $params supplied params
114
 *
115
 * @return void|ElggMenuItem[]
116
 *
117
 * @access private
118
 * @since 3.0
119
 */
120
function _elgg_friends_topbar_menu($hook, $type, $return, $params) {
121
122 1
	$viewer = elgg_get_logged_in_user_entity();
123 1
	if (!$viewer) {
124 1
		return;
125
	}
126
		
127
	$return[] = \ElggMenuItem::factory([
128
		'name' => 'friends',
129
		'href' => "friends/{$viewer->username}",
130
		'text' => elgg_echo('friends'),
131
		'icon' => 'users',
132
		'title' => elgg_echo('friends'),
133
		'priority' => 300,
134
		'section' => 'alt',
135
		'parent_name' => 'account',
136
	]);
137
	
138
	return $return;
139
}
140
141
/**
142
 * Register menu items for the friends page menu
143
 *
144
 * @param string         $hook   'register'
145
 * @param string         $type   'menu:page'
146
 * @param ElggMenuItem[] $return current return value
147
 * @param array          $params supplied params
148
 *
149
 * @return void|ElggMenuItem[]
150
 *
151
 * @access private
152
 * @since 3.0
153
 */
154
function _elgg_friends_page_menu($hook, $type, $return, $params) {
155
156 1
	$owner = elgg_get_page_owner_entity();
157 1
	if (!$owner instanceof ElggUser) {
158
		return;
159
	}
160
161 1
	$return[] = \ElggMenuItem::factory([
162 1
		'name' => 'friends',
163 1
		'text' => elgg_echo('friends'),
164 1
		'href' => 'friends/' . $owner->username,
165
		'contexts' => ['friends'],
166
	]);
167
168 1
	$return[] = \ElggMenuItem::factory([
169 1
		'name' => 'friends:of',
170 1
		'text' => elgg_echo('friends:of'),
171 1
		'href' => 'friendsof/' . $owner->username,
172
		'contexts' => ['friends'],
173
	]);
174
175 1
	return $return;
176
}
177
178
/**
179
 * Notify user that someone has friended them
180
 *
181
 * @param string            $event  'create'
182
 * @param string            $type   'relationship'
183
 * @param \ElggRelationship $object Object
184
 *
185
 * @return bool
186
 * @access private
187
 */
188
function _elgg_send_friend_notification($event, $type, $object) {
189 24
	if ($object->relationship != 'friend') {
190 24
		return true;
191
	}
192
193
	$user_one = get_entity($object->guid_one);
194
	/* @var \ElggUser $user_one */
195
196
	$user_two = get_entity($object->guid_two);
197
	/* @var ElggUser $user_two */
198
199
	if (!$user_one instanceof ElggUser || !$user_two instanceof ElggUser) {
200
		return;
201
	}
202
203
	// Notification subject
204
	$subject = elgg_echo('friend:newfriend:subject', [
205
		$user_one->name
206
	], $user_two->language);
207
208
	// Notification body
209
	$body = elgg_echo("friend:newfriend:body", [
210
		$user_one->name,
211
		$user_one->getURL()
212
	], $user_two->language);
213
214
	// Notification params
215
	$params = [
216
		'action' => 'add_friend',
217
		'object' => $user_one,
218
		'friend' => $user_two,
219
		'url' => $user_two->getURL(),
220
	];
221
222
	return notify_user($user_two->guid, $object->guid_one, $subject, $body, $params);
0 ignored issues
show
Bug Best Practice introduced by
The expression return notify_user($user...ubject, $body, $params) returns the type array which is incompatible with the documented return type boolean.
Loading history...
223
}
224
225
/**
226
 * Add "Friends" tab to common filter
227
 *
228
 * @param string $hook   "filter_tabs"
229
 * @param string $type   Context
230
 * @param array  $items  Menu items to render as tabs
231
 * @param array  $params Hook params
232
 *
233
 * @return array
234
 */
235
function _elgg_friends_filter_tabs($hook, $type, $items, $params) {
236
237
	$user = elgg_extract('user', $params);
238
	if (!$user instanceof ElggUser) {
239
		return;
240
	}
241
242
	$vars = elgg_extract('vars', $params);
243
	$selected = elgg_extract('selected', $params);
244
245
	$items[] = ElggMenuItem::factory([
246
		'name' => 'friend',
247
		'text' => elgg_echo('friends'),
248
		'href' => (isset($vars['friend_link'])) ? $vars['friend_link'] : "$type/friends/{$user->username}",
249
		'selected' => ($selected == 'friends'),
250
		'priority' => 400,
251
	]);
252
	return $items;
253
}
254
255
256
/**
257
 * Returns widget URLS used in widget titles
258
 *
259
 * @param string $hook   Hook name
260
 * @param string $type   Hook type
261
 * @param string $result URL
262
 * @param array  $params Parameters
263
 * @return string|null
264
 * @access private
265
 */
266
function _elgg_friends_widget_urls($hook, $type, $result, $params) {
267 3
	$widget = elgg_extract('entity', $params);
268 3
	if (!($widget instanceof \ElggWidget)) {
269 3
		return;
270
	}
271
	
272
	if ($widget->handler !== 'friends') {
273
		return;
274
	}
275
	
276
	$owner = $widget->getOwnerEntity();
277
	if (!($owner instanceof \ElggUser)) {
278
		return;
279
	}
280
			
281
	return "friends/{$owner->username}";
282
}
283
284
return function() {
285 18
	elgg_register_event_handler('init', 'system', 'elgg_friends_plugin_init');
286
};
287