plugin = $plugin; } /** * Show account, languages and coverage. * * ## EXAMPLES * * wp translateplus status * * @subcommand status */ public function status(): void { try { $a = $this->plugin->api()->account_summary(); \WP_CLI::log( sprintf( 'Account: %s – %s of %s credits remaining', $a['email'] ?? '?', number_format( (int) ( $a['credits_remaining'] ?? 0 ) ), number_format( (int) ( $a['total_credits'] ?? 0 ) ) ) ); } catch ( ApiException $e ) { \WP_CLI::warning( $e->getMessage() ); } \WP_CLI::log( 'Source language: ' . Languages::source() ); $rows = array(); foreach ( Groups::coverage( \TranslatePlus\Core\PostTypes::enabled() ) as $lang => $c ) { $rows[] = array( 'language' => $lang, 'translated' => $c['translated'], 'outdated' => $c['outdated'], 'review' => $c['review'], 'total' => $c['total'], ); } \WP_CLI\Utils\format_items( 'table', $rows, array( 'language', 'translated', 'outdated', 'review', 'total' ) ); $m = Memory::stats(); \WP_CLI::log( sprintf( 'Translation memory: %d entries, %d hits, ~%s characters saved', $m['entries'], $m['hits'], number_format( $m['saved_chars'] ) ) ); } /** * Translate content. * * ## OPTIONS * * [--lang=] * : Comma-separated target languages. Default: all. * * [--post_type=] * : Comma-separated post types. Default: all translatable. * * [--ids=] * : Comma-separated post IDs. * * [--mode=] * : missing, update (missing + outdated) or all (force re-translate). * --- * default: update * --- * * [--dry-run] * : Only estimate characters. * * [--background] * : Queue a background job instead of translating now. * * ## EXAMPLES * * wp translateplus translate --lang=fr,de --post_type=page * wp translateplus translate --ids=12,15 --mode=all * * @param array $args Positional. * @param array $assoc_args Options. */ public function translate( $args, $assoc_args ): void { $spec = array( 'languages' => isset( $assoc_args['lang'] ) ? array_map( 'trim', explode( ',', $assoc_args['lang'] ) ) : Languages::targets(), 'post_types' => isset( $assoc_args['post_type'] ) ? array_map( 'trim', explode( ',', $assoc_args['post_type'] ) ) : \TranslatePlus\Core\PostTypes::enabled(), 'mode' => $assoc_args['mode'] ?? 'update', ); if ( isset( $assoc_args['ids'] ) ) { $spec['ids'] = array_map( 'intval', explode( ',', $assoc_args['ids'] ) ); } if ( isset( $assoc_args['dry-run'] ) ) { $est = $this->plugin->jobs()->estimate( $spec ); \WP_CLI::success( sprintf( '%d translations, about %s characters to send.', $est['tasks'], number_format( $est['chars'] ) ) ); return; } if ( isset( $assoc_args['background'] ) ) { $id = $this->plugin->jobs()->create( $spec ); \WP_CLI::success( sprintf( 'Queued job #%d.', $id ) ); return; } $tasks = Jobs::tasks( $spec ); $progress = \WP_CLI\Utils\make_progress_bar( 'Translating', count( $tasks ) ); $done = 0; $failed = 0; foreach ( $tasks as list( $post_id, $lang ) ) { try { $this->plugin->posts()->translate( $post_id, $lang, array( 'force' => 'all' === $spec['mode'] ) ); $done++; } catch ( ApiException $e ) { if ( ! $e->is_retryable() && ApiException::INVALID_INPUT !== $e->reason() ) { $progress->finish(); \WP_CLI::error( $e->getMessage() ); } $failed++; \WP_CLI::warning( sprintf( 'Post %d → %s: %s', $post_id, $lang, $e->getMessage() ) ); } catch ( \Throwable $e ) { $failed++; \WP_CLI::warning( sprintf( 'Post %d → %s: %s', $post_id, $lang, $e->getMessage() ) ); } $progress->tick(); } $progress->finish(); $stats = $this->plugin->translator()->stats(); \WP_CLI::success( sprintf( '%d translated, %d failed. %s characters sent, %s served from memory.', $done, $failed, number_format( $stats['api_chars'] ), number_format( $stats['memory_chars'] ) ) ); } /** * Translate discovered theme/plugin strings. * * ## OPTIONS * * [--lang=] * : Comma-separated languages. Default: all. * * @subcommand strings */ public function strings( $args, $assoc_args ): void { $langs = isset( $assoc_args['lang'] ) ? array_map( 'trim', explode( ',', $assoc_args['lang'] ) ) : Languages::targets(); foreach ( $langs as $lang ) { $total = 0; do { $res = $this->plugin->strings()->auto_translate( $lang, 300 ); $total += $res['translated']; } while ( $res['translated'] > 0 && $res['remaining'] > 0 ); \WP_CLI::log( sprintf( '%s: %d strings translated.', $lang, $total ) ); } \WP_CLI::success( 'Done.' ); } /** * Export translations as XLIFF or CSV. * * ## OPTIONS * * --lang= * : Target language. * * [--format=] * : xliff or csv. * --- * default: xliff * --- * * [--file=] * : Output file. Default: stdout. * * [--only-missing] * : Only untranslated segments. */ public function export( $args, $assoc_args ): void { if ( ! get_current_user_id() ) { \WP_CLI::error( 'Run this command as a user, e.g. --user=admin. Access to each post is checked like in the dashboard.' ); } $x = new Xliff( $this->plugin->posts() ); $out = 'csv' === $assoc_args['format'] ? $x->export_csv( $assoc_args['lang'], array(), array(), isset( $assoc_args['only-missing'] ) ) : $x->export_xliff( $assoc_args['lang'], array(), array(), isset( $assoc_args['only-missing'] ) ); if ( ! empty( $assoc_args['file'] ) ) { file_put_contents( $assoc_args['file'], $out ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents \WP_CLI::success( 'Written to ' . $assoc_args['file'] ); return; } \WP_CLI::line( $out ); } /** * Import translations from XLIFF or CSV. * * ## OPTIONS * * * : File path. * * --lang= * : Target language. */ public function import( $args, $assoc_args ): void { if ( ! get_current_user_id() ) { \WP_CLI::error( 'Run this command as a user, e.g. --user=admin. Access to each post is checked like in the dashboard.' ); } $content = file_get_contents( $args[0] ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents if ( false === $content ) { \WP_CLI::error( 'Cannot read file.' ); } $res = ( new Xliff( $this->plugin->posts() ) )->import( $content, $assoc_args['lang'] ); foreach ( $res['errors'] as $err ) { \WP_CLI::warning( $err ); } \WP_CLI::success( sprintf( '%d segments imported into %d posts.', $res['units'], $res['posts'] ) ); } /** * Migrate from another multilingual plugin. * * ## OPTIONS * * * : wpml, polylang or translatepress. */ public function migrate( $args ): void { $res = ( new Migration( $this->plugin->settings() ) )->run( $args[0] ); \WP_CLI::success( sprintf( 'Linked %d posts and %d terms, imported %d memory entries. Languages: %s', $res['posts'], $res['terms'], $res['memory'], implode( ', ', $res['languages'] ) ) ); } }