1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Container\Condition; |
4
|
|
|
|
5
|
|
|
use Carbon_Fields\Toolset\WP_Toolset; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Check for a specific term |
9
|
|
|
* |
10
|
|
|
* Accepts the following values: |
11
|
|
|
* Operators "=" and "!=": |
12
|
|
|
* array( |
13
|
|
|
* 'value'=>..., |
14
|
|
|
* 'taxonomy'=>..., |
15
|
|
|
* ['field'=>...] // "slug", "term_id" etc. - see get_term_by() |
16
|
|
|
* ) |
17
|
|
|
* |
18
|
|
|
* Operators "IN" and "NOT IN": |
19
|
|
|
* array( |
20
|
|
|
* array( |
21
|
|
|
* 'value'=>..., |
22
|
|
|
* 'taxonomy'=>..., |
23
|
|
|
* ['field'=>...] |
24
|
|
|
* ), |
25
|
|
|
* ... |
26
|
|
|
* ) |
27
|
|
|
* |
28
|
|
|
* Operator "CUSTOM" is passed the term_id |
29
|
|
|
*/ |
30
|
|
|
class Term_Condition extends Condition { |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* WP_Toolset to fetch term data with |
34
|
|
|
* |
35
|
|
|
* @var WP_Toolset |
36
|
|
|
*/ |
37
|
|
|
protected $wp_toolset; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Constructor |
41
|
|
|
*/ |
42
|
|
|
public function __construct( $wp_toolset ) { |
43
|
|
|
$this->wp_toolset = $wp_toolset; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get the condition value |
48
|
|
|
* |
49
|
|
|
* @return mixed |
50
|
|
|
*/ |
51
|
|
|
public function get_value() { |
52
|
|
|
$value = parent::get_value(); |
53
|
|
|
if ( is_array( $value ) ) { |
54
|
|
|
if ( isset( $value['value'] ) ) { |
55
|
|
|
$value = $this->wp_toolset->wildcard_term_descriptor_to_full_term_descriptor( $value ); |
56
|
|
|
} else { |
57
|
|
|
$value = array_map( array( $this->wp_toolset, 'wildcard_term_descriptor_to_full_term_descriptor' ), $value ); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
return $value; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Pluck term id from a full term descriptor |
65
|
|
|
* |
66
|
|
|
* @param array $full_term_descriptor |
67
|
|
|
* @return integer |
68
|
|
|
*/ |
69
|
|
|
protected function get_term_id_from_full_term_descriptor( $full_term_descriptor ) { |
70
|
|
|
return intval( $full_term_descriptor['term_object']->term_id ); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Pluck term ids from array of full term descriptors |
75
|
|
|
* |
76
|
|
|
* @param array $full_term_descriptors |
77
|
|
|
* @return array<integer> |
78
|
|
|
*/ |
79
|
|
|
protected function get_term_ids_from_full_term_descriptors( $full_term_descriptors ) { |
|
|
|
|
80
|
|
|
return array_map( array( $this, 'get_term_id_from_full_term_descriptor'), $full_term_descriptors ); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Check if the condition is fulfilled |
85
|
|
|
* |
86
|
|
|
* @param array $environment |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
public function is_fulfilled( $environment ) { |
90
|
|
|
$term_id = $environment['term_id']; |
91
|
|
|
|
92
|
|
|
switch ( $this->get_comparison_operator() ) { |
93
|
|
|
case '=': |
94
|
|
|
$value_term_id = $this->get_term_id_from_full_term_descriptor( $this->get_value() ); |
95
|
|
|
return $term_id == $value_term_id; |
96
|
|
|
break; |
|
|
|
|
97
|
|
|
case '!=': |
98
|
|
|
$value_term_id = $this->get_term_id_from_full_term_descriptor( $this->get_value() ); |
99
|
|
|
return $term_id != $value_term_id; |
100
|
|
|
break; |
|
|
|
|
101
|
|
|
case 'IN': |
102
|
|
|
$value_term_ids = $this->get_term_ids_from_full_term_descriptors( $this->get_value() ); |
103
|
|
|
return in_array( $term_id, $value_term_ids ); |
104
|
|
|
break; |
|
|
|
|
105
|
|
|
case 'NOT IN': |
106
|
|
|
$value_term_ids = $this->get_term_ids_from_full_term_descriptors( $this->get_value() ); |
107
|
|
|
return ! in_array( $term_id, $value_term_ids ); |
108
|
|
|
break; |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $this->compare( |
112
|
|
|
$term_id, |
113
|
|
|
$this->get_comparison_operator(), |
114
|
|
|
$this->get_value() |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
} |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.