| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Contact Form 7 |
| 5 |
* |
| 6 |
* Import contact form 7 forms |
| 7 |
*/ |
| 8 |
class WeForms_CF7 { |
| 9 |
|
| 10 |
function __construct() { |
| 11 |
add_action( 'admin_notices', array( $this, 'maybe_show_notice' ) ); |
| 12 |
|
| 13 |
add_action( 'wp_ajax_weforms_import_cf7_dismiss', array( $this, 'dismiss_notice' ) ); |
| 14 |
add_action( 'wp_ajax_weforms_import_cf7_forms', array( $this, 'import_forms' ) ); |
| 15 |
add_action( 'wp_ajax_weforms_cf7_shortcode_replace', array( $this, 'replace_action' ) ); |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Show notice if Contact From 7 found |
| 20 |
* |
| 21 |
* @return void |
| 22 |
*/ |
| 23 |
public function maybe_show_notice() { |
| 24 |
if ( ! class_exists( 'WPCF7' ) ) { |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
if ( $this->is_dimissed() || !current_user_can( 'manage_options' ) ) { |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
?> |
| 33 |
<div class="notice notice-info"> |
| 34 |
<p><strong><?php _e( 'Contact Form 7 Detected', 'weforms' ); ?></strong></p> |
| 35 |
<p><?php _e( 'Hey, looks like you have <strong>Contact Form 7</strong> installed. Would you like to <strong>import</strong> the forms into weForms?', 'weforms' ); ?></p> |
| 36 |
|
| 37 |
<p> |
| 38 |
<a href="#" class="button button-primary weforms-import-cf7" id="weforms-import-cf7"><?php _e( 'Import Forms', 'weforms' ); ?></a> |
| 39 |
<a href="#" class="button weforms-import-cf7" id="weforms-dimiss-cf7"><?php _e( 'No Thanks', 'weforms' ); ?></a> |
| 40 |
</p> |
| 41 |
</div> |
| 42 |
|
| 43 |
<script type="text/javascript"> |
| 44 |
jQuery(function($) { |
| 45 |
$('.notice').on('click', 'a#weforms-import-cf7', function(e) { |
| 46 |
e.preventDefault(); |
| 47 |
|
| 48 |
var self = $(this); |
| 49 |
self.addClass('updating-message'); |
| 50 |
|
| 51 |
wp.ajax.send( 'weforms_import_cf7_forms', { |
| 52 |
data: { |
| 53 |
type: self.data('type'), |
| 54 |
_wpnonce: '<?php echo wp_create_nonce( 'weforms_cf7_import' ); ?>' |
| 55 |
}, |
| 56 |
|
| 57 |
success: function(response) { |
| 58 |
var html = '<p><strong>' + response.title + '</strong></p>' + |
| 59 |
'<p>' + response.message + '</p>' + |
| 60 |
'<p>' + response.action + '</p>'; |
| 61 |
|
| 62 |
html += '<ul>'; |
| 63 |
_.each(response.refs, function(el, index) { |
| 64 |
html += '<li><a target="_blank" href="admin.php?page=weforms#/form/' + el.weforms_id + '/edit">' + el.title + '</a> - <a href="admin.php?page=weforms#/form/' + el.weforms_id + '/edit" target="_blank" class="button button-small"><span class="dashicons dashicons-external"></span> Edit</a></li>'; |
| 65 |
}); |
| 66 |
|
| 67 |
html += '</ul>'; |
| 68 |
html += '<p>' + '<a href="#" class="button button-primary weforms-cf7-replace-action" data-type="replace"><?php _e( 'Replace Shortcodes', 'weforms' ); ?></a> ' + |
| 69 |
'<a href="#" class="button weforms-cf7-replace-action" data-type="skip"><?php _e( 'No Thanks', 'weforms' ); ?></a></p>'; |
| 70 |
|
| 71 |
self.closest('.notice').removeClass('notice-info').addClass('notice-success').html( html ); |
| 72 |
}, |
| 73 |
|
| 74 |
error: function(error) { |
| 75 |
var html = '<p><strong>' + error.title + '</strong></p>' + |
| 76 |
'<p>' + error.message + '</p>'; |
| 77 |
|
| 78 |
self.closest('.notice').removeClass('notice-info').addClass('notice-error').html( html ); |
| 79 |
}, |
| 80 |
|
| 81 |
complete: function() { |
| 82 |
self.removeClass('updating-message'); |
| 83 |
} |
| 84 |
}); |
| 85 |
}); |
| 86 |
|
| 87 |
$('.notice').on('click', '#weforms-dimiss-cf7', function(e) { |
| 88 |
e.preventDefault(); |
| 89 |
|
| 90 |
$(this).closest('.notice').remove(); |
| 91 |
wp.ajax.send('weforms_import_cf7_dismiss'); |
| 92 |
}); |
| 93 |
|
| 94 |
$('.notice').on('click', 'a.weforms-cf7-replace-action', function(e) { |
| 95 |
e.preventDefault(); |
| 96 |
|
| 97 |
var self = $(this); |
| 98 |
var notice = self.closest('.notice'); |
| 99 |
|
| 100 |
self.addClass('updating-message'); |
| 101 |
|
| 102 |
wp.ajax.send( 'weforms_cf7_shortcode_replace', { |
| 103 |
data: { |
| 104 |
type: self.data('type'), |
| 105 |
_wpnonce: '<?php echo wp_create_nonce( 'weforms_cf7_replace' ); ?>' |
| 106 |
}, |
| 107 |
|
| 108 |
success: function(response) { |
| 109 |
notice.remove(); |
| 110 |
|
| 111 |
if ( 'replace' === self.data('type') ) { |
| 112 |
alert( response ); |
| 113 |
} |
| 114 |
}, |
| 115 |
|
| 116 |
error: function(error) { |
| 117 |
notice.remove(); |
| 118 |
alert( error ); |
| 119 |
}, |
| 120 |
|
| 121 |
complete: function() { |
| 122 |
self.removeClass('updating-message'); |
| 123 |
} |
| 124 |
}); |
| 125 |
|
| 126 |
}); |
| 127 |
}); |
| 128 |
</script> |
| 129 |
<?php |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Replace contact form 7 shortcodes |
| 134 |
* |
| 135 |
* @return void |
| 136 |
*/ |
| 137 |
public function replace_action() { |
| 138 |
check_ajax_referer( 'weforms_cf7_replace' ); |
| 139 |
|
| 140 |
$this->check_caps(); |
| 141 |
|
| 142 |
if ( ! in_array( $_POST['type'], array( 'skip', 'replace' ) ) ) { |
| 143 |
wp_send_json_error( __( 'Not a valid action type.', 'weforms' ) ); |
| 144 |
} |
| 145 |
|
| 146 |
if ( 'skip' == $_POST['type'] ) { |
| 147 |
wp_send_json_success(); |
| 148 |
} |
| 149 |
|
| 150 |
$pages_query = new WP_Query( array( |
| 151 |
'post_type' => 'page', |
| 152 |
'posts_per_page' => -1, |
| 153 |
's' => '[contact-form-7' |
| 154 |
) ); |
| 155 |
|
| 156 |
if ( ! $pages_query->found_posts ) { |
| 157 |
wp_send_json_error( __( 'No pages found with Contact Form 7 shortcode. Skipped!', 'weforms' ) ); |
| 158 |
} |
| 159 |
|
| 160 |
$count = 0; |
| 161 |
$refs = get_option( 'weforms_cf7_imported_forms', array() ); |
| 162 |
$pages = $pages_query->get_posts(); |
| 163 |
|
| 164 |
foreach ($pages as $page) { |
| 165 |
preg_match_all( '/\[(\[?)(contact\-form\-7|)(?![\w-])([^\]\/]*(?:\/(?!\])[^\]\/]*)*?)(?:(\/)\]|\](?:([^\[]*+(?:\[(?!\/\2\])[^\[]*+)*+)\[\/\2\])?)(\]?)/s', $page->post_content, $matches, PREG_SET_ORDER ); |
| 166 |
|
| 167 |
if ( empty( $matches ) ) { |
| 168 |
continue; |
| 169 |
} |
| 170 |
|
| 171 |
foreach ( $matches as $shortcode ) { |
| 172 |
$atts = shortcode_parse_atts( $shortcode[0] ); |
| 173 |
|
| 174 |
if ( isset( $atts['id'] ) && array_key_exists( $atts['id'], $refs ) ) { |
| 175 |
$replace = sprintf( '[weforms id="%d"]', $refs[ $atts['id'] ]['weforms_id'] ); |
| 176 |
|
| 177 |
$post_content = str_replace( $shortcode[0], $replace, $page->post_content ); |
| 178 |
|
| 179 |
wp_update_post( array( |
| 180 |
'ID' => $page->ID, |
| 181 |
'post_content' => $post_content |
| 182 |
) ); |
| 183 |
|
| 184 |
$count++; |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
delete_option( 'weforms_cf7_imported_forms' ); |
| 190 |
wp_send_json_success( sprintf( _n( 'Replaced %d form', 'Replaced %d forms', $count ), $count ) ); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Ajax import callback |
| 195 |
* |
| 196 |
* @return void |
| 197 |
*/ |
| 198 |
public function import_forms() { |
| 199 |
check_ajax_referer( 'weforms_cf7_import' ); |
| 200 |
|
| 201 |
$this->check_caps(); |
| 202 |
|
| 203 |
$imported = 0; |
| 204 |
$refs = array(); |
| 205 |
$items = WPCF7_ContactForm::find( array( |
| 206 |
'posts_per_page' => -1 |
| 207 |
) ); |
| 208 |
|
| 209 |
if ( ! $items ) { |
| 210 |
wp_send_json_error( array( |
| 211 |
'title' => __( 'Uh oh!', 'weforms' ), |
| 212 |
'message' => __( 'No contact form found!', 'weforms' ) |
| 213 |
) ); |
| 214 |
|
| 215 |
$this->dismiss_prompt(); |
| 216 |
} |
| 217 |
|
| 218 |
foreach ($items as $form) { |
| 219 |
|
| 220 |
$properties = $form->get_properties(); |
| 221 |
$form_tags = $form->scan_form_tags(); |
| 222 |
|
| 223 |
if ( ! $form_tags ) { |
| 224 |
continue; |
| 225 |
} |
| 226 |
|
| 227 |
$weforms_form = array( |
| 228 |
'post_title' => '[CF7] ' . $form->title(), |
| 229 |
'post_type' => 'wpuf_contact_form', |
| 230 |
'post_status' => 'publish', |
| 231 |
'post_author' => get_current_user_id() |
| 232 |
); |
| 233 |
|
| 234 |
$form_id = wp_insert_post( $weforms_form ); |
| 235 |
|
| 236 |
if ( is_wp_error( $form_id ) ) { |
| 237 |
continue; |
| 238 |
} |
| 239 |
|
| 240 |
$submit_label = __( 'Submit Query', 'weforms' ); |
| 241 |
$form_settings = array( |
| 242 |
'redirect_to' => 'same', |
| 243 |
'message' => $properties['messages']['mail_sent_ok'], |
| 244 |
'page_id' => '', |
| 245 |
'url' => '', |
| 246 |
'submit_text' => __( 'Submit Query', 'weforms' ), |
| 247 |
'schedule_form' => 'false', |
| 248 |
'schedule_start' => '', |
| 249 |
'schedule_end' => '', |
| 250 |
'sc_pending_message' => __( 'Form submission hasn\'t been started yet', 'weforms' ), |
| 251 |
'sc_expired_message' => __( 'Form submission is now closed.', 'weforms' ), |
| 252 |
'require_login' => 'false', |
| 253 |
'req_login_message' => __( 'You need to login to submit a query.', 'weforms' ), |
| 254 |
'limit_entries' => 'false', |
| 255 |
'limit_number' => '1000', |
| 256 |
'limit_message' => __( 'Sorry, we have reached the maximum number of submissions.', 'weforms' ), |
| 257 |
'label_position' => 'above', |
| 258 |
); |
| 259 |
|
| 260 |
$form_notifications = array( |
| 261 |
array( |
| 262 |
'active' => $properties['mail']['active'] ? 'true' : 'false', |
| 263 |
'name' => 'Admin Notification', |
| 264 |
'subject' => str_replace( '[your-subject]', '{field:your-subject}', $properties['mail']['subject'] ), |
| 265 |
'to' => $properties['mail']['recipient'], |
| 266 |
'replyTo' => '{field:your-email}', |
| 267 |
'message' => '{all_fields}', |
| 268 |
'fromName' => '{site_name}', |
| 269 |
'fromAddress' => '{admin_email}', |
| 270 |
'cc' => '', |
| 271 |
'bcc' => '', |
| 272 |
), |
| 273 |
); |
| 274 |
|
| 275 |
$sender_match = array(); |
| 276 |
preg_match( '/([^<"]*)"?\s*<(\S*)>/', $properties['mail']['sender'], $sender_match ); |
| 277 |
|
| 278 |
if ( isset( $sender_match[1] ) ) { |
| 279 |
$form_notifications[0]['fromName'] = $sender_match[1]; |
| 280 |
} |
| 281 |
|
| 282 |
if ( isset( $sender_match[2] ) ) { |
| 283 |
$form_notifications[0]['fromAddress'] = $sender_match[2]; |
| 284 |
} |
| 285 |
|
| 286 |
if ( $properties['mail_2']['active'] ) { |
| 287 |
$form_notifications[] = array( |
| 288 |
'active' => $properties['mail_2']['active'] ? 'true' : 'false', |
| 289 |
'name' => 'Admin Notification', |
| 290 |
'subject' => str_replace( '[your-subject]', '{field:your-subject}', $properties['mail_2']['subject'] ), |
| 291 |
'to' => $properties['mail_2']['recipient'], |
| 292 |
'replyTo' => '{field:your-email}', |
| 293 |
'message' => '{all_fields}', |
| 294 |
'fromName' => '{site_name}', |
| 295 |
'fromAddress' => '{admin_email}', |
| 296 |
'cc' => '', |
| 297 |
'bcc' => '', |
| 298 |
); |
| 299 |
} |
| 300 |
|
| 301 |
$sender2_match = array(); |
| 302 |
preg_match( '/([^<"]*)"?\s*<(\S*)>/', $properties['mail_2']['sender'], $sender2_match ); |
| 303 |
|
| 304 |
if ( isset( $sender2_match[1] ) ) { |
| 305 |
$form_notifications[1]['fromName'] = $sender2_match[1]; |
| 306 |
} |
| 307 |
|
| 308 |
if ( isset( $sender2_match[2] ) ) { |
| 309 |
$form_notifications[1]['fromAddress'] = $sender2_match[2]; |
| 310 |
} |
| 311 |
|
| 312 |
$conditional_config = array( |
| 313 |
'condition_status' => 'no', |
| 314 |
'cond_field' => array(), |
| 315 |
'cond_operator' => array( '=' ), |
| 316 |
'cond_option' => array( '- select -' ), |
| 317 |
'cond_logic' => 'all' |
| 318 |
); |
| 319 |
|
| 320 |
foreach ($form_tags as $menu_order => $cf_field) { |
| 321 |
$field_content = array(); |
| 322 |
|
| 323 |
switch ( $cf_field->basetype ) { |
| 324 |
case 'text': |
| 325 |
$field_content = array( |
| 326 |
'input_type' => 'text', |
| 327 |
'template' => 'text_field', |
| 328 |
'required' => $cf_field->is_required() ? 'yes' : 'no', |
| 329 |
'label' => $this->find_label( $properties['form'], $cf_field->type, $cf_field->name ), |
| 330 |
'name' => $cf_field->name, |
| 331 |
'is_meta' => 'yes', |
| 332 |
'help' => '', |
| 333 |
'css' => $cf_field->get_class_option(), |
| 334 |
'placeholder' => '', |
| 335 |
'default' => '', |
| 336 |
'size' => 40, |
| 337 |
'word_restriction' => '', |
| 338 |
'wpuf_cond' => $conditional_config |
| 339 |
); |
| 340 |
break; |
| 341 |
|
| 342 |
case 'email': |
| 343 |
$field_content = array( |
| 344 |
'input_type' => 'text', |
| 345 |
'template' => 'email_address', |
| 346 |
'required' => $cf_field->is_required() ? 'yes' : 'no', |
| 347 |
'label' => $this->find_label( $properties['form'], $cf_field->type, $cf_field->name ), |
| 348 |
'name' => $cf_field->name, |
| 349 |
'is_meta' => 'yes', |
| 350 |
'help' => '', |
| 351 |
'css' => $cf_field->get_class_option(), |
| 352 |
'placeholder' => '', |
| 353 |
'default' => '', |
| 354 |
'size' => 40, |
| 355 |
'word_restriction' => '', |
| 356 |
'wpuf_cond' => $conditional_config |
| 357 |
); |
| 358 |
break; |
| 359 |
|
| 360 |
case 'textarea': |
| 361 |
$field_content = array( |
| 362 |
'input_type' => 'textarea', |
| 363 |
'template' => 'textarea_field', |
| 364 |
'required' => $cf_field->is_required() ? 'yes' : 'no', |
| 365 |
'label' => $this->find_label( $properties['form'], $cf_field->type, $cf_field->name ), |
| 366 |
'name' => $cf_field->name, |
| 367 |
'is_meta' => 'yes', |
| 368 |
'help' => '', |
| 369 |
'css' => $cf_field->get_class_option(), |
| 370 |
'rows' => 5, |
| 371 |
'cols' => 25, |
| 372 |
'placeholder' => '', |
| 373 |
'default' => '', |
| 374 |
'rich' => 'no', |
| 375 |
'word_restriction' => '', |
| 376 |
'wpuf_cond' => $conditional_config |
| 377 |
); |
| 378 |
break; |
| 379 |
|
| 380 |
case 'select': |
| 381 |
$field_content = array( |
| 382 |
'input_type' => 'select', |
| 383 |
'template' => 'dropdown_field', |
| 384 |
'required' => $cf_field->is_required() ? 'yes' : 'no', |
| 385 |
'label' => $this->find_label( $properties['form'], $cf_field->type, $cf_field->name ), |
| 386 |
'name' => $cf_field->name, |
| 387 |
'is_meta' => 'yes', |
| 388 |
'help' => '', |
| 389 |
'css' => $cf_field->get_class_option(), |
| 390 |
'selected' => '', |
| 391 |
'inline' => 'no', |
| 392 |
'options' => $this->get_options( $cf_field ), |
| 393 |
'wpuf_cond' => $conditional_config |
| 394 |
); |
| 395 |
break; |
| 396 |
|
| 397 |
case 'date': |
| 398 |
$field_content = array( |
| 399 |
'input_type' => 'date', |
| 400 |
'template' => 'date_field', |
| 401 |
'required' => $cf_field->is_required() ? 'yes' : 'no', |
| 402 |
'label' => $this->find_label( $properties['form'], $cf_field->type, $cf_field->name ), |
| 403 |
'name' => $cf_field->name, |
| 404 |
'is_meta' => 'yes', |
| 405 |
'help' => '', |
| 406 |
'css' => $cf_field->get_class_option(), |
| 407 |
'format' => 'dd/mm/yy', |
| 408 |
'time' => '', |
| 409 |
'is_publish_time' => '', |
| 410 |
'wpuf_cond' => $conditional_config |
| 411 |
); |
| 412 |
break; |
| 413 |
|
| 414 |
case 'range': |
| 415 |
case 'number': |
| 416 |
|
| 417 |
$field_content = array( |
| 418 |
'input_type' => 'numeric_text', |
| 419 |
'template' => 'numeric_text_field', |
| 420 |
'required' => $cf_field->is_required() ? 'yes' : 'no', |
| 421 |
'label' => $this->find_label( $properties['form'], $cf_field->type, $cf_field->name ), |
| 422 |
'name' => $cf_field->name, |
| 423 |
'is_meta' => 'yes', |
| 424 |
'help' => '', |
| 425 |
'css' => $cf_field->get_class_option(), |
| 426 |
'placeholder' => '', |
| 427 |
'default' => $value, |
| 428 |
'size' => 40, |
| 429 |
'step_text_field' => $cf_field->get_option( 'step', 'int', true ), |
| 430 |
'min_value_field' => $cf_field->get_option( 'min', 'signed_int', true ), |
| 431 |
'max_value_field' => $cf_field->get_option( 'max', 'signed_int', true ), |
| 432 |
'wpuf_cond' => $conditional_config |
| 433 |
); |
| 434 |
|
| 435 |
if ( $cf_field->has_option( 'placeholder' ) || $cf_field->has_option( 'watermark' ) ) { |
| 436 |
$field_content['placeholder'] = $value; |
| 437 |
$value = ''; |
| 438 |
} |
| 439 |
|
| 440 |
$value = $cf_field->get_default_option( $value ); |
| 441 |
$value = wpcf7_get_hangover( $cf_field->name, $value ); |
| 442 |
$field_content['value'] = $value; |
| 443 |
|
| 444 |
break; |
| 445 |
|
| 446 |
case 'url': |
| 447 |
$field_content = array( |
| 448 |
'input_type' => 'text', |
| 449 |
'template' => 'website_url', |
| 450 |
'required' => $cf_field->is_required() ? 'yes' : 'no', |
| 451 |
'label' => $this->find_label( $properties['form'], $cf_field->type, $cf_field->name ), |
| 452 |
'name' => $cf_field->name, |
| 453 |
'is_meta' => 'yes', |
| 454 |
'help' => '', |
| 455 |
'css' => $cf_field->get_class_option(), |
| 456 |
'placeholder' => '', |
| 457 |
'default' => '', |
| 458 |
'size' => 40, |
| 459 |
'word_restriction' => '', |
| 460 |
'wpuf_cond' => $conditional_config |
| 461 |
); |
| 462 |
break; |
| 463 |
|
| 464 |
case 'range': |
| 465 |
# code... |
| 466 |
break; |
| 467 |
|
| 468 |
case 'checkbox': |
| 469 |
$field_content = array( |
| 470 |
'input_type' => 'checkbox', |
| 471 |
'template' => 'checkbox_field', |
| 472 |
'required' => $cf_field->is_required() ? 'yes' : 'no', |
| 473 |
'label' => $this->find_label( $properties['form'], $cf_field->type, $cf_field->name ), |
| 474 |
'name' => $cf_field->name, |
| 475 |
'is_meta' => 'yes', |
| 476 |
'help' => '', |
| 477 |
'css' => $cf_field->get_class_option(), |
| 478 |
'selected' => '', |
| 479 |
'inline' => 'no', |
| 480 |
'options' => $this->get_options( $cf_field ), |
| 481 |
'wpuf_cond' => $conditional_config |
| 482 |
); |
| 483 |
break; |
| 484 |
|
| 485 |
case 'radio': |
| 486 |
$field_content = array( |
| 487 |
'input_type' => 'radio', |
| 488 |
'template' => 'radio_field', |
| 489 |
'required' => $cf_field->is_required() ? 'yes' : 'no', |
| 490 |
'label' => $this->find_label( $properties['form'], $cf_field->type, $cf_field->name ), |
| 491 |
'name' => $cf_field->name, |
| 492 |
'is_meta' => 'yes', |
| 493 |
'help' => '', |
| 494 |
'css' => $cf_field->get_class_option(), |
| 495 |
'selected' => '', |
| 496 |
'inline' => 'no', |
| 497 |
'options' => $this->get_options( $cf_field ), |
| 498 |
'wpuf_cond' => $conditional_config |
| 499 |
); |
| 500 |
break; |
| 501 |
|
| 502 |
case 'acceptance': |
| 503 |
$field_content = array( |
| 504 |
'input_type' => 'toc', |
| 505 |
'template' => 'toc', |
| 506 |
'required' => $cf_field->is_required() ? 'yes' : 'no', |
| 507 |
'name' => $cf_field->name, |
| 508 |
'description' => $this->find_label( $properties['form'], $cf_field->type, $cf_field->name ), |
| 509 |
'name' => '', |
| 510 |
'is_meta' => 'yes', |
| 511 |
'show_checkbox' => true, |
| 512 |
'wpuf_cond' => $conditional_config |
| 513 |
); |
| 514 |
break; |
| 515 |
|
| 516 |
case 'quiz': |
| 517 |
# code... |
| 518 |
break; |
| 519 |
|
| 520 |
case 'recaptcha': |
| 521 |
$field_content = array( |
| 522 |
'input_type' => 'recaptcha', |
| 523 |
'template' => 'recaptcha', |
| 524 |
'required' => $cf_field->is_required() ? 'yes' : 'no', |
| 525 |
'label' => $this->find_label( $properties['form'], $cf_field->type, $cf_field->name ), |
| 526 |
'name' => $cf_field->name, |
| 527 |
'recaptcha_type' => 'enable_no_captcha', |
| 528 |
'is_meta' => 'yes', |
| 529 |
'help' => '', |
| 530 |
'css' => $cf_field->get_class_option(), |
| 531 |
'placeholder' => '', |
| 532 |
'default' => '', |
| 533 |
'size' => 40, |
| 534 |
'word_restriction' => '', |
| 535 |
'wpuf_cond' => $conditional_config |
| 536 |
); |
| 537 |
break; |
| 538 |
|
| 539 |
case 'file': |
| 540 |
|
| 541 |
$allowed_size = 1024; // default size 1 MB |
| 542 |
$allowed_file_types = array(); |
| 543 |
|
| 544 |
if ( $file_size_a = $cf_field->get_option( 'limit' ) ) { |
| 545 |
$limit_pattern = '/^([1-9][0-9]*)([kKmM]?[bB])?$/'; |
| 546 |
|
| 547 |
foreach ( $file_size_a as $file_size ) { |
| 548 |
if ( preg_match( $limit_pattern, $file_size, $matches ) ) { |
| 549 |
$allowed_size = (int) $matches[1]; |
| 550 |
|
| 551 |
if ( ! empty( $matches[2] ) ) { |
| 552 |
$kbmb = strtolower( $matches[2] ); |
| 553 |
|
| 554 |
if ( 'kb' == $kbmb ) { |
| 555 |
$allowed_size *= 1; |
| 556 |
} elseif ( 'mb' == $kbmb ) { |
| 557 |
$allowed_size *= 1024; |
| 558 |
} |
| 559 |
} |
| 560 |
|
| 561 |
break; |
| 562 |
} |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
|
| 567 |
if ( $file_types_a = $cf_field->get_option( 'filetypes' ) ) { |
| 568 |
foreach ( $file_types_a as $file_types ) { |
| 569 |
$file_types = explode( '|', $file_types ); |
| 570 |
|
| 571 |
foreach ( $file_types as $file_type ) { |
| 572 |
$file_type = trim( $file_type, '.' ); |
| 573 |
$file_type = str_replace( array( '.', '+', '*', '?' ), array( '\.', '\+', '\*', '\?' ), $file_type ); |
| 574 |
|
| 575 |
$_type = $this->get_file_type( $file_type ); |
| 576 |
|
| 577 |
if ( ! in_array( $_type, $allowed_file_types ) ) { |
| 578 |
$allowed_file_types[] = $_type; |
| 579 |
} |
| 580 |
} |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
$field_content = array( |
| 585 |
'input_type' => 'file_upload', |
| 586 |
'template' => 'file_upload', |
| 587 |
'required' => $cf_field->is_required() ? 'yes' : 'no', |
| 588 |
'label' => $this->find_label( $properties['form'], $cf_field->type, $cf_field->name ), |
| 589 |
'name' => $cf_field->name, |
| 590 |
'is_meta' => 'yes', |
| 591 |
'help' => '', |
| 592 |
'css' => $cf_field->get_class_option(), |
| 593 |
'max_size' => $allowed_size, |
| 594 |
'count' => '1', |
| 595 |
'extension' => $allowed_file_types, |
| 596 |
'wpuf_cond' => $conditional_config |
| 597 |
); |
| 598 |
break; |
| 599 |
|
| 600 |
case 'submit': |
| 601 |
$submit_label = isset( $cf_field->values[0] ) ? $cf_field->values[0] : ''; |
| 602 |
break; |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
if ( $field_content ) { |
| 607 |
$form_field = array( |
| 608 |
'post_type' => 'wpuf_input', |
| 609 |
'post_status' => 'publish', |
| 610 |
'post_content' => maybe_serialize( $field_content ), |
| 611 |
'post_parent' => $form_id, |
| 612 |
'menu_order' => $menu_order |
| 613 |
); |
| 614 |
|
| 615 |
wp_insert_post( $form_field ); |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
$form_settings['submit_text'] = $submit_label; |
| 620 |
|
| 621 |
update_post_meta( $form_id, 'wpuf_form_settings', $form_settings ); |
| 622 |
update_post_meta( $form_id, 'notifications', $form_notifications ); |
| 623 |
|
| 624 |
$imported++; |
| 625 |
|
| 626 |
$refs[$form->id] = array( |
| 627 |
'cf7_id' => $form->id, |
| 628 |
'weforms_id' => $form_id, |
| 629 |
'title' => '[CF7] ' . $form->title() |
| 630 |
); |
| 631 |
} |
| 632 |
|
| 633 |
$this->dismiss_prompt(); |
| 634 |
update_option( 'weforms_cf7_imported_forms', $refs ); |
| 635 |
|
| 636 |
wp_send_json_success( array( |
| 637 |
'title' => sprintf( _n( '%s form imported', '%s forms imported', $imported ), $imported ), |
| 638 |
'message' => __( 'We have successfully imported these forms into weForms. You could check and edit in-case anything weird happended.', 'weforms' ), |
| 639 |
'action' => __( 'Do you want to <strong>replace</strong> Contact From 7 shortcodes with weForms?', 'weforms' ), |
| 640 |
'refs' => $refs |
| 641 |
) ); |
| 642 |
} |
| 643 |
|
| 644 |
/** |
| 645 |
* Dismiss the notice |
| 646 |
* |
| 647 |
* @return void |
| 648 |
*/ |
| 649 |
public function dismiss_notice() { |
| 650 |
$this->dismiss_prompt(); |
| 651 |
|
| 652 |
wp_send_json_success(); |
| 653 |
} |
| 654 |
|
| 655 |
/** |
| 656 |
* Check capability if able to process |
| 657 |
* |
| 658 |
* @return void |
| 659 |
*/ |
| 660 |
private function check_caps() { |
| 661 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 662 |
wp_send_json_error( __( 'You are not allowed.', 'weforms' ) ); |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
/** |
| 667 |
* Try to find out the input label |
| 668 |
* |
| 669 |
* Loop through all the label tags and try to find out |
| 670 |
* if the field is inside that tag. Then strip out the field and find out label |
| 671 |
* |
| 672 |
* @param string $content |
| 673 |
* @param string $type |
| 674 |
* @param string $fieldname |
| 675 |
* |
| 676 |
* @return string |
| 677 |
*/ |
| 678 |
private function find_label( $content, $type, $fieldname ) { |
| 679 |
|
| 680 |
// find all enclosing label fields |
| 681 |
$pattern = '/<label>([ \w\S\r\n\t]+?)<\/label>/'; |
| 682 |
preg_match_all( $pattern, $content, $matches ); |
| 683 |
|
| 684 |
foreach ($matches[1] as $key => $match) { |
| 685 |
$match = trim( str_replace( "\n", '', $match ) ); |
| 686 |
|
| 687 |
preg_match( '/\[(?:' . preg_quote( $type ) . ') ' . $fieldname . '(?:[ ](.*?))?(?:[\r\n\t ](\/))?\]/', $match, $input_match ); |
| 688 |
|
| 689 |
if ( $input_match ) { |
| 690 |
$label = strip_tags( str_replace( $input_match[0], '', $match ) ); |
| 691 |
return trim( $label ); |
| 692 |
} |
| 693 |
} |
| 694 |
|
| 695 |
return $fieldname; |
| 696 |
} |
| 697 |
|
| 698 |
/** |
| 699 |
* Get file type for upload files |
| 700 |
* |
| 701 |
* @param string $extension |
| 702 |
* |
| 703 |
* @return boolean|string |
| 704 |
*/ |
| 705 |
private function get_file_type( $extension ) { |
| 706 |
$allowed_extensions = wpuf_allowed_extensions(); |
| 707 |
|
| 708 |
foreach ($allowed_extensions as $type => $extensions) { |
| 709 |
$_extensions = explode( ',', $extensions['ext'] ); |
| 710 |
|
| 711 |
if ( in_array( $extension, $_extensions ) ) { |
| 712 |
return $type; |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
return false; |
| 717 |
} |
| 718 |
|
| 719 |
/** |
| 720 |
* Translate to wpuf field options array |
| 721 |
* |
| 722 |
* @param object $field |
| 723 |
* |
| 724 |
* @return array |
| 725 |
*/ |
| 726 |
private function get_options( $field ) { |
| 727 |
$options = array(); |
| 728 |
|
| 729 |
if ( ! $field->raw_values ) { |
| 730 |
return $options; |
| 731 |
} |
| 732 |
|
| 733 |
foreach ($field->raw_values as $key => $value) { |
| 734 |
$options[ $value ] = $field->values[ $key ]; |
| 735 |
} |
| 736 |
|
| 737 |
return $options; |
| 738 |
} |
| 739 |
|
| 740 |
/** |
| 741 |
* Dismiss the prompt |
| 742 |
* |
| 743 |
* @return void |
| 744 |
*/ |
| 745 |
private function dismiss_prompt() { |
| 746 |
update_option( 'weforms_dismiss_cf7_notice', 'yes' ); |
| 747 |
} |
| 748 |
|
| 749 |
/** |
| 750 |
* If the prompt is dismissed |
| 751 |
* |
| 752 |
* @return boolean |
| 753 |
*/ |
| 754 |
private function is_dimissed() { |
| 755 |
return 'yes' == get_option( 'weforms_dismiss_cf7_notice' ); |
| 756 |
} |
| 757 |
|
| 758 |
} |
| 759 |
|