Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,4 @@ ACF_PRO=false

# ACF Extended
# ACF_EXTENDED_LICENSE_KEY="Your License Key"
ACF_EXTENDED_PRO=false
ACF_EXTENDED_VERSION="latest"
2 changes: 0 additions & 2 deletions .github/workflows/testing-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ jobs:
wordpress: [ '6.1', '5.9' ]
acf_pro: [ true, false ]
acf_version: [ 5.12.4, 6.1.6 ]
acf_extended_pro: [ true, false ]
include:
- php: '8.1'
wordpress: '6.1'
Expand Down Expand Up @@ -86,7 +85,6 @@ jobs:
-e ACF_PRO=${{matrix.acf_pro }} \
-e ACF_LICENSE_KEY=${{secrets.ACF_LICENSE_KEY}} \
-e ACF_VERSION=${{matrix.ACF_VERSION}} \
-e ACF_EXTENDED_PRO=${{matrix.acf_extended_pro}} \
-e ACF_EXTENDED_LICENSE_KEY=${{secrets.ACF_EXTENDED_LICENSE_KEY}} \
testing

Expand Down
2 changes: 1 addition & 1 deletion docker/app.setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ else
fi

# If ACF_EXTENDED_PRO is not true, or the license key is a default value, we'll be using the FREE version of ACF EXTENDED
if [[ true != ${ACF_EXTENDED_PRO} || '.' == ${ACF_EXTENDED_LICENSE_KEY} || 'Your License Key' == ${ACF_EXTENDED_LICENSE_KEY} ]]; then
if [[ true != ${ACF_PRO} || '.' == ${ACF_EXTENDED_LICENSE_KEY} || 'Your License Key' == ${ACF_EXTENDED_LICENSE_KEY} ]]; then

echo "ACF EXTENDED Version: " ${ACF_EXTENDED_VERSION}
ACF_EXTENDED_PLUGIN_SLUG="acf-extended/acf-extended.php"
Expand Down
4 changes: 3 additions & 1 deletion src/Admin/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ public function graphql_types_ajax_callback(): void {

if ( ! isset( $_POST['data'] ) ) {
echo esc_html( __( 'No location rules were found', 'wp-graphql-acf' ) );

/** @noinspection ForgottenDebugOutputInspection */
wp_die();
}

Expand Down Expand Up @@ -208,7 +210,7 @@ public function display_graphql_field_group_fields( $field_group ): void {
'type' => 'true_false',
'name' => 'show_in_graphql',
'prefix' => 'acf_field_group',
'value' => isset( $field_group['show_in_graphql'] ) && (bool) $field_group['show_in_graphql'],
'value' => isset( $field_group['show_in_graphql'] ) ? (bool) $field_group['show_in_graphql'] : 1,
'ui' => 1,
],
'div',
Expand Down
36 changes: 36 additions & 0 deletions src/Data/Loader/AcfOptionsPageLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
namespace WPGraphQLAcf\Data\Loader;

use WPGraphQL\Data\Loader\AbstractDataLoader;

class AcfOptionsPageLoader extends AbstractDataLoader {

/**
* @param array $keys
*
* @return array
* @throws \Exception
*/
protected function loadKeys( array $keys ): array {
if ( empty( $keys ) || ! function_exists( 'acf_get_options_pages' ) ) {
return [];
}

$options_pages = acf_get_options_pages();

if ( empty( $options_pages ) ) {
return [];
}

$response = [];

foreach ( $keys as $key ) {
if ( isset( $options_pages[ $key ] ) ) {
$response[ $key ] = new \WPGraphQLAcf\Model\AcfOptionsPage( $options_pages[ $key ] );
}
}

return $response;

}
}
8 changes: 7 additions & 1 deletion src/FieldType/FlexibleContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,13 @@ public static function register_field_type(): void {
$interfaces[] = $layout_interface_name;
$layout['eagerlyLoadType'] = true;
$layout['graphql_field_name'] = $layout_name;
$layout['fields'] = $field_config->get_registry()->get_fields_for_field_group( $layout );
$layout['fields'] = [
'fieldGroupName' => [
'type' => 'String',
'description' => __( 'The name of the ACF Flex Field Layout', 'wp-graphql-acf' ),
'deprecationReason' => __( 'Use __typename instead', 'wp-graphql-acf' ),
],
];
$layout['interfaces'] = $interfaces;
$layouts[ $layout_name ] = $layout;
}
Expand Down
5 changes: 3 additions & 2 deletions src/LocationRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -962,10 +962,11 @@ public function determine_options_rules( string $field_group_name, string $param

$options_page = acf_get_options_page( $value );

if ( ! isset( $options_page['show_in_graphql'] ) || false === (bool) $options_page['show_in_graphql'] ) {
if ( empty( $options_page ) || ! \WPGraphQLAcf\Utils::should_field_group_show_in_graphql( $options_page ) ) {
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'] );

$type_name = \WPGraphQLAcf\Utils::get_field_group_name( $options_page );
$this->set_graphql_type( $field_group_name, $type_name );
}

Expand Down
63 changes: 63 additions & 0 deletions src/Model/AcfOptionsPage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace WPGraphQLAcf\Model;

use Exception;
use GraphQLRelay\Relay;
use WPGraphQL\Model\Model;
use WPGraphQL\Utils\Utils;

/**
* Class AcfOptionsPage - Models data for avatars
*
* @property string $id
* @property string $slug
* @property string $pageTitle
* @property string $parentId
* @property string $capability
* @property string $acfId
*
* @package WPGraphQL\Model
*/
class AcfOptionsPage extends Model {

/**
* AcfOptionsPage constructor.
*
* @param array $options_page The incoming ACF Options Page to be modeled
*
* @throws Exception Throws Exception.
*/
public function __construct( array $options_page ) {
$this->data = $options_page;
parent::__construct();
}

/**
* @return array
*/
public function get_data(): array {
return $this->data;
}

/**
* @return void
*/
protected function init(): void {
if ( empty( $this->fields ) ) {
$this->fields = [
'slug' => $this->data['menu_slug'] ?? null,
'pageTitle' => $this->data['page_title'] ?? null,
'menuTitle' => $this->data['menu_title'] ?? null,
'parentId' => function () {
$type_name = Utils::format_type_name( \WPGraphQLAcf\Utils::get_field_group_name( $this->data ) );
return ! empty( $this->data['parent_slug'] ) ? Relay::toGlobalId( 'acf_options_page', $this->data['parent_slug'] ) : null;
},
'id' => function () {
return Relay::toGlobalId( 'acf_options_page', $this->data['menu_slug'] );
},
'acfId' => 'options',
];
}
}
}
155 changes: 101 additions & 54 deletions src/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

use Exception;
use GraphQL\Error\Error;
use GraphQL\Type\Definition\ResolveInfo;
use WPGraphQL\AppContext;
use WPGraphQL\Registry\TypeRegistry;
use WPGraphQL\Utils\Utils;
use WPGraphQLAcf\Data\Loader\AcfOptionsPageLoader;
use WPGraphQLAcf\Model\AcfOptionsPage;

class Registry {

Expand Down Expand Up @@ -76,24 +80,7 @@ public function has_registered_field_group( string $key ): bool {
* @return bool
*/
public function should_field_group_show_in_graphql( array $acf_field_group ): bool {

$should = true;

if ( isset( $acf_field_group['active'] ) && false === $acf_field_group['active'] ) {
$should = false;
}

// if the field group was configured with no "show_in_graphql" value, default to the "show_in_rest" value
// to determine if the group should be available in an API
if ( ! isset( $acf_field_group['show_in_graphql'] ) ) {
$acf_field_group['show_in_graphql'] = $acf_field_group['show_in_rest'] ?? false;
}

if ( isset( $acf_field_group['show_in_graphql'] ) && false === $acf_field_group['show_in_graphql'] ) {
$should = false;
}

return (bool) apply_filters( 'graphql_acf_should_field_group_show_in_graphql', $should, $acf_field_group );
return \WPGraphQLAcf\Utils::should_field_group_show_in_graphql( $acf_field_group );
}

/**
Expand Down Expand Up @@ -269,6 +256,26 @@ public function register_initial_graphql_types(): void {
],
],
] );

register_graphql_interface_type( 'AcfOptionsPage', [
'interfaces' => [ 'Node' ],
'description' => __( 'Options Page registered by ACF', 'wp-graphql-acf' ),
'fields' => [
'id' => [
'type' => [ 'non_null' => 'ID' ],
],
'pageTitle' => [
'type' => 'String',
],
'menuTitle' => [
'type' => 'String',
],
'parentId' => [
'type' => 'String',
],
],
] );

}

/**
Expand All @@ -277,6 +284,7 @@ public function register_initial_graphql_types(): void {
* @param array $acf_field_group The ACF Field Group config
*
* @return array
* @throws Error
*/
public function get_field_group_interfaces( array $acf_field_group ): array {

Expand Down Expand Up @@ -307,6 +315,76 @@ public function get_field_group_interfaces( array $acf_field_group ): array {

}

/**
* @return void
* @throws Error
* @throws Exception
*/
public function register_options_pages():void {

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

$graphql_options_pages = acf_get_options_pages();

if ( empty( $graphql_options_pages ) ) {
return;
}

foreach ( $graphql_options_pages as $graphql_options_page ) {
if ( ! $this->should_field_group_show_in_graphql( $graphql_options_page ) ) {
continue;
}

$type_name = $this->get_field_group_graphql_type_name( $graphql_options_page );

if ( empty( $type_name ) ) {
continue;
}

register_graphql_object_type( $type_name, [
'interfaces' => [ 'AcfOptionsPage' ],
'model' => AcfOptionsPage::class,
'fields' => [
'id' => [
'type' => [ 'non_null' => 'ID' ],
],
'pageTitle' => [
'type' => 'String',
],
],
] );

$field_name = Utils::format_field_name( $type_name );

$interface_name = 'WithAcfOptionsPage' . $type_name;

register_graphql_interface_type( $interface_name, [
'description' => sprintf( __( 'Access point for the "%s" ACF Options Page', 'wp-graphql-acf' ), $type_name ),
'fields' => [
$field_name => [
'type' => $type_name,
'resolve' => function ( $source, $args, AppContext $context, ResolveInfo $info ) use ( $graphql_options_page ) {

$loader = $context->get_loader( 'acf_options_page' );

if ( ! $loader instanceof AcfOptionsPageLoader ) {
return 'null';
}

return $context->get_loader( 'acf_options_page' )->load_deferred( $graphql_options_page['menu_slug'] );
},
],
],
]);

register_graphql_interfaces_to_types( [ $interface_name ], [ 'RootQuery' ] );

}

}

/**
* @param array $acf_field_group
*
Expand Down Expand Up @@ -391,42 +469,7 @@ public function map_acf_field_to_graphql( array $acf_field, array $acf_field_gro
* @throws \GraphQL\Error\Error
*/
public function get_field_group_name( array $field_group ): string {

$field_group_name = '';



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 );
} else {
if ( ! empty( $field_group['name'] ) ) {
$field_group_name = $field_group['name'];
} elseif ( ! empty( $field_group['title'] ) ) {
$field_group_name = $field_group['title'];
} elseif ( ! empty( $field_group['label'] ) ) {
$field_group_name = $field_group['label'];
}
$field_group_name = preg_replace( '/[^0-9a-zA-Z_\s]/i', ' ', $field_group_name );
// if the graphql_field_name isn't explicitly defined, we'll format it without underscores
$field_group_name = Utils::format_field_name( $field_group_name, false );
}

if ( empty( $field_group_name ) ) {
return $field_group_name;
}

$starts_with_string = is_numeric( substr( $field_group_name, 0, 1 ) );

if ( $starts_with_string ) {
graphql_debug( __( 'The ACF Field or Field Group could not be added to the schema. GraphQL Field and Type names cannot start with a number', 'wp-graphql-acf' ), [
'invalid' => $field_group,
] );
return '';
}

return $field_group_name;

return \WPGraphQLAcf\Utils::get_field_group_name( $field_group );
}

/**
Expand Down Expand Up @@ -507,6 +550,10 @@ public function get_location_rules( array $acf_field_groups = [] ): array {
*/
public function get_graphql_locations_for_field_group( array $field_group, array $acf_field_groups ): array {

if ( ! $this->should_field_group_show_in_graphql( $field_group ) ) {
return [];
}

$graphql_types = $field_group['graphql_types'] ?? [];

$field_group_name = $field_group['graphql_field_name'] ?? $field_group['title'];
Expand Down
4 changes: 4 additions & 0 deletions src/ThirdParty.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ public function init(): void {
$acfe = new ThirdParty\AcfExtended\AcfExtended();
$acfe->init();

// Initialize support for WPGraphQL Smart Cache
$smart_cache = new ThirdParty\WPGraphQLSmartCache\WPGraphQLSmartCache();
$smart_cache->init();

}

}
Loading