acfViews = $acfViews; $this->options = $options; $this->pageId = 0; $this->groupId = 0; $this->bookViewId = 0; $this->postViewId = 0; $this->postCardId = 0; $this->error = ''; $this->isProcessed = false; $this->isImportRequest = false; $this->acfViewData = $acfViewData->getDeepClone(); $this->item = $item->getDeepClone(); $this->acfCardData = $acfCardData->getDeepClone(); $this->acfCards = $acfCards; } protected function addError(string $error): void { $this->error .= $error; } protected function createPage(): void { $pageId = wp_insert_post([ 'post_type' => 'page', 'post_status' => 'draft', 'post_title' => 'ACF Views Demo Page', ]); if (is_wp_error($pageId)) { $this->addError('Fail to create a post'); return; } $this->pageId = $pageId; } protected function createAcfViews(): void { $bookViewId = wp_insert_post([ 'post_type' => AcfViews::NAME, 'post_status' => 'publish', 'post_title' => 'Book Demo View', ]); $postViewId = wp_insert_post([ 'post_type' => AcfViews::NAME, 'post_status' => 'publish', 'post_title' => 'Post Demo View', ]); if (is_wp_error($bookViewId) || is_wp_error($postViewId)) { $this->addError('Fail to create a post'); return; } $this->bookViewId = $bookViewId; $this->postViewId = $postViewId; } protected function createAcfCard(): void { $cardId = wp_insert_post([ 'post_type' => AcfCards::NAME, 'post_status' => 'publish', 'post_title' => 'Post Demo Card', ]); if (is_wp_error($cardId)) { $this->addError('Fail to create a post'); return; } $this->postCardId = $cardId; } protected function importAcfGroup(): array { $groupJSON = DemoGroup::getGroupInfo(); $groupJSON['title'] = 'ACF Views "Book" Demo Group'; $groupJSON['location'][0][0]['value'] = $this->pageId; 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 fillBookAcfView(array $groupData): void { $view = $this->acfViewData->getDeepClone(); $view->load($this->bookViewId); $view->description = 'It\'s a demo ACF View to display fields the demo "Book" ACF Field Group'; $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 = "#view {\n padding: 30px;\n color: #444444;\n}\n\n" . "#view .acf-view__row {\n display:flex;\n}\n\n" . "#view .acf-view__label {\n font-weight: bold;\n padding-right: 10px;\n}\n\n"; $view->save(); $this->acfViews->performSaveActions($this->bookViewId); } protected function fillPostAcfView(): void { $view = $this->acfViewData->getDeepClone(); $view->load($this->postViewId); $view->description = 'It\'s a demo ACF View to display a "Post" object'; $featuredImage = $this->item->getDeepClone(); $featuredImage->group = Acf::GROUP_POST; $featuredImage->field->key = Field::createKey(Acf::GROUP_POST, Post::FIELD_THUMBNAIL); $view->items[] = $featuredImage; $titleLink = $this->item->getDeepClone(); $titleLink->group = Acf::GROUP_POST; $titleLink->field->key = Field::createKey(Acf::GROUP_POST, Post::FIELD_TITLE_LINK); $view->items[] = $titleLink; $excerpt = $this->item->getDeepClone(); $excerpt->group = Acf::GROUP_POST; $excerpt->field->key = Field::createKey(Acf::GROUP_POST, Post::FIELD_EXCERPT); $view->items[] = $excerpt; $date = $this->item->getDeepClone(); $date->group = Acf::GROUP_POST; $date->field->key = Field::createKey(Acf::GROUP_POST, Post::FIELD_DATE); $view->items[] = $date; $view->cssCode = "#view .acf-card__items {\n display:flex;\n flex-direction:column;\n}\n\n" . "#view ._thumbnail_id img {\n width:100%;\n height:300px;\n object-fit:cover;\n}\n\n" . "#view ._post_title_link a {\n display:block;\n margin:10px 0;\n color:#008BB7;\n}\n\n" . "#view ._post_date {\n margin:10px 0 0;\n font-weight:bold;\n}\n\n"; $view->save(); $this->acfViews->performSaveActions($this->postViewId); } protected function fillPostAcfCard(): void { $acfCard = $this->acfCardData->getDeepClone(); $acfCard->load($this->postCardId); $acfCard->description = 'It\'s a demo ACF Card to display 3 latest posts'; $acfCard->acfViewId = $this->postViewId; $acfCard->limit = 3; $acfCard->postTypes[] = 'post'; $acfCard->order = 'DESC'; $acfCard->orderBy = 'date'; $acfCard->cssCode = "#card .acf-card__items {\n display:flex;\n}\n\n" . "#card .acf-view {\n flex-basis:33%;\n flex-shrink:0;\n padding:10px 20px;\n}\n\n"; $acfCard->save(); $this->acfCards->performSaveActions($this->postCardId); } protected function fillPage(array $groupData): void { update_field($groupData['fields'][0]['key'], 'Fahrenheit 451', $this->pageId); update_field($groupData['fields'][1]['key'], '1953', $this->pageId); update_field($groupData['fields'][2]['key'], 'Ray Bradbury', $this->pageId); $postContent = '

"Book" ACF View

'; $postContent .= '[acf_views view-id="' . $this->bookViewId . '"]'; $postContent .= '

"Posts" ACF Card

'; $postContent .= '[acf_cards card-id="' . $this->postCardId . '"]'; wp_update_post([ 'ID' => $this->pageId, 'post_content' => $postContent, ]); } protected function saveIDsToOptions(): void { $this->options->updateOption(Options::DEMO_IMPORT, [ 'pageId' => $this->pageId, 'groupId' => $this->groupId, 'bookViewId' => $this->bookViewId, 'postViewId' => $this->postViewId, 'postCardId' => $this->postCardId, ]); } 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('pageId', $option) || ! key_exists('groupId', $option) || ! key_exists('bookViewId', $option) || ! key_exists('postViewId', $option) || ! key_exists('postCardId', $option)) { return; } $this->pageId = (int)$option['pageId']; $this->groupId = (int)$option['groupId']; $this->bookViewId = (int)$option['bookViewId']; $this->postViewId = (int)$option['postViewId']; $this->postCardId = (int)$option['postCardId']; } public function import(): void { $this->isProcessed = true; $this->isImportRequest = true; // page should be created first of all $this->createPage(); $groupData = $this->importAcfGroup(); $this->isHasError() || $this->createAcfViews(); $this->isHasError() || $this->createAcfCard(); $this->isHasError() || $this->fillBookAcfView($groupData); $this->isHasError() || $this->fillPostAcfView(); $this->isHasError() || $this->fillPostAcfCard(); $this->isHasError() || $this->fillPage($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->pageId, true); wp_delete_post($this->groupId, true); wp_delete_post($this->bookViewId, true); wp_delete_post($this->postViewId, true); wp_delete_post($this->postCardId, true); $this->options->deleteOption(Options::DEMO_IMPORT); $this->pageId = 0; $this->groupId = 0; $this->bookViewId = 0; $this->postViewId = 0; $this->postCardId = 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->pageId; } public function isImportRequest(): bool { return $this->isImportRequest; } public function getPageLink(): string { return (string)get_the_permalink($this->pageId); } public function getAcfGroupLink(): string { return (string)get_edit_post_link($this->groupId); } public function getBookAcfViewLink(): string { return (string)get_edit_post_link($this->bookViewId); } public function getPostAcfViewLink(): string { return (string)get_edit_post_link($this->postViewId); } public function getPostAcfCardLink(): string { return (string)get_edit_post_link($this->postCardId); } }