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

Hooks::alterMenuSections()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 4
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 6
cp 0
crap 12
1
<?php
2
3
namespace Elgg\DevelopersPlugin;
4
5
/**
6
 * Plugin hook handlers for Developers plugin
7
 */
8
class Hooks {
9
10
	/**
11
	 * Alter input of menu sections in "gear" popup
12
	 *
13
	 * @param string $hook   'view_vars'
14
	 * @param string $type   'navigation/menu/elements/section'
15
	 * @param array  $value  Menu section $vars
16
	 * @param array  $params Hook params
17
	 *
18
	 * @return mixed
19
	 */
20
	public static function alterMenuSectionVars($hook, $type, $value, $params) {
1 ignored issue
show
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

20
	public static function alterMenuSectionVars($hook, $type, $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...
21
		if (!elgg_in_context('developers_gear')) {
22
			return;
23
		}
24
25
		$idx = array_search('elgg-menu-page', $value['class']);
26
		if ($idx !== false) {
27
			unset($value['class'][$idx]);
28
			$value['class'][] = 'elgg-menu-gear';
29
		}
30
31
		// remove the display options
32
		foreach ($value['items'] as $item) {
33
			/* @var \ElggMenuItem $item  */
34
			$child_opts = $item->getChildMenuOptions();
35
			unset($child_opts['display']);
36
			$item->setChildMenuOptions($child_opts);
37
		}
38
39
		return $value;
40
	}
41
42
	/**
43
	 * Alter output of menu sections in "gear" popup
44
	 *
45
	 * @param string $hook   'view'
46
	 * @param string $type   'navigation/menu/elements/section'
47
	 * @param array  $output Menu section HTML
48
	 * @param array  $params Hook params
49
	 *
50
	 * @return mixed
51
	 */
52
	public static function alterMenuSections($hook, $type, $output, $params) {
53
		if (!elgg_in_context('developers_gear')) {
54
			return;
55
		}
56
57
		if (in_array('elgg-developers-gear', $params['vars']['class'])) {
58
			return "<section>$output</section>";
59
		}
60
	}
61
62
	/**
63
	 * Alter output of complete menu in "gear" popup
64
	 *
65
	 * @param string $hook   'view'
66
	 * @param string $type   'navigation/menu/default'
67
	 * @param array  $output Menu HTML
68
	 * @param array  $params Hook params
69
	 *
70
	 * @return mixed
71
	 */
72
	public static function alterMenu($hook, $type, $output, $params) {
1 ignored issue
show
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

72
	public static function alterMenu($hook, $type, $output, /** @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...
73
		if (!elgg_in_context('developers_gear')) {
74
			return;
75
		}
76
77
		$output = preg_replace('~^<nav\b[^>]+>~', '', $output);
78
		$output = preg_replace('~^</nav>$~', '', $output);
79
		return $output;
80
	}
81
	
82
	/**
83
	 * Change the to address if a forwarding address isset
84
	 *
85
	 * @param \Elgg\Hook $hook The hook for 'prepare', 'system:email'
86
	 *
87
	 * @since 3.0
88
	 * @return void|\Elgg\Email
89
	 */
90
	public static function setForwardEmailAddress(\Elgg\Hook $hook) {
91
		
92
		if (elgg_get_plugin_setting('block_email', 'developers') !== 'forward') {
93
			return;
94
		}
95
		
96
		$forward_address = elgg_get_plugin_setting('forward_email', 'developers');
97
		if (empty($forward_address)) {
98
			return;
99
		}
100
		
101
		$email = $hook->getValue();
102
		if (!($email instanceof \Elgg\Email)) {
103
			return;
104
		}
105
		
106
		$to = $email->getTo();
107
		$to->setEmail($forward_address);
108
		
109
		$email->setTo($to);
110
		
111
		return $email;
112
	}
113
	
114
	/**
115
	 * Block outgoing emails
116
	 *
117
	 * @param \Elgg\Hook $hook The hook for 'transport', 'system:email'
118
	 *
119
	 * @since 3.0
120
	 * @return void|true
121
	 */
122
	public static function blockOutgoingEmails(\Elgg\Hook $hook) {
123
		
124
		$block_setting = elgg_get_plugin_setting('block_email', 'developers');
125
		if (!in_array($block_setting, ['all', 'users'])) {
126
			// don't block outgoing e-mails
127
			return;
128
		}
129
		
130
		if ($block_setting === 'all') {
131
			// block all outgoing e-mails
132
			return true;
133
		}
134
		
135
		// only block outgoing e-mails for regular users
136
		// so check if the receiver is an admin
137
		$email = $hook->getParam('email');
138
		if (!($email instanceof \Elgg\Email)) {
139
			return;
140
		}
141
		
142
		$to = $email->getTo();
143
		$users = get_user_by_email($to->getEmail());
144
		if (empty($users)) {
145
			// no user found, so this should be blocked
146
			// as this e-mail address doesn't belong to any user
147
			return true;
148
		}
149
		
150
		foreach ($users as $user) {
151
			if (!$user->isAdmin()) {
152
				// found a non admin, so block outgoing e-mails
153
				return true;
154
			}
155
		}
156
	}
157
}
158