settings = $settings; $this->vault = $vault; $this->posts = $posts; } public function register(): void { add_action( 'wp_after_insert_post', array( $this, 'after_save' ), 20, 4 ); add_action( 'wp_trash_post', array( $this, 'trash' ) ); add_action( 'untrashed_post', array( $this, 'untrash' ) ); add_action( 'before_delete_post', array( $this, 'delete' ) ); add_action( 'delete_term', array( $this, 'delete_term' ), 10, 1 ); } /** * @param int $post_id Post ID. * @param \WP_Post $post Post. * @param bool $update Update. * @param \WP_Post|null $before Previous version. */ public function after_save( $post_id, $post, $update, $before ): void { if ( PostTranslator::$writing || wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) || ! PostTypes::is_translatable( $post->post_type ) ) { return; } if ( in_array( $post->post_status, array( 'auto-draft', 'trash', 'inherit' ), true ) ) { return; } // A translation edited by hand in WordPress is protected from automatic overwrites. if ( Groups::is_translation( 'post', (int) $post_id ) ) { $written = get_post_meta( $post_id, '_tp_written_hash', true ); if ( $written && PostTranslator::content_hash( $post ) !== $written ) { Groups::set_status( 'post', (int) $post_id, Groups::STATUS_MANUAL ); } elseif ( 'publish' === $post->post_status ) { $row = Groups::row( 'post', (int) $post_id ); if ( $row && Groups::STATUS_REVIEW === $row['status'] ) { Groups::set_status( 'post', (int) $post_id, Groups::STATUS_TRANSLATED ); } } return; } // Source content changed: mark translations outdated. $hash = $this->posts->source_hash( $post ); $members = Groups::members( 'post', (int) $post_id ); if ( $members ) { Groups::mark_outdated( 'post', (int) $post_id, $hash ); } if ( ! $this->settings->get( 'auto_translate' ) || ! $this->vault->has() || ! Languages::targets() ) { return; } if ( ! in_array( $post->post_status, array( 'publish', 'future', 'private' ), true ) ) { return; } /** * Filter whether a saved post is translated automatically. * * @param bool $auto Auto-translate. * @param \WP_Post $post Post. */ if ( ! apply_filters( 'translateplus_auto_translate_post', true, $post ) ) { return; } // Only queue when something is missing or the source changed since the last translation. $needs = false; foreach ( Languages::targets() as $lang ) { $row = $members[ $lang ] ?? null; if ( ! $row || ( $row['source_hash'] !== $hash && Groups::STATUS_MANUAL !== $row['status'] ) ) { $needs = true; break; } } if ( $needs ) { Jobs::queue_auto( (int) $post_id ); } } /** * Trashing a source trashes its translations. */ public function trash( $post_id ): void { if ( PostTranslator::$writing || Groups::is_translation( 'post', (int) $post_id ) ) { return; } foreach ( Groups::translations( 'post', (int) $post_id ) as $lang => $id ) { if ( $id !== (int) $post_id && 'trash' !== get_post_status( $id ) ) { PostTranslator::$writing = true; wp_trash_post( $id ); PostTranslator::$writing = false; } } } public function untrash( $post_id ): void { if ( PostTranslator::$writing || Groups::is_translation( 'post', (int) $post_id ) ) { return; } foreach ( Groups::translations( 'post', (int) $post_id ) as $lang => $id ) { if ( $id !== (int) $post_id && 'trash' === get_post_status( $id ) ) { PostTranslator::$writing = true; wp_untrash_post( $id ); PostTranslator::$writing = false; } } } public function delete( $post_id ): void { $post_id = (int) $post_id; if ( ! Groups::is_translation( 'post', $post_id ) ) { Segments::delete_object( 'post', $post_id ); } Groups::delete_object( 'post', $post_id ); } public function delete_term( $term_id ): void { Groups::delete_object( 'term', (int) $term_id ); } }