prefix . 'tp_segments'; } /** * @return array segment_key => row. */ public static function get( string $type, int $source_id, string $lang ): array { global $wpdb; $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM %i WHERE object_type = %s AND object_id = %d AND lang = %s", $wpdb->tp_segments, $type, $source_id, $lang ), ARRAY_A ); // phpcs:ignore WordPress.DB $out = array(); foreach ( $rows as $r ) { $out[ $r['segment_key'] ] = $r; } return $out; } /** * Replace the object's segment set with the current source segments. * * Segments whose source text is unchanged keep their status and origin, so * reviewed translations stay reviewed. * * @param array $sources key => source text. * @param array $translations key => translation. * @param string[] $missing Keys with no translation yet (stored empty). */ public static function sync( string $type, int $source_id, string $lang, array $sources, array $translations, array $missing = array() ): void { $missing = array_flip( $missing ); global $wpdb; $existing = self::get( $type, $source_id, $lang ); $now = current_time( 'mysql', true ); $keep = array(); foreach ( $sources as $key => $src ) { if ( ! Translator::is_translatable( (string) $src ) ) { continue; } $key = (string) $key; $hash = md5( (string) $src ); $tr = isset( $missing[ $key ] ) ? '' : (string) ( $translations[ $key ] ?? $src ); $old = $existing[ $key ] ?? null; $status = self::STATUS_AUTO; $origin = self::ORIGIN_AUTO; $by = 0; if ( isset( $missing[ $key ] ) ) { $status = 'missing'; } elseif ( $old && $old['source_hash'] === $hash ) { $status = $old['status']; $origin = $old['origin']; $by = (int) $old['updated_by']; } $keep[] = $key; $wpdb->query( // phpcs:ignore WordPress.DB $wpdb->prepare( "INSERT INTO %i (object_type, object_id, lang, segment_key, source_hash, source, translation, status, origin, updated_by, updated_at) VALUES (%s, %d, %s, %s, %s, %s, %s, %s, %s, %d, %s) ON DUPLICATE KEY UPDATE source_hash = VALUES(source_hash), source = VALUES(source), translation = VALUES(translation), status = VALUES(status), origin = VALUES(origin), updated_by = VALUES(updated_by), updated_at = VALUES(updated_at)", $wpdb->tp_segments, $type, $source_id, $lang, substr( $key, 0, 191 ), $hash, (string) $src, $tr, $status, $origin, $by, $now ) ); } // Drop segments that no longer exist in the source. $stale = array_diff( array_keys( $existing ), $keep ); foreach ( $stale as $key ) { $wpdb->delete( self::table(), array( 'object_type' => $type, 'object_id' => $source_id, 'lang' => $lang, 'segment_key' => $key ) ); // phpcs:ignore WordPress.DB } } /** * Save manual edits from the editor. * * @param array $edits Edits. * @param array|null $previous Segment rows from before the post was rebuilt, used to tell * real edits from plain approvals. * @param bool $to_memory Also store the text in translation memory. False for * suggestions that still need a reviewer's approval. */ public static function save_manual( string $type, int $source_id, string $lang, array $edits, string $source_lang, ?array $previous = null, bool $to_memory = true ): int { global $wpdb; $existing = self::get( $type, $source_id, $lang ); $previous = null === $previous ? $existing : $previous; $pairs = array(); $n = 0; foreach ( $edits as $edit ) { $key = (string) ( $edit['key'] ?? '' ); if ( ! isset( $existing[ $key ] ) ) { continue; } $tr = (string) ( $edit['translation'] ?? '' ); $status = in_array( $edit['status'] ?? '', array( self::STATUS_AUTO, self::STATUS_REVIEWED ), true ) ? $edit['status'] : self::STATUS_REVIEWED; $wpdb->update( // phpcs:ignore WordPress.DB self::table(), array( 'translation' => $tr, 'status' => $status, 'origin' => isset( $previous[ $key ] ) && $tr === $previous[ $key ]['translation'] ? $previous[ $key ]['origin'] : self::ORIGIN_MANUAL, 'updated_by' => get_current_user_id(), 'updated_at' => current_time( 'mysql', true ), ), array( 'id' => (int) $existing[ $key ]['id'] ) ); $pairs[ $existing[ $key ]['source'] ] = $tr; $n++; } // Manual translations go to memory so they win everywhere the text reappears. if ( $to_memory ) { Memory::save( $pairs, $source_lang, $lang ); } return $n; } /** * Store the object's current segment translations in memory, so a rebuild * applies them (used when a reviewer approves suggested changes). */ public static function to_memory( string $type, int $source_id, string $lang, string $source_lang ): void { $pairs = array(); foreach ( self::get( $type, $source_id, $lang ) as $row ) { if ( '' !== (string) $row['translation'] ) { $pairs[ (string) $row['source'] ] = (string) $row['translation']; } } Memory::save( $pairs, $source_lang, $lang ); } public static function mark_reviewed( string $type, int $source_id, string $lang ): void { global $wpdb; $wpdb->update( self::table(), array( 'status' => self::STATUS_REVIEWED ), array( 'object_type' => $type, 'object_id' => $source_id, 'lang' => $lang ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery } public static function delete_object( string $type, int $source_id ): void { global $wpdb; $wpdb->delete( self::table(), array( 'object_type' => $type, 'object_id' => $source_id ) ); // phpcs:ignore WordPress.DB } /** * @return array{total:int, reviewed:int, manual:int} */ public static function summary( string $type, int $source_id, string $lang ): array { global $wpdb; $row = $wpdb->get_row( $wpdb->prepare( "SELECT COUNT(*) total, SUM(status = 'reviewed') reviewed, SUM(origin = 'manual') manual FROM %i WHERE object_type = %s AND object_id = %d AND lang = %s", $wpdb->tp_segments, $type, $source_id, $lang ), ARRAY_A ); // phpcs:ignore WordPress.DB return array( 'total' => (int) ( $row['total'] ?? 0 ), 'reviewed' => (int) ( $row['reviewed'] ?? 0 ), 'manual' => (int) ( $row['manual'] ?? 0 ), ); } }