prefix . 'aipt_blueprint_decisions'; } /** * Record (or update) a conflict-acceptance decision. * * Tries INSERT first; a duplicate-key failure on the UNIQUE tuple means * the user is re-deciding the SAME conflict (identical subject, field * and competing versions) — e.g. changing which value they picked * without the underlying content having changed at all — so that case * UPDATEs the existing row in place rather than erroring. A genuinely * different `versions_hash` (because a source was refreshed) always * lands as a brand-new row, never this UPDATE path. * * @param array $row { * @type string $subjectType Required. * @type int $subjectId Required. * @type string $fieldKey Required. * @type string $chosenRef 'product' or 'src:'. * @type string $chosenValueHash Default ''. * @type string $versionsHash Required. * @type int $decidedBy Default current user. * } * @return bool */ public function decide( array $row ): bool { global $wpdb; $table = self::table(); $now = current_time( 'mysql', true ); $user = isset( $row['decidedBy'] ) ? (int) $row['decidedBy'] : (int) ( function_exists( 'get_current_user_id' ) ? get_current_user_id() : 0 ); $subject_type = (string) ( $row['subjectType'] ?? '' ); $subject_id = (int) ( $row['subjectId'] ?? 0 ); $field_key = (string) ( $row['fieldKey'] ?? '' ); $versions_hash = (string) ( $row['versionsHash'] ?? '' ); $chosen_ref = (string) ( $row['chosenRef'] ?? '' ); $chosen_hash = (string) ( $row['chosenValueHash'] ?? '' ); $previous_suppress = $wpdb->suppress_errors( true ); $inserted = $wpdb->insert( $table, array( 'subject_type' => $subject_type, 'subject_id' => $subject_id, 'field_key' => $field_key, 'chosen_ref' => $chosen_ref, 'chosen_value_hash' => $chosen_hash, 'versions_hash' => $versions_hash, 'decided_by' => $user, 'decided_at' => $now, ), array( '%s', '%d', '%s', '%s', '%s', '%s', '%d', '%s' ) ); $wpdb->suppress_errors( $previous_suppress ); if ( false !== $inserted ) { return true; } $last_error = (string) $wpdb->last_error; if ( false === strpos( $last_error, 'Duplicate entry' ) && false === strpos( $last_error, '1062' ) ) { return false; } $affected = $wpdb->update( $table, array( 'chosen_ref' => $chosen_ref, 'chosen_value_hash' => $chosen_hash, 'decided_by' => $user, 'decided_at' => $now, ), array( 'subject_type' => $subject_type, 'subject_id' => $subject_id, 'field_key' => $field_key, 'versions_hash' => $versions_hash, ), array( '%s', '%s', '%d', '%s' ), array( '%s', '%d', '%s', '%s' ) ); return false !== $affected; } /** * Find the decision bound to this exact subject/field/versions tuple. * * A caller whose current competing versions_hash no longer matches any * stored row correctly gets null back — the conflict is then * re-evaluated as undecided. * * @param string $subject_type 'product'|'term'|'attachment'. * @param int $subject_id Subject id. * @param string $field_key Field label key. * @param string $versions_hash Hash of the competing source content versions. * @return array|null */ public function find( string $subject_type, int $subject_id, string $field_key, string $versions_hash ): ?array { global $wpdb; $table = self::table(); $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$table} WHERE subject_type = %s AND subject_id = %d AND field_key = %s AND versions_hash = %s", $subject_type, $subject_id, $field_key, $versions_hash ), ARRAY_A ); return is_array( $row ) ? $this->mapRow( $row ) : null; } /** * Every decision recorded for one subject, regardless of field or * versions_hash. * * @param string $subject_type 'product'|'term'|'attachment'. * @param int $subject_id Subject id. * @return array> */ public function listForSubject( string $subject_type, int $subject_id ): array { global $wpdb; $table = self::table(); $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table} WHERE subject_type = %s AND subject_id = %d LIMIT %d", $subject_type, $subject_id, self::MAX_LIST_LIMIT ), ARRAY_A ); return is_array( $rows ) ? array_map( array( $this, 'mapRow' ), $rows ) : array(); } /** * Delete every decision for one subject (e.g. a permanently deleted * product or term). * * @param string $subject_type 'product'|'term'|'attachment'. * @param int $subject_id Subject id. * @return int Rows deleted. */ public function deleteForSubject( string $subject_type, int $subject_id ): int { global $wpdb; $table = self::table(); $result = $wpdb->delete( $table, array( 'subject_type' => $subject_type, 'subject_id' => $subject_id ), array( '%s', '%d' ) ); return false !== $result ? (int) $result : 0; } /** * Map a raw database row to the canonical camelCase response shape. * * @param array $row Database row. * @return array */ private function mapRow( array $row ): array { return array( 'id' => (int) ( $row['id'] ?? 0 ), 'subjectType' => (string) ( $row['subject_type'] ?? '' ), 'subjectId' => (int) ( $row['subject_id'] ?? 0 ), 'fieldKey' => (string) ( $row['field_key'] ?? '' ), 'chosenRef' => (string) ( $row['chosen_ref'] ?? '' ), 'chosenValueHash' => (string) ( $row['chosen_value_hash'] ?? '' ), 'versionsHash' => (string) ( $row['versions_hash'] ?? '' ), 'decidedBy' => (int) ( $row['decided_by'] ?? 0 ), 'decidedAt' => (string) ( $row['decided_at'] ?? '' ), ); } }