acf = $acf; $this->viewMarkup = $viewMarkup; } protected static function getErrorMarkup(string $error): string { return "

{$error}

"; } protected function getAcfView(Post $dataPost, int $viewId, int $pageId): AcfView { $viewGroup = new View($viewId); $viewMarkup = $this->viewMarkup->getMarkup($viewGroup, $pageId); return new AcfView($this->acf, $viewGroup, $dataPost, $pageId, $viewMarkup); } protected function enqueueAdminAssets(array $jsData = []): void { $jsData = array_merge_recursive($jsData, [ 'markupTextarea' => [ [ 'idSelector' => View::getAcfFieldName(View::FIELD_MARKUP), 'isReadOnly' => true, 'mode' => 'htmlmixed', ], [ 'idSelector' => View::getAcfFieldName(View::FIELD_CSS_CODE), 'isReadOnly' => false, 'mode' => 'css', ], [ 'idSelector' => View::getAcfFieldName(View::FIELD_JS_CODE), 'isReadOnly' => false, 'mode' => 'javascript', ], ], 'fieldSelect' => [ [ 'mainSelectId' => Item::getAcfFieldName(Item::FIELD_GROUP), 'subSelectId' => Field::getAcfFieldName(Field::FIELD_KEY), 'identifierInputId' => Field::getAcfFieldName(Field::FIELD_ID), ], ] ]); $version = $this->getVersion(); // $version = time(); wp_enqueue_style(CPT::NAME, $this->getAssetsUrl('admin.css'), [], $version); // jquery is necessary for select2 events wp_enqueue_script(CPT::NAME, $this->getAssetsUrl('admin.js'), ['jquery',], $version); wp_localize_script(CPT::NAME, 'acf_views', $jsData); } public function getName(): string { return $this->name; } public function getSlug(): string { return $this->slug; } public function getVersion(): string { return $this->version; } public function getAssetsUrl(string $file): string { return plugin_dir_url(__FILE__) . 'assets/' . $file; } public function setHooks(): void { add_action('admin_notices', [$this, 'showWarningAboutInactiveAcfPlugin']); add_action('admin_enqueue_scripts', [$this, 'enqueueAdminScripts']); add_shortcode(Plugin::SHORTCODE, [$this, 'acfViewsShortcode']); } public function showWarningAboutInactiveAcfPlugin(): void { if (is_plugin_active('advanced-custom-fields-pro/acf.php')) { return; } echo '
' . '

ACF Views plugin requires an active Advanced Custom Fields PRO plugin.

' . '
'; } public function acfViewsShortcode($attrs): string { if (! function_exists('get_post') || ! function_exists('get_queried_object_id')) { return ''; } $viewId = (int)($attrs['view-id'] ?? 0); $acfViewPost = $viewId ? get_post($viewId) : null; $shortCodeName = ':' . self::SHORTCODE . ':'; if (! $acfViewPost || CPT::NAME !== $acfViewPost->post_type || 'publish' !== $acfViewPost->post_status ) { return self::getErrorMarkup($shortCodeName . ' view-id attribute is missing or wrong'); } $dataPostId = (int)($attrs['object-id'] ?? 0); $dataPostId = $dataPostId ?: get_queried_object_id(); $dataPostId = get_post($dataPostId) ? $dataPostId : 0; if (! $dataPostId) { return self::getErrorMarkup($shortCodeName . ' object id is not a post'); } $acfView = $this->getAcfView(new Post($dataPostId), $viewId, get_queried_object_id()); $acfView->insertFields(); return $acfView->getHTML(); } public function enqueueAdminScripts(): void { $currentScreen = get_current_screen(); if ($currentScreen && (in_array($currentScreen->id, [CPT::NAME,], true) || in_array($currentScreen->post_type, [CPT::NAME], true))) { $this->enqueueAdminAssets(); } } }