add log and fix translate
This commit is contained in:
@@ -55,6 +55,8 @@ After changing routing settings, rewrite rules are refreshed. The initial implem
|
||||
|
||||
Credentials are stored in the WordPress `wpt_settings` option and are not rendered on the front end. The plugin stores the translation identity, language pair, field context, source fingerprint, translated value, status, and timestamps. It does not store full Baidu response payloads.
|
||||
|
||||
The **Log every Baidu translation request** setting is disabled by default. When enabled, each request writes safe metadata to the PHP error log: language pair, field context, source fingerprint and length, result status, and an error code when applicable. Credentials, signatures, source text, translated text, and complete API responses are never logged.
|
||||
|
||||
Use HTTPS and restrict database access appropriately because WordPress options are database-backed. Production sites should configure a real cron runner for reliable translation processing.
|
||||
|
||||
## Uninstalling
|
||||
|
||||
@@ -54,6 +54,8 @@ Btranslate 是一个 WordPress 插件骨架,使用百度翻译 API 持久化
|
||||
|
||||
凭据保存在 WordPress 的 `wpt_settings` 选项中,不会在前端输出。插件会存储翻译标识、语言对、字段上下文、源内容指纹、翻译值、状态和时间戳;不会保存完整的百度响应负载。
|
||||
|
||||
**记录每次百度翻译请求** 默认关闭。开启后,每次请求都会向 PHP 错误日志写入安全元数据:语言对、字段上下文、源文本指纹和长度、结果状态以及适用时的错误码。日志绝不会包含凭据、签名、原文、译文或完整 API 响应。
|
||||
|
||||
由于 WordPress 选项由数据库保存,请使用 HTTPS 并适当限制数据库访问权限。生产站点应配置真实的 Cron 执行器,确保翻译任务可靠运行。
|
||||
|
||||
## 卸载
|
||||
|
||||
@@ -46,6 +46,7 @@ class WPT_Admin {
|
||||
'fallback_language' => sanitize_key( $settings['fallback_language'] ?? 'zh' ),
|
||||
'baidu_app_id' => sanitize_text_field( $settings['baidu_app_id'] ?? '' ),
|
||||
'baidu_secret_key' => sanitize_text_field( $settings['baidu_secret_key'] ?? '' ),
|
||||
'log_requests' => ! empty( $settings['log_requests'] ),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -67,6 +68,7 @@ class WPT_Admin {
|
||||
<tr><th scope="row"><label for="wpt-domain-bindings">域名绑定</label></th><td><textarea id="wpt-domain-bindings" name="wpt_settings[domain_bindings]" rows="4" class="large-text" placeholder="en.example.com=en"><?php echo esc_textarea( implode( "\n", $bindings ) ); ?></textarea><p class="description">每行一个 <code>域名=语言代码</code>,例如 <code>en.example.com=en</code>。请勿填写 <code>https://</code>、路径或端口。</p></td></tr>
|
||||
<tr><th scope="row"><label for="wpt-baidu-app-id">百度应用 ID</label></th><td><input id="wpt-baidu-app-id" name="wpt_settings[baidu_app_id]" type="text" value="<?php echo esc_attr( $settings['baidu_app_id'] ); ?>" class="regular-text" /></td></tr>
|
||||
<tr><th scope="row"><label for="wpt-baidu-secret-key">百度密钥</label></th><td><input id="wpt-baidu-secret-key" name="wpt_settings[baidu_secret_key]" type="password" value="<?php echo esc_attr( $settings['baidu_secret_key'] ); ?>" class="regular-text" autocomplete="new-password" /></td></tr>
|
||||
<tr><th scope="row">请求日志</th><td><label for="wpt-log-requests"><input id="wpt-log-requests" name="wpt_settings[log_requests]" type="checkbox" value="1" <?php checked( $settings['log_requests'] ); ?> /> 记录每次百度翻译请求</label><p class="description">日志写入 PHP 错误日志,包含语言、字段、文本指纹、长度和结果状态;不会记录密钥、原文、译文或完整 API 响应。</p></td></tr>
|
||||
</table>
|
||||
<?php submit_button(); ?>
|
||||
</form>
|
||||
|
||||
@@ -17,6 +17,8 @@ class WPT_Baidu_Provider implements WPT_Translation_Provider {
|
||||
|
||||
public function translate( $source_value, $source_language, $target_language, $context ) {
|
||||
if ( '' === $this->app_id || '' === $this->secret_key ) {
|
||||
$this->log_request( $source_value, $source_language, $target_language, $context, 'missing_credentials' );
|
||||
|
||||
return WPT_Translation_Result::failure( 'missing_credentials', __( 'Baidu Translate credentials are not configured.', 'wp-btranslate' ) );
|
||||
}
|
||||
|
||||
@@ -38,6 +40,8 @@ class WPT_Baidu_Provider implements WPT_Translation_Provider {
|
||||
);
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
$this->log_request( $source_value, $source_language, $target_language, $context, 'request_failed', $response->get_error_code() );
|
||||
|
||||
return WPT_Translation_Result::failure( 'request_failed', $response->get_error_message() );
|
||||
}
|
||||
|
||||
@@ -46,12 +50,35 @@ class WPT_Baidu_Provider implements WPT_Translation_Provider {
|
||||
if ( ! is_array( $body ) || empty( $body['trans_result'] ) || ! is_array( $body['trans_result'] ) ) {
|
||||
$error_code = isset( $body['error_code'] ) ? sanitize_key( $body['error_code'] ) : 'invalid_response';
|
||||
$error_message = isset( $body['error_msg'] ) ? sanitize_text_field( $body['error_msg'] ) : __( 'Baidu Translate returned an invalid response.', 'wp-btranslate' );
|
||||
$this->log_request( $source_value, $source_language, $target_language, $context, 'failed', $error_code );
|
||||
|
||||
return WPT_Translation_Result::failure( $error_code, $error_message );
|
||||
}
|
||||
|
||||
$translated_parts = wp_list_pluck( $body['trans_result'], 'dst' );
|
||||
$this->log_request( $source_value, $source_language, $target_language, $context, 'complete' );
|
||||
|
||||
return WPT_Translation_Result::success( implode( "\n", $translated_parts ) );
|
||||
}
|
||||
|
||||
private function log_request( $source_value, $source_language, $target_language, $context, $status, $error_code = '' ) {
|
||||
$settings = WPT_Settings::get();
|
||||
|
||||
if ( empty( $settings['log_requests'] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$entry = array(
|
||||
'provider' => 'baidu',
|
||||
'source_language' => sanitize_key( $source_language ),
|
||||
'target_language' => sanitize_key( $target_language ),
|
||||
'context' => sanitize_key( $context ),
|
||||
'source_fingerprint' => WPT_Translation_Identity::source_fingerprint( (string) $source_value ),
|
||||
'source_length' => strlen( (string) $source_value ),
|
||||
'status' => sanitize_key( $status ),
|
||||
'error_code' => sanitize_key( $error_code ),
|
||||
);
|
||||
|
||||
error_log( 'WPT translation request: ' . wp_json_encode( $entry ) );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -228,11 +228,23 @@ class WPT_Content_Controller {
|
||||
}
|
||||
|
||||
public function translate_wordpress_ai_meta_description_metadata( $value, $post_id, $meta_key, $single ) {
|
||||
if ( 'wpai_meta_description' !== $meta_key || ! $single ) {
|
||||
static $resolving_raw_value = false;
|
||||
|
||||
if ( 'wpai_meta_description' !== $meta_key || ! $single || $resolving_raw_value ) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
$source_value = is_string( $value ) ? $value : get_metadata_raw( 'post', $post_id, $meta_key, true );
|
||||
if ( is_string( $value ) ) {
|
||||
$source_value = $value;
|
||||
} else {
|
||||
$resolving_raw_value = true;
|
||||
try {
|
||||
$source_value = get_metadata_raw( 'post', $post_id, $meta_key, true );
|
||||
} finally {
|
||||
$resolving_raw_value = false;
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! is_string( $source_value ) ) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ class WPT_Settings {
|
||||
'fallback_language' => 'zh',
|
||||
'baidu_app_id' => '',
|
||||
'baidu_secret_key' => '',
|
||||
'log_requests' => false,
|
||||
);
|
||||
|
||||
return wp_parse_args( (array) get_option( 'wpt_settings', array() ), $defaults );
|
||||
|
||||
Reference in New Issue
Block a user