| @@ -1,57 +1,18 @@ | ||
| 1 | 1 | <?php |
| 2 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 3 | - die( 'You are not allowed to call this page directly.' ); | |
| 4 | -} | |
| 5 | 2 | |
| 6 | 3 | abstract class FrmFormMigrator { |
| 7 | 4 | |
| 8 | - /** | |
| 9 | - * @var bool | |
| 10 | - */ | |
| 11 | 5 | public $source_active; |
| 12 | 6 | |
| 13 | - /** | |
| 14 | - * @var string | |
| 15 | - */ | |
| 16 | 7 | public $slug; |
| 17 | - | |
| 18 | - /** | |
| 19 | - * @var string | |
| 20 | - */ | |
| 21 | 8 | public $path; |
| 22 | - | |
| 23 | - /** | |
| 24 | - * @var string | |
| 25 | - */ | |
| 26 | 9 | public $name; |
| 27 | 10 | |
| 28 | - /** | |
| 29 | - * @var array | |
| 30 | - */ | |
| 31 | 11 | public $response = array(); |
| 32 | - | |
| 33 | - /** | |
| 34 | - * @var string | |
| 35 | - */ | |
| 36 | 12 | public $tracking = 'frm_forms_imported'; |
| 37 | 13 | |
| 38 | 14 | /** |
| 39 | - * @var array | |
| 40 | - */ | |
| 41 | - protected $fields_map = array(); | |
| 42 | - | |
| 43 | - /** | |
| 44 | - * @var mixed | |
| 45 | - */ | |
| 46 | - protected $current_source_form; | |
| 47 | - | |
| 48 | - /** | |
| 49 | - * @var array | |
| 50 | - */ | |
| 51 | - protected $current_section = array(); | |
| 52 | - | |
| 53 | - /** | |
| 54 | 15 | * Define required properties. |
| 55 | 16 | */ |
| 56 | 17 | public function __construct() { |
| 57 | 18 | if ( ! is_admin() ) { |
| @@ -57,16 +18,11 @@ | ||
| 57 | 18 | if ( ! is_admin() ) { |
| 58 | 19 | return; |
| 59 | 20 | } |
| 60 | 21 | |
| 61 | - if ( ! function_exists( 'is_plugin_active' ) ) { | |
| 62 | - require_once ABSPATH . '/wp-admin/includes/plugin.php'; | |
| 63 | - } | |
| 64 | - | |
| 65 | 22 | $this->source_active = is_plugin_active( $this->path ); |
| 66 | - | |
| 67 | 23 | if ( ! $this->source_active ) { |
| 68 | - // If source plugin is not installed, do nothing | |
| 24 | + // if source plugin is not installed, do nothing | |
| 69 | 25 | return; |
| 70 | 26 | } |
| 71 | 27 | |
| 72 | 28 | $this->maybe_add_to_import_page(); |
| @@ -76,90 +32,55 @@ | ||
| 76 | 32 | 'unsupported' => array(), |
| 77 | 33 | ); |
| 78 | 34 | } |
| 79 | 35 | |
| 80 | - /** | |
| 81 | - * @return void | |
| 82 | - */ | |
| 83 | 36 | private function maybe_add_to_import_page() { |
| 84 | - add_action( 'frm_import_settings', array( $this, 'import_page' ) ); | |
| 37 | + $menu_name = sanitize_title( FrmAppHelper::get_menu_name() ); | |
| 38 | + add_action( $menu_name . '_page_formidable-import', array( $this, 'import_page' ), 1 ); | |
| 85 | 39 | add_action( 'wp_ajax_frm_import_' . $this->slug, array( $this, 'import_forms' ) ); |
| 86 | 40 | } |
| 87 | 41 | |
| 88 | - /** | |
| 89 | - * @return void | |
| 90 | - */ | |
| 91 | 42 | public function import_page() { |
| 92 | - $forms = $this->get_forms(); | |
| 93 | - // phpcs:disable Generic.WhiteSpace.ScopeIndent | |
| 94 | 43 | ?> |
| 95 | 44 | <div class="wrap"> |
| 96 | - <h2 class="frm-h2"><?php echo esc_html( $this->name ); ?> Importer</h2> | |
| 97 | - <p class="howto">Import forms and settings automatically from <?php echo esc_html( $this->name ); ?>.</p> | |
| 98 | - <div id="welcome-panel"> | |
| 99 | - <div style="margin-bottom:10px;"> | |
| 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;"> | |
| 100 | 48 | <p class="about-description"> |
| 49 | + Import forms and settings automatically from <?php echo esc_html( $this->name ); ?>. <br/> | |
| 101 | 50 | Select the forms to import. |
| 102 | 51 | </p> |
| 103 | - <form class="frm_form_importer" method="post" | |
| 104 | - action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>"> | |
| 52 | + <form id="frm_form_importer" method="post" action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>"> | |
| 105 | 53 | <?php wp_nonce_field( 'nonce', 'frm_ajax' ); ?> |
| 106 | 54 | <input type="hidden" name="slug" value="<?php echo esc_attr( $this->slug ); ?>" /> |
| 107 | 55 | <input type="hidden" name="action" value="frm_import_<?php echo esc_attr( $this->slug ); ?>" /> |
| 108 | - <div style="max-width:400px;text-align:left;"> | |
| 109 | - <?php | |
| 110 | - if ( ! $forms ) { | |
| 111 | - esc_html_e( 'No Forms Found.', 'formidable' ); | |
| 112 | - } | |
| 113 | - ?> | |
| 114 | - <?php foreach ( $forms as $form_id => $name ) { ?> | |
| 56 | + <div style="margin:10px auto;max-width:400px;text-align:left;"> | |
| 57 | + <?php foreach ( $this->get_forms() as $form_id => $name ) { ?> | |
| 115 | 58 | <p> |
| 116 | 59 | <label> |
| 117 | - <input type="checkbox" name="form_id[]" | |
| 118 | - value="<?php echo esc_attr( $form_id ); ?>" checked="checked" /> | |
| 60 | + <input type="checkbox" name="form_id[]" value="<?php echo esc_attr( $form_id ); ?>" checked="checked" /> | |
| 119 | 61 | <?php |
| 120 | 62 | echo esc_html( $name ); |
| 121 | 63 | $new_form_id = $this->is_imported( $form_id ); |
| 122 | 64 | ?> |
| 123 | 65 | <?php if ( $new_form_id ) { ?> |
| 124 | - (<a href="<?php echo esc_url( FrmForm::get_edit_link( $new_form_id ) ); ?>">previously imported</a>) | |
| 66 | + (<a href="<?php echo esc_url( admin_url( 'admin.php?page=formidable&frm_action=edit&id=' . $new_form_id ) ); ?>">previously imported</a>) | |
| 125 | 67 | <?php } ?> |
| 126 | 68 | </label> |
| 127 | 69 | </p> |
| 128 | 70 | <?php } ?> |
| 129 | 71 | </div> |
| 130 | - <?php | |
| 131 | - $button_atts = array( | |
| 132 | - 'type' => 'submit', | |
| 133 | - 'class' => 'button button-primary frm-button-primary', | |
| 134 | - ); | |
| 135 | - | |
| 136 | - if ( ! $forms ) { | |
| 137 | - $button_atts['disabled'] = 'disabled'; | |
| 138 | - } | |
| 139 | - ?> | |
| 140 | - <p class="submit"> | |
| 141 | - <button <?php FrmAppHelper::array_to_html_params( $button_atts, true ); ?>> | |
| 142 | - <?php esc_html_e( 'Start Import', 'formidable' ); ?> | |
| 143 | - </button> | |
| 144 | - </p> | |
| 72 | + <button type="submit" class="button button-primary button-hero">Start Import</button> | |
| 145 | 73 | </form> |
| 146 | - <div id="frm-importer-process" class="frm-importer-process frm_hidden"> | |
| 74 | + <div id="frm-importer-process" class="frm_hidden"> | |
| 147 | 75 | |
| 148 | 76 | <p class="process-count"> |
| 149 | - <span class="frm-wait" aria-hidden="true"></span> | |
| 150 | - Importing <span class="form-current">1</span> of <span class="form-total">0</span> forms | |
| 151 | - from <?php echo esc_html( $this->name ); ?>. | |
| 77 | + <i class="fa fa-spinner fa-spin" aria-hidden="true"></i> | |
| 78 | + Importing <span class="form-current">1</span> of <span class="form-total">0</span> forms from <?php echo esc_html( $this->name ); ?>. | |
| 152 | 79 | </p> |
| 153 | 80 | |
| 154 | 81 | <p class="process-completed" class="frm_hidden"> |
| 155 | - <?php | |
| 156 | - printf( | |
| 157 | - // translators: %s is the number of forms that were imported. | |
| 158 | - esc_html__( 'The import process has finished! We have successfully imported %s forms. You can review the results below.', 'formidable' ), | |
| 159 | - '<span class="forms-completed"></span>' | |
| 160 | - ); | |
| 161 | - ?> | |
| 82 | + The import process has finished! We have successfully imported <span class="forms-completed"></span> forms. You can review the results below. | |
| 162 | 83 | </p> |
| 163 | 84 | |
| 164 | 85 | <div class="status"></div> |
| 165 | 86 | |
| @@ -167,17 +88,15 @@ | ||
| 167 | 88 | </div> |
| 168 | 89 | </div> |
| 169 | 90 | </div> |
| 170 | 91 | <?php |
| 171 | - // phpcs:enable Generic.WhiteSpace.ScopeIndent | |
| 172 | 92 | } |
| 173 | 93 | |
| 174 | 94 | /** |
| 175 | 95 | * Import all forms using ajax |
| 176 | - * | |
| 177 | - * @return void | |
| 178 | 96 | */ |
| 179 | 97 | public function import_forms() { |
| 98 | + | |
| 180 | 99 | check_ajax_referer( 'frm_ajax', 'nonce' ); |
| 181 | 100 | FrmAppHelper::permission_check( 'frm_edit_forms' ); |
| 182 | 101 | |
| 183 | 102 | $forms = FrmAppHelper::get_simple_request( |
| @@ -189,10 +108,9 @@ | ||
| 189 | 108 | ); |
| 190 | 109 | |
| 191 | 110 | if ( is_array( $forms ) ) { |
| 192 | 111 | $imported = array(); |
| 193 | - | |
| 194 | - foreach ( $forms as $form_id ) { | |
| 112 | + foreach ( (array) $forms as $form_id ) { | |
| 195 | 113 | $imported[] = $this->import_form( $form_id ); |
| 196 | 114 | } |
| 197 | 115 | } else { |
| 198 | 116 | $imported = $this->import_form( $forms ); |
| @@ -202,23 +120,17 @@ | ||
| 202 | 120 | } |
| 203 | 121 | |
| 204 | 122 | /** |
| 205 | 123 | * Import a single form |
| 206 | - * | |
| 207 | - * @param int $source_id Source form ID. | |
| 208 | - * | |
| 209 | - * @return array | |
| 210 | 124 | */ |
| 211 | 125 | protected function import_form( $source_id ) { |
| 212 | - $source_form = $this->get_form( $source_id ); | |
| 213 | - $source_form_name = $this->get_form_name( $source_form ); | |
| 214 | - $source_fields = $this->get_form_fields( $source_form ); | |
| 215 | - $this->maybe_add_end_fields( $source_fields ); | |
| 216 | 126 | |
| 217 | - $this->current_source_form = $source_form; | |
| 127 | + $source_form = $this->get_form( $source_id ); | |
| 128 | + $source_form_name = $this->get_form_name( $source_form ); | |
| 129 | + $source_fields = $this->get_form_fields( $source_id ); | |
| 218 | 130 | |
| 219 | 131 | // If form does not contain fields, bail. |
| 220 | - if ( ! $source_fields ) { | |
| 132 | + if ( empty( $source_fields ) ) { | |
| 221 | 133 | wp_send_json_success( |
| 222 | 134 | array( |
| 223 | 135 | 'error' => true, |
| 224 | 136 | 'name' => esc_html( $source_form_name ), |
| @@ -232,22 +144,11 @@ | ||
| 232 | 144 | $this->prepare_fields( $source_fields, $form ); |
| 233 | 145 | |
| 234 | 146 | $this->prepare_form( $source_form, $form ); |
| 235 | 147 | |
| 236 | - $response = $this->add_form( $form ); | |
| 237 | - | |
| 238 | - // reset | |
| 239 | - $this->current_source_form = null; | |
| 240 | - | |
| 241 | - return $response; | |
| 148 | + return $this->add_form( $form ); | |
| 242 | 149 | } |
| 243 | 150 | |
| 244 | - /** | |
| 245 | - * @param int|string $source_id | |
| 246 | - * @param string $source_form_name | |
| 247 | - * | |
| 248 | - * @return array | |
| 249 | - */ | |
| 250 | 151 | protected function prepare_new_form( $source_id, $source_form_name ) { |
| 251 | 152 | return array( |
| 252 | 153 | 'import_form_id' => $source_id, |
| 253 | 154 | 'fields' => array(), |
| @@ -257,33 +158,20 @@ | ||
| 257 | 158 | 'actions' => array(), |
| 258 | 159 | ); |
| 259 | 160 | } |
| 260 | 161 | |
| 261 | - /** | |
| 262 | - * @param array|object $form | |
| 263 | - * @param array $new_form | |
| 264 | - * | |
| 265 | - * @return void | |
| 266 | - */ | |
| 267 | 162 | protected function prepare_form( $form, &$new_form ) { |
| 268 | - // Customize this function | |
| 163 | + // customize this function | |
| 269 | 164 | } |
| 270 | 165 | |
| 271 | - /** | |
| 272 | - * @param array $fields | |
| 273 | - * @param array $form | |
| 274 | - * | |
| 275 | - * @return void | |
| 276 | - */ | |
| 277 | 166 | protected function prepare_fields( $fields, &$form ) { |
| 278 | 167 | $field_order = 1; |
| 168 | + foreach ( $fields as $field ) { | |
| 279 | 169 | |
| 280 | - foreach ( $fields as $field ) { | |
| 281 | - $field = (array) $field; | |
| 282 | 170 | $label = $this->get_field_label( $field ); |
| 283 | 171 | $type = $this->get_field_type( $field ); |
| 284 | 172 | |
| 285 | - // Check if field is unsupported. If unsupported make note and continue | |
| 173 | + // check if field is unsupported. If unsupported make note and continue | |
| 286 | 174 | if ( $this->is_unsupported_field( $type ) ) { |
| 287 | 175 | $this->response['unsupported'][] = $label; |
| 288 | 176 | continue; |
| 289 | 177 | } |
| @@ -292,297 +180,99 @@ | ||
| 292 | 180 | $this->response['upgrade_omit'][] = $label; |
| 293 | 181 | continue; |
| 294 | 182 | } |
| 295 | 183 | |
| 296 | - $new_type = $this->convert_field_type( $type, $field ); | |
| 297 | - $new_field = FrmFieldsHelper::setup_new_vars( $new_type ); | |
| 298 | - $new_field['name'] = $label; | |
| 184 | + $new_field = FrmFieldsHelper::setup_new_vars( $this->convert_field_type( $type ) ); | |
| 185 | + $new_field['name'] = $label; | |
| 299 | 186 | $new_field['field_order'] = $field_order; |
| 300 | - $new_field['original'] = $type; | |
| 187 | + $new_field['original'] = $type; | |
| 301 | 188 | |
| 302 | 189 | $this->prepare_field( $field, $new_field ); |
| 303 | - | |
| 304 | - $in_section = $this->current_section && ! in_array( $new_type, $this->fields_with_end(), true ) && $new_type !== 'break'; | |
| 305 | - | |
| 306 | - if ( $in_section ) { | |
| 307 | - $new_field['field_options']['in_section'] = $this->current_section['id']; | |
| 308 | - } | |
| 309 | - | |
| 310 | 190 | $form['fields'][] = $new_field; |
| 311 | 191 | |
| 312 | - if ( in_array( $new_type, $this->fields_with_end(), true ) ) { | |
| 313 | - $this->current_section = $field; | |
| 314 | - } elseif ( $new_type === 'break' || $new_type === 'end_divider' ) { | |
| 315 | - $this->current_section = array(); | |
| 316 | - } | |
| 317 | - | |
| 318 | - // This may occasionally skip one level/order e.g. after adding a | |
| 319 | - // List field, as field_order would already be prepared to be used. | |
| 320 | - ++$field_order; | |
| 321 | - | |
| 322 | - if ( empty( $new_field['fields'] ) || ! is_array( $new_field['fields'] ) ) { | |
| 323 | - continue; | |
| 324 | - } | |
| 325 | - | |
| 326 | - // we have (inner) fields to merge | |
| 327 | - | |
| 328 | - $form['fields'] = array_merge( $form['fields'], $new_field['fields'] ); | |
| 329 | - // Set the new field_order as it would have changed | |
| 330 | - $field_order = $new_field['current_order']; | |
| 331 | - }//end foreach | |
| 192 | + $field_order++; | |
| 193 | + } | |
| 332 | 194 | } |
| 333 | 195 | |
| 334 | - /** | |
| 335 | - * @param array|object $field | |
| 336 | - * @param array $new_field | |
| 337 | - * | |
| 338 | - * @return void | |
| 339 | - */ | |
| 340 | 196 | protected function prepare_field( $field, &$new_field ) { |
| 341 | - // Customize this function | |
| 197 | + // customize this function | |
| 342 | 198 | } |
| 343 | 199 | |
| 344 | - /** | |
| 345 | - * Add any field types that will need an end section field. | |
| 346 | - * | |
| 347 | - * @since 4.04.03 | |
| 348 | - * | |
| 349 | - * @return array | |
| 350 | - */ | |
| 351 | - protected function fields_with_end() { | |
| 352 | - return array( 'divider' ); | |
| 200 | + protected function convert_field_type( $type ) { | |
| 201 | + return $type; | |
| 353 | 202 | } |
| 354 | 203 | |
| 355 | 204 | /** |
| 356 | - * @since 4.04.03 | |
| 205 | + * Add the new form to the database and return AJAX data. | |
| 357 | 206 | * |
| 358 | - * @param array $fields | |
| 207 | + * @since 1.4.2 | |
| 359 | 208 | * |
| 360 | - * @return void | |
| 209 | + * @param array $form Form to import. | |
| 210 | + * @param array $upgrade_omit No field alternative | |
| 361 | 211 | */ |
| 362 | - protected function maybe_add_end_fields( &$fields ) { | |
| 363 | - $with_end = $this->fields_with_end(); | |
| 212 | + private function add_form( $form, $upgrade_omit = array() ) { | |
| 364 | 213 | |
| 365 | - if ( ! $with_end ) { | |
| 366 | - return; | |
| 367 | - } | |
| 214 | + // Create empty form so we have an ID to work with. | |
| 215 | + $form_id = FrmForm::create( | |
| 216 | + array( | |
| 217 | + 'name' => $form['name'], | |
| 218 | + 'description' => $form['description'], | |
| 219 | + 'options' => $form['options'], | |
| 220 | + 'form_key' => $form['name'], | |
| 221 | + 'status' => 'published', | |
| 222 | + ) | |
| 223 | + ); | |
| 368 | 224 | |
| 369 | - $open = array(); | |
| 370 | - $order = 0; | |
| 371 | - | |
| 372 | - foreach ( $fields as $field ) { | |
| 373 | - ++$order; | |
| 374 | - $type = $this->get_field_type( $field ); | |
| 375 | - $new_type = $this->convert_field_type( $type, $field ); | |
| 376 | - | |
| 377 | - if ( ! in_array( $new_type, $with_end, true ) && $new_type !== 'break' ) { | |
| 378 | - continue; | |
| 379 | - } | |
| 380 | - | |
| 381 | - if ( $open ) { | |
| 382 | - $this->insert_end_section( $fields, $order ); | |
| 383 | - $open = array(); | |
| 384 | - } | |
| 385 | - | |
| 386 | - if ( in_array( $new_type, $with_end, true ) ) { | |
| 387 | - $open = $field; | |
| 388 | - } | |
| 225 | + if ( empty( $form_id ) ) { | |
| 226 | + return array( | |
| 227 | + 'error' => true, | |
| 228 | + 'name' => sanitize_text_field( $form['settings']['form_title'] ), | |
| 229 | + 'msg' => esc_html__( 'There was an error while creating a new form.', 'formidable' ), | |
| 230 | + ); | |
| 389 | 231 | } |
| 390 | 232 | |
| 391 | - if ( $open ) { | |
| 392 | - $this->insert_end_section( $fields, $order ); | |
| 233 | + foreach ( $form['fields'] as $key => $new_field ) { | |
| 234 | + $new_field['form_id'] = $form_id; | |
| 235 | + $form['fields'][ $key ]['id'] = FrmField::create( $new_field ); | |
| 393 | 236 | } |
| 394 | - } | |
| 395 | 237 | |
| 396 | - /** | |
| 397 | - * @since 4.04.03 | |
| 398 | - * | |
| 399 | - * @param array $fields | |
| 400 | - * @param int $order | |
| 401 | - * | |
| 402 | - * @return void | |
| 403 | - */ | |
| 404 | - protected function insert_end_section( &$fields, &$order ) { | |
| 405 | - $sub = FrmFieldsHelper::setup_new_vars( 'end_divider' ); | |
| 406 | - $sub['name'] = __( 'Section Buttons', 'formidable' ); | |
| 407 | - $subs = array( $sub ); | |
| 408 | - $this->insert_fields_in_array( $subs, $order, 0, $fields ); | |
| 409 | - ++$order; | |
| 410 | - } | |
| 238 | + // create emails | |
| 239 | + foreach ( $form['actions'] as $action ) { | |
| 240 | + $action_control = FrmFormActionsController::get_form_actions( $action['type'] ); | |
| 241 | + unset( $action['type'] ); | |
| 242 | + $new_action = $action_control->prepare_new( $form_id ); | |
| 243 | + foreach ( $action as $key => $value ) { | |
| 244 | + if ( $key === 'post_title' ) { | |
| 245 | + $new_action->post_title = $value; | |
| 246 | + } else { | |
| 247 | + $new_action->post_content[ $key ] = $this->replace_smart_tags( $value, $form['fields'] ); | |
| 248 | + } | |
| 249 | + } | |
| 411 | 250 | |
| 412 | - /** | |
| 413 | - * Replace the original combo field with a group. | |
| 414 | - * This switches the name field to individual fields. | |
| 415 | - * | |
| 416 | - * @since 4.04.03 | |
| 417 | - * | |
| 418 | - * @param array $subs | |
| 419 | - * @param int $start | |
| 420 | - * @param int $remove | |
| 421 | - * @param array $fields | |
| 422 | - * | |
| 423 | - * @return void | |
| 424 | - */ | |
| 425 | - protected function insert_fields_in_array( $subs, $start, $remove, &$fields ) { | |
| 426 | - array_splice( $fields, $start, $remove, $subs ); | |
| 427 | - } | |
| 428 | - | |
| 429 | - /** | |
| 430 | - * @param string $type | |
| 431 | - * @param array $field | |
| 432 | - * @param string $use Which field type to prefer to consider $field as. | |
| 433 | - * This also eases the recursive use of the method, | |
| 434 | - * particularly the overrides in child classes, as | |
| 435 | - * there will be no need to rebuild the converter | |
| 436 | - * array at usage locations. | |
| 437 | - * | |
| 438 | - * @return string | |
| 439 | - */ | |
| 440 | - protected function convert_field_type( $type, $field = array(), $use = '' ) { | |
| 441 | - if ( ! $field ) { | |
| 442 | - // For reverse compatibility. | |
| 443 | - return $type; | |
| 251 | + $action_control->save_settings( $new_action ); | |
| 444 | 252 | } |
| 445 | 253 | |
| 446 | - return $use ? $use : $field['type']; | |
| 447 | - } | |
| 448 | - | |
| 449 | - /** | |
| 450 | - * Add the new form to the database and return AJAX data. | |
| 451 | - * | |
| 452 | - * @param array $form Form to import. | |
| 453 | - * @param array $upgrade_omit No field alternative. | |
| 454 | - * | |
| 455 | - * @return array | |
| 456 | - */ | |
| 457 | - protected function add_form( $form, $upgrade_omit = array() ) { | |
| 458 | - // Create empty form so we have an ID to work with. | |
| 459 | - $form_id = $this->create_form( $form ); | |
| 460 | - | |
| 461 | - if ( ! $form_id ) { | |
| 462 | - return $this->form_creation_error_response( $form ); | |
| 463 | - } | |
| 464 | - | |
| 465 | - $this->create_fields( $form_id, $form ); | |
| 466 | - | |
| 467 | - $this->create_emails( $form, $form_id ); | |
| 468 | - | |
| 469 | 254 | $this->track_import( $form['import_form_id'], $form_id ); |
| 470 | 255 | |
| 471 | 256 | // Build and send final AJAX response! |
| 472 | 257 | return array( |
| 473 | - 'name' => $form['name'], | |
| 474 | - 'id' => $form_id, | |
| 475 | - 'link' => esc_url_raw( FrmForm::get_edit_link( $form_id ) ), | |
| 476 | - 'upgrade_omit' => $this->response['upgrade_omit'], | |
| 258 | + 'name' => $form['name'], | |
| 259 | + 'id' => $form_id, | |
| 260 | + 'link' => esc_url_raw( admin_url( 'admin.php?page=formidable&frm_action=edit&id=' . $form_id ) ), | |
| 261 | + 'upgrade_omit' => $this->response['upgrade_omit'], | |
| 477 | 262 | ); |
| 478 | 263 | } |
| 479 | 264 | |
| 480 | 265 | /** |
| 481 | - * @since 4.04.03 | |
| 482 | - * | |
| 483 | - * @param array $form parameters for the new form to be created. Only | |
| 484 | - * the name key is a must. The keys are the column | |
| 485 | - * names of the forms table in the DB. | |
| 486 | - * | |
| 487 | - * @return bool|int The ID of the newly created form or false on failure. | |
| 488 | - */ | |
| 489 | - protected function create_form( $form ) { | |
| 490 | - $form['form_key'] = $form['name']; | |
| 491 | - $form['status'] = 'published'; | |
| 492 | - | |
| 493 | - return FrmForm::create( $form ); | |
| 494 | - } | |
| 495 | - | |
| 496 | - /** | |
| 497 | - * @since 4.04.03 | |
| 498 | - * | |
| 499 | - * @param array $form | |
| 500 | - * | |
| 501 | - * @return array | |
| 502 | - */ | |
| 503 | - protected function form_creation_error_response( $form ) { | |
| 504 | - return array( | |
| 505 | - 'error' => true, | |
| 506 | - 'name' => sanitize_text_field( $form['name'] ), | |
| 507 | - 'msg' => esc_html__( 'There was an error while creating a new form.', 'formidable' ), | |
| 508 | - ); | |
| 509 | - } | |
| 510 | - | |
| 511 | - /** | |
| 512 | - * @since 4.04.03 | |
| 513 | - * | |
| 514 | - * @param int $form_id | |
| 515 | - * @param array $form | |
| 516 | - * | |
| 517 | - * @return void | |
| 518 | - */ | |
| 519 | - protected function create_fields( $form_id, &$form ) { | |
| 520 | - foreach ( $form['fields'] as $key => $new_field ) { | |
| 521 | - $new_field['form_id'] = $form_id; | |
| 522 | - $form['fields'][ $key ]['id'] = FrmField::create( $new_field ); | |
| 523 | - } | |
| 524 | - } | |
| 525 | - | |
| 526 | - /** | |
| 527 | - * @since 4.04.03 | |
| 528 | - * | |
| 529 | - * @param array $form | |
| 530 | - * @param int|string $form_id | |
| 531 | - * | |
| 532 | - * @return void | |
| 533 | - */ | |
| 534 | - protected function create_emails( $form, $form_id ) { | |
| 535 | - foreach ( $form['actions'] as $action ) { | |
| 536 | - $this->save_action( $action, $form, $form_id ); | |
| 537 | - } | |
| 538 | - } | |
| 539 | - | |
| 540 | - /** | |
| 541 | - * @since 4.04.03 | |
| 542 | - * | |
| 543 | - * @param array $action | |
| 544 | - * @param array $form | |
| 545 | - * @param int $form_id | |
| 546 | - * | |
| 547 | - * @return int|WP_Error | |
| 548 | - */ | |
| 549 | - protected function save_action( $action, $form, $form_id ) { | |
| 550 | - /** | |
| 551 | - * @var FrmFormAction | |
| 552 | - */ | |
| 553 | - $action_control = FrmFormActionsController::get_form_actions( $action['type'] ); | |
| 554 | - unset( $action['type'] ); | |
| 555 | - $new_action = $action_control->prepare_new( $form_id ); | |
| 556 | - | |
| 557 | - foreach ( $action as $key => $value ) { | |
| 558 | - if ( $key === 'post_title' ) { | |
| 559 | - $new_action->post_title = $value; | |
| 560 | - } elseif ( $key === 'ID' ) { | |
| 561 | - $new_action->ID = $value; | |
| 562 | - } elseif ( $key === 'the_post_title' ) { | |
| 563 | - $new_action->post_content['post_title'] = $value; | |
| 564 | - } elseif ( is_string( $value ) ) { | |
| 565 | - $new_action->post_content[ $key ] = $this->replace_smart_tags( $value, $form['fields'] ); | |
| 566 | - } else { | |
| 567 | - $new_action->post_content[ $key ] = $value; | |
| 568 | - } | |
| 569 | - } | |
| 570 | - | |
| 571 | - return $action_control->save_settings( $new_action ); | |
| 572 | - } | |
| 573 | - | |
| 574 | - /** | |
| 575 | 266 | * After a form has been successfully imported we track it, so that in the |
| 576 | 267 | * future we can alert users if they try to import a form that has already |
| 577 | 268 | * been imported. |
| 578 | 269 | * |
| 579 | - * @param int $source_id Imported plugin form ID. | |
| 580 | - * @param int $new_form_id Formidable form ID. | |
| 581 | - * | |
| 582 | - * @return void | |
| 270 | + * @param int $source_id Imported plugin form ID | |
| 271 | + * @param int $new_form_id Formidable form ID | |
| 583 | 272 | */ |
| 584 | - protected function track_import( $source_id, $new_form_id ) { | |
| 273 | + private function track_import( $source_id, $new_form_id ) { | |
| 274 | + | |
| 585 | 275 | $imported = $this->get_tracked_import(); |
| 586 | 276 | |
| 587 | 277 | $imported[ $this->slug ][ $new_form_id ] = $source_id; |
| 588 | 278 | |
| @@ -596,29 +286,18 @@ | ||
| 596 | 286 | return get_option( $this->tracking, array() ); |
| 597 | 287 | } |
| 598 | 288 | |
| 599 | 289 | /** |
| 600 | - * @param int $source_id Imported plugin form ID. | |
| 290 | + * @param int $source_id Imported plugin form ID | |
| 601 | 291 | * |
| 602 | 292 | * @return int the ID of the created form or 0 |
| 603 | 293 | */ |
| 604 | 294 | private function is_imported( $source_id ) { |
| 605 | - $imported = $this->get_tracked_import(); | |
| 295 | + $imported = $this->get_tracked_import(); | |
| 606 | 296 | $new_form_id = 0; |
| 607 | - | |
| 608 | - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict | |
| 609 | - if ( ! isset( $imported[ $this->slug ] ) || ! in_array( $source_id, $imported[ $this->slug ] ) ) { | |
| 610 | - return $new_form_id; | |
| 297 | + if ( isset( $imported[ $this->slug ] ) && in_array( $source_id, $imported[ $this->slug ] ) ) { | |
| 298 | + $new_form_id = array_search( $source_id, array_reverse( $imported[ $this->slug ], true ) ); | |
| 611 | 299 | } |
| 612 | - | |
| 613 | - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict | |
| 614 | - $new_form_id = array_search( $source_id, array_reverse( $imported[ $this->slug ], true ) ); | |
| 615 | - | |
| 616 | - if ( $new_form_id && ! FrmForm::get_key_by_id( $new_form_id ) ) { | |
| 617 | - // Allow reimport if the form was deleted. | |
| 618 | - return 0; | |
| 619 | - } | |
| 620 | - | |
| 621 | 300 | return $new_form_id; |
| 622 | 301 | } |
| 623 | 302 | |
| 624 | 303 | /** Start functions here that should be overridden **/ |
| @@ -629,15 +308,11 @@ | ||
| 629 | 308 | protected function unsupported_field_types() { |
| 630 | 309 | return array(); |
| 631 | 310 | } |
| 632 | 311 | |
| 633 | - /** | |
| 634 | - * @param string $type | |
| 635 | - * | |
| 636 | - * @return bool | |
| 637 | - */ | |
| 638 | 312 | private function is_unsupported_field( $type ) { |
| 639 | - return in_array( $type, $this->unsupported_field_types(), true ); | |
| 313 | + $fields = $this->unsupported_field_types(); | |
| 314 | + return in_array( $type, $fields, true ); | |
| 640 | 315 | } |
| 641 | 316 | |
| 642 | 317 | /** |
| 643 | 318 | * Strict PRO fields with no Lite alternatives. |
| @@ -647,19 +322,11 @@ | ||
| 647 | 322 | protected function skip_pro_fields() { |
| 648 | 323 | return array(); |
| 649 | 324 | } |
| 650 | 325 | |
| 651 | - /** | |
| 652 | - * @param string $type | |
| 653 | - * | |
| 654 | - * @return bool | |
| 655 | - */ | |
| 656 | - protected function should_skip_field( $type ) { | |
| 657 | - if ( FrmAppHelper::pro_is_installed() ) { | |
| 658 | - return false; | |
| 659 | - } | |
| 660 | - | |
| 661 | - return in_array( $type, $this->skip_pro_fields(), true ); | |
| 326 | + private function should_skip_field( $type ) { | |
| 327 | + $skip_pro_fields = $this->skip_pro_fields(); | |
| 328 | + return ( ! FrmAppHelper::pro_is_installed() && in_array( $type, $skip_pro_fields, true ) ); | |
| 662 | 329 | } |
| 663 | 330 | |
| 664 | 331 | /** |
| 665 | 332 | * Replace 3rd-party form provider tags/shortcodes with our own Tags. |
| @@ -681,20 +348,14 @@ | ||
| 681 | 348 | public function get_forms() { |
| 682 | 349 | return array(); |
| 683 | 350 | } |
| 684 | 351 | |
| 685 | - /** | |
| 686 | - * @param int|string $id | |
| 687 | - * | |
| 688 | - * @return array | |
| 689 | - */ | |
| 690 | 352 | public function get_form( $id ) { |
| 691 | 353 | return array(); |
| 692 | 354 | } |
| 693 | 355 | |
| 694 | 356 | /** |
| 695 | - * @param array|object $source_form | |
| 696 | - * | |
| 357 | + * @param object|array $source_form | |
| 697 | 358 | * @return string |
| 698 | 359 | */ |
| 699 | 360 | protected function get_form_name( $source_form ) { |
| 700 | 361 | return __( 'Default Form', 'formidable' ); |
| @@ -700,10 +361,9 @@ | ||
| 700 | 361 | return __( 'Default Form', 'formidable' ); |
| 701 | 362 | } |
| 702 | 363 | |
| 703 | 364 | /** |
| 704 | - * @param array|object $source_form | |
| 705 | - * | |
| 365 | + * @param object|array|int $source_form | |
| 706 | 366 | * @return array |
| 707 | 367 | */ |
| 708 | 368 | protected function get_form_fields( $source_form ) { |
| 709 | 369 | return array(); |
| @@ -709,29 +369,22 @@ | ||
| 709 | 369 | return array(); |
| 710 | 370 | } |
| 711 | 371 | |
| 712 | 372 | /** |
| 713 | - * @param array $field | |
| 714 | - * | |
| 373 | + * @param object|array $field | |
| 715 | 374 | * @return string |
| 716 | 375 | */ |
| 717 | 376 | protected function get_field_type( $field ) { |
| 718 | - return $field['type']; | |
| 377 | + return is_array( $field ) ? $field['type'] : $field->type; | |
| 719 | 378 | } |
| 720 | 379 | |
| 721 | 380 | /** |
| 722 | - * @param array $field | |
| 381 | + * @param object|array $field | |
| 723 | 382 | * |
| 724 | 383 | * @return string |
| 725 | 384 | */ |
| 726 | 385 | protected function get_field_label( $field ) { |
| 727 | - $label = $field['label'] ?? ''; | |
| 728 | - | |
| 729 | - if ( $label ) { | |
| 730 | - return $label; | |
| 731 | - } | |
| 732 | - | |
| 733 | - $type = $this->get_field_type( $field ); | |
| 386 | + $type = $this->get_field_type( $field ); | |
| 734 | 387 | $label = sprintf( |
| 735 | 388 | /* translators: %1$s - field type */ |
| 736 | 389 | esc_html__( '%1$s Field', 'formidable' ), |
| 737 | 390 | ucfirst( $type ) |