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

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

83 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 FluentSupport\Database\Migrations;
4
5 class MetaMigrator
6 {
7 static $tableName = 'fs_meta';
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 `object_type` VARCHAR(192) NULL,
21 `object_id` BIGINT(20) NULL,
22 `key` VARCHAR(192) NULL,
23 `value` LONGTEXT NULL,
24 `created_at` TIMESTAMP NULL,
25 `updated_at` TIMESTAMP NULL,
26 INDEX `idx_object_type` (`object_type`),
27 INDEX `idx_object_id` (`object_id`),
28 INDEX `idx_key` (`key`),
29 INDEX `idx_object_type_created_at` (`object_type`, `created_at`)
30 ) $charsetCollate;";
31 $created = dbDelta($sql);
32 return $created;
33 } else {
34 static::alterTable($table);
35 }
36
37 return false;
38 }
39
40 public static function alterTable($table)
41 {
42 static::addMissingIndexes($table);
43 }
44
45 public static function addMissingIndexes($table)
46 {
47 global $wpdb;
48
49 // $table is always $wpdb->prefix . 'fs_meta' — not user input.
50 // esc_sql() is the correct escaping for SQL identifiers; $wpdb->prepare()
51 // cannot quote identifiers in WP < 6.2 (no %i placeholder available).
52 $table = esc_sql($table);
53
54 // Get existing indexes
55 $existing_indexes = $wpdb->get_results("SHOW INDEX FROM `{$table}`"); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
56 $existing_index_names = [];
57
58 foreach ($existing_indexes as $index) {
59 $existing_index_names[] = $index->Key_name;
60 }
61
62 // Desired indexes — keys and values are all hardcoded string literals.
63 $indexes = [
64 'idx_object_type' => ['object_type'],
65 'idx_object_id' => ['object_id'],
66 'idx_key' => ['key'],
67 // Serves CleanupHandler::cleanExpiredAuthChallenges(), which purges
68 // rows by object_type + created_at on every hourly cron run.
69 'idx_object_type_created_at' => ['object_type', 'created_at'],
70 ];
71
72 // Add missing indexes. $table is esc_sql()'d above; $index_name and
73 // $columns are hardcoded array literals — no user input reaches this query.
74 foreach ($indexes as $index_name => $columns) {
75 if (!in_array($index_name, $existing_index_names)) {
76 $columnList = '`' . implode('`, `', $columns) . '`';
77 // phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter,WordPress.DB.PreparedSQL.NotPrepared -- all identifiers are either esc_sql()'d or hardcoded literals.
78 $wpdb->query("ALTER TABLE `{$table}` ADD INDEX `{$index_name}` ({$columnList})");
79 }
80 }
81 }
82 }
83