From bd5df0032b886ffd86e55bef1d289178c74dcb43 Mon Sep 17 00:00:00 2001 From: Jason Bahl Date: Mon, 27 Jul 2020 07:28:12 -0600 Subject: [PATCH 1/4] - Update version 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 5848415..2f07b8c 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.3.4 + * Version: 0.3.5 * Requires PHP: 7.0 * GitHub Plugin URI: https://github.com/afragen/github-updater * From 2ffafbf737047718efd146a5f602aa5db6744cc6 Mon Sep 17 00:00:00 2001 From: Jason Bahl Date: Thu, 22 Oct 2020 08:43:16 -0600 Subject: [PATCH 2/4] - Update version 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 2f07b8c..8234666 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.3.5 + * Version: 0.3.6 * Requires PHP: 7.0 * GitHub Plugin URI: https://github.com/afragen/github-updater * From 6b2b0fb6d6bd7ed3bb334c996277956dc2e57d97 Mon Sep 17 00:00:00 2001 From: Jason Bahl Date: Thu, 22 Oct 2020 08:45:59 -0600 Subject: [PATCH 3/4] - update version 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 8234666..810f4d9 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.3.6 + * Version: 0.4.0 * Requires PHP: 7.0 * GitHub Plugin URI: https://github.com/afragen/github-updater * From 19ebfc29791fb8506c90234035230f9fc244460a Mon Sep 17 00:00:00 2001 From: Jason Bahl Date: Thu, 14 Jan 2021 12:23:50 -0700 Subject: [PATCH 4/4] - 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() {