| @@ -35,22 +35,27 @@ | ||
| 35 | 35 | if ( $needs_upgrade ) { |
| 36 | 36 | // update rewrite rules for views and other custom post types |
| 37 | 37 | flush_rewrite_rules(); |
| 38 | 38 | |
| 39 | - require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); | |
| 39 | + require_once ABSPATH . 'wp-admin/includes/upgrade.php'; | |
| 40 | 40 | |
| 41 | 41 | $old_db_version = get_option( 'frm_db_version' ); |
| 42 | 42 | |
| 43 | 43 | $this->create_tables(); |
| 44 | 44 | $this->migrate_data( $old_db_version ); |
| 45 | + $this->check_that_tables_exist(); | |
| 45 | 46 | |
| 46 | - /***** SAVE DB VERSION *****/ | |
| 47 | + // SAVE DB VERSION. | |
| 47 | 48 | update_option( 'frm_db_version', FrmAppHelper::plugin_version() . '-' . FrmAppHelper::$db_version ); |
| 48 | 49 | |
| 49 | 50 | if ( ! $old_db_version ) { |
| 50 | 51 | $this->maybe_create_contact_form(); |
| 52 | + | |
| 53 | + if ( false === get_option( 'frm_first_activation' ) ) { | |
| 54 | + update_option( 'frm_first_activation', time(), false ); | |
| 55 | + } | |
| 51 | 56 | } |
| 52 | - } | |
| 57 | + }//end if | |
| 53 | 58 | |
| 54 | 59 | do_action( 'frm_after_install' ); |
| 55 | 60 | |
| 56 | 61 | $frm_vars['doing_upgrade'] = false; |
| @@ -63,8 +68,42 @@ | ||
| 63 | 68 | $frm_style->update( 'default' ); |
| 64 | 69 | } |
| 65 | 70 | } |
| 66 | 71 | |
| 72 | + /** | |
| 73 | + * If we fail to create the database tables, add an inbox notice. | |
| 74 | + * This informs the user that they need to correct the issue and try again. | |
| 75 | + * | |
| 76 | + * @since 6.19 | |
| 77 | + * | |
| 78 | + * @return void | |
| 79 | + */ | |
| 80 | + private function check_that_tables_exist() { | |
| 81 | + // Check the DB that the table $this->forms exists. | |
| 82 | + global $wpdb; | |
| 83 | + $exists = $wpdb->get_results( $wpdb->prepare( 'SHOW TABLES LIKE %s', $this->forms ) ); | |
| 84 | + | |
| 85 | + if ( $exists ) { | |
| 86 | + $inbox = new FrmInbox(); | |
| 87 | + $inbox->dismiss( 'failed-to-create-tables' ); | |
| 88 | + return; | |
| 89 | + } | |
| 90 | + | |
| 91 | + $message = array( | |
| 92 | + 'key' => 'failed-to-create-tables', | |
| 93 | + 'subject' => 'Something went wrong setting up the database', | |
| 94 | + 'message' => 'For steps to continue, see our <a href="https://formidableforms.com/knowledgebase/install-formidable-forms/#kb-missing-database-tables">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.', | |
| 95 | + 'cta' => '<a href="https://formidableforms.com/knowledgebase/install-formidable-forms/#kb-missing-database-tables">Learn More</a>', | |
| 96 | + 'type' => 'error', | |
| 97 | + ); | |
| 98 | + | |
| 99 | + $inbox = new FrmInbox(); | |
| 100 | + $inbox->add_message( $message ); | |
| 101 | + } | |
| 102 | + | |
| 103 | + /** | |
| 104 | + * @return string | |
| 105 | + */ | |
| 67 | 106 | public function collation() { |
| 68 | 107 | global $wpdb; |
| 69 | 108 | if ( ! $wpdb->has_cap( 'collation' ) ) { |
| 70 | 109 | return ''; |
| @@ -157,10 +196,77 @@ | ||
| 157 | 196 | $wpdb->query( $q . $charset_collate ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 158 | 197 | } |
| 159 | 198 | unset( $q ); |
| 160 | 199 | } |
| 200 | + | |
| 201 | + $this->add_composite_indexes_for_entries(); | |
| 161 | 202 | } |
| 162 | 203 | |
| 204 | + /** | |
| 205 | + * These indexes help optimize database queries for entries. | |
| 206 | + * | |
| 207 | + * @since 6.6 | |
| 208 | + * @since 6.16.3 idx_form_id_is_draft was also added to frm_items. | |
| 209 | + * @since 6.17 idx_form_id_type was also added to frm_fields. | |
| 210 | + * | |
| 211 | + * @return void | |
| 212 | + */ | |
| 213 | + private function add_composite_indexes_for_entries() { | |
| 214 | + global $wpdb; | |
| 215 | + | |
| 216 | + $table_name = "{$wpdb->prefix}frm_items"; | |
| 217 | + $index_name = 'idx_is_draft_created_at'; | |
| 218 | + | |
| 219 | + if ( ! self::index_exists( $table_name, $index_name ) ) { | |
| 220 | + $wpdb->query( "CREATE INDEX idx_is_draft_created_at ON `{$wpdb->prefix}frm_items` (is_draft, created_at)" ); | |
| 221 | + } | |
| 222 | + | |
| 223 | + $table_name = "{$wpdb->prefix}frm_item_metas"; | |
| 224 | + $index_name = 'idx_field_id_item_id'; | |
| 225 | + | |
| 226 | + if ( ! self::index_exists( $table_name, $index_name ) ) { | |
| 227 | + $wpdb->query( "CREATE INDEX idx_field_id_item_id ON `{$wpdb->prefix}frm_item_metas` (field_id, item_id)" ); | |
| 228 | + } | |
| 229 | + | |
| 230 | + $table_name = "{$wpdb->prefix}frm_items"; | |
| 231 | + $index_name = 'idx_form_id_is_draft'; | |
| 232 | + | |
| 233 | + if ( ! self::index_exists( $table_name, $index_name ) ) { | |
| 234 | + $wpdb->query( "CREATE INDEX idx_form_id_is_draft ON `{$wpdb->prefix}frm_items` (form_id, is_draft)" ); | |
| 235 | + } | |
| 236 | + | |
| 237 | + $table_name = "{$wpdb->prefix}frm_fields"; | |
| 238 | + $index_name = 'idx_form_id_type'; | |
| 239 | + | |
| 240 | + if ( ! self::index_exists( $table_name, $index_name ) ) { | |
| 241 | + $wpdb->query( "CREATE INDEX idx_form_id_type ON `{$wpdb->prefix}frm_fields` (form_id, type(30))" ); | |
| 242 | + } | |
| 243 | + } | |
| 244 | + | |
| 245 | + /** | |
| 246 | + * Check that an index exists in a database table before trying to add it (which results in an error). | |
| 247 | + * | |
| 248 | + * @since 6.6 | |
| 249 | + * | |
| 250 | + * @param string $table_name | |
| 251 | + * @param string $index_name | |
| 252 | + * @return bool | |
| 253 | + */ | |
| 254 | + private static function index_exists( $table_name, $index_name ) { | |
| 255 | + global $wpdb; | |
| 256 | + $row = $wpdb->get_row( | |
| 257 | + $wpdb->prepare( | |
| 258 | + 'SELECT 1 FROM information_schema.statistics | |
| 259 | + WHERE table_schema = database() | |
| 260 | + AND table_name = %s | |
| 261 | + AND index_name = %s | |
| 262 | + LIMIT 1', | |
| 263 | + array( $table_name, $index_name ) | |
| 264 | + ) | |
| 265 | + ); | |
| 266 | + return (bool) $row; | |
| 267 | + } | |
| 268 | + | |
| 163 | 269 | private function maybe_create_contact_form() { |
| 164 | 270 | $form_exists = FrmForm::get_id_by_key( 'contact-form' ); |
| 165 | 271 | if ( ! $form_exists ) { |
| 166 | 272 | $this->add_default_template(); |
| @@ -204,9 +310,9 @@ | ||
| 204 | 310 | // bail if we don't know the previous version |
| 205 | 311 | return; |
| 206 | 312 | } |
| 207 | 313 | |
| 208 | - $migrations = array( 16, 11, 16, 17, 23, 25, 86, 90, 97, 98 ); | |
| 314 | + $migrations = array( 16, 11, 16, 17, 23, 25, 86, 90, 97, 98, 101 ); | |
| 209 | 315 | foreach ( $migrations as $migration ) { |
| 210 | 316 | if ( FrmAppHelper::$db_version >= $migration && $old_db_version < $migration ) { |
| 211 | 317 | $function_name = 'migrate_to_' . $migration; |
| 212 | 318 | $this->$function_name(); |
| @@ -233,9 +339,10 @@ | ||
| 233 | 339 | delete_option( 'frm_lite_settings_upgrade' ); |
| 234 | 340 | delete_option( 'frm-usage-uuid' ); |
| 235 | 341 | delete_option( 'frm_inbox' ); |
| 236 | 342 | delete_option( 'frmpro_css' ); |
| 237 | - delete_option( 'frm_welcome_redirect' ); | |
| 343 | + delete_option( FrmOnboardingWizardController::REDIRECT_STATUS_OPTION ); | |
| 344 | + delete_option( FrmEmailSummaryHelper::$option_name ); | |
| 238 | 345 | |
| 239 | 346 | // Delete roles. |
| 240 | 347 | $frm_roles = FrmAppHelper::frm_capabilities(); |
| 241 | 348 | $roles = get_editable_roles(); |
| @@ -264,9 +371,9 @@ | ||
| 264 | 371 | // delete transients |
| 265 | 372 | delete_transient( 'frmpro_css' ); |
| 266 | 373 | delete_transient( 'frm_options' ); |
| 267 | 374 | delete_transient( 'frmpro_options' ); |
| 268 | - delete_transient( 'frm_activation_redirect' ); | |
| 375 | + delete_transient( FrmOnboardingWizardController::TRANSIENT_NAME ); | |
| 269 | 376 | |
| 270 | 377 | $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_%' ) ); |
| 271 | 378 | |
| 272 | 379 | do_action( 'frm_after_uninstall' ); |
| @@ -274,8 +381,28 @@ | ||
| 274 | 381 | return true; |
| 275 | 382 | } |
| 276 | 383 | |
| 277 | 384 | /** |
| 385 | + * Disables summary email for multisite (not the main site) if recipient setting isn't changed. | |
| 386 | + * | |
| 387 | + * @since 6.8 | |
| 388 | + */ | |
| 389 | + private function migrate_to_101() { | |
| 390 | + if ( ! is_multisite() || get_main_site_id() === get_current_blog_id() ) { | |
| 391 | + return; | |
| 392 | + } | |
| 393 | + | |
| 394 | + $frm_settings = FrmAppHelper::get_settings(); | |
| 395 | + if ( empty( $frm_settings->summary_emails ) || '[admin_email]' !== $frm_settings->summary_emails_recipients ) { | |
| 396 | + // User changed it. | |
| 397 | + return; | |
| 398 | + } | |
| 399 | + | |
| 400 | + $frm_settings->summary_emails = 0; | |
| 401 | + $frm_settings->store(); | |
| 402 | + } | |
| 403 | + | |
| 404 | + /** | |
| 278 | 405 | * Clear frmpro_css transient. |
| 279 | 406 | * |
| 280 | 407 | * @since 4.10.02 |
| 281 | 408 | */ |
| @@ -318,9 +445,9 @@ | ||
| 318 | 445 | } |
| 319 | 446 | } |
| 320 | 447 | |
| 321 | 448 | /** |
| 322 | - * Delete uneeded default templates | |
| 449 | + * Delete unneeded default templates | |
| 323 | 450 | * |
| 324 | 451 | * @since 3.06 |
| 325 | 452 | */ |
| 326 | 453 | private function migrate_to_90() { |
| @@ -340,9 +467,9 @@ | ||
| 340 | 467 | $fields = $this->get_fields_with_size(); |
| 341 | 468 | |
| 342 | 469 | foreach ( (array) $fields as $f ) { |
| 343 | 470 | FrmAppHelper::unserialize_or_decode( $f->field_options ); |
| 344 | - $size = $f->field_options['size']; | |
| 471 | + $size = $f->field_options['size']; | |
| 345 | 472 | $this->maybe_convert_migrated_size( $size ); |
| 346 | 473 | |
| 347 | 474 | if ( $size === $f->field_options['size'] ) { |
| 348 | 475 | continue; |
| @@ -444,9 +571,9 @@ | ||
| 444 | 571 | return; |
| 445 | 572 | } |
| 446 | 573 | |
| 447 | 574 | foreach ( $styles as $style ) { |
| 448 | - if ( $style->post_content['field_width'] == '400px' ) { | |
| 575 | + if ( $style->post_content['field_width'] === '400px' ) { | |
| 449 | 576 | $style->post_content['field_width'] = '100%'; |
| 450 | 577 | $frm_style->save( (array) $style ); |
| 451 | 578 | |
| 452 | 579 | return; |
| @@ -510,9 +637,9 @@ | ||
| 510 | 637 | |
| 511 | 638 | private function convert_character_to_px( &$size ) { |
| 512 | 639 | $pixel_conversion = 9; |
| 513 | 640 | |
| 514 | - $size = round( $pixel_conversion * (int) $size ); | |
| 641 | + $size = round( $pixel_conversion * (int) $size ); | |
| 515 | 642 | $size .= 'px'; |
| 516 | 643 | } |
| 517 | 644 | |
| 518 | 645 | /** |
| @@ -584,9 +711,9 @@ | ||
| 584 | 711 | $new_default_html = FrmFormsHelper::get_default_html( 'submit' ); |
| 585 | 712 | $draft_link = FrmFormsHelper::get_draft_link(); |
| 586 | 713 | foreach ( $forms as $form ) { |
| 587 | 714 | FrmAppHelper::unserialize_or_decode( $form->options ); |
| 588 | - if ( ! isset( $form->options['submit_html'] ) || empty( $form->options['submit_html'] ) ) { | |
| 715 | + if ( empty( $form->options['submit_html'] ) ) { | |
| 589 | 716 | continue; |
| 590 | 717 | } |
| 591 | 718 | |
| 592 | 719 | if ( $form->options['submit_html'] != $new_default_html && $form->options['submit_html'] == $old_default_html ) { |