Skip to content

Commit

Permalink
Merge branch 'feat/#83-show-in-graphql-options-pages' into release/v2…
Browse files Browse the repository at this point in the history
….0.0-beta.5.0.0

# Conflicts:
#	src/LocationRules/LocationRules.php
#	src/ThirdParty/WPGraphQLContentBlocks/WPGraphQLContentBlocks.php
  • Loading branch information
jasonbahl committed Sep 12, 2023
2 parents 34ddf03 + 6bb29d7 commit 4a6af43
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 10 deletions.
41 changes: 38 additions & 3 deletions src/LocationRules/LocationRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,44 @@ public function determine_nav_menu_item_item_rules( string $field_group_name, st
* @return void
*/
public function determine_block_rules( string $field_group_name, string $param, string $operator, string $value ): void {
// @todo: ACF Blocks are not formally supported by WPGraphQL / WPGraphQL for ACF. More to come in the future!

if ( ! function_exists( 'acf_get_block_types' ) ) {
return;
}

if ( '==' === $operator ) {
$acf_block = acf_get_block_type( $value );

if ( empty( $acf_block ) || ! \WPGraphQL\Acf\Utils::should_field_group_show_in_graphql( $acf_block ) ) {
return;
}

$type_name = \WPGraphQL\Acf\Utils::get_field_group_name( $acf_block );
$this->set_graphql_type( $field_group_name, $type_name );
}

if ( '!=' === $operator ) {
$acf_blocks = acf_get_block_types();

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

foreach ( $acf_blocks as $acf_block ) {
if ( ! isset( $acf_block['show_in_graphql'] ) || false === (bool) $acf_block['show_in_graphql'] ) {
continue;
}
$type_name = isset( $acf_block['graphql_field_name'] ) ? Utils::format_type_name( $acf_block['graphql_field_name'] ) : Utils::format_type_name( $acf_block['name'] );
$this->set_graphql_type( $field_group_name, $type_name );
}

$acf_block = acf_get_block_type( $value );
if ( ! isset( $acf_block['show_in_graphql'] ) || false === $acf_block['show_in_graphql'] ) {
return;
}
$type_name = isset( $acf_block['graphql_field_name'] ) ? Utils::format_type_name( $acf_block['graphql_field_name'] ) : Utils::format_type_name( $acf_block['name'] );
$this->unset_graphql_type( $field_group_name, $type_name );
}
}

/**
Expand Down Expand Up @@ -930,13 +967,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;
}

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
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
class WPGraphQLContentBlocks {

/**
* Initialize support for WPGraphQL Content Blocks
*
* @return void
*/
public function init(): void {

// Bail if WPGraphQL Content Blocks is not active at 1.2.0 or later.
// If WPGraphQL Content Blocks is not active, or is active with a version prior to 1.2.0, don't
// instantiate this functionality
if ( ! defined( 'WPGRAPHQL_CONTENT_BLOCKS_VERSION' ) || version_compare( WPGRAPHQL_CONTENT_BLOCKS_VERSION, '1.2.0', 'lt' ) ) {
return;
}
Expand Down
5 changes: 4 additions & 1 deletion src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,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 @@ -274,7 +276,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 @@ -308,6 +309,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 4a6af43

Please sign in to comment.