| @@ -24,9 +24,10 @@ | ||
| 24 | 24 | `created_at` TIMESTAMP NULL, |
| 25 | 25 | `updated_at` TIMESTAMP NULL, |
| 26 | 26 | INDEX `idx_object_type` (`object_type`), |
| 27 | 27 | INDEX `idx_object_id` (`object_id`), |
| 28 | - INDEX `idx_key` (`key`) | |
| 28 | + INDEX `idx_key` (`key`), | |
| 29 | + INDEX `idx_object_type_created_at` (`object_type`, `created_at`) | |
| 29 | 30 | ) $charsetCollate;"; |
| 30 | 31 | $created = dbDelta($sql); |
| 31 | 32 | return $created; |
| 32 | 33 | } else { |
| @@ -59,19 +60,23 @@ | ||
| 59 | 60 | } |
| 60 | 61 | |
| 61 | 62 | // Desired indexes — keys and values are all hardcoded string literals. |
| 62 | 63 | $indexes = [ |
| 63 | - 'idx_object_type' => 'object_type', | |
| 64 | - 'idx_object_id' => 'object_id', | |
| 65 | - 'idx_key' => 'key', | |
| 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'], | |
| 66 | 70 | ]; |
| 67 | 71 | |
| 68 | 72 | // Add missing indexes. $table is esc_sql()'d above; $index_name and |
| 69 | - // $column_name are hardcoded array literals — no user input reaches this query. | |
| 70 | - foreach ($indexes as $index_name => $column_name) { | |
| 73 | + // $columns are hardcoded array literals — no user input reaches this query. | |
| 74 | + foreach ($indexes as $index_name => $columns) { | |
| 71 | 75 | if (!in_array($index_name, $existing_index_names)) { |
| 76 | + $columnList = '`' . implode('`, `', $columns) . '`'; | |
| 72 | 77 | // phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter,WordPress.DB.PreparedSQL.NotPrepared -- all identifiers are either esc_sql()'d or hardcoded literals. |
| 73 | - $wpdb->query("ALTER TABLE `{$table}` ADD INDEX `{$index_name}` (`{$column_name}`)"); | |
| 78 | + $wpdb->query("ALTER TABLE `{$table}` ADD INDEX `{$index_name}` ({$columnList})"); | |
| 74 | 79 | } |
| 75 | 80 | } |
| 76 | 81 | } |
| 77 | 82 | } |