acfViewFactory = $acfViewFactory; $this->acfCardFactory = $acfCardFactory; $this->cache = $cache; $this->settings = $settings; $this->displayingView = []; // don't use '0' as the default, because it can be 0 in the 'render_callback' hook $this->queryLoopPostId = -1; } protected function getErrorMarkup(string $shortcode, array $args, string $error): string { $attrs = []; foreach ($args as $name => $value) { $attrs[] = sprintf('%s="%s"', $name, $value); } return sprintf( "
%s %s %s
", __('Shortcode error:', 'acf-views'), $error, sprintf('(%s %s)', $shortcode, implode(' ', $attrs)) ); } protected 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; } /** * block theme: skip execution for the Gutenberg common call, as query-loop may be used, and the post id won't be available yet * Exceptions: * 1. if the object-id is set, e.g. as part of the Card shortcode * 2. If mount-point is set, e.g. as part of the MountPoint functionality */ protected function maybeSkipShortcode(string $objectId, array $attrs): string { $isMountPoint = isset($attrs['mount-point']); if (!wp_is_block_theme() || -1 !== $this->queryLoopPostId || $objectId || $isMountPoint) { return ''; } $stringAttrs = array_map( function ($key, $value) { return sprintf('%s="%s"', $key, $value); }, array_keys($attrs), array_values($attrs) ); return sprintf('[%s %s]', self::SHORTCODE_VIEWS, implode(' ', $stringAttrs)); } protected function getDataPostId(string $objectId, int $currentPageId, int $userId): string { if (in_array($objectId, ['$user$', 'options',], true)) { return '$user$' === $objectId ? 'user_' . $userId : $objectId; } global $post; // a. dataPostId from the shortcode argument $dataPostId = (int)$objectId; // b. from the Gutenberg query loop if (!in_array($this->queryLoopPostId, [-1, 0,], true)) { $dataPostId = $dataPostId ?: $this->queryLoopPostId; } // c. dataPostId from the current loop (WordPress posts, WooCommerce products...) $dataPostId = $dataPostId ?: ($post->ID ?? 0); // d. dataPostId from the current page $dataPostId = $dataPostId ?: $currentPageId; // validate the ID return (string)(get_post($dataPostId) ? $dataPostId : 0); } protected function maybeAddQuickLink(string $html, int $postId): string { $roles = wp_get_current_user()->roles; if (!$this->settings->isDevMode() || !in_array('administrator', $roles, true)) { return $html; } preg_match('/^