Skip to content

Commit

Permalink
feat: and operators customizer component data (#1275)
Browse files Browse the repository at this point in the history
* feat: and operators customizer component data

* fix: lint
  • Loading branch information
NiclasNorin authored Feb 3, 2025
1 parent 8cb4469 commit 96baa13
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
46 changes: 41 additions & 5 deletions library/Customizer/Applicators/Types/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ public function applyData(array|object $data)
$this->wpService->addFilter('ComponentLibrary/Component/Data', [$this, 'applyDataFilterFunction'], 10, 1);
}

/**
* Apply data filter function to the provided data.
*
* @param array $data The data to apply the filter on.
* @return array The filtered data.
* @throws Error If the operator or context is not set correctly.
*/
public function applyDataFilterFunction($data)
{
$storedComponentData = $this->cachedData;
Expand All @@ -35,20 +42,25 @@ public function applyDataFilterFunction($data)
foreach ($storedComponentData as $filter) {
$passFilterRules = false;

$andOperators = array_filter($filter['contexts'], function ($context) {
return in_array($context['operator'], ['===', '!=='], true);
});

foreach ($filter['contexts'] as $filterContext) {
// Operator and context must be set
if (!isset($filterContext['operator']) || !isset($filterContext['context'])) {
throw new Error("Operator must be != or == to be used in ComponentData applicator. Context must be set. Provided values: " . print_r($filterContext, true));
throw new Error("Operator must be !=, !== or ==, === to be used in ComponentData applicator. Context must be set. Provided values: " . print_r($filterContext, true));
}

// Operator must be != or ==
if (!in_array($filterContext['operator'], ["!=", "=="])) {
throw new Error("Operator must be != or == to be used in ComponentData applicator. Provided value: " . $filterContext['operator']);
if (!in_array($filterContext['operator'], ["!=", "==", "!==", "==="])) {
throw new Error("Operator must be !=, !== or ==, === to be used in ComponentData applicator. Provided value: " . $filterContext['operator']);
}

if (
($filterContext['operator'] == "==" && in_array($filterContext['context'], $contexts)) ||
($filterContext['operator'] == "!=" && !in_array($filterContext['context'], $contexts))
(($filterContext['operator'] == "==" && in_array($filterContext['context'], $contexts)) ||
($filterContext['operator'] == "!=" && !in_array($filterContext['context'], $contexts))) &&
$this->checkAndOperators($andOperators, $contexts)
) {
$passFilterRules = true;
}
Expand All @@ -62,6 +74,30 @@ public function applyDataFilterFunction($data)
return $data;
}

/**
* Checks if all the given contexts satisfy the specified operators.
*
* @param array $andOperators An array of and operators, each containing a 'context' and 'operator'.
* @param array $contexts An array of contexts to check against.
* @return bool Returns true if all the contexts satisfy the operators, false otherwise.
*/
private function checkAndOperators(array $andOperators, array $contexts)
{
foreach ($andOperators as $andOperator) {
$context = $andOperator['context'];
$operator = $andOperator['operator'];

if (
($operator == "===" && !in_array($context, $contexts)) ||
($operator == "!==" && in_array($context, $contexts))
) {
return false;
}
}

return true;
}

public function getData(): array
{
$fields = $this->getFields();
Expand Down
2 changes: 1 addition & 1 deletion library/Customizer/Sections/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function getShapeFieldAttributes(string $sectionID)
],
[
'context' => 'component.megamenu.button.child',
'operator' => '!='
'operator' => '!=='
],
],
],
Expand Down

0 comments on commit 96baa13

Please sign in to comment.