PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.9
GiveWP – Donation Plugin and Fundraising Platform v4.16.9
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 All 255 releases
give / src / Framework / FieldsAPI / Concerns / HasVisibilityConditions.php

HasVisibilityConditions.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.9, at src/Framework/FieldsAPI/Concerns/HasVisibilityConditions.php

183 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\Framework\FieldsAPI\Concerns;
4
5 use Give\Framework\Exceptions\Primitives\InvalidArgumentException;
6 use Give\Vendors\StellarWP\FieldConditions\Contracts\Condition;
7 use Give\Vendors\StellarWP\FieldConditions\FieldCondition;
8 use Give\Vendors\StellarWP\FieldConditions\SimpleConditionSet;
9 use Give\Vendors\StellarWP\Validation\Concerns\HasValidationRules;
10 use Give\Vendors\StellarWP\Validation\Rules\ExcludeUnless;
11
12 /**
13 * @since 2.13.0
14 *
15 * @mixin HasValidationRules
16 */
17 trait HasVisibilityConditions
18 {
19 /**
20 * The node is visible by default. These are the conditions for it to remain visible.
21 *
22 * @since 2.27.3 update to use SimpleConditionSet
23 * @since 2.13.0
24 *
25 * @var SimpleConditionSet
26 */
27 protected $visibilityConditions;
28
29 /**
30 * @since 2.27.3
31 */
32 public function __construct()
33 {
34 $this->visibilityConditions = new SimpleConditionSet();
35 }
36
37 /**
38 * Get the visibility conditions.
39 *
40 * @since 2.27.3 update to use SimpleConditionSet
41 * @since 2.13.0
42 *
43 * @return Condition[]
44 */
45 public function getVisibilityConditions(): array
46 {
47 return $this->visibilityConditions->getConditions();
48 }
49
50 /**
51 * @since 2.27.3 replace with native condition set method
52 * @since 2.16.0
53 */
54 public function hasVisibilityConditions(): bool
55 {
56 return $this->visibilityConditions->hasConditions();
57 }
58
59 /**
60 * @since 2.27.3
61 */
62 public function passesVisibilityConditions(array $values): bool
63 {
64 return $this->visibilityConditions->passes($values);
65 }
66
67 /**
68 * @since 2.27.3
69 */
70 public function failsVisibilityConditions(array $values): bool
71 {
72 return $this->visibilityConditions->fails($values);
73 }
74
75 /**
76 * Set a condition for showing the node.
77 *
78 * @since 3.0.0 Fixed a bug with encoded HTML entities in the operator, ie > (greater than), < (less than)
79 * @since 2.27.3 update to use SimpleConditionSet
80 * @since 2.13.0
81 */
82 public function showIf(string $field, string $operator, $value, string $boolean = 'and'): self
83 {
84 // Fixes a bug with encoded HTML entities in the operator, ie > (greater than), < (less than)
85 $operator = html_entity_decode($operator);
86
87 if ($boolean === 'and') {
88 $this->visibilityConditions->and($field, $operator, $value);
89 } else {
90 $this->visibilityConditions->or($field, $operator, $value);
91 }
92
93 $this->updateValidationRules();
94
95 return $this;
96 }
97
98 /**
99 * Add an "or" visibility condition, useful when chained for readability.
100 *
101 * @since 2.27.3
102 */
103 public function orShowIf(string $field, string $operator, $value): self
104 {
105 $this->visibilityConditions->or($field, $operator, $value);
106
107 $this->updateValidationRules();
108
109 return $this;
110 }
111
112 /**
113 * Add an "and" visibility condition, useful when chained for readability.
114 *
115 * @since 2.27.3
116 */
117 public function andShowIf(string $field, string $operator, $value): self
118 {
119 $this->visibilityConditions->and($field, $operator, $value);
120
121 $this->updateValidationRules();
122
123 return $this;
124 }
125
126 /**
127 * Set multiple conditions for showing the node.
128 *
129 * @since 2.27.3 update to use FieldCondition
130 * @since 2.13.0
131 *
132 * @param FieldCondition|array ...$conditions
133 */
134 public function showWhen(...$conditions): self
135 {
136 foreach ($conditions as $condition) {
137 $this->visibilityConditions->append($this->normalizeCondition($condition));
138 }
139
140 $this->updateValidationRules();
141
142 return $this;
143 }
144
145 /**
146 * Updates the validation rules to include the visibility conditions. This prevents the node from being validated
147 * when the conditions are not met.
148 *
149 * This also only adds the validation rule if the node has validation rules.
150 *
151 * @since 2.27.3
152 */
153 protected function updateValidationRules()
154 {
155 if (method_exists($this, 'replaceOrPrependRule')) {
156 $this->replaceOrPrependRule(ExcludeUnless::id(), new ExcludeUnless($this->visibilityConditions));
157 }
158 }
159
160 /**
161 * Normalize the condition if in array format.
162 *
163 * @since 2.27.3 update to use FieldCondition
164 * @since 2.13.0
165 *
166 * @param FieldCondition|array $condition
167 *
168 * @throws InvalidArgumentException
169 */
170 protected function normalizeCondition($condition): FieldCondition
171 {
172 if ($condition instanceof FieldCondition) {
173 return $condition;
174 }
175
176 if (is_array($condition)) {
177 return new FieldCondition(...$condition);
178 }
179
180 throw new InvalidArgumentException('Parameter $condition must be a FieldCondition or an array.');
181 }
182 }
183