162 lines
4.7 KiB
PHP
162 lines
4.7 KiB
PHP
<?php
|
|
/**
|
|
* Plugin Name: AI Translator (by ROBOTSTXT)
|
|
* Plugin URI: https://www.robotstxt.software/plugins/robotstxt-ai-translator/
|
|
* Update URI: https://www.robotstxt.software/plugins/robotstxt-ai-translator/
|
|
* Description: Translate post titles and content from the editor using the native WordPress AI client (available in WP 7.0+). Multisite-ready with global or per-site configuration.
|
|
* Version: 1.2.3
|
|
* Author: ROBOTSTXT
|
|
* Author URI: https://www.robotstxt.software/
|
|
* License: GPL-3.0-or-later
|
|
* License URI: https://www.gnu.org/licenses/gpl-3.0.txt
|
|
* Text Domain: robotstxt-ai-translator
|
|
* Domain Path: /languages
|
|
* Requires at least: 7.0
|
|
* Requires PHP: 5.6
|
|
* Network: true
|
|
*
|
|
* @package ROBOTSTXT\AI_Translator
|
|
*/
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Plugin version.
|
|
*/
|
|
define( 'AI_TRANSLATOR_VERSION', '1.2.3' );
|
|
|
|
/**
|
|
* Main plugin file path.
|
|
*/
|
|
define( 'AI_TRANSLATOR_FILE', __FILE__ );
|
|
|
|
/**
|
|
* Plugin directory path (with trailing slash).
|
|
*/
|
|
define( 'AI_TRANSLATOR_DIR', plugin_dir_path( __FILE__ ) );
|
|
|
|
/**
|
|
* Plugin URL (with trailing slash).
|
|
*/
|
|
define( 'AI_TRANSLATOR_URL', plugin_dir_url( __FILE__ ) );
|
|
|
|
/**
|
|
* Plugin basename (e.g. robotstxt-ai-translator/robotstxt-ai-translator.php).
|
|
*/
|
|
define( 'AI_TRANSLATOR_BASENAME', plugin_basename( __FILE__ ) );
|
|
|
|
/**
|
|
* REST API namespace.
|
|
*/
|
|
define( 'AI_TRANSLATOR_REST_NAMESPACE', 'ai-translator/v1' );
|
|
|
|
require_once AI_TRANSLATOR_DIR . 'includes/class-ai-translator-settings.php';
|
|
require_once AI_TRANSLATOR_DIR . 'includes/class-ai-translator-translator.php';
|
|
require_once AI_TRANSLATOR_DIR . 'includes/class-ai-translator-admin.php';
|
|
require_once AI_TRANSLATOR_DIR . 'includes/class-ai-translator-editor-ui.php';
|
|
require_once AI_TRANSLATOR_DIR . 'includes/class-ai-translator-manager-notice.php';
|
|
|
|
if ( class_exists( '\Inpsyde\MultilingualPress\TranslationUi\Post\MetaboxAction' ) ) {
|
|
require_once AI_TRANSLATOR_DIR . 'includes/class-ai-translator-mlp-integration.php';
|
|
}
|
|
|
|
if ( defined( 'WP_CLI' ) && WP_CLI ) {
|
|
require_once AI_TRANSLATOR_DIR . 'includes/class-ai-translator-cli.php';
|
|
}
|
|
|
|
/**
|
|
* Loads the plugin text domain.
|
|
*
|
|
* Hooked on `init` per WordPress 6.7+ guidance (loading earlier triggers a
|
|
* `_load_textdomain_just_in_time` deprecation notice).
|
|
*
|
|
* @since 1.0.0
|
|
*
|
|
* @return void
|
|
*/
|
|
function ai_translator_load_textdomain() {
|
|
load_plugin_textdomain(
|
|
'robotstxt-ai-translator',
|
|
false,
|
|
dirname( AI_TRANSLATOR_BASENAME ) . '/languages'
|
|
);
|
|
}
|
|
add_action( 'init', 'ai_translator_load_textdomain' );
|
|
|
|
/**
|
|
* Bootstraps the plugin once all plugins are loaded.
|
|
*
|
|
* @since 1.0.0
|
|
*
|
|
* @return void
|
|
*/
|
|
function ai_translator_bootstrap() {
|
|
$settings = new AI_Translator_Settings();
|
|
$translator = new AI_Translator_Translator( $settings );
|
|
$manager_notice = new AI_Translator_Manager_Notice();
|
|
$admin = new AI_Translator_Admin( $settings, $manager_notice );
|
|
$editor_ui = new AI_Translator_Editor_UI( $settings, $translator );
|
|
|
|
$admin->register();
|
|
$editor_ui->register();
|
|
|
|
if ( defined( 'WP_CLI' ) && WP_CLI ) {
|
|
$cli = new AI_Translator_CLI( $settings, $translator );
|
|
$cli->register();
|
|
}
|
|
|
|
if ( class_exists( 'AI_Translator_MLP_Integration' ) ) {
|
|
$mlp = new AI_Translator_MLP_Integration( $settings, $translator );
|
|
$mlp->register();
|
|
}
|
|
|
|
$manager_notice->register();
|
|
}
|
|
add_action( 'plugins_loaded', 'ai_translator_bootstrap' );
|
|
|
|
/**
|
|
* Renders an admin notice when no AI provider is configured for text generation.
|
|
*
|
|
* `wp_ai_client_prompt()` is part of WordPress 7.0 core, so the function always
|
|
* exists. The meaningful check is whether a provider capable of text generation has
|
|
* been configured — which is what `AI_Translator_Translator::is_supported()` tests.
|
|
*
|
|
* @since 1.0.0
|
|
*
|
|
* @return void
|
|
*/
|
|
function ai_translator_dependency_notice() {
|
|
if ( ! current_user_can( 'manage_options' ) ) {
|
|
return;
|
|
}
|
|
|
|
$translator = new AI_Translator_Translator( new AI_Translator_Settings() );
|
|
|
|
if ( $translator->is_supported() ) {
|
|
return;
|
|
}
|
|
|
|
$settings_url = admin_url( 'options-general.php?page=options-connectors-wp-admin' );
|
|
|
|
$message = sprintf(
|
|
/* translators: %s: Link to the WordPress AI settings page. */
|
|
esc_html__( 'AI Translator (by ROBOTSTXT) requires a configured AI provider for text generation. Open %s to set one up.', 'robotstxt-ai-translator' ),
|
|
'<a href="' . esc_url( $settings_url ) . '">' . esc_html__( 'Settings → AI', 'robotstxt-ai-translator' ) . '</a>'
|
|
);
|
|
|
|
printf(
|
|
'<div class="notice notice-warning"><p>%s</p></div>',
|
|
wp_kses(
|
|
$message,
|
|
array(
|
|
'a' => array(
|
|
'href' => array(),
|
|
),
|
|
)
|
|
)
|
|
);
|
|
}
|
|
add_action( 'admin_notices', 'ai_translator_dependency_notice' );
|
|
add_action( 'network_admin_notices', 'ai_translator_dependency_notice' );
|