PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 3.06.06
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v3.06.06
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / models / FrmFormMigrator.php

FrmFormMigrator.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 3.06.06, at classes/models/FrmFormMigrator.php

396 lines 10.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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" action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>">
53 <?php wp_nonce_field( 'nonce', 'frm_ajax' ); ?>
54 <input type="hidden" name="slug" value="<?php echo esc_attr( $this->slug ); ?>" />
55 <input type="hidden" name="action" value="frm_import_<?php echo esc_attr( $this->slug ); ?>" />
56 <div style="margin:10px auto;max-width:400px;text-align:left;">
57 <?php foreach ( $this->get_forms() as $form_id => $name ) { ?>
58 <p>
59 <label>
60 <input type="checkbox" name="form_id[]" value="<?php echo esc_attr( $form_id ); ?>" checked="checked" />
61 <?php
62 echo esc_html( $name );
63 $new_form_id = $this->is_imported( $form_id );
64 ?>
65 <?php if ( $new_form_id ) { ?>
66 (<a href="<?php echo esc_url( admin_url( 'admin.php?page=formidable&frm_action=edit&id=' . $new_form_id ) ); ?>">previously imported</a>)
67 <?php } ?>
68 </label>
69 </p>
70 <?php } ?>
71 </div>
72 <button type="submit" class="button button-primary button-hero">Start Import</button>
73 </form>
74 <div id="frm-importer-process" class="frm_hidden">
75
76 <p class="process-count">
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 ); ?>.
79 </p>
80
81 <p class="process-completed" class="frm_hidden">
82 The import process has finished! We have successfully imported <span class="forms-completed"></span> forms. You can review the results below.
83 </p>
84
85 <div class="status"></div>
86
87 </div>
88 </div>
89 </div>
90 </div>
91 <?php
92 }
93
94 /**
95 * Import all forms using ajax
96 */
97 public function import_forms() {
98
99 check_ajax_referer( 'frm_ajax', 'nonce' );
100 FrmAppHelper::permission_check( 'frm_edit_forms' );
101
102 $forms = FrmAppHelper::get_simple_request(
103 array(
104 'param' => 'form_id',
105 'type' => 'post',
106 'sanitize' => 'absint',
107 )
108 );
109
110 if ( is_array( $forms ) ) {
111 $imported = array();
112 foreach ( (array) $forms as $form_id ) {
113 $imported[] = $this->import_form( $form_id );
114 }
115 } else {
116 $imported = $this->import_form( $forms );
117 }
118
119 wp_send_json_success( $imported );
120 }
121
122 /**
123 * Import a single form
124 */
125 protected function import_form( $source_id ) {
126
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 );
130
131 // If form does not contain fields, bail.
132 if ( empty( $source_fields ) ) {
133 wp_send_json_success(
134 array(
135 'error' => true,
136 'name' => esc_html( $source_form_name ),
137 'msg' => __( 'No form fields found.', 'formidable' ),
138 )
139 );
140 }
141
142 $form = $this->prepare_new_form( $source_id, $source_form_name );
143
144 $this->prepare_fields( $source_fields, $form );
145
146 $this->prepare_form( $source_form, $form );
147
148 return $this->add_form( $form );
149 }
150
151 protected function prepare_new_form( $source_id, $source_form_name ) {
152 return array(
153 'import_form_id' => $source_id,
154 'fields' => array(),
155 'name' => $source_form_name,
156 'description' => '',
157 'options' => array(),
158 'actions' => array(),
159 );
160 }
161
162 protected function prepare_form( $form, &$new_form ) {
163 // customize this function
164 }
165
166 protected function prepare_fields( $fields, &$form ) {
167 $field_order = 1;
168 foreach ( $fields as $field ) {
169
170 $label = $this->get_field_label( $field );
171 $type = $this->get_field_type( $field );
172
173 // check if field is unsupported. If unsupported make note and continue
174 if ( $this->is_unsupported_field( $type ) ) {
175 $this->response['unsupported'][] = $label;
176 continue;
177 }
178
179 if ( $this->should_skip_field( $type ) ) {
180 $this->response['upgrade_omit'][] = $label;
181 continue;
182 }
183
184 $new_field = FrmFieldsHelper::setup_new_vars( $this->convert_field_type( $type ) );
185 $new_field['name'] = $label;
186 $new_field['field_order'] = $field_order;
187 $new_field['original'] = $type;
188
189 $this->prepare_field( $field, $new_field );
190 $form['fields'][] = $new_field;
191
192 $field_order++;
193 }
194 }
195
196 protected function prepare_field( $field, &$new_field ) {
197 // customize this function
198 }
199
200 protected function convert_field_type( $type ) {
201 return $type;
202 }
203
204 /**
205 * Add the new form to the database and return AJAX data.
206 *
207 * @since 1.4.2
208 *
209 * @param array $form Form to import.
210 * @param array $upgrade_omit No field alternative
211 */
212 private function add_form( $form, $upgrade_omit = array() ) {
213
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 );
224
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 );
231 }
232
233 foreach ( $form['fields'] as $key => $new_field ) {
234 $new_field['form_id'] = $form_id;
235 $form['fields'][ $key ]['id'] = FrmField::create( $new_field );
236 }
237
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 }
250
251 $action_control->save_settings( $new_action );
252 }
253
254 $this->track_import( $form['import_form_id'], $form_id );
255
256 // Build and send final AJAX response!
257 return array(
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'],
262 );
263 }
264
265 /**
266 * After a form has been successfully imported we track it, so that in the
267 * future we can alert users if they try to import a form that has already
268 * been imported.
269 *
270 * @param int $source_id Imported plugin form ID
271 * @param int $new_form_id Formidable form ID
272 */
273 private function track_import( $source_id, $new_form_id ) {
274
275 $imported = $this->get_tracked_import();
276
277 $imported[ $this->slug ][ $new_form_id ] = $source_id;
278
279 update_option( $this->tracking, $imported, false );
280 }
281
282 /**
283 * @return array
284 */
285 private function get_tracked_import() {
286 return get_option( $this->tracking, array() );
287 }
288
289 /**
290 * @param int $source_id Imported plugin form ID
291 *
292 * @return int the ID of the created form or 0
293 */
294 private function is_imported( $source_id ) {
295 $imported = $this->get_tracked_import();
296 $new_form_id = 0;
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 ) );
299 }
300 return $new_form_id;
301 }
302
303 /** Start functions here that should be overridden **/
304
305 /**
306 * @return array
307 */
308 protected function unsupported_field_types() {
309 return array();
310 }
311
312 private function is_unsupported_field( $type ) {
313 $fields = $this->unsupported_field_types();
314 return in_array( $type, $fields, true );
315 }
316
317 /**
318 * Strict PRO fields with no Lite alternatives.
319 *
320 * @return array
321 */
322 protected function skip_pro_fields() {
323 return array();
324 }
325
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 ) );
329 }
330
331 /**
332 * Replace 3rd-party form provider tags/shortcodes with our own Tags.
333 *
334 * @param string $string String to process the smart tag in.
335 * @param array $fields List of fields for the form.
336 *
337 * @return string
338 */
339 protected function replace_smart_tags( $string, $fields ) {
340 return $string;
341 }
342
343 /**
344 * Get ALL THE FORMS.
345 *
346 * @return array
347 */
348 public function get_forms() {
349 return array();
350 }
351
352 public function get_form( $id ) {
353 return array();
354 }
355
356 /**
357 * @param object|array $source_form
358 * @return string
359 */
360 protected function get_form_name( $source_form ) {
361 return __( 'Default Form', 'formidable' );
362 }
363
364 /**
365 * @param object|array|int $source_form
366 * @return array
367 */
368 protected function get_form_fields( $source_form ) {
369 return array();
370 }
371
372 /**
373 * @param object|array $field
374 * @return string
375 */
376 protected function get_field_type( $field ) {
377 return is_array( $field ) ? $field['type'] : $field->type;
378 }
379
380 /**
381 * @param object|array $field
382 *
383 * @return string
384 */
385 protected function get_field_label( $field ) {
386 $type = $this->get_field_type( $field );
387 $label = sprintf(
388 /* translators: %1$s - field type */
389 esc_html__( '%1$s Field', 'formidable' ),
390 ucfirst( $type )
391 );
392
393 return trim( $label );
394 }
395 }
396