Skip to content

Commit

Permalink
- respect "graphql_type_name" as a field group field in addition to "…
Browse files Browse the repository at this point in the history
…graphql_field_name"

- add support for OptionsPage UI from ACF v6.2+
  • Loading branch information
jasonbahl committed Sep 6, 2023
1 parent 5924342 commit 56fdff0
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 10 deletions.
167 changes: 167 additions & 0 deletions src/Admin/OptionsPageRegistration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<?php
namespace WPGraphQL\Acf\Admin;

use WPGraphQL\Utils\Utils;

class OptionsPageRegistration {

/**
* @return void
*/
public function init(): void {

// Add GraphQL columns to the ACF Options Page registration columns
// NOTE: the priority must be lower (a bigger number) than 10 to not conflict
// with the default ACF columns filter
add_filter( 'manage_acf-ui-options-page_posts_columns', [ $this, 'add_graphql_type_column' ], 20, 1 );

// Display the GraphQL Type in the ACF Taxonomy Registration Columns
add_action( 'manage_acf-ui-options-page_posts_custom_column', [ $this, 'render_graphql_columns' ], 10, 2 );

// Add registration fields to the ACF Options Pages output for exporting / saving as PHP
add_filter( 'acf/ui_options_page/registration_args', [ $this, 'add_registration_fields' ], 10, 2 );

// Add tha GraphQL Tab to the ACF Post Type registration screen
add_filter( 'acf/ui_options_page/additional_settings_tabs', [ $this, 'add_tabs' ] );

// Render the graphql settings tab in the ACF post type registration screen
add_action( 'acf/ui_options_page/render_settings_tab/graphql', [ $this, 'render_settings_tab' ] );
}

/**
* @param array $args
* @param array $post
*
* @return array
*/
public function add_registration_fields( array $args, array $post ) : array {
$show_in_graphql = false;

if ( isset( $args['show_in_graphql'] ) ) {
$show_in_graphql = $args['show_in_graphql'];
} elseif ( isset( $post['show_in_graphql'] ) ) {
$show_in_graphql = $post['show_in_graphql'];
}

$args['show_in_graphql'] = $show_in_graphql;

$graphql_type_name = '';

if ( ! empty( $args['graphql_type_name'] ) ) {
$graphql_type_name = $args['graphql_type_name'];
} elseif ( ! empty( $post['graphql_type_name'] ) ) {
$graphql_type_name = $post['graphql_type_name'];
} elseif ( isset( $args['page_title'] ) ) {
$graphql_type_name = Utils::format_field_name( $args['page_title'], false );
}

// if a graphql_single_name exists, use it, otherwise use the formatted version of the singular_name label
$args['graphql_type_name'] = $graphql_type_name;

return $args;
}

/**
* @param array $tabs
*
* @return array
*/
public function add_tabs( array $tabs ): array {
$tabs['graphql'] = __( 'GraphQL', 'wp-graphql-acf' );
return $tabs;
}

/**
* @param array $acf_ui_options_page
*
* @return void
*/
public function render_settings_tab( array $acf_ui_options_page ): void {
acf_render_field_wrap(
[
'type' => 'true_false',
'name' => 'show_in_graphql',
'key' => 'show_in_graphql',
'prefix' => 'acf_ui_options_page',
'value' => isset( $acf_ui_options_page['show_in_graphql'] ) && true === (bool) $acf_ui_options_page['show_in_graphql'] ? 1 : 0,
'ui' => true,
'label' => __( 'Show in GraphQL', 'wp-graphql-acf' ),
'instructions' => __( 'Whether to show the Post Type in the WPGraphQL Schema.', 'wp-graphql-acf' ),
'default' => false,
]
);

$graphql_type_name = $acf_ui_options_page['graphql_type_name'] ?? '';

if ( empty( $graphql_type_name ) ) {
$graphql_type_name = ! empty( $acf_ui_options_page['page_title'] ) ? Utils::format_field_name( $acf_ui_options_page['page_title'], true ) : '';
}

$graphql_type_name = Utils::format_field_name( $graphql_type_name, true );

acf_render_field_wrap(
[
'type' => 'text',
'name' => 'graphql_type_name',
'key' => 'graphql_type_name',
'prefix' => 'acf_ui_options_page',
'value' => $graphql_type_name,
'label' => __( 'GraphQL Type Name', 'wp-graphql-acf' ),
'instructions' => __( 'How the Options Page should be referenced in the GraphQL Schema.', 'wp-graphql-acf' ),
'default' => $graphql_type_name,
'required' => 1,
'conditions' => [
'field' => 'show_in_graphql',
'operator' => '==',
'value' => '1',
],
],
'div',
'field'
);
}

/**
* Given a list of columns, add "graphql_type" as a column.
*
* @param array $columns The columns on the post type table
*
* @return array
*/
public function add_graphql_type_column( array $columns ): array {
$columns['show_in_graphql'] = __( 'Show in GraphQL', 'wp-graphql-acf' );
$columns['graphql_type'] = __( 'GraphQL Type', 'wp-graphql-acf' );
return $columns;
}

/**
* Determine and echo the markup to show for the graphql_type column
*
* @param string $column_name The name of the column being rendered
* @param int $post_id The ID of the post the column is being displayed for
*
* @return void
*/
public function render_graphql_columns( string $column_name, int $post_id ): void {
$post_type = acf_get_internal_post_type( $post_id, 'acf-ui-options-page' );

// if there's no post type, bail early
if ( empty( $post_type ) ) {
return;
}

// Determine the output for the column
switch ( $column_name ) {
case 'graphql_type':
$graphql_type = Utils::format_type_name( \WPGraphQL\Acf\Utils::get_field_group_name( $post_type ) );
echo esc_html( $graphql_type );
break;
case 'show_in_graphql':
$show = isset( $post_type['show_in_graphql'] ) && true === (bool) $post_type['show_in_graphql'] ? 'true' : 'false';
echo esc_html( $show );
break;
default:
}
}

}
16 changes: 12 additions & 4 deletions src/LocationRules/LocationRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,6 @@ public function determine_block_rules( string $field_group_name, string $param,
return;
}

// Show all options pages
foreach ( $acf_blocks as $acf_block ) {
if ( ! isset( $acf_block['show_in_graphql'] ) || false === (bool) $acf_block['show_in_graphql'] ) {
continue;
Expand All @@ -911,7 +910,6 @@ public function determine_block_rules( string $field_group_name, string $param,
$this->set_graphql_type( $field_group_name, $type_name );
}

// Get the options page to unset
$acf_block = acf_get_block_type( $value );
if ( ! isset( $acf_block['show_in_graphql'] ) || false === $acf_block['show_in_graphql'] ) {
return;
Expand Down Expand Up @@ -956,7 +954,13 @@ public function determine_options_rules( string $field_group_name, string $param
if ( ! isset( $options_page['show_in_graphql'] ) || false === (bool) $options_page['show_in_graphql'] ) {
continue;
}
$type_name = isset( $options_page['graphql_field_name'] ) ? Utils::format_type_name( $options_page['graphql_field_name'] ) : Utils::format_type_name( $options_page['menu_slug'] );

if ( ! empty( $options_page['graphql_single_name'] ) ) {
$type_name = Utils::format_type_name( $options_page['graphql_single_name'] );
} else {
$type_name = isset( $options_page['graphql_type_name'] ) ? Utils::format_type_name( $options_page['graphql_type_name'] ) : Utils::format_type_name( $options_page['menu_slug'] );
}

$this->set_graphql_type( $field_group_name, $type_name );
}

Expand All @@ -965,7 +969,11 @@ public function determine_options_rules( string $field_group_name, string $param
if ( ! isset( $options_page['show_in_graphql'] ) || false === $options_page['show_in_graphql'] ) {
return;
}
$type_name = isset( $options_page['graphql_field_name'] ) ? Utils::format_type_name( $options_page['graphql_field_name'] ) : Utils::format_type_name( $options_page['menu_slug'] );
if ( ! empty( $options_page['graphql_single_name'] ) ) {
$type_name = Utils::format_type_name( $options_page['graphql_single_name'] );
} else {
$type_name = isset( $options_page['graphql_type_name'] ) ? Utils::format_type_name( $options_page['graphql_type_name'] ) : Utils::format_type_name( $options_page['menu_slug'] );
}
$this->unset_graphql_type( $field_group_name, $type_name );
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,11 @@ public static function get_acf_options_pages(): array {
return $options_pages;
}
$acf_options_pages = acf_get_options_pages();

if ( empty( $acf_options_pages ) || ! is_array( $acf_options_pages ) ) {
return $options_pages;
}

return array_filter(
array_map(
static function ( $option_page ) {
Expand Down Expand Up @@ -275,7 +277,6 @@ public static function should_field_show_in_graphql( array $acf_field ): bool {
public static function should_field_group_show_in_graphql( array $acf_field_group ): bool {
$should = true;


$show_in_rest = $acf_field_group['show_in_rest'] ?? false;


Expand Down Expand Up @@ -309,6 +310,8 @@ public static function get_field_group_name( array $field_group ): string {
if ( ! empty( $field_group['graphql_field_name'] ) ) {
$field_group_name = $field_group['graphql_field_name'];
$field_group_name = preg_replace( '/[^0-9a-zA-Z_\s]/i', '', $field_group_name );
} elseif ( ! empty( $field_group['graphql_type_name'] ) ) {
$field_group_name = \WPGraphQL\Utils\Utils::format_field_name( $field_group['graphql_type_name'], true );
} else {
if ( ! empty( $field_group['name'] ) ) {
$field_group_name = $field_group['name'];
Expand Down
8 changes: 6 additions & 2 deletions src/WPGraphQLAcf.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use WPGraphQL\Acf\Admin\PostTypeRegistration;
use WPGraphQL\Acf\Admin\TaxonomyRegistration;
use WPGraphQL\Acf\Admin\OptionsPageRegistration;
use WPGraphQL\Acf\Registry;
use WPGraphQL\Acf\ThirdParty;

Expand Down Expand Up @@ -34,7 +35,7 @@ public function init(): void {

add_action( 'wpgraphql/acf/init', [ $this, 'init_third_party_support' ] );
add_action( 'admin_init', [ $this, 'init_admin_settings' ] );
add_action( 'after_setup_theme', [ $this, 'cpt_tax_registration' ] );
add_action( 'after_setup_theme', [ $this, 'acf_internal_post_type_support' ] );
add_action( 'graphql_register_types', [ $this, 'init_registry' ] );

add_filter( 'graphql_data_loaders', [ $this, 'register_loaders' ], 10, 2 );
Expand Down Expand Up @@ -65,12 +66,15 @@ public function init_admin_settings(): void {
*
* @return void
*/
public function cpt_tax_registration(): void {
public function acf_internal_post_type_support(): void {
$taxonomy_registration_screen = new TaxonomyRegistration();
$taxonomy_registration_screen->init();

$cpt_registration_screen = new PostTypeRegistration();
$cpt_registration_screen->init();

$options_page_registration_screen = new OptionsPageRegistration();
$options_page_registration_screen->init();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/wpunit/LocationRulesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ public function testNoErrorIfFieldGroupIsAssignedToOrphanedOptionsPage() {
'menu_slug' => 'theme-general-settings',
'capability' => 'edit_posts',
'redirect' => false,
'graphql_single_name'
'show_in_graphql' => 1
));

$this->clearSchema();
Expand Down
4 changes: 2 additions & 2 deletions tests/wpunit/OptionsPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public function testOptionsPageRespectsGraphqlFieldName() {
'menu_slug' => 'custom-graphql-name',
'capability' => 'edit_posts',
// options pages will show in the Schema unless set to false
'graphql_field_name' => 'MyCustomOptionsName',
'graphql_type_name' => 'MyCustomOptionsName',
]
);

Expand Down Expand Up @@ -255,7 +255,7 @@ public function testQueryOptionsPageAsNode() {
'menu_slug' => 'options-page-node',
'capability' => 'edit_posts',
// options pages will show in the Schema unless set to false
'graphql_field_name' => 'OptionsPageNode',
'graphql_type_name' => 'OptionsPageNode',
]
);

Expand Down

0 comments on commit 56fdff0

Please sign in to comment.