cpt = $cpt; $this->options = $options; $this->postId = 0; $this->groupId = 0; $this->viewId = 0; $this->error = ''; $this->isProcessed = false; $this->isImportRequest = false; $this->acfViewData = $acfViewData->getDeepClone(); $this->item = $item->getDeepClone(); } protected function addError(string $error): void { $this->error .= $error; } protected function createPost(): void { $postId = wp_insert_post([ 'post_type' => 'post', 'post_status' => 'publish', 'post_title' => 'ACF Views Demo Post', ]); if (is_wp_error($postId)) { $this->addError('Fail to create a post'); return; } $this->postId = $postId; } protected function importAcfGroup(): array { $groupJSON = DemoGroup::getGroupInfo(); $groupJSON['title'] = 'ACF Views Demo Group'; $groupJSON['location'][0][0]['value'] = $this->postId; unset($groupJSON['key']); foreach ($groupJSON['fields'] as &$field) { $field['key'] = uniqid('field_'); } $groupJSON = acf_import_field_group($groupJSON); if (! isset($groupJSON['ID'])) { $this->addError('Fail to import an ACF group'); return []; } $this->groupId = (int)$groupJSON['ID']; return $groupJSON; } protected function fillAcfView(array $groupData): void { $view = $this->acfViewData->getDeepClone(); $view->load($this->viewId); $view->description = 'Give your view a short description...'; $bookItem = $this->item->getDeepClone(); $bookItem->group = $groupData['key']; $bookItem->field->label = 'Book:'; $bookItem->field->key = Field::createKey($groupData['key'], $groupData['fields'][0]['key']); $view->items[] = $bookItem; $publishYearItem = $this->item->getDeepClone(); $publishYearItem->group = $groupData['key']; $publishYearItem->field->label = 'Publish year:'; $publishYearItem->field->key = Field::createKey( $groupData['key'], $groupData['fields'][1]['key'] ); $view->items[] = $publishYearItem; $authorItem = $this->item->getDeepClone(); $authorItem->group = $groupData['key']; $authorItem->field->label = 'Author:'; $authorItem->field->key = Field::createKey($groupData['key'], $groupData['fields'][2]['key']); $view->items[] = $authorItem; $view->cssCode = ".acf-view\t{\n" . "padding: 30px;\nborder: 1px solid #444;\nborder-radius: 12px;\ncolor: #444444;\n}\n" . ".acf-view__row\t{\n" . "display:flex;\n}\n" . ".acf-view__label\t{\nfont-weight: bold;\npadding-right: 10px;\n}\n"; $view->save(); $this->cpt->performSaveActions($this->viewId); } protected function createAcfView(): void { $postId = wp_insert_post([ 'post_type' => AcfViews::NAME, 'post_status' => 'publish', 'post_title' => 'Demo view', ]); if (is_wp_error($postId)) { $this->addError('Fail to create a post'); return; } $this->viewId = $postId; } protected function fillPost(array $groupData): void { update_field($groupData['fields'][0]['key'], 'Fahrenheit 451', $this->postId); update_field($groupData['fields'][1]['key'], '1953', $this->postId); update_field($groupData['fields'][2]['key'], 'Ray Bradbury', $this->postId); wp_update_post([ 'ID' => $this->postId, 'post_content' => '[acf_views view-id="' . $this->viewId . '"]', ]); } protected function saveIDsToOptions(): void { $this->options->updateOption(Options::DEMO_IMPORT, [ 'postId' => $this->postId, 'groupId' => $this->groupId, 'viewId' => $this->viewId, ]); } public function setHooks(): void { add_action('wp_loaded', function () { if (! isset($_POST['_av-demo-import'])) { return; } check_admin_referer('_av-demo-import'); $isImport = isset($_POST['_import']); $isDelete = isset($_POST['_delete']); ! $isImport || $this->import(); $isImport || ! $isDelete || $this->delete(); }); } public function readIDsFromOptions(): void { $option = $this->options->getOption(Options::DEMO_IMPORT); if (! is_array($option) || ! key_exists('postId', $option) || ! key_exists('groupId', $option) || ! key_exists('viewId', $option)) { return; } $this->postId = (int)$option['postId']; $this->groupId = (int)$option['groupId']; $this->viewId = (int)$option['viewId']; } public function import(): void { $this->isProcessed = true; $this->isImportRequest = true; $this->createPost(); $groupData = $this->importAcfGroup(); $this->isHasError() || $this->createAcfView(); $this->isHasError() || $this->fillAcfView($groupData); $this->isHasError() || $this->fillPost($groupData); $this->isHasError() || $this->saveIDsToOptions(); } public function delete(): void { $this->readIDsFromOptions(); if (! $this->isHasData()) { return; } $this->isProcessed = true; // force to bypass a trash wp_delete_post($this->postId, true); wp_delete_post($this->groupId, true); wp_delete_post($this->viewId, true); $this->options->deleteOption(Options::DEMO_IMPORT); $this->postId = 0; $this->groupId = 0; $this->viewId = 0; } public function isHasError(): bool { return ! ! $this->error; } public function getError(): string { return $this->error; } public function isProcessed(): bool { return $this->isProcessed; } public function isHasData(): bool { return ! ! $this->postId; } public function isImportRequest(): bool { return $this->isImportRequest; } public function getPostLink(): string { return get_the_permalink($this->postId); } public function getAcfGroupLink(): string { return get_edit_post_link($this->groupId); } public function getAcfViewLink(): string { return get_edit_post_link($this->viewId); } }