@@ -24,6 +24,7 @@ class FieldGroupCache implements Registerable
2424
2525 private const TRANSIENT_KEY = '_ac_acf_field_counts ' ;
2626 private const TRANSIENT_KEY_TABLE_SCREENS = '_ac_acf_group_table_screens ' ;
27+ private const TRANSIENT_KEY_META_KEYS_BY_TYPE = '_ac_acf_meta_keys_by_type ' ;
2728 private const TTL_SECONDS = WEEK_IN_SECONDS ;
2829
2930 private QueryFactory $ query_factory ;
@@ -150,11 +151,80 @@ public function get_count_for_table_screen(TableScreen $table_screen): int
150151 return $ this ->get_count_for_query ((string )$ table_screen ->get_id (), $ query );
151152 }
152153
154+ /**
155+ * @return array<string, string[]> field type => list of meta_keys (field names)
156+ */
157+ public function get_meta_keys_grouped_by_type (): array
158+ {
159+ $ cached = get_transient (self ::TRANSIENT_KEY_META_KEYS_BY_TYPE );
160+
161+ if (is_array ($ cached )) {
162+ return $ cached ;
163+ }
164+
165+ if ( ! $ this ->is_acf_available ()) {
166+ return [];
167+ }
168+
169+ $ result = [];
170+ $ seen_groups = [];
171+
172+ foreach ($ this ->table_ids_factory ->create () as $ table_id ) {
173+ if ( ! $ this ->table_screen_factory ->can_create ($ table_id )) {
174+ continue ;
175+ }
176+
177+ $ table_screen = $ this ->table_screen_factory ->create ($ table_id );
178+ $ query = $ this ->query_factory ->create ($ table_screen );
179+
180+ if ( ! $ query ) {
181+ continue ;
182+ }
183+
184+ foreach ($ query ->get_groups () as $ group ) {
185+ $ group_key = (string )($ group ['key ' ] ?? '' );
186+
187+ if ('' === $ group_key || isset ($ seen_groups [$ group_key ])) {
188+ continue ;
189+ }
190+
191+ $ seen_groups [$ group_key ] = true ;
192+
193+ $ fields = acf_get_fields ($ group_key );
194+
195+ if ( ! is_array ($ fields )) {
196+ continue ;
197+ }
198+
199+ foreach ($ fields as $ field ) {
200+ if ( ! is_array ($ field ) || empty ($ field ['name ' ]) || empty ($ field ['type ' ])) {
201+ continue ;
202+ }
203+
204+ $ type = (string )$ field ['type ' ];
205+ $ name = (string )$ field ['name ' ];
206+
207+ if ( ! isset ($ result [$ type ])) {
208+ $ result [$ type ] = [];
209+ }
210+
211+ if ( ! in_array ($ name , $ result [$ type ], true )) {
212+ $ result [$ type ][] = $ name ;
213+ }
214+ }
215+ }
216+ }
217+
218+ set_transient (self ::TRANSIENT_KEY_META_KEYS_BY_TYPE , $ result , self ::TTL_SECONDS );
219+
220+ return $ result ;
221+ }
222+
153223 private function is_acf_available (): bool
154224 {
155225 return function_exists ('acf_get_field_groups ' )
156- && function_exists ('acf_get_fields ' )
157- && function_exists ('acf_get_store ' );
226+ && function_exists ('acf_get_fields ' )
227+ && function_exists ('acf_get_store ' );
158228 }
159229
160230 private function get_count_for_query (string $ cache_key , Query $ query ): int
@@ -185,6 +255,7 @@ public function invalidate(): void
185255 {
186256 delete_transient (self ::TRANSIENT_KEY );
187257 delete_transient (self ::TRANSIENT_KEY_TABLE_SCREENS );
258+ delete_transient (self ::TRANSIENT_KEY_META_KEYS_BY_TYPE );
188259 }
189260
190261 private function count_fields (Query $ query ): int
0 commit comments