Term::initializes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace WPDFI;
4
5
/**
6
 * This class handle all actions related with term
7
 *
8
 * @author Duc Bui Quang <[email protected]>
9
 * @since 1.0.0
10
 */
11
12
use WPDFI\Traits\Singleton;
13
14
final class Term
15
{
16
	use Singleton;
17
18
	/**
19
	 * @traitDoc
20
	 */
21
	public function initializes() 
22
	{
23
		//
24
	}
25
26
	/**
27
	 * Get all terms by given taxonomy
28
	 *
29
	 * @param string $taxonomy
30
	 * @since 1.0.0
31
	 * @return array
32
	 */
33
	public function get($taxonomy) {
34
		$names = [];
35
36
		if($taxonomy) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
introduced by
No space after opening parenthesis is prohibited
Loading history...
introduced by
No space before closing parenthesis is prohibited
Loading history...
37
38
			$terms = \get_terms([
39
				'taxonomy' => $taxonomy,
40
				'hide_empty' => false
41
			]);
42
43
			foreach($terms as $index => $term) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
introduced by
No space after opening parenthesis is prohibited
Loading history...
introduced by
No space before closing parenthesis is prohibited
Loading history...
44
45
				$names[$index]['id'] = $term->term_id;
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
46
				$names[$index]['text'] = $term->name;
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
47
48
			}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
49
50
		}
51
		
52
		return $names;
53
	}
54
55
	/**
56
	 * Format a list of WP_Term object to term_id array
57
	 * 
58
	 * @param array $terms
59
	 * @since 1.0.0
60
	 * @return array
61
	 */
62
	public function format_to_compare($terms) {
63
64
		$terms_array = [];
65
		
66
		foreach($terms as $term) {
0 ignored issues
show
introduced by
Space after opening control structure is required
Loading history...
introduced by
No space before opening parenthesis is prohibited
Loading history...
introduced by
No space after opening parenthesis is prohibited
Loading history...
introduced by
No space before closing parenthesis is prohibited
Loading history...
67
68
			$terms_array[$term->taxonomy][] = $term->term_id;
0 ignored issues
show
introduced by
Array keys should be surrounded by spaces unless they contain a string or an integer.
Loading history...
69
70
		}
71
72
		return $terms_array;
73
74
	}
75
76
77
78
79
80
}