Skip to content
This repository was archived by the owner on Jul 23, 2024. It is now read-only.

Support for Advanced Custom Fields: Gravityforms Add-on #143

Open
wants to merge 21 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
b3cdd25
Merge pull request #126 from wp-graphql/release/v0.3.2
jasonbahl Apr 17, 2020
ccefe29
- Update version number for release
jasonbahl Apr 27, 2020
43372b3
Merge commit '966f6137007d44337305db9d2e3b4ac597227e68' into release/…
jasonbahl Apr 27, 2020
ea3b239
Merge pull request #129 from wp-graphql/release/v0.3.3
jasonbahl Apr 27, 2020
89f3adb
Fixes #76
Twansparant May 14, 2020
3c4bd38
Start adding gravityforms add-on support
Twansparant May 15, 2020
581f1b1
Add GravityForm object type
Twansparant May 15, 2020
187d4b3
Add gravity form object fields
Twansparant May 18, 2020
6a3b0fd
Add button types
Twansparant May 18, 2020
5369793
Add WPGraphQLGravityForms class
Twansparant May 18, 2020
f703401
Load WPGraphQLGravityForms class
Twansparant May 18, 2020
f3c2a62
Remove WPGraphQLGravityForms
Twansparant May 18, 2020
1f230a7
Add WPGraphQLGravityForms support
Twansparant May 18, 2020
1e763e0
Use Form type & data manipulator from WPGraphQLGravityForms
Twansparant May 18, 2020
eb9b5c6
Check if WPGraphQLGravityForms exists
Twansparant May 18, 2020
1af1503
Add correct check for wp-graphql-gravity-forms plugin
Twansparant May 18, 2020
6a066e7
Remove add_acf_fields_to_pages
Twansparant May 19, 2020
2522df3
Add add_acf_fields_to_pages support
Twansparant May 19, 2020
3d12947
Check for form id value
Twansparant May 22, 2020
c2ea099
Merge branch 'feature/field-group-page-template-locations' into featu…
Twansparant May 22, 2020
feee4e3
Remove add_acf_fields_to_pages
Twansparant May 25, 2020
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
126 changes: 100 additions & 26 deletions src/class-config.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,47 @@
use WPGraphQL\Model\Term;
use WPGraphQL\Model\User;

// WPGraphQLGravityForms classes
use WPGraphQLGravityForms\Types\Form\Form;
use WPGraphQLGravityForms\DataManipulators;
use WPGraphQLGravityForms\Types\Form\FormPagination;
use WPGraphQLGravityForms\Types\Form\FormNotification;
use WPGraphQLGravityForms\Types\Form\FormConfirmation;
use WPGraphQLGravityForms\Types\Form\SaveAndContinue;
use WPGraphQLGravityForms\Types\Union\ObjectFieldUnion;
use WPGraphQLGravityForms\Types\Button\Button;


/**
* Config class.
*/
class Config {

protected $type_registry;

/**
* FormDataManipulator instance.
*/
private $fields_data_manipulator;
private $form_data_manipulator;

/**
* Initialize WPGraphQL to ACF
*
* @param \WPGraphQL\Registry\TypeRegistry $type_registry Instance of the WPGraphQL TypeRegistry
*/
public function init( \WPGraphQL\Registry\TypeRegistry $type_registry ) {

/**
* Set the TypeRegistry
*/
$this->type_registry = $type_registry;

/**
* Set the DataManipulators
*/
$this->fields_data_manipulator = new DataManipulators\FieldsDataManipulator();
$this->form_data_manipulator = new DataManipulators\FormDataManipulator( $this->fields_data_manipulator );

/**
* Add ACF Fields to GraphQL Types
*/
Expand Down Expand Up @@ -332,7 +354,8 @@ public static function get_supported_fields() {
'color_picker',
'group',
'repeater',
'flexible_content'
'flexible_content',
'forms'
];

/**
Expand Down Expand Up @@ -812,79 +835,79 @@ protected function register_graphql_field( $type_name, $field_name, $config ) {
// ACF 5.8.6 added more data to Google Maps field value
// https://www.advancedcustomfields.com/changelog/
if (\acf_version_compare(acf_get_db_version(), '>=', '5.8.6')) {
$fields += [
'streetName' => [
$fields += [
'streetName' => [
'type' => 'String',
'description' => __( 'The street name associated with the map', 'wp-graphql-acf' ),
'resolve' => function( $root ) {
return isset( $root['street_name'] ) ? $root['street_name'] : null;
},
],
'streetNumber' => [
],
'streetNumber' => [
'type' => 'String',
'description' => __( 'The street number associated with the map', 'wp-graphql-acf' ),
'resolve' => function( $root ) {
return isset( $root['street_number'] ) ? $root['street_number'] : null;
},
],
'city' => [
],
'city' => [
'type' => 'String',
'description' => __( 'The city associated with the map', 'wp-graphql-acf' ),
'resolve' => function( $root ) {
return isset( $root['city'] ) ? $root['city'] : null;
},
],
'state' => [
],
'state' => [
'type' => 'String',
'description' => __( 'The state associated with the map', 'wp-graphql-acf' ),
'resolve' => function( $root ) {
return isset( $root['state'] ) ? $root['state'] : null;
},
],
'stateShort' => [
],
'stateShort' => [
'type' => 'String',
'description' => __( 'The state abbreviation associated with the map', 'wp-graphql-acf' ),
'resolve' => function( $root ) {
return isset( $root['state_short'] ) ? $root['state_short'] : null;
},
],
'postCode' => [
],
'postCode' => [
'type' => 'String',
'description' => __( 'The post code associated with the map', 'wp-graphql-acf' ),
'resolve' => function( $root ) {
return isset( $root['post_code'] ) ? $root['post_code'] : null;
},
],
'country' => [
],
'country' => [
'type' => 'String',
'description' => __( 'The country associated with the map', 'wp-graphql-acf' ),
'resolve' => function( $root ) {
return isset( $root['country'] ) ? $root['country'] : null;
},
],
'countryShort' => [
],
'countryShort' => [
'type' => 'String',
'description' => __( 'The country abbreviation associated with the map', 'wp-graphql-acf' ),
'resolve' => function( $root ) {
return isset( $root['country_short'] ) ? $root['country_short'] : null;
},
],
'placeId' => [
],
'placeId' => [
'type' => 'String',
'description' => __( 'The country associated with the map', 'wp-graphql-acf' ),
'resolve' => function( $root ) {
return isset( $root['place_id'] ) ? $root['place_id'] : null;
},
],
'zoom' => [
],
'zoom' => [
'type' => 'String',
'description' => __( 'The zoom defined with the map', 'wp-graphql-acf' ),
'resolve' => function( $root ) {
return isset( $root['zoom'] ) ? $root['zoom'] : null;
},
],
];
}
],
];
}

register_graphql_object_type(
$field_type_name,
Expand Down Expand Up @@ -1019,6 +1042,58 @@ protected function register_graphql_field( $type_name, $field_name, $config ) {
};
}
break;
/*
* Add acf-gravityforms-add-on support
*/
case 'forms' && class_exists( 'GFAPI' ) && class_exists( 'ACFGravityformsField\\Init' ) :
// GravityForm object
if ($acf_field['return_format'] === 'post_object' && is_plugin_active('wp-graphql-gravity-forms/wp-graphql-gravity-forms.php')) {
if ( empty( $acf_field['multiple'] ) ) {
$field_config['type'] = Form::TYPE;
$field_config['resolve'] = function( $root, $args ) use ( $acf_field ) {
$form = null;
$form_id = $this->get_acf_field_value( $root, $acf_field );

if ($form_id) {
$form_raw = \GFAPI::get_form($form_id);
$form = $this->form_data_manipulator->manipulate( $form_raw, $args );
}

return $form ? apply_filters( 'wp_graphql_gf_form_object', $form ) : null;
};
} else {
$field_config['type'] = [ 'list_of' => Form::TYPE ];
$field_config['resolve'] = function( $root, $args ) use ( $acf_field ) {
$forms = [];
$form_ids = $this->get_acf_field_value( $root, $acf_field );

if ( ! empty( $form_ids ) && is_array( $form_ids ) ) {
foreach ( $form_ids as $form_id ) {
if ($form_id) {
$form_raw = \GFAPI::get_form($form_id);
$form = apply_filters( 'wp_graphql_gf_form_object', $this->form_data_manipulator->manipulate( $form_raw, $args ) );
$forms[] = $form;
}
}
}

return ! empty( $forms ) ? $forms : [];
};
}
// Form id
} else {
if ( empty( $acf_field['multiple'] ) ) {
$field_config['type'] = 'Integer';
} else {
$field_config['type'] = [ 'list_of' => 'Integer' ];
$field_config['resolve'] = function( $root, $args ) use ( $acf_field ) {
$value = $this->get_acf_field_value( $root, $acf_field );

return ! empty( $value ) && is_array( $value ) ? $value : [];
};
}
}
break;
default:
break;
}
Expand Down Expand Up @@ -1727,5 +1802,4 @@ protected function add_acf_fields_to_options_pages() {
}
}
}

}
2 changes: 1 addition & 1 deletion wp-graphql-acf.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Author URI: https://www.wpgraphql.com
* Text Domain: wp-graphql-acf
* Domain Path: /languages
* Version: 0.3.2
* Version: 0.3.3
* Requires PHP: 7.0
* GitHub Plugin URI: https://github.com/afragen/github-updater
*
Expand Down