Completed
Push — development ( bb7ea5...cbb24d )
by
unknown
03:17
created

Term_Parent_Condition   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
B is_fulfilled() 0 23 6
1
<?php
2
3
namespace Carbon_Fields\Container\Condition;
4
5
/**
6
 * Check if term has a specific parent
7
 *
8
 * Accepts the following values:
9
 *     Operators "=" and "!=":
10
 *         array(
11
 *             'value'=>...,
12
 *             'taxonomy'=>...,
13
 *             ['field'=>...] // "slug", "term_id" etc. - see get_term_by()
14
 *         )
15
 *
16
 *     Operators "IN" and "NOT IN":
17
 *         array(
18
 *             array(
19
 *                 'value'=>...,
20
 *                 'taxonomy'=>...,
21
 *                 ['field'=>...]
22
 *             ),
23
 *             ...
24
 *         )
25
 *
26
 *     Operator "CUSTOM" is passed the parent term_id
27
 */
28
class Term_Parent_Condition extends Term_Condition {
29
30
	public function is_fulfilled( $environment ) {
31
		$term = $environment['term'];
32
		$parent_term_id = $term ? intval( $term->parent ) : 0;
33
		$value = $this->get_value();
34
35
		switch ( $this->get_comparison_operator() ) {
36
			case '=': // fallthrough intended
37
			case '!=':
38
				$value = $this->get_term_id_from_full_term_descriptor( $value );
39
				break;
40
41
			case 'IN': // fallthrough intended
42
			case 'NOT IN':
43
				$value = $this->get_term_ids_from_full_term_descriptors( $value );
44
				break;
45
		}
46
47
		return $this->compare(
48
			$parent_term_id,
49
			$this->get_comparison_operator(),
50
			$value
51
		);
52
	}
53
}