rename to HTBD and slug to hyx-translator-for-baidu-translate
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
name: Bug Report and Feature Request
|
||||
description: Report a Btranslate issue or suggest a feature or documentation improvement
|
||||
description: Report a HTBD issue or suggest a feature or documentation improvement
|
||||
title: "[Issue]: "
|
||||
body:
|
||||
- type: markdown
|
||||
@@ -32,7 +32,7 @@ body:
|
||||
- type: input
|
||||
id: plugin-version
|
||||
attributes:
|
||||
label: Btranslate version
|
||||
label: HTBD version
|
||||
placeholder: "For example: 0.2.2"
|
||||
validations:
|
||||
required: true
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
name: 问题反馈与功能建议
|
||||
description: 反馈 Btranslate 的问题,或提出功能和文档改进建议
|
||||
description: 反馈 HTBD 的问题,或提出功能和文档改进建议
|
||||
title: "[Issue]: "
|
||||
body:
|
||||
- type: markdown
|
||||
@@ -32,7 +32,7 @@ body:
|
||||
- type: input
|
||||
id: plugin-version
|
||||
attributes:
|
||||
label: Btranslate 版本
|
||||
label: HTBD 版本
|
||||
placeholder: 例如:0.2.2
|
||||
validations:
|
||||
required: true
|
||||
|
||||
@@ -30,15 +30,15 @@ jobs:
|
||||
id: package
|
||||
run: |
|
||||
set -eu
|
||||
version="$(grep -E '^ \* Version:' btranslate.php | sed -E 's/^ \* Version: *//')"
|
||||
archive_name="btranslate-${version}.zip"
|
||||
version="$(grep -E '^ \* Version:' hyx-translator-for-baidu-translate.php | sed -E 's/^ \* Version: *//')"
|
||||
archive_name="hyx-translator-for-baidu-translate-${version}.zip"
|
||||
./build.sh build
|
||||
printf 'archive=%s\n' "${archive_name}" >> "$GITHUB_OUTPUT"
|
||||
|
||||
- name: Upload plugin archive
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: btranslate-plugin
|
||||
name: hyx-translator-for-baidu-translate-plugin
|
||||
path: ${{ steps.package.outputs.archive }}
|
||||
if-no-files-found: error
|
||||
retention-days: 30
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# btranslate Agent Guide
|
||||
# hyx-translator-for-baidu-translate Agent Guide
|
||||
|
||||
## Project Scope
|
||||
|
||||
@@ -45,6 +45,6 @@ Unless a user explicitly requests a refresh, each translatable value must be tra
|
||||
- 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.
|
||||
- After every file change, review `.github/workflows/package-plugin.yml` and ensure the automated packaging Action includes all runtime files required by the change. Validate the packaging commands locally when practical.
|
||||
- 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.
|
||||
- Plugin slug and PHP prefix: `hyx-translator-for-baidu-translate` and `htbd_`/`HTBD_`. The custom translation table is `{$wpdb->prefix}hyx_bd_translations`. 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.
|
||||
- Document supported languages, routing modes, data retention, translation lifecycle, and known limitations in the README whenever the implementation changes.
|
||||
|
||||
@@ -1,21 +1,21 @@
|
||||
# Btranslate: A Free and Open-Source WordPress Multilingual Plugin Powered by the Baidu Translate API
|
||||
# HTBD: A Free and Open-Source WordPress Multilingual Plugin Powered by the Baidu Translate API
|
||||
|
||||
BTranslate is a multilingual translation plugin for WordPress. It uses the Baidu Translate API to translate posts, pages, and other website content into different languages, with a dedicated URL for each translated version.
|
||||
HTBD is a multilingual translation plugin for WordPress. It uses the Baidu Translate API to translate posts, pages, and other website content into different languages, with a dedicated URL for each translated version.
|
||||
|
||||
Translations are stored on the server. Unless a user explicitly refreshes a translation, the same content is translated only once for each target language. This avoids calling the translation API on every page visit, reducing API usage and improving page load performance.
|
||||
Translations are stored on the server in `{$wpdb->prefix}hyx_bd_translations`. Unless a user explicitly refreshes a translation, the same content is translated only once for each target language. Existing `{$wpdb->prefix}btranslate_translations` data is migrated when the renamed plugin is activated. This avoids calling the translation API on every page visit, reducing API usage and improving page load performance.
|
||||
|
||||
## Project Links
|
||||
|
||||
- GitHub repository: [https://github.com/hanyixuanten/btranslate](https://github.com/hanyixuanten/btranslate)
|
||||
- WordPress plugin page: [https://wordpress.org/plugins/btranslate/](https://wordpress.org/plugins/btranslate/)
|
||||
- GitHub repository: [https://github.com/hanyixuanten/HTBD](https://github.com/hanyixuanten/HTBD)
|
||||
- WordPress plugin page: [https://wordpress.org/plugins/hyx-translator-for-baidu-translate/](https://wordpress.org/plugins/hyx-translator-for-baidu-translate/)
|
||||
|
||||
> **Review status:** BTranslate has not yet been approved for the WordPress.org Plugin Directory, so it cannot currently be installed directly from the WordPress admin plugin marketplace. For now, download it from GitHub and install it manually.
|
||||
> **Review status:** HTBD has not yet been approved for the WordPress.org Plugin Directory, so it cannot currently be installed directly from the WordPress admin plugin marketplace. For now, download it from GitHub and install it manually.
|
||||
|
||||
## Key Features
|
||||
|
||||
### Baidu Translate API Integration
|
||||
|
||||
BTranslate uses the translation API provided by the Baidu Translate Open Platform. Users can configure their own App ID and secret key in WordPress Admin, and the plugin handles request signing, API calls, and error handling.
|
||||
HTBD uses the translation API provided by the Baidu Translate Open Platform. Users can configure their own App ID and secret key in WordPress Admin, and the plugin handles request signing, API calls, and error handling.
|
||||
|
||||
### Post and Page Translation
|
||||
|
||||
@@ -32,7 +32,7 @@ The plugin supports translating the main content of WordPress posts and pages, i
|
||||
|
||||
### Multilingual URLs
|
||||
|
||||
BTranslate can generate dedicated URLs for different languages, with support for:
|
||||
HTBD can generate dedicated URLs for different languages, with support for:
|
||||
|
||||
- Language subdirectories such as `/en/` and `/ja/`
|
||||
- A separate domain for each language
|
||||
@@ -57,7 +57,7 @@ When translating content, the plugin makes every effort to preserve:
|
||||
|
||||
### Multilingual SEO
|
||||
|
||||
BTranslate provides essential SEO support for multilingual pages, including:
|
||||
HTBD provides essential SEO support for multilingual pages, including:
|
||||
|
||||
- Multilingual permalinks
|
||||
- Canonical URLs
|
||||
@@ -93,29 +93,29 @@ Because the plugin has not yet been approved for the WordPress.org Plugin Direct
|
||||
|
||||
#### Method 1: Upload the ZIP Package
|
||||
|
||||
1. Visit the [BTranslate GitHub repository](https://github.com/hanyixuanten/btranslate).
|
||||
1. Visit the [HTBD GitHub repository](https://github.com/hanyixuanten/HTBD).
|
||||
2. Select **Releases**.
|
||||
3. Download the **btranslate-*.zip** file from the latest release.
|
||||
3. Download the **hyx-translator-for-baidu-translate-*.zip** file from the latest release.
|
||||
4. Sign in to WordPress Admin.
|
||||
5. Open **Plugins > Add New Plugin**.
|
||||
6. Select **Upload Plugin**.
|
||||
7. Choose the downloaded ZIP file and start the installation.
|
||||
8. Activate BTranslate after installation is complete.
|
||||
8. Activate HTBD after installation is complete.
|
||||
|
||||
#### Method 2: Upload to the Server
|
||||
|
||||
1. Visit the [BTranslate GitHub repository](https://github.com/hanyixuanten/btranslate).
|
||||
1. Visit the [HTBD GitHub repository](https://github.com/hanyixuanten/HTBD).
|
||||
2. Select **Releases**.
|
||||
3. Download the **btranslate-*.zip** file from the latest release.
|
||||
4. Upload the `btranslate` directory to the WordPress plugin directory: `wp-content/plugins/btranslate/`.
|
||||
3. Download the **hyx-translator-for-baidu-translate-*.zip** file from the latest release.
|
||||
4. Upload the `hyx-translator-for-baidu-translate` directory to the WordPress plugin directory: `wp-content/plugins/hyx-translator-for-baidu-translate/`.
|
||||
5. Sign in to WordPress Admin.
|
||||
6. Open **Plugins > Installed Plugins**.
|
||||
7. Find BTranslate and select **Activate**.
|
||||
7. Find HTBD and select **Activate**.
|
||||
|
||||
### 3. Configure the Plugin
|
||||
|
||||
1. Sign in to WordPress Admin.
|
||||
2. Open the BTranslate settings page.
|
||||
2. Open the HTBD settings page.
|
||||
3. Enter the **APP ID** and **secret key** provided by the Baidu Translate Open Platform.
|
||||
4. Set the website's source language.
|
||||
5. Add the target languages you want to enable.
|
||||
@@ -128,7 +128,7 @@ After changing language routing settings, open **Settings > Permalinks** in Word
|
||||
|
||||
1. Open the post or page you want to translate in WordPress Admin.
|
||||
2. Save or update the content.
|
||||
3. Use the translation feature provided by BTranslate to generate translations for the target languages.
|
||||
3. Use the translation feature provided by HTBD to generate translations for the target languages.
|
||||
4. Wait for the translation tasks to finish.
|
||||
5. Check each translation at its language-specific URL.
|
||||
|
||||
@@ -173,13 +173,13 @@ The plugin prioritizes saved translations. It calls the translation API again on
|
||||
|
||||
To report a problem or suggest a feature, visit the GitHub repository:
|
||||
|
||||
[https://github.com/hanyixuanten/btranslate](https://github.com/hanyixuanten/btranslate)
|
||||
[https://github.com/hanyixuanten/HTBD](https://github.com/hanyixuanten/HTBD)
|
||||
|
||||
When opening an issue, consider including the following information:
|
||||
|
||||
- WordPress version
|
||||
- PHP version
|
||||
- BTranslate version
|
||||
- HTBD version
|
||||
- Languages and routing mode in use
|
||||
- Steps to reproduce the problem
|
||||
- Error logs with sensitive information removed
|
||||
|
||||
+21
-21
@@ -1,21 +1,21 @@
|
||||
# Btranslate:基于百度翻译 API 的免费开源 Wordpress 多语言插件
|
||||
# HTBD:基于百度翻译 API 的免费开源 Wordpress 多语言插件
|
||||
|
||||
BTranslate 是一款 WordPress 多语言翻译插件,通过百度翻译开放平台 API,将网站中的文章、页面及其他内容翻译为不同语言,并为译文生成独立的语言访问地址。
|
||||
HTBD 是一款 WordPress 多语言翻译插件,通过百度翻译开放平台 API,将网站中的文章、页面及其他内容翻译为不同语言,并为译文生成独立的语言访问地址。
|
||||
|
||||
插件会将翻译结果保存在服务器中。除非用户主动刷新翻译,否则同一内容在同一目标语言下只需翻译一次,无需在每次访问页面时重复调用翻译 API,有助于减少 API 请求量并提高页面加载速度。
|
||||
插件会将翻译结果保存在服务器的 `{$wpdb->prefix}hyx_bd_translations` 表中。除非用户主动刷新翻译,否则同一内容在同一目标语言下只需翻译一次。启用改名后的插件时,已有 `{$wpdb->prefix}btranslate_translations` 数据会自动迁移,无需在每次访问页面时重复调用翻译 API,有助于减少 API 请求量并提高页面加载速度。
|
||||
|
||||
## 项目地址
|
||||
|
||||
- GitHub 仓库:[https://github.com/hanyixuanten/btranslate](https://github.com/hanyixuanten/btranslate)
|
||||
- WordPress 插件主页:[https://wordpress.org/plugins/btranslate/](https://wordpress.org/plugins/btranslate/)
|
||||
- GitHub 仓库:[https://github.com/hanyixuanten/HTBD](https://github.com/hanyixuanten/HTBD)
|
||||
- WordPress 插件主页:[https://wordpress.org/plugins/hyx-translator-for-baidu-translate/](https://wordpress.org/plugins/hyx-translator-for-baidu-translate/)
|
||||
|
||||
> **审核状态:** BTranslate 目前尚未通过 WordPress.org 插件目录审核,因此暂时无法直接通过 WordPress 后台插件市场安装。现阶段请从 GitHub 仓库下载并手动安装。
|
||||
> **审核状态:** HTBD 目前尚未通过 WordPress.org 插件目录审核,因此暂时无法直接通过 WordPress 后台插件市场安装。现阶段请从 GitHub 仓库下载并手动安装。
|
||||
|
||||
## 主要功能
|
||||
|
||||
### 百度翻译 API 集成
|
||||
|
||||
BTranslate 使用百度翻译开放平台提供的翻译 API。用户可以在 WordPress 后台配置自己的 App ID 和密钥,由插件完成请求签名、翻译调用及错误处理。
|
||||
HTBD 使用百度翻译开放平台提供的翻译 API。用户可以在 WordPress 后台配置自己的 App ID 和密钥,由插件完成请求签名、翻译调用及错误处理。
|
||||
|
||||
### 文章和页面翻译
|
||||
|
||||
@@ -32,7 +32,7 @@ BTranslate 使用百度翻译开放平台提供的翻译 API。用户可以在 W
|
||||
|
||||
### 多语言访问地址
|
||||
|
||||
BTranslate 可以为不同语言生成独立的访问地址,支持:
|
||||
HTBD 可以为不同语言生成独立的访问地址,支持:
|
||||
|
||||
- 语言子目录,例如 `/en/`、`/ja/`
|
||||
- 不同语言绑定不同域名
|
||||
@@ -57,7 +57,7 @@ BTranslate 可以为不同语言生成独立的访问地址,支持:
|
||||
|
||||
### 多语言 SEO
|
||||
|
||||
BTranslate 为多语言页面提供必要的 SEO 支持,包括:
|
||||
HTBD 为多语言页面提供必要的 SEO 支持,包括:
|
||||
|
||||
- 多语言固定链接
|
||||
- Canonical URL
|
||||
@@ -93,29 +93,29 @@ BTranslate 为多语言页面提供必要的 SEO 支持,包括:
|
||||
|
||||
#### 方法一:上传 ZIP 压缩包
|
||||
|
||||
1. 访问 [BTranslate GitHub 仓库](https://github.com/hanyixuanten/btranslate)。
|
||||
1. 访问 [HTBD GitHub 仓库](https://github.com/hanyixuanten/HTBD)。
|
||||
2. 点击 **Releases**。
|
||||
3. 从最新版本下载 zip 文件 **btranslate-*.zip**
|
||||
3. 从最新版本下载 zip 文件 **hyx-translator-for-baidu-translate-*.zip**
|
||||
4. 登录 WordPress 管理后台。
|
||||
5. 打开“插件” → “安装插件”。
|
||||
6. 点击“上传插件”。
|
||||
7. 选择下载的 ZIP 文件并开始安装。
|
||||
8. 安装完成后启用 BTranslate。
|
||||
8. 安装完成后启用 HTBD。
|
||||
|
||||
#### 方法二:上传到服务器
|
||||
|
||||
1. 访问 [BTranslate GitHub 仓库](https://github.com/hanyixuanten/btranslate)。
|
||||
1. 访问 [HTBD GitHub 仓库](https://github.com/hanyixuanten/HTBD)。
|
||||
2. 点击 **Releases**。
|
||||
3. 从最新版本下载 zip 文件 **btranslate-*.zip**
|
||||
4. 将 `btranslate` 目录上传到 WordPress 的插件目录: `wp-content/plugins/btranslate/`
|
||||
3. 从最新版本下载 zip 文件 **hyx-translator-for-baidu-translate-*.zip**
|
||||
4. 将 `hyx-translator-for-baidu-translate` 目录上传到 WordPress 的插件目录: `wp-content/plugins/hyx-translator-for-baidu-translate/`
|
||||
5. 登录 WordPress 管理后台。
|
||||
6. 打开“插件” → “已安装插件”。
|
||||
7. 找到 BTranslate 并点击“启用”。
|
||||
7. 找到 HTBD 并点击“启用”。
|
||||
|
||||
### 三、配置插件
|
||||
|
||||
1. 登录 WordPress 管理后台。
|
||||
2. 打开 BTranslate 设置页面。
|
||||
2. 打开 HTBD 设置页面。
|
||||
3. 填写百度翻译开放平台提供的 **APP ID** 和 **密钥**。
|
||||
4. 设置网站的源语言。
|
||||
5. 添加需要启用的目标语言。
|
||||
@@ -128,7 +128,7 @@ BTranslate 为多语言页面提供必要的 SEO 支持,包括:
|
||||
|
||||
1. 在 WordPress 后台打开需要翻译的文章或页面。
|
||||
2. 保存或更新内容。
|
||||
3. 使用 BTranslate 提供的翻译功能生成目标语言译文。
|
||||
3. 使用 HTBD 提供的翻译功能生成目标语言译文。
|
||||
4. 等待翻译任务完成。
|
||||
5. 通过对应语言的访问地址检查译文。
|
||||
|
||||
@@ -173,15 +173,15 @@ https://example.com/ja/about/
|
||||
|
||||
如果在使用过程中遇到问题,或者希望提交功能建议,可以前往 GitHub 仓库反馈:
|
||||
|
||||
[https://github.com/hanyixuanten/btranslate](https://github.com/hanyixuanten/btranslate)
|
||||
[https://github.com/hanyixuanten/HTBD](https://github.com/hanyixuanten/HTBD)
|
||||
|
||||
提交问题时,建议提供以下信息:
|
||||
|
||||
- WordPress 版本
|
||||
- PHP 版本
|
||||
- BTranslate 版本
|
||||
- HTBD 版本
|
||||
- 使用的语言和路由模式
|
||||
- 问题复现步骤
|
||||
- 已隐藏敏感信息的错误日志
|
||||
|
||||
欢迎通过 Issue 或 Pull Request 参与 BTranslate 的改进。
|
||||
欢迎通过 Issue 或 Pull Request 参与 HTBD 的改进。
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
var domainMode = document.querySelector('#btranslate-routing-mode input[value="domain"]');
|
||||
var bindingsRow = document.getElementById('btranslate-domain-bindings-row');
|
||||
var domainMode = document.querySelector('#htbd-routing-mode input[value="domain"]');
|
||||
var bindingsRow = document.getElementById('htbd-domain-bindings-row');
|
||||
|
||||
if (domainMode && bindingsRow) {
|
||||
domainMode.addEventListener('change', function () {
|
||||
@@ -10,16 +10,16 @@
|
||||
});
|
||||
}
|
||||
|
||||
document.querySelectorAll('form[data-btranslate-confirm]').forEach(function (form) {
|
||||
document.querySelectorAll('form[data-htbd-confirm]').forEach(function (form) {
|
||||
form.addEventListener('submit', function (event) {
|
||||
if (!window.confirm(form.getAttribute('data-btranslate-confirm'))) {
|
||||
if (!window.confirm(form.getAttribute('data-htbd-confirm'))) {
|
||||
event.preventDefault();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
var container = document.getElementById('btranslate-translation-progress');
|
||||
if (!container || typeof btranslateAdmin === 'undefined') {
|
||||
var container = document.getElementById('htbd-translation-progress');
|
||||
if (!container || typeof htbdAdmin === 'undefined') {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@
|
||||
var percent = document.createElement('strong');
|
||||
percent.textContent = progress.percent + '%';
|
||||
summary.appendChild(percent);
|
||||
appendText(summary, ' ' + format(btranslateAdmin.i18n.contentItems, [progress.completed, progress.total]));
|
||||
appendText(summary, ' ' + format(htbdAdmin.i18n.contentItems, [progress.completed, progress.total]));
|
||||
|
||||
var progressBar = document.createElement('div');
|
||||
progressBar.style.cssText = 'max-width:480px;height:12px;background:#dcdcde';
|
||||
@@ -49,12 +49,12 @@
|
||||
|
||||
var description = document.createElement('p');
|
||||
description.className = 'description';
|
||||
description.textContent = format(btranslateAdmin.i18n.latestTask, [progress.task_label, progress.posts_completed, progress.posts_total, progress.terms_completed, progress.terms_total]);
|
||||
description.textContent = format(htbdAdmin.i18n.latestTask, [progress.task_label, progress.posts_completed, progress.posts_total, progress.terms_completed, progress.terms_total]);
|
||||
|
||||
var children = [summary, progressBar, description];
|
||||
if (progress.failed_items.length) {
|
||||
var failedHeading = document.createElement('h3');
|
||||
failedHeading.textContent = btranslateAdmin.i18n.failedItems;
|
||||
failedHeading.textContent = htbdAdmin.i18n.failedItems;
|
||||
children.push(failedHeading);
|
||||
|
||||
var failedList = document.createElement('ul');
|
||||
@@ -65,7 +65,7 @@
|
||||
var retryLink = document.createElement('a');
|
||||
retryLink.className = 'button button-small';
|
||||
retryLink.href = item.retry_url;
|
||||
retryLink.textContent = btranslateAdmin.i18n.retranslate;
|
||||
retryLink.textContent = htbdAdmin.i18n.retranslate;
|
||||
failedItem.appendChild(retryLink);
|
||||
failedList.appendChild(failedItem);
|
||||
});
|
||||
@@ -77,12 +77,12 @@
|
||||
|
||||
function renderProgressError() {
|
||||
var message = document.createElement('p');
|
||||
message.textContent = btranslateAdmin.i18n.progressError;
|
||||
message.textContent = htbdAdmin.i18n.progressError;
|
||||
container.replaceChildren(message);
|
||||
}
|
||||
|
||||
function updateProgress() {
|
||||
var url = btranslateAdmin.progressUrl + '?action=btranslate_translation_progress&_ajax_nonce=' + encodeURIComponent(btranslateAdmin.progressNonce);
|
||||
var url = htbdAdmin.progressUrl + '?action=htbd_translation_progress&_ajax_nonce=' + encodeURIComponent(htbdAdmin.progressNonce);
|
||||
|
||||
window.fetch(url, { credentials: 'same-origin' })
|
||||
.then(function (response) {
|
||||
@@ -1,45 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Plugin Name: Btranslate
|
||||
* Plugin URI: https://github.com/hanyixuanten/btranslate
|
||||
* Description: Persistent multilingual WordPress translations powered by Baidu Translate.
|
||||
* Version: 0.2.2
|
||||
* Requires at least: 6.4
|
||||
* Requires PHP: 8.1
|
||||
* Author: hanyixuanten
|
||||
* License: GPL-3.0-only
|
||||
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
|
||||
* Text Domain: btranslate
|
||||
* Domain Path: /languages
|
||||
*/
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
define( 'BTRANSLATE_VERSION', '0.2.2' );
|
||||
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';
|
||||
require_once BTRANSLATE_PATH . 'includes/class-btranslate-uninstaller.php';
|
||||
|
||||
register_activation_hook( BTRANSLATE_FILE, array( 'BTRANSLATE_Plugin', 'activate' ) );
|
||||
register_deactivation_hook( BTRANSLATE_FILE, array( 'BTRANSLATE_Plugin', 'deactivate' ) );
|
||||
|
||||
function btranslate_load_textdomain() {
|
||||
load_plugin_textdomain( 'btranslate', false, dirname( plugin_basename( BTRANSLATE_FILE ) ) . '/languages' );
|
||||
}
|
||||
|
||||
add_action( 'plugins_loaded', 'btranslate_load_textdomain', 5 );
|
||||
add_action( 'plugins_loaded', array( 'BTRANSLATE_Plugin', 'instance' ) );
|
||||
@@ -2,7 +2,8 @@
|
||||
|
||||
set -eu
|
||||
|
||||
plugin_slug="btranslate"
|
||||
plugin_slug="hyx-translator-for-baidu-translate"
|
||||
plugin_file="${plugin_slug}.php"
|
||||
script_dir="$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd)"
|
||||
build_dir="${script_dir}/build"
|
||||
plugin_dir="${build_dir}/${plugin_slug}"
|
||||
@@ -20,13 +21,13 @@ build() {
|
||||
fi
|
||||
done
|
||||
|
||||
version="$(grep -E '^ \* Version:' "${script_dir}/btranslate.php" | sed -E 's/^ \* Version: *//')"
|
||||
version="$(grep -E '^ \* Version:' "${script_dir}/${plugin_file}" | sed -E 's/^ \* Version: *//')"
|
||||
archive_name="${plugin_slug}-${version}.zip"
|
||||
archive_path="${script_dir}/${archive_name}"
|
||||
|
||||
clean
|
||||
mkdir -p "${plugin_dir}"
|
||||
cp "${script_dir}/btranslate.php" \
|
||||
cp "${script_dir}/${plugin_file}" \
|
||||
"${script_dir}/uninstall.php" \
|
||||
"${script_dir}/readme.txt" \
|
||||
"${script_dir}/readme-zh_CN.txt" \
|
||||
@@ -36,17 +37,31 @@ build() {
|
||||
cp -R "${script_dir}/assets" "${plugin_dir}/"
|
||||
cp -R "${script_dir}/languages" "${plugin_dir}/"
|
||||
msgfmt --check --check-format \
|
||||
--output-file="${plugin_dir}/languages/btranslate-zh_CN.mo" \
|
||||
"${script_dir}/languages/btranslate-zh_CN.po"
|
||||
--output-file="${plugin_dir}/languages/${plugin_slug}-zh_CN.mo" \
|
||||
"${script_dir}/languages/${plugin_slug}-zh_CN.po"
|
||||
(
|
||||
cd "${build_dir}"
|
||||
zip -qr "${archive_path}" "${plugin_slug}"
|
||||
)
|
||||
|
||||
unzip -p "${archive_path}" "${plugin_slug}/btranslate.php" | grep -q '^ \* Plugin Name: Btranslate$'
|
||||
unzip -p "${archive_path}" "${plugin_slug}/${plugin_file}" | grep -q '^ \* Plugin Name: HTBD$'
|
||||
unzip -p "${archive_path}" "${plugin_slug}/readme.txt" | grep -q "^Stable tag: ${version}$"
|
||||
unzip -p "${archive_path}" "${plugin_slug}/readme-zh_CN.txt" | grep -q "^Stable tag: ${version}$"
|
||||
unzip -Z1 "${archive_path}" | grep -q "^${plugin_slug}/languages/btranslate-zh_CN.mo$"
|
||||
unzip -Z1 "${archive_path}" | grep -q "^${plugin_slug}/languages/${plugin_slug}-zh_CN.mo$"
|
||||
unzip -Z1 "${archive_path}" | grep -q "^${plugin_slug}/includes/interface-translation-provider.php$"
|
||||
unzip -Z1 "${archive_path}" | grep -q "^${plugin_slug}/assets/js/admin.js$"
|
||||
if unzip -Z1 "${archive_path}" | grep -Eq '/class-btranslate-[^/]+\.php$'; then
|
||||
printf 'Unexpected legacy class filename found in archive.\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
if unzip -Z1 "${archive_path}" | grep -Eq '/(interface-btranslate-translation-provider\.php|btranslate-admin\.js)$'; then
|
||||
printf 'Unexpected legacy runtime filename found in archive.\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
if grep -R -q 'BTRANSLATE' "${plugin_dir}" --include='*.php'; then
|
||||
printf 'Unexpected legacy PHP prefix found in archive.\n' >&2
|
||||
exit 1
|
||||
fi
|
||||
if unzip -Z1 "${archive_path}" | grep -Eq '/README(\.zh-CN)?\.md$'; then
|
||||
printf 'Unexpected development README found in archive.\n' >&2
|
||||
exit 1
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
/**
|
||||
* Plugin Name: HTBD
|
||||
* Plugin URI: https://github.com/hanyixuanten/HTBD
|
||||
* Description: Persistent multilingual WordPress translations powered by Baidu Translate.
|
||||
* Version: 0.2.2
|
||||
* Requires at least: 6.4
|
||||
* Requires PHP: 8.1
|
||||
* Author: hanyixuanten
|
||||
* License: GPL-3.0-only
|
||||
* License URI: https://www.gnu.org/licenses/gpl-3.0.html
|
||||
* Text Domain: hyx-translator-for-baidu-translate
|
||||
* Domain Path: /languages
|
||||
*/
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
define( 'HTBD_VERSION', '0.2.2' );
|
||||
define( 'HTBD_FILE', __FILE__ );
|
||||
define( 'HTBD_PATH', plugin_dir_path( __FILE__ ) );
|
||||
|
||||
require_once HTBD_PATH . 'includes/interface-translation-provider.php';
|
||||
require_once HTBD_PATH . 'includes/class-translation-result.php';
|
||||
require_once HTBD_PATH . 'includes/class-translation-identity.php';
|
||||
require_once HTBD_PATH . 'includes/class-translation-store.php';
|
||||
require_once HTBD_PATH . 'includes/class-baidu-provider.php';
|
||||
require_once HTBD_PATH . 'includes/class-translation-service.php';
|
||||
require_once HTBD_PATH . 'includes/class-content-translator.php';
|
||||
require_once HTBD_PATH . 'includes/class-settings.php';
|
||||
require_once HTBD_PATH . 'includes/class-language-router.php';
|
||||
require_once HTBD_PATH . 'includes/class-sitemap-controller.php';
|
||||
require_once HTBD_PATH . 'includes/class-content-controller.php';
|
||||
require_once HTBD_PATH . 'includes/class-admin.php';
|
||||
require_once HTBD_PATH . 'includes/class-plugin.php';
|
||||
require_once HTBD_PATH . 'includes/class-uninstaller.php';
|
||||
|
||||
register_activation_hook( HTBD_FILE, array( 'HTBD_Plugin', 'activate' ) );
|
||||
register_deactivation_hook( HTBD_FILE, array( 'HTBD_Plugin', 'deactivate' ) );
|
||||
|
||||
function htbd_load_textdomain() {
|
||||
load_plugin_textdomain( 'hyx-translator-for-baidu-translate', false, dirname( plugin_basename( HTBD_FILE ) ) . '/languages' );
|
||||
}
|
||||
|
||||
add_action( 'plugins_loaded', 'htbd_load_textdomain', 5 );
|
||||
add_action( 'plugins_loaded', array( 'HTBD_Plugin', 'instance' ) );
|
||||
@@ -0,0 +1,445 @@
|
||||
<?php
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
class HTBD_Admin {
|
||||
public function register() {
|
||||
add_action( 'admin_menu', array( $this, 'add_settings_page' ) );
|
||||
add_action( 'admin_init', array( $this, 'register_settings' ) );
|
||||
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_settings_assets' ) );
|
||||
add_action( 'admin_post_htbd_queue_existing_translations', array( $this, 'queue_existing_translations' ) );
|
||||
add_action( 'admin_post_htbd_clear_translation_cache', array( $this, 'clear_translation_cache' ) );
|
||||
add_action( 'admin_post_htbd_translate_post', array( $this, 'queue_post_translation' ) );
|
||||
add_action( 'admin_post_htbd_retry_failed_translation', array( $this, 'retry_failed_translation' ) );
|
||||
add_action( 'wp_ajax_htbd_translation_progress', array( $this, 'translation_progress' ) );
|
||||
add_filter( 'manage_post_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_page_posts_custom_column', array( $this, 'render_translation_column' ), 10, 2 );
|
||||
add_filter( 'plugin_action_links_' . plugin_basename( HTBD_FILE ), array( $this, 'add_plugin_settings_link' ) );
|
||||
}
|
||||
|
||||
public function add_settings_page() {
|
||||
add_options_page( __( 'HTBD', 'hyx-translator-for-baidu-translate' ), __( 'HTBD', 'hyx-translator-for-baidu-translate' ), 'manage_options', 'htbd', array( $this, 'render_settings_page' ) );
|
||||
}
|
||||
|
||||
public function register_settings() {
|
||||
register_setting( 'htbd_settings', 'htbd_settings', array( $this, 'sanitize_settings' ) );
|
||||
}
|
||||
|
||||
public function enqueue_settings_assets( $hook_suffix ) {
|
||||
if ( 'settings_page_htbd' !== $hook_suffix ) {
|
||||
return;
|
||||
}
|
||||
|
||||
wp_enqueue_script(
|
||||
'htbd-admin',
|
||||
plugins_url( 'assets/js/admin.js', HTBD_FILE ),
|
||||
array(),
|
||||
HTBD_VERSION,
|
||||
true
|
||||
);
|
||||
wp_localize_script(
|
||||
'htbd-admin',
|
||||
'htbdAdmin',
|
||||
array(
|
||||
'progressUrl' => admin_url( 'admin-ajax.php' ),
|
||||
'progressNonce' => wp_create_nonce( 'htbd_translation_progress' ),
|
||||
'i18n' => array(
|
||||
'contentItems' => __( '(%1$s / %2$s content items)', 'hyx-translator-for-baidu-translate' ),
|
||||
'latestTask' => __( 'Latest task: %1$s. Posts and pages: %2$s / %3$s; categories and tags: %4$s / %5$s. Updates automatically every 5 seconds.', 'hyx-translator-for-baidu-translate' ),
|
||||
'failedItems' => __( 'Failed translations', 'hyx-translator-for-baidu-translate' ),
|
||||
'retranslate' => __( 'Retranslate', 'hyx-translator-for-baidu-translate' ),
|
||||
'progressError' => __( 'Unable to load translation progress at this time.', 'hyx-translator-for-baidu-translate' ),
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function sanitize_settings( $settings ) {
|
||||
$settings = (array) $settings;
|
||||
$bindings = array();
|
||||
$routing_modes = HTBD_Settings::routing_modes( $settings['routing_mode'] ?? array() );
|
||||
$target_languages = array_values( array_filter( array_map( 'sanitize_key', explode( ',', (string) ( $settings['target_languages'] ?? 'en' ) ) ) ) );
|
||||
|
||||
foreach ( preg_split( '/\r\n|\r|\n/', (string) ( $settings['domain_bindings'] ?? '' ) ) as $line ) {
|
||||
$parts = array_map( 'trim', explode( '=', $line, 2 ) );
|
||||
$domain = 2 === count( $parts ) ? HTBD_Settings::normalize_domain( $parts[0] ) : '';
|
||||
$language = 2 === count( $parts ) ? sanitize_key( $parts[1] ) : '';
|
||||
if ( '' !== $domain && in_array( $language, $target_languages, true ) && ! in_array( $language, $bindings, true ) ) {
|
||||
$bindings[ $domain ] = $language;
|
||||
}
|
||||
}
|
||||
|
||||
return array(
|
||||
'source_language' => sanitize_key( $settings['source_language'] ?? 'zh' ),
|
||||
'target_languages' => $target_languages,
|
||||
'routing_mode' => $routing_modes,
|
||||
'domain_bindings' => $bindings,
|
||||
'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'] ),
|
||||
);
|
||||
}
|
||||
|
||||
public function render_settings_page() {
|
||||
$settings = HTBD_Settings::get();
|
||||
$bindings = array();
|
||||
foreach ( (array) $settings['domain_bindings'] as $domain => $language ) {
|
||||
$bindings[] = $domain . '=' . $language;
|
||||
}
|
||||
?>
|
||||
<div class="wrap">
|
||||
<h1>HTBD</h1>
|
||||
<form action="options.php" method="post">
|
||||
<?php settings_fields( 'htbd_settings' ); ?>
|
||||
<table class="form-table" role="presentation">
|
||||
<tr><th scope="row"><label for="htbd-source-language"><?php esc_html_e( 'Source language', 'hyx-translator-for-baidu-translate' ); ?></label></th><td><input id="htbd-source-language" name="htbd_settings[source_language]" type="text" value="<?php echo esc_attr( $settings['source_language'] ); ?>" /><p class="description"><?php esc_html_e( 'Use a Baidu Translate language code, for example zh.', 'hyx-translator-for-baidu-translate' ); ?></p></td></tr>
|
||||
<tr><th scope="row"><label for="htbd-target-languages"><?php esc_html_e( 'Target languages', 'hyx-translator-for-baidu-translate' ); ?></label></th><td><input id="htbd-target-languages" name="htbd_settings[target_languages]" type="text" value="<?php echo esc_attr( implode( ',', $settings['target_languages'] ) ); ?>" class="regular-text" /><p class="description"><?php esc_html_e( 'Separate multiple language codes with commas, for example en,ja.', 'hyx-translator-for-baidu-translate' ); ?></p></td></tr>
|
||||
<tr><th scope="row"><?php esc_html_e( 'URL mode', 'hyx-translator-for-baidu-translate' ); ?></th><td><fieldset id="htbd-routing-mode"><label><input type="checkbox" name="htbd_settings[routing_mode][]" value="subdirectory" <?php checked( in_array( 'subdirectory', $settings['routing_mode'], true ) ); ?> /> <?php esc_html_e( 'Subdirectory', 'hyx-translator-for-baidu-translate' ); ?></label><br /><label><input type="checkbox" name="htbd_settings[routing_mode][]" value="domain" <?php checked( in_array( 'domain', $settings['routing_mode'], true ) ); ?> /> <?php esc_html_e( 'Domain', 'hyx-translator-for-baidu-translate' ); ?></label></fieldset></td></tr>
|
||||
<tr id="htbd-domain-bindings-row"<?php echo in_array( 'domain', $settings['routing_mode'], true ) ? '' : ' style="display:none"'; ?>><th scope="row"><label for="htbd-domain-bindings"><?php esc_html_e( 'Domain bindings', 'hyx-translator-for-baidu-translate' ); ?></label></th><td><textarea id="htbd-domain-bindings" name="htbd_settings[domain_bindings]" rows="4" class="large-text" placeholder="en.example.com=en"><?php echo esc_textarea( implode( "\n", $bindings ) ); ?></textarea><p class="description"><?php esc_html_e( 'Enter one domain=language code pair per line, for example en.example.com=en. Do not include https://, a path, or a port.', 'hyx-translator-for-baidu-translate' ); ?></p></td></tr>
|
||||
<tr><th scope="row"><label for="htbd-baidu-app-id"><?php esc_html_e( 'Baidu application ID', 'hyx-translator-for-baidu-translate' ); ?></label></th><td><input id="htbd-baidu-app-id" name="htbd_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="htbd-baidu-secret-key"><?php esc_html_e( 'Baidu secret key', 'hyx-translator-for-baidu-translate' ); ?></label></th><td><input id="htbd-baidu-secret-key" name="htbd_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"><?php esc_html_e( 'Request logging', 'hyx-translator-for-baidu-translate' ); ?></th><td><label for="htbd-log-requests"><input id="htbd-log-requests" name="htbd_settings[log_requests]" type="checkbox" value="1" <?php checked( $settings['log_requests'] ); ?> /> <?php esc_html_e( 'Log each Baidu Translate request', 'hyx-translator-for-baidu-translate' ); ?></label><p class="description"><?php esc_html_e( 'When enabled, fires the htbd_translation_request_logged action with the language, field, text fingerprint, length, and result status. Credentials, source text, translated text, and full API responses are never included.', 'hyx-translator-for-baidu-translate' ); ?></p></td></tr>
|
||||
</table>
|
||||
<?php submit_button(); ?>
|
||||
<div class="notice notice-warning inline">
|
||||
<p><strong><?php esc_html_e( 'After saving settings for the first time, manually run "Retranslate all content" below.', 'hyx-translator-for-baidu-translate' ); ?></strong></p>
|
||||
</div>
|
||||
</form>
|
||||
<hr />
|
||||
<h2><?php esc_html_e( 'Retranslate content', 'hyx-translator-for-baidu-translate' ); ?></h2>
|
||||
<p><?php esc_html_e( 'Queue all published posts, pages, categories, and tags for translation again.', 'hyx-translator-for-baidu-translate' ); ?></p>
|
||||
<form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post" data-htbd-confirm="<?php echo esc_attr__( 'This will retranslate all content and use your API quota. Continue?', 'hyx-translator-for-baidu-translate' ); ?>">
|
||||
<input type="hidden" name="action" value="htbd_queue_existing_translations" />
|
||||
<?php wp_nonce_field( 'htbd_queue_existing_translations' ); ?>
|
||||
<?php submit_button( __( 'Retranslate all content', 'hyx-translator-for-baidu-translate' ), 'secondary', 'submit', false ); ?>
|
||||
</form>
|
||||
<p><?php esc_html_e( 'You can also retranslate a single content type.', 'hyx-translator-for-baidu-translate' ); ?></p>
|
||||
<form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post" style="display:inline-block;margin-right:8px" data-htbd-confirm="<?php echo esc_attr__( 'This will retranslate all posts and pages and use your API quota. Continue?', 'hyx-translator-for-baidu-translate' ); ?>">
|
||||
<input type="hidden" name="action" value="htbd_queue_existing_translations" />
|
||||
<input type="hidden" name="scope" value="posts" />
|
||||
<?php wp_nonce_field( 'htbd_queue_existing_translations' ); ?>
|
||||
<?php submit_button( __( 'Translate all posts', 'hyx-translator-for-baidu-translate' ), 'secondary', 'submit', false ); ?>
|
||||
</form>
|
||||
<form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post" style="display:inline-block" data-htbd-confirm="<?php echo esc_attr__( 'This will retranslate all categories and tags and use your API quota. Continue?', 'hyx-translator-for-baidu-translate' ); ?>">
|
||||
<input type="hidden" name="action" value="htbd_queue_existing_translations" />
|
||||
<input type="hidden" name="scope" value="terms" />
|
||||
<?php wp_nonce_field( 'htbd_queue_existing_translations' ); ?>
|
||||
<?php submit_button( __( 'Translate all categories and tags', 'hyx-translator-for-baidu-translate' ), 'secondary', 'submit', false ); ?>
|
||||
</form>
|
||||
<hr />
|
||||
<h2><?php esc_html_e( 'Translation cache', 'hyx-translator-for-baidu-translate' ); ?></h2>
|
||||
<p><?php esc_html_e( 'After clearing the cache, the front end temporarily displays source content. Baidu Translate API calls resume when content is translated again.', 'hyx-translator-for-baidu-translate' ); ?></p>
|
||||
<form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post" data-htbd-confirm="<?php echo esc_attr__( 'This will delete all saved translations, cancel pending translation tasks, and reset progress. Continue?', 'hyx-translator-for-baidu-translate' ); ?>">
|
||||
<input type="hidden" name="action" value="htbd_clear_translation_cache" />
|
||||
<?php wp_nonce_field( 'htbd_clear_translation_cache' ); ?>
|
||||
<?php submit_button( __( 'Clear translation cache', 'hyx-translator-for-baidu-translate' ), 'delete', 'submit', false ); ?>
|
||||
</form>
|
||||
<hr />
|
||||
<h2><?php esc_html_e( 'Translation progress', 'hyx-translator-for-baidu-translate' ); ?></h2>
|
||||
<div id="htbd-translation-progress" aria-live="polite">
|
||||
<p><?php esc_html_e( 'Loading translation progress...', 'hyx-translator-for-baidu-translate' ); ?></p>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function queue_existing_translations() {
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
wp_die( esc_html__( 'You do not have permission to perform this action.', 'hyx-translator-for-baidu-translate' ) );
|
||||
}
|
||||
|
||||
check_admin_referer( 'htbd_queue_existing_translations' );
|
||||
$scope = isset( $_POST['scope'] ) ? sanitize_key( wp_unslash( $_POST['scope'] ) ) : 'all';
|
||||
if ( ! in_array( $scope, array( 'all', 'posts', 'terms' ), true ) ) {
|
||||
wp_die( esc_html__( 'Invalid translation scope.', 'hyx-translator-for-baidu-translate' ) );
|
||||
}
|
||||
|
||||
$scheduled = 0;
|
||||
$next_run = time();
|
||||
$post_ids = array();
|
||||
$term_ids = array();
|
||||
$languages = HTBD_Settings::target_languages();
|
||||
|
||||
if ( 'all' === $scope || 'posts' === $scope ) {
|
||||
$post_ids = get_posts(
|
||||
array(
|
||||
'post_type' => array( 'post', 'page' ),
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => -1,
|
||||
'fields' => 'ids',
|
||||
)
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
if ( 'all' === $scope || 'terms' === $scope ) {
|
||||
$term_ids = get_terms(
|
||||
array(
|
||||
'taxonomy' => array( 'category', 'post_tag' ),
|
||||
'hide_empty' => false,
|
||||
'fields' => 'ids',
|
||||
)
|
||||
);
|
||||
if ( is_wp_error( $term_ids ) ) {
|
||||
$term_ids = array();
|
||||
}
|
||||
}
|
||||
|
||||
$this->update_translation_task( $post_ids, $term_ids, $languages );
|
||||
|
||||
foreach ( $post_ids as $post_id ) {
|
||||
foreach ( $languages as $target_language ) {
|
||||
$this->clear_scheduled_event( 'htbd_process_translation', array( $post_id, $target_language, true ) );
|
||||
wp_schedule_single_event( $next_run, 'htbd_process_translation', array( $post_id, $target_language, true ) );
|
||||
$next_run += 2;
|
||||
++$scheduled;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ( $term_ids as $term_id ) {
|
||||
foreach ( $languages as $target_language ) {
|
||||
$this->clear_scheduled_event( 'htbd_process_term_translation', array( $term_id, $target_language, true ) );
|
||||
wp_schedule_single_event( $next_run, 'htbd_process_term_translation', array( $term_id, $target_language, true ) );
|
||||
$next_run += 2;
|
||||
++$scheduled;
|
||||
}
|
||||
}
|
||||
|
||||
wp_safe_redirect( add_query_arg( 'htbd_queued', $scheduled, admin_url( 'options-general.php?page=htbd' ) ) );
|
||||
exit;
|
||||
}
|
||||
|
||||
public function clear_translation_cache() {
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
wp_die( esc_html__( 'You do not have permission to perform this action.', 'hyx-translator-for-baidu-translate' ) );
|
||||
}
|
||||
|
||||
check_admin_referer( 'htbd_clear_translation_cache' );
|
||||
$deleted = ( new HTBD_Translation_Store() )->clear();
|
||||
|
||||
if ( false === $deleted ) {
|
||||
wp_die( esc_html__( 'Failed to clear the translation cache.', 'hyx-translator-for-baidu-translate' ) );
|
||||
}
|
||||
|
||||
wp_clear_scheduled_hook( 'htbd_process_translation' );
|
||||
wp_clear_scheduled_hook( 'htbd_process_term_translation' );
|
||||
wp_clear_scheduled_hook( 'htbd_process_seo_output_translation' );
|
||||
delete_option( 'htbd_translation_task' );
|
||||
delete_option( 'htbd_retranslation_batch' );
|
||||
|
||||
wp_safe_redirect( add_query_arg( 'htbd_cache_cleared', 1, admin_url( 'options-general.php?page=htbd' ) ) );
|
||||
exit;
|
||||
}
|
||||
|
||||
private function clear_scheduled_event( $hook, $args ) {
|
||||
while ( false !== ( $timestamp = wp_next_scheduled( $hook, $args ) ) ) {
|
||||
wp_unschedule_event( $timestamp, $hook, $args );
|
||||
}
|
||||
}
|
||||
|
||||
public function add_plugin_settings_link( $links ) {
|
||||
array_unshift( $links, '<a href="' . esc_url( admin_url( 'options-general.php?page=htbd' ) ) . '">' . esc_html__( 'Settings', 'hyx-translator-for-baidu-translate' ) . '</a>' );
|
||||
|
||||
return $links;
|
||||
}
|
||||
|
||||
public function translation_progress() {
|
||||
check_ajax_referer( 'htbd_translation_progress' );
|
||||
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
wp_send_json_error( array( 'message' => 'forbidden' ), 403 );
|
||||
}
|
||||
|
||||
$task = (array) get_option( 'htbd_translation_task', 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() ) ) ) );
|
||||
$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'] ) : '';
|
||||
$store = new HTBD_Translation_Store();
|
||||
$counts = $store->get_completed_item_counts( $languages, $post_ids, $term_ids, $started_at );
|
||||
$failures = $this->prepare_failed_items( $store->get_failed_items( $languages, $post_ids, $term_ids, $started_at ) );
|
||||
|
||||
$posts_total = count( $post_ids ) * count( $languages );
|
||||
$terms_total = count( $term_ids ) * count( $languages );
|
||||
$total = $posts_total + $terms_total;
|
||||
$posts_completed = min( $counts['posts'], $posts_total );
|
||||
$terms_completed = min( $counts['terms'], $terms_total );
|
||||
$completed = $posts_completed + $terms_completed;
|
||||
$percent = $total ? (int) floor( ( $completed / $total ) * 100 ) : 100;
|
||||
|
||||
wp_send_json_success(
|
||||
array(
|
||||
'posts_completed' => $posts_completed,
|
||||
'posts_total' => $posts_total,
|
||||
'terms_completed' => $terms_completed,
|
||||
'terms_total' => $terms_total,
|
||||
'completed' => $completed,
|
||||
'total' => $total,
|
||||
'percent' => $percent,
|
||||
'batch_started_at' => $started_at,
|
||||
'task_label' => $this->translation_task_label( $post_ids, $term_ids ),
|
||||
'failed_items' => $failures,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function add_translation_column( $columns ) {
|
||||
$columns['htbd_translation'] = __( 'Translation status', 'hyx-translator-for-baidu-translate' );
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
public function render_translation_column( $column, $post_id ) {
|
||||
if ( 'htbd_translation' !== $column ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$store = new HTBD_Translation_Store();
|
||||
|
||||
foreach ( HTBD_Settings::target_languages() as $target_language ) {
|
||||
$status = $store->get_post_language_status( $post_id, $target_language );
|
||||
$translated = ! empty( $status['translated_fields'] );
|
||||
$button_text = $translated ? __( 'Retranslate', 'hyx-translator-for-baidu-translate' ) : __( 'Translate', 'hyx-translator-for-baidu-translate' );
|
||||
$action_url = wp_nonce_url(
|
||||
add_query_arg(
|
||||
array(
|
||||
'action' => 'htbd_translate_post',
|
||||
'post_id' => $post_id,
|
||||
'language' => $target_language,
|
||||
'force' => $translated ? '1' : '0',
|
||||
),
|
||||
admin_url( 'admin-post.php' )
|
||||
),
|
||||
'htbd_translate_post_' . $post_id . '_' . $target_language
|
||||
);
|
||||
|
||||
echo '<p><strong>' . esc_html( strtoupper( $target_language ) ) . '</strong>: ';
|
||||
echo $translated ? '<span style="color:#008a20">' . esc_html__( 'Translated', 'hyx-translator-for-baidu-translate' ) . '</span>' : '<span>' . esc_html__( 'Not translated', 'hyx-translator-for-baidu-translate' ) . '</span>';
|
||||
if ( $translated && ! empty( $status['last_translated_at'] ) ) {
|
||||
echo '<br><small>' . esc_html__( 'Last translated:', 'hyx-translator-for-baidu-translate' ) . ' ' . esc_html( get_date_from_gmt( $status['last_translated_at'], 'Y-m-d H:i' ) ) . '</small>';
|
||||
}
|
||||
echo '<br><a class="button button-small" href="' . esc_url( $action_url ) . '">' . esc_html( $button_text ) . '</a></p>';
|
||||
}
|
||||
}
|
||||
|
||||
public function queue_post_translation() {
|
||||
$post_id = isset( $_GET['post_id'] ) ? absint( $_GET['post_id'] ) : 0;
|
||||
$target_language = isset( $_GET['language'] ) ? sanitize_key( wp_unslash( $_GET['language'] ) ) : '';
|
||||
$force_refresh = ! empty( $_GET['force'] );
|
||||
|
||||
if ( ! $post_id || ! current_user_can( 'edit_post', $post_id ) || ! HTBD_Settings::is_supported_language( $target_language ) || $target_language === HTBD_Settings::get()['source_language'] ) {
|
||||
wp_die( esc_html__( 'Invalid translation request.', 'hyx-translator-for-baidu-translate' ) );
|
||||
}
|
||||
|
||||
check_admin_referer( 'htbd_translate_post_' . $post_id . '_' . $target_language );
|
||||
$this->update_translation_task( array( $post_id ), array(), array( $target_language ) );
|
||||
wp_schedule_single_event( time() + 5, 'htbd_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 ) ) );
|
||||
exit;
|
||||
}
|
||||
|
||||
public function retry_failed_translation() {
|
||||
$item_type = isset( $_GET['item_type'] ) ? sanitize_key( wp_unslash( $_GET['item_type'] ) ) : '';
|
||||
$item_id = isset( $_GET['item_id'] ) ? absint( $_GET['item_id'] ) : 0;
|
||||
$target_language = isset( $_GET['language'] ) ? sanitize_key( wp_unslash( $_GET['language'] ) ) : '';
|
||||
|
||||
if ( ! current_user_can( 'manage_options' ) || ! $item_id || ! in_array( $item_type, array( 'post', 'term' ), true ) || ! in_array( $target_language, HTBD_Settings::target_languages(), true ) ) {
|
||||
wp_die( esc_html__( 'Invalid translation request.', 'hyx-translator-for-baidu-translate' ) );
|
||||
}
|
||||
|
||||
check_admin_referer( 'htbd_retry_failed_translation_' . $item_type . '_' . $item_id . '_' . $target_language );
|
||||
|
||||
if ( 'post' === $item_type ) {
|
||||
wp_schedule_single_event( time() + 5, 'htbd_process_translation', array( $item_id, $target_language, true ) );
|
||||
} else {
|
||||
wp_schedule_single_event( time() + 5, 'htbd_process_term_translation', array( $item_id, $target_language, true ) );
|
||||
}
|
||||
|
||||
wp_safe_redirect( admin_url( 'options-general.php?page=htbd' ) );
|
||||
exit;
|
||||
}
|
||||
|
||||
private function prepare_failed_items( $failures ) {
|
||||
$items = array();
|
||||
|
||||
foreach ( (array) $failures as $failure ) {
|
||||
$context_parts = explode( ':', $failure['item_context'], 2 );
|
||||
$item_type = $context_parts[0] ?? '';
|
||||
$item_id = isset( $context_parts[1] ) ? absint( $context_parts[1] ) : 0;
|
||||
$target_language = sanitize_key( $failure['target_language'] ?? '' );
|
||||
|
||||
if ( ! $item_id || ! in_array( $item_type, array( 'post', 'term' ), true ) || '' === $target_language ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( 'post' === $item_type ) {
|
||||
$name = get_the_title( $item_id );
|
||||
} else {
|
||||
$term = get_term( $item_id );
|
||||
$name = $term && ! is_wp_error( $term ) ? $term->name : '';
|
||||
}
|
||||
|
||||
if ( '' === $name ) {
|
||||
$name = sprintf( __( 'Content #%d', 'hyx-translator-for-baidu-translate' ), $item_id );
|
||||
}
|
||||
|
||||
$retry_url = wp_nonce_url(
|
||||
add_query_arg(
|
||||
array(
|
||||
'action' => 'htbd_retry_failed_translation',
|
||||
'item_type' => $item_type,
|
||||
'item_id' => $item_id,
|
||||
'language' => $target_language,
|
||||
),
|
||||
admin_url( 'admin-post.php' )
|
||||
),
|
||||
'htbd_retry_failed_translation_' . $item_type . '_' . $item_id . '_' . $target_language
|
||||
);
|
||||
|
||||
$items[] = array(
|
||||
'name' => $name,
|
||||
'language' => strtoupper( $target_language ),
|
||||
'retry_url' => $retry_url,
|
||||
);
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
private function update_translation_task( $post_ids, $term_ids, $target_languages ) {
|
||||
update_option(
|
||||
'htbd_translation_task',
|
||||
array(
|
||||
'started_at' => current_time( 'mysql', true ),
|
||||
'post_ids' => array_values( array_filter( array_map( 'absint', (array) $post_ids ) ) ),
|
||||
'term_ids' => array_values( array_filter( array_map( 'absint', (array) $term_ids ) ) ),
|
||||
'target_languages' => array_values( array_filter( array_map( 'sanitize_key', (array) $target_languages ) ) ),
|
||||
),
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
private function translation_task_label( $post_ids, $term_ids ) {
|
||||
if ( ! empty( $post_ids ) && ! empty( $term_ids ) ) {
|
||||
return __( 'Full translation', 'hyx-translator-for-baidu-translate' );
|
||||
}
|
||||
|
||||
if ( 1 === count( $post_ids ) ) {
|
||||
return __( 'Single post translation', 'hyx-translator-for-baidu-translate' );
|
||||
}
|
||||
|
||||
if ( ! empty( $post_ids ) ) {
|
||||
return __( 'Posts and pages translation', 'hyx-translator-for-baidu-translate' );
|
||||
}
|
||||
|
||||
if ( ! empty( $term_ids ) ) {
|
||||
return __( 'Categories and tags translation', 'hyx-translator-for-baidu-translate' );
|
||||
}
|
||||
|
||||
return __( 'No translation task', 'hyx-translator-for-baidu-translate' );
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
class BTRANSLATE_Baidu_Provider implements BTRANSLATE_Translation_Provider {
|
||||
class HTBD_Baidu_Provider implements HTBD_Translation_Provider {
|
||||
private $app_id;
|
||||
private $secret_key;
|
||||
|
||||
@@ -19,7 +19,7 @@ class BTRANSLATE_Baidu_Provider implements BTRANSLATE_Translation_Provider {
|
||||
if ( '' === $this->app_id || '' === $this->secret_key ) {
|
||||
$this->log_request( $source_value, $source_language, $target_language, $context, 'missing_credentials' );
|
||||
|
||||
return BTRANSLATE_Translation_Result::failure( 'missing_credentials', __( 'Baidu Translate credentials are not configured.', 'btranslate' ) );
|
||||
return HTBD_Translation_Result::failure( 'missing_credentials', __( 'Baidu Translate credentials are not configured.', 'hyx-translator-for-baidu-translate' ) );
|
||||
}
|
||||
|
||||
$salt = (string) wp_rand( 100000, 999999 );
|
||||
@@ -42,27 +42,27 @@ class BTRANSLATE_Baidu_Provider implements BTRANSLATE_Translation_Provider {
|
||||
if ( is_wp_error( $response ) ) {
|
||||
$this->log_request( $source_value, $source_language, $target_language, $context, 'request_failed', $response->get_error_code() );
|
||||
|
||||
return BTRANSLATE_Translation_Result::failure( 'request_failed', $response->get_error_message() );
|
||||
return HTBD_Translation_Result::failure( 'request_failed', $response->get_error_message() );
|
||||
}
|
||||
|
||||
$body = json_decode( wp_remote_retrieve_body( $response ), true );
|
||||
|
||||
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.', 'btranslate' );
|
||||
$error_message = isset( $body['error_msg'] ) ? sanitize_text_field( $body['error_msg'] ) : __( 'Baidu Translate returned an invalid response.', 'hyx-translator-for-baidu-translate' );
|
||||
$this->log_request( $source_value, $source_language, $target_language, $context, 'failed', $error_code );
|
||||
|
||||
return BTRANSLATE_Translation_Result::failure( $error_code, $error_message );
|
||||
return HTBD_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 BTRANSLATE_Translation_Result::success( implode( "\n", $translated_parts ) );
|
||||
return HTBD_Translation_Result::success( implode( "\n", $translated_parts ) );
|
||||
}
|
||||
|
||||
private function log_request( $source_value, $source_language, $target_language, $context, $status, $error_code = '' ) {
|
||||
$settings = BTRANSLATE_Settings::get();
|
||||
$settings = HTBD_Settings::get();
|
||||
|
||||
if ( empty( $settings['log_requests'] ) ) {
|
||||
return;
|
||||
@@ -73,12 +73,12 @@ class BTRANSLATE_Baidu_Provider implements BTRANSLATE_Translation_Provider {
|
||||
'source_language' => sanitize_key( $source_language ),
|
||||
'target_language' => sanitize_key( $target_language ),
|
||||
'context' => sanitize_key( $context ),
|
||||
'source_fingerprint' => BTRANSLATE_Translation_Identity::source_fingerprint( (string) $source_value ),
|
||||
'source_fingerprint' => HTBD_Translation_Identity::source_fingerprint( (string) $source_value ),
|
||||
'source_length' => strlen( (string) $source_value ),
|
||||
'status' => sanitize_key( $status ),
|
||||
'error_code' => sanitize_key( $error_code ),
|
||||
);
|
||||
|
||||
do_action( 'btranslate_translation_request_logged', $entry );
|
||||
do_action( 'htbd_translation_request_logged', $entry );
|
||||
}
|
||||
}
|
||||
@@ -1,445 +0,0 @@
|
||||
<?php
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
class BTRANSLATE_Admin {
|
||||
public function register() {
|
||||
add_action( 'admin_menu', array( $this, 'add_settings_page' ) );
|
||||
add_action( 'admin_init', array( $this, 'register_settings' ) );
|
||||
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_settings_assets' ) );
|
||||
add_action( 'admin_post_btranslate_queue_existing_translations', array( $this, 'queue_existing_translations' ) );
|
||||
add_action( 'admin_post_btranslate_clear_translation_cache', array( $this, 'clear_translation_cache' ) );
|
||||
add_action( 'admin_post_btranslate_translate_post', array( $this, 'queue_post_translation' ) );
|
||||
add_action( 'admin_post_btranslate_retry_failed_translation', array( $this, 'retry_failed_translation' ) );
|
||||
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_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_page_posts_custom_column', array( $this, 'render_translation_column' ), 10, 2 );
|
||||
add_filter( 'plugin_action_links_' . plugin_basename( BTRANSLATE_FILE ), array( $this, 'add_plugin_settings_link' ) );
|
||||
}
|
||||
|
||||
public function add_settings_page() {
|
||||
add_options_page( __( 'Btranslate', 'btranslate' ), __( 'Btranslate', 'btranslate' ), 'manage_options', 'btranslate', array( $this, 'render_settings_page' ) );
|
||||
}
|
||||
|
||||
public function register_settings() {
|
||||
register_setting( 'btranslate_settings', 'btranslate_settings', array( $this, 'sanitize_settings' ) );
|
||||
}
|
||||
|
||||
public function enqueue_settings_assets( $hook_suffix ) {
|
||||
if ( 'settings_page_btranslate' !== $hook_suffix ) {
|
||||
return;
|
||||
}
|
||||
|
||||
wp_enqueue_script(
|
||||
'btranslate-admin',
|
||||
plugins_url( 'assets/js/btranslate-admin.js', BTRANSLATE_FILE ),
|
||||
array(),
|
||||
BTRANSLATE_VERSION,
|
||||
true
|
||||
);
|
||||
wp_localize_script(
|
||||
'btranslate-admin',
|
||||
'btranslateAdmin',
|
||||
array(
|
||||
'progressUrl' => admin_url( 'admin-ajax.php' ),
|
||||
'progressNonce' => wp_create_nonce( 'btranslate_translation_progress' ),
|
||||
'i18n' => array(
|
||||
'contentItems' => __( '(%1$s / %2$s content items)', 'btranslate' ),
|
||||
'latestTask' => __( 'Latest task: %1$s. Posts and pages: %2$s / %3$s; categories and tags: %4$s / %5$s. Updates automatically every 5 seconds.', 'btranslate' ),
|
||||
'failedItems' => __( 'Failed translations', 'btranslate' ),
|
||||
'retranslate' => __( 'Retranslate', 'btranslate' ),
|
||||
'progressError' => __( 'Unable to load translation progress at this time.', 'btranslate' ),
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function sanitize_settings( $settings ) {
|
||||
$settings = (array) $settings;
|
||||
$bindings = array();
|
||||
$routing_modes = BTRANSLATE_Settings::routing_modes( $settings['routing_mode'] ?? array() );
|
||||
$target_languages = array_values( array_filter( array_map( 'sanitize_key', explode( ',', (string) ( $settings['target_languages'] ?? 'en' ) ) ) ) );
|
||||
|
||||
foreach ( preg_split( '/\r\n|\r|\n/', (string) ( $settings['domain_bindings'] ?? '' ) ) as $line ) {
|
||||
$parts = array_map( 'trim', explode( '=', $line, 2 ) );
|
||||
$domain = 2 === count( $parts ) ? BTRANSLATE_Settings::normalize_domain( $parts[0] ) : '';
|
||||
$language = 2 === count( $parts ) ? sanitize_key( $parts[1] ) : '';
|
||||
if ( '' !== $domain && in_array( $language, $target_languages, true ) && ! in_array( $language, $bindings, true ) ) {
|
||||
$bindings[ $domain ] = $language;
|
||||
}
|
||||
}
|
||||
|
||||
return array(
|
||||
'source_language' => sanitize_key( $settings['source_language'] ?? 'zh' ),
|
||||
'target_languages' => $target_languages,
|
||||
'routing_mode' => $routing_modes,
|
||||
'domain_bindings' => $bindings,
|
||||
'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'] ),
|
||||
);
|
||||
}
|
||||
|
||||
public function render_settings_page() {
|
||||
$settings = BTRANSLATE_Settings::get();
|
||||
$bindings = array();
|
||||
foreach ( (array) $settings['domain_bindings'] as $domain => $language ) {
|
||||
$bindings[] = $domain . '=' . $language;
|
||||
}
|
||||
?>
|
||||
<div class="wrap">
|
||||
<h1>Btranslate</h1>
|
||||
<form action="options.php" method="post">
|
||||
<?php settings_fields( 'btranslate_settings' ); ?>
|
||||
<table class="form-table" role="presentation">
|
||||
<tr><th scope="row"><label for="btranslate-source-language"><?php esc_html_e( 'Source language', 'btranslate' ); ?></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"><?php esc_html_e( 'Use a Baidu Translate language code, for example zh.', 'btranslate' ); ?></p></td></tr>
|
||||
<tr><th scope="row"><label for="btranslate-target-languages"><?php esc_html_e( 'Target languages', 'btranslate' ); ?></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"><?php esc_html_e( 'Separate multiple language codes with commas, for example en,ja.', 'btranslate' ); ?></p></td></tr>
|
||||
<tr><th scope="row"><?php esc_html_e( 'URL mode', 'btranslate' ); ?></th><td><fieldset id="btranslate-routing-mode"><label><input type="checkbox" name="btranslate_settings[routing_mode][]" value="subdirectory" <?php checked( in_array( 'subdirectory', $settings['routing_mode'], true ) ); ?> /> <?php esc_html_e( 'Subdirectory', 'btranslate' ); ?></label><br /><label><input type="checkbox" name="btranslate_settings[routing_mode][]" value="domain" <?php checked( in_array( 'domain', $settings['routing_mode'], true ) ); ?> /> <?php esc_html_e( 'Domain', 'btranslate' ); ?></label></fieldset></td></tr>
|
||||
<tr id="btranslate-domain-bindings-row"<?php echo in_array( 'domain', $settings['routing_mode'], true ) ? '' : ' style="display:none"'; ?>><th scope="row"><label for="btranslate-domain-bindings"><?php esc_html_e( 'Domain bindings', 'btranslate' ); ?></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"><?php esc_html_e( 'Enter one domain=language code pair per line, for example en.example.com=en. Do not include https://, a path, or a port.', 'btranslate' ); ?></p></td></tr>
|
||||
<tr><th scope="row"><label for="btranslate-baidu-app-id"><?php esc_html_e( 'Baidu application ID', 'btranslate' ); ?></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="btranslate-baidu-secret-key"><?php esc_html_e( 'Baidu secret key', 'btranslate' ); ?></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"><?php esc_html_e( 'Request logging', 'btranslate' ); ?></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'] ); ?> /> <?php esc_html_e( 'Log each Baidu Translate request', 'btranslate' ); ?></label><p class="description"><?php esc_html_e( 'When enabled, fires the btranslate_translation_request_logged action with the language, field, text fingerprint, length, and result status. Credentials, source text, translated text, and full API responses are never included.', 'btranslate' ); ?></p></td></tr>
|
||||
</table>
|
||||
<?php submit_button(); ?>
|
||||
<div class="notice notice-warning inline">
|
||||
<p><strong><?php esc_html_e( 'After saving settings for the first time, manually run "Retranslate all content" below.', 'btranslate' ); ?></strong></p>
|
||||
</div>
|
||||
</form>
|
||||
<hr />
|
||||
<h2><?php esc_html_e( 'Retranslate content', 'btranslate' ); ?></h2>
|
||||
<p><?php esc_html_e( 'Queue all published posts, pages, categories, and tags for translation again.', 'btranslate' ); ?></p>
|
||||
<form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post" data-btranslate-confirm="<?php echo esc_attr__( 'This will retranslate all content and use your API quota. Continue?', 'btranslate' ); ?>">
|
||||
<input type="hidden" name="action" value="btranslate_queue_existing_translations" />
|
||||
<?php wp_nonce_field( 'btranslate_queue_existing_translations' ); ?>
|
||||
<?php submit_button( __( 'Retranslate all content', 'btranslate' ), 'secondary', 'submit', false ); ?>
|
||||
</form>
|
||||
<p><?php esc_html_e( 'You can also retranslate a single content type.', 'btranslate' ); ?></p>
|
||||
<form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post" style="display:inline-block;margin-right:8px" data-btranslate-confirm="<?php echo esc_attr__( 'This will retranslate all posts and pages and use your API quota. Continue?', 'btranslate' ); ?>">
|
||||
<input type="hidden" name="action" value="btranslate_queue_existing_translations" />
|
||||
<input type="hidden" name="scope" value="posts" />
|
||||
<?php wp_nonce_field( 'btranslate_queue_existing_translations' ); ?>
|
||||
<?php submit_button( __( 'Translate all posts', 'btranslate' ), 'secondary', 'submit', false ); ?>
|
||||
</form>
|
||||
<form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post" style="display:inline-block" data-btranslate-confirm="<?php echo esc_attr__( 'This will retranslate all categories and tags and use your API quota. Continue?', 'btranslate' ); ?>">
|
||||
<input type="hidden" name="action" value="btranslate_queue_existing_translations" />
|
||||
<input type="hidden" name="scope" value="terms" />
|
||||
<?php wp_nonce_field( 'btranslate_queue_existing_translations' ); ?>
|
||||
<?php submit_button( __( 'Translate all categories and tags', 'btranslate' ), 'secondary', 'submit', false ); ?>
|
||||
</form>
|
||||
<hr />
|
||||
<h2><?php esc_html_e( 'Translation cache', 'btranslate' ); ?></h2>
|
||||
<p><?php esc_html_e( 'After clearing the cache, the front end temporarily displays source content. Baidu Translate API calls resume when content is translated again.', 'btranslate' ); ?></p>
|
||||
<form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post" data-btranslate-confirm="<?php echo esc_attr__( 'This will delete all saved translations, cancel pending translation tasks, and reset progress. Continue?', 'btranslate' ); ?>">
|
||||
<input type="hidden" name="action" value="btranslate_clear_translation_cache" />
|
||||
<?php wp_nonce_field( 'btranslate_clear_translation_cache' ); ?>
|
||||
<?php submit_button( __( 'Clear translation cache', 'btranslate' ), 'delete', 'submit', false ); ?>
|
||||
</form>
|
||||
<hr />
|
||||
<h2><?php esc_html_e( 'Translation progress', 'btranslate' ); ?></h2>
|
||||
<div id="btranslate-translation-progress" aria-live="polite">
|
||||
<p><?php esc_html_e( 'Loading translation progress...', 'btranslate' ); ?></p>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
public function queue_existing_translations() {
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
wp_die( esc_html__( 'You do not have permission to perform this action.', 'btranslate' ) );
|
||||
}
|
||||
|
||||
check_admin_referer( 'btranslate_queue_existing_translations' );
|
||||
$scope = isset( $_POST['scope'] ) ? sanitize_key( wp_unslash( $_POST['scope'] ) ) : 'all';
|
||||
if ( ! in_array( $scope, array( 'all', 'posts', 'terms' ), true ) ) {
|
||||
wp_die( esc_html__( 'Invalid translation scope.', 'btranslate' ) );
|
||||
}
|
||||
|
||||
$scheduled = 0;
|
||||
$next_run = time();
|
||||
$post_ids = array();
|
||||
$term_ids = array();
|
||||
$languages = BTRANSLATE_Settings::target_languages();
|
||||
|
||||
if ( 'all' === $scope || 'posts' === $scope ) {
|
||||
$post_ids = get_posts(
|
||||
array(
|
||||
'post_type' => array( 'post', 'page' ),
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => -1,
|
||||
'fields' => 'ids',
|
||||
)
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
if ( 'all' === $scope || 'terms' === $scope ) {
|
||||
$term_ids = get_terms(
|
||||
array(
|
||||
'taxonomy' => array( 'category', 'post_tag' ),
|
||||
'hide_empty' => false,
|
||||
'fields' => 'ids',
|
||||
)
|
||||
);
|
||||
if ( is_wp_error( $term_ids ) ) {
|
||||
$term_ids = array();
|
||||
}
|
||||
}
|
||||
|
||||
$this->update_translation_task( $post_ids, $term_ids, $languages );
|
||||
|
||||
foreach ( $post_ids as $post_id ) {
|
||||
foreach ( $languages as $target_language ) {
|
||||
$this->clear_scheduled_event( 'btranslate_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;
|
||||
++$scheduled;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ( $term_ids as $term_id ) {
|
||||
foreach ( $languages as $target_language ) {
|
||||
$this->clear_scheduled_event( 'btranslate_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;
|
||||
++$scheduled;
|
||||
}
|
||||
}
|
||||
|
||||
wp_safe_redirect( add_query_arg( 'btranslate_queued', $scheduled, admin_url( 'options-general.php?page=btranslate' ) ) );
|
||||
exit;
|
||||
}
|
||||
|
||||
public function clear_translation_cache() {
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
wp_die( esc_html__( 'You do not have permission to perform this action.', 'btranslate' ) );
|
||||
}
|
||||
|
||||
check_admin_referer( 'btranslate_clear_translation_cache' );
|
||||
$deleted = ( new BTRANSLATE_Translation_Store() )->clear();
|
||||
|
||||
if ( false === $deleted ) {
|
||||
wp_die( esc_html__( 'Failed to clear the translation cache.', 'btranslate' ) );
|
||||
}
|
||||
|
||||
wp_clear_scheduled_hook( 'btranslate_process_translation' );
|
||||
wp_clear_scheduled_hook( 'btranslate_process_term_translation' );
|
||||
wp_clear_scheduled_hook( 'btranslate_process_seo_output_translation' );
|
||||
delete_option( 'btranslate_translation_task' );
|
||||
delete_option( 'btranslate_retranslation_batch' );
|
||||
|
||||
wp_safe_redirect( add_query_arg( 'btranslate_cache_cleared', 1, admin_url( 'options-general.php?page=btranslate' ) ) );
|
||||
exit;
|
||||
}
|
||||
|
||||
private function clear_scheduled_event( $hook, $args ) {
|
||||
while ( false !== ( $timestamp = wp_next_scheduled( $hook, $args ) ) ) {
|
||||
wp_unschedule_event( $timestamp, $hook, $args );
|
||||
}
|
||||
}
|
||||
|
||||
public function add_plugin_settings_link( $links ) {
|
||||
array_unshift( $links, '<a href="' . esc_url( admin_url( 'options-general.php?page=btranslate' ) ) . '">' . esc_html__( 'Settings', 'btranslate' ) . '</a>' );
|
||||
|
||||
return $links;
|
||||
}
|
||||
|
||||
public function translation_progress() {
|
||||
check_ajax_referer( 'btranslate_translation_progress' );
|
||||
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
wp_send_json_error( array( 'message' => 'forbidden' ), 403 );
|
||||
}
|
||||
|
||||
$task = (array) get_option( 'btranslate_translation_task', 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() ) ) ) );
|
||||
$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'] ) : '';
|
||||
$store = new BTRANSLATE_Translation_Store();
|
||||
$counts = $store->get_completed_item_counts( $languages, $post_ids, $term_ids, $started_at );
|
||||
$failures = $this->prepare_failed_items( $store->get_failed_items( $languages, $post_ids, $term_ids, $started_at ) );
|
||||
|
||||
$posts_total = count( $post_ids ) * count( $languages );
|
||||
$terms_total = count( $term_ids ) * count( $languages );
|
||||
$total = $posts_total + $terms_total;
|
||||
$posts_completed = min( $counts['posts'], $posts_total );
|
||||
$terms_completed = min( $counts['terms'], $terms_total );
|
||||
$completed = $posts_completed + $terms_completed;
|
||||
$percent = $total ? (int) floor( ( $completed / $total ) * 100 ) : 100;
|
||||
|
||||
wp_send_json_success(
|
||||
array(
|
||||
'posts_completed' => $posts_completed,
|
||||
'posts_total' => $posts_total,
|
||||
'terms_completed' => $terms_completed,
|
||||
'terms_total' => $terms_total,
|
||||
'completed' => $completed,
|
||||
'total' => $total,
|
||||
'percent' => $percent,
|
||||
'batch_started_at' => $started_at,
|
||||
'task_label' => $this->translation_task_label( $post_ids, $term_ids ),
|
||||
'failed_items' => $failures,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function add_translation_column( $columns ) {
|
||||
$columns['btranslate_translation'] = __( 'Translation status', 'btranslate' );
|
||||
|
||||
return $columns;
|
||||
}
|
||||
|
||||
public function render_translation_column( $column, $post_id ) {
|
||||
if ( 'btranslate_translation' !== $column ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$store = new BTRANSLATE_Translation_Store();
|
||||
|
||||
foreach ( BTRANSLATE_Settings::target_languages() as $target_language ) {
|
||||
$status = $store->get_post_language_status( $post_id, $target_language );
|
||||
$translated = ! empty( $status['translated_fields'] );
|
||||
$button_text = $translated ? __( 'Retranslate', 'btranslate' ) : __( 'Translate', 'btranslate' );
|
||||
$action_url = wp_nonce_url(
|
||||
add_query_arg(
|
||||
array(
|
||||
'action' => 'btranslate_translate_post',
|
||||
'post_id' => $post_id,
|
||||
'language' => $target_language,
|
||||
'force' => $translated ? '1' : '0',
|
||||
),
|
||||
admin_url( 'admin-post.php' )
|
||||
),
|
||||
'btranslate_translate_post_' . $post_id . '_' . $target_language
|
||||
);
|
||||
|
||||
echo '<p><strong>' . esc_html( strtoupper( $target_language ) ) . '</strong>: ';
|
||||
echo $translated ? '<span style="color:#008a20">' . esc_html__( 'Translated', 'btranslate' ) . '</span>' : '<span>' . esc_html__( 'Not translated', 'btranslate' ) . '</span>';
|
||||
if ( $translated && ! empty( $status['last_translated_at'] ) ) {
|
||||
echo '<br><small>' . esc_html__( 'Last translated:', 'btranslate' ) . ' ' . esc_html( get_date_from_gmt( $status['last_translated_at'], 'Y-m-d H:i' ) ) . '</small>';
|
||||
}
|
||||
echo '<br><a class="button button-small" href="' . esc_url( $action_url ) . '">' . esc_html( $button_text ) . '</a></p>';
|
||||
}
|
||||
}
|
||||
|
||||
public function queue_post_translation() {
|
||||
$post_id = isset( $_GET['post_id'] ) ? absint( $_GET['post_id'] ) : 0;
|
||||
$target_language = isset( $_GET['language'] ) ? sanitize_key( wp_unslash( $_GET['language'] ) ) : '';
|
||||
$force_refresh = ! empty( $_GET['force'] );
|
||||
|
||||
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( esc_html__( 'Invalid translation request.', 'btranslate' ) );
|
||||
}
|
||||
|
||||
check_admin_referer( 'btranslate_translate_post_' . $post_id . '_' . $target_language );
|
||||
$this->update_translation_task( array( $post_id ), array(), array( $target_language ) );
|
||||
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 ) ) );
|
||||
exit;
|
||||
}
|
||||
|
||||
public function retry_failed_translation() {
|
||||
$item_type = isset( $_GET['item_type'] ) ? sanitize_key( wp_unslash( $_GET['item_type'] ) ) : '';
|
||||
$item_id = isset( $_GET['item_id'] ) ? absint( $_GET['item_id'] ) : 0;
|
||||
$target_language = isset( $_GET['language'] ) ? sanitize_key( wp_unslash( $_GET['language'] ) ) : '';
|
||||
|
||||
if ( ! current_user_can( 'manage_options' ) || ! $item_id || ! in_array( $item_type, array( 'post', 'term' ), true ) || ! in_array( $target_language, BTRANSLATE_Settings::target_languages(), true ) ) {
|
||||
wp_die( esc_html__( 'Invalid translation request.', 'btranslate' ) );
|
||||
}
|
||||
|
||||
check_admin_referer( 'btranslate_retry_failed_translation_' . $item_type . '_' . $item_id . '_' . $target_language );
|
||||
|
||||
if ( 'post' === $item_type ) {
|
||||
wp_schedule_single_event( time() + 5, 'btranslate_process_translation', array( $item_id, $target_language, true ) );
|
||||
} else {
|
||||
wp_schedule_single_event( time() + 5, 'btranslate_process_term_translation', array( $item_id, $target_language, true ) );
|
||||
}
|
||||
|
||||
wp_safe_redirect( admin_url( 'options-general.php?page=btranslate' ) );
|
||||
exit;
|
||||
}
|
||||
|
||||
private function prepare_failed_items( $failures ) {
|
||||
$items = array();
|
||||
|
||||
foreach ( (array) $failures as $failure ) {
|
||||
$context_parts = explode( ':', $failure['item_context'], 2 );
|
||||
$item_type = $context_parts[0] ?? '';
|
||||
$item_id = isset( $context_parts[1] ) ? absint( $context_parts[1] ) : 0;
|
||||
$target_language = sanitize_key( $failure['target_language'] ?? '' );
|
||||
|
||||
if ( ! $item_id || ! in_array( $item_type, array( 'post', 'term' ), true ) || '' === $target_language ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( 'post' === $item_type ) {
|
||||
$name = get_the_title( $item_id );
|
||||
} else {
|
||||
$term = get_term( $item_id );
|
||||
$name = $term && ! is_wp_error( $term ) ? $term->name : '';
|
||||
}
|
||||
|
||||
if ( '' === $name ) {
|
||||
$name = sprintf( __( 'Content #%d', 'btranslate' ), $item_id );
|
||||
}
|
||||
|
||||
$retry_url = wp_nonce_url(
|
||||
add_query_arg(
|
||||
array(
|
||||
'action' => 'btranslate_retry_failed_translation',
|
||||
'item_type' => $item_type,
|
||||
'item_id' => $item_id,
|
||||
'language' => $target_language,
|
||||
),
|
||||
admin_url( 'admin-post.php' )
|
||||
),
|
||||
'btranslate_retry_failed_translation_' . $item_type . '_' . $item_id . '_' . $target_language
|
||||
);
|
||||
|
||||
$items[] = array(
|
||||
'name' => $name,
|
||||
'language' => strtoupper( $target_language ),
|
||||
'retry_url' => $retry_url,
|
||||
);
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
private function update_translation_task( $post_ids, $term_ids, $target_languages ) {
|
||||
update_option(
|
||||
'btranslate_translation_task',
|
||||
array(
|
||||
'started_at' => current_time( 'mysql', true ),
|
||||
'post_ids' => array_values( array_filter( array_map( 'absint', (array) $post_ids ) ) ),
|
||||
'term_ids' => array_values( array_filter( array_map( 'absint', (array) $term_ids ) ) ),
|
||||
'target_languages' => array_values( array_filter( array_map( 'sanitize_key', (array) $target_languages ) ) ),
|
||||
),
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
private function translation_task_label( $post_ids, $term_ids ) {
|
||||
if ( ! empty( $post_ids ) && ! empty( $term_ids ) ) {
|
||||
return __( 'Full translation', 'btranslate' );
|
||||
}
|
||||
|
||||
if ( 1 === count( $post_ids ) ) {
|
||||
return __( 'Single post translation', 'btranslate' );
|
||||
}
|
||||
|
||||
if ( ! empty( $post_ids ) ) {
|
||||
return __( 'Posts and pages translation', 'btranslate' );
|
||||
}
|
||||
|
||||
if ( ! empty( $term_ids ) ) {
|
||||
return __( 'Categories and tags translation', 'btranslate' );
|
||||
}
|
||||
|
||||
return __( 'No translation task', 'btranslate' );
|
||||
}
|
||||
}
|
||||
+33
-33
@@ -2,20 +2,20 @@
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
class BTRANSLATE_Content_Controller {
|
||||
class HTBD_Content_Controller {
|
||||
private $store;
|
||||
private $router;
|
||||
|
||||
public function __construct( BTRANSLATE_Translation_Store $store, BTRANSLATE_Language_Router $router ) {
|
||||
public function __construct( HTBD_Translation_Store $store, HTBD_Language_Router $router ) {
|
||||
$this->store = $store;
|
||||
$this->router = $router;
|
||||
}
|
||||
|
||||
public function register() {
|
||||
add_action( 'save_post', array( $this, 'schedule_post_translation' ), 20, 3 );
|
||||
add_action( 'btranslate_process_translation', array( $this, 'translate_post' ), 10, 3 );
|
||||
add_action( 'btranslate_process_term_translation', array( $this, 'translate_term' ), 10, 3 );
|
||||
add_action( 'btranslate_process_seo_output_translation', array( $this, 'translate_seo_output' ), 10, 4 );
|
||||
add_action( 'htbd_process_translation', array( $this, 'translate_post' ), 10, 3 );
|
||||
add_action( 'htbd_process_term_translation', array( $this, 'translate_term' ), 10, 3 );
|
||||
add_action( 'htbd_process_seo_output_translation', array( $this, 'translate_seo_output' ), 10, 4 );
|
||||
add_action( 'created_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 );
|
||||
@@ -47,24 +47,24 @@ class BTRANSLATE_Content_Controller {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ( BTRANSLATE_Settings::target_languages() as $target_language ) {
|
||||
if ( ! wp_next_scheduled( 'btranslate_process_translation', array( $post_id, $target_language ) ) ) {
|
||||
wp_schedule_single_event( time() + 10, 'btranslate_process_translation', array( $post_id, $target_language ) );
|
||||
foreach ( HTBD_Settings::target_languages() as $target_language ) {
|
||||
if ( ! wp_next_scheduled( 'htbd_process_translation', array( $post_id, $target_language ) ) ) {
|
||||
wp_schedule_single_event( time() + 10, 'htbd_process_translation', array( $post_id, $target_language ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function translate_post( $post_id, $target_language, $force_refresh = false ) {
|
||||
$post = get_post( $post_id );
|
||||
$settings = BTRANSLATE_Settings::get();
|
||||
$settings = HTBD_Settings::get();
|
||||
|
||||
if ( ! $post || 'publish' !== $post->post_status || ! BTRANSLATE_Settings::is_supported_language( $target_language ) ) {
|
||||
if ( ! $post || 'publish' !== $post->post_status || ! HTBD_Settings::is_supported_language( $target_language ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$service = new BTRANSLATE_Translation_Service(
|
||||
$service = new HTBD_Translation_Service(
|
||||
$this->store,
|
||||
new BTRANSLATE_Baidu_Provider( $settings['baidu_app_id'], $settings['baidu_secret_key'] )
|
||||
new HTBD_Baidu_Provider( $settings['baidu_app_id'], $settings['baidu_secret_key'] )
|
||||
);
|
||||
|
||||
$fields = array(
|
||||
@@ -91,7 +91,7 @@ class BTRANSLATE_Content_Controller {
|
||||
if ( '' !== $value ) {
|
||||
$context = 'post:' . $post_id . ':' . $field;
|
||||
if ( 'post_content' === $field ) {
|
||||
( new BTRANSLATE_Content_Translator( $service ) )->translate( $value, $settings['source_language'], $target_language, $context, $force_refresh );
|
||||
( new HTBD_Content_Translator( $service ) )->translate( $value, $settings['source_language'], $target_language, $context, $force_refresh );
|
||||
} else {
|
||||
$service->get_or_translate( $value, $settings['source_language'], $target_language, $context, $force_refresh );
|
||||
}
|
||||
@@ -107,24 +107,24 @@ class BTRANSLATE_Content_Controller {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ( BTRANSLATE_Settings::target_languages() as $target_language ) {
|
||||
if ( ! wp_next_scheduled( 'btranslate_process_term_translation', array( $term_id, $target_language, false ) ) ) {
|
||||
wp_schedule_single_event( time() + 10, 'btranslate_process_term_translation', array( $term_id, $target_language, false ) );
|
||||
foreach ( HTBD_Settings::target_languages() as $target_language ) {
|
||||
if ( ! wp_next_scheduled( 'htbd_process_term_translation', array( $term_id, $target_language, false ) ) ) {
|
||||
wp_schedule_single_event( time() + 10, 'htbd_process_term_translation', array( $term_id, $target_language, false ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function translate_term( $term_id, $target_language, $force_refresh = false ) {
|
||||
$term = get_term( $term_id );
|
||||
$settings = BTRANSLATE_Settings::get();
|
||||
$settings = HTBD_Settings::get();
|
||||
|
||||
if ( ! $term || is_wp_error( $term ) || ! in_array( $term->taxonomy, array( 'category', 'post_tag' ), true ) || ! BTRANSLATE_Settings::is_supported_language( $target_language ) ) {
|
||||
if ( ! $term || is_wp_error( $term ) || ! in_array( $term->taxonomy, array( 'category', 'post_tag' ), true ) || ! HTBD_Settings::is_supported_language( $target_language ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$service = new BTRANSLATE_Translation_Service(
|
||||
$service = new HTBD_Translation_Service(
|
||||
$this->store,
|
||||
new BTRANSLATE_Baidu_Provider( $settings['baidu_app_id'], $settings['baidu_secret_key'] )
|
||||
new HTBD_Baidu_Provider( $settings['baidu_app_id'], $settings['baidu_secret_key'] )
|
||||
);
|
||||
|
||||
if ( '' !== $term->name ) {
|
||||
@@ -136,15 +136,15 @@ class BTRANSLATE_Content_Controller {
|
||||
}
|
||||
|
||||
public function translate_seo_output( $post_id, $source_value, $target_language, $context ) {
|
||||
$settings = BTRANSLATE_Settings::get();
|
||||
$settings = HTBD_Settings::get();
|
||||
|
||||
if ( ! get_post( $post_id ) || ! BTRANSLATE_Settings::is_supported_language( $target_language ) || '' === $source_value ) {
|
||||
if ( ! get_post( $post_id ) || ! HTBD_Settings::is_supported_language( $target_language ) || '' === $source_value ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$service = new BTRANSLATE_Translation_Service(
|
||||
$service = new HTBD_Translation_Service(
|
||||
$this->store,
|
||||
new BTRANSLATE_Baidu_Provider( $settings['baidu_app_id'], $settings['baidu_secret_key'] )
|
||||
new HTBD_Baidu_Provider( $settings['baidu_app_id'], $settings['baidu_secret_key'] )
|
||||
);
|
||||
|
||||
$service->get_or_translate( $source_value, $settings['source_language'], $target_language, $context );
|
||||
@@ -160,18 +160,18 @@ class BTRANSLATE_Content_Controller {
|
||||
return $this->escaped_translated_value( $content, 'post:' . $post_id . ':post_content' );
|
||||
}
|
||||
|
||||
$segments = BTRANSLATE_Content_Translator::segments( $content );
|
||||
$segments = HTBD_Content_Translator::segments( $content );
|
||||
if ( false === $segments ) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
$text_index = 0;
|
||||
foreach ( $segments as $index => $segment ) {
|
||||
if ( BTRANSLATE_Content_Translator::is_tag( $segment ) || '' === trim( $segment ) ) {
|
||||
if ( HTBD_Content_Translator::is_tag( $segment ) || '' === trim( $segment ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$whitespace = BTRANSLATE_Content_Translator::surrounding_whitespace( $segment );
|
||||
$whitespace = HTBD_Content_Translator::surrounding_whitespace( $segment );
|
||||
$translated = $this->escaped_translated_value( $whitespace['text'], 'post:' . $post_id . ':post_content:text:' . $text_index );
|
||||
$segments[ $index ] = $whitespace['leading'] . $translated . $whitespace['trailing'];
|
||||
++$text_index;
|
||||
@@ -306,15 +306,15 @@ class BTRANSLATE_Content_Controller {
|
||||
}
|
||||
|
||||
private function translated_value( $source_value, $context ) {
|
||||
$settings = BTRANSLATE_Settings::get();
|
||||
$settings = HTBD_Settings::get();
|
||||
$target_language = $this->router->current_language();
|
||||
|
||||
if ( $target_language === $settings['source_language'] || '' === $source_value ) {
|
||||
return $source_value;
|
||||
}
|
||||
|
||||
$provider = new BTRANSLATE_Baidu_Provider( $settings['baidu_app_id'], $settings['baidu_secret_key'] );
|
||||
$identity_key = BTRANSLATE_Translation_Identity::key( $source_value, $settings['source_language'], $target_language, $context, $provider->get_version() );
|
||||
$provider = new HTBD_Baidu_Provider( $settings['baidu_app_id'], $settings['baidu_secret_key'] );
|
||||
$identity_key = HTBD_Translation_Identity::key( $source_value, $settings['source_language'], $target_language, $context, $provider->get_version() );
|
||||
$translation = $this->store->find_valid( $identity_key );
|
||||
|
||||
return ! empty( $translation['translated_value'] ) ? $translation['translated_value'] : $source_value;
|
||||
@@ -327,7 +327,7 @@ class BTRANSLATE_Content_Controller {
|
||||
}
|
||||
|
||||
private function translated_or_schedule_seo_output( $source_value, $post_id, $field_context ) {
|
||||
$settings = BTRANSLATE_Settings::get();
|
||||
$settings = HTBD_Settings::get();
|
||||
$target_language = $this->router->current_language();
|
||||
$context = 'post:' . $post_id . ':' . $field_context;
|
||||
|
||||
@@ -341,8 +341,8 @@ class BTRANSLATE_Content_Controller {
|
||||
}
|
||||
|
||||
$event_args = array( $post_id, $source_value, $target_language, $context );
|
||||
if ( ! wp_next_scheduled( 'btranslate_process_seo_output_translation', $event_args ) ) {
|
||||
wp_schedule_single_event( time() + 5, 'btranslate_process_seo_output_translation', $event_args );
|
||||
if ( ! wp_next_scheduled( 'htbd_process_seo_output_translation', $event_args ) ) {
|
||||
wp_schedule_single_event( time() + 5, 'htbd_process_seo_output_translation', $event_args );
|
||||
}
|
||||
|
||||
return $source_value;
|
||||
+4
-4
@@ -2,17 +2,17 @@
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
class BTRANSLATE_Content_Translator {
|
||||
class HTBD_Content_Translator {
|
||||
private $service;
|
||||
|
||||
public function __construct( BTRANSLATE_Translation_Service $service ) {
|
||||
public function __construct( HTBD_Translation_Service $service ) {
|
||||
$this->service = $service;
|
||||
}
|
||||
|
||||
public function translate( $content, $source_language, $target_language, $context, $force_refresh = false ) {
|
||||
$segments = self::segments( $content );
|
||||
if ( false === $segments ) {
|
||||
return BTRANSLATE_Translation_Result::failure( 'content_parse_failed', 'Unable to protect links before translation.' );
|
||||
return HTBD_Translation_Result::failure( 'content_parse_failed', 'Unable to protect links before translation.' );
|
||||
}
|
||||
|
||||
$text_index = 0;
|
||||
@@ -29,7 +29,7 @@ class BTRANSLATE_Content_Translator {
|
||||
++$text_index;
|
||||
}
|
||||
|
||||
return BTRANSLATE_Translation_Result::success( $content );
|
||||
return HTBD_Translation_Result::success( $content );
|
||||
}
|
||||
|
||||
public static function segments( $content ) {
|
||||
+22
-22
@@ -2,7 +2,7 @@
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
class BTRANSLATE_Language_Router {
|
||||
class HTBD_Language_Router {
|
||||
private $request_language = '';
|
||||
|
||||
public function register() {
|
||||
@@ -17,13 +17,13 @@ class BTRANSLATE_Language_Router {
|
||||
}
|
||||
|
||||
public function query_vars( $query_vars ) {
|
||||
$query_vars[] = 'btranslate_language';
|
||||
$query_vars[] = 'htbd_language';
|
||||
|
||||
return $query_vars;
|
||||
}
|
||||
|
||||
public function strip_language_prefix( $do_parse_request, $wp, $extra_query_vars ) {
|
||||
if ( ! $do_parse_request || ! BTRANSLATE_Settings::is_routing_mode_enabled( 'subdirectory' ) || empty( $_SERVER['REQUEST_URI'] ) ) {
|
||||
if ( ! $do_parse_request || ! HTBD_Settings::is_routing_mode_enabled( 'subdirectory' ) || empty( $_SERVER['REQUEST_URI'] ) ) {
|
||||
return $do_parse_request;
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ class BTRANSLATE_Language_Router {
|
||||
$segments = explode( '/', $relative, 2 );
|
||||
$language = sanitize_key( $segments[0] );
|
||||
|
||||
if ( ! in_array( $language, BTRANSLATE_Settings::subdirectory_languages(), true ) ) {
|
||||
if ( ! in_array( $language, HTBD_Settings::subdirectory_languages(), true ) ) {
|
||||
return $do_parse_request;
|
||||
}
|
||||
|
||||
@@ -53,30 +53,30 @@ class BTRANSLATE_Language_Router {
|
||||
}
|
||||
|
||||
public function resolve_domain_language( $wp ) {
|
||||
$settings = BTRANSLATE_Settings::get();
|
||||
$host = isset( $_SERVER['HTTP_HOST'] ) ? BTRANSLATE_Settings::normalize_domain( sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) ) : '';
|
||||
$settings = HTBD_Settings::get();
|
||||
$host = isset( $_SERVER['HTTP_HOST'] ) ? HTBD_Settings::normalize_domain( sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) ) : '';
|
||||
$bindings = (array) $settings['domain_bindings'];
|
||||
|
||||
if ( '' !== $this->request_language ) {
|
||||
$wp->query_vars['btranslate_language'] = $this->request_language;
|
||||
$wp->query_vars['htbd_language'] = $this->request_language;
|
||||
}
|
||||
|
||||
if ( BTRANSLATE_Settings::is_routing_mode_enabled( 'domain' ) && '' !== $host && isset( $bindings[ $host ] ) && BTRANSLATE_Settings::is_supported_language( $bindings[ $host ] ) ) {
|
||||
if ( HTBD_Settings::is_routing_mode_enabled( 'domain' ) && '' !== $host && isset( $bindings[ $host ] ) && HTBD_Settings::is_supported_language( $bindings[ $host ] ) ) {
|
||||
if ( '' !== $this->request_language ) {
|
||||
$wp->query_vars = array( 'error' => '404' );
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$wp->query_vars['btranslate_language'] = sanitize_key( $bindings[ $host ] );
|
||||
$wp->query_vars['htbd_language'] = sanitize_key( $bindings[ $host ] );
|
||||
}
|
||||
}
|
||||
|
||||
public function current_language() {
|
||||
$settings = BTRANSLATE_Settings::get();
|
||||
$language = get_query_var( 'btranslate_language' );
|
||||
$settings = HTBD_Settings::get();
|
||||
$language = get_query_var( 'htbd_language' );
|
||||
|
||||
if ( $language && BTRANSLATE_Settings::is_supported_language( $language ) ) {
|
||||
if ( $language && HTBD_Settings::is_supported_language( $language ) ) {
|
||||
return sanitize_key( $language );
|
||||
}
|
||||
|
||||
@@ -84,14 +84,14 @@ class BTRANSLATE_Language_Router {
|
||||
}
|
||||
|
||||
public function localized_url( $url, $language = '' ) {
|
||||
$settings = BTRANSLATE_Settings::get();
|
||||
$settings = HTBD_Settings::get();
|
||||
$language = '' === $language ? $this->current_language() : sanitize_key( $language );
|
||||
|
||||
if ( $language === $settings['source_language'] || ! BTRANSLATE_Settings::is_supported_language( $language ) ) {
|
||||
if ( $language === $settings['source_language'] || ! HTBD_Settings::is_supported_language( $language ) ) {
|
||||
return $url;
|
||||
}
|
||||
|
||||
if ( BTRANSLATE_Settings::is_routing_mode_enabled( 'domain' ) && $this->request_uses_domain( $language ) ) {
|
||||
if ( HTBD_Settings::is_routing_mode_enabled( 'domain' ) && $this->request_uses_domain( $language ) ) {
|
||||
$domain = array_search( $language, (array) $settings['domain_bindings'], true );
|
||||
$parts = wp_parse_url( $url );
|
||||
|
||||
@@ -105,7 +105,7 @@ class BTRANSLATE_Language_Router {
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! BTRANSLATE_Settings::is_routing_mode_enabled( 'subdirectory' ) ) {
|
||||
if ( ! HTBD_Settings::is_routing_mode_enabled( 'subdirectory' ) ) {
|
||||
return $url;
|
||||
}
|
||||
|
||||
@@ -130,15 +130,15 @@ class BTRANSLATE_Language_Router {
|
||||
}
|
||||
|
||||
private function request_uses_domain( $language ) {
|
||||
$host = isset( $_SERVER['HTTP_HOST'] ) ? BTRANSLATE_Settings::normalize_domain( sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) ) : '';
|
||||
$bindings = (array) BTRANSLATE_Settings::get()['domain_bindings'];
|
||||
$host = isset( $_SERVER['HTTP_HOST'] ) ? HTBD_Settings::normalize_domain( sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) ) : '';
|
||||
$bindings = (array) HTBD_Settings::get()['domain_bindings'];
|
||||
|
||||
return '' !== $host && isset( $bindings[ $host ] ) && sanitize_key( $bindings[ $host ] ) === $language;
|
||||
}
|
||||
|
||||
private function source_origin_parts( $parts ) {
|
||||
$host = isset( $_SERVER['HTTP_HOST'] ) ? BTRANSLATE_Settings::normalize_domain( sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) ) : '';
|
||||
$bindings = (array) BTRANSLATE_Settings::get()['domain_bindings'];
|
||||
$host = isset( $_SERVER['HTTP_HOST'] ) ? HTBD_Settings::normalize_domain( sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) ) : '';
|
||||
$bindings = (array) HTBD_Settings::get()['domain_bindings'];
|
||||
|
||||
if ( '' === $host || ! isset( $bindings[ $host ] ) ) {
|
||||
return $parts;
|
||||
@@ -164,7 +164,7 @@ class BTRANSLATE_Language_Router {
|
||||
private function localized_path( $path, $language ) {
|
||||
$home_path = $this->home_path();
|
||||
$relative = $this->relative_path( $path );
|
||||
$languages = array_map( 'preg_quote', BTRANSLATE_Settings::target_languages() );
|
||||
$languages = array_map( 'preg_quote', HTBD_Settings::target_languages() );
|
||||
|
||||
if ( ! empty( $languages ) ) {
|
||||
$relative = preg_replace( '#^(?:' . implode( '|', $languages ) . ')(?=/|$)#', $language, $relative, 1 );
|
||||
@@ -180,7 +180,7 @@ class BTRANSLATE_Language_Router {
|
||||
private function source_path( $path ) {
|
||||
$home_path = $this->home_path();
|
||||
$relative = $this->relative_path( $path );
|
||||
$languages = array_map( 'preg_quote', BTRANSLATE_Settings::target_languages() );
|
||||
$languages = array_map( 'preg_quote', HTBD_Settings::target_languages() );
|
||||
|
||||
if ( ! empty( $languages ) ) {
|
||||
$relative = preg_replace( '#^(?:' . implode( '|', $languages ) . ')(?=/|$)/?#', '', $relative, 1 );
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
class BTRANSLATE_Plugin {
|
||||
class HTBD_Plugin {
|
||||
private static $instance;
|
||||
private $router;
|
||||
private $sitemap_controller;
|
||||
@@ -10,13 +10,13 @@ class BTRANSLATE_Plugin {
|
||||
private $admin;
|
||||
|
||||
private function __construct() {
|
||||
$this->router = new BTRANSLATE_Language_Router();
|
||||
$this->sitemap_controller = new BTRANSLATE_Sitemap_Controller( $this->router );
|
||||
$this->content_controller = new BTRANSLATE_Content_Controller( new BTRANSLATE_Translation_Store(), $this->router );
|
||||
$this->admin = new BTRANSLATE_Admin();
|
||||
$this->router = new HTBD_Language_Router();
|
||||
$this->sitemap_controller = new HTBD_Sitemap_Controller( $this->router );
|
||||
$this->content_controller = new HTBD_Content_Controller( new HTBD_Translation_Store(), $this->router );
|
||||
$this->admin = new HTBD_Admin();
|
||||
|
||||
add_action( 'init', array( $this, 'register' ) );
|
||||
add_action( 'update_option_btranslate_settings', array( $this, 'refresh_rewrite_rules' ), 10, 2 );
|
||||
add_action( 'update_option_htbd_settings', array( $this, 'refresh_rewrite_rules' ), 10, 2 );
|
||||
$this->admin->register();
|
||||
}
|
||||
|
||||
@@ -29,9 +29,16 @@ class BTRANSLATE_Plugin {
|
||||
}
|
||||
|
||||
public static function activate() {
|
||||
BTRANSLATE_Translation_Store::install();
|
||||
HTBD_Translation_Store::install();
|
||||
wp_clear_scheduled_hook( 'btranslate_process_translation' );
|
||||
wp_clear_scheduled_hook( 'btranslate_process_term_translation' );
|
||||
wp_clear_scheduled_hook( 'btranslate_process_seo_output_translation' );
|
||||
$legacy_settings = get_option( 'btranslate_settings', null );
|
||||
if ( null !== $legacy_settings && false === get_option( 'htbd_settings', false ) ) {
|
||||
add_option( 'htbd_settings', $legacy_settings, '', false );
|
||||
}
|
||||
add_option(
|
||||
'btranslate_settings',
|
||||
'htbd_settings',
|
||||
array(
|
||||
'source_language' => 'zh',
|
||||
'target_languages' => array( 'en' ),
|
||||
@@ -42,15 +49,15 @@ class BTRANSLATE_Plugin {
|
||||
'',
|
||||
false
|
||||
);
|
||||
$router = new BTRANSLATE_Language_Router();
|
||||
$router = new HTBD_Language_Router();
|
||||
$router->register_rewrite_rules();
|
||||
flush_rewrite_rules();
|
||||
}
|
||||
|
||||
public static function deactivate() {
|
||||
wp_clear_scheduled_hook( 'btranslate_process_translation' );
|
||||
wp_clear_scheduled_hook( 'btranslate_process_term_translation' );
|
||||
wp_clear_scheduled_hook( 'btranslate_process_seo_output_translation' );
|
||||
wp_clear_scheduled_hook( 'htbd_process_translation' );
|
||||
wp_clear_scheduled_hook( 'htbd_process_term_translation' );
|
||||
wp_clear_scheduled_hook( 'htbd_process_seo_output_translation' );
|
||||
flush_rewrite_rules();
|
||||
}
|
||||
|
||||
@@ -59,15 +66,15 @@ class BTRANSLATE_Plugin {
|
||||
$this->sitemap_controller->register();
|
||||
$this->content_controller->register();
|
||||
|
||||
if ( get_option( 'btranslate_flush_rewrite_rules', false ) ) {
|
||||
delete_option( 'btranslate_flush_rewrite_rules' );
|
||||
if ( get_option( 'htbd_flush_rewrite_rules', false ) ) {
|
||||
delete_option( 'htbd_flush_rewrite_rules' );
|
||||
flush_rewrite_rules();
|
||||
}
|
||||
}
|
||||
|
||||
public function refresh_rewrite_rules( $old_value, $new_value ) {
|
||||
if ( (array) $old_value !== (array) $new_value ) {
|
||||
update_option( 'btranslate_flush_rewrite_rules', 1, false );
|
||||
update_option( 'htbd_flush_rewrite_rules', 1, false );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
class BTRANSLATE_Settings {
|
||||
class HTBD_Settings {
|
||||
public static function get() {
|
||||
$defaults = array(
|
||||
'source_language' => 'zh',
|
||||
@@ -15,7 +15,16 @@ class BTRANSLATE_Settings {
|
||||
'log_requests' => false,
|
||||
);
|
||||
|
||||
$settings = wp_parse_args( (array) get_option( 'btranslate_settings', array() ), $defaults );
|
||||
$stored_settings = get_option( 'htbd_settings', false );
|
||||
if ( false === $stored_settings ) {
|
||||
$legacy_settings = get_option( 'btranslate_settings', false );
|
||||
if ( false !== $legacy_settings ) {
|
||||
$stored_settings = $legacy_settings;
|
||||
add_option( 'htbd_settings', $legacy_settings, '', false );
|
||||
}
|
||||
}
|
||||
|
||||
$settings = wp_parse_args( (array) $stored_settings, $defaults );
|
||||
$settings['routing_mode'] = self::routing_modes( $settings['routing_mode'] );
|
||||
|
||||
return $settings;
|
||||
+9
-9
@@ -2,11 +2,11 @@
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
class BTRANSLATE_Sitemap_Controller {
|
||||
class HTBD_Sitemap_Controller {
|
||||
private $router;
|
||||
private $target_language = '';
|
||||
|
||||
public function __construct( BTRANSLATE_Language_Router $router ) {
|
||||
public function __construct( HTBD_Language_Router $router ) {
|
||||
$this->router = $router;
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ class BTRANSLATE_Sitemap_Controller {
|
||||
}
|
||||
|
||||
public function intercept_request( $do_parse_request, $wp, $extra_query_vars ) {
|
||||
$settings = BTRANSLATE_Settings::get();
|
||||
$settings = HTBD_Settings::get();
|
||||
$language = $this->requested_language( $this->request_path(), $settings );
|
||||
|
||||
if ( '' === $language ) {
|
||||
@@ -52,7 +52,7 @@ class BTRANSLATE_Sitemap_Controller {
|
||||
}
|
||||
|
||||
private function source_sitemap_xml( $source_url ) {
|
||||
$cache_key = 'btranslate_sitemap_' . md5( $source_url );
|
||||
$cache_key = 'htbd_sitemap_' . md5( $source_url );
|
||||
$cached = get_transient( $cache_key );
|
||||
|
||||
if ( is_string( $cached ) && '' !== $cached ) {
|
||||
@@ -98,18 +98,18 @@ class BTRANSLATE_Sitemap_Controller {
|
||||
$path = '/' . ltrim( (string) $path, '/' );
|
||||
}
|
||||
|
||||
if ( BTRANSLATE_Settings::is_routing_mode_enabled( 'domain' ) && '/sitemap.xml' === untrailingslashit( $path ) ) {
|
||||
$host = isset( $_SERVER['HTTP_HOST'] ) ? BTRANSLATE_Settings::normalize_domain( sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) ) : '';
|
||||
if ( HTBD_Settings::is_routing_mode_enabled( 'domain' ) && '/sitemap.xml' === untrailingslashit( $path ) ) {
|
||||
$host = isset( $_SERVER['HTTP_HOST'] ) ? HTBD_Settings::normalize_domain( sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) ) : '';
|
||||
$bindings = (array) $settings['domain_bindings'];
|
||||
$language = isset( $bindings[ $host ] ) ? sanitize_key( $bindings[ $host ] ) : '';
|
||||
|
||||
if ( in_array( $language, BTRANSLATE_Settings::target_languages(), true ) ) {
|
||||
if ( in_array( $language, HTBD_Settings::target_languages(), true ) ) {
|
||||
return $language;
|
||||
}
|
||||
}
|
||||
|
||||
if ( BTRANSLATE_Settings::is_routing_mode_enabled( 'subdirectory' ) ) {
|
||||
foreach ( BTRANSLATE_Settings::subdirectory_languages() as $language ) {
|
||||
if ( HTBD_Settings::is_routing_mode_enabled( 'subdirectory' ) ) {
|
||||
foreach ( HTBD_Settings::subdirectory_languages() as $language ) {
|
||||
$language = sanitize_key( $language );
|
||||
|
||||
if ( '/' . $language . '/sitemap.xml' === untrailingslashit( $path ) ) {
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
class BTRANSLATE_Translation_Identity {
|
||||
class HTBD_Translation_Identity {
|
||||
public static function source_fingerprint( $source_value ) {
|
||||
return hash( 'sha256', (string) $source_value );
|
||||
}
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
class BTRANSLATE_Translation_Result {
|
||||
class HTBD_Translation_Result {
|
||||
public $success;
|
||||
public $value;
|
||||
public $error_code;
|
||||
+5
-5
@@ -2,23 +2,23 @@
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
class BTRANSLATE_Translation_Service {
|
||||
class HTBD_Translation_Service {
|
||||
private $store;
|
||||
private $provider;
|
||||
|
||||
public function __construct( BTRANSLATE_Translation_Store $store, BTRANSLATE_Translation_Provider $provider ) {
|
||||
public function __construct( HTBD_Translation_Store $store, HTBD_Translation_Provider $provider ) {
|
||||
$this->store = $store;
|
||||
$this->provider = $provider;
|
||||
}
|
||||
|
||||
public function get_or_translate( $source_value, $source_language, $target_language, $context, $force_refresh = false ) {
|
||||
$source_value = (string) $source_value;
|
||||
$source_fingerprint = BTRANSLATE_Translation_Identity::source_fingerprint( $source_value );
|
||||
$identity_key = BTRANSLATE_Translation_Identity::key( $source_value, $source_language, $target_language, $context, $this->provider->get_version() );
|
||||
$source_fingerprint = HTBD_Translation_Identity::source_fingerprint( $source_value );
|
||||
$identity_key = HTBD_Translation_Identity::key( $source_value, $source_language, $target_language, $context, $this->provider->get_version() );
|
||||
$existing = $this->store->find_valid( $identity_key );
|
||||
|
||||
if ( ! $force_refresh && ! empty( $existing['translated_value'] ) ) {
|
||||
return BTRANSLATE_Translation_Result::success( $existing['translated_value'] );
|
||||
return HTBD_Translation_Result::success( $existing['translated_value'] );
|
||||
}
|
||||
|
||||
$result = $this->provider->translate( $source_value, $source_language, $target_language, $context );
|
||||
+9
-2
@@ -2,11 +2,11 @@
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
class BTRANSLATE_Translation_Store {
|
||||
class HTBD_Translation_Store {
|
||||
public static function table_name() {
|
||||
global $wpdb;
|
||||
|
||||
return $wpdb->prefix . 'btranslate_translations';
|
||||
return $wpdb->prefix . 'hyx_bd_translations';
|
||||
}
|
||||
|
||||
public static function install() {
|
||||
@@ -15,6 +15,13 @@ class BTRANSLATE_Translation_Store {
|
||||
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
|
||||
|
||||
$table_name = self::table_name();
|
||||
$legacy_table = $wpdb->prefix . 'btranslate_translations';
|
||||
$table_exists = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table_name ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Checks the plugin-owned table during activation.
|
||||
$legacy_exists = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $legacy_table ) ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Checks for the legacy plugin-owned table during activation.
|
||||
if ( $legacy_exists === $legacy_table && $table_exists !== $table_name ) {
|
||||
$wpdb->query( $wpdb->prepare( 'RENAME TABLE %i TO %i', $legacy_table, $table_name ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Migrates persisted translations to the renamed table.
|
||||
}
|
||||
|
||||
$charset_collate = $wpdb->get_charset_collate();
|
||||
$sql = "CREATE TABLE {$table_name} (
|
||||
id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
||||
@@ -2,8 +2,12 @@
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
class BTRANSLATE_Uninstaller {
|
||||
class HTBD_Uninstaller {
|
||||
private const OPTION_NAMES = array(
|
||||
'htbd_settings',
|
||||
'htbd_flush_rewrite_rules',
|
||||
'htbd_translation_task',
|
||||
'htbd_retranslation_batch',
|
||||
'btranslate_settings',
|
||||
'btranslate_flush_rewrite_rules',
|
||||
'btranslate_translation_task',
|
||||
@@ -11,6 +15,9 @@ class BTRANSLATE_Uninstaller {
|
||||
);
|
||||
|
||||
private const CRON_HOOKS = array(
|
||||
'htbd_process_translation',
|
||||
'htbd_process_term_translation',
|
||||
'htbd_process_seo_output_translation',
|
||||
'btranslate_process_translation',
|
||||
'btranslate_process_term_translation',
|
||||
'btranslate_process_seo_output_translation',
|
||||
@@ -50,7 +57,12 @@ class BTRANSLATE_Uninstaller {
|
||||
delete_option( $option_name );
|
||||
}
|
||||
|
||||
$table_name = $wpdb->prefix . 'btranslate_translations';
|
||||
$wpdb->query( "DROP TABLE IF EXISTS {$table_name}" );
|
||||
$table_names = array(
|
||||
$wpdb->prefix . 'hyx_bd_translations',
|
||||
$wpdb->prefix . 'btranslate_translations',
|
||||
);
|
||||
foreach ( $table_names as $table_name ) {
|
||||
$wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $table_name ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
interface BTRANSLATE_Translation_Provider {
|
||||
interface HTBD_Translation_Provider {
|
||||
public function get_version();
|
||||
|
||||
public function translate( $source_value, $source_language, $target_language, $context );
|
||||
+9
-9
@@ -1,13 +1,13 @@
|
||||
# Simplified Chinese translations for Btranslate.
|
||||
# Copyright (C) 2026 Btranslate contributors
|
||||
# This file is distributed under the same license as the Btranslate package.
|
||||
# Simplified Chinese translations for HTBD.
|
||||
# Copyright (C) 2026 HTBD contributors
|
||||
# This file is distributed under the same license as the hyx-translator-for-baidu-translate package.
|
||||
msgid ""
|
||||
msgstr ""
|
||||
"Project-Id-Version: Btranslate 0.2.2\n"
|
||||
"Project-Id-Version: hyx-translator-for-baidu-translate 0.2.2\n"
|
||||
"Report-Msgid-Bugs-To: \n"
|
||||
"POT-Creation-Date: 2026-07-28 14:41+0800\n"
|
||||
"PO-Revision-Date: 2026-07-28 14:45+0800\n"
|
||||
"Last-Translator: Btranslate contributors\n"
|
||||
"Last-Translator: HTBD contributors\n"
|
||||
"Language-Team: Chinese (Simplified)\n"
|
||||
"Language: zh_CN\n"
|
||||
"MIME-Version: 1.0\n"
|
||||
@@ -15,8 +15,8 @@ msgstr ""
|
||||
"Content-Transfer-Encoding: 8bit\n"
|
||||
"Plural-Forms: nplurals=1; plural=0;\n"
|
||||
|
||||
msgid "Btranslate"
|
||||
msgstr "Btranslate"
|
||||
msgid "HTBD"
|
||||
msgstr "HTBD"
|
||||
|
||||
#, php-format
|
||||
msgid "(%1$s / %2$s content items)"
|
||||
@@ -74,8 +74,8 @@ msgstr "请求日志"
|
||||
msgid "Log each Baidu Translate request"
|
||||
msgstr "记录每次百度翻译请求"
|
||||
|
||||
msgid "When enabled, fires the btranslate_translation_request_logged action with the language, field, text fingerprint, length, and result status. Credentials, source text, translated text, and full API responses are never included."
|
||||
msgstr "启用后触发 btranslate_translation_request_logged 操作,提供语言、字段、文本指纹、长度和结果状态;不会提供密钥、原文、译文或完整 API 响应。"
|
||||
msgid "When enabled, fires the htbd_translation_request_logged action with the language, field, text fingerprint, length, and result status. Credentials, source text, translated text, and full API responses are never included."
|
||||
msgstr "启用后触发 htbd_translation_request_logged 操作,提供语言、字段、文本指纹、长度和结果状态;不会提供密钥、原文、译文或完整 API 响应。"
|
||||
|
||||
msgid "Retranslate content"
|
||||
msgstr "重新翻译内容"
|
||||
@@ -1,12 +1,12 @@
|
||||
# SOME DESCRIPTIVE TITLE.
|
||||
# Copyright (C) YEAR Btranslate contributors
|
||||
# This file is distributed under the same license as the Btranslate package.
|
||||
# Copyright (C) YEAR HTBD contributors
|
||||
# This file is distributed under the same license as the hyx-translator-for-baidu-translate package.
|
||||
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
|
||||
#
|
||||
#, fuzzy
|
||||
msgid ""
|
||||
msgstr ""
|
||||
"Project-Id-Version: Btranslate 0.2.2\n"
|
||||
"Project-Id-Version: hyx-translator-for-baidu-translate 0.2.2\n"
|
||||
"Report-Msgid-Bugs-To: \n"
|
||||
"POT-Creation-Date: 2026-07-28 14:41+0800\n"
|
||||
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
|
||||
@@ -17,204 +17,204 @@ msgstr ""
|
||||
"Content-Type: text/plain; charset=CHARSET\n"
|
||||
"Content-Transfer-Encoding: 8bit\n"
|
||||
|
||||
#: includes/class-btranslate-admin.php:22
|
||||
msgid "Btranslate"
|
||||
#: includes/class-admin.php:22
|
||||
msgid "HTBD"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:48
|
||||
#: includes/class-admin.php:48
|
||||
#, php-format
|
||||
msgid "(%1$s / %2$s content items)"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:49
|
||||
#: includes/class-admin.php:49
|
||||
#, php-format
|
||||
msgid ""
|
||||
"Latest task: %1$s. Posts and pages: %2$s / %3$s; categories and tags: %4$s / "
|
||||
"%5$s. Updates automatically every 5 seconds."
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:50
|
||||
#: includes/class-admin.php:50
|
||||
msgid "Unable to load translation progress at this time."
|
||||
msgstr ""
|
||||
|
||||
msgid "Failed translations"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:95
|
||||
#: includes/class-admin.php:95
|
||||
msgid "Source language"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:95
|
||||
#: includes/class-admin.php:95
|
||||
msgid "Use a Baidu Translate language code, for example zh."
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:96
|
||||
#: includes/class-admin.php:96
|
||||
msgid "Target languages"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:96
|
||||
#: includes/class-admin.php:96
|
||||
msgid "Separate multiple language codes with commas, for example en,ja."
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:97
|
||||
#: includes/class-admin.php:97
|
||||
msgid "URL mode"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:97
|
||||
#: includes/class-admin.php:97
|
||||
msgid "Subdirectory"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:97
|
||||
#: includes/class-admin.php:97
|
||||
msgid "Domain"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:98
|
||||
#: includes/class-admin.php:98
|
||||
msgid "Domain bindings"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:98
|
||||
#: includes/class-admin.php:98
|
||||
msgid ""
|
||||
"Enter one domain=language code pair per line, for example en.example.com=en. "
|
||||
"Do not include https://, a path, or a port."
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:99
|
||||
#: includes/class-admin.php:99
|
||||
msgid "Baidu application ID"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:100
|
||||
#: includes/class-admin.php:100
|
||||
msgid "Baidu secret key"
|
||||
msgstr ""
|
||||
|
||||
msgid "After saving settings for the first time, manually run \"Retranslate all content\" below."
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:101
|
||||
#: includes/class-admin.php:101
|
||||
msgid "Request logging"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:101
|
||||
#: includes/class-admin.php:101
|
||||
msgid "Log each Baidu Translate request"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:101
|
||||
#: includes/class-admin.php:101
|
||||
msgid ""
|
||||
"When enabled, fires the btranslate_translation_request_logged action with "
|
||||
"When enabled, fires the htbd_translation_request_logged action with "
|
||||
"the language, field, text fingerprint, length, and result status. "
|
||||
"Credentials, source text, translated text, and full API responses are never "
|
||||
"included."
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:106
|
||||
#: includes/class-admin.php:106
|
||||
msgid "Retranslate content"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:107
|
||||
#: includes/class-admin.php:107
|
||||
msgid ""
|
||||
"Queue all published posts, pages, categories, and tags for translation again."
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:108
|
||||
#: includes/class-admin.php:108
|
||||
msgid "This will retranslate all content and use your API quota. Continue?"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:111
|
||||
#: includes/class-admin.php:111
|
||||
msgid "Retranslate all content"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:113
|
||||
#: includes/class-admin.php:113
|
||||
msgid "You can also retranslate a single content type."
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:114
|
||||
#: includes/class-admin.php:114
|
||||
msgid ""
|
||||
"This will retranslate all posts and pages and use your API quota. Continue?"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:118
|
||||
#: includes/class-admin.php:118
|
||||
msgid "Translate all posts"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:120
|
||||
#: includes/class-admin.php:120
|
||||
msgid ""
|
||||
"This will retranslate all categories and tags and use your API quota. "
|
||||
"Continue?"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:124
|
||||
#: includes/class-admin.php:124
|
||||
msgid "Translate all categories and tags"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:127
|
||||
#: includes/class-admin.php:127
|
||||
msgid "Translation cache"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:128
|
||||
#: includes/class-admin.php:128
|
||||
msgid ""
|
||||
"After clearing the cache, the front end temporarily displays source content. "
|
||||
"Baidu Translate API calls resume when content is translated again."
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:129
|
||||
#: includes/class-admin.php:129
|
||||
msgid ""
|
||||
"This will delete all saved translations, cancel pending translation tasks, "
|
||||
"and reset progress. Continue?"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:132
|
||||
#: includes/class-admin.php:132
|
||||
msgid "Clear translation cache"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:135
|
||||
#: includes/class-admin.php:135
|
||||
msgid "Translation progress"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:137
|
||||
#: includes/class-admin.php:137
|
||||
msgid "Loading translation progress..."
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:145
|
||||
#: includes/class-btranslate-admin.php:211
|
||||
#: includes/class-admin.php:145
|
||||
#: includes/class-admin.php:211
|
||||
msgid "You do not have permission to perform this action."
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:151
|
||||
#: includes/class-admin.php:151
|
||||
msgid "Invalid translation scope."
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:218
|
||||
#: includes/class-admin.php:218
|
||||
msgid "Failed to clear the translation cache."
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:238
|
||||
#: includes/class-admin.php:238
|
||||
msgid "Settings"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:281
|
||||
#: includes/class-admin.php:281
|
||||
msgid "Translation status"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:296
|
||||
#: includes/class-admin.php:296
|
||||
msgid "Retranslate"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:296
|
||||
#: includes/class-admin.php:296
|
||||
msgid "Translate"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:311
|
||||
#: includes/class-admin.php:311
|
||||
msgid "Translated"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:311
|
||||
#: includes/class-admin.php:311
|
||||
msgid "Not translated"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:313
|
||||
#: includes/class-admin.php:313
|
||||
msgid "Last translated:"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:325
|
||||
#: includes/class-admin.php:325
|
||||
msgid "Invalid translation request."
|
||||
msgstr ""
|
||||
|
||||
@@ -222,30 +222,30 @@ msgstr ""
|
||||
msgid "Content #%d"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:351
|
||||
#: includes/class-admin.php:351
|
||||
msgid "Full translation"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:355
|
||||
#: includes/class-admin.php:355
|
||||
msgid "Single post translation"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:359
|
||||
#: includes/class-admin.php:359
|
||||
msgid "Posts and pages translation"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:363
|
||||
#: includes/class-admin.php:363
|
||||
msgid "Categories and tags translation"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-admin.php:366
|
||||
#: includes/class-admin.php:366
|
||||
msgid "No translation task"
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-baidu-provider.php:22
|
||||
#: includes/class-baidu-provider.php:22
|
||||
msgid "Baidu Translate credentials are not configured."
|
||||
msgstr ""
|
||||
|
||||
#: includes/class-btranslate-baidu-provider.php:52
|
||||
#: includes/class-baidu-provider.php:52
|
||||
msgid "Baidu Translate returned an invalid response."
|
||||
msgstr ""
|
||||
+9
-9
@@ -1,6 +1,6 @@
|
||||
=== Btranslate ===
|
||||
=== HTBD ===
|
||||
Contributors: hanyixuanten
|
||||
Homepage: https://github.com/hanyixuanten/btranslate
|
||||
Homepage: https://github.com/hanyixuanten/HTBD
|
||||
Requires at least: 6.4
|
||||
Tested up to: 7.0
|
||||
Requires PHP: 8.1
|
||||
@@ -12,31 +12,31 @@ License URI: https://www.gnu.org/licenses/gpl-3.0.html
|
||||
|
||||
== Description ==
|
||||
|
||||
Btranslate 使用百度翻译 API 翻译 WordPress 内容,并将每个翻译结果持久化保存以供重复使用。插件支持语言子目录 URL 和语言域名绑定。
|
||||
HTBD 使用百度翻译 API 翻译 WordPress 内容,并将每个翻译结果持久化保存以供重复使用。插件支持语言子目录 URL 和语言域名绑定。
|
||||
|
||||
WordPress 后台界面会跟随当前 WordPress 语言:默认显示英文,WordPress 设为 `zh_CN` 时使用插件内置的简体中文翻译。
|
||||
|
||||
支持的内容包括已发布文章和页面的标题、正文、摘要、图片替代文本、分类、标签及部分 SEO 字段。插件只为 `publish` 状态的文章和页面自动安排翻译。完成的翻译保存在插件自建数据表中;前端渲染只读取已保存的翻译,不会动态请求翻译服务。
|
||||
支持的内容包括已发布文章和页面的标题、正文、摘要、图片替代文本、分类、标签及部分 SEO 字段。插件只为 `publish` 状态的文章和页面自动安排翻译。完成的翻译保存在 `{$wpdb->prefix}hyx_bd_translations` 自建数据表中;启用改名后的插件时,已有 `{$wpdb->prefix}btranslate_translations` 数据会自动迁移。前端渲染只读取已保存的翻译,不会动态请求翻译服务。
|
||||
|
||||
子目录路由支持 `/en/example-post/` 等路径。域名路由可将配置的主机名映射到目标语言。翻译失败不会中断页面请求;没有有效译文时会回退显示源语言内容。
|
||||
|
||||
== Installation ==
|
||||
|
||||
1. 将 `btranslate` 目录上传到 `/wp-content/plugins/`。
|
||||
2. 在 WordPress 的“插件”页面启用 Btranslate。
|
||||
3. 打开“设置 > Btranslate”,配置百度翻译凭据、源语言、目标语言和路由模式。
|
||||
1. 将 `hyx-translator-for-baidu-translate` 目录上传到 `/wp-content/plugins/`。
|
||||
2. 在 WordPress 的“插件”页面启用 HTBD。
|
||||
3. 打开“设置 > HTBD”,配置百度翻译凭据、源语言、目标语言和路由模式。
|
||||
4. 第一次保存设置后,手动执行“重新翻译所有内容”,为已有内容安排翻译任务。
|
||||
5. 此后保存文章或页面时,插件会自动安排翻译任务。
|
||||
|
||||
== External Services ==
|
||||
|
||||
Btranslate 连接由百度提供的百度翻译开放平台 API,以翻译 WordPress 内容。没有该外部服务,插件无法生成新的翻译。站点管理员需要百度翻译账户、应用 ID 和密钥;百度可能根据管理员选择的服务方案实施请求配额或收取使用费用。
|
||||
HTBD 连接由百度提供的百度翻译开放平台 API,以翻译 WordPress 内容。没有该外部服务,插件无法生成新的翻译。站点管理员需要百度翻译账户、应用 ID 和密钥;百度可能根据管理员选择的服务方案实施请求配额或收取使用费用。
|
||||
|
||||
已发布文章或页面保存后、支持的分类或标签创建或编辑后,或者管理员明确安排或刷新翻译时,计划任务会请求该 API。前端页面访问只读取已保存的翻译,不会请求百度。已有有效持久化翻译时也不会请求百度,除非管理员明确执行重新翻译。
|
||||
|
||||
每次翻译请求会发送待翻译文本、配置的源语言和目标语言代码、百度应用 ID、随机盐值和请求签名。根据待翻译内容,文本可能包含已发布文章或页面的标题、正文、摘要、指定 SEO 标题和描述、附件图片替代文本,以及分类或标签的名称和描述。作为 HTTPS 请求的一部分,百度也能看到来源服务器 IP 等标准网络信息。密钥只在本地用于生成签名,不会作为独立请求字段发送。
|
||||
|
||||
Btranslate 将百度返回的译文保存在 WordPress 数据库中,但不保存完整百度响应。发送给百度的数据受百度自身条款和隐私规则约束:
|
||||
HTBD 将百度返回的译文保存在 WordPress 数据库中,但不保存完整百度响应。发送给百度的数据受百度自身条款和隐私规则约束:
|
||||
|
||||
* 百度翻译 API 文档:https://fanyi-api.baidu.com/doc/23
|
||||
* 百度翻译开放平台服务协议:https://fanyi-api.baidu.com/doc/6
|
||||
|
||||
+9
-9
@@ -1,6 +1,6 @@
|
||||
=== Btranslate ===
|
||||
=== HTBD ===
|
||||
Contributors: hanyixuanten
|
||||
Homepage: https://github.com/hanyixuanten/btranslate
|
||||
Homepage: https://github.com/hanyixuanten/HTBD
|
||||
Requires at least: 6.4
|
||||
Tested up to: 7.0
|
||||
Requires PHP: 8.1
|
||||
@@ -12,30 +12,30 @@ Persistent multilingual WordPress translations powered by the Baidu Translate AP
|
||||
|
||||
== Description ==
|
||||
|
||||
Btranslate translates supported WordPress content with the Baidu Translate API and persists each translation for reuse. It supports language-specific subdirectory URLs and domain bindings.
|
||||
HTBD translates supported WordPress content with the Baidu Translate API and persists each translation for reuse. It supports language-specific subdirectory URLs and domain bindings.
|
||||
|
||||
The WordPress admin interface follows the current WordPress locale. English is the default, and a bundled Simplified Chinese translation is used when WordPress is set to `zh_CN`.
|
||||
|
||||
Supported values include published post and page titles, content, excerpts, image alternative text, categories, tags, and selected SEO fields. Automatic translation is only scheduled for posts and pages with the `publish` status. Completed translations are stored in the plugin's custom database table. Front-end rendering reuses stored values and never calls the translation provider dynamically.
|
||||
Supported values include published post and page titles, content, excerpts, image alternative text, categories, tags, and selected SEO fields. Automatic translation is only scheduled for posts and pages with the `publish` status. Completed translations are stored in the `{$wpdb->prefix}hyx_bd_translations` custom database table. Existing `{$wpdb->prefix}btranslate_translations` data is migrated when the renamed plugin is activated. Front-end rendering reuses stored values and never calls the translation provider dynamically.
|
||||
|
||||
Subdirectory routing supports paths such as `/en/example-post/`. Domain routing maps configured hostnames to target languages. Translation failures are non-fatal and fall back to source-language content.
|
||||
|
||||
== Installation ==
|
||||
|
||||
1. Upload the `btranslate` directory to `/wp-content/plugins/`.
|
||||
2. Activate Btranslate through the Plugins screen in WordPress.
|
||||
3. Open Settings > Btranslate and configure the Baidu credentials, languages, and routing mode.
|
||||
1. Upload the `hyx-translator-for-baidu-translate` directory to `/wp-content/plugins/`.
|
||||
2. Activate HTBD through the Plugins screen in WordPress.
|
||||
3. Open Settings > HTBD and configure the Baidu credentials, languages, and routing mode.
|
||||
4. After saving settings for the first time, manually run Retranslate all content to queue existing content.
|
||||
|
||||
== External Services ==
|
||||
|
||||
Btranslate connects to the Baidu Translate Open Platform API, a service provided by Baidu, to translate WordPress content. The plugin cannot generate new translations without this service. A Baidu Translate account, application ID, and secret key are required. Baidu may apply request quotas or usage charges according to the service plan selected by the site administrator.
|
||||
HTBD connects to the Baidu Translate Open Platform API, a service provided by Baidu, to translate WordPress content. The plugin cannot generate new translations without this service. A Baidu Translate account, application ID, and secret key are required. Baidu may apply request quotas or usage charges according to the service plan selected by the site administrator.
|
||||
|
||||
The API is contacted by scheduled tasks after a published post or page is saved, after a supported category or tag is created or edited, or after an administrator explicitly queues or refreshes translations. Front-end page views reuse stored translations and do not contact Baidu. The plugin also makes no Baidu request when valid persisted translations are already available, unless an administrator explicitly requests a refresh.
|
||||
|
||||
For each translation request, the plugin sends the text being translated, the configured source and target language codes, the Baidu application ID, a random salt, and a request signature. Depending on the content being translated, the text may contain published post or page titles, body text, excerpts, selected SEO title and description values, attached image alternative text, and category or tag names and descriptions. Standard network information, including the originating server IP address, is also visible to Baidu as part of the HTTPS request. The secret key is used locally to create the signature and is not sent as a separate request field.
|
||||
|
||||
Btranslate stores the returned translated text in the WordPress database but does not store complete Baidu response payloads. Data sent to Baidu is subject to Baidu's own terms and privacy practices:
|
||||
HTBD stores the returned translated text in the WordPress database but does not store complete Baidu response payloads. Data sent to Baidu is subject to Baidu's own terms and privacy practices:
|
||||
|
||||
* Baidu Translate API documentation: https://fanyi-api.baidu.com/doc/23
|
||||
* Baidu Translate Open Platform service agreement: https://fanyi-api.baidu.com/doc/6
|
||||
|
||||
+4
-4
@@ -1,14 +1,14 @@
|
||||
<?php
|
||||
/**
|
||||
* Removes all data created by Btranslate.
|
||||
* Removes all data created by HTBD.
|
||||
*
|
||||
* @package BTRANSLATE
|
||||
* @package HTBD
|
||||
*/
|
||||
|
||||
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
require_once __DIR__ . '/includes/class-btranslate-uninstaller.php';
|
||||
require_once __DIR__ . '/includes/class-uninstaller.php';
|
||||
|
||||
BTRANSLATE_Uninstaller::uninstall();
|
||||
HTBD_Uninstaller::uninstall();
|
||||
Reference in New Issue
Block a user