diff --git a/README.md b/README.md index c7392e0..da5a21f 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ WPGraphQL for Advanced Custom Fields automatically exposes your ACF fields to th - [Repeater](#repeater-field) - [Flexible Content](#flexible-content-field) - [Clone](#clone-field) +- [Options Pages](#options-pages) - [Location Rules](#location-rules) ## Install and Activate @@ -1733,6 +1734,45 @@ If we were to re-arrange the layouts, so that the order was "Layout Three", "Lay The clone field is not fully supported (yet). We plan to support it in the future. +## Options Pages + +**Reference**: https://www.advancedcustomfields.com/add-ons/options-page/ + +To add an option page and expose it to the graphql schema, simply add 'show_in_graphql' => true when you register an option page. + +**Example Usage** + +```php +function register_acf_options_pages() { + + // check function exists + if ( ! function_exists( 'acf_add_options_page' ) ) { + return; + } + + // register options page + $my_options_page = acf_add_options_page( + array( + 'page_title' => __( 'My Options Page' ), + 'menu_title' => __( 'My Options Page' ), + 'menu_slug' => 'my-options-page', + 'capability' => 'edit_posts', + 'show_in_graphql' => true, + ) + ); +} + +add_action( 'acf/init', 'register_acf_options_pages' ) +Example Query +query GetMyOptionsPage { + myOptionsPage { + someCustomField + } +} +``` + +Alternatively, it's you can check the Fields API Reference to learn about exposing your custom fields to the Schema. + ## Location Rules Advanced Custom Fields field groups are added to the WordPress dashboard by being assinged "Location Rules". diff --git a/readme.txt b/readme.txt index f9644bd..7e7458a 100644 --- a/readme.txt +++ b/readme.txt @@ -1,59 +1,59 @@ -=== WPGraphQL for Advanced Custom Fields === -Contributors: WPGraphQL, jasonbahl -Donate link: https://wpgraphql.com/acf -Tags: WPGraphQL, GraphQL, API, Advanced Custom Fields, ACF -Requires at least: 5.0 -Tested up to: 5.1.1 -Stable tag: 0.1.1 -License: GPL-3 -License URI: https://www.gnu.org/licenses/gpl-3.0.html - -WPGraphQL for Advanced Custom Fields exposes ACF Field Groups and Fields to the WPGraphQL Schema. - -== Description == - -WPGraphQL for Advanced Custom Fields exposes ACF Field Groups and Fields to the WPGraphQL Schema, -allowing for interacting with ACF field data using GraphQL Queries. - -== Changelog == - -= 0.1.7 = -* Add support for User Edit and User Register form locations - -= 0.1.6 = -* Add support for Single Post / Page location rule for ACF Field Groups -* Fix bug with WYSIWYG field nested within repeaters/flex fields not properly getting wpautop applied -* Fix bug where Relationship fields were throwing errors when an item in the relationship had been deleted. Now the items are not included in the response at all and no errors are thrown - -= 0.1.5 = -* Fix bug where field groups would disappear when using acf-local-json - -= 0.1.4 = -* Fixes bug with WYSIWYG fields nested in Flex Fields and Repeater Fields not properly resolving -* Adds support for assigning ACF Field Groups to attachments (MediaItems) - -= 0.1.3 = - -* Adds support for the following locations: Menus, Menu Items, Taxonomies, Comments -* Fixed a bug where the "show_in_graphql" button wasn't showing on Radio Buttons - -= 0.1.2 = -* Fixes bug with Nested Fields not properly showing in the Schema. By defualt, fields are not supposed -to be exposed in the Schema if they are not set to "show_in_graphql", however there was a flaw in -logic causing nested fields of Flex Field layouts to not properly be exposed to the Schema. This -fixes that issue, so nested fields of Flex Field layouts can properly be queried and seen in the -Schema. - -= 0.1.1 = -* Fixes bug with Field groups not properly being exposed to the Schema for custom post types. - -= 0.1.0 = -* Initial public release. - - -== Upgrade Notice == - -= 0.1.1 = -ACF Field groups were not properly being added to the GraphQL Schema for Custom Post Types. This -addresses that issue, so now Field groups that are set to "show_in_graphql" and are assigned to a -Custom Post Type that's also set to "show_in_graphql" will now be present in the Schema. +=== WPGraphQL for Advanced Custom Fields === +Contributors: WPGraphQL, jasonbahl +Donate link: https://wpgraphql.com/acf +Tags: WPGraphQL, GraphQL, API, Advanced Custom Fields, ACF +Requires at least: 5.0 +Tested up to: 5.1.1 +Stable tag: 0.4.1 +License: GPL-3 +License URI: https://www.gnu.org/licenses/gpl-3.0.html + +WPGraphQL for Advanced Custom Fields exposes ACF Field Groups and Fields to the WPGraphQL Schema. + +== Description == + +WPGraphQL for Advanced Custom Fields exposes ACF Field Groups and Fields to the WPGraphQL Schema, +allowing for interacting with ACF field data using GraphQL Queries. + +== Changelog == + += 0.1.7 = +* Add support for User Edit and User Register form locations + += 0.1.6 = +* Add support for Single Post / Page location rule for ACF Field Groups +* Fix bug with WYSIWYG field nested within repeaters/flex fields not properly getting wpautop applied +* Fix bug where Relationship fields were throwing errors when an item in the relationship had been deleted. Now the items are not included in the response at all and no errors are thrown + += 0.1.5 = +* Fix bug where field groups would disappear when using acf-local-json + += 0.1.4 = +* Fixes bug with WYSIWYG fields nested in Flex Fields and Repeater Fields not properly resolving +* Adds support for assigning ACF Field Groups to attachments (MediaItems) + += 0.1.3 = + +* Adds support for the following locations: Menus, Menu Items, Taxonomies, Comments +* Fixed a bug where the "show_in_graphql" button wasn't showing on Radio Buttons + += 0.1.2 = +* Fixes bug with Nested Fields not properly showing in the Schema. By defualt, fields are not supposed +to be exposed in the Schema if they are not set to "show_in_graphql", however there was a flaw in +logic causing nested fields of Flex Field layouts to not properly be exposed to the Schema. This +fixes that issue, so nested fields of Flex Field layouts can properly be queried and seen in the +Schema. + += 0.1.1 = +* Fixes bug with Field groups not properly being exposed to the Schema for custom post types. + += 0.1.0 = +* Initial public release. + + +== Upgrade Notice == + += 0.1.1 = +ACF Field groups were not properly being added to the GraphQL Schema for Custom Post Types. This +addresses that issue, so now Field groups that are set to "show_in_graphql" and are assigned to a +Custom Post Type that's also set to "show_in_graphql" will now be present in the Schema. diff --git a/src/class-config.php b/src/class-config.php index 773b7f1..7a6014b 100644 --- a/src/class-config.php +++ b/src/class-config.php @@ -54,10 +54,44 @@ public function init( \WPGraphQL\Registry\TypeRegistry $type_registry ) { $this->add_acf_fields_to_users(); $this->add_acf_fields_to_options_pages(); + // This filter tells WPGraphQL to resolve revision meta for ACF fields from the revision's meta, instead + // of the parent (published post) meta. add_filter( 'graphql_resolve_revision_meta_from_parent', function( $should, $object_id, $meta_key, $single ) { - if ( in_array( $meta_key, $this->registered_field_names, true ) ) { - return false; + + // Loop through all registered ACF fields that show in GraphQL. + if ( is_array( $this->registered_field_names ) && ! empty( $this->registered_field_names ) ) { + + $matches = null; + + // Iterate over all field names + foreach ( $this->registered_field_names as $field_name ) { + + // If the field name is an exact match with the $meta_key, the ACF field should + // resolve from the revision meta, so we can return false here, so that meta can + // resolve from the revision instead of the parent + if ( $field_name === $meta_key ) { + return false; + } + + // For flex fields/repeaters, the meta keys are structured a bit funky. + // This checks to see if the $meta_key starts with the same string as one of the + // acf fields (a flex/repeater field) and then checks if it's preceeded by an underscore and a number. + if ( $field_name === substr( $meta_key, 0, strlen( $field_name ) ) ) { + // match any string that starts with the field name, followed by an underscore, followed by a number, followed by another string + // ex my_flex_field_0_text_field or some_repeater_field_12_25MostPopularDogToys + $pattern = '/' . $field_name . '_\d+_\w+/m'; + preg_match( $pattern, $meta_key, $matches ); + } + + // If the meta key matches the pattern, treat it as a sub-field of an ACF Field Group + if ( null !== $matches ) { + return false; + } + + } + } + return $should; }, 10, 4 ); } diff --git a/tests/wpunit/PostObjectFieldsTest.php b/tests/wpunit/PostObjectFieldsTest.php index 388cb9c..2d6054f 100644 --- a/tests/wpunit/PostObjectFieldsTest.php +++ b/tests/wpunit/PostObjectFieldsTest.php @@ -1338,6 +1338,14 @@ public function testQueryRelationshipField() { } + public function test_flex_field_preview() { + // @todo: test that previewing flex fields work + } + + public function test_repeater_field_preview() { + // @todo: test that previewing repeater fields work + } + protected function register_fields() { add_action( 'init', function() {