| @@ -13,9 +13,9 @@ | ||
| 13 | 13 | $charsetCollate = $wpdb->get_charset_collate(); |
| 14 | 14 | |
| 15 | 15 | $table = $wpdb->prefix . static::$tableName; |
| 16 | 16 | |
| 17 | - if ($wpdb->get_var("SHOW TABLES LIKE '$table'") != $table) { | |
| 17 | + if ($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $table)) != $table) { | |
| 18 | 18 | $sql = "CREATE TABLE $table ( |
| 19 | 19 | `id` BIGINT(20) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, |
| 20 | 20 | `object_type` VARCHAR(192) NULL, |
| 21 | 21 | `object_id` BIGINT(20) NULL, |
| @@ -21,10 +21,62 @@ | ||
| 21 | 21 | `object_id` BIGINT(20) NULL, |
| 22 | 22 | `key` VARCHAR(192) NULL, |
| 23 | 23 | `value` LONGTEXT NULL, |
| 24 | 24 | `created_at` TIMESTAMP NULL, |
| 25 | - `updated_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`) | |
| 26 | 30 | ) $charsetCollate;"; |
| 27 | - dbDelta($sql); | |
| 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 | + } | |
| 28 | 80 | } |
| 29 | 81 | } |
| 30 | 82 | } |