-
Notifications
You must be signed in to change notification settings - Fork 14
/
forms-3rdparty-integration.php
734 lines (605 loc) · 25.9 KB
/
forms-3rdparty-integration.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
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
<?php
/*
Plugin Name: Forms: 3rd-Party Integration
Plugin URI: https://github.com/zaus/forms-3rdparty-integration
Description: Send plugin Forms Submissions (Gravity, CF7, Ninja Forms, etc) to a 3rd-party URL
Author: zaus, atlanticbt, spkane
Version: 1.6.6.1
Author URI: http://drzaus.com
Changelog:
1.4 - forked from cf7-3rdparty. Removed 'hidden field plugin'.
1.4.1 - minor cleanup, bugfixes; added 'label' and 'drag' columns to admin ui.
1.4.2 - bugfixes (CF7, empty admin sections), admin JS cleanup, timeout
1.4.3 - cleaning up admin JS, plugin header warning
1.4.4 - protecting against non-attached forms; github issue link; extra hooks
1.4.5 - fixing response failure message notification
1.4.6 - post args hook + bypass, fix arg-by-reference
1.4.7 - totally removing hidden field plugin; js fixes; stripslashes
1.4.8 - fixes for github issue-6 (php5 strict constructor) and issue-8 (configurable multiple values for same key)
1.4.9 - matching cf7 v3.9
1.6.0 - better fplugin base, ninjaforms integration (1.5); refactored gf/cf7 to use fplugin base
1.6.1 - upgrade path
1.6.3 - fixes, updates, ff bugfix
1.6.4 - conditional submission hook
1.6.4.2 - including original $submission in `service_filter_post` hook
1.6.4.3 - fix escape slashes in GF
1.6.5/.1 - github issue #43, indexed placeholder; github #27; added service to `get_submission` hook
1.6.6 - postbox open toggle, issue #35
1.6.6.1 - adding debug message bypass hook
*/
//declare to instantiate
Forms3rdPartyIntegration::$instance = new Forms3rdPartyIntegration;
// handle plugin upgrades
// http://codex.wordpress.org/Function_Reference/register_activation_hook#Examples
include_once dirname( __FILE__ ) . '/upgrade.php';
$ugrader = new Forms3rdPartyIntegrationUpgrade();
$ugrader->register(__FILE__);
class Forms3rdPartyIntegration {
#region =============== CONSTANTS AND VARIABLE NAMES ===============
const pluginPageTitle = 'Forms: 3rd Party Integration';
const pluginPageShortTitle = '3rdparty Services';
/**
* Admin - role capability to view the options page
* @var string
*/
const adminOptionsCapability = 'manage_options';
/**
* Version of current plugin -- match it to the comment
* @var string
*/
const pluginVersion = '1.6.6.1';
/**
* Self-reference to plugin name
* @var string
*/
private $N;
/**
* Namespace the given key
* @param string $key the key to namespace
* @return the namespaced key
*/
public function N($key = false) {
// nothing provided, return namespace
if( ! $key || empty($key) ) { return $this->N; }
return sprintf('%s_%s', $this->N, $key);
}
/**
* Parameter index for mapping - administrative label (reminder)
*/
const PARAM_LBL = 'lbl';
/**
* Parameter index for mapping - source plugin (i.e. GravityForms, CF7, etc)
*/
const PARAM_SRC = 'src';
/**
* Parameter index for mapping - 3rdparty destination
*/
const PARAM_3RD = '3rd';
/**
* How long (seconds) before considering timeout
*/
const DEFAULT_TIMEOUT = 10;
/**
* Singleton
* @var object
*/
public static $instance = null;
#endregion =============== CONSTANTS AND VARIABLE NAMES ===============
#region =============== CONSTRUCTOR and INIT (admin, regular) ===============
// php5 constructor must come first for 'strict standards' -- http://wordpress.org/support/topic/redefining-already-defined-constructor-for-class-wp_widget
function __construct() {
$this->N = __CLASS__;
add_action( 'admin_menu', array( &$this, 'admin_init' ), 20 ); // late, so it'll attach menus farther down
add_action( 'init', array( &$this, 'init' ) ); // want to run late, but can't because it misses CF7 onsend?
} // function
function Forms3rdPartyIntegration() {
$this->__construct();
} // function
function admin_init() {
# perform your code here
//add_action('admin_menu', array(&$this, 'config_page'));
//add plugin entry settings link
add_filter( 'plugin_action_links', array(&$this, 'plugin_action_links'), 10, 2 );
//needs a registered page in order for the above link to work?
#$pageName = add_options_page("Custom Shortcodes - ABT Options", "Shortcodes -ABT", self::adminOptionsCapability, 'abt-shortcodes-config', array(&$this, 'submenu_config'));
if ( function_exists('add_submenu_page') ){
// autoattach to cf7, gravityforms
$subpagesOf = apply_filters($this->N('declare_subpages'), array());
foreach($subpagesOf as $plugin) {
$page = add_submenu_page(/*'plugins.php'*/$plugin, __(self::pluginPageTitle), __(self::pluginPageShortTitle), self::adminOptionsCapability, basename(__FILE__,'.php').'-config', array(&$this, 'submenu_config'));
//add admin stylesheet, etc
add_action('admin_print_styles-' . $page, array(&$this, 'add_admin_headers'));
}
//register options
$default_options = array(
'debug' => array('email'=>get_bloginfo('admin_email'), 'separator'=>', ')
, 0 => array(
'name'=>'Service 1'
, 'url'=>plugins_url('3rd-parties/service_test.php', __FILE__)
, 'success'=>''
, 'forms' => array()
, 'timeout' => self::DEFAULT_TIMEOUT // timeout in seconds
, 'mapping' => array(
array(self::PARAM_LBL=>'The submitter name',self::PARAM_SRC=>'your-name', self::PARAM_3RD=>'name')
, array(self::PARAM_LBL=>'The email address', self::PARAM_SRC=>'your-email', self::PARAM_3RD=>'email')
)
)
);
add_option( $this->N('settings'), $default_options );
}
} // function
/**
* General init
* Add scripts and styles
* but save the enqueue for when the shortcode actually called?
*/
function init(){
// needed here because both admin and before-send functions require v()
/// TODO: more intelligently include...
include_once('includes.php');
#wp_register_script('jquery-flip', plugins_url('jquery.flip.min.js', __FILE__), array('jquery'), self::pluginVersion, true);
#wp_register_style('sponsor-flip', plugins_url('styles.css', __FILE__), array(), self::pluginVersion, 'all');
#
#if( !is_admin() ){
# /*
# add_action('wp_print_header_scripts', array(&$this, 'add_headers'), 1);
# add_action('wp_print_footer_scripts', array(&$this, 'add_footers'), 1);
# */
# wp_enqueue_script('jquery-flip');
# wp_enqueue_script('sponsor-flip-init');
# wp_enqueue_style('sponsor-flip');
#}
// allow extensions; remember to check !is_admin
do_action($this->N('init'), false);
if(!is_admin()){
//add_action('wp_footer', array(&$this, 'shortcode_post_slider_add_script')); //jedi way to add shortcode scripts
}
}
#endregion =============== CONSTRUCTOR and INIT (admin, regular) ===============
#region =============== HEADER/FOOTER -- scripts and styles ===============
/**
* Add admin header stuff
* @see http://codex.wordpress.org/Function_Reference/wp_enqueue_script#Load_scripts_only_on_plugin_pages
*/
function add_admin_headers(){
wp_enqueue_script($this->N('admin'), plugins_url('plugin.admin.js', __FILE__), array('jquery', 'jquery-ui-sortable'), self::pluginVersion, true);
wp_localize_script($this->N('admin'), $this->N('admin'), array(
'N' => $this->N()
));
$stylesToAdd = array(
basename(__FILE__,'.php') => 'plugin.admin.css' //add a stylesheet with the key matching the filename
);
// Have to manually add to in_footer
// Check if script is done, if not, then add to footer
foreach($stylesToAdd as $handle => $stylesheet){
wp_enqueue_style(
$handle //id
, plugins_url($stylesheet, __FILE__) //file
, array() //dependencies
, self::pluginVersion //version
, 'all' //media
);
}
}//--- function add_admin_headers
/**
* Only add scripts and stuff if shortcode found on page
* TODO: figure out how this works -- global $wpdb not correct
* @source http://shibashake.com/wordpress-theme/wp_enqueue_script-after-wp_head
* @source http://old.nabble.com/wp-_-enqueue-_-script%28%29-not-working-while-in-the-Loop-td26818198.html
*/
function add_headers() {
//ignore the examples below
return;
if(is_admin()) return;
$stylesToAdd = array();
// Have to manually add to in_footer
// Check if script is done, if not, then add to footer
foreach($stylesToAdd as $style){
if (!in_array($style, $wp_styles->done) && !in_array($style, $wp_styles->in_footer)) {
$wp_styles->in_header[] = $style;
}
}
}//-- function add_headers
/**
* Only add scripts and stuff if shortcode found on page
* @see http://scribu.net/wordpress/optimal-script-loading.html
*/
function add_footers() {
if(is_admin()){
wp_enqueue_script($this->N('admin'));
return;
}
$scriptsToAdd = array( );
// Have to manually add to in_footer
// Check if script is done, if not, then add to footer
foreach($scriptsToAdd as $script){
if (!in_array($script, $wp_scripts->done) && !in_array($script, $wp_scripts->in_footer)) {
$wp_scripts->in_footer[] = $script;
}
}
}
#endregion =============== HEADER/FOOTER -- scripts and styles ===============
#region =============== Administrative Settings ========
private $_settings;
private $_services;
/**
* Return the plugin settings
*/
function get_settings($stashed = true){
// TODO: if this ever changes, make sure to correspondingly fix 'upgrade.php'
if( $stashed && isset($this->_settings) ) return $this->_settings;
$this->_settings = get_option($this->N('settings'));
// but we only want the actual settings, not the services
$this->_settings = $this->_settings['debug'];
return $this->_settings;
}//--- get_settings
/**
* Return the service configurations
*/
function get_services($stashed = true) {
if( $stashed && isset($this->_services) ) return $this->_services;
$this->_services = get_option($this->N('settings'));
// but we only want service listing, not the settings
// TODO: this will go away once we move to custom post type like CF7
unset($this->_services['debug']);
return $this->_services;
}
function save_services($services) {
$settings = $this->get_settings(false);
$merged = array('debug' => $settings) + (array)$services;
update_option($this->N('settings'), $merged);
$this->_services = $services; // replace stash
}
function save_settings($settings) {
$services = $this->get_services(false);
$merged = array('debug' => $settings) + (array)$services;
update_option($this->N('settings'), $merged);
$this->_settings = $settings; // replace stash
}
/**
* The submenu page
*/
function submenu_config(){
wp_enqueue_script($this->N('admin'));
include_once('plugin-ui.php');
}
/**
* HOOK - Add the "Settings" link to the plugin list entry
* @param $links
* @param $file
*/
function plugin_action_links( $links, $file ) {
if ( $file != plugin_basename( __FILE__ ) )
return $links;
$url = $this->plugin_admin_url( array( 'page' => basename(__FILE__, '.php').'-config' ) );
$settings_link = '<a title="Capability ' . self::adminOptionsCapability . ' required" href="' . esc_attr( $url ) . '">'
. esc_html( __( 'Settings', $this->N ) ) . '</a>';
array_unshift( $links, $settings_link );
return $links;
}
/**
* Copied from Contact Form 7, for adding the plugin link
* @param unknown_type $query
*/
function plugin_admin_url( $query = array() ) {
global $plugin_page;
if ( ! isset( $query['page'] ) )
$query['page'] = $plugin_page;
$path = 'admin.php';
if ( $query = build_query( $query ) )
$path .= '?' . $query;
$url = admin_url( $path );
return esc_url_raw( $url );
}
/**
* Helper to render a select list of available forms
* @param array $forms list of forms from functions like wpcf7_contact_forms()
* @param array $eid entry id - for multiple lists on page
* @param array $selected ids of selected fields
*/
public function form_select_input($forms, $eid, $selected){
?>
<select class="multiple" multiple="multiple" id="forms-<?php echo $eid?>" name="<?php echo $this->N?>[<?php echo $eid?>][forms][]">
<?php
foreach($forms as $f){
$form_id = $f['id'];
$form_title = $f['title']; // as serialized option data
?>
<option <?php if($selected && in_array($form_id, $selected)): ?>selected="selected" <?php endif; ?>value="<?php echo esc_attr( $form_id );?>"><?php echo esc_html( $form_title ); ?></option>
<?php
}// foreach
?>
</select>
<?php
}//-- end function form_select_input
#endregion =============== Administrative Settings ========
/**
* Prepare the service post with numerical placeholder, see github issue #43
* @param $post the service submission
*
* @see https://github.com/zaus/forms-3rdparty-integration/issues/43
*/
function placeholder_separator($post) {
$new = array(); // add results to new so we don't pollute the enumerator
// find the arrays and reformat keys with index
foreach($post as $f => $v) {
if(is_array($v)) {
// for each item in the submission array,
// get its numerical index and replace the
// placeholder in the destination field
foreach($v as $i => $p) {
$k = str_replace('%i', $i, $f);
$new[$k] = $p;
}
unset($post[$f]); // now remove original, since we need to reattach under a different key
}
}
return array_merge($post, $new);
}
/**
* Callback to perform before Form (i.e. Contact-Form-7, Gravity Forms) fires
* @param $form
*
* @see http://www.alexhager.at/how-to-integrate-salesforce-in-contact-form-7/
*/
function before_send($form){
###_log(__LINE__.':'.__FILE__, ' begin before_send', $form);
//get field mappings and settings
$services = $this->get_services();
// unlikely, but skip handling if nothing to do
if(empty($services)) return $form;
$debug = $this->get_settings();
// alias to submission data - in GF it's $_POST, in CF7 it's $cf7->posted_data
$submission = false;
//loop services
foreach($services as $sid => $service):
//check if we're supposed to use this service
if( !isset($service['forms']) || empty($service['forms']) ) continue; // nothing provided
// it's more like "use_this_service", actually...
$use_this_form = apply_filters($this->N('use_form'), false, $form, $sid, $service['forms']);
###_log('are we using this form?', $use_this_form ? "YES" : "NO", $sid, $service);
if( !$use_this_form ) continue;
// only get the submission once, now that we know we're going to use this service/form
if(false === $submission) $submission = apply_filters($this->N('get_submission'), array(), $form, $service);
// now we can conditionally check whether use the service based upon submission data
$use_this_form = apply_filters($this->N('use_submission'), $use_this_form, $submission, $sid);
if( !$use_this_form ) continue;
// populate the 3rdparty post args
$post = array();
$service['separator'] = $debug['separator']; // alias here for reporting
//find mapping
foreach($service['mapping'] as $mid => $mapping){
$third = $mapping[self::PARAM_3RD];
//is this static or dynamic (userinput)?
if(v($mapping['val'])){
$input = $mapping[self::PARAM_SRC];
}
else {
//check if we have that field in post data
if( !isset($submission[ $mapping[self::PARAM_SRC] ]) ) continue;
$input = $submission[ $mapping[self::PARAM_SRC] ];
}
//allow multiple values to attach to same entry
if( isset( $post[ $third ] ) ){
###echo "multiple @$mid - $fsrc, $third :=\n";
if(!is_array($post[$third])) {
$post[$third] = array($post[$third]);
}
$post[$third] []= $input;
}
else {
$post[$third] = $input;
}
}// foreach mapping
//extract special tags;
$post = apply_filters($this->N('service_filter_post_'.$sid), $post, $service, $form, $submission);
$post = apply_filters($this->N('service_filter_post'), $post, $service, $form, $sid, $submission);
// fix for multiple values
switch($service['separator']) {
case '[#]':
// don't do anything to include numerical index (default behavior of `http_build_query`)
break;
case '[%]':
// see github issue #43
$post = $this->placeholder_separator($post);
break;
case '[]':
// must build as querystring then strip `#` out of `[#]=`
$post = http_build_query($post);
$post = preg_replace('/%5B[0-9]+%5D=/', '%5B%5D=', $post);
break;
default:
// otherwise, find the arrays and implode
foreach($post as $f => &$v) {
###_log('checking array', $f, $v, is_array($v) ? 'array' : 'notarray');
if(is_array($v)) $v = implode($service['separator'], $v);
}
break;
}
###_log(__LINE__.':'.__FILE__, ' sending post to '.$service['url'], $post);
// change args sent to remote post -- add headers, etc: http://codex.wordpress.org/Function_Reference/wp_remote_post
// optionally, return an array with 'response_bypass' set to skip the wp_remote_post in favor of whatever you did in the hook
$post_args = apply_filters($this->N('service_filter_args')
, array(
'timeout' => empty($service['timeout']) ? self::DEFAULT_TIMEOUT : $service['timeout']
,'body'=>$post
)
, $service
, $form
);
//remote call
// once more conditionally check whether use the service based upon (mapped) submission data
if(false === $post_args) continue;
// optional bypass -- replace with a SOAP call, etc
elseif(isset($post_args['response_bypass'])) {
$response = $post_args['response_bypass'];
}
else {
//@see http://planetozh.com/blog/2009/08/how-to-make-http-requests-with-wordpress/
$response = wp_remote_post( $service['url'], $post_args );
}
###pbug(__LINE__.':'.__FILE__, ' response from '.$service['url'], $response);
### _log(__LINE__.':'.__FILE__, ' response from '.$service['url'], $submission, $post_args, $response);
$can_hook = true;
//if something went wrong with the remote-request "physically", warn
if (!is_array($response)) { //new occurrence of WP_Error?????
$response_array = array('safe_message'=>'error object', 'object'=>$response);
$form = $this->on_response_failure($form, $debug, $service, $post_args, $response_array);
$can_hook = false;
}
elseif(!$response
|| !isset($response['response'])
|| !isset($response['response']['code'])
|| ! apply_filters($this->N('is_success'), 200 <= $response['response']['code'] && $response['response']['code'] < 400)
) {
$response['safe_message'] = 'physical request failure';
$form = $this->on_response_failure($form, $debug, $service, $post_args, $response);
$can_hook = false;
}
//otherwise, check for a success "condition" if given
elseif(!empty($service['success'])) {
if(strpos($response['body'], $service['success']) === false){
$failMessage = array(
'reason'=>'Could not locate success clause within response'
, 'safe_message' => 'Success Clause not found'
, 'clause'=>$service['success']
, 'response'=>$response['body']
);
$form = $this->on_response_failure($form, $debug, $service, $post_args, $failMessage);
$can_hook = false;
}
}
if($can_hook){
###_log('performing hooks for:', $this->N.'_service_'.$sid);
//hack for pass-by-reference
//holder for callback return results
$callback_results = array('success'=>false, 'errors'=>false, 'attach'=>'', 'message' => '');
// TODO: use object?
$param_ref = array(); foreach($callback_results as $k => &$v){ $param_ref[$k] = &$v; }
//allow hooks
do_action($this->N('service_a'.$sid), $response['body'], $param_ref);
do_action($this->N('service'), $response['body'], $param_ref, $sid);
###_log('after success', $form);
//check for callback errors; if none, then attach stuff to message if requested
if(!empty($callback_results['errors'])){
$failMessage = array(
'reason'=>'Service Callback Failure'
, 'safe_message' => 'Service Callback Failure'
, 'errors'=>$callback_results['errors']);
$form = $this->on_response_failure($form, $debug, $service, $post_args, $failMessage);
}
else {
###_log('checking for attachments', print_r($callback_results, true));
$form = apply_filters($this->N('remote_success'), $form, $callback_results, $service);
}
}// can hook
//forced debug contact
if($debug['mode'] == 'debug'){
$this->send_debug_message($debug['email'], $service, $post_args, $response, $submission);
}
endforeach; //-- loop services
###_log(__LINE__.':'.__FILE__, ' finished before_send', $form);
// some plugins expected usage is as filter, so return (modified?) form
return $form;
}//--- end function before_send
/**
* How to send the debug message
* @param string $email recipient
* @param array $service service options
* @param array $post details sent to 3rdparty
* @param object $response the response object
* @param object $submission the form submission
* @return void n/a
*/
private function send_debug_message($email, $service, $post, $response, $submission){
// allow hooks to bypass, if for example we're not getting debug emails or we want to use some fancy logging service
$passthrough = apply_filters($this->N('debug_message'), true, $service, $post, $submission, $response);
// did the debug message send?
if( !$passthrough || !wp_mail( $email
, self::pluginPageTitle . " Debug: {$service['name']}"
, "*** Service ***\n".print_r($service, true)."\n*** Post (Form) ***\n" . get_bloginfo('url') . $_SERVER['REQUEST_URI'] . "\n".print_r($submission, true)."\n*** Post (to Service) ***\n".print_r($post, true)."\n*** Response ***\n".print_r($response, true)
, array('From: "'.self::pluginPageTitle.' Debug" <'.$this->N.'-debug@' . str_replace('www.', '', $_SERVER['HTTP_HOST']) . '>')
) ) {
///TODO: log? another email? what?
error_log( sprintf("%s:%s could not send F3P debug email (to: %s) for service %s", __LINE__, __FILE__, $email, $service['url']) );
}
}
/**
* Add a javascript warning for failures; also send an email to debugging recipient with details
* parameters passed by reference mostly for efficiency, not actually changed (with the exception of $form)
*
* @param $form reference to CF7 plugin object - contains mail details etc
* @param $debug reference to this plugin "debug" option array
* @param $service reference to service settings
* @param $post_args reference to service post data
* @param $response reference to remote-request response
*/
private function on_response_failure($form, $debug, $service, $post_args, $response){
// failure hooks; pass-by-value
$form = apply_filters($this->N('remote_failure'), $form, $debug, $service, $post_args, $response);
return $form;
}//--- end function on_response_failure
/**
* Email helper for `remote_failure` hooks
* @param array $service service configuration
* @param array $debug the debug settings (to get email)
* @param array $post details sent to 3rdparty
* @param array $response remote-request response
* @param string $form_title name of the form used
* @param string $form_recipient email of the original form recipient, so we know who to follow up with
* @param debug_from_id short identifier of the Form plugin which failed (like 'CF7' or 'GF', etc)
*
* @return true if the warning email sent, false otherwise
*/
public function send_service_error(&$service, &$debug, &$post, &$response, $form_title, $form_recipient, $debug_from_id) {
$body = sprintf('There was an error when trying to integrate with the 3rd party service {%2$s} (%3$s).%1$s%1$s**FORM**%1$sTitle: %6$s%1$sIntended Recipient: %7$s%1$sSource: %8$s%1$s%1$s**SUBMISSION**%1$s%4$s%1$s%1$s**RAW RESPONSE**%1$s%5$s'
, "\n"
, $service['name']
, $service['url']
, print_r($post, true)
, print_r($response, true)
, $form_title
, $form_recipient
, get_bloginfo('url') . $_SERVER['REQUEST_URI']
);
$subject = sprintf('%s-3rdParty Integration Failure: %s'
, $debug_from_id
, $service['name']
);
$headers = array(
sprintf('From: "%1$s-3rdparty Debug" <%1$s-3rdparty-debug@%2$s>'
, $debug_from_id
, str_replace('www.', '', $_SERVER['HTTP_HOST'])
)
);
//log if couldn't send debug email
if(wp_mail( $debug['email'], $subject, $body, $headers )) return true;
###$form->additional_settings .= "\n".'on_sent_ok: \'alert("Could not send debug warning '.$service['name'].'");\'';
error_log(__LINE__.':'.__FILE__ .' response failed from '.$service['url'].', could not send warning email: ' . print_r($response, true));
return false;
}//-- fn send_service_error
/**
* Format the configured service failure message using the $response "safe message" and the plugin's original error message
* @param array $service service configuration
* @param array $response remote-request response
* @param string $original_message the plugin's original failure message
* @return the newly formatted failure message
*/
public function format_failure_message(&$service, &$response, $original_message) {
return sprintf(
__($service['failure'], $this->N())
, $original_message
, __($response['safe_message'], $this->N())
);
}
}//end class
/*
// some servers need at least one 'sacrificial' `error_log` call to make _log call work???
error_log('f3p-after-declare:' . $_SERVER["REQUEST_URI"]);
if(!function_exists('_log')) {
function _log($args) {
$args = func_get_args();
error_log( print_r($args, true) );
}
}
*/