-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgravityforms_custom_post_path.php
225 lines (179 loc) · 6.34 KB
/
gravityforms_custom_post_path.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
<?php
/*
Plugin Name: Custom Post Path For Gravity Forms
Plugin URI:
Description: Configure your Graivty Forms to post to a secondary location. Perfect for CRMs like Sales Force or Marketo
Version: 0.1
Author: Myke Bates - UpTrending
Author URI: http://uptrending.com/
Author Email: [email protected]
License:
Copyright 2013 UpTrending ([email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if( !class_exists( 'gravityforms_custom_post_path' ) ) :
class gravityforms_custom_post_path {
private static $instance;
public static function singleton()
{
if( !isset( self::$instance ) )
{
$class = __CLASS__;
self::$instance = new $class();
}
return self::$instance;
}
/*
* Constructor
*
*/
private function __construct()
{
add_action("gform_advanced_settings", array($this, "cpp_form_advanced_settings"), 10, 2);
add_action("gform_editor_js", array($this, "cpp_form_editor_script"));
add_filter('gform_tooltips', array($this, 'add_cpp_form_tooltips'), 0);
add_action("gform_field_advanced_settings", array($this, "cpp_field_settings"), 10, 2);
add_action("gform_editor_js", array($this, "editor_script"));
add_filter('gform_tooltips', array($this, 'add_cpp_field_tooltips'));
add_action("gform_post_submission", array($this, "send_post_content"), 10, 2);
}
public function cpp_form_advanced_settings($position, $form_id)
{
if($position == 800){
?>
<script type="text/javascript">
function cppSaveFormSetting(value){
form['cppFormField'] = value;
}
</script>
<li class="cpp_setting field_setting">
<label for="field_admin_label">
<?php _e("Custom Post Path", "gravityforms"); ?>
<?php gform_tooltip("cpp_form_value") ?>
</label>
<input type="text" id="cpp_form_value" class="fieldwidth-3" onkeyup="cppSaveFormSetting(this.value);" />
</li>
<?php
}
}
public function cpp_form_editor_script()
{
?>
<script type='text/javascript'>
//adding setting to fields of type "text"
fieldSettings["text"] += ", .cpp_setting";
//binding to the load field settings event to initialize the value
jQuery(document).bind("gform_load_form_settings", function(event, field, form){
jQuery("#cpp_form_value").attr("value", field["cppFormField"]);
});
</script>
<?php
}
//Filter to add a new tooltip
public function add_cpp_form_tooltips($tooltips)
{
$tooltips["cpp_form_value"] = "<h6>Custom Post Path</h6>Set additional path for the form to post to after it is saved.";
return $tooltips;
}
public function cpp_field_settings($position, $form_id)
{
//create settings on position 50 (right after Admin Label)
if($position == 50){
?>
<li class="cpp_field_setting field_setting">
<label for="field_admin_label">
<?php _e("Field Name For Custom Post Path", "gravityforms"); ?>
<?php gform_tooltip("cpp_field_value") ?>
</label>
<input type="text" id="cpp_field_value" size="35" onkeyup="SetFieldProperty('cppField', this.value);" />
</li>
<?php
}
}
public function editor_script()
{
?>
<script type='text/javascript'>
(function(){
jQuery.each(fieldSettings, function(index, value){
fieldSettings[index] += ", .cpp_field_setting";
});
})()
//binding to the load field settings event to initialize the values
jQuery(document).bind("gform_load_field_settings", function(event, field, form){
//console.log(Math.floor(Math.random()*11));
jQuery("#cpp_field_value").attr("value", field["cppField"]);
});
</script>
<?php
}
public function add_cpp_field_tooltips($tooltips)
{
$tooltips["cpp_field_value"] = "<h6>Field Name For Custom Post Path</h6>Set custom field name to pass to 3rd party form processor";
return $tooltips;
}
public function send_post_content($entry, $form)
{
$postPath = $form["cppFormField"];
$fields = $form["fields"];
$ourFields = array();
if($postPath){
// We have a custom post path set.
// Not sure if all fields shoudl post or only ones with explicit values
// Keeping explicit for meow
foreach ($fields as $field) {
if( $field["cppField"] ){
$fieldID = $field["id"];
$fieldValue = $entry["".$fieldID.""];
$fieldName = $field["cppField"];
// Check if this has multiple inputs(like a checkbox list)
// This is the first shot at trying to tackle the issue of multiple value fields
// Part of the problem is every service will handle this differently
// Right now just crafting a csv string
if(count($field["inputs"])){
$listValues = "";
foreach ($field["inputs"] as $input) {
$id = $input["id"];
if($entry["".$id.""]){
$listValues .= $entry["".$id.""].", ";
}
}
if($listValues)
$ourFields[$fieldName] = substr($listValues, 0, -2);
}else{
// Not a multi input value. Much easier to grab
$ourFields[$fieldName] = $fieldValue;
}
}
}
}
$this->postToUrl($postPath, $ourFields);
}
public function postToUrl($url, $data)
{
$fields = '';
foreach($data as $key => $value) {
$fields .= $key . '=' . $value . '&';
}
rtrim($fields, '&');
$post = curl_init();
curl_setopt($post, CURLOPT_URL, $url);
curl_setopt($post, CURLOPT_POST, count($data));
curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($post);
curl_close($post);
}
}
endif; //class_exists 'gravityforms_custom_post_path'
//Instantiate
gravityforms_custom_post_path::singleton();