| @@ -42,9 +42,9 @@ | ||
| 42 | 42 | |
| 43 | 43 | $this->create_tables(); |
| 44 | 44 | $this->migrate_data( $old_db_version ); |
| 45 | 45 | |
| 46 | - /***** SAVE DB VERSION *****/ | |
| 46 | + // SAVE DB VERSION. | |
| 47 | 47 | update_option( 'frm_db_version', FrmAppHelper::plugin_version() . '-' . FrmAppHelper::$db_version ); |
| 48 | 48 | |
| 49 | 49 | if ( ! $old_db_version ) { |
| 50 | 50 | $this->maybe_create_contact_form(); |
| @@ -157,10 +157,61 @@ | ||
| 157 | 157 | $wpdb->query( $q . $charset_collate ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 158 | 158 | } |
| 159 | 159 | unset( $q ); |
| 160 | 160 | } |
| 161 | + | |
| 162 | + $this->add_composite_indexes_for_entries(); | |
| 161 | 163 | } |
| 162 | 164 | |
| 165 | + /** | |
| 166 | + * These indexes help optimize database queries for entries. | |
| 167 | + * | |
| 168 | + * @since 6.6 | |
| 169 | + * | |
| 170 | + * @return void | |
| 171 | + */ | |
| 172 | + private function add_composite_indexes_for_entries() { | |
| 173 | + global $wpdb; | |
| 174 | + | |
| 175 | + $table_name = "{$wpdb->prefix}frm_items"; | |
| 176 | + $index_name = 'idx_is_draft_created_at'; | |
| 177 | + | |
| 178 | + if ( ! self::index_exists( $table_name, $index_name ) ) { | |
| 179 | + $wpdb->query( "CREATE INDEX idx_is_draft_created_at ON `{$wpdb->prefix}frm_items` (is_draft, created_at)" ); | |
| 180 | + } | |
| 181 | + | |
| 182 | + $table_name = "{$wpdb->prefix}frm_item_metas"; | |
| 183 | + $index_name = 'idx_field_id_item_id'; | |
| 184 | + | |
| 185 | + if ( ! self::index_exists( $table_name, $index_name ) ) { | |
| 186 | + $wpdb->query( "CREATE INDEX idx_field_id_item_id ON `{$wpdb->prefix}frm_item_metas` (field_id, item_id)" ); | |
| 187 | + } | |
| 188 | + } | |
| 189 | + | |
| 190 | + /** | |
| 191 | + * Check that an index exists in a database table before trying to add it (which results in an error). | |
| 192 | + * | |
| 193 | + * @since 6.6 | |
| 194 | + * | |
| 195 | + * @param string $table_name | |
| 196 | + * @param string $index_name | |
| 197 | + * @return bool | |
| 198 | + */ | |
| 199 | + private static function index_exists( $table_name, $index_name ) { | |
| 200 | + global $wpdb; | |
| 201 | + $row = $wpdb->get_row( | |
| 202 | + $wpdb->prepare( | |
| 203 | + 'SELECT 1 FROM information_schema.statistics | |
| 204 | + WHERE table_schema = database() | |
| 205 | + AND table_name = %s | |
| 206 | + AND index_name = %s | |
| 207 | + LIMIT 1', | |
| 208 | + array( $table_name, $index_name ) | |
| 209 | + ) | |
| 210 | + ); | |
| 211 | + return (bool) $row; | |
| 212 | + } | |
| 213 | + | |
| 163 | 214 | private function maybe_create_contact_form() { |
| 164 | 215 | $form_exists = FrmForm::get_id_by_key( 'contact-form' ); |
| 165 | 216 | if ( ! $form_exists ) { |
| 166 | 217 | $this->add_default_template(); |
| @@ -234,8 +285,9 @@ | ||
| 234 | 285 | delete_option( 'frm-usage-uuid' ); |
| 235 | 286 | delete_option( 'frm_inbox' ); |
| 236 | 287 | delete_option( 'frmpro_css' ); |
| 237 | 288 | delete_option( 'frm_welcome_redirect' ); |
| 289 | + delete_option( FrmEmailSummaryHelper::$option_name ); | |
| 238 | 290 | |
| 239 | 291 | // Delete roles. |
| 240 | 292 | $frm_roles = FrmAppHelper::frm_capabilities(); |
| 241 | 293 | $roles = get_editable_roles(); |