Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: page link field type #221

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# Changelog

## 2.3.0

## Update Notice

This release refactored some internals regarding how Clone fields and Group fields behave. There was no intentional breaking changes to the Schema, but if you are using Clone and Group fields there is a chance that if you were benefiting from a "bug as a feature" there might be some changes that could impact your Schema and/or resolvers, we recommend testing this update on a staging site to ensure things are still working for you as expected. Should you run into any problems, please [open a new issue](https://github.com/wp-graphql/wpgraphql-acf/issues/new/choose) and provide as much detail as possible to help us reproduce the scenario. Thanks! 🙏

## New Features

- [#193](https://github.com/wp-graphql/wpgraphql-acf/pull/193): feat: improved handling of clone and group fields

## Chores / Bugfixes

- [#194](https://github.com/wp-graphql/wpgraphql-acf/pull/194): ci: test against WordPress 6.5

## 2.2.0

## New Features
Expand Down
747 changes: 399 additions & 348 deletions composer.lock

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ Learn more about how [Appsero collects and uses data](https://appsero.com/privac

== Upgrade Notice ==

= 2.3.0 =

This release refactored some internals regarding how Clone fields and Group fields behave. There was no intentional breaking changes to the Schema, but if you are using Clone and Group fields there is a chance that if you were benefiting from a "bug as a feature" there might be some changes that could impact your Schema and/or resolvers, we recommend testing this update on a staging site to ensure things are still working for you as expected. Should you run into any problems, please [open a new issue](https://github.com/wp-graphql/wpgraphql-acf/issues/new/choose) and provide as much detail as possible to help us reproduce the scenario. Thanks! 🙏

= 2.1.0 =

While fixing some [performance issues](https://github.com/wp-graphql/wpgraphql-acf/pull/152) we had to adjust the fallback logic for mapping ACF Field Groups to the Schema if they do not have "graphql_types" defined.
Expand All @@ -116,6 +120,12 @@ This release is a complete re-architecture of WPGraphQL for ACF, introducing bre

== Changelog ==

= 2.3.0 =

**New Features**

**Chores / Bugfixes**

= 2.2.0 =

**New Features**
Expand Down
7 changes: 4 additions & 3 deletions src/AcfGraphQLFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,10 @@ public function get_admin_field_settings( array $field, Settings $settings ) {
$admin_fields = $this->get_admin_fields( $field, $default_admin_settings, $settings );

// Remove excluded fields
if ( isset( $this->config['exclude_admin_fields'] ) && is_array( $this->config['exclude_admin_fields'] ) ) {
foreach ( $this->config['exclude_admin_fields'] as $excluded ) {
unset( $admin_fields[ $excluded ] );
$excluded_fields = $this->get_config( 'exclude_admin_fields' );
if ( ! empty( $excluded_fields ) && is_array( $excluded_fields ) ) {
foreach ( $excluded_fields as $excluded_field ) {
unset( $admin_fields[ $excluded_field ] );
}
}

Expand Down
73 changes: 71 additions & 2 deletions src/FieldType/PageLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

use WPGraphQL\Acf\AcfGraphQLFieldType;
use WPGraphQL\Acf\FieldConfig;
use WPGraphQL\AppContext;
use WPGraphQL\Model\Post;

class PageLink {

Expand All @@ -15,12 +17,79 @@ public static function register_field_type(): void {
[
'exclude_admin_fields' => [ 'graphql_non_null' ],
'admin_fields' => static function ( $admin_fields, $field, $config, \WPGraphQL\Acf\Admin\Settings $settings ): array {
return Relationship::get_admin_fields( $admin_fields, $field, $config, $settings );
$admin_fields[] = [
'type' => 'select',
'name' => 'graphql_resolve_as',
'label' => __( 'Resolve As', 'wpgraphql-acf' ),
'choices' => [
'single' => __( 'String', 'wpgraphql-acf' ),
'list' => __( 'List of Strings', 'wpgraphql-acf' ),
],
'default_value' => 'list',
'instructions' => __( 'Select whether the field should be presented in the schema as a List of Strings that can return 0, 1 or more URLs, or a String that will return 1 URL or null. Changing this field will change the GraphQL Schema and could cause breaking changes.', 'wpgraphql-acf' ),
'conditions' => [],
];

return $admin_fields;
},
'graphql_type' => static function ( FieldConfig $field_config, AcfGraphQLFieldType $acf_field_type ) {
return Relationship::get_graphql_type( $field_config, $acf_field_type );
$acf_field = $field_config->get_acf_field();
$resolve_type = $acf_field['graphql_resolve_as'] ?? 'list';
if ( 'single' === $resolve_type ) {
return 'String';
}
return [ 'list_of' => 'String' ];
},
'resolve' => static function ( $root, $args, AppContext $context, $info, $field_type, FieldConfig $field_config ) {
$value = $field_config->resolve_field( $root, $args, $context, $info );
$acf_field = $field_config->get_acf_field();
$resolve_type = $acf_field['graphql_resolve_as'] ?? 'list';

$urls = [];

if ( is_array( $value ) ) {
$urls = array_filter(
array_map(
static function ( $v ) {
if ( is_numeric( $v ) ) {
$post = get_post( absint( $v ) );
$post_model = $post instanceof \WP_Post ? new Post( $post ) : null;
$v = $post_model instanceof Post ? $post_model->link : null;
}
return self::get_relative_url( $v );
},
$value
)
);
} elseif ( is_numeric( $value ) ) {
$post = get_post( absint( $value ) );
$post_model = $post instanceof \WP_Post ? new Post( $post ) : null;
$link = $post_model instanceof Post ? $post_model->link : null;
$urls[] = self::get_relative_url( $link );
} else {
$urls[] = self::get_relative_url( $value );
}

if ( 'single' === $resolve_type ) {
return $urls[0] ?? null;
}

return $urls;
},
]
);
}

/**
* Get the relative URL from a full URL
*
* @param string|null $url The full URL
* @return string|null The relative URL or null if the URL is empty
*/
private static function get_relative_url( ?string $url ): ?string {
if ( home_url() === $url ) {
return '/';
}
return $url ? str_replace( home_url(), '', $url ) : null;
}
}
11 changes: 11 additions & 0 deletions tests/_support/WPUnit/WPGraphQLAcfTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public function setUp(): void {
$this->clearSchema();
Utils::clear_field_type_registry();

$this->set_permalink_structure( '/%postname%/' );
$active_plugins = get_option( 'active_plugins' );

// whether the tests are being run with ACF pro or not
Expand Down Expand Up @@ -206,6 +207,16 @@ public function setUp(): void {

}

/**
* @param string $structure
*/
public function set_permalink_structure( $structure = '' ) {
global $wp_rewrite;
$wp_rewrite->init();
$wp_rewrite->set_permalink_structure( $structure );
$wp_rewrite->flush_rules();
}

/**
* @return string
*/
Expand Down
24 changes: 8 additions & 16 deletions tests/wpunit/FieldTypes/PageLinkFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ public function get_field_type(): string {
return 'page_link';
}

public function get_expected_field_resolve_type(): ?string {
return 'AcfContentNodeConnection';
public function get_expected_field_resolve_kind(): ?string {
return 'LIST';
}

public function get_expected_field_resolve_kind(): ?string {
return 'OBJECT';
public function get_expected_field_of_type(): ?array {
return [
'name' => 'String',
];
}

/**
Expand All @@ -42,12 +44,7 @@ public function get_clone_value_to_save(): int {
public function get_acf_clone_fragment(): string {
return '
fragment AcfTestGroupFragment on AcfTestGroup {
clonedTestPageLink {
nodes {
__typename
databaseId
}
}
clonedTestPageLink
}
';
}
Expand All @@ -57,12 +54,7 @@ public function get_acf_clone_fragment(): string {
*/
public function get_expected_clone_value(): array {
return [
'nodes' => [
[
'__typename' => 'Post',
'databaseId' => $this->published_post->ID,
]
]
'/test-post-title/'
];
}

Expand Down
4 changes: 2 additions & 2 deletions wpgraphql-acf.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Description: WPGraphQL for ACF seamlessly integrates Advanced Custom Fields with WPGraphQL.
* Author: WPGraphQL, Jason Bahl
* Author URI: https://www.wpgraphql.com
* Version: 2.2.0
* Version: 2.3.0
* Text Domain: wpgraphql-acf
* Requires PHP: 7.3
* Requires at least: 5.9
Expand All @@ -31,7 +31,7 @@
}

if ( ! defined( 'WPGRAPHQL_FOR_ACF_VERSION' ) ) {
define( 'WPGRAPHQL_FOR_ACF_VERSION', '2.2.0' );
define( 'WPGRAPHQL_FOR_ACF_VERSION', '2.3.0' );
}

if ( ! defined( 'WPGRAPHQL_FOR_ACF_VERSION_WPGRAPHQL_REQUIRED_MIN_VERSION' ) ) {
Expand Down
Loading