class-evf-fm-contactform7.php
| 1 | <?php |
| 2 | /** |
| 3 | * EverestForms Form Migrator ContactForm7 Class |
| 4 | * |
| 5 | * @package EverestForms\Admin |
| 6 | * @since 1.6.0 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * EVF_Fm_Contactform7 class. |
| 13 | */ |
| 14 | class EVF_Fm_Contactform7 extends EVF_Admin_Form_Migrator { |
| 15 | /** |
| 16 | * Define required properties. |
| 17 | * |
| 18 | * @since 2.0.6 |
| 19 | */ |
| 20 | public function init() { |
| 21 | |
| 22 | $this->name = 'Contact Form 7'; |
| 23 | $this->slug = 'contact-form-7'; |
| 24 | $this->path = 'contact-form-7/wp-contact-form-7.php'; |
| 25 | } |
| 26 | /** |
| 27 | * If the importer source is available. |
| 28 | * |
| 29 | * @since 2.0.6 |
| 30 | * |
| 31 | * @return bool |
| 32 | */ |
| 33 | protected function is_active() { |
| 34 | |
| 35 | return is_plugin_active( $this->path ); |
| 36 | } |
| 37 | /** |
| 38 | * Check is the plugin installed or not. |
| 39 | * |
| 40 | * @since 2.0.6 |
| 41 | * |
| 42 | * @return bool |
| 43 | */ |
| 44 | protected function is_installed() { |
| 45 | |
| 46 | return file_exists( trailingslashit( WP_PLUGIN_DIR ) . $this->path ); |
| 47 | } |
| 48 | |
| 49 | |
| 50 | /** |
| 51 | * Get all the forms. |
| 52 | * |
| 53 | * @since 2.0.6 |
| 54 | */ |
| 55 | public function get_forms() { |
| 56 | |
| 57 | $required_form_arr = array(); |
| 58 | |
| 59 | if ( ! $this->is_active() ) { |
| 60 | return $required_form_arr; |
| 61 | } |
| 62 | |
| 63 | $forms = \WPCF7_ContactForm::find( array( 'posts_per_page' => - 1 ) ); |
| 64 | |
| 65 | if ( empty( $forms ) ) { |
| 66 | return $required_form_arr; |
| 67 | } |
| 68 | |
| 69 | foreach ( $forms as $form ) { |
| 70 | if ( ! empty( $form ) && ( $form instanceof \WPCF7_ContactForm ) ) { |
| 71 | $required_form_arr[ $form->id() ] = $form->title(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return $required_form_arr; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get a single form. |
| 80 | * |
| 81 | * @since 2.0.6 |
| 82 | * |
| 83 | * @param int $id Form ID. |
| 84 | * |
| 85 | * @return \WPCF7_ContactForm|bool |
| 86 | */ |
| 87 | public function get_form( $id ) { |
| 88 | |
| 89 | $form = \WPCF7_ContactForm::find( |
| 90 | array( |
| 91 | 'posts_per_page' => 1, |
| 92 | 'p' => $id, |
| 93 | ) |
| 94 | ); |
| 95 | |
| 96 | if ( ! empty( $form[0] ) && ( $form[0] instanceof \WPCF7_ContactForm ) ) { |
| 97 | return $form[0]; |
| 98 | } |
| 99 | |
| 100 | return false; |
| 101 | } |
| 102 | /** |
| 103 | * Get the field label. |
| 104 | * |
| 105 | * @since 2.0.6 |
| 106 | * |
| 107 | * @param string $form Form data and settings. |
| 108 | * @param string $type Field type. |
| 109 | * @param string $name Field name. |
| 110 | * |
| 111 | * @return string |
| 112 | */ |
| 113 | private function get_field_label( $form, $type, $name = '' ) { |
| 114 | |
| 115 | // First pass: <label> tag wrapping the shortcode. |
| 116 | preg_match_all( '/<label>([ \w\S\r\n\t]+?)<\/label>/', $form, $matches ); |
| 117 | |
| 118 | foreach ( $matches[1] as $match ) { |
| 119 | $match = trim( str_replace( "\n", '', $match ) ); |
| 120 | preg_match( '/\[(?:' . preg_quote( $type ) . ')\*? ' . $name . '(?:[ ](.*?))?(?:[\r\n\t ](\/))?\]/', $match, $input_match ); |
| 121 | if ( ! empty( $input_match[0] ) ) { |
| 122 | $label_text = preg_replace( '/\[[^\]]+\]/', '', str_replace( $input_match[0], '', $match ) ); |
| 123 | return sanitize_text_field( trim( $label_text ) ); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | // Second pass: plain-text <label> on a separate line immediately before the field tag. |
| 128 | preg_match( '/<label[^>]*>([^<\[]*)<\/label>\s*\n\s*\[' . preg_quote( $type ) . '[* ]+' . preg_quote( $name ) . '/', $form, $sep_match ); |
| 129 | if ( ! empty( $sep_match[1] ) ) { |
| 130 | return sanitize_text_field( trim( $sep_match[1] ) ); |
| 131 | } |
| 132 | |
| 133 | // Third pass: inline text before the field tag on the same line (e.g. <li>Label [type name]</li>). |
| 134 | preg_match( '/([^<>\[\]\n\r]+?)\s+\[' . preg_quote( $type ) . '[* ]+' . preg_quote( $name ) . '[\s\]]/s', $form, $inline_match ); |
| 135 | if ( ! empty( $inline_match[1] ) ) { |
| 136 | $inline_label = sanitize_text_field( trim( strip_tags( $inline_match[1] ) ) ); |
| 137 | if ( ! empty( $inline_label ) ) { |
| 138 | return $inline_label; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // Fourth pass: text before shortcode in same block, ignoring HTML tags between them. |
| 143 | // Example: <p>Phone *<br /> [select* phone "iPhone"]</p> → "Phone" |
| 144 | preg_match( '/(?:^|>)\s*([^<\[\n\r]+?)\s*(?:<[^>]*>\s*)*\[' . preg_quote( $type ) . '[* ]+' . preg_quote( $name ) . '[\s\]]/', $form, $block_match ); |
| 145 | if ( ! empty( $block_match[1] ) ) { |
| 146 | $block_label = sanitize_text_field( trim( preg_replace( '/\s*\*\s*$/', '', trim( $block_match[1] ) ) ) ); |
| 147 | if ( ! empty( $block_label ) ) { |
| 148 | return $block_label; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // Fifth pass: text after the shortcode on the same line. |
| 153 | // Example: <p>[checkbox quickdelivery] Quick delivery (1 day)</p> → "Quick delivery (1 day)" |
| 154 | preg_match( '/\[' . preg_quote( $type ) . '[* ]*' . preg_quote( $name ) . '[^\]]*\]\s*([^<\[\n\r]+)/', $form, $after_match ); |
| 155 | if ( ! empty( $after_match[1] ) ) { |
| 156 | $after_label = sanitize_text_field( trim( $after_match[1] ) ); |
| 157 | if ( ! empty( $after_label ) ) { |
| 158 | return $after_label; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | $label = sprintf( /* translators: %1$s - field type, %2$s - field name if available. */ |
| 163 | esc_html__( '%1$s Field %2$s', 'everest-forms' ), |
| 164 | ucfirst( $type ), |
| 165 | ! empty( $name ) ? "($name)" : '' |
| 166 | ); |
| 167 | |
| 168 | return trim( $label ); |
| 169 | } |
| 170 | /** |
| 171 | * Get the field acceptance label. |
| 172 | * |
| 173 | * @since 2.0.6 |
| 174 | * |
| 175 | * @param string $form Form data and settings. |
| 176 | * @param string $name Field name. |
| 177 | * |
| 178 | * @return string |
| 179 | */ |
| 180 | private function get_field_acceptance_label( $form, $name ) { |
| 181 | $pattern = '/\[acceptance(?:[^]]* ' . preg_quote( $name ) . '[^]]*)?\](.*?)\[\/acceptance\]/s'; |
| 182 | |
| 183 | preg_match_all( $pattern, $form, $matches ); |
| 184 | |
| 185 | foreach ( $matches[1] as $match ) { |
| 186 | return strip_shortcodes( sanitize_text_field( $match ) ); |
| 187 | } |
| 188 | |
| 189 | return ''; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Extracts question-answer pairs from a [form] based on a specified name attribute. |
| 194 | * |
| 195 | * @since 2.0.6 |
| 196 | * |
| 197 | * @param string $form The input string containing the [form]. |
| 198 | * @param string $name The name attribute to match within the [form]. |
| 199 | * |
| 200 | * @return array An array containing question-answer pairs. |
| 201 | */ |
| 202 | private function get_quiz_questions_and_answers( $form, $name = '' ) { |
| 203 | $pattern = '/\[quiz ' . preg_quote( $name ) . '(.*?)\]/s'; |
| 204 | |
| 205 | preg_match_all( $pattern, $form, $matches ); |
| 206 | |
| 207 | $qa_pairs = array(); |
| 208 | |
| 209 | // If there is a match, extract question-answer pairs |
| 210 | if ( ! empty( $matches[1] ) ) { |
| 211 | preg_match_all( '/"([^"]+?)\|([^"]+?)"/', $matches[1][0], $pairs, PREG_SET_ORDER ); |
| 212 | |
| 213 | foreach ( $pairs as $pair ) { |
| 214 | $question = strip_shortcodes( sanitize_text_field( $pair[1] ) ); |
| 215 | $answer = strip_shortcodes( sanitize_text_field( $pair[2] ) ); |
| 216 | |
| 217 | $qa_pairs[] = array( |
| 218 | 'question' => $question, |
| 219 | 'answer' => $answer, |
| 220 | ); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | return $qa_pairs; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Lookup and return the placeholder or default value. |
| 229 | * |
| 230 | * @since 2.0.6 |
| 231 | * |
| 232 | * @param object $field Field object. |
| 233 | * @param string $type Type of the field. |
| 234 | * |
| 235 | * @return string |
| 236 | */ |
| 237 | private function get_field_placeholder_default( $field, $type = 'placeholder' ) { |
| 238 | |
| 239 | $placeholder = ''; |
| 240 | $default_value = (string) reset( $field->values ); |
| 241 | |
| 242 | if ( $field->has_option( 'placeholder' ) || $field->has_option( 'watermark' ) ) { |
| 243 | $placeholder = $default_value; |
| 244 | $default_value = ''; |
| 245 | } |
| 246 | |
| 247 | if ( $type === 'placeholder' ) { |
| 248 | return $placeholder; |
| 249 | } |
| 250 | |
| 251 | return $default_value; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Replace 3rd-party form provider tags/shortcodes with our own Smart Tags. |
| 256 | * |
| 257 | * @since 2.0.6 |
| 258 | * |
| 259 | * @param string $string Text to look for Smart Tags in. |
| 260 | * @param array $fields List of fields to process Smart Tags in. |
| 261 | * |
| 262 | * @return string |
| 263 | */ |
| 264 | private function get_smarttags( $string, $fields ) { |
| 265 | |
| 266 | preg_match_all( '/\[(.+?)\]/', $string, $tags ); |
| 267 | |
| 268 | if ( empty( $tags[1] ) ) { |
| 269 | return $string; |
| 270 | } |
| 271 | |
| 272 | // Process form-tags and mail-tags. |
| 273 | foreach ( $tags[1] as $tag ) { |
| 274 | foreach ( $fields as $field ) { |
| 275 | if ( ! empty( $field['cf7_name'] ) && $field['cf7_name'] === $tag ) { |
| 276 | $field_id = $this->get_field_id_for_smarttags( $field ); |
| 277 | $string = str_replace( '[' . $tag . ']', '{field_id="' . $field_id . '"}', $string ); |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | // Process CF7 tags that we can map with EVF alternatives. |
| 283 | $string = str_replace( |
| 284 | array( |
| 285 | '[_remote_ip]', |
| 286 | '[_date]', |
| 287 | '[time]', |
| 288 | '[_post_title]', |
| 289 | '[_post_url]', |
| 290 | '[_url]', |
| 291 | '[_post_author]', |
| 292 | '[_post_author_email]', |
| 293 | '[_site_admin_email]', |
| 294 | '[_user_email]', |
| 295 | '[_user_login]', |
| 296 | '[_user_first_name]', |
| 297 | '[_user_last_name]', |
| 298 | '[_user_display_name]', |
| 299 | '[_site_title]', |
| 300 | '[_post_name]', |
| 301 | ), |
| 302 | array( |
| 303 | '{user_ip_address}', |
| 304 | '{current_date}', |
| 305 | '{current_time}', |
| 306 | '{post_title}', |
| 307 | '{page_url}', |
| 308 | '{page_url}', |
| 309 | '{author_name}', |
| 310 | '{author_email}', |
| 311 | '{admin_email}', |
| 312 | '{user_email}', |
| 313 | '{user_name}', |
| 314 | '{first_name}', |
| 315 | '{last_name}', |
| 316 | '{display_name}', |
| 317 | '{site_name}', |
| 318 | '{form_name}', |
| 319 | ), |
| 320 | $string |
| 321 | ); |
| 322 | |
| 323 | // Replace those CF7 that are used in Notifications by default and that we can't leave empty. |
| 324 | $string = str_replace( |
| 325 | array( |
| 326 | '[_site_description]', |
| 327 | '[_site_url]', |
| 328 | ), |
| 329 | array( |
| 330 | get_bloginfo( 'description' ), |
| 331 | get_bloginfo( 'url' ), |
| 332 | ), |
| 333 | $string |
| 334 | ); |
| 335 | |
| 336 | /* |
| 337 | * We are not replacing certain special CF7 tags: [_user_url], [_user_agent], [_invalid_fields], [_serial_number] |
| 338 | * [_post_id]. |
| 339 | * Without them some logic may be broken and for user it will be harder to stop missing strings. |
| 340 | * With them - they can see strange text and will be able to understand, based on the tag name, which value is expected there. |
| 341 | */ |
| 342 | |
| 343 | return $string; |
| 344 | } |
| 345 | /** |
| 346 | * Find Reply-To in headers if provided. |
| 347 | * |
| 348 | * @since 2.0.6 |
| 349 | * |
| 350 | * @param string $headers CF7 email headers. |
| 351 | * @param array $fields List of fields. |
| 352 | * |
| 353 | * @return string |
| 354 | */ |
| 355 | private function get_replyto( $headers, $fields ) { |
| 356 | |
| 357 | if ( strpos( $headers, 'Reply-To:' ) !== false ) { |
| 358 | preg_match( '/Reply-To: \[(.+?)\]/', $headers, $tag ); |
| 359 | |
| 360 | if ( ! empty( $tag[1] ) ) { |
| 361 | foreach ( $fields as $field ) { |
| 362 | if ( ! empty( $field['cf7_name'] ) && $field['cf7_name'] === $tag[1] ) { |
| 363 | $field_id = $this->get_field_id_for_smarttags( $field ); |
| 364 | return '{field_id="' . $field_id . '"}'; |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | return ''; |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * Sender information. |
| 375 | * |
| 376 | * @since 2.0.6 |
| 377 | * |
| 378 | * @param string $sender Sender strings in "Name <email@example.com>" format. |
| 379 | * @param array $fields List of fields. |
| 380 | * |
| 381 | * @return bool|array |
| 382 | */ |
| 383 | private function get_sender_details( $sender, $fields ) { |
| 384 | |
| 385 | preg_match( '/(.+?)\<(.+?)\>/', $sender, $tag ); |
| 386 | |
| 387 | if ( ! empty( $tag[1] ) && ! empty( $tag[2] ) ) { |
| 388 | return array( |
| 389 | 'name' => $this->get_smarttags( $tag[1], $fields ), |
| 390 | 'address' => $this->get_smarttags( $tag[2], $fields ), |
| 391 | ); |
| 392 | } |
| 393 | |
| 394 | return false; |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Email notification settings. |
| 399 | * |
| 400 | * @since 2.0.6 |
| 401 | * @param [array] $form The form. |
| 402 | * @param [object] $cf7_form The contact form 7 form. |
| 403 | */ |
| 404 | private function get_email_notification_settings( $form, $cf7_form ) { |
| 405 | $cf7_form_name = $cf7_form->title(); |
| 406 | $notification_settings = array( |
| 407 | 'connection_1' => array( |
| 408 | 'enable_email_notification' => '1', |
| 409 | 'connection_name' => esc_html__( 'Admin Notification', 'everest-forms' ), |
| 410 | 'evf_to_email' => '{admin_email}', |
| 411 | 'evf_from_name' => esc_html__( 'Everest Forms', 'everest-forms' ), |
| 412 | 'evf_from_email' => '{admin_email}', |
| 413 | 'evf_reply_to' => '', |
| 414 | 'evf_email_subject' => sprintf( '%s - %s', esc_html__( 'New Form Entry', 'everest-forms' ), esc_attr( $cf7_form_name ) ), |
| 415 | 'evf_email_message' => '{all_fields}', |
| 416 | ), |
| 417 | ); |
| 418 | |
| 419 | return $notification_settings; |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * Mapping the form setting. |
| 424 | * |
| 425 | * @since 2.0.6 |
| 426 | * @param [array] $form The form data. |
| 427 | * @param [object] $cf7_form The wpforms form settings. |
| 428 | * @param [int] $cf7_form_id The wpforms ID. |
| 429 | */ |
| 430 | private function get_form_settings( $form, $cf7_form, $cf7_form_id ) { |
| 431 | $cf7_form_name = $cf7_form->title(); |
| 432 | $form['settings'] = array( |
| 433 | 'email' => apply_filters( 'evf_fm_' . $this->slug . 'email_notification_settings', $this->get_email_notification_settings( $form, $cf7_form ), $form, $cf7_form ), |
| 434 | 'form_title' => sanitize_text_field( $cf7_form_name ), |
| 435 | 'form_description' => '', |
| 436 | 'form_disable_message' => esc_html__( 'This form is disabled.', 'everest-forms' ), |
| 437 | 'successful_form_submission_message' => ! empty( $cf7_form->message( 'mail_sent_ok' ) ) ? sanitize_text_field( $cf7_form->message( 'mail_sent_ok' ) ) : esc_html__( 'Thanks for contacting us! We will be in touch with you shortly.', 'everest-forms' ), |
| 438 | 'submission_message_scroll' => '1', |
| 439 | 'redirect_to' => 'same', |
| 440 | 'custom_page' => '', |
| 441 | 'external_url' => '', |
| 442 | 'enable_redirect_query_string' => 0, |
| 443 | 'query_string' => '', |
| 444 | 'layout_class' => 'default', |
| 445 | 'form_class' => '', |
| 446 | 'submit_button_text' => esc_html__( 'Submit', 'everest-forms' ), |
| 447 | 'submit_button_processing_text' => esc_html__( 'Processing', 'everest-forms' ), |
| 448 | 'submit_button_class' => '', |
| 449 | 'ajax_form_submission' => '0', |
| 450 | 'disabled_entries' => '0', |
| 451 | 'honeypot' => '1', |
| 452 | 'akismet' => '0', |
| 453 | 'akismet_protection_type' => 'validation_failed', |
| 454 | 'recaptcha_support' => '0', |
| 455 | 'evf-enable-custom-css' => '0', |
| 456 | 'evf-custom-css' => '', |
| 457 | 'evf-enable-custom-js' => '0', |
| 458 | 'evf-custom-js' => '', |
| 459 | 'structure' => array(), |
| 460 | 'imported_from' => array( |
| 461 | 'form_id' => absint( $cf7_form_id ), |
| 462 | 'form_from' => $this->slug, |
| 463 | ), |
| 464 | ); |
| 465 | |
| 466 | return $form; |
| 467 | } |
| 468 | /** |
| 469 | * Mapped the form datas. |
| 470 | * |
| 471 | * @since 2.0.6 |
| 472 | * @param [array] $cf7_form_ids |
| 473 | */ |
| 474 | public function get_fm_mapped_form_data( $cf7_form_ids ) { |
| 475 | $cf7_forms_data = array(); |
| 476 | foreach ( $cf7_form_ids as $cf7_form_id ) { |
| 477 | $cf7_form = $this->get_form( $cf7_form_id ); |
| 478 | |
| 479 | if ( ! $cf7_form ) { |
| 480 | $cf7_forms_data[ $cf7_form_id ] = $cf7_form; |
| 481 | continue; |
| 482 | } |
| 483 | |
| 484 | $cf7_form_name = $cf7_form->title(); |
| 485 | $cf7_fields = $cf7_form->scan_form_tags(); |
| 486 | $cf7_properties = $cf7_form->get_properties(); |
| 487 | $cf7_recaptcha = false; |
| 488 | $fields_pro_plan = array( 'quiz' ); |
| 489 | $fields_pro_omit = array(); |
| 490 | $fields_unsupported = array(); |
| 491 | $upgrade_plan = array(); |
| 492 | $upgrade_omit = array(); |
| 493 | $unsupported = array(); |
| 494 | |
| 495 | $form = array( |
| 496 | 'id' => '', |
| 497 | 'form_enabled' => '1', |
| 498 | 'form_field_id' => '', |
| 499 | 'form_fields' => array(), |
| 500 | 'settings' => array(), |
| 501 | ); |
| 502 | // Settings. |
| 503 | $form = apply_filters( 'evf_fm_' . $this->slug . '_form_after_settings_mapping', $this->get_form_settings( $form, $cf7_form, $cf7_form_id ), $cf7_form_id, $cf7_form ); |
| 504 | |
| 505 | // Mapping Fields. |
| 506 | if ( empty( $cf7_fields ) ) { |
| 507 | // If form does not contain fields, bail. |
| 508 | wp_send_json_error( |
| 509 | array( |
| 510 | 'form_name' => sanitize_text_field( $cf7_form_name ), |
| 511 | 'message' => esc_html__( 'No form fields found.', 'everest-forms' ), |
| 512 | ) |
| 513 | ); |
| 514 | } |
| 515 | // Convert fields. |
| 516 | foreach ( $cf7_fields as $cf7_field ) { |
| 517 | if ( ! $cf7_field instanceof \WPCF7_FormTag ) { |
| 518 | continue; |
| 519 | } |
| 520 | |
| 521 | // Try to determine field label to use. |
| 522 | $label = $this->get_field_label( $cf7_properties['form'], $cf7_field->basetype, $cf7_field->name ); |
| 523 | |
| 524 | // If no label found, fall back to placeholder, then default value, then clean generic. |
| 525 | $generic_label = trim( |
| 526 | sprintf( |
| 527 | /* translators: %1$s - field type, %2$s - field name if available. */ |
| 528 | esc_html__( '%1$s Field %2$s', 'everest-forms' ), |
| 529 | ucfirst( $cf7_field->basetype ), |
| 530 | ! empty( $cf7_field->name ) ? "({$cf7_field->name})" : '' |
| 531 | ) |
| 532 | ); |
| 533 | if ( $label === $generic_label ) { |
| 534 | $placeholder = $this->get_field_placeholder_default( $cf7_field ); |
| 535 | $default_val = $this->get_field_placeholder_default( $cf7_field, 'default' ); |
| 536 | if ( ! empty( $placeholder ) ) { |
| 537 | $label = $placeholder; |
| 538 | } elseif ( ! empty( $default_val ) && ! filter_var( $default_val, FILTER_VALIDATE_URL ) ) { |
| 539 | $label = $default_val; |
| 540 | } elseif ( preg_match( '/^' . preg_quote( $cf7_field->basetype, '/' ) . '-\d+$/', $cf7_field->name ) ) { |
| 541 | $label = trim( sprintf( esc_html__( '%s Field', 'everest-forms' ), ucfirst( $cf7_field->basetype ) ) ); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | // Next, check if field is unsupported. If supported make note and |
| 546 | // then continue to the next field. |
| 547 | if ( in_array( $cf7_field->basetype, $fields_unsupported, true ) ) { |
| 548 | $unsupported[] = $label; |
| 549 | |
| 550 | continue; |
| 551 | } |
| 552 | if ( ( ! defined( 'EFP_VERSION' ) || version_compare( EFP_VERSION, '1.7.1', '<' ) ) && in_array( $cf7_field->basetype, $fields_pro_plan, true ) ) { |
| 553 | $upgrade_plan[] = $label; |
| 554 | continue; |
| 555 | } |
| 556 | if ( ( ! defined( 'EFP_VERSION' ) || version_compare( EFP_VERSION, '1.7.1', '<' ) ) && in_array( $cf7_field->basetype, $fields_pro_omit, true ) ) { |
| 557 | $upgrade_omit[] = $label; |
| 558 | |
| 559 | continue; |
| 560 | } |
| 561 | |
| 562 | // Calculating the field ids and storing next field id. |
| 563 | if ( ! empty( $form['form_field_id'] ) ) { |
| 564 | $form_field_id = absint( $form['form_field_id'] ); |
| 565 | ++$form['form_field_id']; |
| 566 | } else { |
| 567 | $form_field_id = '0'; |
| 568 | $form['form_field_id'] = '1'; |
| 569 | } |
| 570 | |
| 571 | $cf7_field_classes = ''; |
| 572 | if ( $cf7_field->has_option( 'class' ) ) { |
| 573 | $cf7_field_classes = implode( ' ', $cf7_field->get_option( 'class', '', false ) ); |
| 574 | } |
| 575 | |
| 576 | $field_id = evf_get_random_string() . '-' . $form_field_id; |
| 577 | // Mapping the field type and formtting the fields settings. |
| 578 | switch ( $cf7_field->basetype ) { |
| 579 | case 'text': |
| 580 | case 'textarea': |
| 581 | $type = $cf7_field->basetype; |
| 582 | $form['structure']['row_1']['grid_1'][] = $field_id; |
| 583 | $form['form_fields'][ $field_id ] = array( |
| 584 | 'id' => $field_id, |
| 585 | 'type' => $type, |
| 586 | 'label' => $label, |
| 587 | 'meta-key' => $cf7_field->name, |
| 588 | 'description' => '', |
| 589 | 'required' => $cf7_field->is_required() ? '1' : '', |
| 590 | 'required_field_message_setting' => 'global', |
| 591 | 'required-field-message' => '', |
| 592 | 'placeholder' => $this->get_field_placeholder_default( $cf7_field ), |
| 593 | 'limit_count' => '1', |
| 594 | 'limit_mode' => 'characters', |
| 595 | 'min_length_count' => '1', |
| 596 | 'min_length_mode' => 'characters', |
| 597 | 'default_value' => $this->get_field_placeholder_default( $cf7_field, 'default' ), |
| 598 | 'css' => $cf7_field_classes, |
| 599 | 'input_mask' => '', |
| 600 | 'regex_value' => '', |
| 601 | 'regex_message' => esc_html__( 'Please provide a valid value for this field.', 'everest-forms' ), |
| 602 | 'cf7_name' => $cf7_field->name, |
| 603 | ); |
| 604 | break; |
| 605 | case 'email': |
| 606 | $type = $cf7_field->basetype; |
| 607 | $form['structure']['row_1']['grid_1'][] = $field_id; |
| 608 | $form['form_fields'][ $field_id ] = array( |
| 609 | 'id' => $field_id, |
| 610 | 'type' => $type, |
| 611 | 'label' => $label, |
| 612 | 'meta-key' => $cf7_field->name, |
| 613 | 'description' => '', |
| 614 | 'required' => $cf7_field->is_required() ? '1' : '', |
| 615 | 'required_field_message_setting' => 'global', |
| 616 | 'required-field-message' => '', |
| 617 | 'placeholder' => $this->get_field_placeholder_default( $cf7_field ), |
| 618 | 'default_value' => $this->get_field_placeholder_default( $cf7_field, 'default' ), |
| 619 | 'css' => $cf7_field_classes, |
| 620 | 'regex_value' => '', |
| 621 | 'regex_message' => esc_html__( 'Please provide a valid value for this field.', 'everest-forms' ), |
| 622 | 'cf7_name' => $cf7_field->name, |
| 623 | ); |
| 624 | break; |
| 625 | case 'url': |
| 626 | $type = $cf7_field->basetype; |
| 627 | $form['structure']['row_1']['grid_1'][] = $field_id; |
| 628 | $form['form_fields'][ $field_id ] = array( |
| 629 | 'id' => $field_id, |
| 630 | 'type' => $type, |
| 631 | 'label' => $label, |
| 632 | 'meta-key' => $cf7_field->name, |
| 633 | 'description' => '', |
| 634 | 'required' => $cf7_field->is_required() ? '1' : '', |
| 635 | 'required_field_message_setting' => 'global', |
| 636 | 'required-field-message' => '', |
| 637 | 'placeholder' => $this->get_field_placeholder_default( $cf7_field ), |
| 638 | 'default_value' => $this->get_field_placeholder_default( $cf7_field, 'default' ), |
| 639 | 'css' => $cf7_field_classes, |
| 640 | 'regex_value' => '', |
| 641 | 'regex_message' => esc_html__( 'Please provide a valid value for this field.', 'everest-forms' ), |
| 642 | 'cf7_name' => $cf7_field->name, |
| 643 | ); |
| 644 | break; |
| 645 | case 'date': |
| 646 | $type = 'date-time'; |
| 647 | $form['structure']['row_1']['grid_1'][] = $field_id; |
| 648 | $form['form_fields'][ $field_id ] = array( |
| 649 | 'id' => $field_id, |
| 650 | 'type' => $type, |
| 651 | 'label' => $label, |
| 652 | 'meta-key' => $cf7_field->name, |
| 653 | 'format' => 'date', |
| 654 | 'datetime_style' => 'picker', |
| 655 | 'description' => '', |
| 656 | 'required' => $cf7_field->is_required() ? '1' : '', |
| 657 | 'required_field_message_setting' => 'global', |
| 658 | 'required-field-message' => '', |
| 659 | 'placeholder' => $this->get_field_placeholder_default( $cf7_field ), |
| 660 | 'date_format' => 'Y-m-d', |
| 661 | 'disable_dates' => '', |
| 662 | 'date_localization' => 'en', |
| 663 | 'date_timezone' => 'Default', |
| 664 | 'date_mode' => 'single', |
| 665 | 'min_date' => '', |
| 666 | 'max_date' => '', |
| 667 | 'min_date_range' => '', |
| 668 | 'max_date_range' => '', |
| 669 | 'time_interval' => '15', |
| 670 | 'time_format' => 'g:i A', |
| 671 | 'min_time_hour' => '9', |
| 672 | 'min_time_minute' => '30', |
| 673 | 'max_time_hour' => '18', |
| 674 | 'max_time_minute' => '30', |
| 675 | 'css' => $cf7_field_classes, |
| 676 | ); |
| 677 | break; |
| 678 | // Select, radio, and checkbox fields. |
| 679 | case 'select': |
| 680 | case 'radio': |
| 681 | case 'checkbox': |
| 682 | $type = $cf7_field->basetype; |
| 683 | $form['structure']['row_1']['grid_1'][] = $field_id; |
| 684 | |
| 685 | $choices = array(); |
| 686 | $options = (array) $cf7_field->labels; |
| 687 | if ( $cf7_field->has_option( 'default' ) ) { |
| 688 | $default = $cf7_field->get_option( 'default', '', true ); |
| 689 | } else { |
| 690 | $default = ''; |
| 691 | } |
| 692 | foreach ( $options as $key => $option ) { |
| 693 | $choice = array( |
| 694 | 'label' => $option, |
| 695 | 'value' => '', |
| 696 | 'image' => '', |
| 697 | ); |
| 698 | if ( in_array( (string) ( $key + 1 ), explode( '_', $default ), true ) ) { |
| 699 | $choice['default'] = '1'; |
| 700 | } |
| 701 | $choices[] = $choice; |
| 702 | } |
| 703 | |
| 704 | $form['form_fields'][ $field_id ] = array( |
| 705 | 'id' => $field_id, |
| 706 | 'type' => $type, |
| 707 | 'label' => $label, |
| 708 | 'meta-key' => $cf7_field->name, |
| 709 | 'choices' => $choices, |
| 710 | 'description' => '', |
| 711 | 'required' => $cf7_field->is_required() ? '1' : '', |
| 712 | 'required_field_message_setting' => 'global', |
| 713 | 'required-field-message' => '', |
| 714 | 'placeholder' => $this->get_field_placeholder_default( $cf7_field ), |
| 715 | 'css' => $cf7_field_classes, |
| 716 | 'cf7_name' => $cf7_field->name, |
| 717 | ); |
| 718 | |
| 719 | if ( 'select' === $cf7_field->basetype ) { |
| 720 | if ( $cf7_field->has_option( 'multiple' ) ) { |
| 721 | $form['form_fields'][ $field_id ]['multiple_choices'] = '1'; |
| 722 | } |
| 723 | } |
| 724 | if ( 'radio' === $cf7_field->basetype || 'checkbox' === $cf7_field->basetype ) { |
| 725 | $form['form_fields'][ $field_id ]['input_columns'] = ''; |
| 726 | } |
| 727 | |
| 728 | if ( 'checkbox' === $cf7_field->basetype ) { |
| 729 | $form['form_fields'][ $field_id ]['choice_limit'] = ''; |
| 730 | } |
| 731 | break; |
| 732 | case 'number': |
| 733 | $type = $cf7_field->basetype; |
| 734 | $form['structure']['row_1']['grid_1'][] = $field_id; |
| 735 | $form['form_fields'][ $field_id ] = array( |
| 736 | 'id' => $field_id, |
| 737 | 'type' => $type, |
| 738 | 'label' => $label, |
| 739 | 'meta-key' => $cf7_field->name, |
| 740 | 'description' => '', |
| 741 | 'required' => $cf7_field->is_required() ? '1' : '', |
| 742 | 'required_field_message_setting' => 'global', |
| 743 | 'required-field-message' => '', |
| 744 | 'step' => $cf7_field->get_option( 'step', 'int', true ), |
| 745 | 'min_value' => $cf7_field->get_option( 'min', 'signed_int', true ), |
| 746 | 'max_value' => $cf7_field->get_option( 'max', 'signed_int', true ), |
| 747 | 'placeholder' => $this->get_field_placeholder_default( $cf7_field ), |
| 748 | 'default_value' => $this->get_field_placeholder_default( $cf7_field, 'default' ), |
| 749 | 'css' => $cf7_field_classes, |
| 750 | 'regex_value' => '', |
| 751 | 'regex_message' => esc_html__( 'Please provide a valid value for this field.', 'everest-forms' ), |
| 752 | 'cf7_name' => $cf7_field->name, |
| 753 | ); |
| 754 | break; |
| 755 | case 'tel': |
| 756 | $type = 'phone'; |
| 757 | $form['structure']['row_1']['grid_1'][] = $field_id; |
| 758 | $form['form_fields'][ $field_id ] = array( |
| 759 | 'id' => $field_id, |
| 760 | 'type' => $type, |
| 761 | 'label' => $label, |
| 762 | 'meta-key' => $cf7_field->name, |
| 763 | 'phone_format' => 'smart', |
| 764 | 'description' => '', |
| 765 | 'input_mask' => '(999) 999-9999', |
| 766 | 'required' => $cf7_field->is_required() ? '1' : '', |
| 767 | 'required_field_message_setting' => 'global', |
| 768 | 'required-field-message' => '', |
| 769 | 'tooltip_description' => '', |
| 770 | 'validate_message' => esc_html__( 'This field value needs to be unique.', 'everest-forms' ), |
| 771 | 'placeholder' => '', |
| 772 | 'default_value' => '', |
| 773 | 'css' => $cf7_field_classes, |
| 774 | ); |
| 775 | break; |
| 776 | case 'file': |
| 777 | $allowed_size = 1024; // default size 1 MB |
| 778 | $allowed_file_types = array(); |
| 779 | |
| 780 | if ( $file_size_a = $cf7_field->get_option( 'limit' ) ) { |
| 781 | $limit_pattern = '/^([1-9][0-9]*)([kKmM]?[bB])?$/'; |
| 782 | |
| 783 | foreach ( $file_size_a as $file_size ) { |
| 784 | if ( preg_match( $limit_pattern, $file_size, $matches ) ) { |
| 785 | $allowed_size = (int) $matches[1]; |
| 786 | |
| 787 | if ( ! empty( $matches[2] ) ) { |
| 788 | $kbmb = strtolower( $matches[2] ); |
| 789 | |
| 790 | if ( 'kb' == $kbmb ) { |
| 791 | $allowed_size *= 1; |
| 792 | } elseif ( 'mb' == $kbmb ) { |
| 793 | $allowed_size *= 1024; |
| 794 | } |
| 795 | } |
| 796 | |
| 797 | break; |
| 798 | } |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | if ( $file_types_a = $cf7_field->get_option( 'filetypes' ) ) { |
| 803 | foreach ( $file_types_a as $file_types ) { |
| 804 | $file_types = explode( '|', $file_types ); |
| 805 | |
| 806 | foreach ( $file_types as $file_type ) { |
| 807 | $file_type = trim( $file_type, '.' ); |
| 808 | $file_type = str_replace( array( '.', '+', '*', '?' ), array( '\.', '\+', '\*', '\?' ), $file_type ); |
| 809 | |
| 810 | if ( ! in_array( $file_type, $allowed_file_types ) ) { |
| 811 | $allowed_file_types[] = $file_type; |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | $type = 'file-upload'; |
| 818 | $form['structure']['row_1']['grid_1'][] = $field_id; |
| 819 | $form['form_fields'][ $field_id ] = array( |
| 820 | 'id' => $field_id, |
| 821 | 'type' => $type, |
| 822 | 'label' => $label, |
| 823 | 'meta-key' => $cf7_field->name, |
| 824 | 'description' => '', |
| 825 | 'upload_message' => esc_html__( 'Drop your file here or click here to upload', 'everest-forms' ), |
| 826 | 'limit_message' => esc_html__( 'You can upload up to 1 files.', 'everest-forms' ), |
| 827 | 'extensions' => implode( ',', $allowed_file_types ), |
| 828 | 'max_size' => $allowed_size, |
| 829 | 'max_file_number' => '1', |
| 830 | 'required' => $cf7_field->is_required() ? '1' : '', |
| 831 | 'required_field_message_setting' => 'global', |
| 832 | 'required-field-message' => '', |
| 833 | 'tooltip_description' => '', |
| 834 | 'media_library' => '', |
| 835 | 'css' => $cf7_field_classes, |
| 836 | ); |
| 837 | break; |
| 838 | case 'acceptance': |
| 839 | $type = 'privacy-policy'; |
| 840 | $acceptance_text = $this->get_field_acceptance_label( $cf7_properties['form'], $cf7_field->name ); |
| 841 | $form['structure']['row_1']['grid_1'][] = $field_id; |
| 842 | $form['form_fields'][ $field_id ] = array( |
| 843 | 'id' => $field_id, |
| 844 | 'type' => $type, |
| 845 | 'label' => ! empty( $acceptance_text ) ? $acceptance_text : $label, |
| 846 | 'meta-key' => $cf7_field->name, |
| 847 | 'description' => '', |
| 848 | 'consent_message' => $acceptance_text, |
| 849 | 'add_local_page' => '', |
| 850 | 'add_custom_link_label' => '', |
| 851 | 'add_custom_link_url' => '', |
| 852 | 'required' => $cf7_field->is_required() ? '1' : '', |
| 853 | 'required_field_message_setting' => 'global', |
| 854 | 'required-field-message' => '', |
| 855 | 'tooltip_description' => '', |
| 856 | 'css' => $cf7_field_classes, |
| 857 | ); |
| 858 | break; |
| 859 | case 'quiz': |
| 860 | $type = 'captcha'; |
| 861 | $form['structure']['row_1']['grid_1'][] = $field_id; |
| 862 | $form['form_fields'][ $field_id ] = array( |
| 863 | 'id' => $field_id, |
| 864 | 'type' => $type, |
| 865 | 'label' => $label, |
| 866 | 'description' => '', |
| 867 | 'required' => '1', |
| 868 | 'format' => 'question', |
| 869 | 'questions' => $this->get_quiz_questions_and_answers( $cf7_properties['form'], $cf7_field->name ), |
| 870 | 'placeholder' => '', |
| 871 | 'css' => $cf7_field_classes, |
| 872 | ); |
| 873 | break; |
| 874 | case 'hidden': |
| 875 | $type = 'hidden'; |
| 876 | $form['structure']['row_1']['grid_1'][] = $field_id; |
| 877 | $form['form_fields'][ $field_id ] = array( |
| 878 | 'id' => $field_id, |
| 879 | 'type' => $type, |
| 880 | 'label' => $label, |
| 881 | 'meta-key' => $cf7_field->name, |
| 882 | 'default_value' => (string) reset( $cf7_field->values ), |
| 883 | 'css' => $cf7_field_classes, |
| 884 | 'cf7_name' => $cf7_field->name, |
| 885 | ); |
| 886 | break; |
| 887 | // ReCAPTCHA field. |
| 888 | case 'recaptcha': |
| 889 | $cf7_recaptcha = true; |
| 890 | |
| 891 | } |
| 892 | } |
| 893 | |
| 894 | // ReCAPTCHA. |
| 895 | if ( $cf7_recaptcha ) { |
| 896 | // If the user has already defined v2 reCAPTCHA keys in the EVFForms |
| 897 | // settings, use those. |
| 898 | $type = get_option( 'everest_forms_recaptcha_type', 'v2' ); |
| 899 | if ( 'v2' === $type ) { |
| 900 | $site_key = get_option( 'everest_forms_recaptcha_v2_site_key', '' ); |
| 901 | $secret_key = get_option( 'everest_forms_recaptcha_v2_secret_key', '' ); |
| 902 | // Try to abstract keys from CF7. |
| 903 | if ( empty( $site_key ) || empty( $secret_key ) ) { |
| 904 | $cf7_settings = get_option( 'wpcf7' ); |
| 905 | |
| 906 | if ( |
| 907 | ! empty( $cf7_settings['recaptcha'] ) && |
| 908 | is_array( $cf7_settings['recaptcha'] ) |
| 909 | ) { |
| 910 | foreach ( $cf7_settings['recaptcha'] as $key => $val ) { |
| 911 | if ( ! empty( $key ) && ! empty( $val ) ) { |
| 912 | $site_key = $key; |
| 913 | $secret_key = $val; |
| 914 | } |
| 915 | } |
| 916 | update_option( 'everest_forms_recaptcha_v2_site_key', $site_key ); |
| 917 | update_option( 'everest_forms_recaptcha_v2_secret_key', $secret_key ); |
| 918 | } |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | // Don't enable reCAPTCHA if user had configured invisible reCAPTCHA. |
| 923 | if ( |
| 924 | $type === 'v2' && |
| 925 | ! empty( $site_key ) && |
| 926 | ! empty( $secret_key ) |
| 927 | ) { |
| 928 | $form['settings']['recaptcha_support'] = '1'; |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | // Setup email notifications. |
| 933 | if ( ! empty( $cf7_properties['mail']['subject'] ) ) { |
| 934 | $form['settings']['email']['connection_1']['evf_email_subject'] = $this->get_smarttags( $cf7_properties['mail']['subject'], $form['form_fields'] ); |
| 935 | } |
| 936 | |
| 937 | if ( ! empty( $cf7_properties['mail']['recipient'] ) ) { |
| 938 | $form['settings']['email']['connection_1']['evf_to_email'] = $this->get_smarttags( $cf7_properties['mail']['recipient'], $form['form_fields'] ); |
| 939 | } |
| 940 | |
| 941 | if ( ! empty( $cf7_properties['mail']['body'] ) ) { |
| 942 | $form['settings']['email']['connection_1']['evf_email_message'] = $this->get_smarttags( $cf7_properties['mail']['body'], $form['form_fields'] ); |
| 943 | } |
| 944 | |
| 945 | if ( ! empty( $cf7_properties['mail']['additional_headers'] ) ) { |
| 946 | $form['settings']['email']['connection_1']['evf_reply_to'] = $this->get_replyto( $cf7_properties['mail']['additional_headers'], $form['form_fields'] ); |
| 947 | } |
| 948 | |
| 949 | if ( ! empty( $cf7_properties['mail']['sender'] ) ) { |
| 950 | $sender = $this->get_sender_details( $cf7_properties['mail']['sender'], $form['form_fields'] ); |
| 951 | |
| 952 | if ( $sender ) { |
| 953 | $form['settings']['email']['connection_1']['evf_from_name'] = $sender['name']; |
| 954 | $form['settings']['email']['connection_1']['evf_from_email'] = $sender['address']; |
| 955 | } |
| 956 | } |
| 957 | |
| 958 | // Setup mail_2 (auto-reply) notification if active. |
| 959 | if ( ! empty( $cf7_properties['mail_2']['active'] ) ) { |
| 960 | $form['settings']['email']['connection_2'] = array( |
| 961 | 'enable_email_notification' => '1', |
| 962 | 'connection_name' => esc_html__( 'Second Email', 'everest-forms' ), |
| 963 | 'evf_to_email' => '{admin_email}', |
| 964 | 'evf_from_name' => esc_html__( 'Everest Forms', 'everest-forms' ), |
| 965 | 'evf_from_email' => '{admin_email}', |
| 966 | 'evf_reply_to' => '', |
| 967 | 'evf_email_subject' => sprintf( '%s - %s', esc_html__( 'New Form Entry', 'everest-forms' ), esc_attr( $cf7_form_name ) ), |
| 968 | 'evf_email_message' => '{all_fields}', |
| 969 | ); |
| 970 | |
| 971 | if ( ! empty( $cf7_properties['mail_2']['subject'] ) ) { |
| 972 | $form['settings']['email']['connection_2']['evf_email_subject'] = $this->get_smarttags( $cf7_properties['mail_2']['subject'], $form['form_fields'] ); |
| 973 | } |
| 974 | |
| 975 | if ( ! empty( $cf7_properties['mail_2']['recipient'] ) ) { |
| 976 | $form['settings']['email']['connection_2']['evf_to_email'] = $this->get_smarttags( $cf7_properties['mail_2']['recipient'], $form['form_fields'] ); |
| 977 | } |
| 978 | |
| 979 | if ( ! empty( $cf7_properties['mail_2']['body'] ) ) { |
| 980 | $form['settings']['email']['connection_2']['evf_email_message'] = $this->get_smarttags( $cf7_properties['mail_2']['body'], $form['form_fields'] ); |
| 981 | } |
| 982 | |
| 983 | if ( ! empty( $cf7_properties['mail_2']['additional_headers'] ) ) { |
| 984 | $form['settings']['email']['connection_2']['evf_reply_to'] = $this->get_replyto( $cf7_properties['mail_2']['additional_headers'], $form['form_fields'] ); |
| 985 | } |
| 986 | |
| 987 | if ( ! empty( $cf7_properties['mail_2']['sender'] ) ) { |
| 988 | $sender = $this->get_sender_details( $cf7_properties['mail_2']['sender'], $form['form_fields'] ); |
| 989 | |
| 990 | if ( $sender ) { |
| 991 | $form['settings']['email']['connection_2']['evf_from_name'] = $sender['name']; |
| 992 | $form['settings']['email']['connection_2']['evf_from_email'] = $sender['address']; |
| 993 | } |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | $form = apply_filters( 'evf_fm_' . $this->slug . '_form_after_fields_mapping', $form, $cf7_form_id, $cf7_form ); |
| 998 | |
| 999 | $response = $this->import_form( $form, $unsupported, $upgrade_plan, $upgrade_omit ); |
| 1000 | |
| 1001 | $cf7_forms_data[ $cf7_form_id ] = $response; |
| 1002 | } |
| 1003 | return $cf7_forms_data; |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | new EVF_Fm_Contactform7(); |
| 1008 |