| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\Database\Migrations; |
| 4 |
|
| 5 |
class FormAnalytics |
| 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_analytics'; |
| 19 |
|
| 20 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- 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 |
`user_id` INT UNSIGNED NULL, |
| 27 |
`source_url` TEXT NOT NULL, |
| 28 |
`platform` CHAR(30) NULL, |
| 29 |
`browser` CHAR(30) NULL, |
| 30 |
`city` VARCHAR (100) NULL, |
| 31 |
`country` VARCHAR (100) NULL, |
| 32 |
`ip` CHAR(15) NULL, |
| 33 |
`count` INT DEFAULT 1, |
| 34 |
`created_at` TIMESTAMP NULL, |
| 35 |
PRIMARY KEY (`id`), |
| 36 |
KEY `form_id_ip` (`form_id`, `ip`), |
| 37 |
KEY `created_at` (`created_at`)) $charsetCollate;"; |
| 38 |
|
| 39 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 40 |
|
| 41 |
dbDelta($sql); |
| 42 |
} else { |
| 43 |
// increase column type of source_url from varchar to text - for already installed sites |
| 44 |
$column_name = 'source_url'; |
| 45 |
$dataType = $wpdb->get_col_length($table, $column_name); |
| 46 |
$type = $dataType['type'] ?? false; |
| 47 |
if ($type == 'char') { |
| 48 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Migration file, table/column names cannot be prepared |
| 49 |
$sql = "ALTER TABLE {$table} MODIFY {$column_name} TEXT NOT NULL"; |
| 50 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Migration file, schema change is the purpose, table/column names are safe |
| 51 |
$wpdb->query($sql); |
| 52 |
} |
| 53 |
|
| 54 |
// Add indexes to existing tables |
| 55 |
self::maybeAddIndexes(); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Add indexes to existing tables for better query performance. |
| 61 |
* |
| 62 |
* @return void |
| 63 |
*/ |
| 64 |
private static function maybeAddIndexes() |
| 65 |
{ |
| 66 |
global $wpdb; |
| 67 |
|
| 68 |
$table = $wpdb->prefix . 'fluentform_form_analytics'; |
| 69 |
|
| 70 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQLPlaceholders.UnquotedComplexPlaceholder -- Migration file, checking indexes, %1s is for identifier |
| 71 |
$indexes = $wpdb->get_results($wpdb->prepare("SHOW INDEX FROM %1s", $table), ARRAY_A); |
| 72 |
|
| 73 |
$existingIndexes = []; |
| 74 |
foreach ($indexes as $index) { |
| 75 |
$existingIndexes[] = $index['Key_name']; |
| 76 |
} |
| 77 |
|
| 78 |
// Add composite index form_id + ip if it doesn't exist |
| 79 |
if (!in_array('form_id_ip', $existingIndexes)) { |
| 80 |
// 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 |
| 81 |
$wpdb->query($wpdb->prepare("ALTER TABLE %1s ADD KEY `form_id_ip` (`form_id`, `ip`)", $table)); |
| 82 |
} |
| 83 |
|
| 84 |
// Add created_at index if it doesn't exist |
| 85 |
if (!in_array('created_at', $existingIndexes)) { |
| 86 |
// 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 |
| 87 |
$wpdb->query($wpdb->prepare("ALTER TABLE %1s ADD KEY `created_at` (`created_at`)", $table)); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|