From 69ac12b55c61214836a55d5bde6b9f40dbe5e28a Mon Sep 17 00:00:00 2001 From: hanyixuanten <105723997+hanyixuanten@users.noreply.github.com> Date: Mon, 27 Jul 2026 00:25:03 +0800 Subject: [PATCH] feat: add clean button --- README.md | 2 ++ README.zh-CN.md | 2 ++ includes/class-wpt-admin.php | 30 ++++++++++++++++++++++++ includes/class-wpt-translation-store.php | 6 +++++ 4 files changed, 40 insertions(+) diff --git a/README.md b/README.md index 05927f5..a5e80f0 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,8 @@ The Posts and Pages lists show a status column for every target language, includ The Settings page also shows a live progress panel. It refreshes every five seconds and counts completed post/page and category/tag items for every configured target language. When **重新翻译所有内容** starts a batch, progress resets and counts only translations completed after that batch began, so historical translations do not make a new batch appear complete. Forced retranslation jobs are scheduled one minute apart, so a single WP-Cron request processes at most one batch item instead of timing out while processing every post. Progress reads only local WordPress and translation-table data; it does not send additional requests to Baidu. +The **清除已翻译的缓存** action deletes every persisted translation, cancels pending translation jobs, and resets batch progress. The front end then falls back to source content until translations are explicitly queued again; regenerating them consumes Baidu API quota. + ## Known Limitations - The settings UI accepts language codes but does not yet validate them against a maintained Baidu language-code list. diff --git a/README.zh-CN.md b/README.zh-CN.md index a4473a5..cbb363a 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -76,6 +76,8 @@ find . -path './.git' -prune -o -type f -name '*.php' -print0 | xargs -0 -n1 php 设置页还显示实时进度面板。面板每五秒刷新一次,统计每种已配置目标语言中完成的文章/页面和分类/标签项目。当 **重新翻译所有内容** 启动批处理时,进度会重置,仅统计该批处理开始后完成的翻译,因此历史翻译不会让新的批处理看似已完成。强制重翻任务会按一分钟间隔排队,单次 WP-Cron 请求最多处理一个内容项,避免一次处理全部文章而超时。进度只读取本地 WordPress 和翻译表数据,不会发送额外请求。 +设置页的 **清除已翻译的缓存** 会删除所有持久化翻译记录、取消待执行的翻译任务并重置进度。清除后前台将显示源文,需使用相应翻译操作重新生成翻译;重新生成会再次消耗百度 API 配额。 + ## 已知限制 - 设置界面接受语言代码,但尚未依据维护中的百度语言代码列表进行验证。 diff --git a/includes/class-wpt-admin.php b/includes/class-wpt-admin.php index f5f6b72..49673de 100644 --- a/includes/class-wpt-admin.php +++ b/includes/class-wpt-admin.php @@ -7,6 +7,7 @@ class WPT_Admin { add_action( 'admin_menu', array( $this, 'add_settings_page' ) ); add_action( 'admin_init', array( $this, 'register_settings' ) ); add_action( 'admin_post_wpt_queue_existing_translations', array( $this, 'queue_existing_translations' ) ); + add_action( 'admin_post_wpt_clear_translation_cache', array( $this, 'clear_translation_cache' ) ); add_action( 'admin_post_wpt_translate_post', array( $this, 'queue_post_translation' ) ); add_action( 'wp_ajax_wpt_translation_progress', array( $this, 'translation_progress' ) ); add_filter( 'manage_post_posts_columns', array( $this, 'add_translation_column' ) ); @@ -91,6 +92,14 @@ class WPT_Admin {
清除后,前台会暂时显示源文;重新翻译时将再次调用百度翻译 API。
+ +正在读取翻译进度...
@@ -193,6 +202,27 @@ class WPT_Admin { exit; } + public function clear_translation_cache() { + if ( ! current_user_can( 'manage_options' ) ) { + wp_die( '您没有执行此操作的权限。' ); + } + + check_admin_referer( 'wpt_clear_translation_cache' ); + $deleted = ( new WPT_Translation_Store() )->clear(); + + if ( false === $deleted ) { + wp_die( '翻译缓存清除失败。' ); + } + + wp_clear_scheduled_hook( 'wpt_process_translation' ); + wp_clear_scheduled_hook( 'wpt_process_term_translation' ); + wp_clear_scheduled_hook( 'wpt_process_seo_output_translation' ); + delete_option( 'wpt_retranslation_batch' ); + + wp_safe_redirect( add_query_arg( 'wpt_cache_cleared', 1, admin_url( 'options-general.php?page=wp-btranslate' ) ) ); + exit; + } + private function clear_scheduled_event( $hook, $args ) { while ( false !== ( $timestamp = wp_next_scheduled( $hook, $args ) ) ) { wp_unschedule_event( $timestamp, $hook, $args ); diff --git a/includes/class-wpt-translation-store.php b/includes/class-wpt-translation-store.php index f5a4588..0c08746 100644 --- a/includes/class-wpt-translation-store.php +++ b/includes/class-wpt-translation-store.php @@ -70,6 +70,12 @@ class WPT_Translation_Store { ); } + public function clear() { + global $wpdb; + + return $wpdb->query( 'DELETE FROM ' . self::table_name() ); + } + public function get_post_language_status( $post_id, $target_language ) { global $wpdb;