88use WPGraphQL \Acf \Model \AcfOptionsPage ;
99use WPGraphQL \AppContext ;
1010use WPGraphQL \Registry \TypeRegistry ;
11- use WPGraphQL \Utils \Utils ;
1211
1312class Registry {
1413
@@ -17,6 +16,18 @@ class Registry {
1716 */
1817 protected $ registered_fields = [];
1918
19+ /**
20+ * Stores location rules once they've been mapped
21+ *
22+ * @var array<mixed>
23+ */
24+ protected $ mapped_location_rules = [];
25+
26+ /**
27+ * @var array<mixed>
28+ */
29+ protected $ all_acf_field_groups = [];
30+
2031 /**
2132 * @todo should be protected with getter/setter?
2233 * @var array<mixed>
@@ -61,6 +72,15 @@ public function get_type_registry(): TypeRegistry {
6172 return $ this ->type_registry ;
6273 }
6374
75+ /**
76+ * Returns the mapped location rules
77+ *
78+ * @return array<mixed>
79+ */
80+ public function get_mapped_location_rules (): array {
81+ return $ this ->mapped_location_rules ;
82+ }
83+
6484 /**
6585 * @param string $key
6686 * @param mixed $field_group
@@ -82,7 +102,7 @@ public function has_registered_field_group( string $key ): bool {
82102 * @param array<mixed> $acf_field_group
83103 */
84104 public function should_field_group_show_in_graphql ( array $ acf_field_group ): bool {
85- return \ WPGraphQL \ Acf \ Utils::should_field_group_show_in_graphql ( $ acf_field_group );
105+ return Utils::should_field_group_show_in_graphql ( $ acf_field_group );
86106 }
87107
88108 /**
@@ -91,6 +111,11 @@ public function should_field_group_show_in_graphql( array $acf_field_group ): bo
91111 * @return array<mixed>
92112 */
93113 public function get_acf_field_groups (): array {
114+
115+ if ( ! empty ( $ this ->all_acf_field_groups ) ) {
116+ return $ this ->all_acf_field_groups ;
117+ }
118+
94119 $ all_acf_field_groups = acf_get_field_groups ();
95120
96121 $ graphql_field_groups = [];
@@ -105,7 +130,8 @@ public function get_acf_field_groups(): array {
105130 $ graphql_field_groups [ $ acf_field_group ['key ' ] ] = $ acf_field_group ;
106131 }
107132
108- return $ graphql_field_groups ;
133+ $ this ->all_acf_field_groups = $ graphql_field_groups ;
134+ return $ this ->all_acf_field_groups ;
109135 }
110136
111137 /**
@@ -286,7 +312,8 @@ public function get_field_group_interfaces( array $acf_field_group ): array {
286312 * @throws \Exception
287313 */
288314 public function register_options_pages (): void {
289- $ graphql_options_pages = \WPGraphQL \Acf \Utils::get_acf_options_pages ();
315+
316+ $ graphql_options_pages = Utils::get_acf_options_pages ();
290317
291318 if ( empty ( $ graphql_options_pages ) ) {
292319 return ;
@@ -342,7 +369,7 @@ public function register_options_pages(): void {
342369 ]
343370 );
344371
345- $ field_name = Utils::format_field_name ( $ type_name );
372+ $ field_name = \ WPGraphQL \ Utils \ Utils::format_field_name ( $ type_name );
346373
347374 $ interface_name = 'WithAcfOptionsPage ' . $ type_name ;
348375
@@ -499,7 +526,7 @@ public function map_acf_field_to_graphql( array $acf_field, array $acf_field_gro
499526 * @throws \GraphQL\Error\Error
500527 */
501528 public function get_field_group_name ( array $ field_group ): string {
502- return \ WPGraphQL \ Acf \ Utils::get_field_group_name ( $ field_group );
529+ return Utils::get_field_group_name ( $ field_group );
503530 }
504531
505532 /**
@@ -508,7 +535,7 @@ public function get_field_group_name( array $field_group ): string {
508535 * @throws \GraphQL\Error\Error
509536 */
510537 public function get_graphql_field_name ( array $ acf_field ): string {
511- return Utils::format_field_name ( $ this ->get_field_group_name ( $ acf_field ), true );
538+ return \ WPGraphQL \ Utils \ Utils::format_field_name ( $ this ->get_field_group_name ( $ acf_field ), true );
512539 }
513540
514541 /**
@@ -540,7 +567,7 @@ public function get_field_group_graphql_type_name( array $field_group ): ?string
540567 return null ;
541568 }
542569
543- return Utils::format_type_name ( $ replaced );
570+ return \ WPGraphQL \ Utils \ Utils::format_type_name ( $ replaced );
544571 }
545572
546573 /**
@@ -551,6 +578,11 @@ public function get_field_group_graphql_type_name( array $field_group ): ?string
551578 * @return array<mixed>
552579 */
553580 public function get_location_rules ( array $ acf_field_groups = [] ): array {
581+
582+ if ( ! empty ( $ this ->get_mapped_location_rules () ) ) {
583+ return $ this ->get_mapped_location_rules ();
584+ }
585+
554586 $ field_groups = $ acf_field_groups ;
555587 $ rules = [];
556588
@@ -571,7 +603,36 @@ public function get_location_rules( array $acf_field_groups = [] ): array {
571603 $ rules = new LocationRules ();
572604 $ rules ->determine_location_rules ();
573605
574- return $ rules ->get_rules ();
606+ // Set the mapped location rules for future use
607+ $ rules_by_group = $ rules ->get_rules ();
608+
609+ $ this ->mapped_location_rules = $ rules_by_group ;
610+
611+ // Return them
612+ return $ this ->mapped_location_rules ;
613+ }
614+
615+ /**
616+ * Get the location rules grouped by location instead of grouped by field group
617+ *
618+ * @return array<mixed>
619+ */
620+ public function get_location_rules_grouped_by_location (): array {
621+ $ location_rules = $ this ->get_location_rules ();
622+ $ rules_by_location = [];
623+ if ( ! empty ( $ location_rules ) ) {
624+ foreach ( $ location_rules as $ group => $ locations ) {
625+ if ( ! empty ( $ locations ) ) {
626+ foreach ( $ locations as $ location ) {
627+ if ( ! array_key_exists ( $ location , $ rules_by_location ) ) {
628+ $ rules_by_location [ $ location ] = [];
629+ }
630+ $ rules_by_location [ $ location ][] = $ group ;
631+ }
632+ }
633+ }
634+ }
635+ return $ rules_by_location ;
575636 }
576637
577638 /**
@@ -583,37 +644,31 @@ public function get_location_rules( array $acf_field_groups = [] ): array {
583644 * @return array<mixed>
584645 */
585646 public function get_graphql_locations_for_field_group ( array $ field_group , array $ acf_field_groups ): array {
586- if ( ! $ this ->should_field_group_show_in_graphql ( $ field_group ) ) {
587- return [];
588- }
589647
590- $ graphql_types = $ field_group [ ' graphql_types ' ] ?? [];
648+ $ graphql_types = [];
591649
592- $ field_group_name = '' ;
650+ if ( ! $ this ->should_field_group_show_in_graphql ( $ field_group ) ) {
651+ return $ graphql_types ;
652+ }
593653
594- if ( ! empty ( $ field_group ['graphql_field_name ' ] ) ) {
595- $ field_group_name = $ field_group ['graphql_field_name ' ];
596- } elseif ( ! empty ( $ field_group ['title ' ] ) ) {
597- $ field_group_name = $ field_group ['title ' ];
598- } elseif ( ! empty ( $ field_group ['name ' ] ) ) {
599- $ field_group_name = $ field_group ['name ' ];
654+ if ( ! empty ( $ field_group ['graphql_types ' ] ) && is_array ( $ field_group ['graphql_types ' ] ) ) {
655+ return $ field_group ['graphql_types ' ];
600656 }
601657
602- if ( empty ( $ field_group_name ) ) {
603- return [] ;
658+ if ( empty ( $ field_group [ ' location ' ] ) ) {
659+ return $ graphql_types ;
604660 }
605661
606- $ field_group_name = Utils::format_field_name ( $ field_group_name , true );
662+ $ field_group_name = Utils::get_field_group_name ( $ field_group );
663+ $ field_group_name = \WPGraphQL \Utils \Utils::format_field_name ( $ field_group_name , true );
664+ // The fields are mapped as lowercase strings and should be retrieved as such
665+ // see: LocationRules.php
666+ $ field_group_name = strtolower ( $ field_group_name );
607667
608- $ manually_set_graphql_types = isset ( $ field_group [ ' map_graphql_types_from_location_rules ' ] ) && ( bool ) $ field_group [ ' map_graphql_types_from_location_rules ' ] ;
668+ $ location_rules = $ this -> get_location_rules ( $ acf_field_groups ) ;
609669
610- if ( false === $ manually_set_graphql_types || empty ( $ graphql_types ) ) {
611- if ( empty ( $ field_group ['graphql_types ' ] ) ) {
612- $ location_rules = $ this ->get_location_rules ( $ acf_field_groups );
613- if ( isset ( $ location_rules [ $ field_group_name ] ) ) {
614- $ graphql_types = $ location_rules [ $ field_group_name ];
615- }
616- }
670+ if ( isset ( $ location_rules [ $ field_group_name ] ) ) {
671+ $ graphql_types = $ location_rules [ $ field_group_name ];
617672 }
618673
619674 return ! empty ( $ graphql_types ) && is_array ( $ graphql_types ) ? array_unique ( array_filter ( $ graphql_types ) ) : [];
@@ -648,7 +703,7 @@ public function register_acf_field_groups_to_graphql( array $acf_field_groups =
648703 if ( ! empty ( $ locations ) ) {
649704 $ with_field_group_interface_name = 'WithAcf ' . $ type_name ;
650705
651- $ field_name = Utils::format_field_name ( $ type_name , true );
706+ $ field_name = \ WPGraphQL \ Utils \ Utils::format_field_name ( $ type_name , true );
652707
653708 if ( ! $ this ->has_registered_field_group ( $ with_field_group_interface_name ) ) {
654709 register_graphql_interface_type (
@@ -680,7 +735,7 @@ public function register_acf_field_groups_to_graphql( array $acf_field_groups =
680735 }
681736
682737 // If the field group has locations defined (Types to be added to)
683- // Add the
738+ // Add the WithAcf$FieldGroup Interface to the corresponding graphql_types
684739 register_graphql_interfaces_to_types ( [ $ with_field_group_interface_name ], $ locations );
685740 }
686741
0 commit comments