settingsForm = $form; $iteratorRecursive = static function ($block) use (&$iteratorRecursive, $form) { /** @var Block $block */ $reflector = new ReflectionMethod($block, 'settingsForm'); if (! $block->noSettings(null) && $reflector->getDeclaringClass()->getName() !== Block::class) { $block->settingsFormTab($form); $block->settingsForm($form); } $block->rotationSettingsForm($form); $block->blocksIterator($iteratorRecursive); }; $this->settingsFillAllowed(false); $iteratorRecursive($this); $this->settingsFillAllowed(true); } /** * Manage settings fill allowed * @return bool */ public function settingsFillAllowed(?bool $allow = null) { if (! is_null($allow)) { $this->settingsFillAllowed = $allow; if ($this->settingsFormPage && $this->settingsFormPage !== $this) { $this->settingsFormPage->settingsFillAllowed($allow); } } return $this->settingsFillAllowed; } /** * Get block settings form instance * @return Form|null */ public function getSettingsForm() { if (! is_null($this->settingsForm)) { return $this->settingsForm; } $page = $this->getParentPage(); if (! $page) { return null; } $page = $page->getAliasFor() ?? $page; return $this->app->theme()->getPageSettingsForm($page, $this->settingsFormPage); } /** * Get block settings by key * @param string $key * @return mixed|null */ public function getBlockSettings($key) { if (is_array($this->settingsData)) { return $this->settingsData[$key] ?? null; } return null; } /** * Load settings & cache * @param string|null $lang */ public function loadSettings(?string $lang = null) { if (! is_null($this->settingsData)) { return; } $cacheKey = $this->settingsCacheKey($lang); $this->settingsData = Cache::rememberForever($cacheKey, function () { $data = []; # Save blocks state to revert after building settings form & cache data $blocks = $this->blocks; $form = $this->getSettingsForm(); if ($form) { $iteratorRecursive = static function ($block) use (&$iteratorRecursive, $form, &$data) { /** @var Block $block */ $key = $block->getKey(); $reflector = new ReflectionMethod($block, 'settingsForm'); if ($reflector->getDeclaringClass()->getName() !== Block::class) { $block->settingsFormTab($form); $data[$key] = array_merge($data[$key] ?? [], $form->getActiveTabValues()); } $rotation = $block->rotationSettingsValues($form); if (! empty($rotation)) { $data[$key] = array_merge($data[$key] ?? [], $rotation); } $block->blocksIterator($iteratorRecursive); }; $this->settingsFillAllowed(false); $iteratorRecursive($this); $this->settingsFillAllowed(true); } $this->blocks = $blocks; return $data; }); # json to array if (is_string($this->settingsData)) { $this->settingsData = json_decode($this->settingsData, true); $toArray = true; } if (! is_array($this->settingsData)) { $this->settingsData = []; $toArray = true; } if (isset($toArray)) { Cache::forever($cacheKey, $this->settingsData); } if ($this->settingsFormPage && $this->settingsFormPage !== $this) { $this->settingsFormPage->loadSettings($lang); } } /** * Get settings cache key * @param string|null $lang * @return string */ protected function settingsCacheKey(?string $lang = null) { return 'view.settings.' . $this->getKey() . '.' . ($lang ?? $this->locale->current()); } /** * Reset settings cache */ public function settingsCacheReset() { foreach ($this->locale->getLanguages() as $lang) { Cache::forget($this->settingsCacheKey($lang)); } } }