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 / FormAnalytics.php

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

91 lines 4.1 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 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