translator = $translator; $this->settings = $settings; $this->terms = $terms; $this->provider_factory = $provider_factory; } public function translator(): Translator { return $this->translator; } /** @return FieldProvider[] */ public function providers(): array { if ( null === $this->providers ) { $this->providers = (array) apply_filters( 'translateplus_field_providers', call_user_func( $this->provider_factory ) ); } return $this->providers; } /** * Every translatable segment of a source post. * * @return array key => text. */ public function collect( \WP_Post $post ): array { $segments = array( 'title' => $post->post_title, 'excerpt' => $post->post_excerpt, ); foreach ( Segmenter::collect( $post->post_content, 'content' ) as $k => $v ) { $segments[ $k ] = $v; } foreach ( $this->providers() as $provider ) { if ( $provider->supports( $post ) ) { foreach ( $provider->collect( $post ) as $k => $v ) { $segments[ $provider->id() . ':' . $k ] = $v; } } } /** * Filter the segments collected from a post. * * @param array $segments key => text. * @param \WP_Post $post Source post. */ return (array) apply_filters( 'translateplus_post_segments', $segments, $post ); } /** * Stable hash of a post's translatable source content. */ public function source_hash( \WP_Post $post ): string { return md5( (string) wp_json_encode( $this->collect( $post ) ) ); } /** * Characters that a translation would send to the API. */ public function estimate( \WP_Post $post, string $lang ): int { return Translator::estimate( array_values( $this->collect( $post ) ), Languages::source(), $lang ); } /** * Translate (or update the translation of) a post. * * @param array $opts force (bool): overwrite manual edits; memory (bool): use translation memory; * offline (bool): only memory/manual text, never the API (manual translation); * pending (bool): never publish (used for users without review rights). * @return int Translated post ID. * @throws \TranslatePlus\Api\ApiException On API failure. * @throws \RuntimeException On invalid input. */ public function translate( int $post_id, string $lang, array $opts = array() ): int { $post_id = Groups::source_id( 'post', $post_id ); $post = get_post( $post_id ); if ( ! $post || ! PostTypes::is_translatable( $post->post_type ) ) { throw new \RuntimeException( esc_html__( 'This content type is not translatable.', 'translateplus' ) ); } if ( ! Languages::is_target( $lang ) ) { throw new \RuntimeException( esc_html__( 'This language is not enabled.', 'translateplus' ) ); } $existing = Groups::translation( 'post', $post_id, $lang ); if ( $existing && ! get_post( $existing ) ) { Groups::delete_object( 'post', $existing ); $existing = 0; } $force = ! empty( $opts['force'] ); if ( $existing && ! $force ) { $row = Groups::row( 'post', $existing ); if ( $row && Groups::STATUS_MANUAL === $row['status'] ) { return $existing; // Edited by hand in WordPress; never overwritten automatically. } } $segments = $this->collect( $post ); $translations = $this->translator->translate( $segments, Languages::source(), $lang, array( 'memory' => $opts['memory'] ?? true, 'offline' => ! empty( $opts['offline'] ), ) ); $missing = $this->translator->missing(); $target_id = $this->write( $post, $lang, $segments, $translations, $existing, ! empty( $opts['pending'] ) ); Segments::sync( 'post', $post_id, $lang, $segments, $translations, $missing ); /** * Fires after a post has been translated. * * @param int $target_id Translated post ID. * @param \WP_Post $post Source post. * @param string $lang Language. * @param array $translations Segment translations. */ do_action( 'translateplus_post_translated', $target_id, $post, $lang, $translations ); return $target_id; } /** * Rebuild a translation from stored segments and memory, without API calls * for text that is already known (used after manual edits). */ public function rebuild( int $post_id, string $lang ): int { return $this->translate( $post_id, $lang, array( 'force' => true ) ); } /** * @param array $segments Source segments. * @param array $translations Translations by key. */ private function write( \WP_Post $post, string $lang, array $segments, array $translations, int $existing, bool $pending = false ): int { $hash = md5( (string) wp_json_encode( $segments ) ); $auto_publish = ! $pending && (bool) $this->settings->get( 'auto_publish', false ); $by_prefix = array( 'content' => array(), ); foreach ( $translations as $key => $value ) { if ( 0 === strpos( $key, 'content.' ) ) { $by_prefix['content'][ $key ] = $value; } } $title = $translations['title'] ?? $post->post_title; $content = Segmenter::apply( $post->post_content, $by_prefix['content'], 'content' ); $excerpt = $translations['excerpt'] ?? $post->post_excerpt; $parent = 0; if ( $post->post_parent ) { $parent = Groups::translation( 'post', (int) $post->post_parent, $lang ) ?: (int) $post->post_parent; } $data = array( 'post_type' => $post->post_type, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt, 'post_parent' => $parent, 'menu_order' => $post->menu_order, 'comment_status' => $post->comment_status, 'ping_status' => $post->ping_status, 'post_password' => $post->post_password, 'post_author' => $post->post_author, 'post_date' => $post->post_date, 'post_date_gmt' => $post->post_date_gmt, ); if ( $existing ) { $data['ID'] = $existing; $current = get_post_status( $existing ); // Keep the current status, but follow the source into publish when auto-publishing. $data['post_status'] = ( $auto_publish && 'publish' === $post->post_status ) ? 'publish' : $current; $group_status = 'publish' === $data['post_status'] || $auto_publish ? Groups::STATUS_TRANSLATED : Groups::STATUS_REVIEW; } else { $data['post_status'] = $auto_publish ? $post->post_status : 'pending'; if ( 'trash' === $data['post_status'] || 'auto-draft' === $data['post_status'] ) { $data['post_status'] = 'draft'; } $data['post_name'] = $this->settings->get( 'translate_slugs', true ) ? sanitize_title( $title ) : $post->post_name; $group_status = $auto_publish ? Groups::STATUS_TRANSLATED : Groups::STATUS_REVIEW; } /** * Filter the post data before a translation is written. * * @param array $data Post data for wp_insert_post(). * @param \WP_Post $post Source post. * @param string $lang Language. */ $data = apply_filters( 'translateplus_translated_post_data', $data, $post, $lang ); self::$writing = true; try { if ( ! $existing ) { // Link before inserting the slug so wp_unique_post_slug() knows the language. $target_id = wp_insert_post( wp_slash( array_merge( $data, array( 'post_status' => 'draft', 'post_name' => '' ) ) ), true ); if ( is_wp_error( $target_id ) ) { throw new \RuntimeException( esc_html( $target_id->get_error_message() ) ); } Groups::link( 'post', $post->ID, (int) $target_id, $lang, $group_status, $hash ); $data['ID'] = (int) $target_id; } else { $target_id = $existing; } $result = wp_update_post( wp_slash( $data ), true ); if ( is_wp_error( $result ) ) { throw new \RuntimeException( esc_html( $result->get_error_message() ) ); } $this->copy_meta( $post, (int) $target_id, $lang ); foreach ( $this->providers() as $provider ) { if ( ! $provider->supports( $post ) ) { continue; } $prefix = $provider->id() . ':'; $scoped = array(); foreach ( $translations as $key => $value ) { if ( 0 === strpos( $key, $prefix ) ) { $scoped[ substr( $key, strlen( $prefix ) ) ] = $value; } } $provider->apply( $post, (int) $target_id, $scoped, $lang ); } $this->sync_terms( $post, (int) $target_id, $lang ); update_post_meta( (int) $target_id, '_tp_written_hash', self::content_hash( get_post( (int) $target_id ) ) ); } finally { self::$writing = false; } Groups::link( 'post', $post->ID, (int) $target_id, $lang, $group_status, $hash ); clean_post_cache( (int) $target_id ); return (int) $target_id; } /** * Hash of the parts of a post a human edits, to detect manual changes. */ public static function content_hash( ?\WP_Post $post ): string { return $post ? md5( $post->post_title . "\0" . $post->post_content . "\0" . $post->post_excerpt ) : ''; } /** * Copy non-translatable meta (prices, images, layout settings...). */ private function copy_meta( \WP_Post $post, int $target_id, string $lang ): void { $owned = array(); foreach ( $this->providers() as $provider ) { if ( $provider->supports( $post ) ) { $owned = array_merge( $owned, $provider->owned_meta_keys( $post ) ); } } $owned = array_flip( $owned ); foreach ( get_post_meta( $post->ID ) as $key => $values ) { if ( isset( $owned[ $key ] ) || 0 === strpos( (string) $key, '_tp_' ) ) { continue; } delete_post_meta( $target_id, $key ); foreach ( $values as $value ) { add_post_meta( $target_id, $key, wp_slash( maybe_unserialize( $value ) ) ); } } } /** * Assign terms: translatable taxonomies get translated terms, others are copied. */ private function sync_terms( \WP_Post $post, int $target_id, string $lang ): void { foreach ( get_object_taxonomies( $post->post_type ) as $taxonomy ) { $terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) ); if ( is_wp_error( $terms ) ) { continue; } if ( PostTypes::is_translatable_taxonomy( $taxonomy ) || 0 === strpos( $taxonomy, 'pa_' ) ) { $mapped = array(); foreach ( $terms as $term_id ) { try { $mapped[] = $this->terms->translate( (int) $term_id, $lang ); } catch ( \Exception $e ) { $mapped[] = (int) $term_id; } } $terms = $mapped; } wp_set_object_terms( $target_id, array_map( 'intval', $terms ), $taxonomy ); } } }