From 48c23f1ffe724b43a5e2903a4dfd078f18c680bc Mon Sep 17 00:00:00 2001 From: Mostafa Soufi Date: Thu, 25 Mar 2021 10:23:58 +0200 Subject: [PATCH] Support overwite block types on child theme --- src/Blocks.php | 48 ++++++++++++++++++++++++++++++-- src/FlexibleContentBlockType.php | 17 +++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/src/Blocks.php b/src/Blocks.php index 382e14a..a1df636 100644 --- a/src/Blocks.php +++ b/src/Blocks.php @@ -45,6 +45,16 @@ class Blocks */ protected $building = false; + /** + * @var mixed|null + */ + private $nameSpace; + + /** + * @var mixed|null + */ + private $childNameSpace; + /** * ContentBuilder constructor */ @@ -64,6 +74,10 @@ public function init($config = []) // Parse block types from given config $blockTypes = $this->parseConfig($config); + // Set parent & child namespaces + $this->nameSpace = isset($config['namespace']) ? $config['namespace'] : null; + $this->childNameSpace = isset($config['child_namespace']) ? $config['child_namespace'] : null; + // Register blocks from config, if applicable $this->registerBlockTypes($blockTypes); } @@ -86,10 +100,20 @@ protected function parseConfig($config) if (array_key_exists('namespace', $config) && array_key_exists('blocktypes', $config)) { // Add trailing slash if it's missing - $namespace = rtrim($config['namespace'], '\\') . '\\'; + $namespace = rtrim($config['namespace'], '\\') . '\\'; + $childNamespace = rtrim($config['child_namespace'], '\\') . '\\'; foreach ($config['blocktypes'] as &$blockType) { if (is_string($blockType)) { + + /** + * Overwrite child block type if exists + */ + if (class_exists($childNamespace . $blockType)) { + $blockType = $childNamespace . $blockType; + continue; + } + $blockType = $namespace . $blockType; } } @@ -321,7 +345,7 @@ public function getBuilder($postId = null, $blockName = null) if (!isset($this->builders[$postId])) { if (!$this->building || $postId === 'option') { - $this->building = true; + $this->building = true; $this->builders[$postId] = $this->createContentBuilder($postId); } else { if (!$blockName) { @@ -338,6 +362,26 @@ public function getBuilder($postId = null, $blockName = null) return $this->builders[$postId]; } + /** + * Get namespace + * + * @return mixed|null + */ + public function getNameSpace() + { + return $this->nameSpace; + } + + /** + * Get child namespace + * + * @return mixed|null + */ + public function getChildNameSpace() + { + return $this->childNameSpace; + } + /** * @param $postId * diff --git a/src/FlexibleContentBlockType.php b/src/FlexibleContentBlockType.php index b5ec7cc..cab5050 100644 --- a/src/FlexibleContentBlockType.php +++ b/src/FlexibleContentBlockType.php @@ -50,7 +50,24 @@ protected function setupFlexibleContent() */ public function registerBlockType($blockType) { + /** + * Generating the child namespace + */ + $overwrittenNameSpace = str_replace( + Blocks::getInstance()->getNameSpace(), + Blocks::getInstance()->getChildNameSpace(), + $blockType + ); + if (is_string($blockType)) { + + /** + * Overwrite the child block type if exists + */ + if (class_exists($overwrittenNameSpace)) { + $blockType = $overwrittenNameSpace; + } + $blockType = new $blockType(); }