@@ -48,6 +48,7 @@ public function init( \WPGraphQL\Registry\TypeRegistry $type_registry ) {
48
48
$ this ->add_acf_fields_to_individual_posts ();
49
49
$ this ->add_acf_fields_to_users ();
50
50
$ this ->add_acf_fields_to_options_pages ();
51
+ $ this ->add_acf_fields_to_pages ();
51
52
}
52
53
53
54
/**
@@ -1728,4 +1729,100 @@ protected function add_acf_fields_to_options_pages() {
1728
1729
}
1729
1730
}
1730
1731
1732
+ /**
1733
+ * Add field groups for `page_type` and `page_template`.
1734
+ * Note that these will show up for all pages in the schema,
1735
+ * they will not be limited to the page types or templates assigned in the field group.
1736
+ */
1737
+ protected function add_acf_fields_to_pages () {
1738
+
1739
+ $ page_field_groups = [];
1740
+
1741
+ /**
1742
+ * Get the field groups.
1743
+ *
1744
+ * TODO: Can we filter this to only page related ACF Field Groups?
1745
+ */
1746
+ $ field_groups = acf_get_field_groups ();
1747
+
1748
+ /**
1749
+ * We only want to add field groups for page type and template params.
1750
+ * Page by location is already covered by `add_acf_fields_to_individual_posts`,
1751
+ * so we do not want to duplicate that.
1752
+ */
1753
+ $ allowed_page_params = [
1754
+ 'page_type ' ,
1755
+ 'page_template ' ,
1756
+ ];
1757
+
1758
+ foreach ( $ field_groups as $ field_group ) {
1759
+ if ( ! empty ( $ field_group ['location ' ] ) && is_array ( $ field_group ['location ' ] ) ) {
1760
+ foreach ( $ field_group ['location ' ] as $ locations ) {
1761
+ if ( ! empty ( $ locations ) && is_array ( $ locations ) ) {
1762
+ foreach ( $ locations as $ location ) {
1763
+
1764
+ /**
1765
+ * If the operator is not equal to, we don't need to do anything,
1766
+ * so we can just continue
1767
+ */
1768
+ if ( '!= ' === $ location ['operator ' ] ) {
1769
+ continue ;
1770
+ }
1771
+
1772
+ /**
1773
+ * Add field group if the param is page related.
1774
+ */
1775
+ if ( in_array ( $ location ['param ' ], $ allowed_page_params , true ) && '== ' === $ location ['operator ' ] ) {
1776
+ $ page_field_groups [] = [
1777
+ 'type ' => $ location ['param ' ],
1778
+ 'field_group ' => $ field_group ,
1779
+ 'post_id ' => $ location ['value ' ]
1780
+ ];
1781
+ }
1782
+ }
1783
+ }
1784
+ }
1785
+ }
1786
+ }
1787
+
1788
+
1789
+ /**
1790
+ * If no field groups are assigned to pages, we don't need to modify the Schema.
1791
+ */
1792
+ if ( empty ( $ page_field_groups ) ) {
1793
+ return ;
1794
+ }
1795
+
1796
+ /**
1797
+ * Loop over the field groups assigned to pages
1798
+ * and register them to the Schema
1799
+ */
1800
+ foreach ( $ page_field_groups as $ key => $ group ) {
1801
+
1802
+ if ( empty ( $ group ['field_group ' ] ) || ! is_array ( $ group ['field_group ' ] ) ) {
1803
+ continue ;
1804
+ }
1805
+
1806
+ // Initialize our field group object.
1807
+ $ field_group = $ group ['field_group ' ];
1808
+
1809
+ $ page_type_object = get_post_type_object ('page ' );
1810
+
1811
+ $ field_name = isset ( $ field_group ['graphql_field_name ' ] ) ? $ field_group ['graphql_field_name ' ] : Config::camel_case ( $ field_group ['title ' ] );
1812
+ $ field_group ['type ' ] = 'group ' ;
1813
+ $ field_group ['name ' ] = $ field_name ;
1814
+ $ description = $ field_group ['description ' ] ? $ field_group ['description ' ] . ' | ' : '' ;
1815
+ $ config = [
1816
+ 'name ' => $ field_name ,
1817
+ 'description ' => $ description ,
1818
+ 'acf_field ' => $ field_group ,
1819
+ 'acf_field_group ' => null ,
1820
+ 'resolve ' => function ( $ root ) use ( $ field_group ) {
1821
+ return isset ( $ root ) ? $ root : null ;
1822
+ }
1823
+ ];
1824
+
1825
+ $ this ->register_graphql_field ($ page_type_object ->graphql_single_name , $ field_name , $ config );
1826
+ }
1827
+ }
1731
1828
}
0 commit comments