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

embed_select_tab()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 6
nop 4
dl 0
loc 18
ccs 0
cts 10
cp 0
crap 30
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Elgg media embed plugin
4
 *
5
 * @package ElggEmbed
6
 */
7
8
/**
9
 * Init function
10
 *
11
 * @return void
12
 */
13
function embed_init() {
14 31
	elgg_extend_view('elgg.css', 'embed/css');
15 31
	elgg_extend_view('admin.css', 'embed/css');
16
17 31
	if (elgg_is_logged_in()) {
18
		elgg_register_plugin_hook_handler('register', 'menu:longtext', 'embed_longtext_menu');
19
	}
20 31
	elgg_register_plugin_hook_handler('register', 'menu:embed', 'embed_select_tab', 1000);
21
22
	// Page handler for the modal media embed
23 31
	elgg_register_page_handler('embed', 'embed_page_handler');
0 ignored issues
show
Deprecated Code introduced by
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

23
	/** @scrutinizer ignore-deprecated */ elgg_register_page_handler('embed', 'embed_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...
24
25 31
	elgg_register_plugin_hook_handler('entity:icon:url', 'object', 'embed_set_thumbnail_url', 1000);
26 31
}
27
28
/**
29
 * Add the embed menu item to the long text menu
30
 *
31
 * @param string         $hook  'register'
32
 * @param string         $type  'menu:longtext'
33
 * @param ElggMenuItem[] $items current return value
34
 * @param array          $vars  supplied params
35
 *
36
 * @return void|ElggMenuItem[]
37
 */
38
function embed_longtext_menu($hook, $type, $items, $vars) {
2 ignored issues
show
Unused Code introduced by
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

38
function embed_longtext_menu(/** @scrutinizer ignore-unused */ $hook, $type, $items, $vars) {

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
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

38
function embed_longtext_menu($hook, /** @scrutinizer ignore-unused */ $type, $items, $vars) {

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...
39
40
	if (elgg_get_context() == 'embed') {
41
		return;
42
	}
43
	
44
	$id = elgg_extract('textarea_id', $vars);
45
	if ($id === null) {
46
		return;
47
	}
48
49
	$url = 'embed';
50
51
	$page_owner = elgg_get_page_owner_entity();
52
	if ($page_owner instanceof ElggGroup && $page_owner->isMember()) {
53
		$url = elgg_http_add_url_query_elements($url, [
54
			'container_guid' => $page_owner->guid,
55
		]);
56
	}
57
58
	$items[] = ElggMenuItem::factory([
59
		'name' => 'embed',
60
		'href' => 'javascript:',
61
		'data-colorbox-opts' => json_encode([
62
			'href' => elgg_normalize_url($url),
63
		]),
64
		'text' => elgg_echo('embed:media'),
65
		'rel' => "embed-lightbox-{$id}",
66
		'link_class' => "elgg-longtext-control elgg-lightbox embed-control embed-control-{$id} elgg-lightbox",
67
		'deps' => ['elgg/embed'],
68
		'priority' => 10,
69
	]);
70
71
	return $items;
72
}
73
74
/**
75
 * Select the correct embed tab for display
76
 *
77
 * @param string         $hook  'register'
78
 * @param string         $type  'menu:embed'
79
 * @param ElggMenuItem[] $items current return value
80
 * @param array          $vars  supplied params
81
 *
82
 * @return ElggMenuItem[]
83
 */
84
function embed_select_tab($hook, $type, $items, $vars) {
2 ignored issues
show
Unused Code introduced by
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

84
function embed_select_tab(/** @scrutinizer ignore-unused */ $hook, $type, $items, $vars) {

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
The parameter $vars 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

84
function embed_select_tab($hook, $type, $items, /** @scrutinizer ignore-unused */ $vars) {

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
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

84
function embed_select_tab($hook, /** @scrutinizer ignore-unused */ $type, $items, $vars) {

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...
85
86
	// can this ba called from page handler instead?
87
	$page = get_input('page');
88
	$tab_name = array_pop(explode('/', $page));
0 ignored issues
show
Bug introduced by
explode('/', $page) cannot be passed to array_pop() as the parameter $array expects a reference. ( Ignorable by Annotation )

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

88
	$tab_name = array_pop(/** @scrutinizer ignore-type */ explode('/', $page));
Loading history...
89
	foreach ($items as $item) {
90
		if ($item->getName() == $tab_name) {
91
			$item->setSelected();
92
			elgg_set_config('embed_tab', $item);
93
		}
94
	}
95
96
	if (!elgg_get_config('embed_tab') && count($items) > 0) {
97
		$items[0]->setSelected();
98
		elgg_set_config('embed_tab', $items[0]);
99
	}
100
	
101
	return $items;
102
}
103
104
/**
105
 * Serves the content for the embed lightbox
106
 *
107
 * @param array $page URL segments
108
 *
109
 * @return true
110
 */
111
function embed_page_handler($page) {
112
113
	elgg_ajax_gatekeeper();
114
115
	$container_guid = (int) get_input('container_guid');
116
	if ($container_guid) {
117
		$container = get_entity($container_guid);
118
119
		if ($container instanceof ElggGroup && $container->isMember()) {
120
			// embedding inside a group so save file to group files
121
			elgg_set_page_owner_guid($container_guid);
122
		}
123
	}
124
125
	set_input('page', $page[1]);
126
127
	echo elgg_view('embed/layout');
128
	return true;
129
}
130
131
/**
132
 * A special listing function for selectable content
133
 *
134
 * This calls a custom list view for entities.
135
 *
136
 * @param array $entities Array of ElggEntity objects
137
 * @param array $vars     Display parameters
138
 * @return string
139
 */
140
function embed_list_items($entities, $vars = []) {
141
142
	$defaults = [
143
		'items' => $entities,
144
		'list_class' => 'elgg-list-entity',
145
	];
146
147
	$vars = array_merge($defaults, $vars);
148
149
	return elgg_view('embed/list', $vars);
150
}
151
152
/**
153
 * Set the options for the list of embedable content
154
 *
155
 * @param array $options additional options
156
 *
157
 * @return array
158
 */
159
function embed_get_list_options($options = []) {
160
161
	$container_guids = [elgg_get_logged_in_user_guid()];
162
	if (elgg_get_page_owner_guid()) {
163
		$page_owner_guid = elgg_get_page_owner_guid();
164
		if ($page_owner_guid != elgg_get_logged_in_user_guid()) {
165
			$container_guids[] = $page_owner_guid;
166
		}
167
	}
168
169
	$defaults = [
170
		'limit' => 6,
171
		'container_guids' => $container_guids,
172
		'item_class' => 'embed-item',
173
		'no_results' => elgg_echo('notfound'),
174
	];
175
176
	$options = array_merge($defaults, $options);
177
178
	return $options;
179
}
180
181
/**
182
 * Substitutes thumbnail's inline URL with a permanent URL
183
 * Registered with a very late priority of 1000 to ensure we replace all previous values
184
 *
185
 * @param string $hook   "entity:icon:url"
186
 * @param string $type   "object"
187
 * @param string $return URL
188
 * @param array  $params Hook params
189
 * @return string
190
 */
191
function embed_set_thumbnail_url($hook, $type, $return, $params) {
3 ignored issues
show
Unused Code introduced by
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

191
function embed_set_thumbnail_url(/** @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
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

191
function embed_set_thumbnail_url($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
The parameter $return 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

191
function embed_set_thumbnail_url($hook, $type, /** @scrutinizer ignore-unused */ $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...
192
193 1
	if (!elgg_in_context('embed')) {
194 1
		return;
195
	}
196
	
197
	$entity = elgg_extract('entity', $params);
198
	$size = elgg_extract('size', $params);
199
200
	$thumbnail = $entity->getIcon($size);
201
	if (!$thumbnail->exists()) {
202
		return;
203
	}
204
205
	return elgg_get_embed_url($entity, $size);
206
}
207
208
return function() {
209 18
	elgg_register_event_handler('init', 'system', 'embed_init');
210
};
211