PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / trunk
Fluent Support – Helpdesk & Customer Support Ticket System vtrunk
2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 All 67 releases
fluent-support / database / Migrations / ProductsMigrator.php

ProductsMigrator.php in Fluent Support – Helpdesk & Customer Support Ticket System trunk, at database/Migrations/ProductsMigrator.php

81 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentSupport\Database\Migrations;
4
5 class ProductsMigrator
6 {
7 static $tableName = 'fs_products';
8
9 public static function migrate()
10 {
11 global $wpdb;
12
13 $charsetCollate = $wpdb->get_charset_collate();
14
15 $table = $wpdb->prefix . static::$tableName;
16
17 if ($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $table)) != $table) {
18 $sql = "CREATE TABLE $table (
19 `id` BIGINT(20) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
20 `source_uid` BIGINT(20) UNSIGNED NULL,
21 `mailbox_id` BIGINT(20) UNSIGNED NULL,
22 `title` VARCHAR(192) NULL,
23 `description` TEXT NULL,
24 `settings` LONGTEXT NULL,
25 `source` VARCHAR(100) DEFAULT 'local',
26 `created_by` BIGINT(20) UNSIGNED NULL,
27 `created_at` TIMESTAMP NULL,
28 `updated_at` TIMESTAMP NULL,
29 INDEX `idx_mailbox_id` (`mailbox_id`),
30 INDEX `idx_created_at` (`created_at`),
31 INDEX `idx_source` (`source`)
32 ) $charsetCollate;";
33 $created = dbDelta($sql);
34 return $created;
35 } else {
36 static::alterTable($table);
37 }
38
39 return false;
40 }
41
42 public static function alterTable($table)
43 {
44 static::addMissingIndexes($table);
45 }
46
47 public static function addMissingIndexes($table)
48 {
49 global $wpdb;
50
51 // $table is always $wpdb->prefix . 'fs_products' — not user input.
52 // esc_sql() is the correct escaping for SQL identifiers; $wpdb->prepare()
53 // cannot quote identifiers in WP < 6.2 (no %i placeholder available).
54 $table = esc_sql($table);
55
56 // Get existing indexes
57 $existing_indexes = $wpdb->get_results("SHOW INDEX FROM `{$table}`"); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
58 $existing_index_names = [];
59
60 foreach ($existing_indexes as $index) {
61 $existing_index_names[] = $index->Key_name;
62 }
63
64 // Desired indexes — keys and values are all hardcoded string literals.
65 $indexes = [
66 'idx_mailbox_id' => 'mailbox_id',
67 'idx_created_at' => 'created_at',
68 'idx_source' => 'source',
69 ];
70
71 // Add missing indexes. $table is esc_sql()'d above; $index_name and
72 // $column_name are hardcoded array literals — no user input reaches this query.
73 foreach ($indexes as $index_name => $column_name) {
74 if (!in_array($index_name, $existing_index_names)) {
75 // phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter,WordPress.DB.PreparedSQL.NotPrepared -- all identifiers are either esc_sql()'d or hardcoded literals.
76 $wpdb->query("ALTER TABLE `{$table}` ADD INDEX `{$index_name}` (`{$column_name}`)");
77 }
78 }
79 }
80 }
81