" . $this->getIndexName() . "", "" . $this->getTableName() . "" ); } public function run(): bool { if ($this->shouldRun()) { $sql = sprintf( 'CREATE INDEX %s ON %s (%s)', $this->getIndexName(), $this->getTableName(), implode(', ', $this->getIndexColumns()) ); $result = $this->wpdb->query($sql); if (false === $result) { // Optionally log error: $this->wpdb->last_error return false; } } return true; } public function shouldRun(): bool { // Use backticks for table name to avoid issues with %i placeholder $table_name = $this->getTableName(); $exists = $this->wpdb->get_var($this->wpdb->prepare( sprintf('SHOW INDEX FROM `%s` WHERE Key_name = %%s', $table_name), $this->getIndexName() )); return empty($exists); } public function getDiagnostics(): array { return [ [ 'key' => $this->getIndexName(), 'exists' => !$this->shouldRun(), 'table' => $this->getTableName(), 'columns' => implode(', ', $this->getIndexColumns()), ] ]; } }