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

{$error}

"; } protected function getAcfView(Post $dataPost, int $viewId, int $pageId): AcfView { $viewGroup = $this->acfViewData->getDeepClone(); $viewGroup->load($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' => AcfViewData::getAcfFieldName(AcfViewData::FIELD_MARKUP), 'isReadOnly' => true, 'mode' => 'htmlmixed', ], [ 'idSelector' => AcfViewData::getAcfFieldName(AcfViewData::FIELD_CSS_CODE), 'isReadOnly' => false, 'mode' => 'css', ], [ 'idSelector' => AcfViewData::getAcfFieldName(AcfViewData::FIELD_JS_CODE), 'isReadOnly' => false, 'mode' => 'javascript', ], [ 'idSelector' => AcfCardData::getAcfFieldName(AcfCardData::FIELD_CSS_CODE), 'isReadOnly' => false, 'mode' => 'css', ], [ 'idSelector' => AcfCardData::getAcfFieldName(AcfCardData::FIELD_JS_CODE), 'isReadOnly' => false, 'mode' => 'javascript', ], [ 'idSelector' => AcfCardData::getAcfFieldName(AcfCardData::FIELD_QUERY_PREVIEW), 'isReadOnly' => true, 'mode' => 'javascript', ], ], 'fieldSelect' => [ [ 'mainSelectId' => Item::getAcfFieldName(Item::FIELD_GROUP), 'subSelectId' => Field::getAcfFieldName(Field::FIELD_KEY), 'identifierInputId' => Field::getAcfFieldName(Field::FIELD_ID), ], [ 'mainSelectId' => AcfCardData::getAcfFieldName(AcfCardData::FIELD_ORDER_BY_META_FIELD_GROUP), 'subSelectId' => AcfCardData::getAcfFieldName(AcfCardData::FIELD_ORDER_BY_META_FIELD_KEY), 'identifierInputId' => '', ], ] ]); wp_enqueue_style(AcfViews::NAME, $this->getAssetsUrl('admin.css'), [], $this->getVersion()); // jquery is necessary for select2 events wp_enqueue_script(AcfViews::NAME, $this->getAssetsUrl('admin.js'), ['jquery',], $this->getVersion()); wp_localize_script(AcfViews::NAME, 'acf_views', $jsData); } public function getName(): string { return $this->name; } public function getSlug(): string { return $this->slug; } public function getVersion(): string { // return strval(time()); 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']); add_shortcode(Plugin::SHORTCODE_CARDS, [$this, 'acfCardsShortcode']); add_action('wp_footer', [$this, 'printCustomAssets']); } 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 acfCardsShortcode($attrs): string { if (! function_exists('get_post') || ! function_exists('get_queried_object_id')) { return ''; } $cardId = (int)($attrs['card-id'] ?? 0); $acfCardPost = $cardId ? get_post($cardId) : null; $shortCodeName = ':' . self::SHORTCODE_CARDS . ':'; if (! $acfCardPost || ! in_array($acfCardPost->post_type, [AcfCards::NAME,], true) || 'publish' !== $acfCardPost->post_status ) { return self::getErrorMarkup($shortCodeName . ' card-id attribute is missing or wrong'); } $acfCardData = $this->acfCardData->getDeepClone(); $acfCardData->load($cardId); return $this->cardMarkup->getMarkup($acfCardData, get_queried_object_id()); } 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 || ! in_array($acfViewPost->post_type, [AcfViews::NAME,], true) || '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, [AcfViews::NAME, AcfCards::NAME,], true) || in_array($currentScreen->post_type, [AcfViews::NAME, AcfCards::NAME], true))) { $this->enqueueAdminAssets(); } } public function printCustomAssets(): void { $code = ''; $views = $this->viewMarkup->getRenderedViews(); foreach ($views as $view) { $cssCode = str_replace(["\t", "\n", "\r"], '', $view->cssCode); $cssCode = str_replace('#view ', sprintf('.acf-view--id--%s ', $view->getSource()), $cssCode); $cssCode = trim($cssCode); $jsCode = str_replace(["\t", "\n", "\r"], '', $view->jsCode); $jsCode = trim($jsCode); // no escaping, it's a CSS code, so e.g '.a > .b' shouldn't be escaped $code .= $cssCode ? sprintf("", $view->getSource(), $cssCode) : ''; $code .= $jsCode ? sprintf("", $view->getSource(), $jsCode) : ''; } $cards = $this->cardMarkup->getRenderedCards(); foreach ($cards as $card) { $cssCode = str_replace(["\t", "\n", "\r"], '', $card->cssCode); $cssCode = str_replace('#card ', sprintf('.acf-card--id--%s ', $card->getSource()), $cssCode); $cssCode = trim($cssCode); $jsCode = str_replace(["\t", "\n", "\r"], '', $card->jsCode); $jsCode = trim($jsCode); // no escaping, it's a CSS code, so e.g '.a > .b' shouldn't be escaped $code .= $cssCode ? sprintf("", $card->getSource(), $cssCode) : ''; $code .= $jsCode ? sprintf("", $card->getSource(), $jsCode) : ''; } echo $code; } }