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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"phpunit/phpunit": "^9.5",
"slevomat/coding-standard": "^8.9",
"simpod/php-coveralls-mirror": "^3.0",
"phpstan/extension-installer": "^1.3"
"phpstan/extension-installer": "^1.3",
"php-stubs/acf-pro-stubs": "6.*"
},
"config": {
"platform": {
Expand Down
46 changes: 45 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ parameters:
level: 8
inferPrivatePropertyTypeFromConstructor: true
checkMissingIterableValueType: false
treatPhpDocTypesAsCertain: false
stubFiles:
# Simulate added properties
- phpstan/class-wp-post-type.stub
- phpstan/class-wp-taxonomy.stub
- phpstan/class-wp-dependency.stub
bootstrapFiles:
- vendor/php-stubs/acf-pro-stubs/acf-pro-stubs.php
- phpstan/constants.php
- wp-graphql-acf.php
- access-functions.php
Expand Down
4 changes: 0 additions & 4 deletions src/Admin/PostTypeRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public function add_tabs( array $tabs ): array {
*/
public function render_settings_tab( array $acf_post_type ): void {

// @phpstan-ignore-next-line
acf_render_field_wrap(
[
'type' => 'true_false',
Expand All @@ -81,7 +80,6 @@ public function render_settings_tab( array $acf_post_type ): void {

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

// @phpstan-ignore-next-line
acf_render_field_wrap(
[
'type' => 'text',
Expand Down Expand Up @@ -111,7 +109,6 @@ public function render_settings_tab( array $acf_post_type ): void {

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

// @phpstan-ignore-next-line
acf_render_field_wrap(
[
'type' => 'text',
Expand Down Expand Up @@ -223,7 +220,6 @@ public function add_graphql_type_column( array $columns ): array {
*/
public function render_graphql_columns( string $column_name, int $post_id ): void {

// @phpstan-ignore-next-line
$post_type = acf_get_internal_post_type( $post_id, 'acf-post-type' );

// if there's no post type, bail early
Expand Down
19 changes: 6 additions & 13 deletions src/Admin/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,11 @@ protected function setup_field_settings(): void {
add_action( 'acf/render_field_settings', [ $this, 'add_field_settings' ] );
} else {

// @phpstan-ignore-next-line
$acf_field_types = acf_get_field_types();

// We want to add settings to _all_ field types
$acf_field_types = array_keys( acf_get_field_types() );
$acf_field_types = is_array( $acf_field_types ) && ! empty( $acf_field_types ) ? array_keys( $acf_field_types ) : [];

if ( ! empty( $acf_field_types ) ) {

Expand Down Expand Up @@ -199,7 +202,6 @@ public function display_graphql_field_group_fields( $field_group ): void {
}

// Render a field in the Field Group settings to allow for a Field Group to be shown in GraphQL.
// @phpstan-ignore-next-line
acf_render_field_wrap(
[
'label' => __( 'Show in GraphQL', 'wp-graphql-acf' ),
Expand All @@ -216,7 +218,6 @@ public function display_graphql_field_group_fields( $field_group ): void {
);

// Render a field in the Field Group settings to set the GraphQL field name for the field group.
// @phpstan-ignore-next-line
acf_render_field_wrap(
[
'label' => __( 'GraphQL Type Name', 'wp-graphql-acf' ),
Expand All @@ -233,7 +234,6 @@ public function display_graphql_field_group_fields( $field_group ): void {
true
);

// @phpstan-ignore-next-line
acf_render_field_wrap(
[
'label' => __( 'Manually Set GraphQL Types for Field Group', 'wp-graphql-acf' ),
Expand All @@ -251,7 +251,6 @@ public function display_graphql_field_group_fields( $field_group ): void {

$choices = Utils::get_all_graphql_types();

// @phpstan-ignore-next-line
acf_render_field_wrap(
[
'label' => __( 'GraphQL Types to Show the Field Group On', 'wp-graphql-acf' ),
Expand All @@ -272,8 +271,6 @@ public function display_graphql_field_group_fields( $field_group ): void {
$interfaces = $this->get_registry()->get_field_group_interfaces( $field_group );
$field_group_type_name = $this->get_registry()->get_field_group_graphql_type_name( $field_group );


// @phpstan-ignore-next-line
acf_render_field_wrap(
[
'label' => __( 'GraphQL Interfaces', 'wp-graphql-acf' ),
Expand Down Expand Up @@ -366,7 +363,6 @@ public function add_field_settings( array $field, ?string $field_type = null ):
// Merge the default field setting with the passed in field setting
$setting_field_config = array_merge( $default_config, $admin_field_setting_config );

// @phpstan-ignore-next-line
acf_render_field_setting( $field, $setting_field_config, (bool) $setting_field_config['global'] );
}
}
Expand Down Expand Up @@ -505,25 +501,23 @@ public function wpgraphql_admin_table_columns_html( string $column_name, int $po
echo null;
}

// @phpstan-ignore-next-line
$field_group = acf_get_field_group( $post_id );

if ( empty( $field_group ) ) {
echo null;
return;
}

switch ( $column_name ) {
case 'acf-wpgraphql-type':
$type_name = $this->get_registry()->get_field_group_graphql_type_name( $field_group );

// @phpstan-ignore-next-line
echo '<span class="acf-wpgraphql-type">' . acf_esc_html( $type_name ) . '</span>';
echo ! empty( $type_name ) ? '<span class="acf-wpgraphql-type">' . acf_esc_html( $type_name ) . '</span>' : '';
break;
case 'acf-wpgraphql-interfaces':
$interfaces = $this->get_registry()->get_field_group_interfaces( $field_group );
$html = Utils::array_list_by_limit( $interfaces, 5 );

// @phpstan-ignore-next-line
echo '<span class="acf-wpgraphql-interfaces">' . acf_esc_html( $html ) . '</span>';
break;
case 'acf-wpgraphql-locations':
Expand All @@ -532,7 +526,6 @@ public function wpgraphql_admin_table_columns_html( string $column_name, int $po
if ( $locations ) {
$html = Utils::array_list_by_limit( $locations, 5 );

// @phpstan-ignore-next-line
echo '<span class="acf-wpgraphql-location-types">' . acf_esc_html( $html ) . '</span>';
}
break;
Expand Down
4 changes: 0 additions & 4 deletions src/Admin/TaxonomyRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public function add_tabs( array $tabs ): array {
*/
public function render_settings_tab( array $acf_taxonomy ): void {

// @phpstan-ignore-next-line
acf_render_field_wrap(
[
'type' => 'true_false',
Expand All @@ -80,7 +79,6 @@ public function render_settings_tab( array $acf_taxonomy ): void {

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

// @phpstan-ignore-next-line
acf_render_field_wrap(
[
'type' => 'text',
Expand Down Expand Up @@ -109,7 +107,6 @@ public function render_settings_tab( array $acf_taxonomy ): void {

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

// @phpstan-ignore-next-line
acf_render_field_wrap(
[
'type' => 'text',
Expand Down Expand Up @@ -221,7 +218,6 @@ public function add_graphql_type_column( array $columns ): array {
*/
public function render_graphql_columns( string $column_name, int $post_id ): void {

// @phpstan-ignore-next-line
$post_type = acf_get_internal_post_type( $post_id, 'acf-taxonomy' );

// if there's no post type, bail early
Expand Down
58 changes: 33 additions & 25 deletions src/FieldConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,17 +301,16 @@ public function should_format_field_value( string $field_type ): bool {
*/
public function resolve_field( $root, array $args, AppContext $context, ResolveInfo $info ) {

// @todo: Handle options pages??
$field_config = $info->fieldDefinition->config['acf_field'] ?? $this->acf_field;

$node = $root['node'] ?? null;
$node_id = $node ? Utils::get_node_acf_id( $node ) : null;
$node = $root['node'] ?? null;
$node_id = $node ? Utils::get_node_acf_id( $node, $field_config ) : null;

$field_key = null;
$is_cloned = false;

if ( ! empty( $field_config['cloned_key'] ) ) {
$field_key = $field_config['cloned_key'];
// if the field is cloned
if ( ! empty( $field_config['_clone'] ) ) {
$field_key = $field_config['_clone'];
$is_cloned = true;
} elseif ( ! empty( $field_config['key'] ) ) {
$field_key = $field_config['key'];
Expand All @@ -324,11 +323,13 @@ public function resolve_field( $root, array $args, AppContext $context, ResolveI
if ( $is_cloned ) {
if ( isset( $field_config['_name'] ) && ! empty( $node_id ) ) {
$field_key = $field_config['_name'];
} elseif ( isset( $field_config['cloned_key'] ) ) {
$field_key = $field_config['cloned_key'];
} elseif ( ! empty( $field_config['__key'] ) ) {
$field_key = $field_config['__key'];
}
// @phpstan-ignore-next-line
$field_config = acf_get_field( $field_key );

$cloned_field_config = acf_get_field( $field_key );
$field_config = ! empty( $cloned_field_config ) ? $cloned_field_config : $field_config;

}

$should_format_value = false;
Expand All @@ -350,11 +351,6 @@ public function resolve_field( $root, array $args, AppContext $context, ResolveI
return $this->prepare_acf_field_value( $root[ $field_key ], $node, $node_id, $field_config );
}

// If there's no node_id at this point, we can return null
if ( empty( $node_id ) ) {
return null;
}

/**
* Filter the field value before resolving.
*
Expand All @@ -363,15 +359,29 @@ public function resolve_field( $root, array $args, AppContext $context, ResolveI
* @param mixed|string|int $node_id The ACF ID of the node to resolve the field with
* @param array $acf_field The ACF Field config
* @param bool $format Whether to apply formatting to the field
* @param string $field_key The key of the field being resolved
*/
$value = apply_filters( 'wpgraphql/acf/pre_resolve_acf_field', null, $root, $node_id, $field_config, $should_format_value );
$pre_value = apply_filters( 'wpgraphql/acf/pre_resolve_acf_field', null, $root, $node_id, $field_config, $should_format_value, $field_key );

// If the filter has returned a value, we can return the value that was returned.
if ( null !== $value ) {
return $value;
if ( null !== $pre_value ) {
return $pre_value;
}

// resolve block field
if ( is_array( $node ) && isset( $node['blockName'] ) ) {

$fields = acf_setup_meta( $node['attrs']['data'], 0, true );
acf_reset_meta();

return $fields[ $field_config['name'] ] ?? null;
}

// If there's no node_id at this point, we can return null
if ( empty( $node_id ) ) {
return null;
}

// @phpstan-ignore-next-line
$value = get_field( $field_key, $node_id, $should_format_value );
$value = $this->prepare_acf_field_value( $value, $root, $node_id, $field_config );

Expand Down Expand Up @@ -433,7 +443,7 @@ public function prepare_acf_field_value( $value, $root, $node_id, ?array $acf_fi

// @todo: This was ported over, but I'm not 💯 sure what this is solving and
// why it's only applied on options pages and not other pages 🤔
if ( is_array( $root ) && ! ( ! empty( $root['type'] ) && 'options_page' === $root['type'] ) && isset( $acf_field_config['key'] ) && isset( $root[ $acf_field_config['key'] ] ) ) {
if ( isset( $acf_field_config['key'], $root[ $acf_field_config['key'] ] ) && is_array( $root ) && ! ( ! empty( $root['type'] ) && 'options_page' === $root['type'] ) ) {
$value = $root[ $acf_field_config['key'] ];
if ( 'wysiwyg' === $acf_field_config['type'] ) {
$value = apply_filters( 'the_content', $value );
Expand All @@ -460,15 +470,13 @@ public function prepare_acf_field_value( $value, $root, $node_id, ?array $acf_fi
}

/**
* @param string $from_type
* @param string $to_type
* @param string $from_field_name
*
* @return string
*/
public function get_connection_name( string $from_type, string $to_type, string $from_field_name ): string {
public function get_connection_name( string $to_type ): string {
// Create connection name using $from_type + To + $to_type + Connection.
return \WPGraphQL\Utils\Utils::format_type_name( ucfirst( $from_type ) . ucfirst( $from_field_name ) . 'To' . ucfirst( $to_type ) . 'Connection' );
return \WPGraphQL\Utils\Utils::format_type_name( 'Acf' . ucfirst( $to_type ) . 'Connection' );
}

/**
Expand All @@ -487,7 +495,7 @@ public function register_graphql_connections( array $config ): void {
return;
}

$connection_name = $this->get_connection_name( $type_name, $to_type, $this->get_graphql_field_name() );
$connection_name = $this->get_connection_name( $to_type );

$connection_config = array_merge( [
'description' => $this->get_field_description(),
Expand Down
6 changes: 2 additions & 4 deletions src/FieldType/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ public static function register_field_type(): void {
return null;
}

$type_name = $field_config->get_graphql_field_group_type_name();
$to_type = 'MediaItem';
$connection_name = $field_config->get_connection_name( $type_name, $to_type, $field_config->get_graphql_field_name() );
$type_name = $field_config->get_graphql_field_group_type_name();
$to_type = 'MediaItem';

$field_config->register_graphql_connections( [
'description' => $field_config->get_field_description(),
Expand All @@ -34,7 +33,6 @@ public static function register_field_type(): void {
'fromType' => $type_name,
'toType' => $to_type,
'fromFieldName' => $field_config->get_graphql_field_name(),
'connectionTypeName' => $connection_name,
'oneToOne' => true,
'resolve' => function ( $root, $args, AppContext $context, ResolveInfo $info ) use ( $field_config ) {

Expand Down
Loading