Skip to content

Commit 39448e6

Browse files
committed
Merge commit '071cfec6ba62494ec9fa4be4bb8d2fdebed612ba' into release/v2.2.0
2 parents b37338a + 071cfec commit 39448e6

File tree

14 files changed

+389
-26
lines changed

14 files changed

+389
-26
lines changed

src/Admin/PostTypeRegistration.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,18 @@ public function add_cpt_registration_fields( array $args, array $post_type ): ar
167167
}
168168

169169
/**
170-
* @param string $screen
170+
* Enqueue the admin scripts for the ACF Post Type settings.
171+
* This script is only enqueued on the ACF Post Type edit screen.
172+
*
173+
* @param ?string $screen
171174
*/
172-
public function enqueue_admin_scripts( string $screen ): void {
175+
public function enqueue_admin_scripts( ?string $screen ): void {
173176
global $post;
174177

178+
if ( empty( $screen ) ) {
179+
return;
180+
}
181+
175182
// if the screen is not a new post / edit post screen, do nothing
176183
if ( ! ( 'post-new.php' === $screen || 'post.php' === $screen ) ) {
177184
return;

src/Admin/TaxonomyRegistration.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,18 @@ public function add_taxonomy_registration_fields( array $args, array $taxonomy )
165165
}
166166

167167
/**
168-
* @param string $screen
168+
* Enqueue the admin scripts for the ACF Post Type settings.
169+
* This script is only enqueued on the ACF Taxonomy edit screen.
170+
*
171+
* @param ?string $screen
169172
*/
170-
public function enqueue_admin_scripts( string $screen ): void {
173+
public function enqueue_admin_scripts( ?string $screen ): void {
171174
global $post;
172175

176+
if ( empty( $screen ) ) {
177+
return;
178+
}
179+
173180
// if the screen is not a new post / edit post screen, do nothing
174181
if ( ! ( 'post-new.php' === $screen || 'post.php' === $screen ) ) {
175182
return;

src/FieldConfig.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ protected function is_supported_field_type(): bool {
135135
*/
136136
public function get_field_description(): string {
137137

138+
$graphql_field_type = $this->get_graphql_field_type();
139+
$field_type_config = ( $graphql_field_type instanceof AcfGraphQLFieldType ) ? $graphql_field_type->get_config() : [];
140+
138141
// Use the explicit graphql_description, if set
139142
if ( ! empty( $this->acf_field['graphql_description'] ) ) {
140143
$description = $this->acf_field['graphql_description'];
@@ -153,6 +156,14 @@ public function get_field_description(): string {
153156
);
154157
}
155158

159+
if ( isset( $field_type_config['graphql_description_after'] ) ) {
160+
if ( is_callable( $field_type_config['graphql_description_after'] ) ) {
161+
$description .= ' ' . call_user_func( $field_type_config['graphql_description_after'], $this );
162+
} else {
163+
$description .= ' ' . $field_type_config['graphql_description_after'];
164+
}
165+
}
166+
156167
return $description;
157168
}
158169

@@ -234,6 +245,7 @@ public function get_graphql_field_config(): ?array {
234245
// bail and let the connection handle registration to the schema
235246
// and resolution
236247
if ( 'connection' === $field_type ) {
248+
$this->registry->register_field( $this->acf_field );
237249
return null;
238250
}
239251

@@ -390,11 +402,17 @@ public function resolve_field( $root, array $args, AppContext $context, ResolveI
390402

391403
// If the root being passed down already has a value
392404
// for the field key, let's use it to resolve
393-
if ( ! empty( $root[ $field_key ] ) ) {
394-
return $this->prepare_acf_field_value( $root[ $field_key ], $node, $node_id, $field_config );
405+
if ( isset( $field_config['key'] ) && ! empty( $root[ $field_config['key'] ] ) ) {
406+
return $this->prepare_acf_field_value( $root[ $field_config['key'] ], $node, $node_id, $field_config );
407+
}
408+
409+
// Check if the cloned field key is being used to pass values down
410+
if ( isset( $field_config['__key'] ) && ! empty( $root[ $field_config['__key'] ] ) ) {
411+
return $this->prepare_acf_field_value( $root[ $field_config['__key'] ], $node, $node_id, $field_config );
395412
}
396413

397-
if ( ! empty( $root[ $field_config['name'] ] ) ) {
414+
// Else check if the values are being passed down via the name
415+
if ( isset( $field_config['name'] ) && ! empty( $root[ $field_config['name'] ] ) ) {
398416
return $this->prepare_acf_field_value( $root[ $field_config['name'] ], $node, $node_id, $field_config );
399417
}
400418

src/FieldType/DatePicker.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,16 @@ public static function register_field_type(): void {
1212
register_graphql_acf_field_type(
1313
'date_picker',
1414
[
15-
'graphql_type' => 'String',
16-
'resolve' => static function ( $root, $args, $context, $info, $field_type, FieldConfig $field_config ) {
15+
'graphql_type' => 'String',
16+
// Apply a description to be appended to the field description.
17+
// @todo: consider removing when CustomScalar types are supported along with the @specifiedBy directive
18+
'graphql_description_after' => static function ( FieldConfig $field_config ) {
19+
$field_type = $field_config->get_acf_field()['type'] ?? null;
20+
21+
// translators: The $s is the name of the acf field type that is returning a date string according to the RFC3339 spec.
22+
return '(' . sprintf( __( 'ACF Fields of the %s type return a date string according to the RFC3339 spec: https://datatracker.ietf.org/doc/html/rfc3339.', 'wpgraphql-acf' ), $field_type ) . ')';
23+
},
24+
'resolve' => static function ( $root, $args, $context, $info, $field_type, FieldConfig $field_config ) {
1725
$value = $field_config->resolve_field( $root, $args, $context, $info );
1826

1927
if ( empty( $value ) ) {

src/FieldType/DateTimePicker.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@ public static function register_field_type(): void {
1313
register_graphql_acf_field_type(
1414
'date_time_picker',
1515
[
16-
'graphql_type' => 'String',
17-
'resolve' => static function ( $root, $args, $context, $info, $field_type, FieldConfig $field_config ) {
16+
'graphql_type' => 'String',
17+
// Apply a description to be appended to the field description.
18+
// @todo: consider removing when CustomScalar types are supported along with the @specifiedBy directive
19+
'graphql_description_after' => static function ( FieldConfig $field_config ) {
20+
$field_type = $field_config->get_acf_field()['type'] ?? null;
21+
22+
// translators: The $s is the name of the acf field type that is returning a date string according to the RFC3339 spec.
23+
return '(' . sprintf( __( 'ACF Fields of the "%s" type return a date string according to the RFC3339 spec: https://datatracker.ietf.org/doc/html/rfc3339.', 'wpgraphql-acf' ), $field_type ) . ')';
24+
},
25+
'resolve' => static function ( $root, $args, $context, $info, $field_type, FieldConfig $field_config ) {
1826
$value = $field_config->resolve_field( $root, $args, $context, $info );
1927

2028
if ( empty( $value ) ) {

src/FieldType/FlexibleContent.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ static function ( $field ) use ( $layout ) {
7373
)
7474
);
7575

76-
$layout['sub_fields'] = array_merge( $sub_fields, $layout['sub_fields'] );
76+
$layout_sub_fields = ! empty( $layout['sub_fields'] ) && is_array( $layout['sub_fields'] ) ? $layout['sub_fields'] : [];
77+
78+
$layout['sub_fields'] = array_merge( $sub_fields, $layout_sub_fields );
7779

7880
$layouts[] = $layout;
7981
}

src/Registry.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@ public function get_registered_fields() {
6565
return $this->registered_fields;
6666
}
6767

68+
/**
69+
* @param array<mixed> $acf_field
70+
*
71+
* @return void
72+
*/
73+
public function register_field( $acf_field ) {
74+
75+
if ( isset( $acf_field['name'] ) && ! in_array( $acf_field['name'], $this->registered_fields, true ) ) {
76+
$this->registered_fields[] = $acf_field['name'];
77+
}
78+
79+
if ( isset( $acf_field['key'] ) && ! in_array( $acf_field['key'], $this->registered_fields, true ) ) {
80+
$this->registered_fields[] = $acf_field['key'];
81+
}
82+
}
83+
6884
/**
6985
* Get the TypeRegistry instance
7086
*/
@@ -505,11 +521,8 @@ public function map_acf_field_to_graphql( array $acf_field, array $acf_field_gro
505521
$field_config = ( new FieldConfig( $acf_field, $acf_field_group, $this ) )->get_graphql_field_config();
506522

507523
if ( ! empty( $field_config['acf_field'] ) ) {
508-
if ( isset( $field_config['acf_field']['key'] ) && ! in_array( $field_config['acf_field']['key'], $this->registered_fields, true ) ) {
509-
$this->registered_fields[] = $field_config['acf_field']['key'];
510-
}
511-
if ( isset( $field_config['acf_field']['name'] ) && ! in_array( $field_config['acf_field']['name'], $this->registered_fields, true ) ) {
512-
$this->registered_fields[] = $field_config['acf_field']['name'];
524+
if ( isset( $field_config['acf_field'] ) ) {
525+
$this->register_field( $field_config['acf_field'] );
513526
}
514527
}
515528

src/ThirdParty/AcfExtended/AcfExtended.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,12 +339,12 @@ public function register_initial_types(): void {
339339
'startDate' => [
340340
// @todo: DATETIME Scalar
341341
'type' => 'String',
342-
'description' => __( 'The start date of a date range returned as an RFC 3339 time string', 'wpgraphql-acf' ),
342+
'description' => __( 'The start date of a date range returned as an RFC 3339 time string (https://datatracker.ietf.org/doc/html/rfc3339)', 'wpgraphql-acf' ),
343343
],
344344
'endDate' => [
345345
// @todo: DATETIME Scalar
346346
'type' => 'String',
347-
'description' => __( 'The start date of a date range RFC 3339 time string', 'wpgraphql-acf' ),
347+
'description' => __( 'The start date of a date range RFC 3339 time string (https://datatracker.ietf.org/doc/html/rfc3339)', 'wpgraphql-acf' ),
348348
],
349349
],
350350
]

tests/_data/images/test2.png

3.49 KB
Loading

tests/_support/WPUnit/AcfFieldTestCase.php

Lines changed: 132 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ public function get_data_to_store() {
7575
return 'text value...';
7676
}
7777

78+
/**
79+
* @return mixed
80+
*/
81+
public function get_preview_data_to_store() {
82+
return 'preview - ' . $this->get_data_to_store();
83+
}
84+
7885
/**
7986
* Override this in the testing class to test the field against blocks
8087
*
@@ -123,6 +130,16 @@ public function get_expected_value() {
123130
return 'null';
124131
}
125132

133+
/**
134+
* Test class should override this with the expected value
135+
*
136+
* @return mixed
137+
*/
138+
public function get_expected_preview_value() {
139+
return 'null';
140+
}
141+
142+
126143
/**
127144
* @return string
128145
*/
@@ -672,11 +689,14 @@ public function testFieldDescriptionUsesInstructionsIfGraphqlDescriptionNotProvi
672689
// the instructions should be used for the description
673690
// if "graphql_description" is not provided
674691
$this->expectedNode( '__type.fields', [
675-
'name' => $this->get_formatted_field_name(),
676-
'description' => $instructions
692+
$this->expectedField( 'name', $this->get_formatted_field_name() ),
693+
$this->expectedField( 'description', self::NOT_NULL ),
677694
]),
695+
678696
] );
679697

698+
$this->assertStringContainsString( $instructions, $actual['data']['__type']['fields'][0]['description'] );
699+
680700
// remove the local field
681701
acf_remove_local_field( $field_key );
682702

@@ -720,11 +740,14 @@ public function testFieldDescriptionUsesGraphqlDescriptionIfProvided(): void {
720740
// the instructions should be used for the description
721741
// if "graphql_description" is not provided
722742
$this->expectedNode( '__type.fields', [
723-
'name' => $this->get_formatted_field_name(),
724-
'description' => $graphql_description
743+
$this->expectedField( 'name', $this->get_formatted_field_name() ),
744+
$this->expectedField( 'description', self::NOT_NULL ),
725745
]),
726746
] );
727747

748+
$this->assertStringContainsString( $graphql_description, $actual['data']['__type']['fields'][0]['description'] );
749+
750+
728751
// remove the local field
729752
acf_remove_local_field( $field_key );
730753

@@ -1026,6 +1049,111 @@ public function testQueryFieldOnPostReturnsExpectedValue() {
10261049

10271050
}
10281051

1052+
public function testQueryFieldOnPostAsPreviewReturnsExpectedValue() {
1053+
1054+
// disable the block editor
1055+
add_filter('use_block_editor_for_post', '__return_false');
1056+
1057+
$field_key = $this->register_acf_field();
1058+
1059+
// Save data to the post
1060+
update_field( $field_key, $this->get_data_to_store(), $this->published_post->ID );
1061+
1062+
$fragment = $this->get_query_fragment();
1063+
1064+
if ( 'null' === $fragment ) {
1065+
$this->markTestIncomplete( 'get_query_fragment() not defined' );
1066+
}
1067+
1068+
$expected_value = $this->get_expected_value();
1069+
1070+
if ( 'null' === $expected_value ) {
1071+
$this->markTestIncomplete( 'get_expected_value() not defined' );
1072+
}
1073+
1074+
$expected_preview_value = $this->get_expected_preview_value();
1075+
1076+
if ( 'null' === $expected_preview_value ) {
1077+
$this->markTestIncomplete( 'get_expected_preview_value() not defined' );
1078+
}
1079+
1080+
$preview_data_to_store = $this->get_preview_data_to_store();
1081+
1082+
if ( 'null' === $preview_data_to_store ) {
1083+
$this->markTestIncomplete( 'get_preview_data_to_store() not defined' );
1084+
}
1085+
1086+
$query = '
1087+
query AcfFieldOnPost ($id: ID! $asPreview:Boolean) {
1088+
post( id: $id idType: DATABASE_ID asPreview: $asPreview) {
1089+
databaseId
1090+
__typename
1091+
title
1092+
...on WithAcfAcfTestGroup {
1093+
acfTestGroup {
1094+
...AcfTestGroupFragment
1095+
}
1096+
}
1097+
}
1098+
}
1099+
' . $fragment;
1100+
1101+
$actual = $this->graphql([
1102+
'query' => $query,
1103+
'variables' => [
1104+
'id' => $this->published_post->ID,
1105+
'asPreview' => false,
1106+
]
1107+
]);
1108+
1109+
// querying as preview without any preview revision will
1110+
// return the same result as querying as published
1111+
self::assertQuerySuccessful( $actual, [
1112+
$this->expectedField( 'post.databaseId', $this->published_post->ID ),
1113+
$this->expectedField( 'post.__typename', 'Post' ),
1114+
$this->expectedField( 'post.acfTestGroup.' . $this->get_formatted_field_name(), $this->get_expected_value() )
1115+
]);
1116+
1117+
// set the current user as admin
1118+
wp_set_current_user( $this->admin->ID );
1119+
1120+
// create a preview revision
1121+
$preview_id = wp_create_post_autosave( [
1122+
'post_ID' => $this->published_post->ID,
1123+
'post_type' => 'post',
1124+
'post_title' => 'Preview Title',
1125+
] );
1126+
1127+
if ( is_wp_error( $preview_id ) ) {
1128+
// Handle error; autosave creation failed
1129+
codecept_debug( 'Error creating autosave: ' . $preview_id->get_error_message() );
1130+
}
1131+
1132+
// update the preview revision with the preview data
1133+
update_field( $field_key, $this->get_preview_data_to_store(), $preview_id );
1134+
1135+
$actual = $this->graphql([
1136+
'query' => $query,
1137+
'variables' => [
1138+
'id' => $this->published_post->ID,
1139+
'asPreview' => true,
1140+
]
1141+
]);
1142+
1143+
self::assertQuerySuccessful( $actual, [
1144+
$this->expectedField( 'post.databaseId', $preview_id ),
1145+
$this->expectedField( 'post.__typename', 'Post' ),
1146+
$this->expectedField( 'post.title', 'Preview Title' ),
1147+
$this->expectedField( 'post.acfTestGroup.' . $this->get_formatted_field_name(), $this->get_expected_preview_value() )
1148+
]);
1149+
1150+
wp_delete_post( $preview_id, true );
1151+
1152+
// re-enable the block editor
1153+
add_filter('use_block_editor_for_post', '__return_true');
1154+
1155+
}
1156+
10291157
/**
10301158
* @todo: implement the below tests
10311159
*/

0 commit comments

Comments
 (0)