Completed
Push — milestone/2_0/container-condit... ( 045f6a...a2cac7 )
by
unknown
02:59
created

Term_Condition::post_has_term()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Carbon_Fields\Container\Condition;
4
5
use Carbon_Fields\App;
6
use Carbon_Fields\Toolset\WP_Toolset;
7
8
/**
9
 * Check for a specific term
10
 * 
11
 * Accepts the following values:
12
 *     Operators "=" and "!=":
13
 *         array(
14
 *             'value'=>...,
15
 *             'taxonomy'=>...,
16
 *             ['field'=>...] // "slug", "term_id" etc. - see get_term_by()
17
 *         )
18
 *     
19
 *     Operators "IN" and "NOT IN":
20
 *         array(
21
 *             array(
22
 *                 'value'=>...,
23
 *                 'taxonomy'=>...,
24
 *                 ['field'=>...]
25
 *             ),
26
 *             ...
27
 *         )
28
 *     
29
 *     Operators "REGEX" and CUSTOM" are passed the term_id
30
 */
31
class Term_Condition extends Condition {
32
33
	/**
34
	 * WP_Toolset to fetch term data with
35
	 * 
36
	 * @var WP_Toolset
37
	 */
38
	protected $wp_toolset;
39
40
	/**
41
	 * Constructor
42
	 * 
43
	 * @param WP_Toolset $wp_toolset
44
	 */
45
	public function __construct( WP_Toolset $wp_toolset ) {
46
		$this->wp_toolset = $wp_toolset;
47
		$this->set_comparers( array( 
48
			App::resolve( 'container_condition_comparer_type_equality' ),
49
			App::resolve( 'container_condition_comparer_type_contain' ),
50
			App::resolve( 'container_condition_comparer_type_regex' ),
51
			App::resolve( 'container_condition_comparer_type_custom' ),
52
		) );
53
	}
54
	
55
	/**
56
	 * Check if the condition is fulfilled
57
	 * 
58
	 * @param  array $environment
59
	 * @return bool
60
	 */
61
	public function is_fulfilled( $environment ) {
62
		$term_id = $environment['term_id'];
63
		$value = $this->wp_toolset->get_term_by_descriptor( $this->get_value() );
64
65
		return $this->first_supported_comparer_is_correct(
66
			$term_id,
67
			$this->get_comparison_operator(),
68
			$value->term_id
69
		);
70
	}
71
}