| 1 |
<?php |
| 2 |
|
| 3 |
abstract class FrmFormMigrator { |
| 4 |
|
| 5 |
public $source_active; |
| 6 |
|
| 7 |
public $slug; |
| 8 |
public $path; |
| 9 |
public $name; |
| 10 |
|
| 11 |
public $response = array(); |
| 12 |
public $tracking = 'frm_forms_imported'; |
| 13 |
|
| 14 |
/** |
| 15 |
* Define required properties. |
| 16 |
*/ |
| 17 |
public function __construct() { |
| 18 |
if ( ! is_admin() ) { |
| 19 |
return; |
| 20 |
} |
| 21 |
|
| 22 |
$this->source_active = is_plugin_active( $this->path ); |
| 23 |
if ( ! $this->source_active ) { |
| 24 |
// if source plugin is not installed, do nothing |
| 25 |
return; |
| 26 |
} |
| 27 |
|
| 28 |
$this->maybe_add_to_import_page(); |
| 29 |
|
| 30 |
$this->response = array( |
| 31 |
'upgrade_omit' => array(), |
| 32 |
'unsupported' => array(), |
| 33 |
); |
| 34 |
} |
| 35 |
|
| 36 |
private function maybe_add_to_import_page() { |
| 37 |
$menu_name = sanitize_title( FrmAppHelper::get_menu_name() ); |
| 38 |
add_action( $menu_name . '_page_formidable-import', array( $this, 'import_page' ), 1 ); |
| 39 |
add_action( 'wp_ajax_frm_import_' . $this->slug, array( $this, 'import_forms' ) ); |
| 40 |
} |
| 41 |
|
| 42 |
public function import_page() { |
| 43 |
?> |
| 44 |
<div class="wrap"> |
| 45 |
<div class="welcome-panel" id="welcome-panel"> |
| 46 |
<h2><?php echo esc_html( $this->name ); ?> Importer</h2> |
| 47 |
<div class="welcome-panel-content" style="text-align:center;margin-bottom:10px;"> |
| 48 |
<p class="about-description"> |
| 49 |
Import forms and settings automatically from <?php echo esc_html( $this->name ); ?>. <br/> |
| 50 |
Select the forms to import. |
| 51 |
</p> |
| 52 |
<form id="frm_form_importer" method="post" |
| 53 |
action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>"> |
| 54 |
<?php wp_nonce_field( 'nonce', 'frm_ajax' ); ?> |
| 55 |
<input type="hidden" name="slug" value="<?php echo esc_attr( $this->slug ); ?>" /> |
| 56 |
<input type="hidden" name="action" value="frm_import_<?php echo esc_attr( $this->slug ); ?>" /> |
| 57 |
<div style="margin:10px auto;max-width:400px;text-align:left;"> |
| 58 |
<?php foreach ( $this->get_forms() as $form_id => $name ) { ?> |
| 59 |
<p> |
| 60 |
<label> |
| 61 |
<input type="checkbox" name="form_id[]" |
| 62 |
value="<?php echo esc_attr( $form_id ); ?>" checked="checked" /> |
| 63 |
<?php |
| 64 |
echo esc_html( $name ); |
| 65 |
$new_form_id = $this->is_imported( $form_id ); |
| 66 |
?> |
| 67 |
<?php if ( $new_form_id ) { ?> |
| 68 |
(<a href="<?php echo esc_url( FrmForm::get_edit_link( $new_form_id ) ); ?>">previously imported</a>) |
| 69 |
<?php } ?> |
| 70 |
</label> |
| 71 |
</p> |
| 72 |
<?php } ?> |
| 73 |
</div> |
| 74 |
<button type="submit" class="button button-primary button-hero">Start Import</button> |
| 75 |
</form> |
| 76 |
<div id="frm-importer-process" class="frm_hidden"> |
| 77 |
|
| 78 |
<p class="process-count"> |
| 79 |
<span class="frm-wait" aria-hidden="true"></span> |
| 80 |
Importing <span class="form-current">1</span> of <span class="form-total">0</span> forms |
| 81 |
from <?php echo esc_html( $this->name ); ?>. |
| 82 |
</p> |
| 83 |
|
| 84 |
<p class="process-completed" class="frm_hidden"> |
| 85 |
The import process has finished! We have successfully imported |
| 86 |
<span class="forms-completed"></span> forms. You can review the results below. |
| 87 |
</p> |
| 88 |
|
| 89 |
<div class="status"></div> |
| 90 |
|
| 91 |
</div> |
| 92 |
</div> |
| 93 |
</div> |
| 94 |
</div> |
| 95 |
<?php |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Import all forms using ajax |
| 100 |
*/ |
| 101 |
public function import_forms() { |
| 102 |
|
| 103 |
check_ajax_referer( 'frm_ajax', 'nonce' ); |
| 104 |
FrmAppHelper::permission_check( 'frm_edit_forms' ); |
| 105 |
|
| 106 |
$forms = FrmAppHelper::get_simple_request( |
| 107 |
array( |
| 108 |
'param' => 'form_id', |
| 109 |
'type' => 'post', |
| 110 |
'sanitize' => 'absint', |
| 111 |
) |
| 112 |
); |
| 113 |
|
| 114 |
if ( is_array( $forms ) ) { |
| 115 |
$imported = array(); |
| 116 |
foreach ( (array) $forms as $form_id ) { |
| 117 |
$imported[] = $this->import_form( $form_id ); |
| 118 |
} |
| 119 |
} else { |
| 120 |
$imported = $this->import_form( $forms ); |
| 121 |
} |
| 122 |
|
| 123 |
wp_send_json_success( $imported ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Import a single form |
| 128 |
*/ |
| 129 |
protected function import_form( $source_id ) { |
| 130 |
|
| 131 |
$source_form = $this->get_form( $source_id ); |
| 132 |
$source_form_name = $this->get_form_name( $source_form ); |
| 133 |
$source_fields = $this->get_form_fields( $source_id ); |
| 134 |
|
| 135 |
// If form does not contain fields, bail. |
| 136 |
if ( empty( $source_fields ) ) { |
| 137 |
wp_send_json_success( |
| 138 |
array( |
| 139 |
'error' => true, |
| 140 |
'name' => esc_html( $source_form_name ), |
| 141 |
'msg' => __( 'No form fields found.', 'formidable' ), |
| 142 |
) |
| 143 |
); |
| 144 |
} |
| 145 |
|
| 146 |
$form = $this->prepare_new_form( $source_id, $source_form_name ); |
| 147 |
|
| 148 |
$this->prepare_fields( $source_fields, $form ); |
| 149 |
|
| 150 |
$this->prepare_form( $source_form, $form ); |
| 151 |
|
| 152 |
return $this->add_form( $form ); |
| 153 |
} |
| 154 |
|
| 155 |
protected function prepare_new_form( $source_id, $source_form_name ) { |
| 156 |
return array( |
| 157 |
'import_form_id' => $source_id, |
| 158 |
'fields' => array(), |
| 159 |
'name' => $source_form_name, |
| 160 |
'description' => '', |
| 161 |
'options' => array(), |
| 162 |
'actions' => array(), |
| 163 |
); |
| 164 |
} |
| 165 |
|
| 166 |
protected function prepare_form( $form, &$new_form ) { |
| 167 |
// customize this function |
| 168 |
} |
| 169 |
|
| 170 |
protected function prepare_fields( $fields, &$form ) { |
| 171 |
$field_order = 1; |
| 172 |
foreach ( $fields as $field ) { |
| 173 |
|
| 174 |
$label = $this->get_field_label( $field ); |
| 175 |
$type = $this->get_field_type( $field ); |
| 176 |
|
| 177 |
// check if field is unsupported. If unsupported make note and continue |
| 178 |
if ( $this->is_unsupported_field( $type ) ) { |
| 179 |
$this->response['unsupported'][] = $label; |
| 180 |
continue; |
| 181 |
} |
| 182 |
|
| 183 |
if ( $this->should_skip_field( $type ) ) { |
| 184 |
$this->response['upgrade_omit'][] = $label; |
| 185 |
continue; |
| 186 |
} |
| 187 |
|
| 188 |
$new_field = FrmFieldsHelper::setup_new_vars( $this->convert_field_type( $type ) ); |
| 189 |
$new_field['name'] = $label; |
| 190 |
$new_field['field_order'] = $field_order; |
| 191 |
$new_field['original'] = $type; |
| 192 |
|
| 193 |
$this->prepare_field( $field, $new_field ); |
| 194 |
$form['fields'][] = $new_field; |
| 195 |
|
| 196 |
$field_order ++; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
protected function prepare_field( $field, &$new_field ) { |
| 201 |
// customize this function |
| 202 |
} |
| 203 |
|
| 204 |
protected function convert_field_type( $type ) { |
| 205 |
return $type; |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* Add the new form to the database and return AJAX data. |
| 210 |
* |
| 211 |
* @since 1.4.2 |
| 212 |
* |
| 213 |
* @param array $form Form to import. |
| 214 |
* @param array $upgrade_omit No field alternative |
| 215 |
*/ |
| 216 |
private function add_form( $form, $upgrade_omit = array() ) { |
| 217 |
|
| 218 |
// Create empty form so we have an ID to work with. |
| 219 |
$form_id = FrmForm::create( |
| 220 |
array( |
| 221 |
'name' => $form['name'], |
| 222 |
'description' => $form['description'], |
| 223 |
'options' => $form['options'], |
| 224 |
'form_key' => $form['name'], |
| 225 |
'status' => 'published', |
| 226 |
) |
| 227 |
); |
| 228 |
|
| 229 |
if ( empty( $form_id ) ) { |
| 230 |
return array( |
| 231 |
'error' => true, |
| 232 |
'name' => sanitize_text_field( $form['settings']['form_title'] ), |
| 233 |
'msg' => esc_html__( 'There was an error while creating a new form.', 'formidable' ), |
| 234 |
); |
| 235 |
} |
| 236 |
|
| 237 |
foreach ( $form['fields'] as $key => $new_field ) { |
| 238 |
$new_field['form_id'] = $form_id; |
| 239 |
$form['fields'][ $key ]['id'] = FrmField::create( $new_field ); |
| 240 |
} |
| 241 |
|
| 242 |
// create emails |
| 243 |
foreach ( $form['actions'] as $action ) { |
| 244 |
$action_control = FrmFormActionsController::get_form_actions( $action['type'] ); |
| 245 |
unset( $action['type'] ); |
| 246 |
$new_action = $action_control->prepare_new( $form_id ); |
| 247 |
foreach ( $action as $key => $value ) { |
| 248 |
if ( $key === 'post_title' ) { |
| 249 |
$new_action->post_title = $value; |
| 250 |
} else { |
| 251 |
$new_action->post_content[ $key ] = $this->replace_smart_tags( $value, $form['fields'] ); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
$action_control->save_settings( $new_action ); |
| 256 |
} |
| 257 |
|
| 258 |
$this->track_import( $form['import_form_id'], $form_id ); |
| 259 |
|
| 260 |
// Build and send final AJAX response! |
| 261 |
return array( |
| 262 |
'name' => $form['name'], |
| 263 |
'id' => $form_id, |
| 264 |
'link' => esc_url_raw( FrmForm::get_edit_link( $form_id ) ), |
| 265 |
'upgrade_omit' => $this->response['upgrade_omit'], |
| 266 |
); |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* After a form has been successfully imported we track it, so that in the |
| 271 |
* future we can alert users if they try to import a form that has already |
| 272 |
* been imported. |
| 273 |
* |
| 274 |
* @param int $source_id Imported plugin form ID |
| 275 |
* @param int $new_form_id Formidable form ID |
| 276 |
*/ |
| 277 |
private function track_import( $source_id, $new_form_id ) { |
| 278 |
|
| 279 |
$imported = $this->get_tracked_import(); |
| 280 |
|
| 281 |
$imported[ $this->slug ][ $new_form_id ] = $source_id; |
| 282 |
|
| 283 |
update_option( $this->tracking, $imported, false ); |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* @return array |
| 288 |
*/ |
| 289 |
private function get_tracked_import() { |
| 290 |
return get_option( $this->tracking, array() ); |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* @param int $source_id Imported plugin form ID |
| 295 |
* |
| 296 |
* @return int the ID of the created form or 0 |
| 297 |
*/ |
| 298 |
private function is_imported( $source_id ) { |
| 299 |
$imported = $this->get_tracked_import(); |
| 300 |
$new_form_id = 0; |
| 301 |
if ( isset( $imported[ $this->slug ] ) && in_array( $source_id, $imported[ $this->slug ] ) ) { |
| 302 |
$new_form_id = array_search( $source_id, array_reverse( $imported[ $this->slug ], true ) ); |
| 303 |
} |
| 304 |
|
| 305 |
return $new_form_id; |
| 306 |
} |
| 307 |
|
| 308 |
/** Start functions here that should be overridden **/ |
| 309 |
|
| 310 |
/** |
| 311 |
* @return array |
| 312 |
*/ |
| 313 |
protected function unsupported_field_types() { |
| 314 |
return array(); |
| 315 |
} |
| 316 |
|
| 317 |
private function is_unsupported_field( $type ) { |
| 318 |
$fields = $this->unsupported_field_types(); |
| 319 |
|
| 320 |
return in_array( $type, $fields, true ); |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* Strict PRO fields with no Lite alternatives. |
| 325 |
* |
| 326 |
* @return array |
| 327 |
*/ |
| 328 |
protected function skip_pro_fields() { |
| 329 |
return array(); |
| 330 |
} |
| 331 |
|
| 332 |
private function should_skip_field( $type ) { |
| 333 |
$skip_pro_fields = $this->skip_pro_fields(); |
| 334 |
|
| 335 |
return ( ! FrmAppHelper::pro_is_installed() && in_array( $type, $skip_pro_fields, true ) ); |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* Replace 3rd-party form provider tags/shortcodes with our own Tags. |
| 340 |
* |
| 341 |
* @param string $string String to process the smart tag in. |
| 342 |
* @param array $fields List of fields for the form. |
| 343 |
* |
| 344 |
* @return string |
| 345 |
*/ |
| 346 |
protected function replace_smart_tags( $string, $fields ) { |
| 347 |
return $string; |
| 348 |
} |
| 349 |
|
| 350 |
/** |
| 351 |
* Get ALL THE FORMS. |
| 352 |
* |
| 353 |
* @return array |
| 354 |
*/ |
| 355 |
public function get_forms() { |
| 356 |
return array(); |
| 357 |
} |
| 358 |
|
| 359 |
public function get_form( $id ) { |
| 360 |
return array(); |
| 361 |
} |
| 362 |
|
| 363 |
/** |
| 364 |
* @param object|array $source_form |
| 365 |
* |
| 366 |
* @return string |
| 367 |
*/ |
| 368 |
protected function get_form_name( $source_form ) { |
| 369 |
return __( 'Default Form', 'formidable' ); |
| 370 |
} |
| 371 |
|
| 372 |
/** |
| 373 |
* @param object|array|int $source_form |
| 374 |
* |
| 375 |
* @return array |
| 376 |
*/ |
| 377 |
protected function get_form_fields( $source_form ) { |
| 378 |
return array(); |
| 379 |
} |
| 380 |
|
| 381 |
/** |
| 382 |
* @param object|array $field |
| 383 |
* |
| 384 |
* @return string |
| 385 |
*/ |
| 386 |
protected function get_field_type( $field ) { |
| 387 |
return is_array( $field ) ? $field['type'] : $field->type; |
| 388 |
} |
| 389 |
|
| 390 |
/** |
| 391 |
* @param object|array $field |
| 392 |
* |
| 393 |
* @return string |
| 394 |
*/ |
| 395 |
protected function get_field_label( $field ) { |
| 396 |
$type = $this->get_field_type( $field ); |
| 397 |
$label = sprintf( |
| 398 |
/* translators: %1$s - field type */ |
| 399 |
esc_html__( '%1$s Field', 'formidable' ), |
| 400 |
ucfirst( $type ) |
| 401 |
); |
| 402 |
|
| 403 |
return trim( $label ); |
| 404 |
} |
| 405 |
} |
| 406 |
|