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
|
|
|
} |