prefix . 'tp_translation_groups'; } /** * Row for an object, or null. */ public static function row( string $type, int $id ): ?array { if ( $id <= 0 ) { return null; } $key = $type . ':' . $id; $found = false; $row = wp_cache_get( $key, self::CACHE_GROUP, false, $found ); if ( $found ) { return $row ?: null; } global $wpdb; $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM %i WHERE object_type = %s AND object_id = %d", $wpdb->tp_translation_groups, $type, $id ), ARRAY_A ); // phpcs:ignore WordPress.DB wp_cache_set( $key, $row ?: 0, self::CACHE_GROUP ); return $row ?: null; } public static function lang_of( string $type, int $id ): string { $row = self::row( $type, $id ); return $row ? (string) $row['lang'] : Languages::source(); } public static function is_translation( string $type, int $id ): bool { $row = self::row( $type, $id ); return $row && (int) $row['group_id'] !== (int) $row['object_id']; } public static function source_id( string $type, int $id ): int { $row = self::row( $type, $id ); return $row ? (int) $row['group_id'] : $id; } /** * All members of the object's group. * * @return array lang => object id (source included). */ public static function translations( string $type, int $id ): array { $group = self::source_id( $type, $id ); $key = 'g:' . $type . ':' . $group; $found = false; $map = wp_cache_get( $key, self::CACHE_GROUP, false, $found ); if ( ! $found ) { global $wpdb; $rows = $wpdb->get_results( $wpdb->prepare( "SELECT lang, object_id FROM %i WHERE object_type = %s AND group_id = %d", $wpdb->tp_translation_groups, $type, $group ), ARRAY_A ); // phpcs:ignore WordPress.DB $map = array(); foreach ( $rows as $r ) { $map[ $r['lang'] ] = (int) $r['object_id']; } wp_cache_set( $key, $map, self::CACHE_GROUP ); } if ( ! $map ) { $map = array( Languages::source() => $group ); } return $map; } public static function translation( string $type, int $id, string $lang ): int { $map = self::translations( $type, $id ); return (int) ( $map[ $lang ] ?? 0 ); } /** * Group rows (with status) for a source object, keyed by language. * * @return array */ public static function members( string $type, int $source_id ): array { global $wpdb; $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM %i WHERE object_type = %s AND group_id = %d", $wpdb->tp_translation_groups, $type, $source_id ), ARRAY_A ); // phpcs:ignore WordPress.DB $out = array(); foreach ( $rows as $r ) { $out[ $r['lang'] ] = $r; } return $out; } /** * Ensure the source object has its own row. */ public static function ensure_source( string $type, int $source_id, string $lang = '' ): void { if ( self::row( $type, $source_id ) ) { return; } self::upsert( $type, $source_id, $source_id, $lang ?: Languages::source(), self::STATUS_SOURCE ); } public static function link( string $type, int $source_id, int $translated_id, string $lang, string $status = self::STATUS_TRANSLATED, string $source_hash = '' ): void { self::ensure_source( $type, $source_id ); self::upsert( $type, $source_id, $translated_id, $lang, $status, $source_hash ); } public static function set_status( string $type, int $id, string $status, ?string $source_hash = null ): void { global $wpdb; $data = array( 'status' => $status, 'updated_at' => current_time( 'mysql', true ), ); if ( null !== $source_hash ) { $data['source_hash'] = $source_hash; } $wpdb->update( self::table(), $data, array( 'object_type' => $type, 'object_id' => $id ) ); // phpcs:ignore WordPress.DB self::flush( $type, $id ); } /** * Mark every translation of a source as outdated when its content hash changed. */ public static function mark_outdated( string $type, int $source_id, string $new_hash ): int { global $wpdb; $n = (int) $wpdb->query( // phpcs:ignore WordPress.DB $wpdb->prepare( "UPDATE %i SET status = %s WHERE object_type = %s AND group_id = %d AND object_id <> group_id AND source_hash <> %s AND status IN ('translated','review')", $wpdb->tp_translation_groups, self::STATUS_OUTDATED, $type, $source_id, $new_hash ) ); self::flush_group( $type, $source_id ); return $n; } public static function delete_object( string $type, int $id ): void { global $wpdb; $row = self::row( $type, $id ); if ( ! $row ) { return; } // Translations of a deleted source keep their rows, so they keep their language. $wpdb->delete( self::table(), array( 'object_type' => $type, 'object_id' => $id ) ); // phpcs:ignore WordPress.DB self::flush_group( $type, (int) $row['group_id'] ); self::flush( $type, $id ); } /** * Language coverage: share of source objects of a post type translated per language. * * @return array */ public static function coverage( array $post_types ): array { global $wpdb; $types = array_values( $post_types ); if ( ! $types ) { return array(); } $type_list = implode( ',', $types ); // phpcs:disable WordPress.DB.DirectDatabaseQuery $total = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i p LEFT JOIN %i g ON g.object_type = 'post' AND g.object_id = p.ID WHERE INSTR( CONCAT( ',', %s, ',' ), CONCAT( ',', p.post_type, ',' ) ) > 0 AND p.post_status = 'publish' AND ( g.id IS NULL OR g.group_id = g.object_id )", $wpdb->posts, $wpdb->tp_translation_groups, $type_list ) ); $rows = $wpdb->get_results( $wpdb->prepare( "SELECT g.lang, g.status, COUNT(*) n FROM %i g JOIN %i p ON p.ID = g.object_id WHERE g.object_type = 'post' AND g.group_id <> g.object_id AND INSTR( CONCAT( ',', %s, ',' ), CONCAT( ',', p.post_type, ',' ) ) > 0 AND p.post_status IN ('publish','draft','pending') GROUP BY g.lang, g.status", $wpdb->tp_translation_groups, $wpdb->posts, $type_list ), ARRAY_A ); // phpcs:enable $out = array(); foreach ( Languages::targets() as $lang ) { $out[ $lang ] = array( 'translated' => 0, 'outdated' => 0, 'review' => 0, 'total' => $total ); } foreach ( $rows as $r ) { if ( ! isset( $out[ $r['lang'] ] ) ) { continue; } if ( self::STATUS_OUTDATED === $r['status'] ) { $out[ $r['lang'] ]['outdated'] += (int) $r['n']; } elseif ( self::STATUS_REVIEW === $r['status'] ) { $out[ $r['lang'] ]['review'] += (int) $r['n']; } $out[ $r['lang'] ]['translated'] += (int) $r['n']; } return $out; } private static function upsert( string $type, int $group_id, int $object_id, string $lang, string $status, string $source_hash = '' ): void { global $wpdb; $wpdb->query( // phpcs:ignore WordPress.DB $wpdb->prepare( "INSERT INTO %i (group_id, object_type, object_id, lang, source_hash, status, updated_at) VALUES (%d, %s, %d, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE group_id = VALUES(group_id), lang = VALUES(lang), source_hash = VALUES(source_hash), status = VALUES(status), updated_at = VALUES(updated_at)", $wpdb->tp_translation_groups, $group_id, $type, $object_id, $lang, $source_hash, $status, current_time( 'mysql', true ) ) ); self::flush( $type, $object_id ); self::flush_group( $type, $group_id ); } private static function flush( string $type, int $id ): void { wp_cache_delete( $type . ':' . $id, self::CACHE_GROUP ); } private static function flush_group( string $type, int $group_id ): void { wp_cache_delete( 'g:' . $type . ':' . $group_id, self::CACHE_GROUP ); } }