abstract-evf-form-migrator.php
3 months ago
class-evf-fm-contactform7.php
3 weeks ago
class-evf-fm-wpforms.php
2 years ago
abstract-evf-form-migrator.php
337 lines
| 1 | <?php |
| 2 | /** |
| 3 | * EverestForms Form Migrator Class |
| 4 | * |
| 5 | * @package EverestForms\Admin |
| 6 | * @since 2.0.8 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * EVF_Admin_Form_Migrator class. |
| 13 | */ |
| 14 | abstract class EVF_Admin_Form_Migrator { |
| 15 | /** |
| 16 | * Importer name. |
| 17 | * |
| 18 | * @since 2.0.8 |
| 19 | * |
| 20 | * @var string |
| 21 | */ |
| 22 | public $name; |
| 23 | |
| 24 | /** |
| 25 | * Importer name in slug format. |
| 26 | * |
| 27 | * @since 2.0.8 |
| 28 | * |
| 29 | * @var string |
| 30 | */ |
| 31 | public $slug; |
| 32 | |
| 33 | /** |
| 34 | * Importer plugin path. |
| 35 | * |
| 36 | * @since 2.0.8 |
| 37 | * |
| 38 | * @var string |
| 39 | */ |
| 40 | public $path; |
| 41 | |
| 42 | /** |
| 43 | * Primary class constructor. |
| 44 | * |
| 45 | * @since 2.0.8 |
| 46 | */ |
| 47 | public function __construct() { |
| 48 | |
| 49 | add_action( 'admin_notices', array( $this, 'show_fm_notice' ) ); |
| 50 | $this->init(); |
| 51 | } |
| 52 | /** |
| 53 | * Undocumented function |
| 54 | * |
| 55 | * @since 2.0.8 |
| 56 | */ |
| 57 | abstract public function init(); |
| 58 | |
| 59 | /** |
| 60 | * Add to list of registered importers. |
| 61 | * |
| 62 | * @since 2.0.8 |
| 63 | * |
| 64 | * @param array $importers List of supported importers. |
| 65 | * |
| 66 | * @return array |
| 67 | */ |
| 68 | public function register( $importers = array() ) { |
| 69 | |
| 70 | $importers = array( |
| 71 | 'name' => $this->name, |
| 72 | 'slug' => $this->slug, |
| 73 | 'path' => $this->path, |
| 74 | 'installed' => $this->is_installed(), |
| 75 | 'active' => $this->is_active(), |
| 76 | ); |
| 77 | |
| 78 | return $importers; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Get all the forms |
| 83 | * |
| 84 | * @return array |
| 85 | */ |
| 86 | abstract protected function get_forms(); |
| 87 | |
| 88 | /** |
| 89 | * Get the form id |
| 90 | * |
| 91 | * @param int $id Form ID. |
| 92 | * |
| 93 | * @return array|object|bool |
| 94 | */ |
| 95 | abstract protected function get_form( $id ); |
| 96 | |
| 97 | /** |
| 98 | * If the importer source is available. |
| 99 | * |
| 100 | * @since 2.0.8 |
| 101 | * |
| 102 | * @return bool |
| 103 | */ |
| 104 | abstract protected function is_active(); |
| 105 | |
| 106 | /** |
| 107 | * Check is the plugin installed or not. |
| 108 | * |
| 109 | * @since 2.0.8 |
| 110 | * |
| 111 | * @return bool |
| 112 | */ |
| 113 | abstract protected function is_installed(); |
| 114 | |
| 115 | /** |
| 116 | * Modify the field id for smart tags. |
| 117 | * |
| 118 | * @since 2.0.8 |
| 119 | * @param [array] $field The field array. |
| 120 | */ |
| 121 | protected function get_field_id_for_smarttags( $field ) { |
| 122 | $field_id = $field['id']; |
| 123 | $field_label = $field['label']; |
| 124 | if ( 'fullname' !== $field_id && 'email' !== $field_id && 'subject' !== $field_id && 'message' !== $field_id ) { |
| 125 | $field_label = preg_split( '/[\s\-\_]/', $field_label ); |
| 126 | foreach ( $field_label as $key => $value ) { |
| 127 | if ( 0 === $key ) { |
| 128 | $field_label[ $key ] = strtolower( $value ); |
| 129 | } else { |
| 130 | $field_label[ $key ] = ucfirst( $value ); |
| 131 | } |
| 132 | } |
| 133 | $field_label = implode( '', $field_label ); |
| 134 | $field_id = $field_label . '_' . $field_id; |
| 135 | } else { |
| 136 | $field_id = $field_id; |
| 137 | } |
| 138 | |
| 139 | return $field_id; |
| 140 | } |
| 141 | /** |
| 142 | * Tracks the successful import of a form, allowing future alerts for attempts to |
| 143 | * import a form that has already been imported. |
| 144 | * |
| 145 | * @since 2.0.8 |
| 146 | * |
| 147 | * @param int $source_id Imported plugin form ID. |
| 148 | * @param int $evf_forms_id Form ID. |
| 149 | */ |
| 150 | protected function track_import( $source_id, $evf_forms_id ) { |
| 151 | |
| 152 | $imported = get_option( 'evf_fm_' . $this->slug . '_imported_form_list', array() ); |
| 153 | |
| 154 | $imported[ $evf_forms_id ] = $source_id; |
| 155 | |
| 156 | update_option( 'evf_fm_' . $this->slug . '_imported_form_list', $imported, false ); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Import the new form to the database and return AJAX data. |
| 161 | * |
| 162 | * @since 2.0.8 |
| 163 | * |
| 164 | * @param array $form Form to import. |
| 165 | * @param array $unsupported List of unsupported fields. |
| 166 | * @param array $upgrade_plan List of fields, that are supported inside the EVF Forms Pro, but not in Free. |
| 167 | * @param array $upgrade_omit No field alternative in EVF. |
| 168 | */ |
| 169 | protected function import_form( $form, $unsupported = array(), $upgrade_plan = array(), $upgrade_omit = array() ) { |
| 170 | $imported_form_list = get_option( 'evf_fm_' . $this->slug . '_imported_form_list', array() ); |
| 171 | if ( empty( $form ) ) { |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | $form_id = array_search( $form['settings']['imported_from']['form_id'], $imported_form_list ); |
| 176 | if ( ! $form_id ) { |
| 177 | $form_id = evf()->form->create( $form['settings']['form_title'] ); |
| 178 | |
| 179 | if ( empty( $form_id ) || is_wp_error( $form_id ) ) { |
| 180 | wp_send_json_success( |
| 181 | array( |
| 182 | 'error' => true, |
| 183 | 'name' => sanitize_text_field( $form['settings']['form_title'] ), |
| 184 | 'msg' => esc_html__( 'There was an error while creating a new form.', 'everest-forms' ), |
| 185 | ) |
| 186 | ); |
| 187 | } |
| 188 | } |
| 189 | $form['id'] = $form_id; |
| 190 | $form['field_id'] = count( $form['form_fields'] ) + 1; |
| 191 | |
| 192 | // Update the form with all our compiled data. |
| 193 | $form_id = evf()->form->update( $form['id'], $form ); |
| 194 | // Updating the post meta to track the migrated forms. |
| 195 | update_post_meta( $form_id, 'evf_fm_imported_from', $form['settings']['imported_from'] ); |
| 196 | $form_styles = get_option( 'everest_forms_styles', array() ); |
| 197 | $logger = evf_get_logger(); |
| 198 | $logger->info( |
| 199 | __( 'Saving form.', 'everest-forms' ), |
| 200 | array( 'source' => 'form-save' ) |
| 201 | ); |
| 202 | do_action( 'everest_forms_save_form', $form_id, $form, array(), ! empty( $form_styles[ $form_id ] ) ); |
| 203 | |
| 204 | if ( ! $form_id ) { |
| 205 | $logger->error( |
| 206 | __( 'An error occurred while saving the form.', 'everest-forms' ), |
| 207 | array( 'source' => 'form-save' ) |
| 208 | ); |
| 209 | wp_send_json_error( |
| 210 | array( |
| 211 | 'errorTitle' => esc_html__( 'Form not found', 'everest-forms' ), |
| 212 | 'errorMessage' => esc_html__( 'An error occurred while saving the form.', 'everest-forms' ), |
| 213 | ) |
| 214 | ); |
| 215 | } else { |
| 216 | $logger->info( |
| 217 | __( 'Form Imported successfully.', 'everest-forms' ), |
| 218 | array( 'source' => 'form-save' ) |
| 219 | ); |
| 220 | } |
| 221 | |
| 222 | // Make note that this form has been imported. |
| 223 | $this->track_import( $form['settings']['imported_from']['form_id'], $form_id ); |
| 224 | |
| 225 | // Build and send final AJAX response! |
| 226 | $final_response = array( |
| 227 | 'name' => $form['settings']['form_title'], |
| 228 | 'evf_form_id' => $form_id, |
| 229 | 'edit' => esc_url_raw( admin_url( 'admin.php?page=evf-builder&tab=fields&form_id=' . $form_id ) ), |
| 230 | 'preview' => '', |
| 231 | 'unsupported' => $unsupported, |
| 232 | 'upgrade_plan' => $upgrade_plan, |
| 233 | 'upgrade_omit' => $upgrade_omit, |
| 234 | ); |
| 235 | return apply_filters( 'evf_fm_cf7_final_response', $final_response ); |
| 236 | } |
| 237 | /** |
| 238 | * Show form migrator notice in admin area of everest form if the plugin found |
| 239 | * |
| 240 | * @since 2.0.8 |
| 241 | * |
| 242 | * @return void |
| 243 | */ |
| 244 | public function show_fm_notice() { |
| 245 | $screen = get_current_screen(); |
| 246 | $screen_id = $screen ? $screen->id : ''; |
| 247 | if ( ! in_array( $screen_id, evf_get_screen_ids(), true ) ) { |
| 248 | return; |
| 249 | } |
| 250 | if ( ! file_exists( trailingslashit( WP_PLUGIN_DIR ) . $this->path ) ) { |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | if ( $this->is_dimissed() || ! current_user_can( 'manage_options' ) ) { |
| 255 | return; |
| 256 | } |
| 257 | ?> |
| 258 | <div class="notice notice-info is-dismissible evf-fm-notice inline"> |
| 259 | <p><?php printf( wp_kses_post( 'Hey, it seems that you have <strong>%s</strong> installed. Are you interested in <strong>migrating</strong> your forms to Everest Form?', 'everest-forms' ), wp_kses_post( $this->name ) ); ?></p> |
| 260 | <p> |
| 261 | <a href="<?php printf( esc_url( admin_url( 'admin.php?page=evf-tools&tab=form_migrator' ) ) ); ?>" class="button button-primary evf-fm-<?php echo esc_attr( $this->slug ); ?>" id="evf-fm-<?php echo esc_attr( $this->slug ); ?>"><?php esc_html_e( 'Form Migrator', 'everest-forms' ); ?></a> |
| 262 | <a href="#" class="button evf-fm-dismiss-notice" data-option-id="evf_fm_dismiss_xnotice_<?php echo esc_attr( $this->slug ); ?>" id="evf-fm-dimiss-<?php echo esc_attr( $this->slug ); ?>"><?php esc_html_e( 'No Thanks', 'everest-forms' ); ?></a> |
| 263 | <a href="<?php printf( esc_url( 'https://docs.everestforms.net/' ) ); ?>" target="_blank" class="button evf-fm-<?php echo esc_attr( $this->slug ); ?>" id="evf-fm-dimiss-<?php echo esc_attr( $this->slug ); ?>"><?php esc_html_e( 'For More', 'everest-forms' ); ?></a> |
| 264 | </p> |
| 265 | </div> |
| 266 | <?php |
| 267 | } |
| 268 | /** |
| 269 | * Save the migrated entry. |
| 270 | * |
| 271 | * @since 2.0.8 |
| 272 | * @param [array] $entries The entries. |
| 273 | * @param [array] $entry_list The entry list with value. |
| 274 | * @param [array] $form_data The form data list. |
| 275 | */ |
| 276 | protected function save_migrated_entry( $entries, $entry_list = array(), $form_data = array() ) { |
| 277 | global $wpdb; |
| 278 | $result = $wpdb->insert( $wpdb->prefix . 'evf_entries', $entries ); |
| 279 | if ( is_wp_error( $result ) || ! $result ) { |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | $entry_id = $wpdb->insert_id; |
| 284 | // Create meta data. |
| 285 | if ( $entry_id ) { |
| 286 | foreach ( $entry_list as $field ) { |
| 287 | $field = apply_filters( 'everest_forms_entry_save_fields', $field, $form_data, $entry_id ); |
| 288 | // Add only whitelisted fields to entry meta. |
| 289 | if ( in_array( $field['type'], array( 'html', 'title' ), true ) ) { |
| 290 | continue; |
| 291 | } |
| 292 | |
| 293 | // If empty file is supplied, don't store their data nor send email. |
| 294 | if ( in_array( $field['type'], array( 'image-upload', 'file-upload' ), true ) ) { |
| 295 | |
| 296 | // BW compatibility for previous file uploader. |
| 297 | if ( isset( $field['value']['file_url'] ) && '' === $field['value']['file_url'] ) { |
| 298 | continue; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | // If empty label is provided for choice field, don't store their data nor send email. |
| 303 | if ( in_array( $field['type'], array( 'radio', 'payment-multiple' ), true ) ) { |
| 304 | if ( isset( $field['value']['label'] ) && '' === $field['value']['label'] ) { |
| 305 | continue; |
| 306 | } |
| 307 | } elseif ( in_array( $field['type'], array( 'checkbox', 'payment-checkbox' ), true ) ) { |
| 308 | if ( isset( $field['value']['label'] ) && ( empty( $field['value']['label'] ) || '' === current( $field['value']['label'] ) ) ) { |
| 309 | continue; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | if ( isset( $field['meta_key'], $field['value'] ) && '' !== $field['value'] ) { |
| 314 | $entry_metadata = array( |
| 315 | 'entry_id' => $entry_id, |
| 316 | 'meta_key' => sanitize_key( $field['meta_key'] ), |
| 317 | 'meta_value' => maybe_serialize( $field['value'] ), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value |
| 318 | ); |
| 319 | // Insert entry meta. |
| 320 | $wpdb->insert( $wpdb->prefix . 'evf_entrymeta', $entry_metadata ); |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | return $entry_id; |
| 325 | } |
| 326 | /** |
| 327 | * If the prompt is dismissed |
| 328 | * |
| 329 | * @since 2.0.8 |
| 330 | * |
| 331 | * @return bool |
| 332 | */ |
| 333 | public function is_dimissed() { |
| 334 | return evf_string_to_bool( get_option( 'evf_fm_dismiss_xnotice_' . $this->slug ) ); |
| 335 | } |
| 336 | } |
| 337 |