getGroupChoices(); return $field; } ); add_filter( 'acf/load_field/name=' . Field::getAcfFieldName(Field::FIELD_KEY), function (array $field) { $field['choices'] = $this->getFieldChoices(); return $field; } ); add_filter( 'acf/load_field/name=' . AcfCardData::getAcfFieldName(AcfCardData::FIELD_POST_TYPES), function (array $field) { $field['choices'] = $this->getPostTypeChoices(); return $field; } ); add_filter( 'acf/load_field/name=' . AcfCardData::getAcfFieldName(AcfCardData::FIELD_POST_STATUSES), function (array $field) { $field['choices'] = $this->getPostStatusChoices(); return $field; } ); add_filter( 'acf/load_field/name=' . AcfCardData::getAcfFieldName(AcfCardData::FIELD_ORDER_BY_META_FIELD_GROUP), function (array $field) { $field['choices'] = $this->getGroupChoices(false); return $field; } ); add_filter( 'acf/load_field/name=' . AcfCardData::getAcfFieldName(AcfCardData::FIELD_ORDER_BY_META_FIELD_KEY), function (array $field) { $field['choices'] = $this->getFieldChoices(false); return $field; } ); add_filter( 'acf/load_field/name=' . RepeaterField::getAcfFieldName(RepeaterField::FIELD_KEY), function (array $field) { $field['choices'] = $this->getRepeaterChoices(); return $field; } ); add_filter( 'acf/load_field/name=' . MetaField::getAcfFieldName(MetaField::FIELD_GROUP), function (array $field) { $field['choices'] = $this->getGroupChoices(false); return $field; } ); add_filter( 'acf/load_field/name=' . MetaField::getAcfFieldName(MetaField::FIELD_FIELD_KEY), function (array $field) { $field['choices'] = $this->getFieldChoices(false); return $field; } ); add_filter( 'acf/load_field/name=' . TaxField::getAcfFieldName(TaxField::FIELD_TAXONOMY), function (array $field) { $field['choices'] = $this->getTaxonomyChoices(); return $field; } ); add_filter( 'acf/load_field/name=' . TaxField::getAcfFieldName(TaxField::FIELD_TERM), function (array $field) { $field['choices'] = $this->getTermChoices(); return $field; } ); } // Important! Use this wrapper to avoid recursion public function getGroups(): array { if (! function_exists('acf_get_field_groups')) { return []; } $acfGroups = acf_get_field_groups(); // Important! To avoid recursion, otherwise within 'getChoices()' will be available the same group as the current // and this class will call 'acf_get_fields()' that will call 'getChoices()' $acfGroups = array_filter($acfGroups, function ($acfGroup) { $isPrivate = (bool)($acfGroup['private'] ?? false); $isOwn = 0 === strpos($acfGroup['key'], AcfGroup::GROUP_NAME_PREFIX); return (! $isPrivate && ! $isOwn); }); return array_values($acfGroups); } public function getGroupedFieldTypes(): array { return [ 'basic' => [ 'text', 'textarea', 'number', 'range', 'email', 'url', 'password', ], 'content' => [ 'image', 'file', 'wysiwyg', ], 'choice' => [ 'select', 'checkbox', 'radio', 'true_false', ], 'jquery' => [ 'date_picker', 'date_time_picker', 'time_picker', 'color_picker', ], 'relational' => [ 'link', 'page_link', ], ]; } public function getFieldTypes(): array { $fieldTypes = []; $groupedFieldTypes = $this->getGroupedFieldTypes(); foreach ($groupedFieldTypes as $group => $fields) { $fieldTypes = array_merge($fieldTypes, $fields); } return $fieldTypes; } public function getGroupChoices(bool $isWithPost = true): array { $groupChoices = [ '' => 'Select', ]; if ($isWithPost) { $groupChoices[self::GROUP_POST] = '$Post$'; } $groups = $this->getGroups(); foreach ($groups as $group) { $groupId = $group['key']; $groupChoices[$groupId] = $group['title']; } return $groupChoices; } public function getFieldChoices(bool $isWithPost = true): array { $fieldChoices = []; if (! function_exists('acf_get_fields')) { return $fieldChoices; } $fieldChoices = [ '' => 'Select', ]; if ($isWithPost) { $fieldChoices = array_merge($fieldChoices, [ Field::createKey(self::GROUP_POST, Post::FIELD_TITLE) => 'Title', Field::createKey(self::GROUP_POST, Post::FIELD_TITLE_LINK) => 'Title with link', Field::createKey(self::GROUP_POST, Post::FIELD_EXCERPT) => 'Excerpt', Field::createKey(self::GROUP_POST, Post::FIELD_THUMBNAIL) => 'Thumbnail', Field::createKey(self::GROUP_POST, Post::FIELD_AUTHOR) => 'Author', Field::createKey(self::GROUP_POST, Post::FIELD_DATE) => 'Published date', Field::createKey(self::GROUP_POST, Post::FIELD_MODIFIED) => 'Modified date', Field::createKey(self::GROUP_POST, Post::FIELD_LINK) => 'Link', ]); } $supportedFieldTypes = $this->getFieldTypes(); $groups = $this->getGroups(); foreach ($groups as $group) { $fields = acf_get_fields($group); foreach ($fields as $groupField) { if (! in_array($groupField['type'], $supportedFieldTypes, true)) { continue; } $fullFieldId = Field::createKey($group['key'], $groupField['key']); $fieldChoices[$fullFieldId] = $groupField['label'] . ' (' . $groupField['type'] . ')'; } } return $fieldChoices; } public function getPostTypeChoices(): array { return get_post_types(); } public function getPostStatusChoices(): array { return get_post_statuses(); } public function getRepeaterChoices(): array { $repeaterChoices = [ '' => 'Select', ]; $supportedFieldTypes = $this->getFieldTypes(); $groups = $this->getGroups(); foreach ($groups as $group) { $fields = acf_get_fields($group); foreach ($fields as $groupField) { $subFields = (array)($groupField['sub_fields'] ?? []); if ('repeater' !== $groupField['type'] || ! $subFields) { continue; } foreach ($subFields as $subField) { // repeater in repeater is not allowed if (! in_array($subField['type'], $supportedFieldTypes, true) || 'repeater' === $subField['type']) { continue; } $fullFieldId = Field::createKey( $group['key'], $groupField['key'], $subField['key'] ); $repeaterChoices[$fullFieldId] = $subField['label']; } } } return $repeaterChoices; } public function getTaxonomyChoices(): array { $taxChoices = [ '' => 'Select', ]; $taxonomies = get_taxonomies([], 'objects'); foreach ($taxonomies as $taxonomy) { $taxChoices[$taxonomy->name] = $taxonomy->label; } return $taxChoices; } public function getTermChoices(): array { $termChoices = [ '' => 'Select', ]; $taxonomyNames = get_taxonomies([]); foreach ($taxonomyNames as $taxonomyName) { $terms = get_terms([ 'taxonomy' => $taxonomyName, 'hide_empty' => false, ]); foreach ($terms as $term) { $fullTaxId = TaxField::createKey($taxonomyName, $term->term_id); $termChoices[$fullTaxId] = $term->name; } } return $termChoices; } }