Read more * @button_label Add Mount Point * @a-no-tab 1 * @a-order 2 * @a-pro The field must be not required or have default value! */ public array $mountPoints; // just define without any annotations, it'll be overwritten by children public bool $isMarkupWithDigitalId; public string $customMarkup; public string $markup; public string $cssCode; public string $jsCode; public bool $isWithoutWebComponent; public string $bemName; public string $cssClasses; /** * @a-deprecated This argument is used just to hide the field from the UI */ public string $uniqueId; /** * @a-deprecated This argument is used just to hide the field from the UI */ public string $title; /** * This field is used in the Git feature, so old items can be updated while importing * @a-deprecated This argument is used just to hide the field from the UI */ public string $pluginVersion; /** * @return string[] */ abstract protected function getUsedMetaGroupIds(): array; abstract public function getCssCode(bool $isMinify = true, bool $isPreview = false): string; /** * @return array */ abstract public function getMultilingualStrings(): array; protected function minifyCode(string $code): string { // remove all CSS comments $codeWithoutComments = preg_replace('|\/\*(.?)+\*\/|', '', $code); $code = null !== $codeWithoutComments ? $codeWithoutComments : $code; // remove unnecessary spaces $code = str_replace(["\t", "\n", "\r"], '', $code); // replace multiple spaces with one $codeWithoutExtraSpaces = preg_replace('|\s+|', ' ', $code); $code = null !== $codeWithoutExtraSpaces ? $codeWithoutExtraSpaces : $code; $code = str_replace(': ', ':', $code); $code = str_replace('; ', ';', $code); $code = str_replace(' {', '{', $code); $code = str_replace('{ ', '{', $code); $code = str_replace(' }', '}', $code); $code = str_replace('} ', '}', $code); return $code; } protected function getMinifiedCss(): string { return $this->minifyCode($this->cssCode); } protected function getMinifiedJs(): string { $jsCode = $this->minifyCode($this->jsCode); $jsCode = str_replace(' =', '=', $jsCode); $jsCode = str_replace('= ', '=', $jsCode); $jsCode = str_replace(' ?', '?', $jsCode); $jsCode = str_replace('? ', '?', $jsCode); return $jsCode; } /** * @param array $mlStrings * @return array */ protected function getMultilingualStringsFromCustomMarkup(array $mlStrings): array { $textDomains = []; // __("Some data") or __("Some data", "my-theme") preg_match_all( '/__\([ ]*["]([^"]+)["]([, ]+["]([^"]+)["])*[ ]*\)/', $this->customMarkup, $functionsWithDoubleQuotes, PREG_SET_ORDER ); // __('Some data') or __('Some data', 'my-theme') preg_match_all( "/__\([ ]*[']([^']+)[']([, ]+[']([^']+)['])*[ ]*\)/", $this->customMarkup, $functionsWithSingleQuotes, PREG_SET_ORDER ); // "Some data"|translate or "Some data"|translate("my-theme") preg_match_all( '/["]([^"]+)["]\|translate(\([ ]*["]([^"]+)["][ ]*\))*/', $this->customMarkup, $filtersWithDoubleQuotes, PREG_SET_ORDER ); // 'Some data'|translate or 'Some data'|translate('my-theme') preg_match_all( "/[']([^']+)[']\|translate(\([ ]*[']([^']+)['][ ]*\))*/", $this->customMarkup, $filtersWithSingleQuotes, PREG_SET_ORDER ); $functions = array_merge($functionsWithDoubleQuotes, $functionsWithSingleQuotes); $filters = array_merge($filtersWithDoubleQuotes, $filtersWithSingleQuotes); $matches = array_merge($functions, $filters); foreach ($matches as $match) { $label = $match[1] ?? ''; $textDomain = $match[3] ?? Plugin::getThemeTextDomain(); $mlStrings[$textDomain] = $mlStrings[$textDomain] ?? []; $mlStrings[$textDomain][] = $label; $textDomains[] = $textDomain; } $textDomains = array_unique($textDomains); foreach ($textDomains as $textDomain) { $mlStrings[$textDomain] = array_unique($mlStrings[$textDomain]); } return $mlStrings; } /** * @return array */ public function getCommonMountPoints(): array { $commonMountPoints = []; foreach ($this->mountPoints as $mountPoint) { // both into one array, as IDs and postTypes are different and can't be mixed up $commonMountPoints = array_merge($commonMountPoints, $mountPoint->postTypes); $commonMountPoints = array_merge($commonMountPoints, $mountPoint->posts); } return array_values(array_unique($commonMountPoints)); } /** * @return array */ public function getExposedPostFields(): array { return [ 'post_excerpt' => join(',', $this->getCommonMountPoints()), 'post_content_filtered' => join(',', $this->getUsedMetaGroupIds()), 'post_title' => $this->title, 'post_name' => $this->getUniqueId(), ]; } /** * @param array $postFields * @throws Exception */ public function saveToPostContent(array $postFields = [], bool $isSkipDefaults = false): bool { $postFields = $this->getExposedPostFields(); // skipDefaults. We won't need to save default values to the DB // @phpstan-ignore-next-line $result = parent::saveToPostContent($postFields, true); // we made a direct WP query, which means we need to clean the cache, // to make the changes available in the WP cache clean_post_cache((int)$this->getSource()); return $result; } /** * @param bool $isWithoutPrefix Set to true, when need short (abc3 in case of view_abc3) * @return string */ public function getUniqueId(bool $isWithoutPrefix = false): string { return !$isWithoutPrefix ? $this->uniqueId : explode('_', $this->uniqueId)[1] ?? ''; } public function getPostId(): int { return (int)$this->getSource(); } // safe for json-only items public function getEditPostLink(string $context = 'display'): string { $postId = $this->getPostId(); return 0 !== $postId ? (string)get_edit_post_link($postId, $context) : admin_url( sprintf( 'edit.php?post_type=acf_%ss&post_status=%s', $this->getType(), FsOnlyCptTableTab::NAME ) ); } public function getType(): string { return explode('_', $this->getUniqueId())[0]; } public function getMarkupId(): string { return !$this->isMarkupWithDigitalId ? $this->getUniqueId(true) : (string)$this->getSource(); } public function getJsCode(bool $isMinified = true): string { return $isMinified ? $this->getMinifiedJs() : $this->jsCode; } public function getTagName(string $prefix = ''): string { if ($this->isWithoutWebComponent) { return 'div'; } $bemName = '' !== $this->bemName ? str_replace('_', '-', $this->bemName) : ''; // WebComponents require at least one dash in the name return ('' !== $bemName && false !== strpos($bemName, '-')) ? $bemName : sprintf('%s-%s', $prefix, $this->getUniqueId(true)); } }