Skip to content

Commit b56bef5

Browse files
authored
Merge pull request #132 from wp-graphql/fix/128-options-page-with-custom-post_id-not-resolving
fix: options page with custom post_id not resolving properly
2 parents 8ae6cd0 + e3b0af0 commit b56bef5

File tree

4 files changed

+47
-32
lines changed

4 files changed

+47
-32
lines changed

src/Model/AcfOptionsPage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ protected function init(): void {
5353
'id' => function () {
5454
return Relay::toGlobalId( 'acf_options_page', $this->data['menu_slug'] );
5555
},
56-
'acfId' => 'options',
56+
'acfId' => $this->data['post_id'] ?? 'options',
5757
];
5858
}
5959
}

src/Registry.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ public function register_options_pages():void {
356356
);
357357

358358
foreach ( $graphql_options_pages as $graphql_options_page ) {
359+
$graphql_options_page['is_options_page'] = true;
359360
if ( ! $this->should_field_group_show_in_graphql( $graphql_options_page ) ) {
360361
continue;
361362
}

src/Utils.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ public static function get_node_acf_id( $node ) {
5656
// @phpstan-ignore-next-line
5757
$id = 'comment_' . absint( $node->databaseId );
5858
break;
59-
case is_array( $node ) && isset( $node['post_id'] ) && 'options' === $node['post_id']:
60-
$id = $node['post_id'];
61-
break;
6259
case $node instanceof AcfOptionsPage:
6360
$id = $node->acfId;
6461
break;
62+
case is_array( $node ) && isset( $node['post_id'] ):
63+
$id = $node['post_id'];
64+
break;
6565
default:
6666
$id = 0;
6767
break;
@@ -282,7 +282,7 @@ public static function should_field_group_show_in_graphql( array $acf_field_grou
282282
// if the field group was configured with no "show_in_graphql" value, default to the "show_in_rest" value
283283
// to determine if the group should be available in an API
284284
if (
285-
( isset( $acf_field_group['post_id'] ) && 'options' !== $acf_field_group['post_id'] ) &&
285+
( isset( $acf_field_group['is_options_page'] ) && false === $acf_field_group['is_options_page'] ) &&
286286
! isset( $acf_field_group['show_in_graphql'] ) ) {
287287
$acf_field_group['show_in_graphql'] = $show_in_rest ?? false;
288288
}

tests/wpunit/OptionsPageTest.php

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
class OptionsPageTest extends \Tests\WPGraphQL\Acf\WPUnit\WPGraphQLAcfTestCase {
44

5-
65
public function setUp():void {
6+
77
if ( ! function_exists( 'acf_add_options_page' ) ) {
88
$this->markTestSkipped( 'ACF Options Pages are not available in this test environment' );
99
}
@@ -15,13 +15,14 @@ public function tearDown(): void {
1515
parent::tearDown();
1616
}
1717

18-
public function registerOptionsPage( $title = 'My Options Page', $config = [] ) {
1918

2019

20+
public function registerOptionsPage( $config = [] ) {
21+
2122
// register options page
2223
acf_add_options_page(
2324
array_merge( [
24-
'page_title' => $title,
25+
'page_title' => 'My Options Page',
2526
'menu_title' => __( 'My Options Page' ),
2627
'menu_slug' => 'my-options-page',
2728
'capability' => 'edit_posts',
@@ -35,15 +36,7 @@ public function testAcfOptionsPageIsQueryableInSchema() {
3536

3637
$this->registerOptionsPage();
3738

38-
$expected_value = uniqid( 'test', true );
39-
40-
// Save a value to the ACF Option Field
41-
// see: https://www.advancedcustomfields.com/resources/update_field/#update-a-value-from-different-objects
42-
if ( function_exists( 'update_field' ) ) {
43-
update_field( 'text', $expected_value, 'option' );
44-
}
45-
46-
$this->register_acf_field( [], [
39+
$field_key = $this->register_acf_field( [], [
4740
'graphql_field_name' => 'OptionsFields',
4841
'location' => [
4942
[
@@ -56,6 +49,16 @@ public function testAcfOptionsPageIsQueryableInSchema() {
5649
],
5750
]);
5851

52+
$expected_value = uniqid( 'test', true );
53+
54+
// Save a value to the ACF Option Field
55+
// see: https://www.advancedcustomfields.com/resources/update_field/#update-a-value-from-different-objects
56+
if ( function_exists( 'update_field' ) ) {
57+
update_field( $field_key, $expected_value, 'options' );
58+
}
59+
60+
61+
5962
$query = '
6063
{
6164
myOptionsPage {
@@ -103,7 +106,7 @@ public function testAcfOptionsPageIsQueryableInSchema() {
103106
*/
104107
public function testOptionsPageNotInSchemaIfShowInGraphqlIsFalse() {
105108

106-
acf_add_options_page(
109+
$this->registerOptionsPage(
107110
[
108111
'page_title' => 'ShowInGraphQLFalse',
109112
'menu_title' => __( 'Show in GraphQL False' ),
@@ -175,26 +178,20 @@ public function testOptionsPageNotInSchemaIfShowInGraphqlIsFalse() {
175178

176179
// - options page shows with different name if "graphql_field_name" is set
177180
public function testOptionsPageRespectsGraphqlFieldName() {
178-
acf_add_options_page(
181+
182+
$this->registerOptionsPage(
179183
[
180184
'page_title' => 'CustomGraphqlName',
181-
'menu_title' => __( 'Show in GraphQL False' ),
185+
'menu_title' => __( 'Custom GraphQL Name' ),
182186
'menu_slug' => 'custom-graphql-name',
183187
'capability' => 'edit_posts',
188+
'post_id' => 'custom-graphql-name',
184189
// options pages will show in the Schema unless set to false
185190
'graphql_type_name' => 'MyCustomOptionsName',
186191
]
187192
);
188193

189-
$expected_value = uniqid( 'test', true );
190-
191-
// Save a value to the ACF Option Field
192-
// see: https://www.advancedcustomfields.com/resources/update_field/#update-a-value-from-different-objects
193-
if ( function_exists( 'update_field' ) ) {
194-
update_field( 'text', $expected_value, 'option' );
195-
}
196-
197-
$this->register_acf_field( [], [
194+
$acf_field = $this->register_acf_field( [], [
198195
'graphql_field_name' => 'OptionsFields',
199196
'location' => [
200197
[
@@ -207,10 +204,21 @@ public function testOptionsPageRespectsGraphqlFieldName() {
207204
],
208205
]);
209206

207+
$expected_value = 'test value';
208+
209+
// Save a value to the ACF Option Field
210+
// see: https://www.advancedcustomfields.com/resources/update_field/#update-a-value-from-different-objects
211+
if ( ! function_exists( 'update_field' ) ) {
212+
$this->markTestSkipped( 'update_field not available...' );
213+
}
214+
215+
// update the field using the field key of the registered field
216+
update_field( $acf_field, $expected_value, 'custom-graphql-name' );
217+
210218
$query = '
211219
{
212-
myCustomOptionsName {
213-
optionsFields {
220+
myCustomOptionsName { # this is the name of the options page based on graphql_type_name
221+
optionsFields { # this is the field name for the field group
214222
text
215223
}
216224
}
@@ -221,6 +229,12 @@ public function testOptionsPageRespectsGraphqlFieldName() {
221229
'query' => $query,
222230
]);
223231

232+
codecept_debug( [
233+
'$get_field' => get_field( 'text', 'custom-graphql-name' ),
234+
'options_pages' => acf_get_options_pages(),
235+
'$acf_field' => $acf_field,
236+
]);
237+
224238
self::assertQuerySuccessful( $actual, [
225239
// the instructions should be used for the description
226240
$this->expectedField( 'myCustomOptionsName.optionsFields.text', $expected_value ),
@@ -248,7 +262,7 @@ public function testOptionsPageRespectsGraphqlFieldName() {
248262

249263
public function testQueryOptionsPageAsNode() {
250264

251-
acf_add_options_page(
265+
$this->registerOptionsPage(
252266
[
253267
'page_title' => 'OptionsPageNode',
254268
'menu_title' => __( 'Options Page Node' ),

0 commit comments

Comments
 (0)