From ea8fc9199e253acbf7f63a807442d9322dd03257 Mon Sep 17 00:00:00 2001 From: Zaharia Alexandru Date: Thu, 8 Aug 2024 16:36:23 +0300 Subject: [PATCH] fix: Do not match global blocks against the current post shown with the template. issue: https://github.com/bagrinsergiu/blox-editor/issues/26972 --- admin/blocks/main.php | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/admin/blocks/main.php b/admin/blocks/main.php index cdbf3b02ee..13c864d603 100644 --- a/admin/blocks/main.php +++ b/admin/blocks/main.php @@ -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); @@ -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, @@ -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']; @@ -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); }