dbCore = $dbCore; } /** * Insert a lookup value (or fetch its existing id) and return the id. * * @param string $valueToInsert * @return int */ public function insertLookupValueAndGetID($valueToInsert) { $query = "INSERT INTO {wp_abj404_lookup} (lkup_value) VALUES (%s) ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id)"; $result = $this->dbCore->queryAndGetResults($query, array('query_params' => array($valueToInsert))); // The query envelope snapshots native mutation metadata before nested // diagnostics can replace wpdb's fields. It also handles timeout-wrapped // INSERTs that WordPress does not recognize as mutations. if (!empty($result['last_error'])) { return 0; } $insertId = $result['insert_id'] ?? 0; return is_scalar($insertId) ? intval($insertId) : 0; } /** * Reverse-lookup an id for an existing value. Returns -1 if missing. * * @param string $userName * @return int */ public function getLookupIDForUser($userName) { // allow-unbounded-select: single lkup_value equality; only the first row is used $query = "select id from {wp_abj404_lookup} where lkup_value = %s"; $results = $this->dbCore->queryAndGetResults($query, array('query_params' => array($userName))); $lookupRows = is_array($results['rows']) ? $results['rows'] : array(); if (count($lookupRows) > 0) { $row1 = is_array($lookupRows[0]) ? $lookupRows[0] : array(); $id = isset($row1['id']) ? $row1['id'] : 0; return is_scalar($id) ? intval($id) : 0; } return -1; } /** * Repair the lookup table after duplicate lkup_value rows accumulate * (pre-4.1.8 installs could insert duplicates before the unique key). * Runs with log_errors=false / skip_repair=true so a missing table during * fresh-install doesn't spam the debug log or trigger repair recursion. * * @return void */ public function correctDuplicateLookupValues(): void { $query = ABJ_404_Solution_FileSystemService::readFileContents(__DIR__ . "/../sql/correctLookupTableIssue.sql"); $this->dbCore->queryAndGetResults($query, array('log_errors' => false, 'skip_repair' => true)); } }