From 64699cf4d981f4b5d320a2dc362ac083e80ab916 Mon Sep 17 00:00:00 2001 From: Jason Bahl Date: Mon, 23 Nov 2020 09:06:54 -0700 Subject: [PATCH 1/3] Update README.md --- README.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) 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". From 19ebfc29791fb8506c90234035230f9fc244460a Mon Sep 17 00:00:00 2001 From: Jason Bahl Date: Thu, 14 Jan 2021 12:23:50 -0700 Subject: [PATCH 2/3] - Update the filter for ACF Fields to resolve from revisions instead of the revision parent to include subfields of flex/repeaters. This allows flex/repeater fields to be previewed using the revised data --- src/class-config.php | 38 +++++++++++++++++++++++++-- tests/wpunit/PostObjectFieldsTest.php | 8 ++++++ 2 files changed, 44 insertions(+), 2 deletions(-) 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() { From 2d47d5b98d1866b4095e94901733599192d54fdd Mon Sep 17 00:00:00 2001 From: Jason Bahl Date: Tue, 19 Jan 2021 14:18:29 -0700 Subject: [PATCH 3/3] - Update version number for release --- wp-graphql-acf.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wp-graphql-acf.php b/wp-graphql-acf.php index 810f4d9..fb3dd68 100644 --- a/wp-graphql-acf.php +++ b/wp-graphql-acf.php @@ -7,7 +7,7 @@ * Author URI: https://www.wpgraphql.com * Text Domain: wp-graphql-acf * Domain Path: /languages - * Version: 0.4.0 + * Version: 0.4.1 * Requires PHP: 7.0 * GitHub Plugin URI: https://github.com/afragen/github-updater *