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; } ); } // 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(): array { $groupChoices = [ '' => 'Select', self::GROUP_POST => '$Post$', ]; $groups = $this->getGroups(); foreach ($groups as $group) { $groupId = $group['key']; $groupChoices[$groupId] = $group['title']; } return $groupChoices; } public function getFieldChoices(): array { $fieldChoices = []; if (! function_exists('acf_get_fields')) { return $fieldChoices; } $fieldChoices = [ '' => 'Select', Field::createKey(self::GROUP_POST, Post::FIELD_TITLE) => 'Title', 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', ]; $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(); } }