feat: add clean button

This commit is contained in:
hanyixuanten
2026-07-27 00:25:03 +08:00
parent e221a87dd5
commit 69ac12b55c
4 changed files with 40 additions and 0 deletions
+2
View File
@@ -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.
+2
View File
@@ -76,6 +76,8 @@ find . -path './.git' -prune -o -type f -name '*.php' -print0 | xargs -0 -n1 php
设置页还显示实时进度面板。面板每五秒刷新一次,统计每种已配置目标语言中完成的文章/页面和分类/标签项目。当 **重新翻译所有内容** 启动批处理时,进度会重置,仅统计该批处理开始后完成的翻译,因此历史翻译不会让新的批处理看似已完成。强制重翻任务会按一分钟间隔排队,单次 WP-Cron 请求最多处理一个内容项,避免一次处理全部文章而超时。进度只读取本地 WordPress 和翻译表数据,不会发送额外请求。
设置页的 **清除已翻译的缓存** 会删除所有持久化翻译记录、取消待执行的翻译任务并重置进度。清除后前台将显示源文,需使用相应翻译操作重新生成翻译;重新生成会再次消耗百度 API 配额。
## 已知限制
- 设置界面接受语言代码,但尚未依据维护中的百度语言代码列表进行验证。
+30
View File
@@ -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 {
<?php submit_button( '翻译所有分类标签', 'secondary', 'submit', false ); ?>
</form>
<hr />
<h2>翻译缓存</h2>
<p>清除后,前台会暂时显示源文;重新翻译时将再次调用百度翻译 API。</p>
<form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post" onsubmit="return window.confirm('这将删除所有已保存的翻译、取消待执行翻译任务并重置进度。确认继续吗?');">
<input type="hidden" name="action" value="wpt_clear_translation_cache" />
<?php wp_nonce_field( 'wpt_clear_translation_cache' ); ?>
<?php submit_button( '清除已翻译的缓存', 'delete', 'submit', false ); ?>
</form>
<hr />
<h2>翻译进度</h2>
<div id="wpt-translation-progress" aria-live="polite">
<p>正在读取翻译进度...</p>
@@ -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 );
+6
View File
@@ -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;