Skip to content

Commit bd93fac

Browse files
committed
- fix options pages to resolve when set with a custom post_id
- update tests (one still failing)
1 parent 503d8f9 commit bd93fac

File tree

3 files changed

+42
-16
lines changed

3 files changed

+42
-16
lines changed

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: 37 additions & 12 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',
@@ -40,7 +41,7 @@ public function testAcfOptionsPageIsQueryableInSchema() {
4041
// Save a value to the ACF Option Field
4142
// see: https://www.advancedcustomfields.com/resources/update_field/#update-a-value-from-different-objects
4243
if ( function_exists( 'update_field' ) ) {
43-
update_field( 'text', $expected_value, 'option' );
44+
update_field( 'text', $expected_value, 'options' );
4445
}
4546

4647
$this->register_acf_field( [], [
@@ -103,7 +104,7 @@ public function testAcfOptionsPageIsQueryableInSchema() {
103104
*/
104105
public function testOptionsPageNotInSchemaIfShowInGraphqlIsFalse() {
105106

106-
acf_add_options_page(
107+
$this->registerOptionsPage(
107108
[
108109
'page_title' => 'ShowInGraphQLFalse',
109110
'menu_title' => __( 'Show in GraphQL False' ),
@@ -175,24 +176,44 @@ public function testOptionsPageNotInSchemaIfShowInGraphqlIsFalse() {
175176

176177
// - options page shows with different name if "graphql_field_name" is set
177178
public function testOptionsPageRespectsGraphqlFieldName() {
178-
acf_add_options_page(
179+
180+
$this->registerOptionsPage(
179181
[
180182
'page_title' => 'CustomGraphqlName',
181-
'menu_title' => __( 'Show in GraphQL False' ),
183+
'menu_title' => __( 'Custom GraphQL Name' ),
182184
'menu_slug' => 'custom-graphql-name',
183185
'capability' => 'edit_posts',
186+
'post_id' => 'custom-graphql-name',
184187
// options pages will show in the Schema unless set to false
185188
'graphql_type_name' => 'MyCustomOptionsName',
186189
]
187190
);
188191

189-
$expected_value = uniqid( 'test', true );
192+
$expected_value = 'test value';
190193

191194
// Save a value to the ACF Option Field
192195
// 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+
update_field( 'text', $expected_value, 'custom-graphql-name' );
197+
$get_field = get_field( 'text', 'custom-graphql-name' );
198+
199+
codecept_debug( [
200+
'$get_field' => $get_field,
201+
'$options_pages' => acf_get_options_pages(),
202+
]);
203+
204+
$this->assertSame( $expected_value, $get_field );
205+
206+
$this->registerOptionsPage(
207+
[
208+
'page_title' => 'CustomGraphqlName',
209+
'menu_title' => __( 'Custom GraphQL Name' ),
210+
'menu_slug' => 'custom-graphql-name',
211+
'capability' => 'edit_posts',
212+
'post_id' => 'custom-graphql-name',
213+
// options pages will show in the Schema unless set to false
214+
'graphql_type_name' => 'MyCustomOptionsName',
215+
]
216+
);
196217

197218
$this->register_acf_field( [], [
198219
'graphql_field_name' => 'OptionsFields',
@@ -221,6 +242,10 @@ public function testOptionsPageRespectsGraphqlFieldName() {
221242
'query' => $query,
222243
]);
223244

245+
codecept_debug( [
246+
'$get_field' => $get_field,
247+
]);
248+
224249
self::assertQuerySuccessful( $actual, [
225250
// the instructions should be used for the description
226251
$this->expectedField( 'myCustomOptionsName.optionsFields.text', $expected_value ),
@@ -248,7 +273,7 @@ public function testOptionsPageRespectsGraphqlFieldName() {
248273

249274
public function testQueryOptionsPageAsNode() {
250275

251-
acf_add_options_page(
276+
$this->registerOptionsPage(
252277
[
253278
'page_title' => 'OptionsPageNode',
254279
'menu_title' => __( 'Options Page Node' ),

0 commit comments

Comments
 (0)