-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-gf-cloud-storage.php
287 lines (243 loc) · 10.3 KB
/
class-gf-cloud-storage.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
<?php
/*
* References:
* - https://docs.gravityforms.com/gffeedaddon/
* - https://docs.gravityforms.com/category/developers/php-api/add-on-framework/settings-api/
*/
GFForms::include_feed_addon_framework();
class GFCloudStorage extends GFFeedAddOn {
private $provider = 'Nextcloud';
protected $_version = GF_CLOUD_STORAGE_VERSION;
protected $_min_gravityforms_version = '1.9.16';
protected $_slug = 'gf-cloud-storage';
protected $_path = 'gf-cloud-storage/gf-cloud-storage.php';
protected $_full_path = __FILE__;
protected $_title = 'Gravity Forms Cloud Storage';
protected $_short_title = 'Cloud Storage';
private static $_instance = null;
/**
* Get an instance of this class.
*
* @return GFCloudStorage
*/
public static function get_instance() {
if ( self::$_instance == null ) {
self::$_instance = new GFCloudStorage();
}
return self::$_instance;
}
/**
* Plugin starting point. Handles hooks, loading of language files and PayPal delayed payment support.
*/
public function init() {
parent::init();
$this->add_delayed_payment_support( [
'option_label' => esc_html__( 'Subscribe contact to service x only when payment is received.', 'gf_cloudstorage' )
] );
}
// # FEED PROCESSING -----------------------------------------------------------------------------------------------
/**
* 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 ) {
$settings = $this->get_plugin_settings();
$protocol= rgar( $settings, 'storage_protocol' );
$endpoint = rgar( $settings, 'storage_endpoint' );
$username = GFCommon::replace_variables($feed['meta']['storage_username'], $form, $entry);
$password = GFCommon::replace_variables($feed['meta']['storage_password'], $form, $entry);
$folder = GFCommon::replace_variables($feed['meta']['storage_folder'], $form, $entry);
$filename = sanitize_file_name(GFCommon::replace_variables($feed['meta']['storage_filename'], $form, $entry) . '-' . $entry['id'] . '.html');
$filehead = GFCommon::replace_variables($feed['meta']['storage_fileheader'], $form, $entry, false, true, false);
$filedata = GFCommon::replace_variables('{all_fields}', $form, $entry, false, true, false);
$filefoot = GFCommon::replace_variables($feed['meta']['storage_filefooter'], $form, $entry, false, true, false);
// 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 );
}
$data = $filehead . $filedata . $filefoot;
$this->upload($protocol, $endpoint, $username, $password, $folder, $filename, $data);
}
/**
* Creates a custom page for this add-on.
*/
public function plugin_page() {
echo 'This page appears in the Forms menu';
}
/**
* Configures the settings which should be rendered on the add-on settings tab.
*
* @return array
*/
public function plugin_settings_fields() {
return [ [
'title' => esc_html__( 'Cloud Storage Settings', 'gf_cloudstorage' ),
'fields' => [ [
'label' => 'Protocol',
'label' => esc_html__( 'Protocol', 'gf_cloudstorage' ),
'type' => 'select',
'name' => 'storage_protocol',
'tooltip' => esc_html__( 'Is the endpoint secure?', 'gf_cloudstorage' ),
'choices' => [ [
'label' => 'HTTP',
'value' => 'http://'
], [
'label' => 'HTTPS',
'value' => 'https://'
] ]
], [
'name' => 'storage_endpoint',
'tooltip' => esc_html__( 'Endpoint to post to, e.g. https://cloud.orwa.org/remote.php/dav/files/', 'gf_cloudstorage' ),
'label' => esc_html__( 'API Endpoint', 'gf_cloudstorage' ),
'type' => 'text',
'class' => 'small',
], ],
], ];
}
/**
* Configures the settings which should be rendered on the feed edit page in the Form Settings > Cloud Storage area.
*
* @return array
*/
public function feed_settings_fields() {
return [ [
'title' => $this->provider . ' Integration Settings',
'fields' => [ [
'label' => 'Name This Integration',
'type' => 'text',
'name' => 'storage_name',
'tooltip' => 'Expecially useful for multiple cloud storage integrations.',
'class' => 'medium',
'feedback_callback' => [ $this, 'is_valid_setting' ]
], [
'label' => $this->provider . ' Username',
'type' => 'text',
'name' => 'storage_username',
'tooltip' => 'Your username for the cloud storage provider.',
'class' => 'medium merge-tag-support mt-position-right',
'feedback_callback' => [ $this, 'is_valid_setting' ]
], [
'label' => $this->provider . ' Password',
'type' => 'text',
'name' => 'storage_password',
'tooltip' => 'Your password for the cloud storage provider.',
'class' => 'medium merge-tag-support mt-position-right',
'feedback_callback' => [ $this, 'is_valid_setting' ]
], [
'label' => $this->provider . ' Folder',
'type' => 'text',
'name' => 'storage_folder',
'tooltip' => 'This is the path we want to save our file in. Default: "/"',
'class' => 'medium merge-tag-support mt-position-right',
'feedback_callback' => [ $this, 'is_valid_setting' ]
], [
'label' => $this->provider . ' Filename',
'type' => 'text',
'name' => 'storage_filename',
'tooltip' => 'Name of the file, feel free to use merge tags!',
'class' => 'medium merge-tag-support mt-position-right',
'feedback_callback' => [ $this, 'is_valid_setting' ]
], [
'label' => 'Document Header',
'type' => 'textarea',
'name' => 'storage_fileheader',
'tooltip' => 'HTML Header',
'class' => 'medium merge-tag-support mt-position-right',
'allow_html' => true
], [
'label' => 'Document Footer',
'type' => 'textarea',
'name' => 'storage_filefooter',
'tooltip' => 'HTML Footer',
'class' => 'medium merge-tag-support mt-position-right',
'allow_html' => true
], ],
] ];
}
/**
* Configures which columns should be displayed on the feed list page.
*
* @return array
*/
public function feed_list_columns() {
return [
'storage_name' => esc_html__( 'Integration Name', 'gf_cloudstorage' ),
];
}
/**
* Format the value to be displayed in the storage_name column.
*
* @param array $feed The feed being included in the feed list.
*
* @return string
*/
public function get_column_value_storage_name( $feed ) {
return '<b>' . rgars( $feed, 'meta/storage_name' ) . '</b>';
}
/**
* Prevent feeds being listed or created if an api key isn't valid.
*
* @return bool
*/
public function can_create_feed() {
return true;
}
/**
* @see parent
* @see credit: [Heroicons](https://heroicons.com/)
*
* @since 1.6.0
*/
public function get_menu_icon() {
$icon = file_get_contents(plugin_dir_path( __FILE__) . 'img/icon.svg' );
return apply_filters('cloud_storage_icon', $icon, 1 );
}
// This private function is what makes it "nextcloud" - therefore we do not need options like
// endpoint ... we should prolly have "domain" tho
// @TODO: Conditionally handle empty data based on form option to send HTML or PDF file
private function upload($protocol, $domain, $username, $password, $folder, $filename, $data = '') {
try {
// $filename = 'entry-1234.pdf';
$endpoint = $protocol . $domain . '/' . $username . '/' . $folder . '/' . $filename;
// $mimeType = 'application/pdf';
$mimeType = 'text/html';
// $data = new \CURLFile('./' . $filename, $mimeType, $filename);
$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: ' . $mimeType
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$results = curl_exec($ch);
// Check the return value of curl_exec(), too
if ($results === false) {
throw new Exception(curl_error($ch), curl_errno($ch));
}
// Check HTTP return code, too; might be something else than 200
$httpReturnCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
} catch(Exception $e) {
trigger_error(sprintf(
'Curl failed with error #%d: %s',
$e->getCode(), $e->getMessage()),
E_USER_ERROR);
} finally {
// Close curl handle unless it failed to initialize
if (is_resource($ch)) {
curl_close($ch);
}
}
}
}