PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / trunk
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More vtrunk
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
← All changes | classes/models/FrmMigrate.php +367 -54 6.3.1 → trunk View file →
@@ -3,21 +3,46 @@
3 3 die( 'You are not allowed to call this page directly.' );
4 4 }
5 5
6 6 class FrmMigrate {
7 +
8 + /**
9 + * @var string
10 + */
7 11 public $fields;
12 +
13 + /**
14 + * @var string
15 + */
8 16 public $forms;
17 +
18 + /**
19 + * @var string
20 + */
9 21 public $entries;
22 +
23 + /**
24 + * @var string
25 + */
10 26 public $entry_metas;
11 27
28 + /**
29 + * @var string
30 + */
31 + public $gated_tokens;
32 +
12 33 public function __construct() {
13 34 global $wpdb;
14 - $this->fields = $wpdb->prefix . 'frm_fields';
15 - $this->forms = $wpdb->prefix . 'frm_forms';
16 - $this->entries = $wpdb->prefix . 'frm_items';
17 - $this->entry_metas = $wpdb->prefix . 'frm_item_metas';
35 + $this->fields = $wpdb->prefix . 'frm_fields';
36 + $this->forms = $wpdb->prefix . 'frm_forms';
37 + $this->entries = $wpdb->prefix . 'frm_items';
38 + $this->entry_metas = $wpdb->prefix . 'frm_item_metas';
39 + $this->gated_tokens = $wpdb->prefix . 'frm_gated_tokens';
18 40 }
19 41
42 + /**
43 + * @return void
44 + */
20 45 public function upgrade() {
21 46 do_action( 'frm_before_install' );
22 47
23 48 global $wpdb, $frm_vars;
@@ -32,25 +57,34 @@
32 57 )
33 58 );
34 59
35 60 if ( $needs_upgrade ) {
36 - // update rewrite rules for views and other custom post types
61 + $this->maybe_delete_htaccess_file();
62 +
63 + // Update rewrite rules for views and other custom post types
37 64 flush_rewrite_rules();
38 65
39 - require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
66 + require_once ABSPATH . 'wp-admin/includes/upgrade.php';
40 67
41 68 $old_db_version = get_option( 'frm_db_version' );
42 69
43 70 $this->create_tables();
44 71 $this->migrate_data( $old_db_version );
72 + $this->check_that_tables_exist();
45 73
46 - /***** SAVE DB VERSION *****/
74 + // SAVE DB VERSION.
47 75 update_option( 'frm_db_version', FrmAppHelper::plugin_version() . '-' . FrmAppHelper::$db_version );
48 76
49 77 if ( ! $old_db_version ) {
50 78 $this->maybe_create_contact_form();
79 +
80 + if ( false === get_option( 'frm_first_activation' ) ) {
81 + update_option( 'frm_first_activation', time(), false );
82 + }
83 +
84 + $this->update_settings_for_new_install();
51 85 }
52 - }
86 + }//end if
53 87
54 88 do_action( 'frm_after_install' );
55 89
56 90 $frm_vars['doing_upgrade'] = false;
@@ -56,24 +90,97 @@
56 90 $frm_vars['doing_upgrade'] = false;
57 91
58 92 FrmAppHelper::save_combined_js();
59 93
60 - // update the styling settings
61 - if ( function_exists( 'get_filesystem_method' ) ) {
62 - $frm_style = new FrmStyle();
63 - $frm_style->update( 'default' );
94 + // Update the styling settings
95 + if ( ! function_exists( 'get_filesystem_method' ) ) {
96 + return;
64 97 }
98 +
99 + $frm_style = new FrmStyle();
100 + $frm_style->update( 'default' );
65 101 }
66 102
67 - public function collation() {
103 + /**
104 + * Check if the .htaccess file should be deleted based on server response.
105 + * If a server has AllowOverride FileInfo but not AllowOverride AuthConfig, JS and CSS files
106 + * will result in a 500 error.
107 + *
108 + * @since 6.27
109 + *
110 + * @return void
111 + */
112 + private function maybe_delete_htaccess_file() {
113 + $css_file_request = wp_remote_get( FrmAppHelper::plugin_url() . '/css/frm_fonts.css' );
114 +
115 + if ( 200 === wp_remote_retrieve_response_code( $css_file_request ) ) {
116 + return;
117 + }
118 +
119 + $htaccess_path = FrmAppHelper::plugin_path() . '/.htaccess';
120 +
121 + if ( file_exists( $htaccess_path ) ) {
122 + wp_delete_file( $htaccess_path );
123 + }
124 + }
125 +
126 + /**
127 + * Updates some settings for new installs.
128 + *
129 + * @since 6.23
130 + *
131 + * @return void
132 + */
133 + private function update_settings_for_new_install() {
134 + $settings = FrmAppHelper::get_settings();
135 +
136 + $settings->denylist_check = 1;
137 + $settings->installed_after_welcome_tour_update = 1;
138 + $settings->store();
139 + }
140 +
141 + /**
142 + * If we fail to create the database tables, add an inbox notice.
143 + * This informs the user that they need to correct the issue and try again.
144 + *
145 + * @since 6.19
146 + *
147 + * @return void
148 + */
149 + private function check_that_tables_exist() {
150 + // Check the DB that the table $this->forms exists.
68 151 global $wpdb;
69 - if ( ! $wpdb->has_cap( 'collation' ) ) {
70 - return '';
152 + $exists = $wpdb->get_results( $wpdb->prepare( 'SHOW TABLES LIKE %s', $this->forms ) );
153 +
154 + if ( $exists ) {
155 + $inbox = new FrmInbox();
156 + $inbox->dismiss( 'failed-to-create-tables' );
157 + return;
71 158 }
72 159
73 - return $wpdb->get_charset_collate();
160 + $message = array(
161 + 'key' => 'failed-to-create-tables',
162 + 'subject' => 'Something went wrong setting up the database',
163 + 'message' => 'For steps to continue, see our <a href="https://formidableforms.com/knowledgebase/install-formidable-forms/#kb-missing-database-tables" target="_blank" rel="noopener">documentation</a>. If you need assistance, we recommend that you reach out to your hosting provider. Then <a href="' . esc_url( admin_url( 'admin.php?page=formidable&frm_add_tables=1' ) ) . '">click here</a> to try again.', // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
164 + 'cta' => '<a href="https://formidableforms.com/knowledgebase/install-formidable-forms/#kb-missing-database-tables" target="_blank" rel="noopener">Learn More</a>', // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
165 + 'type' => 'error',
166 + );
167 +
168 + $inbox = new FrmInbox();
169 + $inbox->add_message( $message );
74 170 }
75 171
172 + /**
173 + * @return string
174 + */
175 + public function collation() {
176 + global $wpdb;
177 + return $wpdb->has_cap( 'collation' ) ? $wpdb->get_charset_collate() : '';
178 + }
179 +
180 + /**
181 + * @return void
182 + */
76 183 private function create_tables() {
77 184 $charset_collate = $this->collation();
78 185 $sql = array();
79 186
@@ -148,8 +255,25 @@
148 255 KEY field_id (field_id),
149 256 KEY item_id (item_id)
150 257 )';
151 258
259 + /* Create/Upgrade Gated Tokens Table */
260 + $sql[] = 'CREATE TABLE ' . $this->gated_tokens . ' (
261 + id BIGINT UNSIGNED NOT NULL auto_increment,
262 + token_hash varchar(64) NOT NULL,
263 + action_id BIGINT UNSIGNED NOT NULL,
264 + entry_id BIGINT UNSIGNED NOT NULL,
265 + user_id BIGINT UNSIGNED default NULL,
266 + ip_address varchar(255) default NULL,
267 + created_at int UNSIGNED NOT NULL,
268 + expired_at int UNSIGNED default NULL,
269 + PRIMARY KEY (id),
270 + UNIQUE KEY token_hash (token_hash),
271 + KEY action_id (action_id),
272 + KEY user_id (user_id),
273 + KEY expired_at (expired_at)
274 + )';
275 +
152 276 foreach ( $sql as $q ) {
153 277 if ( function_exists( 'dbDelta' ) ) {
154 278 dbDelta( $q . $charset_collate . ';' );
155 279 } else {
@@ -157,12 +281,84 @@
157 281 $wpdb->query( $q . $charset_collate ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
158 282 }
159 283 unset( $q );
160 284 }
285 +
286 + $this->add_composite_indexes_for_entries();
161 287 }
162 288
289 + /**
290 + * These indexes help optimize database queries for entries.
291 + *
292 + * @since 6.6
293 + * @since 6.16.3 idx_form_id_is_draft was also added to frm_items.
294 + * @since 6.17 idx_form_id_type was also added to frm_fields.
295 + *
296 + * @return void
297 + */
298 + private function add_composite_indexes_for_entries() {
299 + global $wpdb;
300 +
301 + $table_name = "{$wpdb->prefix}frm_items";
302 + $index_name = 'idx_is_draft_created_at';
303 +
304 + if ( ! self::index_exists( $table_name, $index_name ) ) {
305 + $wpdb->query( "CREATE INDEX idx_is_draft_created_at ON `{$wpdb->prefix}frm_items` (is_draft, created_at)" );
306 + }
307 +
308 + $table_name = "{$wpdb->prefix}frm_item_metas";
309 + $index_name = 'idx_field_id_item_id';
310 +
311 + if ( ! self::index_exists( $table_name, $index_name ) ) {
312 + $wpdb->query( "CREATE INDEX idx_field_id_item_id ON `{$wpdb->prefix}frm_item_metas` (field_id, item_id)" );
313 + }
314 +
315 + $table_name = "{$wpdb->prefix}frm_items";
316 + $index_name = 'idx_form_id_is_draft';
317 +
318 + if ( ! self::index_exists( $table_name, $index_name ) ) {
319 + $wpdb->query( "CREATE INDEX idx_form_id_is_draft ON `{$wpdb->prefix}frm_items` (form_id, is_draft)" );
320 + }
321 +
322 + $table_name = "{$wpdb->prefix}frm_fields";
323 + $index_name = 'idx_form_id_type';
324 +
325 + if ( ! self::index_exists( $table_name, $index_name ) ) {
326 + $wpdb->query( "CREATE INDEX idx_form_id_type ON `{$wpdb->prefix}frm_fields` (form_id, type(30))" );
327 + }
328 + }
329 +
330 + /**
331 + * Check that an index exists in a database table before trying to add it (which results in an error).
332 + *
333 + * @since 6.6
334 + *
335 + * @param string $table_name
336 + * @param string $index_name
337 + *
338 + * @return bool
339 + */
340 + private static function index_exists( $table_name, $index_name ) {
341 + global $wpdb;
342 + $row = $wpdb->get_row(
343 + $wpdb->prepare(
344 + 'SELECT 1 FROM information_schema.statistics
345 + WHERE table_schema = database()
346 + AND table_name = %s
347 + AND index_name = %s
348 + LIMIT 1',
349 + array( $table_name, $index_name )
350 + )
351 + );
352 + return (bool) $row;
353 + }
354 +
355 + /**
356 + * @return void
357 + */
163 358 private function maybe_create_contact_form() {
164 359 $form_exists = FrmForm::get_id_by_key( 'contact-form' );
360 +
165 361 if ( ! $form_exists ) {
166 362 $this->add_default_template();
167 363 }
168 364 }
@@ -170,8 +366,10 @@
170 366 /**
171 367 * Create the default contact form
172 368 *
173 369 * @since 3.06
370 + *
371 + * @return void
174 372 */
175 373 private function add_default_template() {
176 374 if ( FrmXMLHelper::check_if_libxml_disable_entity_loader_exists() ) {
177 375 // XML import is not enabled on your server.
@@ -179,10 +377,9 @@
179 377 }
180 378
181 379 $set_err = libxml_use_internal_errors( true );
182 380 $loader = FrmXMLHelper::maybe_libxml_disable_entity_loader( true );
183 -
184 - $file = FrmAppHelper::plugin_path() . '/classes/views/xml/default-templates.xml';
381 + $file = FrmAppHelper::plugin_path() . '/classes/views/xml/default-templates.xml';
185 382 FrmXMLHelper::import_xml( $file );
186 383
187 384 libxml_use_internal_errors( $set_err );
188 385 FrmXMLHelper::maybe_libxml_disable_entity_loader( $loader );
@@ -189,32 +386,41 @@
189 386 }
190 387
191 388 /**
192 389 * @param int|string $old_db_version
390 + *
391 + * @return void
193 392 */
194 393 private function migrate_data( $old_db_version ) {
195 394 if ( ! $old_db_version ) {
196 395 $old_db_version = get_option( 'frm_db_version' );
197 396 }
198 - if ( strpos( $old_db_version, '-' ) ) {
397 +
398 + if ( str_contains( $old_db_version, '-' ) ) {
199 399 $last_upgrade = explode( '-', $old_db_version );
200 - $old_db_version = (int) $last_upgrade[1];
400 + $old_db_version = intval( end( $last_upgrade ) );
201 401 }
202 402
203 403 if ( ! is_numeric( $old_db_version ) ) {
204 - // bail if we don't know the previous version
404 + // Bail if we don't know the previous version
205 405 return;
206 406 }
207 407
208 - $migrations = array( 16, 11, 16, 17, 23, 25, 86, 90, 97, 98 );
408 + $migrations = array( 16, 11, 16, 17, 23, 25, 86, 90, 97, 98, 101, 104, 105 );
409 +
209 410 foreach ( $migrations as $migration ) {
210 - if ( FrmAppHelper::$db_version >= $migration && $old_db_version < $migration ) {
211 - $function_name = 'migrate_to_' . $migration;
212 - $this->$function_name();
411 + if ( FrmAppHelper::$db_version < $migration || $old_db_version >= $migration ) {
412 + continue;
213 413 }
414 +
415 + $function_name = 'migrate_to_' . $migration;
416 + $this->$function_name();
214 417 }
215 418 }
216 419
420 + /**
421 + * @return bool
422 + */
217 423 public function uninstall() {
218 424 if ( ! current_user_can( 'administrator' ) ) {
219 425 $frm_settings = FrmAppHelper::get_settings();
220 426 wp_die( esc_html( $frm_settings->admin_permission ) );
@@ -225,8 +431,9 @@
225 431 $wpdb->query( 'DROP TABLE IF EXISTS ' . $this->fields ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
226 432 $wpdb->query( 'DROP TABLE IF EXISTS ' . $this->forms ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
227 433 $wpdb->query( 'DROP TABLE IF EXISTS ' . $this->entries ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
228 434 $wpdb->query( 'DROP TABLE IF EXISTS ' . $this->entry_metas ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
435 + $wpdb->query( 'DROP TABLE IF EXISTS ' . $this->gated_tokens ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
229 436
230 437 delete_option( 'frm_options' );
231 438 delete_option( 'frm_db_version' );
232 439 delete_option( 'frm_install_running' );
@@ -233,13 +440,15 @@
233 440 delete_option( 'frm_lite_settings_upgrade' );
234 441 delete_option( 'frm-usage-uuid' );
235 442 delete_option( 'frm_inbox' );
236 443 delete_option( 'frmpro_css' );
237 - delete_option( 'frm_welcome_redirect' );
444 + delete_option( FrmOnboardingWizardController::REDIRECT_STATUS_OPTION );
445 + delete_option( FrmEmailSummaryHelper::$option_name );
238 446
239 447 // Delete roles.
240 448 $frm_roles = FrmAppHelper::frm_capabilities();
241 449 $roles = get_editable_roles();
450 +
242 451 foreach ( $frm_roles as $frm_role => $frm_role_description ) {
243 452 foreach ( $roles as $role => $details ) {
244 453 $wp_roles->remove_cap( $role, $frm_role );
245 454 unset( $role, $details );
@@ -247,15 +456,16 @@
247 456 unset( $frm_role, $frm_role_description );
248 457 }
249 458 unset( $roles, $frm_roles );
250 459
251 - // delete actions, views, and styles
460 + // Delete actions, views, and styles
252 461
253 - // prevent the post deletion from triggering entries to be deleted
462 + // Prevent the post deletion from triggering entries to be deleted
254 463 remove_action( 'before_delete_post', 'FrmProDisplaysController::before_delete_post' );
255 464 remove_action( 'deleted_post', 'FrmProEntriesController::delete_entry' );
256 465
257 - $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' ) );
466 + $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' ) ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
467 +
258 468 foreach ( $post_ids as $post_id ) {
259 469 // Delete's each post.
260 470 wp_delete_post( $post_id, true );
261 471 }
@@ -260,15 +470,15 @@
260 470 wp_delete_post( $post_id, true );
261 471 }
262 472 unset( $post_ids );
263 473
264 - // delete transients
474 + // Delete transients
265 475 delete_transient( 'frmpro_css' );
266 476 delete_transient( 'frm_options' );
267 477 delete_transient( 'frmpro_options' );
268 - delete_transient( 'frm_activation_redirect' );
478 + delete_transient( FrmOnboardingWizardController::TRANSIENT_NAME );
269 479
270 - $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_%' ) );
480 + $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_%' ) ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
271 481
272 482 do_action( 'frm_after_uninstall' );
273 483
274 484 return true;
@@ -274,11 +484,67 @@
274 484 return true;
275 485 }
276 486
277 487 /**
488 + * In older versions of Lite, it's possible we've saved the wrong location ID.
489 + * So force it to get valid values again.
490 + *
491 + * @since 6.25
492 + *
493 + * @return void
494 + */
495 + private function migrate_to_104() {
496 + if ( FrmSquareLiteConnectHelper::get_merchant_id( 'test' ) ) {
497 + FrmSquareLiteConnectHelper::get_location_id( true, 'test' );
498 + }
499 +
500 + if ( FrmSquareLiteConnectHelper::get_merchant_id( 'live' ) ) {
501 + FrmSquareLiteConnectHelper::get_location_id( true, 'live' );
502 + }
503 + }
504 +
505 + /**
506 + * Add new wp_options row for custom setting.
507 + *
508 + * @since 6.26
509 + *
510 + * @return void
511 + */
512 + private function migrate_to_105() {
513 + if ( ! FrmAppHelper::pro_is_installed() ) {
514 + update_option( 'frm_show_pricing_fields_modal', 1, false );
515 + }
516 + }
517 +
518 + /**
519 + * Disables summary email for multisite (not the main site) if recipient setting isn't changed.
520 + *
521 + * @since 6.8
522 + *
523 + * @return void
524 + */
525 + private function migrate_to_101() {
526 + if ( ! is_multisite() || get_main_site_id() === get_current_blog_id() ) {
527 + return;
528 + }
529 +
530 + $frm_settings = FrmAppHelper::get_settings();
531 +
532 + if ( empty( $frm_settings->summary_emails ) || '[admin_email]' !== $frm_settings->summary_emails_recipients ) {
533 + // User changed it.
534 + return;
535 + }
536 +
537 + $frm_settings->summary_emails = 0;
538 + $frm_settings->store();
539 + }
540 +
541 + /**
278 542 * Clear frmpro_css transient.
279 543 *
280 544 * @since 4.10.02
545 + *
546 + * @return void
281 547 */
282 548 private function migrate_to_98() {
283 549 delete_transient( 'frmpro_css' );
284 550 }
@@ -286,8 +552,10 @@
286 552 /**
287 553 * Move default_blank and clear_on_focus to placeholder.
288 554 *
289 555 * @since 4.0
556 + *
557 + * @return void
290 558 */
291 559 private function migrate_to_97() {
292 560 $this->migrate_to_placeholder( 'clear_on_focus' );
293 561 $this->migrate_to_placeholder( 'default_blank' );
@@ -296,8 +564,12 @@
296 564 /**
297 565 * Move clear_on_focus or default_blank to placeholder.
298 566 *
299 567 * @since 4.0
568 + *
569 + * @param string $type Field option key to migrate.
570 + *
571 + * @return void
300 572 */
301 573 private function migrate_to_placeholder( $type = 'clear_on_focus' ) {
302 574 $query = array(
303 575 'field_options like' => '"' . $type . '";s:1:"1";',
@@ -308,9 +580,10 @@
308 580 foreach ( $fields as $field ) {
309 581 FrmAppHelper::unserialize_or_decode( $field->field_options );
310 582 FrmAppHelper::unserialize_or_decode( $field->options );
311 583 $update_values = FrmXMLHelper::migrate_field_placeholder( $field, $type );
312 - if ( empty( $update_values ) ) {
584 +
585 + if ( ! $update_values ) {
313 586 continue;
314 587 }
315 588
316 589 FrmField::update( $field->id, $update_values );
@@ -318,14 +591,18 @@
318 591 }
319 592 }
320 593
321 594 /**
322 - * Delete uneeded default templates
595 + * Delete unneeded default templates
323 596 *
324 597 * @since 3.06
598 + *
599 + * @return void
325 600 */
326 601 private function migrate_to_90() {
327 602 $form = FrmForm::getOne( 'contact' );
603 +
604 + // phpcs:ignore Universal.Operators.StrictComparisons
328 605 if ( $form && $form->default_template == 1 ) {
329 606 FrmForm::destroy( 'contact' );
330 607 }
331 608 }
@@ -333,16 +610,17 @@
333 610 /**
334 611 * Reverse migration 17 -- Divide by 9
335 612 *
336 613 * @since 3.0.05
614 + *
615 + * @return void
337 616 */
338 617 private function migrate_to_86() {
339 -
340 618 $fields = $this->get_fields_with_size();
341 619
342 - foreach ( (array) $fields as $f ) {
620 + foreach ( $fields as $f ) {
343 621 FrmAppHelper::unserialize_or_decode( $f->field_options );
344 - $size = $f->field_options['size'];
622 + $size = $f->field_options['size'];
345 623 $this->maybe_convert_migrated_size( $size );
346 624
347 625 if ( $size === $f->field_options['size'] ) {
348 626 continue;
@@ -352,11 +630,12 @@
352 630 FrmField::update( $f->id, array( 'field_options' => $f->field_options ) );
353 631 unset( $f );
354 632 }
355 633
356 - // reverse the extra size changes in widgets
634 + // Reverse the extra size changes in widgets
357 635 $widgets = get_option( 'widget_frm_show_form' );
358 - if ( empty( $widgets ) ) {
636 +
637 + if ( ! $widgets ) {
359 638 return;
360 639 }
361 640
362 641 $this->revert_widget_field_size();
@@ -361,8 +640,11 @@
361 640
362 641 $this->revert_widget_field_size();
363 642 }
364 643
644 + /**
645 + * @return array
646 + */
365 647 private function get_fields_with_size() {
366 648 $field_types = array(
367 649 'textarea',
368 650 'text',
@@ -390,16 +672,20 @@
390 672 /**
391 673 * Reverse the extra size changes in widgets
392 674 *
393 675 * @since 3.0.05
676 + *
677 + * @return void
394 678 */
395 679 private function revert_widget_field_size() {
396 680 $widgets = get_option( 'widget_frm_show_form' );
397 - if ( empty( $widgets ) ) {
681 +
682 + if ( ! $widgets ) {
398 683 return;
399 684 }
400 685
401 686 FrmAppHelper::unserialize_or_decode( $widgets );
687 +
402 688 foreach ( $widgets as $k => $widget ) {
403 689 if ( ! is_array( $widget ) || ! isset( $widget['size'] ) ) {
404 690 continue;
405 691 }
@@ -405,8 +691,9 @@
405 691 }
406 692
407 693 $this->maybe_convert_migrated_size( $widgets[ $k ]['size'] );
408 694 }
695 +
409 696 update_option( 'widget_frm_show_form', $widgets );
410 697 }
411 698
412 699 /**
@@ -412,23 +699,28 @@
412 699 /**
413 700 * Divide by 9 to reverse the multiplication
414 701 *
415 702 * @since 3.0.05
703 + *
704 + * @param string $size Size string to maybe convert, passed by reference.
705 + *
706 + * @return void
416 707 */
417 708 private function maybe_convert_migrated_size( &$size ) {
418 - $has_px_size = ! empty( $size ) && strpos( $size, 'px' );
709 + $has_px_size = $size && str_contains( $size, 'px' );
710 +
419 711 if ( ! $has_px_size ) {
420 712 return;
421 713 }
422 714
423 715 $int_size = str_replace( 'px', '', $size );
716 +
424 717 if ( ! is_numeric( $int_size ) || (int) $int_size < 900 ) {
425 718 return;
426 719 }
427 720
428 721 $pixel_conversion = 9;
429 -
430 - $size = round( (int) $int_size / $pixel_conversion );
722 + $size = round( (int) $int_size / $pixel_conversion );
431 723 }
432 724
433 725 /**
434 726 * Migrate old styling settings. If sites are using the old
@@ -434,19 +726,22 @@
434 726 * Migrate old styling settings. If sites are using the old
435 727 * default 400px field width, switch it to 100%
436 728 *
437 729 * @since 2.0.4
730 + *
731 + * @return void
438 732 */
439 733 private function migrate_to_25() {
440 - // get the style that was created with the style migration
734 + // Get the style that was created with the style migration
441 735 $frm_style = new FrmStyle();
442 736 $styles = $frm_style->get_all( 'post_date', 'ASC', 1 );
443 - if ( empty( $styles ) ) {
737 +
738 + if ( ! $styles ) {
444 739 return;
445 740 }
446 741
447 742 foreach ( $styles as $style ) {
448 - if ( $style->post_content['field_width'] == '400px' ) {
743 + if ( $style->post_content['field_width'] === '400px' ) {
449 744 $style->post_content['field_width'] = '100%';
450 745 $frm_style->save( (array) $style );
451 746
452 747 return;
@@ -458,13 +753,16 @@
458 753 * Check if the parent_form_id columns exists.
459 754 * If not, try and add it again
460 755 *
461 756 * @since 2.0.2
757 + *
758 + * @return void
462 759 */
463 760 private function migrate_to_23() {
464 761 global $wpdb;
465 762 $exists = $wpdb->get_row( 'SHOW COLUMNS FROM ' . $this->forms . ' LIKE "parent_form_id"' ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
466 - if ( empty( $exists ) ) {
763 +
764 + if ( ! $exists ) {
467 765 $wpdb->query( 'ALTER TABLE ' . $this->forms . ' ADD parent_form_id int(11) default 0' ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
468 766 }
469 767 }
470 768
@@ -469,8 +767,10 @@
469 767 }
470 768
471 769 /**
472 770 * Change field size from character to pixel -- Multiply by 9
771 + *
772 + * @return void
473 773 */
474 774 private function migrate_to_17() {
475 775 $fields = $this->get_fields_with_size();
476 776
@@ -475,8 +775,9 @@
475 775 $fields = $this->get_fields_with_size();
476 776
477 777 foreach ( $fields as $f ) {
478 778 FrmAppHelper::unserialize_or_decode( $f->field_options );
779 +
479 780 if ( empty( $f->field_options['size'] ) || ! is_numeric( $f->field_options['size'] ) ) {
480 781 continue;
481 782 }
482 783
@@ -490,16 +791,20 @@
490 791 }
491 792
492 793 /**
493 794 * Change the characters in widgets to pixels
795 + *
796 + * @return void
494 797 */
495 798 private function adjust_widget_size() {
496 799 $widgets = get_option( 'widget_frm_show_form' );
497 - if ( empty( $widgets ) ) {
800 +
801 + if ( ! $widgets ) {
498 802 return;
499 803 }
500 804
501 805 FrmAppHelper::unserialize_or_decode( $widgets );
806 +
502 807 foreach ( $widgets as $k => $widget ) {
503 808 if ( ! is_array( $widget ) || ! isset( $widget['size'] ) ) {
504 809 continue;
505 810 }
@@ -504,15 +809,21 @@
504 809 continue;
505 810 }
506 811 $this->convert_character_to_px( $widgets[ $k ]['size'] );
507 812 }
813 +
508 814 update_option( 'widget_frm_show_form', $widgets );
509 815 }
510 816
817 + /**
818 + * @param string $size
819 + *
820 + * @return void
821 + */
511 822 private function convert_character_to_px( &$size ) {
512 823 $pixel_conversion = 9;
513 824
514 - $size = round( $pixel_conversion * (int) $size );
825 + $size = round( $pixel_conversion * (int) $size );
515 826 $size .= 'px';
516 827 }
517 828
518 829 /**
@@ -551,9 +862,9 @@
551 862 * post_excerpt: message
552 863 */
553 864 foreach ( $forms as $form ) {
554 865 if ( $form->is_template && $form->default_template ) {
555 - // don't migrate the default templates since the email will be added anyway
866 + // Don't migrate the default templates since the email will be added anyway
556 867 continue;
557 868 }
558 869
559 870 // Format form options
@@ -567,10 +878,9 @@
567 878
568 879 private function migrate_to_11() {
569 880 global $wpdb;
570 881
571 - $forms = FrmDb::get_results( $this->forms, array(), 'id, options' );
572 -
882 + $forms = FrmDb::get_results( $this->forms, array(), 'id, options' );
573 883 $sending = __( 'Sending', 'formidable' );
574 884 $img = FrmAppHelper::plugin_url() . '/images/ajax_loader.gif';
575 885 $old_default_html = <<<DEFAULT_HTML
576 886 <div class="frm_submit">
@@ -582,18 +892,21 @@
582 892 unset( $sending, $img );
583 893
584 894 $new_default_html = FrmFormsHelper::get_default_html( 'submit' );
585 895 $draft_link = FrmFormsHelper::get_draft_link();
896 +
586 897 foreach ( $forms as $form ) {
587 898 FrmAppHelper::unserialize_or_decode( $form->options );
588 - if ( ! isset( $form->options['submit_html'] ) || empty( $form->options['submit_html'] ) ) {
899 +
900 + if ( empty( $form->options['submit_html'] ) ) {
589 901 continue;
590 902 }
591 903
904 + // phpcs:ignore Universal.Operators.StrictComparisons
592 905 if ( $form->options['submit_html'] != $new_default_html && $form->options['submit_html'] == $old_default_html ) {
593 906 $form->options['submit_html'] = $new_default_html;
594 907 $wpdb->update( $this->forms, array( 'options' => serialize( $form->options ) ), array( 'id' => $form->id ) );
595 - } elseif ( ! strpos( $form->options['submit_html'], 'save_draft' ) ) {
908 + } elseif ( ! str_contains( $form->options['submit_html'], 'save_draft' ) ) {
596 909 $form->options['submit_html'] = preg_replace( '~\<\/div\>(?!.*\<\/div\>)~', $draft_link . "\r\n</div>", $form->options['submit_html'] );
597 910 $wpdb->update( $this->forms, array( 'options' => serialize( $form->options ) ), array( 'id' => $form->id ) );
598 911 }
599 912 unset( $form );