acf = $acf; $this->viewMarkup = $viewMarkup; $this->cardMarkup = $cardMarkup; $this->queryBuilder = $queryBuilder; $this->options = $options; $this->cache = $cache; } protected static function getErrorMarkup(string $error): string { return "
{$error}
"; } // static, as called also in AcfGroup public static function isAcfProPluginAvailable(): bool { return class_exists('acf_pro'); } protected function getAcfView(Post $dataPost, int $viewId, int $pageId): AcfView { $viewGroup = $this->cache->getAcfViewData($viewId); // don't use the 'AcfViewData->markup' field, as user can override it (and it shouldn't be supported) $viewMarkup = $this->viewMarkup->getMarkup($viewGroup, $pageId); return new AcfView($this->acf, $viewGroup, $dataPost, $pageId, $viewMarkup); } protected function getAcfCard(AcfCardData $acfCardData): AcfCard { return new AcfCard($acfCardData, $this->queryBuilder, $this->cardMarkup); } protected function getViewPreviewJsData(): array { $jsData = [ 'HTML' => '', 'CSS' => '', ]; global $post; if (! $this->isCPTScreen(AcfViews::NAME) || 'publish' !== $post->post_status) { return $jsData; } $acfViewData = $this->cache->getAcfViewData($post->ID); $previewPostId = $acfViewData->previewPost ?: 0; if ($previewPostId) { $acfView = $this->getAcfView(new Post($previewPostId), $post->ID, 0); $acfView->insertFields(); // without minify, it's a preview $viewHTML = $acfView->getHTML(false); } else { $viewHTML = $this->viewMarkup->getMarkup($acfViewData, 0); } // amend to allow work the '#view' alias $viewHTML = str_replace('class="acf-view ', 'id="view" class="acf-view ', $viewHTML); $jsData['HTML'] = htmlentities($viewHTML, ENT_QUOTES); $jsData['CSS'] = htmlentities($acfViewData->getCssCode(false, true), ENT_QUOTES); $jsData['HOME'] = get_site_url(); return $jsData; } protected function getCardPreviewJsData(): array { $jsData = [ 'HTML' => '', 'CSS' => '', ]; global $post; if (! $this->isCPTScreen(AcfCards::NAME) || 'publish' !== $post->post_status) { return $jsData; } $acfCardData = $this->cache->getAcfCardData($post->ID); $acfCard = $this->getAcfCard($acfCardData); $acfCard->queryPostsAndInsertData(1); $acfViewData = $this->cache->getAcfViewData($acfCardData->acfViewId); // amend to allow work the '#card' alias $viewHTML = str_replace( 'class="acf-card ', 'id="card" class="acf-card ', $acfCard->getHTML(false) ); $jsData['HTML'] = htmlentities($viewHTML, ENT_QUOTES); // Card CSS without minification as it's for views' purposes $jsData['CSS'] = htmlentities($acfCardData->getCssCode(false, true), ENT_QUOTES); $jsData['VIEW_CSS'] = htmlentities($acfViewData->getCssCode(), ENT_QUOTES); $jsData['HOME'] = get_site_url(); return $jsData; } 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' => AcfViewData::getAcfFieldName(AcfViewData::FIELD_CUSTOM_MARKUP), 'isReadOnly' => false, 'mode' => 'htmlmixed', ], [ 'idSelector' => AcfViewData::getAcfFieldName(AcfViewData::FIELD_PHP_VARIABLES), 'isReadOnly' => false, 'mode' => 'php', ], [ 'idSelector' => AcfCardData::getAcfFieldName(AcfCardData::FIELD_MARKUP), 'isReadOnly' => true, 'mode' => 'htmlmixed', ], [ '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_CUSTOM_MARKUP ), 'isReadOnly' => false, 'mode' => 'htmlmixed', ], [ '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' => '', ], [ 'mainSelectId' => Field::getAcfFieldName(Field::FIELD_KEY), 'subSelectId' => RepeaterField::getAcfFieldName(RepeaterField::FIELD_KEY), 'identifierInputId' => RepeaterField::getAcfFieldName(RepeaterField::FIELD_ID), ], [ 'mainSelectId' => MetaField::getAcfFieldName(MetaField::FIELD_GROUP), 'subSelectId' => MetaField::getAcfFieldName(MetaField::FIELD_FIELD_KEY), 'identifierInputId' => '', ], [ 'mainSelectId' => TaxField::getAcfFieldName(TaxField::FIELD_TAXONOMY), 'subSelectId' => TaxField::getAcfFieldName(TaxField::FIELD_TERM), 'identifierInputId' => '', ], ], 'viewPreview' => $this->getViewPreviewJsData(), 'cardPreview' => $this->getCardPreviewJsData(), ]); 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 isShortcodeAvailableForUser(array $userRoles, array $shortcodeArgs): bool { $userWithRoles = (string)($shortcodeArgs['user-with-roles'] ?? ''); $userWithRoles = trim($userWithRoles); $userWithRoles = $userWithRoles ? explode(',', $userWithRoles) : []; $userWithoutRoles = (string)($shortcodeArgs['user-without-roles'] ?? ''); $userWithoutRoles = trim($userWithoutRoles); $userWithoutRoles = $userWithoutRoles ? explode(',', $userWithoutRoles) : []; if (! $userWithRoles && ! $userWithoutRoles) { return true; } $userHasAllowedRoles = ! ! array_intersect($userWithRoles, $userRoles); $userHasDeniedRoles = ! ! array_intersect($userWithoutRoles, $userRoles); if (($userWithRoles && ! $userHasAllowedRoles) || ($userWithoutRoles && $userHasDeniedRoles)) { return false; } return true; } 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 isProVersion(): bool { return $this->isProVersion; } public function getAssetsUrl(string $file): string { return plugin_dir_url(__FILE__) . 'assets/' . $file; } public function getAcfProAssetsUrl(string $file): string { return plugin_dir_url(__FILE__) . 'AcfPro/' . $file; } public function isAcfPluginAvailable(bool $isProOnly = false): bool { // don't use 'is_plugin_active()' as the function available lately return static::isAcfProPluginAvailable() || (! $isProOnly && class_exists('ACF')); } public function setHooks(): void { add_action('admin_notices', [$this, 'showWarningAboutInactiveAcfPlugin']); add_action('admin_enqueue_scripts', [$this, 'enqueueAdminScripts']); add_action('wp_footer', [$this, 'printCustomAssets']); add_action('wp_footer', [$this, 'enqueueGoogleMapsJS']); add_action('activated_plugin', [$this, 'deactivateOtherInstances']); add_action('pre_current_active_plugins', [$this, 'showPluginDeactivatedNotice']); add_shortcode(Plugin::SHORTCODE, [$this, 'acfViewsShortcode']); add_shortcode(Plugin::SHORTCODE_CARDS, [$this, 'acfCardsShortcode']); add_filter('acf/prepare_field', [$this, 'amendProFieldLabelAndInstruction']); add_filter('acf/field_wrapper_attributes', [$this, 'addClassToAdminProFieldClasses'], 10, 2); add_filter('script_loader_tag', [$this, 'makeEnqueueJSAsync'], 10, 2); } public function showWarningAboutInactiveAcfPlugin(): void { if ($this->isAcfPluginAvailable()) { return; } $acfPluginInstallLink = get_admin_url(null, static::ACF_INSTALL_URL); $acfFree = 'https://wordpress.org/plugins/advanced-custom-fields/'; $acfPro = 'https://www.advancedcustomfields.com/pro/'; echo sprintf( '"ACF Views" plugin requires Advanced Custom Fields plugin to be enabled on your website (either Free or PRO version).' . ' Please enable "Advanced Custom Fields" plugin or "ACF Views" plugin won\'t work properly.
' . '%s
' . '