| 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 |
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 boolean |
| 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( array( |
| 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 = array(); |
| 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 = array(); |
| 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, array( |
| 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, array( |
| 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, array( |
| 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', array( |
| 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, array( |
| 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 = array(); |
| 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 |
|
| 185 |
if ( $file_types_a = $cf_field->get_option( 'filetypes' ) ) { |
| 186 |
foreach ( $file_types_a as $file_types ) { |
| 187 |
$file_types = explode( '|', $file_types ); |
| 188 |
|
| 189 |
foreach ( $file_types as $file_type ) { |
| 190 |
$file_type = trim( $file_type, '.' ); |
| 191 |
$file_type = str_replace( array( '.', '+', '*', '?' ), array( '\.', '\+', '\*', '\?' ), $file_type ); |
| 192 |
|
| 193 |
$_type = $this->get_file_type( $file_type ); |
| 194 |
|
| 195 |
if ( ! in_array( $_type, $allowed_file_types ) ) { |
| 196 |
$allowed_file_types[] = $_type; |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
$form_fields[] = $this->get_form_field( $cf_field->basetype, array( |
| 203 |
'required' => $cf_field->is_required() ? 'yes' : 'no', |
| 204 |
'label' => $this->find_label( $properties['form'], $cf_field->type, $cf_field->name ), |
| 205 |
'name' => $cf_field->name, |
| 206 |
'css_class' => $cf_field->get_class_option(), |
| 207 |
'max_size' => $allowed_size, |
| 208 |
'extension' => $allowed_file_types, |
| 209 |
) ); |
| 210 |
break; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
return $form_fields; |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Get form settings |
| 219 |
* |
| 220 |
* @param object $form |
| 221 |
* |
| 222 |
* @return array |
| 223 |
*/ |
| 224 |
public function get_form_settings( $form ) { |
| 225 |
$default = $this->get_default_form_settings(); |
| 226 |
$properties = $form->get_properties(); |
| 227 |
|
| 228 |
$settings = wp_parse_args( array( |
| 229 |
'message' => $properties['messages']['mail_sent_ok'], |
| 230 |
), $default ); |
| 231 |
|
| 232 |
return $settings; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Get form notifications |
| 237 |
* |
| 238 |
* @param object $form |
| 239 |
* |
| 240 |
* @return array |
| 241 |
*/ |
| 242 |
public function get_form_notifications( $form ) { |
| 243 |
$notifications = array(); |
| 244 |
$properties = $form->get_properties(); |
| 245 |
|
| 246 |
$notifications = array( |
| 247 |
array( |
| 248 |
'active' => $properties['mail']['active'] ? 'true' : 'false', |
| 249 |
'name' => 'Admin Notification', |
| 250 |
'subject' => str_replace( '[your-subject]', '{field:your-subject}', $properties['mail']['subject'] ), |
| 251 |
'to' => $properties['mail']['recipient'], |
| 252 |
'replyTo' => '{field:your-email}', |
| 253 |
'message' => '{all_fields}', |
| 254 |
'fromName' => '{site_name}', |
| 255 |
'fromAddress' => '{admin_email}', |
| 256 |
'cc' => '', |
| 257 |
'bcc' => '', |
| 258 |
), |
| 259 |
); |
| 260 |
|
| 261 |
$sender_match = $this->get_notification_sender_match( $properties['mail'] ); |
| 262 |
|
| 263 |
if ( !empty( $sender_match['fromName'] ) ) { |
| 264 |
$form_notifications[0]['fromName'] = $sender_match['fromName']; |
| 265 |
} |
| 266 |
|
| 267 |
if ( isset( $sender_match['fromAddress'] ) ) { |
| 268 |
$form_notifications[0]['fromAddress'] = $sender_match['fromAddress']; |
| 269 |
} |
| 270 |
|
| 271 |
if ( $properties['mail_2']['active'] ) { |
| 272 |
$notifications[] = array( |
| 273 |
'active' => $properties['mail_2']['active'] ? 'true' : 'false', |
| 274 |
'name' => 'Admin Notification', |
| 275 |
'subject' => str_replace( '[your-subject]', '{field:your-subject}', $properties['mail_2']['subject'] ), |
| 276 |
'to' => $properties['mail_2']['recipient'], |
| 277 |
'replyTo' => '{field:your-email}', |
| 278 |
'message' => '{all_fields}', |
| 279 |
'fromName' => '{site_name}', |
| 280 |
'fromAddress' => '{admin_email}', |
| 281 |
'cc' => '', |
| 282 |
'bcc' => '', |
| 283 |
); |
| 284 |
} |
| 285 |
|
| 286 |
$sender_match = $this->get_notification_sender_match( $properties['mail2'] ); |
| 287 |
|
| 288 |
if ( !empty( $sender_match['fromName'] ) ) { |
| 289 |
$form_notifications[1]['fromName'] = $sender_match['fromName']; |
| 290 |
} |
| 291 |
|
| 292 |
if ( isset( $sender_match['fromAddress'] ) ) { |
| 293 |
$form_notifications[1]['fromAddress'] = $sender_match['fromAddress']; |
| 294 |
} |
| 295 |
|
| 296 |
return $notifications; |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* Match the sender |
| 301 |
* |
| 302 |
* @param array $mail |
| 303 |
* |
| 304 |
* @return array |
| 305 |
*/ |
| 306 |
public function get_notification_sender_match( $mail ) { |
| 307 |
$sender = array( 'fromName' => '', 'fromAddress' => '' ); |
| 308 |
$sender_match = array(); |
| 309 |
|
| 310 |
preg_match( '/([^<"]*)"?\s*<(\S*)>/', $mail['sender'], $sender_match ); |
| 311 |
|
| 312 |
if ( isset( $sender_match[1] ) ) { |
| 313 |
$sender['fromName'] = $sender_match[1]; |
| 314 |
} |
| 315 |
|
| 316 |
if ( isset( $sender_match[2] ) ) { |
| 317 |
$sender['fromAddress'] = $sender_match[2]; |
| 318 |
} |
| 319 |
|
| 320 |
return $sender; |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* Try to find out the input label |
| 325 |
* |
| 326 |
* Loop through all the label tags and try to find out |
| 327 |
* if the field is inside that tag. Then strip out the field and find out label |
| 328 |
* |
| 329 |
* @param string $content |
| 330 |
* @param string $type |
| 331 |
* @param string $fieldname |
| 332 |
* |
| 333 |
* @return string |
| 334 |
*/ |
| 335 |
private function find_label( $content, $type, $fieldname ) { |
| 336 |
|
| 337 |
// find all enclosing label fields |
| 338 |
$pattern = '/<label>([ \w\S\r\n\t]+?)<\/label>/'; |
| 339 |
preg_match_all( $pattern, $content, $matches ); |
| 340 |
|
| 341 |
foreach ($matches[1] as $key => $match) { |
| 342 |
$match = trim( str_replace( "\n", '', $match ) ); |
| 343 |
|
| 344 |
preg_match( '/\[(?:' . preg_quote( $type ) . ') ' . $fieldname . '(?:[ ](.*?))?(?:[\r\n\t ](\/))?\]/', $match, $input_match ); |
| 345 |
|
| 346 |
if ( $input_match ) { |
| 347 |
$label = strip_tags( str_replace( $input_match[0], '', $match ) ); |
| 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 boolean|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 = array(); |
| 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 |
} |
| 398 |
|