-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-gf-on24-addon.php
357 lines (326 loc) · 12.9 KB
/
class-gf-on24-addon.php
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<?php
GFForms::include_feed_addon_framework();
class GFOn24AddOn extends GFFeedAddOn
{
protected $_version = GF_ON24_ADDON_VERSION;
protected $_min_gravityforms_version = '1.9.16';
protected $_slug = 'gravityforms-addon-on24';
protected $_path = 'gravityforms-addon-on24/gravityforms-addon-on24.php';
protected $_full_path = __FILE__;
protected $_title = 'Gravity Forms On24 Add-On';
protected $_short_title = 'On24 Add-On';
private static $_instance = null;
/**
* Get an instance of this class.
*
* @return GFOn24AddOn
*/
public static function get_instance()
{
if (self::$_instance == null) {
self::$_instance = new GFOn24AddOn();
}
return self::$_instance;
}
/**
* Plugin starting point. Handles hooks and loading of language files.
*/
public function init()
{
parent::init();
}
/**
* Process the feed e.g. subscribe the user to a list.
*
* @param array $feed The feed object to be processed.
* @param array $entry The entry object currently being processed.
* @param array $form The form object currently being processed.
*
* @return bool|void
*/
public function process_feed($feed, $entry, $form)
{
// Get the event ID and key by grabbing the last two sections of the URL
$parsedFeed = array_slice(explode('/', $feed['meta']['eventUrl']), -2, 2);
if (count($parsedFeed) !== 2 || ! $parsedFeed[0] || ! $parsedFeed[1]) {
$this->log_debug('[On24] Invalid $eventUrl: ' . print_r($feed['meta']['eventUrl'], true));
return;
}
$apiUrl = 'https://event.on24.com/utilApp/r?eventid='
. $parsedFeed[0] . '&key='
. $parsedFeed[1];
$apiUrl = filter_var($apiUrl, FILTER_SANITIZE_URL);
if (filter_var($apiUrl, FILTER_VALIDATE_URL) === false) {
$this->log_debug('[On24] Invalid $apiUrl: ' . print_r($apiUrl, true));
return;
}
// Retrieve the name => value pairs for all fields mapped in the 'mappedFields' field map.
$field_map = $this->get_field_map_fields($feed, 'mappedFields');
// Loop through the fields from the field map setting building an array of values to be passed to the third-party service.
$merge_vars = [];
foreach ($field_map as $name => $field_id) {
// Get the field value for the specified field id
$merge_vars[$name] = $this->get_field_value($form, $entry, $field_id);
}
// Send the values to the third-party service.
$this->log_debug('[On24] Sending: ' . print_r($merge_vars, true));
$request = new WP_Http();
$response = $request->post($apiUrl, ['body' => $merge_vars]);
$this->log_debug('[On24] Received: ' . print_r($response, true));
// Add note to the entry
$this->add_note(
$entry['id'],
"Data posted to {$apiUrl}\r\n"
. print_r($response['response'], true)
);
return;
}
/**
* Return the scripts which should be enqueued.
*
* @return array
*/
public function scripts()
{
// $scripts = array(
// array(
// 'handle' => 'my_script_js',
// 'src' => $this->get_base_url() . '/js/my_script.js',
// 'version' => $this->_version,
// 'deps' => array( 'jquery' ),
// 'strings' => array(
// 'first' => esc_html__('First Choice', 'on24addon'),
// 'second' => esc_html__('Second Choice', 'on24addon'),
// 'third' => esc_html__('Third Choice', 'on24addon'),
// ),
// 'enqueue' => array(
// array(
// 'admin_page' => array( 'form_settings' ),
// 'tab' => 'on24addon',
// ),
// ),
// ),
// );
$scripts = [];
return array_merge(parent::scripts(), $scripts);
}
/**
* Return the stylesheets which should be enqueued.
*
* @return array
*/
public function styles()
{
// $styles = [
// [
// 'handle' => 'styles',
// 'src' => $this->get_base_url() . '/css/styles.css',
// 'version' => $this->_version,
// 'enqueue' => [
// ['field_types' => ['poll',],],
// ],
// ],
// ];
$styles = [];
return array_merge(parent::styles(), $styles);
}
/**
* Creates a custom page for this add-on.
*/
public function plugin_page()
{
echo <<<EOT
<p style="max-width: 65ch;">Enable On24 for a particular form by adding a new feed in the form's settings, under "On24 Add-On." There you can set the On24 event ID and key, map form fields to On24 fields and add conditional processing rules.</p>
<p><a href="?page=gf_edit_forms">Browse forms</a></p>
EOT;
}
/**
* Configures the settings which should be rendered on the add-on settings tab.
*
* @return array
*/
// public function plugin_settings_fields()
// {
// // return [
// // [
// // 'title' => esc_html__('On24 Settings', 'on24addon'),
// // 'fields' => [
// // [
// // 'name' => 'textbox',
// // 'tooltip' => esc_html__('This is the tooltip', 'on24addon'),
// // 'label' => esc_html__('This is the label', 'on24addon'),
// // 'type' => 'text',
// // 'class' => 'small',
// // ],
// // ],
// // ],
// // ];
// }
/**
* Configures the settings which should be rendered on the feed edit page in the Form Settings > On24 Add-On area.
*
* @return array
*/
public function feed_settings_fields()
{
$stdTooltip = __('Ensure the ON24 custom fields (i.e., "std1") are used consistently across events. <a href="https://on24.my.site.com/Support/s/article/Connect-Registration-Form-Best-Practices" target="_blank">Learn more</a>', 'on24addon');
return [[
'title' => esc_html__('On24 Settings', 'on24addon'),
'fields' => [
[
'label' => esc_html__('Feed name', 'on24addon'),
'type' => 'text',
'name' => 'feedName',
'tooltip' => esc_html__('Enter a name to identify this feed. It is not public.', 'on24addon'),
'class' => 'small',
'required' => 1,
],
[
'label' => esc_html__('Webinar Audience Event URL', 'on24addon'),
'type' => 'textarea',
'name' => 'eventUrl',
'tooltip' => esc_html__('Copy the event\'s Audience URL from the "Event URLs" section in On24.', 'on24addon'),
'class' => 'small',
'required' => 1,
'allow_html' => true,
],
[
'name' => 'mappedFields',
'label' => esc_html__('Map Fields', 'on24addon'),
'type' => 'field_map',
'field_map' => [
[
'name' => 'email',
'label' => esc_html__('Email', 'on24addon'),
'required' => 1,
'field_type' => [ 'email', 'hidden' ],
],
[
'name' => 'firstname',
'label' => esc_html__('First Name', 'on24addon'),
'required' => 0,
],
[
'name' => 'lastname',
'label' => esc_html__('Last Name', 'on24addon'),
'required' => 0,
],
[
'name' => 'job_title',
'label' => esc_html__('Job Title', 'on24addon'),
'required' => 0,
],
[
'name' => 'company',
'label' => esc_html__('Company', 'on24addon'),
'required' => 0,
],
[
'name' => 'work_phone',
'label' => esc_html__('Work Phone', 'on24addon'),
'required' => 0,
'field_type' => 'phone',
],
[
'name' => 'state',
'label' => esc_html__('State', 'on24addon'),
'required' => 0,
],
[
'name' => 'std1',
'label' => esc_html__('std1', 'on24addon'),
'required' => 0,
'tooltip' => $stdTooltip,
],
[
'name' => 'std2',
'label' => esc_html__('std2', 'on24addon'),
'required' => 0,
'tooltip' => $stdTooltip,
],
[
'name' => 'std3',
'label' => esc_html__('std3', 'on24addon'),
'required' => 0,
'tooltip' => $stdTooltip,
],
[
'name' => 'std4',
'label' => esc_html__('std4', 'on24addon'),
'required' => 0,
'tooltip' => $stdTooltip,
],
[
'name' => 'std5',
'label' => esc_html__('std5', 'on24addon'),
'required' => 0,
'tooltip' => $stdTooltip,
],
[
'name' => 'std6',
'label' => esc_html__('std6', 'on24addon'),
'required' => 0,
'tooltip' => $stdTooltip,
],
[
'name' => 'std7',
'label' => esc_html__('std7', 'on24addon'),
'required' => 0,
'tooltip' => $stdTooltip,
],
[
'name' => 'std8',
'label' => esc_html__('std8', 'on24addon'),
'required' => 0,
'tooltip' => $stdTooltip,
],
[
'name' => 'std9',
'label' => esc_html__('std9', 'on24addon'),
'required' => 0,
'tooltip' => $stdTooltip,
],
[
'name' => 'std10',
'label' => esc_html__('std10', 'on24addon'),
'required' => 0,
'tooltip' => $stdTooltip,
],
],
],
[
'name' => 'condition',
'label' => esc_html__('Conditional Processing', 'on24addon'),
'type' => 'feed_condition',
'checkbox_label' => esc_html__('Enable conditional processing', 'on24addon'),
'instructions' => esc_html__('Only send to On24 if', 'on24addon'),
],
],
]];
}
/**
* Configures which columns should be displayed on the feed list page.
*
* @return array
*/
public function feed_list_columns()
{
return array(
'feedName' => esc_html__('Feed', 'on24addon'),
'eventUrl' => esc_html__('On24 URL', 'on24addon'),
);
}
/**
* Prevent feeds being listed or created if an api key isn't valid.
*
* @return bool
*/
public function can_create_feed()
{
// // Get the plugin settings.
// $settings = $this->get_plugin_settings();
// // Access a specific setting e.g. an api key
// $key = rgar($settings, 'apiKey');
return true;
}
}