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 / Form / LegacyConsumer / Actions / DetermineVisibilityForRequest.php

DetermineVisibilityForRequest.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.9, at src/Form/LegacyConsumer/Actions/DetermineVisibilityForRequest.php

90 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\Form\LegacyConsumer\Actions;
4
5 use Give\Framework\FieldsAPI\Field;
6 use Give\Vendors\StellarWP\FieldConditions\Contracts\Condition;
7 use Give\Vendors\StellarWP\FieldConditions\FieldCondition;
8
9 /**
10 * @since 2.27.3 change postData to an array
11 * @since 2.21.0
12 */
13 class DetermineVisibilityForRequest
14 {
15 /** @var bool */
16 const IS_VISIBLE = true;
17
18 /** @var Field */
19 private $field;
20
21 /** @var array */
22 protected $postData;
23
24 /**
25 * @since 2.27.3 add parameter and return types
26 * @since 2.21.0
27 */
28 public function __construct(Field $field, array $postData)
29 {
30 $this->field = $field;
31 $this->postData = $postData;
32 }
33
34 /**
35 * @since 2.21.0
36 */
37 public function __invoke(): bool
38 {
39 if (!$this->fieldHasVisibilityConditions()) {
40 return self::IS_VISIBLE;
41 }
42
43 $conditions = $this->field->getVisibilityConditions();
44 return array_reduce($conditions, [$this, 'reduceVisibility'], self::IS_VISIBLE);
45 }
46
47 /**
48 * @since 2.21.0
49 * @return bool
50 */
51 protected function fieldHasVisibilityConditions(): bool
52 {
53 return method_exists($this->field, 'hasVisibilityConditions')
54 && $this->field->hasVisibilityConditions();
55 }
56
57 /**
58 * @since 2.27.3 update to use FieldConditions
59 * @since 2.21.0
60 */
61 protected function reduceVisibility(bool $visibility, Condition $condition): bool
62 {
63 $result = $this->compareConditionWithOperator($condition);
64
65 return 'and' === $condition->getLogicalOperator()
66 ? $visibility && $result
67 : $visibility || $result;
68 }
69
70 /**
71 * @since 2.29.2 Return false if the condition's field isn't present in the post data
72 * @since 2.27.3 update to use FieldConditions
73 * @since 2.21.0
74 */
75 protected function compareConditionWithOperator(Condition $condition): bool
76 {
77 if (is_a($condition, FieldCondition::class)) {
78 $conditionField = $condition->jsonSerialize()['field'];
79
80 if ( ! isset($this->postData[$conditionField])) {
81 return false;
82 }
83
84 return $condition->passes($this->postData);
85 }
86
87 return self::IS_VISIBLE;
88 }
89 }
90