abstract-evf-form-migrator.php
3 months ago
class-evf-fm-contactform7.php
4 weeks ago
class-evf-fm-wpforms.php
2 years ago
class-evf-fm-wpforms.php
755 lines
| 1 | <?php |
| 2 | /** |
| 3 | * EverestForms Form Migrator WPforms Class |
| 4 | * |
| 5 | * @package EverestForms\Admin |
| 6 | * @since 2.0.6 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * EVF_Fm_Wpforms class. |
| 13 | */ |
| 14 | class EVF_Fm_Wpforms extends EVF_Admin_Form_Migrator { |
| 15 | /** |
| 16 | * Importer plugin pro path. |
| 17 | * |
| 18 | * @since 2.0.6 |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | public $pro_path; |
| 23 | |
| 24 | /** |
| 25 | * Primary class constructor. |
| 26 | * |
| 27 | * @since 2.0.6 |
| 28 | */ |
| 29 | /** |
| 30 | * Define required properties. |
| 31 | * |
| 32 | * @since 2.0.6 |
| 33 | */ |
| 34 | public function init() { |
| 35 | |
| 36 | $this->name = 'WPForms'; |
| 37 | $this->slug = 'wpforms'; |
| 38 | $this->path = 'wpforms-lite/wpforms.php'; |
| 39 | $this->pro_path = 'wpforms/wpforms.php'; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * If the importer source is available. |
| 44 | * |
| 45 | * @since 2.0.6 |
| 46 | * |
| 47 | * @return bool |
| 48 | */ |
| 49 | protected function is_active() { |
| 50 | return is_plugin_active( $this->path ) || is_plugin_active( $this->pro_path ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Check is the plugin installed or not. |
| 55 | * |
| 56 | * @since 2.0.6 |
| 57 | * |
| 58 | * @return bool |
| 59 | */ |
| 60 | protected function is_installed() { |
| 61 | return file_exists( trailingslashit( WP_PLUGIN_DIR ) . $this->path ) || file_exists( trailingslashit( WP_PLUGIN_DIR ) . $this->pro_path ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Get all the forms. |
| 66 | * |
| 67 | * @since 2.0.6 |
| 68 | */ |
| 69 | public function get_forms() { |
| 70 | |
| 71 | $required_form_arr = array(); |
| 72 | if ( function_exists( 'wpforms' ) ) { |
| 73 | $forms = wpforms()->form->get( '' ); |
| 74 | if ( empty( $forms ) ) { |
| 75 | return $required_form_arr; |
| 76 | } |
| 77 | foreach ( $forms as $form ) { |
| 78 | if ( empty( $form ) ) { |
| 79 | continue; |
| 80 | } |
| 81 | $required_form_arr[ $form->ID ] = $form->post_title; |
| 82 | } |
| 83 | } |
| 84 | return $required_form_arr; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Get a single form. |
| 89 | * |
| 90 | * @since 2.0.6 |
| 91 | * |
| 92 | * @param int $id Form ID. |
| 93 | * |
| 94 | * @return object|bool |
| 95 | */ |
| 96 | public function get_form( $id ) { |
| 97 | if ( ! function_exists( 'wpforms' ) ) { |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | $forms = wpforms()->form->get( $id ); |
| 102 | |
| 103 | return $forms; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Replace 3rd-party form provider tags/shortcodes with our own Smart Tags. |
| 108 | * |
| 109 | * @since 2.0.6 |
| 110 | * |
| 111 | * @param string $string Text to look for Smart Tags in. |
| 112 | * @param array $fields List of fields to process Smart Tags in. |
| 113 | * |
| 114 | * @return string |
| 115 | */ |
| 116 | private function get_smarttags( $string, $fields = array() ) { |
| 117 | |
| 118 | preg_match_all( '/\{field_id=\"([^\"]*)\"\}/', $string, $tags ); |
| 119 | |
| 120 | if ( empty( $tags[1] ) ) { |
| 121 | return $string; |
| 122 | } |
| 123 | // Process form-tags and mail-tags. |
| 124 | foreach ( $tags[1] as $tag ) { |
| 125 | foreach ( $fields as $field ) { |
| 126 | if ( ! empty( $field['wpf_name'] ) && $field['wpf_name'] == $tag ) { |
| 127 | $field_id = $this->get_field_id_for_smarttags( $field ); |
| 128 | $string = str_replace( '{field_id="' . $tag . '"}', '{field_id="' . $field_id . '"}', $string ); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // Process wpforms tags that we can map with EVF alternatives. |
| 134 | $string = str_replace( |
| 135 | array( |
| 136 | '{user_ip}', |
| 137 | '{page_title}', |
| 138 | '{user_first_name}', |
| 139 | '{user_last_name}', |
| 140 | ), |
| 141 | array( |
| 142 | '{user_ip_address}', |
| 143 | '{post_title}', |
| 144 | '{first_name}', |
| 145 | '{last_name}', |
| 146 | ), |
| 147 | $string |
| 148 | ); |
| 149 | |
| 150 | return $string; |
| 151 | } |
| 152 | /** |
| 153 | * Email notification settings. |
| 154 | * |
| 155 | * @since 2.0.6 |
| 156 | * @param [array] $form The form. |
| 157 | * @param [array] $wpf_settings The wpfoms settings. |
| 158 | */ |
| 159 | private function get_email_notification_settings( $form, $wpf_settings ) { |
| 160 | $notification_settings = array( |
| 161 | 'connection_1' => array( |
| 162 | 'enable_email_notification' => $wpf_settings['notification_enable'], |
| 163 | 'connection_name' => esc_html__( 'Admin Notification', 'everest-forms' ), |
| 164 | 'evf_to_email' => $this->get_smarttags( $wpf_settings['notifications'][1]['email'], $form['form_fields'] ), |
| 165 | 'evf_from_name' => $this->get_smarttags( $wpf_settings['notifications'][1]['sender_name'], $form['form_fields'] ), |
| 166 | 'evf_from_email' => $this->get_smarttags( $wpf_settings['notifications'][1]['sender_address'], $form['form_fields'] ), |
| 167 | 'evf_reply_to' => $this->get_smarttags( $wpf_settings['notifications'][1]['replyto'], $form['form_fields'] ), |
| 168 | 'evf_email_subject' => $this->get_smarttags( $wpf_settings['notifications'][1]['subject'], $form['form_fields'] ), |
| 169 | 'evf_email_message' => $this->get_smarttags( $wpf_settings['notifications'][1]['message'], $form['form_fields'] ), |
| 170 | ), |
| 171 | ); |
| 172 | |
| 173 | return $notification_settings; |
| 174 | } |
| 175 | /** |
| 176 | * Conver the browser details. |
| 177 | * |
| 178 | * @since 2.0.6 |
| 179 | * @param [string] $user_agent The user agent from wpforms's entry. |
| 180 | */ |
| 181 | private function get_browser_detail( $user_agent ) { |
| 182 | $browser_info = get_browser( $user_agent, true ); |
| 183 | $modified_agent = ''; |
| 184 | if ( $browser_info !== false ) { |
| 185 | $browser = $browser_info['browser'] ?? 'Unknown Browser'; |
| 186 | $platform = $browser_info['platform'] ?? 'Unknown Platform'; |
| 187 | $device_type = $browser_info['device_type'] ?? 'Unknown Device Type'; |
| 188 | |
| 189 | $agent = $browser . '/' . $platform . '/' . $device_type; |
| 190 | } |
| 191 | return $modified_agent; |
| 192 | } |
| 193 | /** |
| 194 | * Mapping the form setting. |
| 195 | * |
| 196 | * @since 2.0.6 |
| 197 | * @param [array] $form The form data. |
| 198 | * @param [aray] $wpf_settings The wpforms form settings. |
| 199 | * @param [int] $wpf_form_id The wpforms ID. |
| 200 | */ |
| 201 | private function get_form_settings( $form, $wpf_settings, $wpf_form_id ) { |
| 202 | $form['settings'] = array( |
| 203 | 'email' => apply_filters( 'evf_fm_' . $this->slug . 'email_notification_settings', $this->get_email_notification_settings( $form, $wpf_settings ), $form, $wpf_settings ), |
| 204 | 'form_title' => $wpf_settings['form_title'], |
| 205 | 'form_description' => $wpf_settings['form_desc'], |
| 206 | 'form_disable_message' => esc_html__( 'This form is disabled.', 'everest-forms' ), |
| 207 | 'successful_form_submission_message' => strip_tags( $wpf_settings['confirmations'][1]['message'], '' ), |
| 208 | 'submission_message_scroll' => $wpf_settings['confirmations'][1]['message_scroll'], |
| 209 | 'redirect_to' => 'message' === $wpf_settings['confirmations'][1]['type'] ? 'same' : ( 'page' === $wpf_settings['confirmations'][1]['type'] ? 'custom_page' : ( 'redirect' === $wpf_settings['confirmations'][1]['type'] ? 'external_url' : $wpf_settings['confirmations'][1]['type'] ) ), |
| 210 | 'custom_page' => $wpf_settings['confirmations'][1]['page'], |
| 211 | 'external_url' => $wpf_settings['confirmations'][1]['redirect'], |
| 212 | 'enable_redirect_query_string' => 0, |
| 213 | 'query_string' => '', |
| 214 | 'layout_class' => 'default', |
| 215 | 'form_class' => $wpf_settings['form_class'], |
| 216 | 'submit_button_text' => $wpf_settings['submit_text'], |
| 217 | 'submit_button_processing_text' => $wpf_settings['submit_text_processing'], |
| 218 | 'submit_button_class' => $wpf_settings['submit_class'], |
| 219 | 'ajax_form_submission' => isset( $wpf_settings['ajax_submit'] ) ? $wpf_settings['ajax_submit'] : '0', |
| 220 | 'disabled_entries' => isset( $wpf_settings['store_spam_entries'] ) ? $wpf_settings['store_spam_entries'] : '0', |
| 221 | 'honeypot' => '1', |
| 222 | 'akismet' => isset( $wpf_settings['akismet'] ) ? $wpf_settings['akismet'] : '0', |
| 223 | 'akismet_protection_type' => 'validation_failed', |
| 224 | 'recaptcha_support' => isset( $wpf_settings['recaptcha'] ) ? $wpf_settings['recaptcha'] : '0', |
| 225 | 'evf-enable-custom-css' => '0', |
| 226 | 'evf-custom-css' => '', |
| 227 | 'evf-enable-custom-js' => '0', |
| 228 | 'evf-custom-js' => '', |
| 229 | 'structure' => array(), |
| 230 | 'imported_from' => array( |
| 231 | 'form_id' => absint( $wpf_form_id ), |
| 232 | 'form_from' => $this->slug, |
| 233 | ), |
| 234 | ); |
| 235 | |
| 236 | return $form; |
| 237 | } |
| 238 | /** |
| 239 | * Mapped the form datas. |
| 240 | * |
| 241 | * @since 2.0.6 |
| 242 | * @param [array] $wpf_form_ids |
| 243 | */ |
| 244 | public function get_fm_mapped_form_data( $wpf_form_ids ) { |
| 245 | $wpf_forms_data = array(); |
| 246 | foreach ( $wpf_form_ids as $wpf_form_id ) { |
| 247 | $wpf_form = $this->get_form( $wpf_form_id ); |
| 248 | if ( ! $wpf_form ) { |
| 249 | $wpf_forms_data[ $wpf_form_id ] = $wpf_form; |
| 250 | continue; |
| 251 | } |
| 252 | $wpf_form_name = $wpf_form->post_title; |
| 253 | $wpf_form_post_content = json_decode( $wpf_form->post_content, true ); |
| 254 | $wpf_fields = isset( $wpf_form_post_content['fields'] ) ? $wpf_form_post_content['fields'] : ''; |
| 255 | $wpf_settings = isset( $wpf_form_post_content['settings'] ) ? $wpf_form_post_content['settings'] : ''; |
| 256 | $fields_pro_plan = array( 'number-slider' ); |
| 257 | $fields_pro_omit = array(); |
| 258 | $fields_unsupported = array(); |
| 259 | $upgrade_plan = array(); |
| 260 | $upgrade_omit = array(); |
| 261 | $unsupported = array(); |
| 262 | |
| 263 | $form = array( |
| 264 | 'id' => '', |
| 265 | 'form_enabled' => '1', |
| 266 | 'form_field_id' => '', |
| 267 | 'form_fields' => array(), |
| 268 | 'settings' => array(), |
| 269 | ); |
| 270 | |
| 271 | // Mapping Fields. |
| 272 | if ( empty( $wpf_fields ) ) { |
| 273 | // If form does not contain fields, bail. |
| 274 | wp_send_json_error( |
| 275 | array( |
| 276 | 'form_name' => sanitize_text_field( $wpf_form_name ), |
| 277 | 'message' => esc_html__( 'No form fields found.', 'everest-forms' ), |
| 278 | ) |
| 279 | ); |
| 280 | } |
| 281 | // Convert fields. |
| 282 | foreach ( $wpf_fields as $wpf_field ) { |
| 283 | |
| 284 | // Next, check if field is unsupported. If supported make note and |
| 285 | // then continue to the next field. |
| 286 | if ( in_array( $wpf_field['type'], $fields_unsupported, true ) ) { |
| 287 | $unsupported[] = $wpf_field['label']; |
| 288 | |
| 289 | continue; |
| 290 | } |
| 291 | if ( ! defined( 'EFP_VERSION' ) && '1.7.1' <= 'EFP_VERSION' && in_array( $wpf_field['type'], $fields_pro_plan, true ) ) { |
| 292 | $upgrade_plan[] = $wpf_field['label']; |
| 293 | |
| 294 | continue; |
| 295 | } |
| 296 | if ( ! defined( 'EFP_VERSION' ) && '1.7.1' <= 'EFP_VERSION' && in_array( $wpf_field['type'], $fields_pro_omit, true ) ) { |
| 297 | $upgrade_omit[] = $wpf_field['label']; |
| 298 | |
| 299 | continue; |
| 300 | } |
| 301 | |
| 302 | // Calculating the field ids and storing next field id. |
| 303 | if ( ! empty( $form['form_field_id'] ) ) { |
| 304 | $form_field_id = absint( $form['form_field_id'] ); |
| 305 | ++$form['form_field_id']; |
| 306 | } else { |
| 307 | $form_field_id = '0'; |
| 308 | $form['form_field_id'] = '1'; |
| 309 | } |
| 310 | |
| 311 | $field_id = evf_get_random_string() . '-' . $form_field_id; |
| 312 | // Mapping the field type and formtting the fields settings. |
| 313 | switch ( $wpf_field['type'] ) { |
| 314 | case 'text': |
| 315 | case 'textarea': |
| 316 | $type = $wpf_field['type']; |
| 317 | $form['structure'][ 'row_' . $form['form_field_id'] ]['grid_1'][] = $field_id; |
| 318 | $form['form_fields'][ $field_id ] = array( |
| 319 | 'id' => $field_id, |
| 320 | 'type' => $type, |
| 321 | 'label' => $wpf_field['label'], |
| 322 | 'meta-key' => $type . '-' . $wpf_field['id'], |
| 323 | 'description' => $wpf_field['description'], |
| 324 | 'required' => isset( $wpf_field['required'] ) ? $wpf_field['required'] : '0', |
| 325 | 'required_field_message_setting' => 'global', |
| 326 | 'required-field-message' => '', |
| 327 | 'placeholder' => $wpf_field['placeholder'], |
| 328 | 'label_hide' => isset( $wpf_field['label_hide'] ) ? $wpf_field['label_hide'] : '0', |
| 329 | 'limit_enabled' => isset( $wpf_field['limit_enabled'] ) ? $wpf_field['limit_enabled'] : '0', |
| 330 | 'limit_count' => isset( $wpf_field['limit_count'] ) ? $wpf_field['limit_count'] : '1', |
| 331 | 'limit_mode' => $wpf_field['limit_mode'], |
| 332 | 'min_length_count' => '1', |
| 333 | 'min_length_mode' => 'characters', |
| 334 | 'default_value' => $this->get_smarttags( $wpf_field['default_value'] ), |
| 335 | 'css' => $wpf_field['css'], |
| 336 | 'input_mask' => isset( $wpf_field['input_mask'] ) ? $wpf_field['input_mask'] : '', |
| 337 | 'regex_value' => '', |
| 338 | 'regex_message' => esc_html__( 'Please provide a valid value for this field.', 'everest-forms' ), |
| 339 | 'wpf_name' => $wpf_field['id'], |
| 340 | ); |
| 341 | break; |
| 342 | case 'name': |
| 343 | $name_format = array_map( 'trim', explode( '-', $wpf_field['format'] ) ); |
| 344 | $name_format_count = count( $name_format ); |
| 345 | $row_num = $form['form_field_id']; |
| 346 | |
| 347 | foreach ( $name_format as $index => $format ) { |
| 348 | if ( 'simple' === $format || 'first' === $format || 'middle' === $format ) { |
| 349 | $type = 'first-name'; |
| 350 | $label = ucfirst( $format ) . ' ' . $wpf_field['label']; |
| 351 | if ( 'middle' === $format ) { |
| 352 | $form['structure'][ 'row_' . $row_num ]['grid_2'][] = $field_id; |
| 353 | } else { |
| 354 | $form['structure'][ 'row_' . $row_num ]['grid_1'][] = $field_id; |
| 355 | } |
| 356 | } elseif ( 'last' === $format ) { |
| 357 | $type = 'last-name'; |
| 358 | $label = ucfirst( $format ) . ' ' . $wpf_field['label']; |
| 359 | if ( in_array( 'middle', $name_format, true ) ) { |
| 360 | $form['structure'][ 'row_' . $row_num ]['grid_3'][] = $field_id; |
| 361 | } else { |
| 362 | $form['structure'][ 'row_' . $row_num ]['grid_2'][] = $field_id; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | $form['form_fields'][ $field_id ] = array( |
| 367 | 'id' => $field_id, |
| 368 | 'type' => $type, |
| 369 | 'label' => $label, |
| 370 | 'meta-key' => $format . '_' . $wpf_field['id'], |
| 371 | 'description' => $wpf_field['description'], |
| 372 | 'required' => isset( $wpf_field['required'] ) ? $wpf_field['required'] : '0', |
| 373 | 'required_field_message_setting' => 'global', |
| 374 | 'required-field-message' => '', |
| 375 | 'placeholder' => isset( $wpf_field[ $format . '_placeholder' ] ) ? $wpf_field[ $format . '_placeholder' ] : '', |
| 376 | 'label_hide' => isset( $wpf_field['label_hide'] ) ? $wpf_field['label_hide'] : '0', |
| 377 | 'default_value' => isset( $wpf_field[ $format . '_default' ] ) ? $wpf_field[ $format . '_default' ] : '', |
| 378 | 'css' => $wpf_field['css'], |
| 379 | 'regex_value' => '', |
| 380 | 'regex_message' => esc_html__( 'Please provide a valid value for this field.', 'everest-forms' ), |
| 381 | 'wpf_name' => $wpf_field['id'], |
| 382 | ); |
| 383 | |
| 384 | if ( ( $index + 1 ) < $name_format_count ) { |
| 385 | // Calculating field id. |
| 386 | $form_field_id = absint( $form['form_field_id'] ); |
| 387 | ++$form['form_field_id']; |
| 388 | $field_id = evf_get_random_string() . '-' . $form_field_id; |
| 389 | } |
| 390 | } |
| 391 | break; |
| 392 | case 'email': |
| 393 | $type = $wpf_field['type']; |
| 394 | $form['structure'][ 'row_' . $form['form_field_id'] ]['grid_1'][] = $field_id; |
| 395 | $form['form_fields'][ $field_id ] = array( |
| 396 | 'id' => $field_id, |
| 397 | 'type' => $type, |
| 398 | 'label' => $wpf_field['label'], |
| 399 | 'meta-key' => $type . '-' . $wpf_field['id'], |
| 400 | 'description' => $wpf_field['description'], |
| 401 | 'required' => isset( $wpf_field['required'] ) ? $wpf_field['required'] : '0', |
| 402 | 'required_field_message_setting' => 'global', |
| 403 | 'required-field-message' => '', |
| 404 | 'placeholder' => $wpf_field['placeholder'], |
| 405 | 'confirmation_placeholder' => $wpf_field['confirmation_placeholder'], |
| 406 | 'label_hide' => isset( $wpf_field['label_hide'] ) ? $wpf_field['label_hide'] : '0', |
| 407 | 'default_value' => $this->get_smarttags( $wpf_field['default_value'] ), |
| 408 | 'css' => $wpf_field['css'], |
| 409 | 'regex_value' => '', |
| 410 | 'regex_message' => esc_html__( 'Please provide a valid value for this field.', 'everest-forms' ), |
| 411 | 'wpf_name' => $wpf_field['id'], |
| 412 | ); |
| 413 | break; |
| 414 | case 'select': |
| 415 | case 'radio': |
| 416 | case 'checkbox': |
| 417 | $type = $wpf_field['type']; |
| 418 | $form['structure'][ 'row_' . $form['form_field_id'] ]['grid_1'][] = $field_id; |
| 419 | $evf_choices = array(); |
| 420 | if ( isset( $wpf_field['choices'] ) && ! empty( $wpf_field['choices'] ) ) { |
| 421 | foreach ( $wpf_field['choices'] as $choice ) { |
| 422 | $evf_choice = array( |
| 423 | 'label' => $choice['label'], |
| 424 | 'value' => $choice['value'], |
| 425 | 'image' => $choice['image'], |
| 426 | ); |
| 427 | if ( isset( $choice['default'] ) ) { |
| 428 | $evf_choice['default'] = $choice['default']; |
| 429 | } |
| 430 | $evf_choices[] = $evf_choice; |
| 431 | } |
| 432 | } |
| 433 | // To manage static meta key issue in our plugin. |
| 434 | if ( 'select' === $type ) { |
| 435 | $compatible_meta_key = 'dropdown_'; |
| 436 | } else { |
| 437 | $compatible_meta_key = $type; |
| 438 | } |
| 439 | |
| 440 | $form['form_fields'][ $field_id ] = array( |
| 441 | 'id' => $field_id, |
| 442 | 'type' => $type, |
| 443 | 'label' => $wpf_field['label'], |
| 444 | 'meta-key' => $compatible_meta_key . '-' . $wpf_field['id'], |
| 445 | 'choices' => $evf_choices, |
| 446 | 'description' => $wpf_field['description'], |
| 447 | 'label_hide' => isset( $wpf_field['label_hide'] ) ? $wpf_field['label_hide'] : '0', |
| 448 | 'required' => isset( $wpf_field['required'] ) ? $wpf_field['required'] : '0', |
| 449 | 'required_field_message_setting' => 'global', |
| 450 | 'required-field-message' => '', |
| 451 | 'css' => $wpf_field['css'], |
| 452 | 'wpf_name' => $wpf_field['id'], |
| 453 | ); |
| 454 | |
| 455 | if ( 'select' === $type ) { |
| 456 | if ( isset( $wpf_field['multiple'] ) ) { |
| 457 | $form['form_fields'][ $field_id ]['multiple_choices'] = $wpf_field['multiple']; |
| 458 | } |
| 459 | $form['form_fields'][ $field_id ]['placeholder'] = $wpf_field['placeholder']; |
| 460 | } |
| 461 | if ( 'radio' === $type || 'checkbox' === $type ) { |
| 462 | $form['form_fields'][ $field_id ]['input_columns'] = ''; |
| 463 | if ( isset( $wpf_field['choices_images'] ) ) { |
| 464 | $form['form_fields'][ $field_id ]['choices_images'] = $wpf_field['choices_images']; |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | if ( 'checkbox' === $type ) { |
| 469 | $form['form_fields'][ $field_id ]['choice_limit'] = ''; |
| 470 | } |
| 471 | break; |
| 472 | case 'number': |
| 473 | $type = $wpf_field['type']; |
| 474 | $form['structure'][ 'row_' . $form['form_field_id'] ]['grid_1'][] = $field_id; |
| 475 | $form['form_fields'][ $field_id ] = array( |
| 476 | 'id' => $field_id, |
| 477 | 'type' => $type, |
| 478 | 'label' => $wpf_field['label'], |
| 479 | 'meta-key' => $type . '-' . $wpf_field['id'], |
| 480 | 'description' => $wpf_field['description'], |
| 481 | 'required' => isset( $wpf_field['required'] ) ? $wpf_field['required'] : '0', |
| 482 | 'required_field_message_setting' => 'global', |
| 483 | 'required-field-message' => '', |
| 484 | 'step' => '0', |
| 485 | 'min_value' => '', |
| 486 | 'max_value' => '', |
| 487 | 'placeholder' => $wpf_field['placeholder'], |
| 488 | 'label_hide' => isset( $wpf_field['label_hide'] ) ? $wpf_field['label_hide'] : '0', |
| 489 | 'default_value' => $this->get_smarttags( $wpf_field['default_value'] ), |
| 490 | 'css' => $wpf_field['css'], |
| 491 | 'regex_value' => '', |
| 492 | 'regex_message' => esc_html__( 'Please provide a valid value for this field.', 'everest-forms' ), |
| 493 | 'wpf_name' => $wpf_field['id'], |
| 494 | ); |
| 495 | break; |
| 496 | case 'number-slider': |
| 497 | $type = 'range-slider'; |
| 498 | $form['structure'][ 'row_' . $form['form_field_id'] ]['grid_1'][] = $field_id; |
| 499 | $form['form_fields'][ $field_id ] = array( |
| 500 | 'id' => $field_id, |
| 501 | 'type' => $type, |
| 502 | 'label' => $wpf_field['label'], |
| 503 | 'meta-key' => $type . '-' . $wpf_field['id'], |
| 504 | 'description' => $wpf_field['description'], |
| 505 | 'required' => isset( $wpf_field['required'] ) ? $wpf_field['required'] : '0', |
| 506 | 'required_field_message_setting' => 'global', |
| 507 | 'required-field-message' => '', |
| 508 | 'step' => $wpf_field['step'], |
| 509 | 'min_value' => $wpf_field['min'], |
| 510 | 'max_value' => $wpf_field['max'], |
| 511 | 'placeholder' => '', |
| 512 | 'label_hide' => isset( $wpf_field['label_hide'] ) ? $wpf_field['label_hide'] : '0', |
| 513 | 'default_value' => $wpf_field['default_value'], |
| 514 | 'css' => $wpf_field['css'], |
| 515 | 'skin' => '', |
| 516 | 'handle_color' => '', |
| 517 | 'highlight_color' => '', |
| 518 | 'track_color' => '', |
| 519 | 'prefix_text' => '', |
| 520 | 'show_slider_input' => '1', |
| 521 | 'wpf_name' => $wpf_field['id'], |
| 522 | ); |
| 523 | break; |
| 524 | case 'date-time': |
| 525 | $type = $wpf_field['type']; |
| 526 | $form['structure'][ 'row_' . $form['form_field_id'] ]['grid_1'][] = $field_id; |
| 527 | $form['form_fields'][ $field_id ] = array( |
| 528 | 'id' => $field_id, |
| 529 | 'type' => $type, |
| 530 | 'label' => $wpf_field['label'], |
| 531 | 'meta-key' => $type . '-' . $wpf_field['id'], |
| 532 | 'datetime_format' => $wpf_field['format'], |
| 533 | 'datetime_style' => 'datepicker' === $wpf_field['date_type'] ? 'picker' : $wpf_field['date_type'], |
| 534 | 'description' => $wpf_field['description'], |
| 535 | 'required' => isset( $wpf_field['required'] ) ? $wpf_field['required'] : '0', |
| 536 | 'required_field_message_setting' => 'global', |
| 537 | 'required-field-message' => '', |
| 538 | 'placeholder' => $wpf_field['date_placeholder'], |
| 539 | 'label_hide' => isset( $wpf_field['label_hide'] ) ? $wpf_field['label_hide'] : '0', |
| 540 | 'css' => $wpf_field['css'], |
| 541 | 'date_format' => $wpf_field['date_format'], |
| 542 | 'disable_dates' => isset( $wpf_field['date_disable_past_dates'] ) ? $wpf_field['date_disable_past_dates'] : '', |
| 543 | 'date_localization' => 'en', |
| 544 | 'date_timezone' => 'Default', |
| 545 | 'date_mode' => 'single', |
| 546 | 'min_date' => '', |
| 547 | 'max_date' => '', |
| 548 | 'min_date_range' => '', |
| 549 | 'max_date_range' => '', |
| 550 | 'time_interval' => $wpf_field['time_interval'], |
| 551 | 'time_format' => $wpf_field['time_format'], |
| 552 | 'min_time_hour' => $wpf_field['time_limit_hours_start_hour'], |
| 553 | 'min_time_minute' => $wpf_field['time_limit_hours_start_min'], |
| 554 | 'max_time_hour' => $wpf_field['time_limit_hours_end_hour'], |
| 555 | 'max_time_minute' => $wpf_field['time_limit_hours_end_min'], |
| 556 | 'wpf_name' => $wpf_field['id'], |
| 557 | ); |
| 558 | break; |
| 559 | case 'url': |
| 560 | $type = $wpf_field['type']; |
| 561 | $form['structure'][ 'row_' . $form['form_field_id'] ]['grid_1'][] = $field_id; |
| 562 | $form['form_fields'][ $field_id ] = array( |
| 563 | 'id' => $field_id, |
| 564 | 'type' => $type, |
| 565 | 'label' => $wpf_field['label'], |
| 566 | 'meta-key' => $type . '-' . $wpf_field['id'], |
| 567 | 'description' => $wpf_field['description'], |
| 568 | 'required' => isset( $wpf_field['required'] ) ? $wpf_field['required'] : '0', |
| 569 | 'required_field_message_setting' => 'global', |
| 570 | 'required-field-message' => '', |
| 571 | 'placeholder' => $wpf_field['placeholder'], |
| 572 | 'label_hide' => isset( $wpf_field['label_hide'] ) ? $wpf_field['label_hide'] : '0', |
| 573 | 'default_value' => $this->get_smarttags( $wpf_field['default_value'] ), |
| 574 | 'css' => $wpf_field['css'], |
| 575 | 'regex_value' => '', |
| 576 | 'regex_message' => esc_html__( 'Please provide a valid value for this field.', 'everest-forms' ), |
| 577 | 'wpf_name' => $wpf_field['id'], |
| 578 | ); |
| 579 | break; |
| 580 | default: |
| 581 | break; |
| 582 | } |
| 583 | } |
| 584 | $form = apply_filters( 'evf_fm_' . $this->slug . '_form_after_fields_mapping', $form, $wpf_form_id, $wpf_form ); |
| 585 | |
| 586 | // Form Settings mapping. |
| 587 | $form = apply_filters( 'evf_fm_' . $this->slug . '_form_after_settings_mapping', $this->get_form_settings( $form, $wpf_settings, $wpf_form_id ), $wpf_form_id, $wpf_form ); |
| 588 | |
| 589 | $response = $this->import_form( $form, $unsupported, $upgrade_plan, $upgrade_omit ); |
| 590 | |
| 591 | $wpf_forms_data[ $wpf_form_id ] = $response; |
| 592 | } |
| 593 | return $wpf_forms_data; |
| 594 | } |
| 595 | /** |
| 596 | * Migrate the entry. |
| 597 | * |
| 598 | * @since 2.0.6 |
| 599 | * |
| 600 | * @param int $evf_form_id The everest form ID. |
| 601 | * @param int $form_id The importer form ID. |
| 602 | */ |
| 603 | public function migrate_entry( $evf_form_id, $form_id ) { |
| 604 | $form_data = evf()->form->get( |
| 605 | absint( $evf_form_id ), |
| 606 | array( |
| 607 | 'content_only' => true, |
| 608 | ) |
| 609 | ); |
| 610 | |
| 611 | $evf_form_fields = $form_data['form_fields']; |
| 612 | $evf_form_entries = array(); |
| 613 | $args = array( |
| 614 | 'form_id' => $form_id, |
| 615 | ); |
| 616 | |
| 617 | $submissions = wpforms()->entry->get_entries( $args ); |
| 618 | $entries = array(); |
| 619 | |
| 620 | if ( ! $submissions || ! is_array( $submissions ) ) { |
| 621 | return $evf_form_entries; |
| 622 | } |
| 623 | |
| 624 | foreach ( $submissions as $submission ) { |
| 625 | $fields = \json_decode( $submission->fields, true ); |
| 626 | if ( ! $fields ) { |
| 627 | continue; |
| 628 | } |
| 629 | $entry_list = array(); |
| 630 | foreach ( $fields as $field_id => $field ) { |
| 631 | if ( 'name' === $field['type'] ) { |
| 632 | $meta_keys = array( |
| 633 | 'first_' . $field_id, |
| 634 | 'middle_' . $field_id, |
| 635 | 'last_' . $field_id, |
| 636 | ); |
| 637 | } elseif ( 'select' === $field['type'] ) { |
| 638 | $meta_keys = array( 'dropdown_' . '-' . $field_id ); |
| 639 | } elseif ( 'number-slider' === $field['type'] ) { |
| 640 | $meta_keys = array( 'range-slider' . '-' . $field_id ); |
| 641 | } else { |
| 642 | $meta_keys = array( $field['type'] . '-' . $field_id ); |
| 643 | } |
| 644 | $field_keys = array(); |
| 645 | |
| 646 | foreach ( $evf_form_fields as $key => $form_field ) { |
| 647 | if ( in_array( $form_field['meta-key'], $meta_keys, true ) ) { |
| 648 | $field_keys[] = $key; |
| 649 | } |
| 650 | } |
| 651 | if ( ! empty( $field_keys ) ) { |
| 652 | foreach ( $field_keys as $field_key ) { |
| 653 | $entry = array(); |
| 654 | |
| 655 | $field_type = $evf_form_fields[ $field_key ]['type']; |
| 656 | $field_name = $evf_form_fields[ $field_key ]['label']; |
| 657 | $field_meta_key = $evf_form_fields[ $field_key ]['meta-key']; |
| 658 | switch ( $field_type ) { |
| 659 | |
| 660 | case 'first-name': |
| 661 | case 'last-name': |
| 662 | $format_arr = explode( '_', $evf_form_fields[ $field_key ]['meta-key'] ); |
| 663 | $format = $format_arr[0]; |
| 664 | $entry['id'] = $field_key; |
| 665 | $entry['type'] = $field_type; |
| 666 | $entry['meta_key'] = $field_meta_key; |
| 667 | $entry['value'] = $field[ $format ]; |
| 668 | $entry['name'] = $field_name; |
| 669 | break; |
| 670 | case 'checkbox': |
| 671 | $choice_label = array(); |
| 672 | foreach ( $evf_form_fields[ $field_key ]['choices'] as $choice ) { |
| 673 | $choice_label[] = $choice['label']; |
| 674 | } |
| 675 | $entry['id'] = $field_key; |
| 676 | $entry['type'] = $field_type; |
| 677 | $entry['value'] = array( |
| 678 | 'name' => $field_name, |
| 679 | 'type' => $field_type, |
| 680 | 'label' => $choice_label, |
| 681 | ); |
| 682 | $entry['meta_key'] = $field_meta_key; |
| 683 | $entry['value_raw'] = wp_json_encode( $field['value_raw'] ); |
| 684 | break; |
| 685 | case 'radio': |
| 686 | $entry['id'] = $field_key; |
| 687 | $entry['type'] = $field_type; |
| 688 | $entry['value'] = array( |
| 689 | 'name' => $field_name, |
| 690 | 'type' => $field_type, |
| 691 | 'label' => $field['value'], |
| 692 | ); |
| 693 | $entry['value_raw'] = wp_json_encode( $field['value_raw'] ); |
| 694 | $entry['meta_key'] = $field_meta_key; |
| 695 | |
| 696 | break; |
| 697 | |
| 698 | case 'select': |
| 699 | $entry['id'] = $field_key; |
| 700 | $entry['type'] = $field_type; |
| 701 | $entry['meta_key'] = $field_meta_key; |
| 702 | $entry['name'] = $field_name; |
| 703 | $entry['value'] = array( $field['value'] ); |
| 704 | $entry['value_raw'] = array( $field['value_raw'] ); |
| 705 | |
| 706 | break; |
| 707 | |
| 708 | default: |
| 709 | $entry['name'] = $field_name; |
| 710 | $entry['type'] = $field_type; |
| 711 | $entry['meta_key'] = $field_meta_key; |
| 712 | $entry['id'] = $field_key; |
| 713 | $entry['value'] = $field['value']; |
| 714 | break; |
| 715 | } |
| 716 | if ( empty( $entry ) ) { |
| 717 | continue; |
| 718 | } |
| 719 | $entry_list[ $field_key ] = $entry; |
| 720 | } |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | $entries['user_id'] = $submission->user_id; |
| 725 | $entries['user_device'] = ''; |
| 726 | $entries['user_ip_address'] = $submission->ip_address; |
| 727 | $entries['form_id'] = $evf_form_id; |
| 728 | $entries['referer'] = ''; |
| 729 | $entries['fields'] = wp_json_encode( $entry_list ); |
| 730 | if ( $this->check_token_column() ) { |
| 731 | $entries['token'] = null; |
| 732 | } |
| 733 | $entries['status'] = 'publish'; |
| 734 | $entries['viewed'] = $submission->viewed; |
| 735 | $entries['starred'] = $submission->starred; |
| 736 | $entries['date_created'] = $submission->date; |
| 737 | |
| 738 | $entry_id = $this->save_migrated_entry( $entries, $entry_list, $form_data ); |
| 739 | |
| 740 | $evf_form_entries[ $submission->entry_id ] = $entry_id; |
| 741 | } |
| 742 | |
| 743 | return $evf_form_entries; |
| 744 | } |
| 745 | /** |
| 746 | * Function to check the token. |
| 747 | * It exists only if the save and continue addon in use. |
| 748 | */ |
| 749 | public function check_token_column() { |
| 750 | return defined( 'EVF_SAVE_AND_CONTINUE_VERSION' ); |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | new EVF_Fm_Wpforms(); |
| 755 |