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-cf7.php

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

397 lines 13.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_CF7 extends WeForms_Importer_Abstract {
9
10 public function __construct() {
11 $this->id = 'cf7';
12 $this->title = 'Contact Form 7';
13 $this->shortcode = 'contact-form-7';
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( 'WPCF7' );
25 }
26
27 /**
28 * Get all the forms
29 *
30 * @return array
31 */
32 public function get_forms() {
33 $items = WPCF7_ContactForm::find( [
34 'posts_per_page' => -1,
35 ] );
36
37 return $items;
38 }
39
40 /**
41 * Get form name
42 *
43 * @param object $form
44 *
45 * @return string
46 */
47 public function get_form_name( $form ) {
48 return $form->title();
49 }
50
51 /**
52 * Get the form id
53 *
54 * @param mixed $form
55 *
56 * @return int
57 */
58 protected function get_form_id( $form ) {
59 return $form->id;
60 }
61
62 /**
63 * Get the form fields
64 *
65 * @param object $form
66 *
67 * @return array
68 */
69 public function get_form_fields( $form ) {
70 $form_fields = [];
71 $form_tags = $form->scan_form_tags();
72 $properties = $form->get_properties();
73
74 if ( !$form_tags ) {
75 return $form_fields;
76 }
77
78 foreach ( $form_tags as $menu_order => $cf_field ) {
79 $field_content = [];
80
81 switch ( $cf_field->basetype ) {
82 case 'text':
83 case 'email':
84 case 'textarea':
85 case 'date':
86 case 'url':
87
88 $form_fields[] = $this->get_form_field( $cf_field->basetype, [
89 'required' => $cf_field->is_required() ? 'yes' : 'no',
90 'label' => $this->find_label( $properties['form'], $cf_field->type, $cf_field->name ),
91 'name' => $cf_field->name,
92 'css_class' => $cf_field->get_class_option(),
93 ] );
94 break;
95
96 case 'select':
97 case 'radio':
98 case 'checkbox':
99 $form_fields[] = $this->get_form_field( $cf_field->basetype, [
100 'required' => $cf_field->is_required() ? 'yes' : 'no',
101 'label' => $this->find_label( $properties['form'], $cf_field->type, $cf_field->name ),
102 'name' => $cf_field->name,
103 'css_class' => $cf_field->get_class_option(),
104 'options' => $this->get_options( $cf_field ),
105 ] );
106
107 break;
108
109 case 'range':
110 case 'number':
111
112 $field_content = $this->get_form_field( $cf_field->basetype, [
113 'required' => $cf_field->is_required() ? 'yes' : 'no',
114 'label' => $this->find_label( $properties['form'], $cf_field->type, $cf_field->name ),
115 'name' => $cf_field->name,
116 'css_class' => $cf_field->get_class_option(),
117 'step_text_field' => $cf_field->get_option( 'step', 'int', true ),
118 'min_value_field' => $cf_field->get_option( 'min', 'signed_int', true ),
119 'max_value_field' => $cf_field->get_option( 'max', 'signed_int', true ),
120 ] );
121
122 if ( $cf_field->has_option( 'placeholder' ) || $cf_field->has_option( 'watermark' ) ) {
123 $field_content['placeholder'] = $value;
124 $value = '';
125 }
126
127 $value = $cf_field->get_default_option( $value );
128 $value = wpcf7_get_hangover( $cf_field->name, $value );
129 $field_content['value'] = $value;
130
131 $form_fields[] = $field_content;
132
133 break;
134
135 case 'range':
136 case 'quiz':
137 // code...
138 break;
139
140 case 'acceptance':
141 $form_fields[] = $this->get_form_field( 'toc', [
142 'required' => $cf_field->is_required() ? 'yes' : 'no',
143 'description' => $this->find_label( $properties['form'], $cf_field->type, $cf_field->name ),
144 'name' => $cf_field->name,
145 ] );
146 break;
147
148 case 'recaptcha':
149 $form_fields[] = $this->get_form_field( $cf_field->basetype, [
150 'required' => $cf_field->is_required() ? 'yes' : 'no',
151 'label' => $this->find_label( $properties['form'], $cf_field->type, $cf_field->name ),
152 'name' => $cf_field->name,
153 'css_class' => $cf_field->get_class_option(),
154 ] );
155 break;
156
157 case 'file':
158
159 $allowed_size = 1024; // default size 1 MB
160 $allowed_file_types = [];
161
162 if ( $file_size_a = $cf_field->get_option( 'limit' ) ) {
163 $limit_pattern = '/^([1-9][0-9]*)([kKmM]?[bB])?$/';
164
165 foreach ( $file_size_a as $file_size ) {
166 if ( preg_match( $limit_pattern, $file_size, $matches ) ) {
167 $allowed_size = (int) $matches[1];
168
169 if ( !empty( $matches[2] ) ) {
170 $kbmb = strtolower( $matches[2] );
171
172 if ( 'kb' == $kbmb ) {
173 $allowed_size *= 1;
174 } elseif ( 'mb' == $kbmb ) {
175 $allowed_size *= 1024;
176 }
177 }
178
179 break;
180 }
181 }
182 }
183
184 if ( $file_types_a = $cf_field->get_option( 'filetypes' ) ) {
185 foreach ( $file_types_a as $file_types ) {
186 $file_types = explode( '|', $file_types );
187
188 foreach ( $file_types as $file_type ) {
189 $file_type = trim( $file_type, '.' );
190 $file_type = str_replace( [ '.', '+', '*', '?' ], [ '\.', '\+', '\*', '\?' ], $file_type );
191
192 $_type = $this->get_file_type( $file_type );
193
194 if ( !in_array( $_type, $allowed_file_types ) ) {
195 $allowed_file_types[] = $_type;
196 }
197 }
198 }
199 }
200
201 $form_fields[] = $this->get_form_field( $cf_field->basetype, [
202 'required' => $cf_field->is_required() ? 'yes' : 'no',
203 'label' => $this->find_label( $properties['form'], $cf_field->type, $cf_field->name ),
204 'name' => $cf_field->name,
205 'css_class' => $cf_field->get_class_option(),
206 'max_size' => $allowed_size,
207 'extension' => $allowed_file_types,
208 ] );
209 break;
210 }
211 }
212
213 return $form_fields;
214 }
215
216 /**
217 * Get form settings
218 *
219 * @param object $form
220 *
221 * @return array
222 */
223 public function get_form_settings( $form ) {
224 $default = $this->get_default_form_settings();
225 $properties = $form->get_properties();
226
227 $settings = wp_parse_args( [
228 'message' => $properties['messages']['mail_sent_ok'],
229 ], $default );
230
231 return $settings;
232 }
233
234 /**
235 * Get form notifications
236 *
237 * @param object $form
238 *
239 * @return array
240 */
241 public function get_form_notifications( $form ) {
242 $notifications = [];
243 $properties = $form->get_properties();
244
245 $notifications = [
246 [
247 'active' => $properties['mail']['active'] ? 'true' : 'false',
248 'name' => 'Admin Notification',
249 'subject' => str_replace( '[your-subject]', '{field:your-subject}', $properties['mail']['subject'] ),
250 'to' => $properties['mail']['recipient'],
251 'replyTo' => '{field:your-email}',
252 'message' => '{all_fields}',
253 'fromName' => '{site_name}',
254 'fromAddress' => '{admin_email}',
255 'cc' => '',
256 'bcc' => '',
257 ],
258 ];
259
260 $sender_match = $this->get_notification_sender_match( $properties['mail'] );
261
262 if ( !empty( $sender_match['fromName'] ) ) {
263 $form_notifications[0]['fromName'] = $sender_match['fromName'];
264 }
265
266 if ( isset( $sender_match['fromAddress'] ) ) {
267 $form_notifications[0]['fromAddress'] = $sender_match['fromAddress'];
268 }
269
270 if ( $properties['mail_2']['active'] ) {
271 $notifications[] = [
272 'active' => $properties['mail_2']['active'] ? 'true' : 'false',
273 'name' => 'Admin Notification',
274 'subject' => str_replace( '[your-subject]', '{field:your-subject}', $properties['mail_2']['subject'] ),
275 'to' => $properties['mail_2']['recipient'],
276 'replyTo' => '{field:your-email}',
277 'message' => '{all_fields}',
278 'fromName' => '{site_name}',
279 'fromAddress' => '{admin_email}',
280 'cc' => '',
281 'bcc' => '',
282 ];
283 }
284
285 $sender_match = $this->get_notification_sender_match( $properties['mail2'] );
286
287 if ( !empty( $sender_match['fromName'] ) ) {
288 $form_notifications[1]['fromName'] = $sender_match['fromName'];
289 }
290
291 if ( isset( $sender_match['fromAddress'] ) ) {
292 $form_notifications[1]['fromAddress'] = $sender_match['fromAddress'];
293 }
294
295 return $notifications;
296 }
297
298 /**
299 * Match the sender
300 *
301 * @param array $mail
302 *
303 * @return array
304 */
305 public function get_notification_sender_match( $mail ) {
306 $sender = [ 'fromName' => '', 'fromAddress' => '' ];
307 $sender_match = [];
308
309 preg_match( '/([^<"]*)"?\s*<(\S*)>/', $mail['sender'], $sender_match );
310
311 if ( isset( $sender_match[1] ) ) {
312 $sender['fromName'] = $sender_match[1];
313 }
314
315 if ( isset( $sender_match[2] ) ) {
316 $sender['fromAddress'] = $sender_match[2];
317 }
318
319 return $sender;
320 }
321
322 /**
323 * Try to find out the input label
324 *
325 * Loop through all the label tags and try to find out
326 * if the field is inside that tag. Then strip out the field and find out label
327 *
328 * @param string $content
329 * @param string $type
330 * @param string $fieldname
331 *
332 * @return string
333 */
334 private function find_label( $content, $type, $fieldname ) {
335
336 // find all enclosing label fields
337 $pattern = '/<label>([ \w\S\r\n\t]+?)<\/label>/';
338 preg_match_all( $pattern, $content, $matches );
339
340 foreach ( $matches[1] as $key => $match ) {
341 $match = trim( str_replace( "\n", '', $match ) );
342
343 preg_match( '/\[(?:' . preg_quote( $type ) . ') ' . $fieldname . '(?:[ ](.*?))?(?:[\r\n\t ](\/))?\]/', $match, $input_match );
344
345 if ( $input_match ) {
346 $label = strip_tags( str_replace( $input_match[0], '', $match ) );
347
348 return trim( $label );
349 }
350 }
351
352 return $fieldname;
353 }
354
355 /**
356 * Get file type for upload files
357 *
358 * @param string $extension
359 *
360 * @return bool|string
361 */
362 private function get_file_type( $extension ) {
363 $allowed_extensions = weforms_allowed_extensions();
364
365 foreach ( $allowed_extensions as $type => $extensions ) {
366 $_extensions = explode( ',', $extensions['ext'] );
367
368 if ( in_array( $extension, $_extensions ) ) {
369 return $type;
370 }
371 }
372
373 return false;
374 }
375
376 /**
377 * Translate to wpuf field options array
378 *
379 * @param object $field
380 *
381 * @return array
382 */
383 private function get_options( $field ) {
384 $options = [];
385
386 if ( !$field->raw_values ) {
387 return $options;
388 }
389
390 foreach ( $field->raw_values as $key => $value ) {
391 $options[ $value ] = $field->values[ $key ];
392 }
393
394 return $options;
395 }
396 }
397