Skip to content

Commit

Permalink
fix: Do not match global blocks against the current post shown with t…
Browse files Browse the repository at this point in the history
…he template.

issue: bagrinsergiu/blox-editor#26972
  • Loading branch information
alecszaharia committed Aug 8, 2024
1 parent 175b927 commit ea8fc91
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions admin/blocks/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,11 @@ public function getMatchingBrizyBlocks($wpPost = null)
$ruleMatches = $this->getTemplateRuleMatches($template->getWpPost());
}

$ruleMatches[] = [
'applyFor' => Brizy_Admin_Rule::POSTS,
'entityType' => $wpPost->post_type,
'entityValues' => [$wpPost->ID],
];
// $ruleMatches[] = [
// 'applyFor' => Brizy_Admin_Rule::POSTS,
// 'entityType' => $wpPost->post_type,
// 'entityValues' => [$wpPost->ID],
// ];
}
$matching_blocks = $this->findMatchingBlocks($ruleMatches);

Expand All @@ -254,14 +254,14 @@ private function getTemplateRuleMatches(WP_Post $template)
{
$rule_manager = new Brizy_Admin_Rules_Manager();
$template_rules = $rule_manager->getRules($template->ID);
$ruleMatches = array_map(function (Brizy_Admin_Rule $r) {
return [
//'type' => $r->getType(),
'applyFor' => $r->getAppliedFor(),
'entityType' => $r->getEntityType(),
'entityValues' => $r->getEntityValues(),
];
}, $template_rules);
// $ruleMatches = array_map(function (Brizy_Admin_Rule $r) {
// return [
// //'type' => $r->getType(),
// 'applyFor' => $r->getAppliedFor(),
// 'entityType' => $r->getEntityType(),
// 'entityValues' => $r->getEntityValues(),
// ];
// }, $template_rules);
$ruleMatches[] = [
'applyFor' => Brizy_Admin_Rule::BRIZY_TEMPLATE,
'entityType' => $template->post_type,
Expand Down Expand Up @@ -292,7 +292,10 @@ private function findMatchingBlocks($ruleMatches)
foreach ($allBlocks as $aBlock) {
$ruleSets[$aBlock->ID] = $ruleManager->getRuleSet($aBlock->ID);
}

$excludeIds = [];
foreach ($ruleMatches as $ruleMatch) {
$include = null;
$applyFor = $ruleMatch['applyFor'];
$entityType = $ruleMatch['entityType'];
$entityValues = $ruleMatch['entityValues'];
Expand All @@ -306,14 +309,21 @@ private function findMatchingBlocks($ruleMatches)
if ($ruleSets[$aBlock->ID]->isMatching($applyFor, $entityType, $entityValues)) {
$resultBlocks[$aBlock->ID] = Brizy_Editor_Block::get($aBlock);
} else {
$t = 0;
$excludeIds[] = $aBlock->ID;
}
} catch (\Exception $e) {
continue; // we catch here the exclusions
$excludeIds[] = $aBlock->ID;
break;
}
}
}

foreach ($resultBlocks as $i => $resultBlock) {
if (in_array($i, $excludeIds)) {
unset($resultBlocks[$i]);
}
}

return array_values($resultBlocks);
}

Expand Down

0 comments on commit ea8fc91

Please sign in to comment.