-
Notifications
You must be signed in to change notification settings - Fork 1
/
action.php
93 lines (69 loc) · 1.92 KB
/
action.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
<?php if ( ! defined( 'ABSPATH' ) ) exit;
/**
* Class NFR_Action
*/
final class NFR_Action extends NF_Abstracts_Action
{
/**
* @var string
*/
protected $_name = 'nf_rest';
/**
* @var array
*/
protected $_tags = array();
/**
* @var string
*/
protected $_timing = 'late';
/**
* @var int
*/
protected $_priority = 10;
/**
* Constructor
*/
public function __construct()
{
parent::__construct();
$this->_nicename = __( 'Send to REST Endpoint', 'ninja-forms' );
$settings = include 'config.php';
$this->_settings = array_merge( $this->_settings, $settings );
}
/*
* PUBLIC METHODS
*/
public function save( $action_settings )
{
}
public function process( $action_settings, $form_id, $data )
{
$payload = array();
$fields = $data['fields'];
foreach($fields as $field){
$key = $field["key"];
$val = $field["value"];
$payload[$key] = $val;
}
$url = $action_settings['endpoint_url'];
$method = $action_settings['endpoint_method'];
$args = array(
'reject_unsafe_urls' => false,
'method' => $method,
'user-agent' => 'josegomezr/ninja-form',
'body' => $payload
);
$obj = wp_remote_request($url, $args);
$message = "";
if(is_wp_error($obj) || $obj['response']['code'] >= 300){
$message = do_shortcode( $action_settings['error_message'] );
}else{
$message = do_shortcode( $action_settings['success_message'] );
}
if( ! isset( $data[ 'actions' ] ) || ! isset( $data[ 'actions' ][ 'success_message' ] ) ) {
$data[ 'actions' ][ 'success_message' ] = '';
}
$data['actions']['success_message'] .= wpautop( $message );
return $data;
}
}