-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgravityforms-external-data-fields.php
More file actions
513 lines (428 loc) · 17.4 KB
/
Copy pathgravityforms-external-data-fields.php
File metadata and controls
513 lines (428 loc) · 17.4 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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
<?php
/*
Plugin Name: Gravity Forms External Data Fields
Plugin URI: https://github.com/BellevueCollege/gravityforms-external-data-fields
Description: Extend Gravity Forms with Bellevue College data api data
Author: Bellevue College Integration Team
Version: 3.0.0
Author URI: http://www.bellevuecollege.edu
GitHub Plugin URI: bellevuecollege/gravityforms-external-data-fields
*/
defined ( 'ABSPATH' ) OR exit;
require_once( plugin_dir_path( __FILE__ ) . 'classes/config.php' );
require_once( "classes/shortcode.php" );
require_once( "classes/studentData.php" );
require_once( "classes/employeeData.php" );
//error_reporting ( E_ALL ^ E_NOTICE ); // Report all errors except E_NOTICE
class GFEDF {
const STATUS_OK = 'ok';
protected $studentdata;
protected $empdata;
protected $shortcode;
/**
* Aggregated external data lookup status for the current request.
* null | ok | not_found | unavailable
*
* @var string|null
*/
protected $externalDataStatus = null;
/**
* Whether the current page shortcode restricts access to employees.
*
* @var bool
*/
protected $requiresEmployeeAccess = false;
/**
* Dynamic population parameter names provided by this plugin.
*
* @var array
*/
protected $externalDataParameters = array(
'bc_sid',
'bc_first_name',
'bc_last_name',
'bc_email',
'bc_dayphone',
'bc_evephone',
);
function __construct() {
$this->studentdata = new StudentData();
$this->empdata = null;
$this->shortcode = new GFEDF_Shortcode();
// This action needs to run AFTER the user has been authenticated
//don't run this if user is on admin/backend page
if ( !is_admin() ) {
add_action ( 'wp', array( $this, 'after_auth_action' ), 10 );
}
//autofill function filters
add_filter( 'gform_field_value_bc_sid', array( $this, 'populate_bc_sid' ) );
add_filter( 'gform_field_value_bc_first_name', array( $this, 'populate_bc_first_name' ) );
add_filter( 'gform_field_value_bc_last_name', array( $this, 'populate_bc_last_name' ) );
add_filter( 'gform_field_value_bc_email', array( $this, 'populate_bc_email' ) );
add_filter( 'gform_field_value_bc_dayphone', array( $this, 'populate_bc_dayphone' ) );
add_filter( 'gform_field_value_bc_evephone', array( $this, 'populate_bc_evephone' ) );
add_filter( 'gform_get_form_filter', array( $this, 'maybe_block_form_display' ), 10, 2 );
add_filter( 'gform_validation', array( $this, 'maybe_block_form_submission' ) );
add_filter( 'allowed_redirect_hosts', array( $this, 'allow_bellevue_redirect_host' ), 10, 2 );
//shortcode filters
add_shortcode( 'gfedf_redirect_to_login', array( $this, 'gfedf_login_redirect' ) );
}
// Add shortcode for redirect to login
// allows attribute 'class' that will be used to style returned element
function gfedf_login_redirect( $atts , $content = null ) {
$redirect_url = wp_login_url( get_permalink() );
$attrs = shortcode_atts( array( 'class' => '' ), $atts );
$class_attr = "";
$content_val = "Log in";
if ( !empty( $attrs["class"] ) ){
$class_attr = "class='" . esc_attr( $attrs["class"] ) . "'";
}
if ( !empty( $content ) ) {
$content_val = esc_html( $content );
}
return "<a href='" . esc_url( $redirect_url ) . "' title='Log in' $class_attr>" . $content_val . "</a>";
}
###########################################
// Filter functions to pre-populate fields
###########################################
// To use: add the string after gform_field_value_
// Ex. bc_sid or bc_first_name
// Add desired string to "Allow field to be populated automatically" field in advanced tab of the Gravity Forms Form Editor
function populate_bc_sid( $value ) {
debug_log ( "(= gform_field_value_bc_sid =) Setting SID..." );
$bc_sid = '';
if ( $this->studentdata->has_student_record() )
$bc_sid = $this->studentdata->get_student_id();
else {
if ( $this->empdata != null )
$bc_sid = $this->empdata->get_sid();
}
debug_log( "...'$bc_sid'" );
return $bc_sid;
}
// First name
function populate_bc_first_name( $value ) {
debug_log ( "(= gform_field_value_bc_first_name =) Setting first name..." );
$bc_first_name = '';
if ( $this->studentdata->has_student_record() )
$bc_first_name = $this->studentdata->get_first_name();
else {
if ( $this->empdata != null )
$bc_first_name = $this->empdata->get_first_name();
}
debug_log( "...'$bc_first_name'" );
return $bc_first_name;
}
// Last name
function populate_bc_last_name( $value ) {
debug_log ( "(= gform_field_value_bc_last_name =) Setting last name..." );
$bc_last_name = '';
if ( $this->studentdata->has_student_record() )
$bc_last_name = $this->studentdata->get_last_name();
else {
if ( $this->empdata != null )
$bc_last_name = $this->empdata->get_last_name();
}
debug_log( "...'$bc_last_name'" );
return $bc_last_name;
}
// BC email
function populate_bc_email( $value ) {
debug_log ( "(= gform_field_value_bc_email =) Setting email..." );
$bc_email = '';
if ( $this->studentdata->has_student_record() )
$bc_email = $this->studentdata->get_email();
else {
if ( $this->empdata != null )
$bc_email = $this->empdata->get_email();
}
debug_log( "...'$bc_email'" );
return $bc_email;
}
// Day phone
function populate_bc_dayphone( $value ) {
debug_log ( "(= gform_field_value_bc_dayphone =) Setting daytime phone..." );
$bc_dayphone = '';
if ( $this->studentdata->has_student_record() )
$bc_dayphone = $this->studentdata->get_daytime_phone();
else {
if ( $this->empdata != null )
$bc_dayphone = $this->empdata->get_phone();
}
debug_log( "...'$bc_dayphone'" );
return $bc_dayphone;
}
// Evening phone
function populate_bc_evephone( $value ) {
debug_log ( "(= gform_field_value_bc_evephone =) Setting evening phone..." );
$bc_evephone = $this->studentdata->get_evening_phone();
debug_log( "...'$bc_evephone'" );
return $bc_evephone;
}
/**
* Whether the form uses any plugin external-data population parameters.
*
* @param array $form Gravity Forms form object.
* @return bool
*/
protected function form_uses_external_data_fields( $form ) {
$fields = rgar( $form, 'fields' );
if ( empty( $fields ) || ! is_array( $fields ) ) {
return false;
}
foreach ( $fields as $field ) {
if ( ! empty( $field->inputName ) && in_array( $field->inputName, $this->externalDataParameters, true ) ) {
return true;
}
if ( empty( $field->inputs ) || ! is_array( $field->inputs ) ) {
continue;
}
foreach ( $field->inputs as $input ) {
$input_name = rgar( $input, 'name' );
if ( ! empty( $input_name ) && in_array( $input_name, $this->externalDataParameters, true ) ) {
return true;
}
}
}
return false;
}
/**
* Whether post content embeds a Gravity Forms shortcode or block.
*
* @param string $content Post content.
* @return bool
*/
protected function content_has_gravity_form( $content ) {
return has_shortcode( $content, 'gravityform' )
|| has_shortcode( $content, 'gravityforms' )
|| has_block( 'gravityforms/form', $content );
}
/**
* Form ID from a gravityforms/form block tree.
*
* @param array $blocks Parsed blocks from parse_blocks().
* @return int
*/
protected function find_gravity_forms_block_form_id( $blocks ) {
foreach ( $blocks as $block ) {
$block_name = isset( $block['blockName'] ) ? $block['blockName'] : '';
if ( $block_name === 'gravityforms/form' && ! empty( $block['attrs']['formId'] ) ) {
return absint( $block['attrs']['formId'] );
}
if ( ! empty( $block['innerBlocks'] ) && is_array( $block['innerBlocks'] ) ) {
$inner_form_id = $this->find_gravity_forms_block_form_id( $block['innerBlocks'] );
if ( $inner_form_id ) {
return $inner_form_id;
}
}
}
return 0;
}
/**
* Resolve the embedded Gravity Forms form ID from shortcode or block.
*
* @param string $content Post content.
* @return int
*/
protected function get_embedded_form_id( $content ) {
$form_id = $this->shortcode->get_attribute_value( 'id' );
if ( ! empty( $form_id ) ) {
return absint( $form_id );
}
if ( ! has_block( 'gravityforms/form', $content ) ) {
return 0;
}
return $this->find_gravity_forms_block_form_id( parse_blocks( $content ) );
}
/**
* Whether this page needs a Data API lookup for the current request.
*
* @param string $content Post content.
* @return bool
*/
protected function page_needs_external_data_lookup( $content ) {
if ( $this->requiresEmployeeAccess ) {
return true;
}
$form_id = $this->get_embedded_form_id( $content );
if ( empty( $form_id ) || ! class_exists( 'GFAPI' ) ) {
return false;
}
$form = GFAPI::get_form( $form_id );
if ( empty( $form ) || is_wp_error( $form ) ) {
return false;
}
return $this->form_uses_external_data_fields( $form );
}
/**
* Aggregate student/employee lookup results into a single status.
*
* @return string ok|not_found|unavailable
*/
protected function aggregate_external_data_status() {
$has_employee = $this->empdata && $this->empdata->is_employee();
if ( $this->studentdata->has_student_record() || $has_employee ) {
return self::STATUS_OK;
}
$employee_unavailable = $this->empdata && ! $this->empdata->is_lookup_available();
if ( ! $this->studentdata->is_lookup_available() || $employee_unavailable ) {
return DataApi::STATUS_UNAVAILABLE;
}
return DataApi::STATUS_NOT_FOUND;
}
/**
* Human-readable message for the current blocking status.
*
* @return string
*/
protected function get_external_data_error_message() {
$service_desk_link = '<a href="https://www.bellevuecollege.edu/servicedesk">Bellevue College ITS Service Desk</a>';
if ( $this->externalDataStatus === DataApi::STATUS_UNAVAILABLE ) {
return "We are temporarily unable to load your information from college systems, so this form cannot be loaded. Please try again later. If the problem continues, contact the {$service_desk_link}.";
}
return "We could not find a matching student or employee record for your account, so this form cannot be loaded. If you believe this is an error, please contact the {$service_desk_link}.";
}
/**
* Whether dependent forms should be blocked for the current request.
*
* @return bool
*/
protected function should_block_external_data_form() {
// Fail closed: employee-only pages require a confirmed employee match.
if ( $this->requiresEmployeeAccess ) {
$is_employee = $this->empdata && $this->empdata->is_employee();
if ( ! $is_employee ) {
return true;
}
}
return $this->externalDataStatus === DataApi::STATUS_NOT_FOUND
|| $this->externalDataStatus === DataApi::STATUS_UNAVAILABLE;
}
/**
* Replace form markup when external data is missing or unavailable.
*
* @param string $form_string Generated form HTML.
* @param array $form Form object.
* @return string
*/
function maybe_block_form_display( $form_string, $form ) {
if ( ! $this->should_block_external_data_form() ) {
return $form_string;
}
if ( ! $this->requiresEmployeeAccess && ! $this->form_uses_external_data_fields( $form ) ) {
return $form_string;
}
// Reuse Gravity Forms validation styling for a familiar error presentation.
$message = wp_kses(
$this->get_external_data_error_message(),
array( 'a' => array( 'href' => true ) )
);
return "<div class='validation_error alert alert-danger'>" . $message . '</div>';
}
/**
* Reject submissions when external data is missing or unavailable.
*
* @param array $validation_result Gravity Forms validation result.
* @return array
*/
function maybe_block_form_submission( $validation_result ) {
$form = $validation_result['form'];
if ( ! $this->should_block_external_data_form() ) {
return $validation_result;
}
if ( ! $this->requiresEmployeeAccess && ! $this->form_uses_external_data_fields( $form ) ) {
return $validation_result;
}
$validation_result['is_valid'] = false;
// Standard Gravity Forms pattern: mark a field failed so the form-level error is shown.
if ( ! empty( $form['fields'][0] ) ) {
$form['fields'][0]->failed_validation = true;
$form['fields'][0]->validation_message = wp_kses(
$this->get_external_data_error_message(),
array( 'a' => array( 'href' => true ) )
);
}
$validation_result['form'] = $form;
return $validation_result;
}
/**
* Allow bellevuecollege.edu and its subdomains for wp_safe_redirect.
*
* @param string[] $hosts Allowed hosts.
* @param string $host Host being redirected to.
* @return string[]
*/
public function allow_bellevue_redirect_host( $hosts, $host ) {
if ( preg_match( '/(^|\.)bellevuecollege\.edu$/i', $host ) ) {
$hosts[] = $host;
}
return $hosts;
}
/*******
* Check if user authenticated, then gather data for user.
* Also checks if 'allow' is set to restrict type of user and redirects as necessary
* This function must be in 'wp' action so that global WP variables (i.e. $post) are available
*******/
function after_auth_action () {
debug_log ( "(wp) GF External Data Fields plugin :: after_auth_action" );
global $post;
if ( empty( $post ) || empty( $post->post_content ) ) {
return;
}
$content = $post->post_content;
if ( ! $this->content_has_gravity_form( $content ) ) {
return;
}
$this->shortcode->get_shortcode_from_content( $content );
$restrict_attr = GFEDF_Config::get_restrict_attr();
$restrict_value = $this->shortcode->get_attribute_value( $restrict_attr );
$this->requiresEmployeeAccess = ( ! empty( $restrict_value ) && strtolower( $restrict_value ) === 'employees' );
if ( ! is_user_logged_in() ) {
return;
}
// Skip remote Data API calls when the page does not use this plugin's features.
if ( ! $this->page_needs_external_data_lookup( $content ) ) {
return;
}
$current_user = wp_get_current_user();
$username = $current_user->user_login;
//$username = 'student.test@bellevuecollege.edu'; // DEBUGGING ONLY -- TEST ACCOUNT USERNAME
$student_only_domain = '@my.bellevuecollege.edu';
$is_student_only_account = substr_compare(
$username,
$student_only_domain,
-strlen( $student_only_domain ),
strlen( $student_only_domain ),
true
) === 0;
// Always query students first; my.bellevuecollege.edu accounts are student-only.
$this->studentdata = new StudentData( $username );
$isEmp = false;
if ( ! $is_student_only_account ) {
$this->empdata = new EmployeeData( $username );
$isEmp = $this->empdata->is_employee();
}
$this->externalDataStatus = $this->aggregate_external_data_status();
debug_log( "GFEDF :: aggregated external data status: " . $this->externalDataStatus );
/*
* Limited access for employees based on shortcode attribute.
* Redirect only when the lookup completed and the user is not an employee.
* During an API outage, fail closed via maybe_block_* instead of redirecting.
*/
if ( $this->requiresEmployeeAccess && ! $isEmp
&& $this->externalDataStatus !== DataApi::STATUS_UNAVAILABLE ) {
$redirect_url = $this->shortcode->get_attribute_value( GFEDF_Config::get_restrict_fail_redirect_attr() );
if ( empty( $redirect_url ) ) {
$redirect_url = GFEDF_Config::get_default_redirect_url();
}
if ( ! empty( $redirect_url ) ) {
wp_safe_redirect( $redirect_url );
exit;
}
}
}
}
if ( class_exists( 'GFEDF' ) ) {
//instantiate class
$gfedf_obj = new GFEDF();
}