PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.13
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.13
6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 3.6.66 All 195 releases
fluentform / database / Migrations / FormMeta.php

FormMeta.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 6.2.13, at database/Migrations/FormMeta.php

74 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\Database\Migrations;
4
5 class FormMeta
6 {
7 /**
8 * Migrate the table.
9 *
10 * @return void
11 */
12 public static function migrate()
13 {
14 global $wpdb;
15
16 $charsetCollate = $wpdb->get_charset_collate();
17
18 $table = $wpdb->prefix . 'fluentform_form_meta';
19
20 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Migration file, direct query needed
21 if ($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $table)) != $table) {
22 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange -- Migration file, schema change is the purpose
23 $sql = "CREATE TABLE $table (
24 `id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
25 `form_id` INT UNSIGNED NULL,
26 `meta_key` VARCHAR(255) NOT NULL,
27 `value` LONGTEXT NULL,
28 PRIMARY KEY (`id`),
29 KEY `form_id_meta_key` (`form_id`, `meta_key`(191)),
30 KEY `meta_key` (`meta_key`(191))) $charsetCollate;";
31
32 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
33
34 dbDelta($sql);
35 } else {
36 // Add indexes to existing tables
37 self::maybeAddIndexes();
38 }
39 }
40
41 /**
42 * Add indexes to existing tables for better query performance.
43 *
44 * @return void
45 */
46 private static function maybeAddIndexes()
47 {
48 global $wpdb;
49
50 $table = $wpdb->prefix . 'fluentform_form_meta';
51
52 // Check if indexes already exist
53 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQLPlaceholders.UnquotedComplexPlaceholder -- Migration file, checking indexes, %1s is for identifier
54 $indexes = $wpdb->get_results($wpdb->prepare("SHOW INDEX FROM %1s", $table), ARRAY_A);
55
56 $existingIndexes = [];
57 foreach ($indexes as $index) {
58 $existingIndexes[] = $index['Key_name'];
59 }
60
61 // Add composite index if it doesn't exist
62 if (!in_array('form_id_meta_key', $existingIndexes)) {
63 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQLPlaceholders.UnquotedComplexPlaceholder -- Migration file, adding composite index for performance, %1s is for identifier
64 $wpdb->query($wpdb->prepare("ALTER TABLE %1s ADD KEY `form_id_meta_key` (`form_id`, `meta_key`(191))", $table));
65 }
66
67 // Add meta_key index if it doesn't exist (needed for queries that filter by meta_key alone)
68 if (!in_array('meta_key', $existingIndexes)) {
69 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQLPlaceholders.UnquotedComplexPlaceholder -- Migration file, adding index for performance, %1s is for identifier
70 $wpdb->query($wpdb->prepare("ALTER TABLE %1s ADD KEY `meta_key` (`meta_key`(191))", $table));
71 }
72 }
73 }
74