posts = $posts; } /** * @param int[] $ids Source post IDs (empty = all of the given types). * @return array */ public function units( string $lang, array $ids = array(), array $types = array(), bool $only_missing = false ): array { if ( ! $ids ) { $ids = get_posts( array( 'post_type' => $types ?: PostTypes::enabled(), 'post_status' => array( 'publish', 'future', 'private', 'draft' ), 'posts_per_page' => 2000, 'fields' => 'ids', 'tp_lang' => Languages::source(), ) ); } // Only content the current user may access is exported. $ids = array_filter( array_unique( array_map( static function ( $id ) { return Groups::source_id( 'post', (int) $id ); }, $ids ) ), array( Roles::class, 'can_access_post' ) ); $units = array(); foreach ( $ids as $id ) { $post = get_post( $id ); if ( ! $post ) { continue; } $segments = array_filter( $this->posts->collect( $post ), array( Translator::class, 'is_translatable' ) ); $stored = Segments::get( 'post', $id, $lang ); $memory = Memory::lookup( array_values( $segments ), Languages::source(), $lang ); foreach ( $segments as $key => $text ) { $row = $stored[ $key ] ?? null; $target = ( $row && $row['source_hash'] === md5( $text ) ) ? $row['translation'] : ( $memory[ $text ] ?? '' ); if ( $only_missing && '' !== $target ) { continue; } $units[] = array( 'id' => 'post:' . $id . ':' . $key, 'source' => $text, 'target' => $target, 'note' => get_the_title( $id ), ); } } return $units; } public function export_xliff( string $lang, array $ids = array(), array $types = array(), bool $only_missing = false ): string { $units = $this->units( $lang, $ids, $types, $only_missing ); $x = new \XMLWriter(); $x->openMemory(); $x->setIndent( true ); $x->startDocument( '1.0', 'UTF-8' ); $x->startElement( 'xliff' ); $x->writeAttribute( 'version', '1.2' ); $x->writeAttribute( 'xmlns', 'urn:oasis:names:tc:xliff:document:1.2' ); $x->startElement( 'file' ); $x->writeAttribute( 'original', home_url( '/' ) ); $x->writeAttribute( 'source-language', Languages::bcp47( Languages::source() ) ); $x->writeAttribute( 'target-language', Languages::bcp47( $lang ) ); $x->writeAttribute( 'datatype', 'html' ); $x->writeAttribute( 'tool-id', 'translateplus' ); $x->startElement( 'body' ); foreach ( $units as $u ) { $x->startElement( 'trans-unit' ); $x->writeAttribute( 'id', $u['id'] ); $x->startElement( 'source' ); $x->writeCdata( $u['source'] ); $x->endElement(); $x->startElement( 'target' ); $x->writeCdata( $u['target'] ); $x->endElement(); $x->writeElement( 'note', $u['note'] ); $x->endElement(); } $x->endElement(); $x->endElement(); $x->endElement(); $x->endDocument(); return $x->outputMemory(); } public function export_csv( string $lang, array $ids = array(), array $types = array(), bool $only_missing = false ): string { $fh = fopen( 'php://temp', 'w+' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen fputcsv( $fh, array( 'id', 'source', 'target', 'note' ) ); foreach ( $this->units( $lang, $ids, $types, $only_missing ) as $u ) { fputcsv( $fh, array( $u['id'], $u['source'], $u['target'], $u['note'] ) ); } rewind( $fh ); $out = stream_get_contents( $fh ); fclose( $fh ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose return (string) $out; } /** * Import translations and rebuild affected posts. * * @return array{units:int, posts:int, pending_review:int, errors:string[]} */ public function import( string $content, string $lang ): array { $units = $this->parse( $content ); $filter = ! current_user_can( 'unfiltered_html' ); $by = array(); $known = array(); // post ID => segment key => source text, read from the post itself. foreach ( $units as $u ) { if ( ! preg_match( '/^post:(\d+):(.+)$/', $u['id'], $m ) || '' === trim( $u['target'] ) ) { continue; } $post_id = Groups::source_id( 'post', (int) $m[1] ); if ( ! isset( $known[ $post_id ] ) ) { $known[ $post_id ] = Roles::can_access_post( $post_id ) ? $this->posts->collect( get_post( $post_id ) ) : array(); } // Only segments that exist in the post are imported, with the post's own source // text: a file can never add memory entries for arbitrary text. if ( ! isset( $known[ $post_id ][ $m[2] ] ) ) { continue; } $by[ $post_id ][] = array( 'key' => $m[2], 'source' => (string) $known[ $post_id ][ $m[2] ], 'translation' => $filter ? wp_kses_post( $u['target'] ) : $u['target'], ); } $errors = array(); $posts = 0; $n = 0; $review = new Review( $this->posts ); $pending = 0; foreach ( $by as $post_id => $edits ) { try { // Same rules as the editor: without review rights, published translations get suggestions only. $result = $review->save_edits( $post_id, $lang, $edits ); $posts++; $n += $result['saved']; $pending += $result['pending_review'] ? 1 : 0; } catch ( \Throwable $e ) { /* translators: 1: post ID, 2: error */ $errors[] = sprintf( __( 'Post %1$d: %2$s', 'translateplus' ), $post_id, $e->getMessage() ); } } return array( 'units' => $n, 'posts' => $posts, 'pending_review' => $pending, 'errors' => $errors, ); } /** * @return array */ private function parse( string $content ): array { $content = ltrim( $content, "\xEF\xBB\xBF" ); $units = array(); if ( 0 === strpos( ltrim( $content ), '<' ) ) { $prev = libxml_use_internal_errors( true ); $xml = simplexml_load_string( $content, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NONET ); libxml_use_internal_errors( $prev ); if ( ! $xml ) { return array(); } $xml->registerXPathNamespace( 'x', 'urn:oasis:names:tc:xliff:document:1.2' ); $nodes = $xml->xpath( '//x:trans-unit' ) ?: $xml->xpath( '//trans-unit' ); foreach ( (array) $nodes as $node ) { $units[] = array( 'id' => (string) $node['id'], 'source' => (string) $node->source, 'target' => (string) $node->target, ); } return $units; } $fh = fopen( 'php://temp', 'w+' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen fwrite( $fh, $content ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite rewind( $fh ); $header = fgetcsv( $fh ); $cols = array_flip( array_map( 'strtolower', (array) $header ) ); while ( ( $row = fgetcsv( $fh ) ) !== false ) { $units[] = array( 'id' => (string) ( $row[ $cols['id'] ?? 0 ] ?? '' ), 'source' => (string) ( $row[ $cols['source'] ?? 1 ] ?? '' ), 'target' => (string) ( $row[ $cols['target'] ?? 2 ] ?? '' ), ); } fclose( $fh ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose return $units; } }