*/ protected array $values; protected SettingsData $settingsData; protected Settings $settings; protected ViewsDataStorage $viewsDataStorage; protected CardsDataStorage $cardsDataStorage; public function __construct( SettingsData $settingsData, Settings $settings, ViewsDataStorage $viewsDataStorage, CardsDataStorage $cardsDataStorage ) { $this->values = []; $this->settingsData = $settingsData; $this->settings = $settings; $this->viewsDataStorage = $viewsDataStorage; $this->cardsDataStorage = $cardsDataStorage; } /** * @param mixed $postId */ protected function isMySource($postId): bool { $screen = get_current_screen(); return null !== $screen && 'acf_views_page_acf-views-settings' === $screen->id && 'options' === $postId; } public function addPage(): void { // do not use 'acf_add_options_page', as the global options-related functions may be unavailable // (in case of the manual include) if (!function_exists('acf_options_page')) { return; } $updatedMessage = __('Settings successfully updated.', 'acf-views'); acf_options_page()->add_page([ 'slug' => self::SLUG, 'page_title' => __('Settings', 'acf-views'), 'menu_title' => __('Settings', 'acf-views'), 'parent_slug' => sprintf('edit.php?post_type=%s', ViewsCpt::NAME), 'position' => 2, 'update_button' => __('Save changes', 'acf-views'), 'updated_message' => $updatedMessage, ]); } /** * @param mixed $postId */ public function maybeCatchValues($postId): void { if (!$this->isMySource($postId)) { return; } add_filter('acf/pre_update_value', function ($isUpdated, $value, $postId, array $field): bool { // extra check, as probably it's about another post if (!$this->isMySource($postId)) { return $isUpdated; } $fieldName = (string)($field['name'] ?? ''); $this->values[$fieldName] = $value; // avoid saving to the postmeta return true; }, 10, 4); } public function maybeInjectValues(): void { if (!$this->isMySource('options')) { return; } add_filter('acf/pre_load_value', function ($value, $postId, $field) { // extra check, as probably it's about another post if (!$this->isMySource($postId)) { return $value; } $fieldName = $field['name']; $value = ''; switch ($fieldName) { case SettingsData::getAcfFieldName(SettingsData::FIELD_IS_DEV_MODE): $value = $this->settings->isDevMode(); break; case SettingsData::getAcfFieldName(SettingsData::FIELD_IS_FILE_SYSTEM_STORAGE): $value = '' !== $this->viewsDataStorage->getBaseFolder(); break; } return $value; }, 10, 3); } /** * @param mixed $postId * @throws Exception */ public function maybeProcess($postId): void { if (!$this->isMySource($postId) || [] === $this->values) { return; } $this->settingsData->load(false, '', $this->values); $this->settings->setIsDevMode($this->settingsData->isDevMode); $themeTemplatesFolder = $this->viewsDataStorage->getBaseFolder(); if (true === $this->settingsData->isFileSystemStorage && '' === $themeTemplatesFolder && true === mkdir($this->viewsDataStorage->getTargetBaseFolder(), 0755)) { // set, as the folder was just created $this->viewsDataStorage->setBaseFolder(); $this->cardsDataStorage->setBaseFolder(); $this->viewsDataStorage->activateFileSystemStorage(); $this->cardsDataStorage->activateFileSystemStorage(); } if (false === $this->settingsData->isFileSystemStorage && '' !== $themeTemplatesFolder) { $this->viewsDataStorage->deactivateFileSystemStorage(); $this->cardsDataStorage->deactivateFileSystemStorage(); $this->viewsDataStorage->removeFolderRecursively($themeTemplatesFolder); } $this->settings->save(); } public function setHooks(bool $isAdmin): void { if (!$isAdmin) { return; } // init, not acf/init, as the method uses 'get_edit_post_link' which will be available only since this hook // (because we sign up the CPTs in this hook) add_action('init', [$this, 'addPage',]); add_action('acf/save_post', [$this, 'maybeCatchValues',]); // priority 20, as it's after the ACF's save_post hook add_action('acf/save_post', [$this, 'maybeProcess',], 20); add_action('acf/input/admin_head', [$this, 'maybeInjectValues']); } }