PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.5.4
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.5.4
1.6.7 1.6.8 1.6.9 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.18 1.6.19 1.6.2 1.6.20 1.6.21 1.6.22 1.6.23 1.6.24 1.6.25 1.6.26 1.6.27 1.6.28 1.6.3 1.6.4 1.6.5 1.6.6 All 74 releases
weforms / includes / importer / class-importer-gf.php

class-importer-gf.php in weForms – Easy Drag & Drop Contact Form Builder For WordPress 1.5.4, at includes/importer/class-importer-gf.php

450 lines 17.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Contact Form 7
5 *
6 * Import contact form 7 forms
7 */
8 class WeForms_Importer_GF extends WeForms_Importer_Abstract {
9
10 public function __construct() {
11 $this->id = 'gf';
12 $this->title = 'Gravity Form';
13 $this->shortcode = 'gravityform';
14
15 parent::__construct();
16 }
17
18 /**
19 * See if the plugin exists
20 *
21 * @return bool
22 */
23 public function plugin_exists() {
24 return class_exists( 'GFForms' );
25 }
26
27 /**
28 * Get all forms
29 *
30 * @since 1.0.0
31 *
32 * @return [type]
33 */
34 public function get_forms() {
35 return GFAPI::get_forms();
36 }
37
38 /**
39 * Get form title
40 *
41 * @since 1.0.0
42 *
43 * @param array $form
44 *
45 * @return string
46 */
47 public function get_form_name( $form ) {
48 return $form['title'];
49 }
50
51 /**
52 * Mapping all form fileds
53 *
54 * @since 1.0.0
55 *
56 * @param array $form
57 *
58 * @return array
59 */
60 public function get_form_fields( $form ) {
61 $form_fields = [];
62
63 $form_tags = $form['fields'];
64
65 if ( !$form_tags ) {
66 return;
67 }
68
69 foreach ( $form_tags as $menu_order => $field ) {
70 switch ( $field->type ) {
71
72 case 'text':
73 case 'email':
74 case 'date':
75 case 'url':
76
77 $form_fields[] = $this->get_form_field( $field->type, [
78 'required' => $field->isRequired ? 'yes' : 'no',
79 'label' => $field->label,
80 'name' => $this->get_meta_key( $field ),
81 'css_class' => $field->cssClass,
82 'help' => $field->description,
83 'placeholder' => $field->placeholder,
84 ] );
85 break;
86
87 case 'textarea':
88
89 $form_fields[] = [
90 'input_type' => 'textarea',
91 'template' => 'textarea_field',
92 'required' => $field->isRequired ? 'yes' : 'no',
93 'label' => $field->label,
94 'name' => $this->get_meta_key( $field ),
95 'css' => $field->cssClass,
96 'help' => $field->description,
97 'placeholder' => $field->placeholder,
98 'is_meta' => 'yes',
99 'rows' => 5,
100 'cols' => 25,
101 'default' => $field->defaultValue,
102 'rich' => $field->useRichTextEditor ? 'yes' : 'no',
103 'wpuf_cond' => $this->conditionals,
104 ];
105 break;
106
107 case 'select':
108 case 'radio':
109 case 'checkbox':
110 $form_fields[] = $this->get_form_field( $field->type, [
111 'required' => $field->isRequired ? 'yes' : 'no',
112 'label' => $field->label,
113 'name' => $this->get_meta_key( $field ),
114 'css_class' => $field->cssClass,
115 'help' => $field->description,
116 'options' => $this->get_options( $field ),
117 ] );
118 break;
119
120 case 'multiselect':
121 $form_fields[] = [
122 'input_type' => 'multiselect',
123 'template' => 'multiple_select',
124 'required' => $field->isRequired ? 'yes' : 'no',
125 'label' => $field->label,
126 'name' => $this->get_meta_key( $field ),
127 'is_meta' => 'yes',
128 'css' => $field->cssClass,
129 'help' => $field->description,
130 'selected' => [],
131 'options' => $this->get_options( $field ),
132 'wpuf_cond' => $this->conditionals,
133 ];
134 break;
135
136 case 'number':
137 $form_fields[] = $this->get_form_field( $field->type, [
138 'required' => $field->isRequired ? 'yes' : 'no',
139 'label' => $field->label,
140 'name' => $this->get_meta_key( $field ),
141 'css_class' => $field->cssClass,
142 'help' => $field->description,
143 'options' => $this->get_options( $field ),
144 'value' => $field->defaultValue,
145 'min' => $field->rangeMin,
146 'max' => $field->rangeMax,
147 ] );
148 break;
149
150 case 'address':
151 $form_fields[] = [
152 'input_type' => 'address',
153 'template' => 'address_field',
154 'required' => $field->isRequired ? 'yes' : 'no',
155 'label' => $field->label,
156 'name' => $this->get_meta_key( $field ),
157 'is_meta' => 'yes',
158 'css_class' => $field->cssClass,
159 'help' => $field->description,
160 'address' => $this->get_address_field_data( $field ),
161 ];
162 break;
163
164 case 'fileupload':
165 $extenion_type = [];
166 $file_ext = explode( ',', $field->allowedExtensions );
167
168 foreach ( $file_ext as $ext ) {
169 $allowed_ext = $this->get_file_type( $ext );
170
171 if ( $allowed_ext ) {
172 $extenion_type[] = $data;
173 }
174 }
175
176 $form_fields[] = $this->get_form_field( 'file', [
177 'required' => $field->isRequired ? 'yes' : 'no',
178 'label' => $field->label,
179 'name' => $this->get_meta_key( $field ),
180 'css_class' => $field->cssClass,
181 'help' => $field->description,
182 'max_size' => !empty( $field->maxFileSize ) ? $field->maxFileSize * 1024 : 1024,
183 'extension' => array_unique( $extenion_type ),
184 ] );
185 break;
186
187 case 'hidden':
188 $form_fields[] = [
189 'input_type' => 'hidden',
190 'template' => 'custom_hidden_field',
191 'label' => $field->label,
192 'name' => $this->get_meta_key( $field ),
193 'is_meta' => 'yes',
194 'meta_value' => $field->defaultValue,
195 'wpuf_cond' => '',
196 ];
197
198 break;
199
200 case 'name':
201 if ( $this->get_name_format( $field ) ) {
202 $form_fields[] = [
203 'input_type' => 'name',
204 'template' => 'name_field',
205 'required' => $field->isRequired ? 'yes' : 'no',
206 'label' => $field->label,
207 'name' => $this->get_meta_key( $field ),
208 'is_meta' => 'yes',
209 'css' => $field->cssClass,
210 'wpuf_cond' => $this->conditionals,
211 'format' => $this->get_name_format( $field ),
212 'first_name' => [
213 'sub' => isset( $field->inputs[1]['label'] ) ? $field->inputs[1]['label'] : '',
214 'default' => isset( $field->inputs[1]['defaultValue'] ) ? $field->inputs[1]['defaultValue'] : '',
215 'placeholder' => isset( $field->inputs[1]['placeholder'] ) ? $field->inputs[1]['placeholder'] : '',
216 ],
217 'middle_name' => [
218 'sub' => isset( $field->inputs[2]['label'] ) ? $field->inputs[2]['label'] : '',
219 'default' => isset( $field->inputs[2]['defaultValue'] ) ? $field->inputs[2]['defaultValue'] : '',
220 'placeholder' => isset( $field->inputs[2]['placeholder'] ) ? $field->inputs[2]['placeholder'] : '',
221 ],
222 'last_name' => [
223 'sub' => isset( $field->inputs[3]['label'] ) ? $field->inputs[3]['label'] : '',
224 'default' => isset( $field->inputs[3]['defaultValue'] ) ? $field->inputs[3]['defaultValue'] : '',
225 'placeholder' => isset( $field->inputs[3]['placeholder'] ) ? $field->inputs[3]['placeholder'] : '',
226 ],
227 'hide_subs' => '',
228 ];
229 }
230 break;
231
232 case 'captcha':
233 $form_fields[] = $this->get_form_field( 'recaptcha', [
234 'required' => $field->isRequired ? 'yes' : 'no',
235 'label' => $field->label,
236 'name' => $this->get_meta_key( $field ),
237 'css_class' => $field->cssClass,
238 ] );
239 break;
240 }
241 }
242
243 return $form_fields;
244 }
245
246 /**
247 * Mapping form settings
248 *
249 * @since 1.0.0
250 *
251 * @param array $form
252 *
253 * @return array
254 */
255 public function get_form_settings( $form ) {
256 $default_settings = $this->get_default_form_settings();
257 $start_date = '';
258 $end_date = '';
259
260 if ( isset( $form['scheduleForm'] ) && $form['scheduleForm'] ) {
261 $start_time = sprintf( '%02d', $form['scheduleStartHour'] ) . ':' . sprintf( '%02d', $form['scheduleStartMinute'] ) . ' ' . $form['scheduleStartAmpm'];
262 $end_time = sprintf( '%02d', $form['scheduleEndHour'] ) . ':' . sprintf( '%02d', $form['scheduleEndMinute'] ) . ' ' . $form['scheduleEndAmpm'];
263 $start_date = date( 'Y-m-d H:i:s', strtotime( $form['scheduleStart'] . $start_time ) );
264 $end_date = date( 'Y-m-d H:i:s', strtotime( $form['scheduleEnd'] . $end_time ) );
265 }
266
267 $form_label_position = !empty( $form['labelPlacement'] ) ? $form['labelPlacement'] : 'top_label';
268
269 $form_settings = [
270 'redirect_to' => 'same',
271 'message' => __( 'Thanks for contacting us! We will get in touch with you shortly.', 'weforms' ),
272 'page_id' => '',
273 'url' => '',
274 'submit_text' => !empty( $form['button']['text'] ) ? $form['button']['text'] : __( 'Submit Query', 'weforms' ),
275 'schedule_form' => ( isset( $form['scheduleForm'] ) && $form['scheduleForm'] ) ? 'true' : 'false',
276 'schedule_start' => $start_date,
277 'schedule_end' => $end_date,
278 'sc_pending_message' => !empty( $form['schedulePendingMessage'] ) ? $form['schedulePendingMessage'] : '',
279 'sc_expired_message' => !empty( $form['scheduleMessage'] ) ? $form['scheduleMessage'] : '',
280 'require_login' => !empty( $form['requireLogin'] ) ? 'true' : 'false',
281 'req_login_message' => !empty( $form['requireLoginMessage'] ) ? $form['requireLoginMessage'] : '',
282 'limit_entries' => !empty( $form['limitEntries'] ) ? 'true' : 'false',
283 'limit_number' => !empty( $form['limitEntriesCount'] ) ? $form['limitEntriesCount'] : '',
284 'limit_message' => !empty( $form['limitEntriesMessage'] ) ? $form['limitEntriesMessage'] : '',
285 'label_position' => $this->get_label_position( $form_label_position ),
286 ];
287
288 $settings = wp_parse_args( $form_settings, $default_settings );
289
290 return $settings;
291 }
292
293 /**
294 * Mapping form notification settings
295 *
296 * @since 1.0.0
297 *
298 * @param array $form
299 *
300 * @return array
301 */
302 public function get_form_notifications( $form ) {
303 $notifications = [];
304
305 if ( empty( $form['notifications'] ) ) {
306 return $notifications;
307 }
308
309 foreach ( $form['notifications'] as $key => $notification ) {
310 $notifications[] = [
311 'active' => ( isset( $notification['isActive'] ) && $notification['isActive'] ) ? 'true' : 'false',
312 'name' => $notification['name'],
313 'subject' => str_replace( '[your-subject]', '{field:your-subject}', $notification['subject'] ),
314 'to' => $notification['to'],
315 'replyTo' => '{field:your-email}',
316 'message' => '{all_fields}',
317 'fromName' => '{site_name}',
318 'fromAddress' => '{admin_email}',
319 'cc' => '',
320 'bcc' => '',
321 ];
322 }
323
324 return $notifications;
325 }
326
327 /**
328 * Get the form id
329 *
330 * @param mixed $form
331 *
332 * @return int
333 */
334 protected function get_form_id( $form ) {
335 return $form['id'];
336 }
337
338 /**
339 * Get unique meta key for field name
340 *
341 * @since 1.0.0
342 *
343 * @return string [slug format]
344 **/
345 public function get_meta_key( $field ) {
346 return str_replace( '-', '_', sanitize_title( $field->label . '-' . $field->id ) );
347 }
348
349 /**
350 * Get selecte, checkbox, multiselect and radio field options value
351 *
352 * @since 1.0.0
353 *
354 * @return void
355 **/
356 public function get_options( $field ) {
357 if ( empty( $field['choices'] ) ) {
358 return [];
359 }
360
361 return wp_list_pluck( $field['choices'], 'text', 'value' );
362 }
363
364 /**
365 * Get address field data
366 *
367 * @since 1.0.0
368 *
369 * @return void
370 **/
371 public function get_address_field_data( $field ) {
372 $address = [];
373 $address_data_key = [ 'street_address', 'street_address2', 'city_name', 'state', 'zip', 'country_select' ];
374
375 foreach ( $address_data_key as $key => $value ) {
376 $address[$value] = [
377 'checked' => ( isset( $field->inputs[$key]['isHidden'] ) && $field->inputs[$key]['isHidden'] ) ? '' : 'checked',
378 'type' => ( 'country_select' == $value ) ? 'select' : 'text',
379 'required' => $field->isRequired ? 'checked' : '',
380 'label' => !empty( $field->inputs[$key]['customLabel'] ) ? $field->inputs[$key]['customLabel'] : $field->inputs[$key]['label'],
381 'value' => !empty( $field->inputs[$key]['defaultValue'] ) ? $field->inputs[$key]['defaultValue'] : '',
382 'placeholder' => !empty( $field->inputs[$key]['placeholder'] ) ? $field->inputs[$key]['placeholder'] : '',
383 ];
384
385 if ( 'country_select' == $value ) {
386 $address[$value]['country_list_visibility_opt_name'] = 'all';
387 $address[$value]['country_select_hide_list'] = [];
388 $address[$value]['country_select_show_list'] = [];
389 }
390 }
391
392 return $address;
393 }
394
395 /**
396 * Get file type for upload files
397 *
398 * @param string $extension
399 *
400 * @return bool|string
401 */
402 private function get_file_type( $extension ) {
403 $allowed_extensions = weforms_allowed_extensions();
404
405 foreach ( $allowed_extensions as $type => $extensions ) {
406 $_extensions = explode( ',', $extensions['ext'] );
407
408 if ( in_array( $extension, $_extensions ) ) {
409 return $type;
410 }
411 }
412
413 return false;
414 }
415
416 /**
417 * Get name format
418 *
419 * @since 1.0.0
420 *
421 * @return void
422 **/
423 private function get_name_format( $field ) {
424 if ( !isset( $field->inputs[1]['isHidden'] ) && !isset( $field->inputs[2]['isHidden'] ) && !isset( $field->inputs[3]['isHidden'] ) ) {
425 return 'first-middle-last';
426 } elseif ( !isset( $field->inputs[1]['isHidden'] ) && !isset( $field->inputs[3]['isHidden'] ) ) {
427 return 'first-last';
428 } else {
429 return false;
430 }
431 }
432
433 /**
434 * Get label placement position
435 *
436 * @since 1.0.0
437 *
438 * @return void
439 **/
440 public function get_label_position( $label ) {
441 $labels = [
442 'top_label' => 'above',
443 'left_label' => 'left',
444 'right_label' => 'right',
445 ];
446
447 return $labels[$label];
448 }
449 }
450