Skip to content

Commit 96baa13

Browse files
authored
feat: and operators customizer component data (#1275)
* feat: and operators customizer component data * fix: lint
1 parent 8cb4469 commit 96baa13

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

library/Customizer/Applicators/Types/Component.php

+41-5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ public function applyData(array|object $data)
2626
$this->wpService->addFilter('ComponentLibrary/Component/Data', [$this, 'applyDataFilterFunction'], 10, 1);
2727
}
2828

29+
/**
30+
* Apply data filter function to the provided data.
31+
*
32+
* @param array $data The data to apply the filter on.
33+
* @return array The filtered data.
34+
* @throws Error If the operator or context is not set correctly.
35+
*/
2936
public function applyDataFilterFunction($data)
3037
{
3138
$storedComponentData = $this->cachedData;
@@ -35,20 +42,25 @@ public function applyDataFilterFunction($data)
3542
foreach ($storedComponentData as $filter) {
3643
$passFilterRules = false;
3744

45+
$andOperators = array_filter($filter['contexts'], function ($context) {
46+
return in_array($context['operator'], ['===', '!=='], true);
47+
});
48+
3849
foreach ($filter['contexts'] as $filterContext) {
3950
// Operator and context must be set
4051
if (!isset($filterContext['operator']) || !isset($filterContext['context'])) {
41-
throw new Error("Operator must be != or == to be used in ComponentData applicator. Context must be set. Provided values: " . print_r($filterContext, true));
52+
throw new Error("Operator must be !=, !== or ==, === to be used in ComponentData applicator. Context must be set. Provided values: " . print_r($filterContext, true));
4253
}
4354

4455
// Operator must be != or ==
45-
if (!in_array($filterContext['operator'], ["!=", "=="])) {
46-
throw new Error("Operator must be != or == to be used in ComponentData applicator. Provided value: " . $filterContext['operator']);
56+
if (!in_array($filterContext['operator'], ["!=", "==", "!==", "==="])) {
57+
throw new Error("Operator must be !=, !== or ==, === to be used in ComponentData applicator. Provided value: " . $filterContext['operator']);
4758
}
4859

4960
if (
50-
($filterContext['operator'] == "==" && in_array($filterContext['context'], $contexts)) ||
51-
($filterContext['operator'] == "!=" && !in_array($filterContext['context'], $contexts))
61+
(($filterContext['operator'] == "==" && in_array($filterContext['context'], $contexts)) ||
62+
($filterContext['operator'] == "!=" && !in_array($filterContext['context'], $contexts))) &&
63+
$this->checkAndOperators($andOperators, $contexts)
5264
) {
5365
$passFilterRules = true;
5466
}
@@ -62,6 +74,30 @@ public function applyDataFilterFunction($data)
6274
return $data;
6375
}
6476

77+
/**
78+
* Checks if all the given contexts satisfy the specified operators.
79+
*
80+
* @param array $andOperators An array of and operators, each containing a 'context' and 'operator'.
81+
* @param array $contexts An array of contexts to check against.
82+
* @return bool Returns true if all the contexts satisfy the operators, false otherwise.
83+
*/
84+
private function checkAndOperators(array $andOperators, array $contexts)
85+
{
86+
foreach ($andOperators as $andOperator) {
87+
$context = $andOperator['context'];
88+
$operator = $andOperator['operator'];
89+
90+
if (
91+
($operator == "===" && !in_array($context, $contexts)) ||
92+
($operator == "!==" && in_array($context, $contexts))
93+
) {
94+
return false;
95+
}
96+
}
97+
98+
return true;
99+
}
100+
65101
public function getData(): array
66102
{
67103
$fields = $this->getFields();

library/Customizer/Sections/Button.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public function getShapeFieldAttributes(string $sectionID)
129129
],
130130
[
131131
'context' => 'component.megamenu.button.child',
132-
'operator' => '!='
132+
'operator' => '!=='
133133
],
134134
],
135135
],

0 commit comments

Comments
 (0)