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 / FrmMigrate.php

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

537 lines 16.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class FrmMigrate {
4 public $fields;
5 public $forms;
6 public $entries;
7 public $entry_metas;
8
9 public function __construct() {
10 if ( ! defined( 'ABSPATH' ) ) {
11 die( 'You are not allowed to call this page directly.' );
12 }
13
14 global $wpdb;
15 $this->fields = $wpdb->prefix . 'frm_fields';
16 $this->forms = $wpdb->prefix . 'frm_forms';
17 $this->entries = $wpdb->prefix . 'frm_items';
18 $this->entry_metas = $wpdb->prefix . 'frm_item_metas';
19 }
20
21 public function upgrade() {
22 do_action( 'frm_before_install' );
23
24 global $wpdb, $frm_vars;
25
26 $frm_vars['doing_upgrade'] = true;
27
28 $needs_upgrade = FrmAppController::compare_for_update(
29 array(
30 'option' => 'frm_db_version',
31 'new_db_version' => FrmAppHelper::$db_version,
32 'new_plugin_version' => FrmAppHelper::plugin_version(),
33 )
34 );
35
36 if ( $needs_upgrade ) {
37 // update rewrite rules for views and other custom post types
38 flush_rewrite_rules();
39
40 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
41
42 $old_db_version = get_option( 'frm_db_version' );
43
44 $this->create_tables();
45 $this->migrate_data( $old_db_version );
46
47 /***** SAVE DB VERSION *****/
48 update_option( 'frm_db_version', FrmAppHelper::plugin_version() . '-' . FrmAppHelper::$db_version );
49
50 if ( ! $old_db_version ) {
51 $this->maybe_create_contact_form();
52 }
53 }
54
55 do_action( 'frm_after_install' );
56
57 $frm_vars['doing_upgrade'] = false;
58
59 FrmAppHelper::save_combined_js();
60
61 // update the styling settings
62 if ( function_exists( 'get_filesystem_method' ) ) {
63 $frm_style = new FrmStyle();
64 $frm_style->update( 'default' );
65 }
66 }
67
68 public function collation() {
69 global $wpdb;
70 if ( ! $wpdb->has_cap( 'collation' ) ) {
71 return '';
72 }
73
74 return $wpdb->get_charset_collate();
75 }
76
77 private function create_tables() {
78 $charset_collate = $this->collation();
79 $sql = array();
80
81 /* Create/Upgrade Fields Table */
82 $sql[] = 'CREATE TABLE ' . $this->fields . ' (
83 id BIGINT(20) NOT NULL auto_increment,
84 field_key varchar(100) default NULL,
85 name text default NULL,
86 description longtext default NULL,
87 type text default NULL,
88 default_value longtext default NULL,
89 options longtext default NULL,
90 field_order int(11) default 0,
91 required int(1) default NULL,
92 field_options longtext default NULL,
93 form_id int(11) default NULL,
94 created_at datetime NOT NULL,
95 PRIMARY KEY (id),
96 KEY form_id (form_id),
97 UNIQUE KEY field_key (field_key)
98 )';
99
100 /* Create/Upgrade Forms Table */
101 $sql[] = 'CREATE TABLE ' . $this->forms . ' (
102 id int(11) NOT NULL auto_increment,
103 form_key varchar(100) default NULL,
104 name varchar(255) default NULL,
105 description text default NULL,
106 parent_form_id int(11) default 0,
107 logged_in tinyint(1) default NULL,
108 editable tinyint(1) default NULL,
109 is_template tinyint(1) default 0,
110 default_template tinyint(1) default 0,
111 status varchar(255) default NULL,
112 options longtext default NULL,
113 created_at datetime NOT NULL,
114 PRIMARY KEY (id),
115 UNIQUE KEY form_key (form_key)
116 )';
117
118 /* Create/Upgrade Items Table */
119 $sql[] = 'CREATE TABLE ' . $this->entries . ' (
120 id BIGINT(20) NOT NULL auto_increment,
121 item_key varchar(100) default NULL,
122 name varchar(255) default NULL,
123 description text default NULL,
124 ip text default NULL,
125 form_id BIGINT(20) default NULL,
126 post_id BIGINT(20) default NULL,
127 user_id BIGINT(20) default NULL,
128 parent_item_id BIGINT(20) default 0,
129 is_draft tinyint(1) default 0,
130 updated_by BIGINT(20) default NULL,
131 created_at datetime NOT NULL,
132 updated_at datetime NOT NULL,
133 PRIMARY KEY (id),
134 KEY form_id (form_id),
135 KEY post_id (post_id),
136 KEY user_id (user_id),
137 KEY parent_item_id (parent_item_id),
138 UNIQUE KEY item_key (item_key)
139 )';
140
141 /* Create/Upgrade Meta Table */
142 $sql[] = 'CREATE TABLE ' . $this->entry_metas . ' (
143 id BIGINT(20) NOT NULL auto_increment,
144 meta_value longtext default NULL,
145 field_id BIGINT(20) NOT NULL,
146 item_id BIGINT(20) NOT NULL,
147 created_at datetime NOT NULL,
148 PRIMARY KEY (id),
149 KEY field_id (field_id),
150 KEY item_id (item_id)
151 )';
152
153 foreach ( $sql as $q ) {
154 if ( function_exists( 'dbDelta' ) ) {
155 dbDelta( $q . $charset_collate . ';' );
156 } else {
157 global $wpdb;
158 $wpdb->query( $q . $charset_collate ); // WPCS: unprepared SQL ok.
159 }
160 unset( $q );
161 }
162 }
163
164 private function maybe_create_contact_form() {
165 $form_exists = FrmForm::get_id_by_key( 'contact-form' );
166 if ( ! $form_exists ) {
167 $this->add_default_template();
168 }
169 }
170
171 /**
172 * Create the default contact form
173 *
174 * @since 3.06
175 */
176 private function add_default_template() {
177 if ( ! function_exists( 'libxml_disable_entity_loader' ) ) {
178 // XML import is not enabled on your server.
179 return;
180 }
181
182 $set_err = libxml_use_internal_errors( true );
183 $loader = libxml_disable_entity_loader( true );
184
185 $file = FrmAppHelper::plugin_path() . '/classes/views/xml/default-templates.xml';
186 FrmXMLHelper::import_xml( $file );
187
188 libxml_use_internal_errors( $set_err );
189 libxml_disable_entity_loader( $loader );
190 }
191
192 /**
193 * @param int|string $old_db_version
194 */
195 private function migrate_data( $old_db_version ) {
196 if ( ! $old_db_version ) {
197 $old_db_version = get_option( 'frm_db_version' );
198 }
199 if ( strpos( $old_db_version, '-' ) ) {
200 $last_upgrade = explode( '-', $old_db_version );
201 $old_db_version = (int) $last_upgrade[1];
202 }
203
204 if ( ! is_numeric( $old_db_version ) ) {
205 // bail if we don't know the previous version
206 return;
207 }
208
209 $migrations = array( 16, 11, 16, 17, 23, 25, 86, 90 );
210 foreach ( $migrations as $migration ) {
211 if ( FrmAppHelper::$db_version >= $migration && $old_db_version < $migration ) {
212 $function_name = 'migrate_to_' . $migration;
213 $this->$function_name();
214 }
215 }
216 }
217
218 public function uninstall() {
219 if ( ! current_user_can( 'administrator' ) ) {
220 $frm_settings = FrmAppHelper::get_settings();
221 wp_die( esc_html( $frm_settings->admin_permission ) );
222 }
223
224 global $wpdb, $wp_roles;
225
226 $wpdb->query( 'DROP TABLE IF EXISTS ' . $this->fields ); // WPCS: unprepared SQL ok.
227 $wpdb->query( 'DROP TABLE IF EXISTS ' . $this->forms ); // WPCS: unprepared SQL ok.
228 $wpdb->query( 'DROP TABLE IF EXISTS ' . $this->entries ); // WPCS: unprepared SQL ok.
229 $wpdb->query( 'DROP TABLE IF EXISTS ' . $this->entry_metas ); // WPCS: unprepared SQL ok.
230
231 delete_option( 'frm_options' );
232 delete_option( 'frm_db_version' );
233 delete_option( 'frm_install_running' );
234 delete_option( 'frm_lite_settings_upgrade' );
235 delete_option( 'frm-usage-uuid' );
236
237 //delete roles
238 $frm_roles = FrmAppHelper::frm_capabilities();
239 $roles = get_editable_roles();
240 foreach ( $frm_roles as $frm_role => $frm_role_description ) {
241 foreach ( $roles as $role => $details ) {
242 $wp_roles->remove_cap( $role, $frm_role );
243 unset( $role, $details );
244 }
245 unset( $frm_role, $frm_role_description );
246 }
247 unset( $roles, $frm_roles );
248
249 // delete actions, views, and styles
250
251 // prevent the post deletion from triggering entries to be deleted
252 remove_action( 'before_delete_post', 'FrmProDisplaysController::before_delete_post' );
253 remove_action( 'deleted_post', 'FrmProEntriesController::delete_entry' );
254
255 $post_ids = $wpdb->get_col( $wpdb->prepare( 'SELECT ID FROM ' . $wpdb->posts . ' WHERE post_type in (%s, %s, %s)', FrmFormActionsController::$action_post_type, FrmStylesController::$post_type, 'frm_display' ) );
256 foreach ( $post_ids as $post_id ) {
257 // Delete's each post.
258 wp_delete_post( $post_id, true );
259 }
260 unset( $post_ids );
261
262 // delete transients
263 delete_transient( 'frmpro_css' );
264 delete_transient( 'frm_options' );
265 delete_transient( 'frmpro_options' );
266
267 $wpdb->query( $wpdb->prepare( 'DELETE FROM ' . $wpdb->options . ' WHERE option_name LIKE %s OR option_name LIKE %s', '_transient_timeout_frm_form_fields_%', '_transient_frm_form_fields_%' ) );
268
269 do_action( 'frm_after_uninstall' );
270 return true;
271 }
272
273 /**
274 * Delete uneeded default templates
275 *
276 * @since 3.06
277 */
278 private function migrate_to_90() {
279 $form = FrmForm::getOne( 'contact' );
280 if ( $form && $form->default_template == 1 ) {
281 FrmForm::destroy( 'contact' );
282 }
283 }
284
285 /**
286 * Reverse migration 17 -- Divide by 9
287 *
288 * @since 3.0.05
289 */
290 private function migrate_to_86() {
291
292 $fields = $this->get_fields_with_size();
293
294 foreach ( (array) $fields as $f ) {
295 $f->field_options = maybe_unserialize( $f->field_options );
296 $size = $f->field_options['size'];
297 $this->maybe_convert_migrated_size( $size );
298
299 if ( $size === $f->field_options['size'] ) {
300 continue;
301 }
302
303 $f->field_options['size'] = $size;
304 FrmField::update( $f->id, array( 'field_options' => $f->field_options ) );
305 unset( $f );
306 }
307
308 // reverse the extra size changes in widgets
309 $widgets = get_option( 'widget_frm_show_form' );
310 if ( empty( $widgets ) ) {
311 return;
312 }
313
314 $this->revert_widget_field_size();
315 }
316
317 private function get_fields_with_size() {
318 $field_types = array( 'textarea', 'text', 'number', 'email', 'url', 'rte', 'date', 'phone', 'password', 'image', 'tag', 'file' );
319 $query = array(
320 'type' => $field_types,
321 'field_options like' => 's:4:"size";',
322 'field_options not like' => 's:4:"size";s:0:',
323 );
324
325 return FrmDb::get_results( $this->fields, $query, 'id, field_options' );
326 }
327
328 /**
329 * Reverse the extra size changes in widgets
330 *
331 * @since 3.0.05
332 */
333 private function revert_widget_field_size() {
334 $widgets = get_option( 'widget_frm_show_form' );
335 if ( empty( $widgets ) ) {
336 return;
337 }
338
339 $widgets = maybe_unserialize( $widgets );
340 foreach ( $widgets as $k => $widget ) {
341 if ( ! is_array( $widget ) || ! isset( $widget['size'] ) ) {
342 continue;
343 }
344
345 $this->maybe_convert_migrated_size( $widgets[ $k ]['size'] );
346 }
347 update_option( 'widget_frm_show_form', $widgets );
348 }
349
350 /**
351 * Divide by 9 to reverse the multiplication
352 *
353 * @since 3.0.05
354 */
355 private function maybe_convert_migrated_size( &$size ) {
356 $has_px_size = ! empty( $size ) && strpos( $size, 'px' );
357 if ( ! $has_px_size ) {
358 return;
359 }
360
361 $int_size = str_replace( 'px', '', $size );
362 if ( ! is_numeric( $int_size ) || (int) $int_size < 900 ) {
363 return;
364 }
365
366 $pixel_conversion = 9;
367 $size = round( (int) $int_size / $pixel_conversion );
368 }
369
370 /**
371 * Migrate old styling settings. If sites are using the old
372 * default 400px field width, switch it to 100%
373 *
374 * @since 2.0.4
375 */
376 private function migrate_to_25() {
377 // get the style that was created with the style migration
378 $frm_style = new FrmStyle();
379 $styles = $frm_style->get_all( 'post_date', 'ASC', 1 );
380 if ( empty( $styles ) ) {
381 return;
382 }
383
384 foreach ( $styles as $style ) {
385 if ( $style->post_content['field_width'] == '400px' ) {
386 $style->post_content['field_width'] = '100%';
387 $frm_style->save( (array) $style );
388 return;
389 }
390 }
391 }
392
393 /**
394 * Check if the parent_form_id columns exists.
395 * If not, try and add it again
396 *
397 * @since 2.0.2
398 */
399 private function migrate_to_23() {
400 global $wpdb;
401 $exists = $wpdb->get_row( 'SHOW COLUMNS FROM ' . $this->forms . ' LIKE "parent_form_id"' ); // WPCS: unprepared SQL ok.
402 if ( empty( $exists ) ) {
403 $wpdb->query( 'ALTER TABLE ' . $this->forms . ' ADD parent_form_id int(11) default 0' ); // WPCS: unprepared SQL ok.
404 }
405 }
406
407 /**
408 * Change field size from character to pixel -- Multiply by 9
409 */
410 private function migrate_to_17() {
411 $fields = $this->get_fields_with_size();
412
413 foreach ( $fields as $f ) {
414 $f->field_options = maybe_unserialize( $f->field_options );
415 if ( empty( $f->field_options['size'] ) || ! is_numeric( $f->field_options['size'] ) ) {
416 continue;
417 }
418
419 $this->convert_character_to_px( $f->field_options['size'] );
420
421 FrmField::update( $f->id, array( 'field_options' => $f->field_options ) );
422 unset( $f );
423 }
424
425 $this->adjust_widget_size();
426 }
427
428 /**
429 * Change the characters in widgets to pixels
430 */
431 private function adjust_widget_size() {
432 $widgets = get_option( 'widget_frm_show_form' );
433 if ( empty( $widgets ) ) {
434 return;
435 }
436
437 $widgets = maybe_unserialize( $widgets );
438 foreach ( $widgets as $k => $widget ) {
439 if ( ! is_array( $widget ) || ! isset( $widget['size'] ) ) {
440 continue;
441 }
442 $this->convert_character_to_px( $widgets[ $k ]['size'] );
443 }
444 update_option( 'widget_frm_show_form', $widgets );
445 }
446
447 private function convert_character_to_px( &$size ) {
448 $pixel_conversion = 9;
449 $size = round( $pixel_conversion * (int) $size );
450 $size .= 'px';
451 }
452
453 /**
454 * Migrate post and email notification settings into actions
455 */
456 private function migrate_to_16() {
457 $forms = FrmDb::get_results( $this->forms, array(), 'id, options, is_template, default_template' );
458
459 /**
460 * Old email settings format:
461 * email_to: Email or field id
462 * also_email_to: array of fields ids
463 * reply_to: Email, field id, 'custom'
464 * cust_reply_to: string
465 * reply_to_name: field id, 'custom'
466 * cust_reply_to_name: string
467 * plain_text: 0|1
468 * email_message: string or ''
469 * email_subject: string or ''
470 * inc_user_info: 0|1
471 * update_email: 0, 1, 2
472 *
473 * Old autoresponder settings format:
474 * auto_responder: 0|1
475 * ar_email_message: string or ''
476 * ar_email_to: field id
477 * ar_plain_text: 0|1
478 * ar_reply_to_name: string
479 * ar_reply_to: string
480 * ar_email_subject: string
481 * ar_update_email: 0, 1, 2
482 *
483 * New email settings:
484 * post_content: json settings
485 * post_title: form id
486 * post_excerpt: message
487 */
488 foreach ( $forms as $form ) {
489 if ( $form->is_template && $form->default_template ) {
490 // don't migrate the default templates since the email will be added anyway
491 continue;
492 }
493
494 // Format form options
495 $form_options = maybe_unserialize( $form->options );
496
497 // Migrate settings to actions
498 FrmXMLHelper::migrate_form_settings_to_actions( $form_options, $form->id );
499 }
500 }
501
502 private function migrate_to_11() {
503 global $wpdb;
504
505 $forms = FrmDb::get_results( $this->forms, array(), 'id, options' );
506
507 $sending = __( 'Sending', 'formidable' );
508 $img = FrmAppHelper::plugin_url() . '/images/ajax_loader.gif';
509 $old_default_html = <<<DEFAULT_HTML
510 <div class="frm_submit">
511 [if back_button]<input type="submit" value="[back_label]" name="frm_prev_page" formnovalidate="formnovalidate" [back_hook] />[/if back_button]
512 <input type="submit" value="[button_label]" [button_action] />
513 <img class="frm_ajax_loading" src="$img" alt="$sending" style="visibility:hidden;" />
514 </div>
515 DEFAULT_HTML;
516 unset( $sending, $img );
517
518 $new_default_html = FrmFormsHelper::get_default_html( 'submit' );
519 $draft_link = FrmFormsHelper::get_draft_link();
520 foreach ( $forms as $form ) {
521 $form->options = maybe_unserialize( $form->options );
522 if ( ! isset( $form->options['submit_html'] ) || empty( $form->options['submit_html'] ) ) {
523 continue;
524 }
525
526 if ( $form->options['submit_html'] != $new_default_html && $form->options['submit_html'] == $old_default_html ) {
527 $form->options['submit_html'] = $new_default_html;
528 $wpdb->update( $this->forms, array( 'options' => serialize( $form->options ) ), array( 'id' => $form->id ) );
529 } else if ( ! strpos( $form->options['submit_html'], 'save_draft' ) ) {
530 $form->options['submit_html'] = preg_replace( '~\<\/div\>(?!.*\<\/div\>)~', $draft_link . "\r\n</div>", $form->options['submit_html'] );
531 $wpdb->update( $this->forms, array( 'options' => serialize( $form->options ) ), array( 'id' => $form->id ) );
532 }
533 unset( $form );
534 }
535 }
536 }
537