Skip to content

Commit

Permalink
Merge branch 'feat/content-blocks-support'
Browse files Browse the repository at this point in the history
# Conflicts:
#	composer.json
#	composer.lock
#	src/FieldType/File.php
#	src/FieldType/FlexibleContent.php
#	src/FieldType/Gallery.php
#	src/FieldType/Image.php
#	src/LocationRules/LocationRules.php
#	src/Registry.php
#	src/ThirdParty.php
#	src/Utils.php
  • Loading branch information
jasonbahl committed Sep 5, 2023
2 parents 9ca9521 + 708a1dd commit 39a9243
Show file tree
Hide file tree
Showing 25 changed files with 418 additions and 138 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"slevomat/coding-standard": "^8.9",
"simpod/php-coveralls-mirror": "^3.0",
"phpstan/extension-installer": "^1.3",
"wp-cli/wp-cli-bundle": "^2.8"
"wp-cli/wp-cli-bundle": "^2.8",
"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
6 changes: 0 additions & 6 deletions src/Admin/PostTypeRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ public function add_tabs( array $tabs ): array {
* @return void
*/
public function render_settings_tab( array $acf_post_type ): void {

// @phpstan-ignore-next-line
acf_render_field_wrap(
[
'type' => 'true_false',
Expand All @@ -80,7 +78,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 @@ -110,7 +107,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 @@ -220,8 +216,6 @@ public function add_graphql_type_column( array $columns ): array {
* @return void
*/
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 @@ -113,8 +113,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 ) ) {
array_map(
Expand Down Expand Up @@ -212,7 +215,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 @@ -229,7 +231,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 @@ -246,7 +247,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 @@ -264,7 +264,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 @@ -285,8 +284,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 @@ -377,7 +374,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 @@ -501,25 +497,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 @@ -528,7 +522,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
6 changes: 0 additions & 6 deletions src/Admin/TaxonomyRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ public function add_tabs( array $tabs ): array {
* @return void
*/
public function render_settings_tab( array $acf_taxonomy ): void {

// @phpstan-ignore-next-line
acf_render_field_wrap(
[
'type' => 'true_false',
Expand All @@ -79,7 +77,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 @@ -108,7 +105,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 @@ -217,8 +213,6 @@ public function add_graphql_type_column( array $columns ): array {
* @return void
*/
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
57 changes: 31 additions & 26 deletions src/FieldConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,18 +299,16 @@ public function should_format_field_value( string $field_type ): bool {
* @return mixed
*/
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 @@ -323,11 +321,12 @@ 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 @@ -349,11 +348,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 @@ -362,15 +356,28 @@ 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 @@ -431,7 +438,7 @@ static function ( $opt ) {

// @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 Down Expand Up @@ -460,15 +467,13 @@ static function ( $opt ) {
}

/**
* @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 @@ -486,7 +491,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(
[
Expand Down
Loading

0 comments on commit 39a9243

Please sign in to comment.