change id
This commit is contained in:
@@ -28,19 +28,19 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
set -eu
|
set -eu
|
||||||
plugin_slug="btranslate"
|
plugin_slug="btranslate"
|
||||||
version="$(grep -E '^ \* Version:' wp-translate.php | sed -E 's/^ \* Version: *//')"
|
version="$(grep -E '^ \* Version:' btranslate.php | sed -E 's/^ \* Version: *//')"
|
||||||
archive_name="${plugin_slug}-${version}.zip"
|
archive_name="${plugin_slug}-${version}.zip"
|
||||||
|
|
||||||
rm -rf build
|
rm -rf build
|
||||||
mkdir -p "build/${plugin_slug}"
|
mkdir -p "build/${plugin_slug}"
|
||||||
cp wp-translate.php README.md "build/${plugin_slug}/"
|
cp btranslate.php README.md "build/${plugin_slug}/"
|
||||||
cp -R includes "build/${plugin_slug}/"
|
cp -R includes "build/${plugin_slug}/"
|
||||||
(
|
(
|
||||||
cd build
|
cd build
|
||||||
zip -qr "../${archive_name}" "${plugin_slug}"
|
zip -qr "../${archive_name}" "${plugin_slug}"
|
||||||
)
|
)
|
||||||
|
|
||||||
unzip -p "${archive_name}" "${plugin_slug}/wp-translate.php" | grep -q '^ \* Plugin Name: Btranslate$'
|
unzip -p "${archive_name}" "${plugin_slug}/btranslate.php" | grep -q '^ \* Plugin Name: Btranslate$'
|
||||||
|
|
||||||
printf 'archive=%s\n' "${archive_name}" >> "$GITHUB_OUTPUT"
|
printf 'archive=%s\n' "${archive_name}" >> "$GITHUB_OUTPUT"
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
# wp-translate Agent Guide
|
# btranslate Agent Guide
|
||||||
|
|
||||||
## Project Scope
|
## Project Scope
|
||||||
|
|
||||||
@@ -44,6 +44,6 @@ Unless a user explicitly requests a refresh, each translatable value must be tra
|
|||||||
- Add focused tests for language selection, routing, translation cache reuse and invalidation, persistence, provider signing/error handling, and content/meta filtering.
|
- Add focused tests for language selection, routing, translation cache reuse and invalidation, persistence, provider signing/error handling, and content/meta filtering.
|
||||||
- Mock Baidu API calls in every test. Tests must not require live credentials or outbound network access.
|
- Mock Baidu API calls in every test. Tests must not require live credentials or outbound network access.
|
||||||
- Keep changes narrow and avoid unrelated refactors or formatting churn.
|
- Keep changes narrow and avoid unrelated refactors or formatting churn.
|
||||||
- Plugin slug and PHP prefix: `wp-translate` and `wpt_`/`WPT_`. The current minimum supported PHP version is 8.1 and the current minimum WordPress version is 6.4.
|
- Plugin slug and PHP prefix: `btranslate` and `btranslate_`/`BTRANSLATE_`. The current minimum supported PHP version is 8.1 and the current minimum WordPress version is 6.4.
|
||||||
- The repository has no Composer-managed development dependencies or bundled automated test suite. Validate PHP syntax with `find . -path './.git' -prune -o -type f -name '*.php' -print0 | xargs -0 -n1 php -l`. Any future automated tests must mock WordPress API calls and must not require live credentials, external network access, or a WordPress database.
|
- The repository has no Composer-managed development dependencies or bundled automated test suite. Validate PHP syntax with `find . -path './.git' -prune -o -type f -name '*.php' -print0 | xargs -0 -n1 php -l`. Any future automated tests must mock WordPress API calls and must not require live credentials, external network access, or a WordPress database.
|
||||||
- Document supported languages, routing modes, data retention, translation lifecycle, and known limitations in the README whenever the implementation changes.
|
- Document supported languages, routing modes, data retention, translation lifecycle, and known limitations in the README whenever the implementation changes.
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ Btranslate is a WordPress plugin skeleton for persistent multilingual content us
|
|||||||
|
|
||||||
## Translation Lifecycle
|
## Translation Lifecycle
|
||||||
|
|
||||||
Translations are generated by the `wpt_process_translation` cron task after a post or page is saved. For content published before the plugin was activated, open **Settings > Btranslate** and use **Translate existing posts and pages** to schedule all published posts and pages. A translation has a deterministic identity based on the source fingerprint, source language, target language, field context, and provider version. The plugin first looks up a completed result in the `{$wpdb->prefix}wpt_translations` table. Only a cache miss causes a request to Baidu.
|
Translations are generated by the `btranslate_process_translation` cron task after a post or page is saved. For content published before the plugin was activated, open **Settings > Btranslate** and use **Translate existing posts and pages** to schedule all published posts and pages. A translation has a deterministic identity based on the source fingerprint, source language, target language, field context, and provider version. The plugin first looks up a completed result in the `{$wpdb->prefix}btranslate_translations` table. Only a cache miss causes a request to Baidu.
|
||||||
|
|
||||||
Changing source text produces a new fingerprint and therefore schedules a new translation without deleting a valid prior translation. Front-end rendering never contacts Baidu; it reads a completed value from the table and otherwise falls back to the source value. WordPress cron must run after queuing content; production sites should configure a real server cron to call WordPress cron regularly.
|
Changing source text produces a new fingerprint and therefore schedules a new translation without deleting a valid prior translation. Front-end rendering never contacts Baidu; it reads a completed value from the table and otherwise falls back to the source value. WordPress cron must run after queuing content; production sites should configure a real server cron to call WordPress cron regularly.
|
||||||
|
|
||||||
@@ -55,7 +55,7 @@ After changing routing settings, rewrite rules are refreshed. The initial implem
|
|||||||
|
|
||||||
## Security And Data Retention
|
## Security And Data Retention
|
||||||
|
|
||||||
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.
|
Credentials are stored in the WordPress `btranslate_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.
|
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.
|
||||||
|
|
||||||
@@ -63,7 +63,7 @@ Use HTTPS and restrict database access appropriately because WordPress options a
|
|||||||
|
|
||||||
## Uninstalling
|
## Uninstalling
|
||||||
|
|
||||||
Uninstalling Btranslate permanently deletes its settings and credentials, retranslation batch state, all pending Btranslate cron events, and every site's `{$wpdb->prefix}wpt_translations` table. This removes all persisted translations. Deactivating the plugin does not delete these records.
|
Uninstalling Btranslate permanently deletes its settings and credentials, retranslation batch state, all pending Btranslate cron events, and every site's `{$wpdb->prefix}btranslate_translations` table. This removes all persisted translations. Deactivating the plugin does not delete these records.
|
||||||
|
|
||||||
## Development
|
## Development
|
||||||
|
|
||||||
|
|||||||
+3
-3
@@ -21,7 +21,7 @@ Btranslate 是一个 WordPress 插件骨架,使用百度翻译 API 持久化
|
|||||||
|
|
||||||
## 翻译生命周期
|
## 翻译生命周期
|
||||||
|
|
||||||
保存文章或页面后,`wpt_process_translation` Cron 任务会生成翻译。对于插件启用前已发布的内容,请打开 **设置 > Btranslate**,使用 **翻译现有文章和页面** 为全部已发布文章和页面安排任务。翻译具有确定性标识,标识由源内容指纹、源语言、目标语言、字段上下文和提供程序版本构成。插件会先在 `{$wpdb->prefix}wpt_translations` 表中查找已完成的结果;只有缓存未命中时才会向百度发起请求。
|
保存文章或页面后,`btranslate_process_translation` Cron 任务会生成翻译。对于插件启用前已发布的内容,请打开 **设置 > Btranslate**,使用 **翻译现有文章和页面** 为全部已发布文章和页面安排任务。翻译具有确定性标识,标识由源内容指纹、源语言、目标语言、字段上下文和提供程序版本构成。插件会先在 `{$wpdb->prefix}btranslate_translations` 表中查找已完成的结果;只有缓存未命中时才会向百度发起请求。
|
||||||
|
|
||||||
源文本变更会生成新的指纹,因此会安排新的翻译任务,而不会删除此前有效的翻译。前端渲染不会请求百度 API;它只读取表中已完成的值,若不存在则回退显示源文本。内容入队后必须运行 WordPress Cron;生产环境应配置真实的服务器 Cron,定期调用 WordPress Cron。
|
源文本变更会生成新的指纹,因此会安排新的翻译任务,而不会删除此前有效的翻译。前端渲染不会请求百度 API;它只读取表中已完成的值,若不存在则回退显示源文本。内容入队后必须运行 WordPress Cron;生产环境应配置真实的服务器 Cron,定期调用 WordPress Cron。
|
||||||
|
|
||||||
@@ -54,7 +54,7 @@ Btranslate 是一个 WordPress 插件骨架,使用百度翻译 API 持久化
|
|||||||
|
|
||||||
## 安全和数据保留
|
## 安全和数据保留
|
||||||
|
|
||||||
凭据保存在 WordPress 的 `wpt_settings` 选项中,不会在前端输出。插件会存储翻译标识、语言对、字段上下文、源内容指纹、翻译值、状态和时间戳;不会保存完整的百度响应负载。
|
凭据保存在 WordPress 的 `btranslate_settings` 选项中,不会在前端输出。插件会存储翻译标识、语言对、字段上下文、源内容指纹、翻译值、状态和时间戳;不会保存完整的百度响应负载。
|
||||||
|
|
||||||
**记录每次百度翻译请求** 默认关闭。开启后,每次请求都会向 PHP 错误日志写入安全元数据:语言对、字段上下文、源文本指纹和长度、结果状态以及适用时的错误码。日志绝不会包含凭据、签名、原文、译文或完整 API 响应。
|
**记录每次百度翻译请求** 默认关闭。开启后,每次请求都会向 PHP 错误日志写入安全元数据:语言对、字段上下文、源文本指纹和长度、结果状态以及适用时的错误码。日志绝不会包含凭据、签名、原文、译文或完整 API 响应。
|
||||||
|
|
||||||
@@ -62,7 +62,7 @@ Btranslate 是一个 WordPress 插件骨架,使用百度翻译 API 持久化
|
|||||||
|
|
||||||
## 卸载
|
## 卸载
|
||||||
|
|
||||||
卸载 Btranslate 会永久删除其设置和凭据、重新翻译批处理状态、所有待执行的 Btranslate Cron 事件,以及每个站点的 `{$wpdb->prefix}wpt_translations` 表。这会删除所有已持久化的翻译。停用插件不会删除这些记录。
|
卸载 Btranslate 会永久删除其设置和凭据、重新翻译批处理状态、所有待执行的 Btranslate Cron 事件,以及每个站点的 `{$wpdb->prefix}btranslate_translations` 表。这会删除所有已持久化的翻译。停用插件不会删除这些记录。
|
||||||
|
|
||||||
## 开发
|
## 开发
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Plugin Name: Btranslate
|
||||||
|
* Description: Persistent multilingual WordPress translations powered by Baidu Translate.
|
||||||
|
* Version: 0.2.0
|
||||||
|
* Requires at least: 6.4
|
||||||
|
* Requires PHP: 8.1
|
||||||
|
* Author: hanyixuanten
|
||||||
|
* Text Domain: btranslate
|
||||||
|
*/
|
||||||
|
|
||||||
|
defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
|
define( 'BTRANSLATE_VERSION', '0.2.0' );
|
||||||
|
define( 'BTRANSLATE_FILE', __FILE__ );
|
||||||
|
define( 'BTRANSLATE_PATH', plugin_dir_path( __FILE__ ) );
|
||||||
|
|
||||||
|
require_once BTRANSLATE_PATH . 'includes/interface-btranslate-translation-provider.php';
|
||||||
|
require_once BTRANSLATE_PATH . 'includes/class-btranslate-translation-result.php';
|
||||||
|
require_once BTRANSLATE_PATH . 'includes/class-btranslate-translation-identity.php';
|
||||||
|
require_once BTRANSLATE_PATH . 'includes/class-btranslate-translation-store.php';
|
||||||
|
require_once BTRANSLATE_PATH . 'includes/class-btranslate-baidu-provider.php';
|
||||||
|
require_once BTRANSLATE_PATH . 'includes/class-btranslate-translation-service.php';
|
||||||
|
require_once BTRANSLATE_PATH . 'includes/class-btranslate-content-translator.php';
|
||||||
|
require_once BTRANSLATE_PATH . 'includes/class-btranslate-settings.php';
|
||||||
|
require_once BTRANSLATE_PATH . 'includes/class-btranslate-language-router.php';
|
||||||
|
require_once BTRANSLATE_PATH . 'includes/class-btranslate-sitemap-controller.php';
|
||||||
|
require_once BTRANSLATE_PATH . 'includes/class-btranslate-content-controller.php';
|
||||||
|
require_once BTRANSLATE_PATH . 'includes/class-btranslate-admin.php';
|
||||||
|
require_once BTRANSLATE_PATH . 'includes/class-btranslate-plugin.php';
|
||||||
|
|
||||||
|
register_activation_hook( BTRANSLATE_FILE, array( 'BTRANSLATE_Plugin', 'activate' ) );
|
||||||
|
register_deactivation_hook( BTRANSLATE_FILE, array( 'BTRANSLATE_Plugin', 'deactivate' ) );
|
||||||
|
|
||||||
|
add_action( 'plugins_loaded', array( 'BTRANSLATE_Plugin', 'instance' ) );
|
||||||
@@ -2,19 +2,19 @@
|
|||||||
|
|
||||||
defined( 'ABSPATH' ) || exit;
|
defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
class WPT_Admin {
|
class BTRANSLATE_Admin {
|
||||||
public function register() {
|
public function register() {
|
||||||
add_action( 'admin_menu', array( $this, 'add_settings_page' ) );
|
add_action( 'admin_menu', array( $this, 'add_settings_page' ) );
|
||||||
add_action( 'admin_init', array( $this, 'register_settings' ) );
|
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_btranslate_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_btranslate_clear_translation_cache', array( $this, 'clear_translation_cache' ) );
|
||||||
add_action( 'admin_post_wpt_translate_post', array( $this, 'queue_post_translation' ) );
|
add_action( 'admin_post_btranslate_translate_post', array( $this, 'queue_post_translation' ) );
|
||||||
add_action( 'wp_ajax_wpt_translation_progress', array( $this, 'translation_progress' ) );
|
add_action( 'wp_ajax_btranslate_translation_progress', array( $this, 'translation_progress' ) );
|
||||||
add_filter( 'manage_post_posts_columns', array( $this, 'add_translation_column' ) );
|
add_filter( 'manage_post_posts_columns', array( $this, 'add_translation_column' ) );
|
||||||
add_filter( 'manage_page_posts_columns', array( $this, 'add_translation_column' ) );
|
add_filter( 'manage_page_posts_columns', array( $this, 'add_translation_column' ) );
|
||||||
add_action( 'manage_post_posts_custom_column', array( $this, 'render_translation_column' ), 10, 2 );
|
add_action( 'manage_post_posts_custom_column', array( $this, 'render_translation_column' ), 10, 2 );
|
||||||
add_action( 'manage_page_posts_custom_column', array( $this, 'render_translation_column' ), 10, 2 );
|
add_action( 'manage_page_posts_custom_column', array( $this, 'render_translation_column' ), 10, 2 );
|
||||||
add_filter( 'plugin_action_links_' . plugin_basename( WPT_FILE ), array( $this, 'add_plugin_settings_link' ) );
|
add_filter( 'plugin_action_links_' . plugin_basename( BTRANSLATE_FILE ), array( $this, 'add_plugin_settings_link' ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
public function add_settings_page() {
|
public function add_settings_page() {
|
||||||
@@ -22,7 +22,7 @@ class WPT_Admin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function register_settings() {
|
public function register_settings() {
|
||||||
register_setting( 'wpt_settings', 'wpt_settings', array( $this, 'sanitize_settings' ) );
|
register_setting( 'btranslate_settings', 'btranslate_settings', array( $this, 'sanitize_settings' ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
public function sanitize_settings( $settings ) {
|
public function sanitize_settings( $settings ) {
|
||||||
@@ -31,7 +31,7 @@ class WPT_Admin {
|
|||||||
|
|
||||||
foreach ( preg_split( '/\r\n|\r|\n/', (string) ( $settings['domain_bindings'] ?? '' ) ) as $line ) {
|
foreach ( preg_split( '/\r\n|\r|\n/', (string) ( $settings['domain_bindings'] ?? '' ) ) as $line ) {
|
||||||
$parts = array_map( 'trim', explode( '=', $line, 2 ) );
|
$parts = array_map( 'trim', explode( '=', $line, 2 ) );
|
||||||
$domain = 2 === count( $parts ) ? WPT_Settings::normalize_domain( $parts[0] ) : '';
|
$domain = 2 === count( $parts ) ? BTRANSLATE_Settings::normalize_domain( $parts[0] ) : '';
|
||||||
$language = 2 === count( $parts ) ? sanitize_key( $parts[1] ) : '';
|
$language = 2 === count( $parts ) ? sanitize_key( $parts[1] ) : '';
|
||||||
if ( '' !== $domain && '' !== $language ) {
|
if ( '' !== $domain && '' !== $language ) {
|
||||||
$bindings[ $domain ] = $language;
|
$bindings[ $domain ] = $language;
|
||||||
@@ -51,7 +51,7 @@ class WPT_Admin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function render_settings_page() {
|
public function render_settings_page() {
|
||||||
$settings = WPT_Settings::get();
|
$settings = BTRANSLATE_Settings::get();
|
||||||
$bindings = array();
|
$bindings = array();
|
||||||
foreach ( (array) $settings['domain_bindings'] as $domain => $language ) {
|
foreach ( (array) $settings['domain_bindings'] as $domain => $language ) {
|
||||||
$bindings[] = $domain . '=' . $language;
|
$bindings[] = $domain . '=' . $language;
|
||||||
@@ -60,15 +60,15 @@ class WPT_Admin {
|
|||||||
<div class="wrap">
|
<div class="wrap">
|
||||||
<h1>Btranslate</h1>
|
<h1>Btranslate</h1>
|
||||||
<form action="options.php" method="post">
|
<form action="options.php" method="post">
|
||||||
<?php settings_fields( 'wpt_settings' ); ?>
|
<?php settings_fields( 'btranslate_settings' ); ?>
|
||||||
<table class="form-table" role="presentation">
|
<table class="form-table" role="presentation">
|
||||||
<tr><th scope="row"><label for="wpt-source-language">源语言</label></th><td><input id="wpt-source-language" name="wpt_settings[source_language]" type="text" value="<?php echo esc_attr( $settings['source_language'] ); ?>" /><p class="description">使用百度翻译语言代码,例如 <code>zh</code>。</p></td></tr>
|
<tr><th scope="row"><label for="btranslate-source-language">源语言</label></th><td><input id="btranslate-source-language" name="btranslate_settings[source_language]" type="text" value="<?php echo esc_attr( $settings['source_language'] ); ?>" /><p class="description">使用百度翻译语言代码,例如 <code>zh</code>。</p></td></tr>
|
||||||
<tr><th scope="row"><label for="wpt-target-languages">目标语言</label></th><td><input id="wpt-target-languages" name="wpt_settings[target_languages]" type="text" value="<?php echo esc_attr( implode( ',', $settings['target_languages'] ) ); ?>" class="regular-text" /><p class="description">多个语言代码用英文逗号分隔,例如 <code>en,ja</code>。</p></td></tr>
|
<tr><th scope="row"><label for="btranslate-target-languages">目标语言</label></th><td><input id="btranslate-target-languages" name="btranslate_settings[target_languages]" type="text" value="<?php echo esc_attr( implode( ',', $settings['target_languages'] ) ); ?>" class="regular-text" /><p class="description">多个语言代码用英文逗号分隔,例如 <code>en,ja</code>。</p></td></tr>
|
||||||
<tr><th scope="row"><label for="wpt-routing-mode">网址模式</label></th><td><select id="wpt-routing-mode" name="wpt_settings[routing_mode]"><option value="subdirectory" <?php selected( $settings['routing_mode'], 'subdirectory' ); ?>>子目录</option><option value="domain" <?php selected( $settings['routing_mode'], 'domain' ); ?>>绑定域名</option></select></td></tr>
|
<tr><th scope="row"><label for="btranslate-routing-mode">网址模式</label></th><td><select id="btranslate-routing-mode" name="btranslate_settings[routing_mode]"><option value="subdirectory" <?php selected( $settings['routing_mode'], 'subdirectory' ); ?>>子目录</option><option value="domain" <?php selected( $settings['routing_mode'], 'domain' ); ?>>绑定域名</option></select></td></tr>
|
||||||
<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="btranslate-domain-bindings">域名绑定</label></th><td><textarea id="btranslate-domain-bindings" name="btranslate_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="btranslate-baidu-app-id">百度应用 ID</label></th><td><input id="btranslate-baidu-app-id" name="btranslate_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"><label for="btranslate-baidu-secret-key">百度密钥</label></th><td><input id="btranslate-baidu-secret-key" name="btranslate_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>
|
<tr><th scope="row">请求日志</th><td><label for="btranslate-log-requests"><input id="btranslate-log-requests" name="btranslate_settings[log_requests]" type="checkbox" value="1" <?php checked( $settings['log_requests'] ); ?> /> 记录每次百度翻译请求</label><p class="description">日志写入 PHP 错误日志,包含语言、字段、文本指纹、长度和结果状态;不会记录密钥、原文、译文或完整 API 响应。</p></td></tr>
|
||||||
</table>
|
</table>
|
||||||
<?php submit_button(); ?>
|
<?php submit_button(); ?>
|
||||||
</form>
|
</form>
|
||||||
@@ -76,44 +76,44 @@ class WPT_Admin {
|
|||||||
<h2>重新翻译所有内容</h2>
|
<h2>重新翻译所有内容</h2>
|
||||||
<p>此操作会将所有已发布文章、页面、分类和标签重新加入翻译队列。</p>
|
<p>此操作会将所有已发布文章、页面、分类和标签重新加入翻译队列。</p>
|
||||||
<form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post" onsubmit="return window.confirm('这将重新翻译所有内容,请注意 API 消耗。确认继续吗?');">
|
<form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post" onsubmit="return window.confirm('这将重新翻译所有内容,请注意 API 消耗。确认继续吗?');">
|
||||||
<input type="hidden" name="action" value="wpt_queue_existing_translations" />
|
<input type="hidden" name="action" value="btranslate_queue_existing_translations" />
|
||||||
<?php wp_nonce_field( 'wpt_queue_existing_translations' ); ?>
|
<?php wp_nonce_field( 'btranslate_queue_existing_translations' ); ?>
|
||||||
<?php submit_button( '重新翻译所有内容', 'secondary', 'submit', false ); ?>
|
<?php submit_button( '重新翻译所有内容', 'secondary', 'submit', false ); ?>
|
||||||
</form>
|
</form>
|
||||||
<p>也可以只重新翻译一种内容类型。</p>
|
<p>也可以只重新翻译一种内容类型。</p>
|
||||||
<form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post" style="display:inline-block;margin-right:8px" onsubmit="return window.confirm('这将重新翻译所有文章和页面,请注意 API 消耗。确认继续吗?');">
|
<form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post" style="display:inline-block;margin-right:8px" onsubmit="return window.confirm('这将重新翻译所有文章和页面,请注意 API 消耗。确认继续吗?');">
|
||||||
<input type="hidden" name="action" value="wpt_queue_existing_translations" />
|
<input type="hidden" name="action" value="btranslate_queue_existing_translations" />
|
||||||
<input type="hidden" name="scope" value="posts" />
|
<input type="hidden" name="scope" value="posts" />
|
||||||
<?php wp_nonce_field( 'wpt_queue_existing_translations' ); ?>
|
<?php wp_nonce_field( 'btranslate_queue_existing_translations' ); ?>
|
||||||
<?php submit_button( '翻译所有文章', 'secondary', 'submit', false ); ?>
|
<?php submit_button( '翻译所有文章', 'secondary', 'submit', false ); ?>
|
||||||
</form>
|
</form>
|
||||||
<form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post" style="display:inline-block" onsubmit="return window.confirm('这将重新翻译所有分类和标签,请注意 API 消耗。确认继续吗?');">
|
<form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post" style="display:inline-block" onsubmit="return window.confirm('这将重新翻译所有分类和标签,请注意 API 消耗。确认继续吗?');">
|
||||||
<input type="hidden" name="action" value="wpt_queue_existing_translations" />
|
<input type="hidden" name="action" value="btranslate_queue_existing_translations" />
|
||||||
<input type="hidden" name="scope" value="terms" />
|
<input type="hidden" name="scope" value="terms" />
|
||||||
<?php wp_nonce_field( 'wpt_queue_existing_translations' ); ?>
|
<?php wp_nonce_field( 'btranslate_queue_existing_translations' ); ?>
|
||||||
<?php submit_button( '翻译所有分类标签', 'secondary', 'submit', false ); ?>
|
<?php submit_button( '翻译所有分类标签', 'secondary', 'submit', false ); ?>
|
||||||
</form>
|
</form>
|
||||||
<hr />
|
<hr />
|
||||||
<h2>翻译缓存</h2>
|
<h2>翻译缓存</h2>
|
||||||
<p>清除后,前台会暂时显示源文;重新翻译时将再次调用百度翻译 API。</p>
|
<p>清除后,前台会暂时显示源文;重新翻译时将再次调用百度翻译 API。</p>
|
||||||
<form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post" onsubmit="return window.confirm('这将删除所有已保存的翻译、取消待执行翻译任务并重置进度。确认继续吗?');">
|
<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" />
|
<input type="hidden" name="action" value="btranslate_clear_translation_cache" />
|
||||||
<?php wp_nonce_field( 'wpt_clear_translation_cache' ); ?>
|
<?php wp_nonce_field( 'btranslate_clear_translation_cache' ); ?>
|
||||||
<?php submit_button( '清除已翻译的缓存', 'delete', 'submit', false ); ?>
|
<?php submit_button( '清除已翻译的缓存', 'delete', 'submit', false ); ?>
|
||||||
</form>
|
</form>
|
||||||
<hr />
|
<hr />
|
||||||
<h2>翻译进度</h2>
|
<h2>翻译进度</h2>
|
||||||
<div id="wpt-translation-progress" aria-live="polite">
|
<div id="btranslate-translation-progress" aria-live="polite">
|
||||||
<p>正在读取翻译进度...</p>
|
<p>正在读取翻译进度...</p>
|
||||||
</div>
|
</div>
|
||||||
<script>
|
<script>
|
||||||
(function () {
|
(function () {
|
||||||
var container = document.getElementById('wpt-translation-progress');
|
var container = document.getElementById('btranslate-translation-progress');
|
||||||
var endpoint = <?php echo wp_json_encode( admin_url( 'admin-ajax.php' ) ); ?>;
|
var endpoint = <?php echo wp_json_encode( admin_url( 'admin-ajax.php' ) ); ?>;
|
||||||
var nonce = <?php echo wp_json_encode( wp_create_nonce( 'wpt_translation_progress' ) ); ?>;
|
var nonce = <?php echo wp_json_encode( wp_create_nonce( 'btranslate_translation_progress' ) ); ?>;
|
||||||
|
|
||||||
function updateProgress() {
|
function updateProgress() {
|
||||||
fetch(endpoint + '?action=wpt_translation_progress&_ajax_nonce=' + encodeURIComponent(nonce), { credentials: 'same-origin' })
|
fetch(endpoint + '?action=btranslate_translation_progress&_ajax_nonce=' + encodeURIComponent(nonce), { credentials: 'same-origin' })
|
||||||
.then(function (response) { return response.json(); })
|
.then(function (response) { return response.json(); })
|
||||||
.then(function (response) {
|
.then(function (response) {
|
||||||
if (!response.success) {
|
if (!response.success) {
|
||||||
@@ -142,7 +142,7 @@ class WPT_Admin {
|
|||||||
wp_die( '您没有执行此操作的权限。' );
|
wp_die( '您没有执行此操作的权限。' );
|
||||||
}
|
}
|
||||||
|
|
||||||
check_admin_referer( 'wpt_queue_existing_translations' );
|
check_admin_referer( 'btranslate_queue_existing_translations' );
|
||||||
$scope = isset( $_POST['scope'] ) ? sanitize_key( wp_unslash( $_POST['scope'] ) ) : 'all';
|
$scope = isset( $_POST['scope'] ) ? sanitize_key( wp_unslash( $_POST['scope'] ) ) : 'all';
|
||||||
if ( ! in_array( $scope, array( 'all', 'posts', 'terms' ), true ) ) {
|
if ( ! in_array( $scope, array( 'all', 'posts', 'terms' ), true ) ) {
|
||||||
wp_die( '无效的翻译范围。' );
|
wp_die( '无效的翻译范围。' );
|
||||||
@@ -152,7 +152,7 @@ class WPT_Admin {
|
|||||||
$next_run = time();
|
$next_run = time();
|
||||||
$post_ids = array();
|
$post_ids = array();
|
||||||
$term_ids = array();
|
$term_ids = array();
|
||||||
$languages = WPT_Settings::target_languages();
|
$languages = BTRANSLATE_Settings::target_languages();
|
||||||
|
|
||||||
if ( 'all' === $scope || 'posts' === $scope ) {
|
if ( 'all' === $scope || 'posts' === $scope ) {
|
||||||
$post_ids = get_posts(
|
$post_ids = get_posts(
|
||||||
@@ -183,8 +183,8 @@ class WPT_Admin {
|
|||||||
|
|
||||||
foreach ( $post_ids as $post_id ) {
|
foreach ( $post_ids as $post_id ) {
|
||||||
foreach ( $languages as $target_language ) {
|
foreach ( $languages as $target_language ) {
|
||||||
$this->clear_scheduled_event( 'wpt_process_translation', array( $post_id, $target_language, true ) );
|
$this->clear_scheduled_event( 'btranslate_process_translation', array( $post_id, $target_language, true ) );
|
||||||
wp_schedule_single_event( $next_run, 'wpt_process_translation', array( $post_id, $target_language, true ) );
|
wp_schedule_single_event( $next_run, 'btranslate_process_translation', array( $post_id, $target_language, true ) );
|
||||||
$next_run += 2;
|
$next_run += 2;
|
||||||
++$scheduled;
|
++$scheduled;
|
||||||
}
|
}
|
||||||
@@ -192,14 +192,14 @@ class WPT_Admin {
|
|||||||
|
|
||||||
foreach ( $term_ids as $term_id ) {
|
foreach ( $term_ids as $term_id ) {
|
||||||
foreach ( $languages as $target_language ) {
|
foreach ( $languages as $target_language ) {
|
||||||
$this->clear_scheduled_event( 'wpt_process_term_translation', array( $term_id, $target_language, true ) );
|
$this->clear_scheduled_event( 'btranslate_process_term_translation', array( $term_id, $target_language, true ) );
|
||||||
wp_schedule_single_event( $next_run, 'wpt_process_term_translation', array( $term_id, $target_language, true ) );
|
wp_schedule_single_event( $next_run, 'btranslate_process_term_translation', array( $term_id, $target_language, true ) );
|
||||||
$next_run += 2;
|
$next_run += 2;
|
||||||
++$scheduled;
|
++$scheduled;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
wp_safe_redirect( add_query_arg( 'wpt_queued', $scheduled, admin_url( 'options-general.php?page=wp-btranslate' ) ) );
|
wp_safe_redirect( add_query_arg( 'btranslate_queued', $scheduled, admin_url( 'options-general.php?page=wp-btranslate' ) ) );
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,20 +208,20 @@ class WPT_Admin {
|
|||||||
wp_die( '您没有执行此操作的权限。' );
|
wp_die( '您没有执行此操作的权限。' );
|
||||||
}
|
}
|
||||||
|
|
||||||
check_admin_referer( 'wpt_clear_translation_cache' );
|
check_admin_referer( 'btranslate_clear_translation_cache' );
|
||||||
$deleted = ( new WPT_Translation_Store() )->clear();
|
$deleted = ( new BTRANSLATE_Translation_Store() )->clear();
|
||||||
|
|
||||||
if ( false === $deleted ) {
|
if ( false === $deleted ) {
|
||||||
wp_die( '翻译缓存清除失败。' );
|
wp_die( '翻译缓存清除失败。' );
|
||||||
}
|
}
|
||||||
|
|
||||||
wp_clear_scheduled_hook( 'wpt_process_translation' );
|
wp_clear_scheduled_hook( 'btranslate_process_translation' );
|
||||||
wp_clear_scheduled_hook( 'wpt_process_term_translation' );
|
wp_clear_scheduled_hook( 'btranslate_process_term_translation' );
|
||||||
wp_clear_scheduled_hook( 'wpt_process_seo_output_translation' );
|
wp_clear_scheduled_hook( 'btranslate_process_seo_output_translation' );
|
||||||
delete_option( 'wpt_translation_task' );
|
delete_option( 'btranslate_translation_task' );
|
||||||
delete_option( 'wpt_retranslation_batch' );
|
delete_option( 'btranslate_retranslation_batch' );
|
||||||
|
|
||||||
wp_safe_redirect( add_query_arg( 'wpt_cache_cleared', 1, admin_url( 'options-general.php?page=wp-btranslate' ) ) );
|
wp_safe_redirect( add_query_arg( 'btranslate_cache_cleared', 1, admin_url( 'options-general.php?page=wp-btranslate' ) ) );
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -238,18 +238,18 @@ class WPT_Admin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function translation_progress() {
|
public function translation_progress() {
|
||||||
check_ajax_referer( 'wpt_translation_progress' );
|
check_ajax_referer( 'btranslate_translation_progress' );
|
||||||
|
|
||||||
if ( ! current_user_can( 'manage_options' ) ) {
|
if ( ! current_user_can( 'manage_options' ) ) {
|
||||||
wp_send_json_error( array( 'message' => 'forbidden' ), 403 );
|
wp_send_json_error( array( 'message' => 'forbidden' ), 403 );
|
||||||
}
|
}
|
||||||
|
|
||||||
$task = (array) get_option( 'wpt_translation_task', array() );
|
$task = (array) get_option( 'btranslate_translation_task', array() );
|
||||||
$post_ids = array_values( array_filter( array_map( 'absint', (array) ( $task['post_ids'] ?? array() ) ) ) );
|
$post_ids = array_values( array_filter( array_map( 'absint', (array) ( $task['post_ids'] ?? array() ) ) ) );
|
||||||
$term_ids = array_values( array_filter( array_map( 'absint', (array) ( $task['term_ids'] ?? array() ) ) ) );
|
$term_ids = array_values( array_filter( array_map( 'absint', (array) ( $task['term_ids'] ?? array() ) ) ) );
|
||||||
$languages = array_values( array_filter( array_map( 'sanitize_key', (array) ( $task['target_languages'] ?? array() ) ) ) );
|
$languages = array_values( array_filter( array_map( 'sanitize_key', (array) ( $task['target_languages'] ?? array() ) ) ) );
|
||||||
$started_at = isset( $task['started_at'] ) ? sanitize_text_field( $task['started_at'] ) : '';
|
$started_at = isset( $task['started_at'] ) ? sanitize_text_field( $task['started_at'] ) : '';
|
||||||
$counts = ( new WPT_Translation_Store() )->get_completed_item_counts( $languages, $post_ids, $term_ids, $started_at );
|
$counts = ( new BTRANSLATE_Translation_Store() )->get_completed_item_counts( $languages, $post_ids, $term_ids, $started_at );
|
||||||
|
|
||||||
$posts_total = count( $post_ids ) * count( $languages );
|
$posts_total = count( $post_ids ) * count( $languages );
|
||||||
$terms_total = count( $term_ids ) * count( $languages );
|
$terms_total = count( $term_ids ) * count( $languages );
|
||||||
@@ -275,33 +275,33 @@ class WPT_Admin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function add_translation_column( $columns ) {
|
public function add_translation_column( $columns ) {
|
||||||
$columns['wpt_translation'] = '翻译状态';
|
$columns['btranslate_translation'] = '翻译状态';
|
||||||
|
|
||||||
return $columns;
|
return $columns;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function render_translation_column( $column, $post_id ) {
|
public function render_translation_column( $column, $post_id ) {
|
||||||
if ( 'wpt_translation' !== $column ) {
|
if ( 'btranslate_translation' !== $column ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$store = new WPT_Translation_Store();
|
$store = new BTRANSLATE_Translation_Store();
|
||||||
|
|
||||||
foreach ( WPT_Settings::target_languages() as $target_language ) {
|
foreach ( BTRANSLATE_Settings::target_languages() as $target_language ) {
|
||||||
$status = $store->get_post_language_status( $post_id, $target_language );
|
$status = $store->get_post_language_status( $post_id, $target_language );
|
||||||
$translated = ! empty( $status['translated_fields'] );
|
$translated = ! empty( $status['translated_fields'] );
|
||||||
$button_text = $translated ? '重新翻译' : '翻译';
|
$button_text = $translated ? '重新翻译' : '翻译';
|
||||||
$action_url = wp_nonce_url(
|
$action_url = wp_nonce_url(
|
||||||
add_query_arg(
|
add_query_arg(
|
||||||
array(
|
array(
|
||||||
'action' => 'wpt_translate_post',
|
'action' => 'btranslate_translate_post',
|
||||||
'post_id' => $post_id,
|
'post_id' => $post_id,
|
||||||
'language' => $target_language,
|
'language' => $target_language,
|
||||||
'force' => $translated ? '1' : '0',
|
'force' => $translated ? '1' : '0',
|
||||||
),
|
),
|
||||||
admin_url( 'admin-post.php' )
|
admin_url( 'admin-post.php' )
|
||||||
),
|
),
|
||||||
'wpt_translate_post_' . $post_id . '_' . $target_language
|
'btranslate_translate_post_' . $post_id . '_' . $target_language
|
||||||
);
|
);
|
||||||
|
|
||||||
echo '<p><strong>' . esc_html( strtoupper( $target_language ) ) . '</strong>: ';
|
echo '<p><strong>' . esc_html( strtoupper( $target_language ) ) . '</strong>: ';
|
||||||
@@ -318,13 +318,13 @@ class WPT_Admin {
|
|||||||
$target_language = isset( $_GET['language'] ) ? sanitize_key( wp_unslash( $_GET['language'] ) ) : '';
|
$target_language = isset( $_GET['language'] ) ? sanitize_key( wp_unslash( $_GET['language'] ) ) : '';
|
||||||
$force_refresh = ! empty( $_GET['force'] );
|
$force_refresh = ! empty( $_GET['force'] );
|
||||||
|
|
||||||
if ( ! $post_id || ! current_user_can( 'edit_post', $post_id ) || ! WPT_Settings::is_supported_language( $target_language ) || $target_language === WPT_Settings::get()['source_language'] ) {
|
if ( ! $post_id || ! current_user_can( 'edit_post', $post_id ) || ! BTRANSLATE_Settings::is_supported_language( $target_language ) || $target_language === BTRANSLATE_Settings::get()['source_language'] ) {
|
||||||
wp_die( '无效的翻译请求。' );
|
wp_die( '无效的翻译请求。' );
|
||||||
}
|
}
|
||||||
|
|
||||||
check_admin_referer( 'wpt_translate_post_' . $post_id . '_' . $target_language );
|
check_admin_referer( 'btranslate_translate_post_' . $post_id . '_' . $target_language );
|
||||||
$this->update_translation_task( array( $post_id ), array(), array( $target_language ) );
|
$this->update_translation_task( array( $post_id ), array(), array( $target_language ) );
|
||||||
wp_schedule_single_event( time() + 5, 'wpt_process_translation', array( $post_id, $target_language, $force_refresh ) );
|
wp_schedule_single_event( time() + 5, 'btranslate_process_translation', array( $post_id, $target_language, $force_refresh ) );
|
||||||
|
|
||||||
wp_safe_redirect( wp_get_referer() ? wp_get_referer() : admin_url( 'edit.php?post_type=' . get_post_type( $post_id ) ) );
|
wp_safe_redirect( wp_get_referer() ? wp_get_referer() : admin_url( 'edit.php?post_type=' . get_post_type( $post_id ) ) );
|
||||||
exit;
|
exit;
|
||||||
@@ -332,7 +332,7 @@ class WPT_Admin {
|
|||||||
|
|
||||||
private function update_translation_task( $post_ids, $term_ids, $target_languages ) {
|
private function update_translation_task( $post_ids, $term_ids, $target_languages ) {
|
||||||
update_option(
|
update_option(
|
||||||
'wpt_translation_task',
|
'btranslate_translation_task',
|
||||||
array(
|
array(
|
||||||
'started_at' => current_time( 'mysql', true ),
|
'started_at' => current_time( 'mysql', true ),
|
||||||
'post_ids' => array_values( array_filter( array_map( 'absint', (array) $post_ids ) ) ),
|
'post_ids' => array_values( array_filter( array_map( 'absint', (array) $post_ids ) ) ),
|
||||||
+8
-8
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
defined( 'ABSPATH' ) || exit;
|
defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
class WPT_Baidu_Provider implements WPT_Translation_Provider {
|
class BTRANSLATE_Baidu_Provider implements BTRANSLATE_Translation_Provider {
|
||||||
private $app_id;
|
private $app_id;
|
||||||
private $secret_key;
|
private $secret_key;
|
||||||
|
|
||||||
@@ -19,7 +19,7 @@ class WPT_Baidu_Provider implements WPT_Translation_Provider {
|
|||||||
if ( '' === $this->app_id || '' === $this->secret_key ) {
|
if ( '' === $this->app_id || '' === $this->secret_key ) {
|
||||||
$this->log_request( $source_value, $source_language, $target_language, $context, 'missing_credentials' );
|
$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' ) );
|
return BTRANSLATE_Translation_Result::failure( 'missing_credentials', __( 'Baidu Translate credentials are not configured.', 'wp-btranslate' ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
$salt = (string) wp_rand( 100000, 999999 );
|
$salt = (string) wp_rand( 100000, 999999 );
|
||||||
@@ -42,7 +42,7 @@ class WPT_Baidu_Provider implements WPT_Translation_Provider {
|
|||||||
if ( is_wp_error( $response ) ) {
|
if ( is_wp_error( $response ) ) {
|
||||||
$this->log_request( $source_value, $source_language, $target_language, $context, 'request_failed', $response->get_error_code() );
|
$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() );
|
return BTRANSLATE_Translation_Result::failure( 'request_failed', $response->get_error_message() );
|
||||||
}
|
}
|
||||||
|
|
||||||
$body = json_decode( wp_remote_retrieve_body( $response ), true );
|
$body = json_decode( wp_remote_retrieve_body( $response ), true );
|
||||||
@@ -52,17 +52,17 @@ class WPT_Baidu_Provider implements WPT_Translation_Provider {
|
|||||||
$error_message = isset( $body['error_msg'] ) ? sanitize_text_field( $body['error_msg'] ) : __( 'Baidu Translate returned an invalid response.', 'wp-btranslate' );
|
$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 );
|
$this->log_request( $source_value, $source_language, $target_language, $context, 'failed', $error_code );
|
||||||
|
|
||||||
return WPT_Translation_Result::failure( $error_code, $error_message );
|
return BTRANSLATE_Translation_Result::failure( $error_code, $error_message );
|
||||||
}
|
}
|
||||||
|
|
||||||
$translated_parts = wp_list_pluck( $body['trans_result'], 'dst' );
|
$translated_parts = wp_list_pluck( $body['trans_result'], 'dst' );
|
||||||
$this->log_request( $source_value, $source_language, $target_language, $context, 'complete' );
|
$this->log_request( $source_value, $source_language, $target_language, $context, 'complete' );
|
||||||
|
|
||||||
return WPT_Translation_Result::success( implode( "\n", $translated_parts ) );
|
return BTRANSLATE_Translation_Result::success( implode( "\n", $translated_parts ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
private function log_request( $source_value, $source_language, $target_language, $context, $status, $error_code = '' ) {
|
private function log_request( $source_value, $source_language, $target_language, $context, $status, $error_code = '' ) {
|
||||||
$settings = WPT_Settings::get();
|
$settings = BTRANSLATE_Settings::get();
|
||||||
|
|
||||||
if ( empty( $settings['log_requests'] ) ) {
|
if ( empty( $settings['log_requests'] ) ) {
|
||||||
return;
|
return;
|
||||||
@@ -73,12 +73,12 @@ class WPT_Baidu_Provider implements WPT_Translation_Provider {
|
|||||||
'source_language' => sanitize_key( $source_language ),
|
'source_language' => sanitize_key( $source_language ),
|
||||||
'target_language' => sanitize_key( $target_language ),
|
'target_language' => sanitize_key( $target_language ),
|
||||||
'context' => sanitize_key( $context ),
|
'context' => sanitize_key( $context ),
|
||||||
'source_fingerprint' => WPT_Translation_Identity::source_fingerprint( (string) $source_value ),
|
'source_fingerprint' => BTRANSLATE_Translation_Identity::source_fingerprint( (string) $source_value ),
|
||||||
'source_length' => strlen( (string) $source_value ),
|
'source_length' => strlen( (string) $source_value ),
|
||||||
'status' => sanitize_key( $status ),
|
'status' => sanitize_key( $status ),
|
||||||
'error_code' => sanitize_key( $error_code ),
|
'error_code' => sanitize_key( $error_code ),
|
||||||
);
|
);
|
||||||
|
|
||||||
error_log( 'WPT translation request: ' . wp_json_encode( $entry ) );
|
error_log( 'BTRANSLATE translation request: ' . wp_json_encode( $entry ) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+33
-33
@@ -2,20 +2,20 @@
|
|||||||
|
|
||||||
defined( 'ABSPATH' ) || exit;
|
defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
class WPT_Content_Controller {
|
class BTRANSLATE_Content_Controller {
|
||||||
private $store;
|
private $store;
|
||||||
private $router;
|
private $router;
|
||||||
|
|
||||||
public function __construct( WPT_Translation_Store $store, WPT_Language_Router $router ) {
|
public function __construct( BTRANSLATE_Translation_Store $store, BTRANSLATE_Language_Router $router ) {
|
||||||
$this->store = $store;
|
$this->store = $store;
|
||||||
$this->router = $router;
|
$this->router = $router;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function register() {
|
public function register() {
|
||||||
add_action( 'save_post', array( $this, 'schedule_post_translation' ), 20, 3 );
|
add_action( 'save_post', array( $this, 'schedule_post_translation' ), 20, 3 );
|
||||||
add_action( 'wpt_process_translation', array( $this, 'translate_post' ), 10, 3 );
|
add_action( 'btranslate_process_translation', array( $this, 'translate_post' ), 10, 3 );
|
||||||
add_action( 'wpt_process_term_translation', array( $this, 'translate_term' ), 10, 3 );
|
add_action( 'btranslate_process_term_translation', array( $this, 'translate_term' ), 10, 3 );
|
||||||
add_action( 'wpt_process_seo_output_translation', array( $this, 'translate_seo_output' ), 10, 4 );
|
add_action( 'btranslate_process_seo_output_translation', array( $this, 'translate_seo_output' ), 10, 4 );
|
||||||
add_action( 'created_term', array( $this, 'schedule_term_translation' ), 20, 3 );
|
add_action( 'created_term', array( $this, 'schedule_term_translation' ), 20, 3 );
|
||||||
add_action( 'edited_term', array( $this, 'schedule_term_translation' ), 20, 3 );
|
add_action( 'edited_term', array( $this, 'schedule_term_translation' ), 20, 3 );
|
||||||
add_filter( 'the_title', array( $this, 'translate_title' ), 20, 2 );
|
add_filter( 'the_title', array( $this, 'translate_title' ), 20, 2 );
|
||||||
@@ -47,24 +47,24 @@ class WPT_Content_Controller {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ( WPT_Settings::target_languages() as $target_language ) {
|
foreach ( BTRANSLATE_Settings::target_languages() as $target_language ) {
|
||||||
if ( ! wp_next_scheduled( 'wpt_process_translation', array( $post_id, $target_language ) ) ) {
|
if ( ! wp_next_scheduled( 'btranslate_process_translation', array( $post_id, $target_language ) ) ) {
|
||||||
wp_schedule_single_event( time() + 30, 'wpt_process_translation', array( $post_id, $target_language ) );
|
wp_schedule_single_event( time() + 30, 'btranslate_process_translation', array( $post_id, $target_language ) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function translate_post( $post_id, $target_language, $force_refresh = false ) {
|
public function translate_post( $post_id, $target_language, $force_refresh = false ) {
|
||||||
$post = get_post( $post_id );
|
$post = get_post( $post_id );
|
||||||
$settings = WPT_Settings::get();
|
$settings = BTRANSLATE_Settings::get();
|
||||||
|
|
||||||
if ( ! $post || ! WPT_Settings::is_supported_language( $target_language ) ) {
|
if ( ! $post || ! BTRANSLATE_Settings::is_supported_language( $target_language ) ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$service = new WPT_Translation_Service(
|
$service = new BTRANSLATE_Translation_Service(
|
||||||
$this->store,
|
$this->store,
|
||||||
new WPT_Baidu_Provider( $settings['baidu_app_id'], $settings['baidu_secret_key'] )
|
new BTRANSLATE_Baidu_Provider( $settings['baidu_app_id'], $settings['baidu_secret_key'] )
|
||||||
);
|
);
|
||||||
|
|
||||||
$fields = array(
|
$fields = array(
|
||||||
@@ -91,7 +91,7 @@ class WPT_Content_Controller {
|
|||||||
if ( '' !== $value ) {
|
if ( '' !== $value ) {
|
||||||
$context = 'post:' . $post_id . ':' . $field;
|
$context = 'post:' . $post_id . ':' . $field;
|
||||||
if ( 'post_content' === $field ) {
|
if ( 'post_content' === $field ) {
|
||||||
( new WPT_Content_Translator( $service ) )->translate( $value, $settings['source_language'], $target_language, $context, $force_refresh );
|
( new BTRANSLATE_Content_Translator( $service ) )->translate( $value, $settings['source_language'], $target_language, $context, $force_refresh );
|
||||||
} else {
|
} else {
|
||||||
$service->get_or_translate( $value, $settings['source_language'], $target_language, $context, $force_refresh );
|
$service->get_or_translate( $value, $settings['source_language'], $target_language, $context, $force_refresh );
|
||||||
}
|
}
|
||||||
@@ -107,24 +107,24 @@ class WPT_Content_Controller {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ( WPT_Settings::target_languages() as $target_language ) {
|
foreach ( BTRANSLATE_Settings::target_languages() as $target_language ) {
|
||||||
if ( ! wp_next_scheduled( 'wpt_process_term_translation', array( $term_id, $target_language, false ) ) ) {
|
if ( ! wp_next_scheduled( 'btranslate_process_term_translation', array( $term_id, $target_language, false ) ) ) {
|
||||||
wp_schedule_single_event( time() + 30, 'wpt_process_term_translation', array( $term_id, $target_language, false ) );
|
wp_schedule_single_event( time() + 30, 'btranslate_process_term_translation', array( $term_id, $target_language, false ) );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function translate_term( $term_id, $target_language, $force_refresh = false ) {
|
public function translate_term( $term_id, $target_language, $force_refresh = false ) {
|
||||||
$term = get_term( $term_id );
|
$term = get_term( $term_id );
|
||||||
$settings = WPT_Settings::get();
|
$settings = BTRANSLATE_Settings::get();
|
||||||
|
|
||||||
if ( ! $term || is_wp_error( $term ) || ! in_array( $term->taxonomy, array( 'category', 'post_tag' ), true ) || ! WPT_Settings::is_supported_language( $target_language ) ) {
|
if ( ! $term || is_wp_error( $term ) || ! in_array( $term->taxonomy, array( 'category', 'post_tag' ), true ) || ! BTRANSLATE_Settings::is_supported_language( $target_language ) ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$service = new WPT_Translation_Service(
|
$service = new BTRANSLATE_Translation_Service(
|
||||||
$this->store,
|
$this->store,
|
||||||
new WPT_Baidu_Provider( $settings['baidu_app_id'], $settings['baidu_secret_key'] )
|
new BTRANSLATE_Baidu_Provider( $settings['baidu_app_id'], $settings['baidu_secret_key'] )
|
||||||
);
|
);
|
||||||
|
|
||||||
if ( '' !== $term->name ) {
|
if ( '' !== $term->name ) {
|
||||||
@@ -136,15 +136,15 @@ class WPT_Content_Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function translate_seo_output( $post_id, $source_value, $target_language, $context ) {
|
public function translate_seo_output( $post_id, $source_value, $target_language, $context ) {
|
||||||
$settings = WPT_Settings::get();
|
$settings = BTRANSLATE_Settings::get();
|
||||||
|
|
||||||
if ( ! get_post( $post_id ) || ! WPT_Settings::is_supported_language( $target_language ) || '' === $source_value ) {
|
if ( ! get_post( $post_id ) || ! BTRANSLATE_Settings::is_supported_language( $target_language ) || '' === $source_value ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$service = new WPT_Translation_Service(
|
$service = new BTRANSLATE_Translation_Service(
|
||||||
$this->store,
|
$this->store,
|
||||||
new WPT_Baidu_Provider( $settings['baidu_app_id'], $settings['baidu_secret_key'] )
|
new BTRANSLATE_Baidu_Provider( $settings['baidu_app_id'], $settings['baidu_secret_key'] )
|
||||||
);
|
);
|
||||||
|
|
||||||
$service->get_or_translate( $source_value, $settings['source_language'], $target_language, $context );
|
$service->get_or_translate( $source_value, $settings['source_language'], $target_language, $context );
|
||||||
@@ -160,18 +160,18 @@ class WPT_Content_Controller {
|
|||||||
return $this->translated_value( $content, 'post:' . $post_id . ':post_content' );
|
return $this->translated_value( $content, 'post:' . $post_id . ':post_content' );
|
||||||
}
|
}
|
||||||
|
|
||||||
$segments = WPT_Content_Translator::segments( $content );
|
$segments = BTRANSLATE_Content_Translator::segments( $content );
|
||||||
if ( false === $segments ) {
|
if ( false === $segments ) {
|
||||||
return $content;
|
return $content;
|
||||||
}
|
}
|
||||||
|
|
||||||
$text_index = 0;
|
$text_index = 0;
|
||||||
foreach ( $segments as $index => $segment ) {
|
foreach ( $segments as $index => $segment ) {
|
||||||
if ( WPT_Content_Translator::is_tag( $segment ) || '' === trim( $segment ) ) {
|
if ( BTRANSLATE_Content_Translator::is_tag( $segment ) || '' === trim( $segment ) ) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$whitespace = WPT_Content_Translator::surrounding_whitespace( $segment );
|
$whitespace = BTRANSLATE_Content_Translator::surrounding_whitespace( $segment );
|
||||||
$translated = $this->translated_value( $whitespace['text'], 'post:' . $post_id . ':post_content:text:' . $text_index );
|
$translated = $this->translated_value( $whitespace['text'], 'post:' . $post_id . ':post_content:text:' . $text_index );
|
||||||
$segments[ $index ] = $whitespace['leading'] . $translated . $whitespace['trailing'];
|
$segments[ $index ] = $whitespace['leading'] . $translated . $whitespace['trailing'];
|
||||||
++$text_index;
|
++$text_index;
|
||||||
@@ -306,22 +306,22 @@ class WPT_Content_Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function translated_value( $source_value, $context ) {
|
private function translated_value( $source_value, $context ) {
|
||||||
$settings = WPT_Settings::get();
|
$settings = BTRANSLATE_Settings::get();
|
||||||
$target_language = $this->router->current_language();
|
$target_language = $this->router->current_language();
|
||||||
|
|
||||||
if ( $target_language === $settings['source_language'] || '' === $source_value ) {
|
if ( $target_language === $settings['source_language'] || '' === $source_value ) {
|
||||||
return $source_value;
|
return $source_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
$provider = new WPT_Baidu_Provider( $settings['baidu_app_id'], $settings['baidu_secret_key'] );
|
$provider = new BTRANSLATE_Baidu_Provider( $settings['baidu_app_id'], $settings['baidu_secret_key'] );
|
||||||
$identity_key = WPT_Translation_Identity::key( $source_value, $settings['source_language'], $target_language, $context, $provider->get_version() );
|
$identity_key = BTRANSLATE_Translation_Identity::key( $source_value, $settings['source_language'], $target_language, $context, $provider->get_version() );
|
||||||
$translation = $this->store->find_valid( $identity_key );
|
$translation = $this->store->find_valid( $identity_key );
|
||||||
|
|
||||||
return ! empty( $translation['translated_value'] ) ? $translation['translated_value'] : $source_value;
|
return ! empty( $translation['translated_value'] ) ? $translation['translated_value'] : $source_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function translated_or_schedule_seo_output( $source_value, $post_id, $field_context ) {
|
private function translated_or_schedule_seo_output( $source_value, $post_id, $field_context ) {
|
||||||
$settings = WPT_Settings::get();
|
$settings = BTRANSLATE_Settings::get();
|
||||||
$target_language = $this->router->current_language();
|
$target_language = $this->router->current_language();
|
||||||
$context = 'post:' . $post_id . ':' . $field_context;
|
$context = 'post:' . $post_id . ':' . $field_context;
|
||||||
|
|
||||||
@@ -335,8 +335,8 @@ class WPT_Content_Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$event_args = array( $post_id, $source_value, $target_language, $context );
|
$event_args = array( $post_id, $source_value, $target_language, $context );
|
||||||
if ( ! wp_next_scheduled( 'wpt_process_seo_output_translation', $event_args ) ) {
|
if ( ! wp_next_scheduled( 'btranslate_process_seo_output_translation', $event_args ) ) {
|
||||||
wp_schedule_single_event( time() + 5, 'wpt_process_seo_output_translation', $event_args );
|
wp_schedule_single_event( time() + 5, 'btranslate_process_seo_output_translation', $event_args );
|
||||||
}
|
}
|
||||||
|
|
||||||
return $source_value;
|
return $source_value;
|
||||||
+4
-4
@@ -2,17 +2,17 @@
|
|||||||
|
|
||||||
defined( 'ABSPATH' ) || exit;
|
defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
class WPT_Content_Translator {
|
class BTRANSLATE_Content_Translator {
|
||||||
private $service;
|
private $service;
|
||||||
|
|
||||||
public function __construct( WPT_Translation_Service $service ) {
|
public function __construct( BTRANSLATE_Translation_Service $service ) {
|
||||||
$this->service = $service;
|
$this->service = $service;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function translate( $content, $source_language, $target_language, $context, $force_refresh = false ) {
|
public function translate( $content, $source_language, $target_language, $context, $force_refresh = false ) {
|
||||||
$segments = self::segments( $content );
|
$segments = self::segments( $content );
|
||||||
if ( false === $segments ) {
|
if ( false === $segments ) {
|
||||||
return WPT_Translation_Result::failure( 'content_parse_failed', 'Unable to protect links before translation.' );
|
return BTRANSLATE_Translation_Result::failure( 'content_parse_failed', 'Unable to protect links before translation.' );
|
||||||
}
|
}
|
||||||
|
|
||||||
$text_index = 0;
|
$text_index = 0;
|
||||||
@@ -29,7 +29,7 @@ class WPT_Content_Translator {
|
|||||||
++$text_index;
|
++$text_index;
|
||||||
}
|
}
|
||||||
|
|
||||||
return WPT_Translation_Result::success( $content );
|
return BTRANSLATE_Translation_Result::success( $content );
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function segments( $content ) {
|
public static function segments( $content ) {
|
||||||
+16
-16
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
defined( 'ABSPATH' ) || exit;
|
defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
class WPT_Language_Router {
|
class BTRANSLATE_Language_Router {
|
||||||
public function register() {
|
public function register() {
|
||||||
add_filter( 'query_vars', array( $this, 'query_vars' ) );
|
add_filter( 'query_vars', array( $this, 'query_vars' ) );
|
||||||
add_action( 'parse_request', array( $this, 'resolve_domain_language' ) );
|
add_action( 'parse_request', array( $this, 'resolve_domain_language' ) );
|
||||||
@@ -11,42 +11,42 @@ class WPT_Language_Router {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function register_rewrite_rules() {
|
public function register_rewrite_rules() {
|
||||||
if ( 'subdirectory' !== WPT_Settings::get()['routing_mode'] ) {
|
if ( 'subdirectory' !== BTRANSLATE_Settings::get()['routing_mode'] ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$language_pattern = implode( '|', array_map( 'preg_quote', array_map( 'strtolower', WPT_Settings::target_languages() ) ) );
|
$language_pattern = implode( '|', array_map( 'preg_quote', array_map( 'strtolower', BTRANSLATE_Settings::target_languages() ) ) );
|
||||||
|
|
||||||
if ( '' === $language_pattern ) {
|
if ( '' === $language_pattern ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
add_rewrite_tag( '%wpt_language%', '(' . $language_pattern . ')' );
|
add_rewrite_tag( '%btranslate_language%', '(' . $language_pattern . ')' );
|
||||||
add_rewrite_rule( '^(' . $language_pattern . ')/(.*)$', 'index.php?wpt_language=$matches[1]&pagename=$matches[2]', 'top' );
|
add_rewrite_rule( '^(' . $language_pattern . ')/(.*)$', 'index.php?btranslate_language=$matches[1]&pagename=$matches[2]', 'top' );
|
||||||
add_rewrite_rule( '^(' . $language_pattern . ')/?$', 'index.php?wpt_language=$matches[1]', 'top' );
|
add_rewrite_rule( '^(' . $language_pattern . ')/?$', 'index.php?btranslate_language=$matches[1]', 'top' );
|
||||||
}
|
}
|
||||||
|
|
||||||
public function query_vars( $query_vars ) {
|
public function query_vars( $query_vars ) {
|
||||||
$query_vars[] = 'wpt_language';
|
$query_vars[] = 'btranslate_language';
|
||||||
|
|
||||||
return $query_vars;
|
return $query_vars;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function resolve_domain_language( $wp ) {
|
public function resolve_domain_language( $wp ) {
|
||||||
$settings = WPT_Settings::get();
|
$settings = BTRANSLATE_Settings::get();
|
||||||
$host = isset( $_SERVER['HTTP_HOST'] ) ? WPT_Settings::normalize_domain( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : '';
|
$host = isset( $_SERVER['HTTP_HOST'] ) ? BTRANSLATE_Settings::normalize_domain( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : '';
|
||||||
$bindings = (array) $settings['domain_bindings'];
|
$bindings = (array) $settings['domain_bindings'];
|
||||||
|
|
||||||
if ( 'domain' === $settings['routing_mode'] && '' !== $host && isset( $bindings[ $host ] ) && WPT_Settings::is_supported_language( $bindings[ $host ] ) ) {
|
if ( 'domain' === $settings['routing_mode'] && '' !== $host && isset( $bindings[ $host ] ) && BTRANSLATE_Settings::is_supported_language( $bindings[ $host ] ) ) {
|
||||||
$wp->query_vars['wpt_language'] = sanitize_key( $bindings[ $host ] );
|
$wp->query_vars['btranslate_language'] = sanitize_key( $bindings[ $host ] );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function current_language() {
|
public function current_language() {
|
||||||
$settings = WPT_Settings::get();
|
$settings = BTRANSLATE_Settings::get();
|
||||||
$language = get_query_var( 'wpt_language' );
|
$language = get_query_var( 'btranslate_language' );
|
||||||
|
|
||||||
if ( $language && WPT_Settings::is_supported_language( $language ) ) {
|
if ( $language && BTRANSLATE_Settings::is_supported_language( $language ) ) {
|
||||||
return sanitize_key( $language );
|
return sanitize_key( $language );
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,10 +54,10 @@ class WPT_Language_Router {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function localized_url( $url ) {
|
public function localized_url( $url ) {
|
||||||
$settings = WPT_Settings::get();
|
$settings = BTRANSLATE_Settings::get();
|
||||||
$language = $this->current_language();
|
$language = $this->current_language();
|
||||||
|
|
||||||
if ( $language === $settings['source_language'] || ! WPT_Settings::is_supported_language( $language ) ) {
|
if ( $language === $settings['source_language'] || ! BTRANSLATE_Settings::is_supported_language( $language ) ) {
|
||||||
return $url;
|
return $url;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
defined( 'ABSPATH' ) || exit;
|
defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
class WPT_Plugin {
|
class BTRANSLATE_Plugin {
|
||||||
private static $instance;
|
private static $instance;
|
||||||
private $router;
|
private $router;
|
||||||
private $sitemap_controller;
|
private $sitemap_controller;
|
||||||
@@ -10,13 +10,13 @@ class WPT_Plugin {
|
|||||||
private $admin;
|
private $admin;
|
||||||
|
|
||||||
private function __construct() {
|
private function __construct() {
|
||||||
$this->router = new WPT_Language_Router();
|
$this->router = new BTRANSLATE_Language_Router();
|
||||||
$this->sitemap_controller = new WPT_Sitemap_Controller( $this->router );
|
$this->sitemap_controller = new BTRANSLATE_Sitemap_Controller( $this->router );
|
||||||
$this->content_controller = new WPT_Content_Controller( new WPT_Translation_Store(), $this->router );
|
$this->content_controller = new BTRANSLATE_Content_Controller( new BTRANSLATE_Translation_Store(), $this->router );
|
||||||
$this->admin = new WPT_Admin();
|
$this->admin = new BTRANSLATE_Admin();
|
||||||
|
|
||||||
add_action( 'init', array( $this, 'register' ) );
|
add_action( 'init', array( $this, 'register' ) );
|
||||||
add_action( 'update_option_wpt_settings', array( $this, 'refresh_rewrite_rules' ), 10, 2 );
|
add_action( 'update_option_btranslate_settings', array( $this, 'refresh_rewrite_rules' ), 10, 2 );
|
||||||
$this->admin->register();
|
$this->admin->register();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,9 +29,9 @@ class WPT_Plugin {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static function activate() {
|
public static function activate() {
|
||||||
WPT_Translation_Store::install();
|
BTRANSLATE_Translation_Store::install();
|
||||||
add_option(
|
add_option(
|
||||||
'wpt_settings',
|
'btranslate_settings',
|
||||||
array(
|
array(
|
||||||
'source_language' => 'zh',
|
'source_language' => 'zh',
|
||||||
'target_languages' => array( 'en' ),
|
'target_languages' => array( 'en' ),
|
||||||
@@ -42,13 +42,15 @@ class WPT_Plugin {
|
|||||||
'',
|
'',
|
||||||
false
|
false
|
||||||
);
|
);
|
||||||
$router = new WPT_Language_Router();
|
$router = new BTRANSLATE_Language_Router();
|
||||||
$router->register_rewrite_rules();
|
$router->register_rewrite_rules();
|
||||||
flush_rewrite_rules();
|
flush_rewrite_rules();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function deactivate() {
|
public static function deactivate() {
|
||||||
wp_clear_scheduled_hook( 'wpt_process_translation' );
|
wp_clear_scheduled_hook( 'btranslate_process_translation' );
|
||||||
|
wp_clear_scheduled_hook( 'btranslate_process_term_translation' );
|
||||||
|
wp_clear_scheduled_hook( 'btranslate_process_seo_output_translation' );
|
||||||
flush_rewrite_rules();
|
flush_rewrite_rules();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
defined( 'ABSPATH' ) || exit;
|
defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
class WPT_Settings {
|
class BTRANSLATE_Settings {
|
||||||
public static function get() {
|
public static function get() {
|
||||||
$defaults = array(
|
$defaults = array(
|
||||||
'source_language' => 'zh',
|
'source_language' => 'zh',
|
||||||
@@ -15,7 +15,7 @@ class WPT_Settings {
|
|||||||
'log_requests' => false,
|
'log_requests' => false,
|
||||||
);
|
);
|
||||||
|
|
||||||
return wp_parse_args( (array) get_option( 'wpt_settings', array() ), $defaults );
|
return wp_parse_args( (array) get_option( 'btranslate_settings', array() ), $defaults );
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function target_languages() {
|
public static function target_languages() {
|
||||||
+4
-4
@@ -2,10 +2,10 @@
|
|||||||
|
|
||||||
defined( 'ABSPATH' ) || exit;
|
defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
class WPT_Sitemap_Controller {
|
class BTRANSLATE_Sitemap_Controller {
|
||||||
private $router;
|
private $router;
|
||||||
|
|
||||||
public function __construct( WPT_Language_Router $router ) {
|
public function __construct( BTRANSLATE_Language_Router $router ) {
|
||||||
$this->router = $router;
|
$this->router = $router;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -14,10 +14,10 @@ class WPT_Sitemap_Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function maybe_serve_localized_sitemap() {
|
public function maybe_serve_localized_sitemap() {
|
||||||
$settings = WPT_Settings::get();
|
$settings = BTRANSLATE_Settings::get();
|
||||||
$language = $this->router->current_language();
|
$language = $this->router->current_language();
|
||||||
|
|
||||||
if ( $language === sanitize_key( $settings['source_language'] ) || ! in_array( $language, WPT_Settings::target_languages(), true ) ) {
|
if ( $language === sanitize_key( $settings['source_language'] ) || ! in_array( $language, BTRANSLATE_Settings::target_languages(), true ) ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
+1
-1
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
defined( 'ABSPATH' ) || exit;
|
defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
class WPT_Translation_Identity {
|
class BTRANSLATE_Translation_Identity {
|
||||||
public static function source_fingerprint( $source_value ) {
|
public static function source_fingerprint( $source_value ) {
|
||||||
return hash( 'sha256', (string) $source_value );
|
return hash( 'sha256', (string) $source_value );
|
||||||
}
|
}
|
||||||
+1
-1
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
defined( 'ABSPATH' ) || exit;
|
defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
class WPT_Translation_Result {
|
class BTRANSLATE_Translation_Result {
|
||||||
public $success;
|
public $success;
|
||||||
public $value;
|
public $value;
|
||||||
public $error_code;
|
public $error_code;
|
||||||
+5
-5
@@ -2,23 +2,23 @@
|
|||||||
|
|
||||||
defined( 'ABSPATH' ) || exit;
|
defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
class WPT_Translation_Service {
|
class BTRANSLATE_Translation_Service {
|
||||||
private $store;
|
private $store;
|
||||||
private $provider;
|
private $provider;
|
||||||
|
|
||||||
public function __construct( WPT_Translation_Store $store, WPT_Translation_Provider $provider ) {
|
public function __construct( BTRANSLATE_Translation_Store $store, BTRANSLATE_Translation_Provider $provider ) {
|
||||||
$this->store = $store;
|
$this->store = $store;
|
||||||
$this->provider = $provider;
|
$this->provider = $provider;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get_or_translate( $source_value, $source_language, $target_language, $context, $force_refresh = false ) {
|
public function get_or_translate( $source_value, $source_language, $target_language, $context, $force_refresh = false ) {
|
||||||
$source_value = (string) $source_value;
|
$source_value = (string) $source_value;
|
||||||
$source_fingerprint = WPT_Translation_Identity::source_fingerprint( $source_value );
|
$source_fingerprint = BTRANSLATE_Translation_Identity::source_fingerprint( $source_value );
|
||||||
$identity_key = WPT_Translation_Identity::key( $source_value, $source_language, $target_language, $context, $this->provider->get_version() );
|
$identity_key = BTRANSLATE_Translation_Identity::key( $source_value, $source_language, $target_language, $context, $this->provider->get_version() );
|
||||||
$existing = $force_refresh ? null : $this->store->find_valid( $identity_key );
|
$existing = $force_refresh ? null : $this->store->find_valid( $identity_key );
|
||||||
|
|
||||||
if ( ! empty( $existing['translated_value'] ) ) {
|
if ( ! empty( $existing['translated_value'] ) ) {
|
||||||
return WPT_Translation_Result::success( $existing['translated_value'] );
|
return BTRANSLATE_Translation_Result::success( $existing['translated_value'] );
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = $this->provider->translate( $source_value, $source_language, $target_language, $context );
|
$result = $this->provider->translate( $source_value, $source_language, $target_language, $context );
|
||||||
+2
-2
@@ -2,11 +2,11 @@
|
|||||||
|
|
||||||
defined( 'ABSPATH' ) || exit;
|
defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
class WPT_Translation_Store {
|
class BTRANSLATE_Translation_Store {
|
||||||
public static function table_name() {
|
public static function table_name() {
|
||||||
global $wpdb;
|
global $wpdb;
|
||||||
|
|
||||||
return $wpdb->prefix . 'wpt_translations';
|
return $wpdb->prefix . 'btranslate_translations';
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function install() {
|
public static function install() {
|
||||||
+1
-1
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
defined( 'ABSPATH' ) || exit;
|
defined( 'ABSPATH' ) || exit;
|
||||||
|
|
||||||
interface WPT_Translation_Provider {
|
interface BTRANSLATE_Translation_Provider {
|
||||||
public function get_version();
|
public function get_version();
|
||||||
|
|
||||||
public function translate( $source_value, $source_language, $target_language, $context );
|
public function translate( $source_value, $source_language, $target_language, $context );
|
||||||
+11
-11
@@ -2,24 +2,24 @@
|
|||||||
/**
|
/**
|
||||||
* Removes all data created by Btranslate.
|
* Removes all data created by Btranslate.
|
||||||
*
|
*
|
||||||
* @package WPT
|
* @package BTRANSLATE
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
|
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
function wpt_uninstall_site_data() {
|
function btranslate_uninstall_site_data() {
|
||||||
global $wpdb;
|
global $wpdb;
|
||||||
|
|
||||||
wp_clear_scheduled_hook( 'wpt_process_translation' );
|
wp_clear_scheduled_hook( 'btranslate_process_translation' );
|
||||||
wp_clear_scheduled_hook( 'wpt_process_term_translation' );
|
wp_clear_scheduled_hook( 'btranslate_process_term_translation' );
|
||||||
wp_clear_scheduled_hook( 'wpt_process_seo_output_translation' );
|
wp_clear_scheduled_hook( 'btranslate_process_seo_output_translation' );
|
||||||
delete_option( 'wpt_settings' );
|
delete_option( 'btranslate_settings' );
|
||||||
delete_option( 'wpt_translation_task' );
|
delete_option( 'btranslate_translation_task' );
|
||||||
delete_option( 'wpt_retranslation_batch' );
|
delete_option( 'btranslate_retranslation_batch' );
|
||||||
|
|
||||||
$table_name = $wpdb->prefix . 'wpt_translations';
|
$table_name = $wpdb->prefix . 'btranslate_translations';
|
||||||
$wpdb->query( "DROP TABLE IF EXISTS {$table_name}" );
|
$wpdb->query( "DROP TABLE IF EXISTS {$table_name}" );
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,9 +33,9 @@ if ( is_multisite() ) {
|
|||||||
|
|
||||||
foreach ( $site_ids as $site_id ) {
|
foreach ( $site_ids as $site_id ) {
|
||||||
switch_to_blog( $site_id );
|
switch_to_blog( $site_id );
|
||||||
wpt_uninstall_site_data();
|
btranslate_uninstall_site_data();
|
||||||
restore_current_blog();
|
restore_current_blog();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
wpt_uninstall_site_data();
|
btranslate_uninstall_site_data();
|
||||||
}
|
}
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Plugin Name: Btranslate
|
|
||||||
* Description: Persistent multilingual WordPress translations powered by Baidu Translate.
|
|
||||||
* Version: 0.2.0
|
|
||||||
* Requires at least: 6.4
|
|
||||||
* Requires PHP: 8.1
|
|
||||||
* Author: hanyixuanten
|
|
||||||
* Text Domain: wp-btranslate
|
|
||||||
*/
|
|
||||||
|
|
||||||
defined( 'ABSPATH' ) || exit;
|
|
||||||
|
|
||||||
define( 'WPT_VERSION', '0.2.0' );
|
|
||||||
define( 'WPT_FILE', __FILE__ );
|
|
||||||
define( 'WPT_PATH', plugin_dir_path( __FILE__ ) );
|
|
||||||
|
|
||||||
require_once WPT_PATH . 'includes/interface-wpt-translation-provider.php';
|
|
||||||
require_once WPT_PATH . 'includes/class-wpt-translation-result.php';
|
|
||||||
require_once WPT_PATH . 'includes/class-wpt-translation-identity.php';
|
|
||||||
require_once WPT_PATH . 'includes/class-wpt-translation-store.php';
|
|
||||||
require_once WPT_PATH . 'includes/class-wpt-baidu-provider.php';
|
|
||||||
require_once WPT_PATH . 'includes/class-wpt-translation-service.php';
|
|
||||||
require_once WPT_PATH . 'includes/class-wpt-content-translator.php';
|
|
||||||
require_once WPT_PATH . 'includes/class-wpt-settings.php';
|
|
||||||
require_once WPT_PATH . 'includes/class-wpt-language-router.php';
|
|
||||||
require_once WPT_PATH . 'includes/class-wpt-sitemap-controller.php';
|
|
||||||
require_once WPT_PATH . 'includes/class-wpt-content-controller.php';
|
|
||||||
require_once WPT_PATH . 'includes/class-wpt-admin.php';
|
|
||||||
require_once WPT_PATH . 'includes/class-wpt-plugin.php';
|
|
||||||
|
|
||||||
register_activation_hook( WPT_FILE, array( 'WPT_Plugin', 'activate' ) );
|
|
||||||
register_deactivation_hook( WPT_FILE, array( 'WPT_Plugin', 'deactivate' ) );
|
|
||||||
|
|
||||||
add_action( 'plugins_loaded', array( 'WPT_Plugin', 'instance' ) );
|
|
||||||
Reference in New Issue
Block a user