| @@ -35,22 +35,29 @@ | ||
| 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 | + } | |
| 56 | + | |
| 57 | + $this->update_settings_for_new_install(); | |
| 51 | 58 | } |
| 52 | - } | |
| 59 | + }//end if | |
| 53 | 60 | |
| 54 | 61 | do_action( 'frm_after_install' ); |
| 55 | 62 | |
| 56 | 63 | $frm_vars['doing_upgrade'] = false; |
| @@ -63,8 +70,54 @@ | ||
| 63 | 70 | $frm_style->update( 'default' ); |
| 64 | 71 | } |
| 65 | 72 | } |
| 66 | 73 | |
| 74 | + /** | |
| 75 | + * Updates some settings for new installs. | |
| 76 | + * | |
| 77 | + * @since 6.23 | |
| 78 | + */ | |
| 79 | + private function update_settings_for_new_install() { | |
| 80 | + $settings = FrmAppHelper::get_settings(); | |
| 81 | + | |
| 82 | + $settings->denylist_check = 1; | |
| 83 | + $settings->store(); | |
| 84 | + } | |
| 85 | + | |
| 86 | + /** | |
| 87 | + * If we fail to create the database tables, add an inbox notice. | |
| 88 | + * This informs the user that they need to correct the issue and try again. | |
| 89 | + * | |
| 90 | + * @since 6.19 | |
| 91 | + * | |
| 92 | + * @return void | |
| 93 | + */ | |
| 94 | + private function check_that_tables_exist() { | |
| 95 | + // Check the DB that the table $this->forms exists. | |
| 96 | + global $wpdb; | |
| 97 | + $exists = $wpdb->get_results( $wpdb->prepare( 'SHOW TABLES LIKE %s', $this->forms ) ); | |
| 98 | + | |
| 99 | + if ( $exists ) { | |
| 100 | + $inbox = new FrmInbox(); | |
| 101 | + $inbox->dismiss( 'failed-to-create-tables' ); | |
| 102 | + return; | |
| 103 | + } | |
| 104 | + | |
| 105 | + $message = array( | |
| 106 | + 'key' => 'failed-to-create-tables', | |
| 107 | + 'subject' => 'Something went wrong setting up the database', | |
| 108 | + '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.', | |
| 109 | + 'cta' => '<a href="https://formidableforms.com/knowledgebase/install-formidable-forms/#kb-missing-database-tables">Learn More</a>', | |
| 110 | + 'type' => 'error', | |
| 111 | + ); | |
| 112 | + | |
| 113 | + $inbox = new FrmInbox(); | |
| 114 | + $inbox->add_message( $message ); | |
| 115 | + } | |
| 116 | + | |
| 117 | + /** | |
| 118 | + * @return string | |
| 119 | + */ | |
| 67 | 120 | public function collation() { |
| 68 | 121 | global $wpdb; |
| 69 | 122 | if ( ! $wpdb->has_cap( 'collation' ) ) { |
| 70 | 123 | return ''; |
| @@ -157,10 +210,77 @@ | ||
| 157 | 210 | $wpdb->query( $q . $charset_collate ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 158 | 211 | } |
| 159 | 212 | unset( $q ); |
| 160 | 213 | } |
| 214 | + | |
| 215 | + $this->add_composite_indexes_for_entries(); | |
| 161 | 216 | } |
| 162 | 217 | |
| 218 | + /** | |
| 219 | + * These indexes help optimize database queries for entries. | |
| 220 | + * | |
| 221 | + * @since 6.6 | |
| 222 | + * @since 6.16.3 idx_form_id_is_draft was also added to frm_items. | |
| 223 | + * @since 6.17 idx_form_id_type was also added to frm_fields. | |
| 224 | + * | |
| 225 | + * @return void | |
| 226 | + */ | |
| 227 | + private function add_composite_indexes_for_entries() { | |
| 228 | + global $wpdb; | |
| 229 | + | |
| 230 | + $table_name = "{$wpdb->prefix}frm_items"; | |
| 231 | + $index_name = 'idx_is_draft_created_at'; | |
| 232 | + | |
| 233 | + if ( ! self::index_exists( $table_name, $index_name ) ) { | |
| 234 | + $wpdb->query( "CREATE INDEX idx_is_draft_created_at ON `{$wpdb->prefix}frm_items` (is_draft, created_at)" ); | |
| 235 | + } | |
| 236 | + | |
| 237 | + $table_name = "{$wpdb->prefix}frm_item_metas"; | |
| 238 | + $index_name = 'idx_field_id_item_id'; | |
| 239 | + | |
| 240 | + if ( ! self::index_exists( $table_name, $index_name ) ) { | |
| 241 | + $wpdb->query( "CREATE INDEX idx_field_id_item_id ON `{$wpdb->prefix}frm_item_metas` (field_id, item_id)" ); | |
| 242 | + } | |
| 243 | + | |
| 244 | + $table_name = "{$wpdb->prefix}frm_items"; | |
| 245 | + $index_name = 'idx_form_id_is_draft'; | |
| 246 | + | |
| 247 | + if ( ! self::index_exists( $table_name, $index_name ) ) { | |
| 248 | + $wpdb->query( "CREATE INDEX idx_form_id_is_draft ON `{$wpdb->prefix}frm_items` (form_id, is_draft)" ); | |
| 249 | + } | |
| 250 | + | |
| 251 | + $table_name = "{$wpdb->prefix}frm_fields"; | |
| 252 | + $index_name = 'idx_form_id_type'; | |
| 253 | + | |
| 254 | + if ( ! self::index_exists( $table_name, $index_name ) ) { | |
| 255 | + $wpdb->query( "CREATE INDEX idx_form_id_type ON `{$wpdb->prefix}frm_fields` (form_id, type(30))" ); | |
| 256 | + } | |
| 257 | + } | |
| 258 | + | |
| 259 | + /** | |
| 260 | + * Check that an index exists in a database table before trying to add it (which results in an error). | |
| 261 | + * | |
| 262 | + * @since 6.6 | |
| 263 | + * | |
| 264 | + * @param string $table_name | |
| 265 | + * @param string $index_name | |
| 266 | + * @return bool | |
| 267 | + */ | |
| 268 | + private static function index_exists( $table_name, $index_name ) { | |
| 269 | + global $wpdb; | |
| 270 | + $row = $wpdb->get_row( | |
| 271 | + $wpdb->prepare( | |
| 272 | + 'SELECT 1 FROM information_schema.statistics | |
| 273 | + WHERE table_schema = database() | |
| 274 | + AND table_name = %s | |
| 275 | + AND index_name = %s | |
| 276 | + LIMIT 1', | |
| 277 | + array( $table_name, $index_name ) | |
| 278 | + ) | |
| 279 | + ); | |
| 280 | + return (bool) $row; | |
| 281 | + } | |
| 282 | + | |
| 163 | 283 | private function maybe_create_contact_form() { |
| 164 | 284 | $form_exists = FrmForm::get_id_by_key( 'contact-form' ); |
| 165 | 285 | if ( ! $form_exists ) { |
| 166 | 286 | $this->add_default_template(); |
| @@ -204,9 +324,9 @@ | ||
| 204 | 324 | // bail if we don't know the previous version |
| 205 | 325 | return; |
| 206 | 326 | } |
| 207 | 327 | |
| 208 | - $migrations = array( 16, 11, 16, 17, 23, 25, 86, 90, 97, 98 ); | |
| 328 | + $migrations = array( 16, 11, 16, 17, 23, 25, 86, 90, 97, 98, 101 ); | |
| 209 | 329 | foreach ( $migrations as $migration ) { |
| 210 | 330 | if ( FrmAppHelper::$db_version >= $migration && $old_db_version < $migration ) { |
| 211 | 331 | $function_name = 'migrate_to_' . $migration; |
| 212 | 332 | $this->$function_name(); |
| @@ -233,9 +353,10 @@ | ||
| 233 | 353 | delete_option( 'frm_lite_settings_upgrade' ); |
| 234 | 354 | delete_option( 'frm-usage-uuid' ); |
| 235 | 355 | delete_option( 'frm_inbox' ); |
| 236 | 356 | delete_option( 'frmpro_css' ); |
| 237 | - delete_option( 'frm_welcome_redirect' ); | |
| 357 | + delete_option( FrmOnboardingWizardController::REDIRECT_STATUS_OPTION ); | |
| 358 | + delete_option( FrmEmailSummaryHelper::$option_name ); | |
| 238 | 359 | |
| 239 | 360 | // Delete roles. |
| 240 | 361 | $frm_roles = FrmAppHelper::frm_capabilities(); |
| 241 | 362 | $roles = get_editable_roles(); |
| @@ -264,9 +385,9 @@ | ||
| 264 | 385 | // delete transients |
| 265 | 386 | delete_transient( 'frmpro_css' ); |
| 266 | 387 | delete_transient( 'frm_options' ); |
| 267 | 388 | delete_transient( 'frmpro_options' ); |
| 268 | - delete_transient( 'frm_activation_redirect' ); | |
| 389 | + delete_transient( FrmOnboardingWizardController::TRANSIENT_NAME ); | |
| 269 | 390 | |
| 270 | 391 | $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 | 392 | |
| 272 | 393 | do_action( 'frm_after_uninstall' ); |
| @@ -274,8 +395,28 @@ | ||
| 274 | 395 | return true; |
| 275 | 396 | } |
| 276 | 397 | |
| 277 | 398 | /** |
| 399 | + * Disables summary email for multisite (not the main site) if recipient setting isn't changed. | |
| 400 | + * | |
| 401 | + * @since 6.8 | |
| 402 | + */ | |
| 403 | + private function migrate_to_101() { | |
| 404 | + if ( ! is_multisite() || get_main_site_id() === get_current_blog_id() ) { | |
| 405 | + return; | |
| 406 | + } | |
| 407 | + | |
| 408 | + $frm_settings = FrmAppHelper::get_settings(); | |
| 409 | + if ( empty( $frm_settings->summary_emails ) || '[admin_email]' !== $frm_settings->summary_emails_recipients ) { | |
| 410 | + // User changed it. | |
| 411 | + return; | |
| 412 | + } | |
| 413 | + | |
| 414 | + $frm_settings->summary_emails = 0; | |
| 415 | + $frm_settings->store(); | |
| 416 | + } | |
| 417 | + | |
| 418 | + /** | |
| 278 | 419 | * Clear frmpro_css transient. |
| 279 | 420 | * |
| 280 | 421 | * @since 4.10.02 |
| 281 | 422 | */ |
| @@ -318,9 +459,9 @@ | ||
| 318 | 459 | } |
| 319 | 460 | } |
| 320 | 461 | |
| 321 | 462 | /** |
| 322 | - * Delete uneeded default templates | |
| 463 | + * Delete unneeded default templates | |
| 323 | 464 | * |
| 324 | 465 | * @since 3.06 |
| 325 | 466 | */ |
| 326 | 467 | private function migrate_to_90() { |
| @@ -340,9 +481,9 @@ | ||
| 340 | 481 | $fields = $this->get_fields_with_size(); |
| 341 | 482 | |
| 342 | 483 | foreach ( (array) $fields as $f ) { |
| 343 | 484 | FrmAppHelper::unserialize_or_decode( $f->field_options ); |
| 344 | - $size = $f->field_options['size']; | |
| 485 | + $size = $f->field_options['size']; | |
| 345 | 486 | $this->maybe_convert_migrated_size( $size ); |
| 346 | 487 | |
| 347 | 488 | if ( $size === $f->field_options['size'] ) { |
| 348 | 489 | continue; |
| @@ -444,9 +585,9 @@ | ||
| 444 | 585 | return; |
| 445 | 586 | } |
| 446 | 587 | |
| 447 | 588 | foreach ( $styles as $style ) { |
| 448 | - if ( $style->post_content['field_width'] == '400px' ) { | |
| 589 | + if ( $style->post_content['field_width'] === '400px' ) { | |
| 449 | 590 | $style->post_content['field_width'] = '100%'; |
| 450 | 591 | $frm_style->save( (array) $style ); |
| 451 | 592 | |
| 452 | 593 | return; |
| @@ -510,9 +651,9 @@ | ||
| 510 | 651 | |
| 511 | 652 | private function convert_character_to_px( &$size ) { |
| 512 | 653 | $pixel_conversion = 9; |
| 513 | 654 | |
| 514 | - $size = round( $pixel_conversion * (int) $size ); | |
| 655 | + $size = round( $pixel_conversion * (int) $size ); | |
| 515 | 656 | $size .= 'px'; |
| 516 | 657 | } |
| 517 | 658 | |
| 518 | 659 | /** |
| @@ -584,9 +725,9 @@ | ||
| 584 | 725 | $new_default_html = FrmFormsHelper::get_default_html( 'submit' ); |
| 585 | 726 | $draft_link = FrmFormsHelper::get_draft_link(); |
| 586 | 727 | foreach ( $forms as $form ) { |
| 587 | 728 | FrmAppHelper::unserialize_or_decode( $form->options ); |
| 588 | - if ( ! isset( $form->options['submit_html'] ) || empty( $form->options['submit_html'] ) ) { | |
| 729 | + if ( empty( $form->options['submit_html'] ) ) { | |
| 589 | 730 | continue; |
| 590 | 731 | } |
| 591 | 732 | |
| 592 | 733 | if ( $form->options['submit_html'] != $new_default_html && $form->options['submit_html'] == $old_default_html ) { |