Passed
Push — master ( c2d8e3...289151 )
by Jeroen
06:06
created

elgg_get_tags()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Elgg tags
4
 * Functions for managing tags and tag clouds.
5
 *
6
 * @package Elgg.Core
7
 * @subpackage Tags
8
 */
9
10
/**
11
 * Takes in a comma-separated string and returns an array of tags
12
 * which have been trimmed
13
 *
14
 * @param string $string Comma-separated tag string
15
 *
16
 * @return mixed An array of strings or the original data if input was not a string
17
 */
18
function string_to_tag_array($string) {
19 3
	if (!is_string($string)) {
20 1
		return $string;
21
	}
22
	
23 2
	$ar = explode(",", $string);
24 2
	$ar = array_map('trim', $ar);
25 2
	$ar = array_filter($ar, 'is_not_null');
26 2
	$ar = array_map('strip_tags', $ar);
27 2
	$ar = array_unique($ar);
28 2
	return $ar;
29
}
30
31
/**
32
 * Get popular tags and their frequencies
33
 *
34
 * Accepts all options supported by {@link elgg_get_entities()}
35
 *
36
 * Returns an array of objects that include "tag" and "total" properties
37
 *
38
 * @todo When updating this function for 3.0, I have noticed that docs explicitly mention
39
 *       that tags must be registered, but it was not really checked anywhere in code
40
 *       So, either update the docs or decide what the behavior should be
41
 *
42
 * @param array $options Options
43
 *
44
 * @option int      $threshold Minimum number of tag occurrences
45
 * @option string[] $tag_names Names of registered tag names to include in search
46
 *
47
 * @return 	object[]|false
48
 * @since 1.7.1
49
 */
50
function elgg_get_tags(array $options = []) {
51
	return _elgg_services()->metadataTable->getTags($options);
52 1
}
53
54
/**
55
 * Registers a metadata name as containing tags for an entity.
56 1
 * This is required if you are using a non-standard metadata name
57
 * for your tags.
58 1
 *
59 1
 * Because tags are simply names of metadata, This is used
60
 * in search to prevent data exposure by searching on
61 1
 * arbitrary metadata.
62 1
 *
63
 * @param string $name Tag name
64
 *
65
 * @return bool
66 1
 * @since 1.7.0
67
 */
68 1
function elgg_register_tag_metadata_name($name) {
69 1
	return _elgg_services()->metadataTable->registerTagName($name);
70
}
71 1
72 1
/**
73 1
 * Unregister metadata tag name
74 1
 *
75 1
 * @param string $name Tag name
76 1
 *
77 1
 * @return bool
78 1
 * @since 3.0
79
 */
80 1
function elgg_unregister_tag_metadata_name($name) {
81 1
	return _elgg_services()->metadataTable->unregisterTagName($name);
82 1
}
83
84 1
/**
85
 * Returns an array of valid metadata names for tags.
86
 *
87
 * @return string[]
88
 * @since 1.7.0
89
 */
90
function elgg_get_registered_tag_metadata_names() {
91
	return _elgg_services()->metadataTable->getTagNames();
92
}
93
94
/**
95
 * Tags init
96
 *
97
 * @return void
98
 *
99
 * @access private
100
 */
101
function _elgg_tags_init() {
102 32
	// register the standard tags metadata name
103
	elgg_register_tag_metadata_name('tags');
104
}
105
106
/**
107
 * @see \Elgg\Application::loadCore Do not do work here. Just register for events.
108
 */
109
return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
1 ignored issue
show
Unused Code introduced by
The parameter $hooks 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

109
return function(\Elgg\EventsService $events, /** @scrutinizer ignore-unused */ \Elgg\HooksRegistrationService $hooks) {

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...
110
	$events->registerHandler('init', 'system', '_elgg_tags_init');
111
};
112