-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-integration.php
More file actions
140 lines (113 loc) · 5.06 KB
/
class-integration.php
File metadata and controls
140 lines (113 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/**
* Manage the API integration between GravityForms and Fishbowl
*
* Upon a form submission to GravityForms, parse the data and submit it to the FishBowl API endpoint
* JSON payload will consist of a mix of user-entered form data and hardcoded account data for client's Fishbowl account.
*
* Fishbowl API Documentation: https://fishbowl-api.readme.io/reference/subscription (note: very sparse, not very helpful)
* Fishbowl technical point of contact: Brandon Flude <brandon@fishbowl.com>
*
* Mission-critical dependency: GravityForms fields MUST be labeled correctly: "First Name", "Email Address", etc, as that is how this plugin parses that data.
* Ideally this would be more robust, but have to skip in the interest of time. Other alternative would be to lookup based on field ID but that could be problematic
* given we're working with 20+ diferent forms.
*
*/
namespace GF_Fishbowl;
class Integration {
public function __construct() {
add_action( 'gform_after_submission', array( $this, 'submit_to_fishbowl' ), 10, 2 );
}
public static function submit_to_fishbowl( $entry, $form ) {
Utils::init_fishbowl_status($entry, $form);
// If Fishbowl integration is not enabled, exit
if ( ! get_option( 'fishbowl_integration_is_enabled' ) ) {
return;
}
// Fetch API account info from the admin settings page
$source = get_option( 'fishbowl_integration_source' );
$brand_uuid = get_option( 'fishbowl_brand_uuid' );
$brand_schema = get_option( 'fishbowl_brand_schema' );
$list_uuid = get_option( 'fishbowl_list_uuid' );
// User-entered form data
$firstName = Utils::get_field_value( $form, $entry, 'First Name', 'label' );
$lastName = Utils::get_field_value( $form, $entry, 'Last Name', 'label' );
$email = Utils::get_field_value( $form, $entry, 'Email Address', 'label' );
$phoneNumber = Utils::get_field_value( $form, $entry, 'Phone Number', 'label' ) ?: '0000000000';
$location = Utils::get_field_value( $form, $entry, 'Location', 'label' );
$birthday = Utils::get_field_value( $form, $entry, 'Birthday', 'label' ) ?: '0000-00-00';
$sport = Utils::get_field_value( $form, $entry, 'Favorite Sport', 'label' );
$options = Utils::get_field_value( $form, $entry, 'Options', 'label' ); // email & SMS opt-in, true/false
$options_field_id = Utils::get_field_id( $form, 'Options', 'label' );
if ( 'yes' == $entry[$options_field_id . '.1'] ) {
$options_value = true;
} else {
$options_value = false;
}
if ( 'yes-sms' == $entry[$options_field_id . '.2'] ) {
$options_value_sms = true;
} else {
$options_value_sms = false;
}
// Parse out birthday values
$birthdayMonth = substr($birthday, 5, 2);
$birthdayDay = substr($birthday, 8, 2);
$birthdayYear = substr($birthday, 0, 4);
$location_id = Utils::get_location_id( $location );
$sport_id = Utils::get_sport_id( $sport );
$zipCode = Utils::get_field_value( $form, $entry, 'Zipcode', 'label' ) ?: '00000';
// Check Source
$pageURL = Utils::get_field_value( $form, $entry, 'Page URL', 'label' );
// Set $source to "Scan to Win" if $pageURL contains the substring 'scan-to-win'
if ( strpos( $pageURL, 'scan-to-win' ) !== false ) {
$source = 'Scan to Win';
}
// Parse entry values into array
$data = array(
"firstName" => $firstName,
"lastName" => $lastName,
"email" => $email,
"phoneNumber" => $phoneNumber,
"zipCode" => $zipCode,
"location" => $location_id,
"birthdayMonth" => (int) $birthdayMonth, // per Fishbowl API specs, only birthdayMonth is an int
"birthdayDay" => $birthdayDay,
"birthdayYear" => $birthdayYear,
"favoriteSport" => $sport_id,
"receiveEmails" => (bool) $options_value,
"receiveSms" => (bool) $options_value_sms,
"source" => $source,
"brandUUID" => $brand_uuid,
"brandSchemaId" => $brand_schema,
"listUuid" => $list_uuid
);
$dataJson = json_encode($data);
gform_update_meta( $entry['id'], 'fishbowl_request_data_json', $dataJson );
gform_update_meta( $entry['id'], 'fishbowl_request_data', $data );
// create POST request to external API endpoint
$response = wp_remote_post( 'https://api.fishbowl.com/api/external/subscription', array(
'method' => 'POST',
'headers' => array(
'Accept' => 'application/json',
'Content-Type' => 'application/json'
),
'body' => $dataJson,
'timeout' => 10
) );
if ( is_wp_error( $response ) ) {
$response_code = $response->get_error_code();
$response_body = $response->get_error_message();
} else {
$response_code = wp_remote_retrieve_response_code( $response );
$response_body = wp_remote_retrieve_body( $response );
}
gform_update_meta( $entry['id'], 'fishbowl_response_code', $response_code );
gform_update_meta( $entry['id'], 'fishbowl_response_message', $response_body );
if ( 'Error: http_request_failed' === $response_code ) {
// Utils::update_fishbowl_status("Timeout Error", $entry, $form);
error_log("timeout");
} else {
Utils::update_fishbowl_status($response_code, $entry, $form);
}
}
}