Completed
Push — master ( c4606f...2cda3d )
by
unknown
15:54 queued 06:31
created

Term_Ancestor_Condition::is_fulfilled()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 28
Code Lines 20

Duplication

Lines 11
Ratio 39.29 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 20
c 1
b 0
f 1
nc 10
nop 1
dl 11
loc 28
rs 8.439
1
<?php
2
3
namespace Carbon_Fields\Container\Condition;
4
5
/**
6
 * Check if term has a specific ancestor
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 an array of ancestor term ids
27
 */
28
class Term_Ancestor_Condition extends Term_Condition {
29
30
	public function is_fulfilled( $environment ) {
31
		$term_id = $environment['term_id'];
32
		$term = $environment['term'];
33
		$ancestors = array();
34
35
		if ( $term ) {
36
			$ancestors = array_map( 'intval', get_ancestors( $term_id, $term->taxonomy ) );
37
		}
38
39
		$value = $this->get_value();
40 View Code Duplication
		switch ( $this->get_comparison_operator() ) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
			case '=': // fallthrough intended
42
			case '!=':
43
				$value = $this->get_term_id_from_full_term_descriptor( $value );
44
				break;
45
46
			case 'IN': // fallthrough intended
47
			case 'NOT IN':
48
				$value = $this->get_term_ids_from_full_term_descriptors( $value );
49
				break;
50
		}
51
52
		return $this->compare(
53
			$ancestors,
54
			$this->get_comparison_operator(),
55
			$value
56
		);
57
	}
58
}