100 ] );
$keys = [
'_aimstr_translation_group',
'_aimstr_is_source',
'_aimstr_source_site',
'_aimstr_source_post',
];
foreach ( $sites as $site ) {
switch_to_blog( $site->blog_id );
$posts = get_posts([
'post_type' => 'any',
'post_status' => 'any',
'posts_per_page' => -1,
'fields' => 'ids',
'meta_query' => [
[
'key' => '_aimstr_translation_group',
'compare' => 'EXISTS',
],
],
]);
foreach ( $posts as $post_id ) {
foreach ( $keys as $key ) {
delete_post_meta( $post_id, $key );
}
}
restore_current_blog();
}
}
/**
* Register the Network Admin menu page.
*/
public function register_network_menu() {
add_menu_page(
__( 'AI Multisite Translate', 'ai-multisite-translate' ),
__( 'AI Translate', 'ai-multisite-translate' ),
'manage_network_options',
'aimstr-network',
[ $this, 'render_network_page' ],
'dashicons-translation',
30
);
}
/**
* Render the network admin page.
*/
public function render_network_page() {
$sites = get_sites();
?>
blog_id, 'WPLANG', '' ) ?: 'en_US';
}, $sites )
);
echo count( $unique_langs );
?>
|
|
|
|
|
blog_id;
$lang_code = get_blog_option( $blog_id, 'WPLANG', '' ) ?: 'en_US';
$details = get_blog_details( $blog_id );
// translators: %d is the numeric site ID.
$site_name = $details ? esc_html( $details->blogname ) : sprintf( __( 'Site %d', 'ai-multisite-translate' ), $blog_id );
$site_url = get_site_url( $blog_id );
$settings_url = get_admin_url( $blog_id, 'options-general.php' );
$is_main = is_main_site( $blog_id );
?>
|
|
|
|
|
|
render_model_settings(); ?>
render_translate_site_section( $sites ); ?>
render_bulk_mapping_section( $sites ); ?>
[ 'post', 'page' ],
'post_status' => 'any',
'posts_per_page' => -1,
]);
restore_current_blog();
switch_to_blog( $target_id );
$target_posts = get_posts([
'post_type' => [ 'post', 'page' ],
'post_status' => 'any',
'posts_per_page' => -1,
]);
restore_current_blog();
$target_by_slug = [];
$target_list = [];
foreach ( $target_posts as $post ) {
$target_by_slug[ $post->post_name ] = $post->ID;
$target_list[] = [
'id' => $post->ID,
'title' => $post->post_title,
'slug' => $post->post_name,
];
}
if ( empty( $source_posts ) ) {
wp_send_json_success([
'matches' => [],
'target_list' => $target_list,
'message' => __( 'No posts found on the source site.', 'ai-multisite-translate' ),
]);
return;
}
$matches = [];
foreach ( $source_posts as $post ) {
switch_to_blog( $source_id );
$existing_group = get_post_meta( $post->ID, '_aimstr_translation_group', true );
restore_current_blog();
$match_id = 0;
$confidence = 'none';
if ( $existing_group ) {
switch_to_blog( $target_id );
$mapped = get_posts([
'post_type' => 'any',
'post_status' => 'any',
'meta_key' => '_aimstr_translation_group',
'meta_value' => $existing_group,
'numberposts' => 1,
'fields' => 'ids',
]);
restore_current_blog();
if ( $mapped ) {
$match_id = $mapped[0];
$confidence = 'high';
} else {
switch_to_blog( $target_id );
$reverse = get_posts([
'post_type' => 'any',
'post_status' => 'any',
'meta_query' => [
[
'key' => '_aimstr_source_post',
'value' => $post->ID,
],
[
'key' => '_aimstr_source_site',
'value' => $source_id,
],
],
'numberposts' => 1,
'fields' => 'ids',
]);
restore_current_blog();
if ( $reverse ) {
$match_id = $reverse[0];
$confidence = 'high';
}
}
}
if ( ! $match_id && isset( $target_by_slug[ $post->post_name ] ) ) {
$candidate = $target_by_slug[ $post->post_name ];
switch_to_blog( $target_id );
$target_group = get_post_meta( $candidate, '_aimstr_translation_group', true );
restore_current_blog();
$match_id = $candidate;
$confidence = $target_group ? 'conflict' : 'high';
}
$matches[] = [
'source_id' => $post->ID,
'source_title' => $post->post_title,
'source_slug' => $post->post_name,
'match_id' => $match_id,
'confidence' => $confidence,
'already_mapped' => ! empty( $existing_group ),
];
}
wp_send_json_success([
'matches' => $matches,
'target_list' => $target_list,
]);
}
/**
* Get or create a translation group UUID for a post.
*/
private function get_or_create_group_id( int $site_id, int $post_id ): string {
switch_to_blog( $site_id );
$group_id = get_post_meta( $post_id, '_aimstr_translation_group', true );
restore_current_blog();
return $group_id ?: wp_generate_uuid4();
}
/**
* AJAX handler to save bulk mappings.
*/
public function ajax_save_bulk_mapping() {
check_ajax_referer( 'aimstr_generate', 'nonce' );
if ( ! current_user_can( 'manage_network' ) ) {
wp_send_json_error( 'Insufficient permissions.' );
}
$source_id = isset( $_POST['source_id'] ) ? (int) $_POST['source_id'] : 0;
$target_id = isset( $_POST['target_id'] ) ? (int) $_POST['target_id'] : 0;
$raw_mappings = isset( $_POST['mappings'] ) ? wp_unslash( $_POST['mappings'] ) : []; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$mappings = array_map( function( $mapping ) {
return [
'source_id' => isset( $mapping['source_id'] ) ? absint( $mapping['source_id'] ) : 0,
'target_id' => isset( $mapping['target_id'] ) ? absint( $mapping['target_id'] ) : 0,
];
}, (array) $raw_mappings );
if ( ! $source_id || ! $target_id ) {
wp_send_json_error( 'Invalid site IDs.' );
}
$saved = 0;
foreach ( $mappings as $mapping ) {
$source_post_id = $mapping['source_id'];
$target_post_id = $mapping['target_id'];
if ( ! $source_post_id || ! $target_post_id ) {
continue;
}
switch_to_blog( $source_id );
$group_id = get_post_meta( $source_post_id, '_aimstr_translation_group', true );
restore_current_blog();
if ( ! $group_id ) {
switch_to_blog( $target_id );
$group_id = get_post_meta( $target_post_id, '_aimstr_translation_group', true );
restore_current_blog();
}
if ( ! $group_id ) {
$group_id = $this->get_or_create_group_id( $source_id, $source_post_id );
}
switch_to_blog( $source_id );
update_post_meta( $source_post_id, '_aimstr_translation_group', $group_id );
update_post_meta( $source_post_id, '_aimstr_source_site', $source_id );
update_post_meta( $source_post_id, '_aimstr_source_post', $source_post_id );
restore_current_blog();
switch_to_blog( $target_id );
update_post_meta( $target_post_id, '_aimstr_translation_group', $group_id );
update_post_meta( $target_post_id, '_aimstr_source_site', $source_id );
update_post_meta( $target_post_id, '_aimstr_source_post', $source_post_id );
restore_current_blog();
$saved++;
}
wp_send_json_success([
'saved' => $saved,
'message' => sprintf(
/* translators: %d number of mappings saved */
__( '%d mapping(s) saved successfully.', 'ai-multisite-translate' ),
$saved
),
]);
}
/**
* Render the translate entire site section.
*/
private function render_translate_site_section( $sites ) {
?>
[
'label' => 'Anthropic',
'models' => [
'claude-haiku-4-5-20251001' => 'Claude Haiku 4.5 (Fastest, cheapest)',
'claude-sonnet-4-6' => 'Claude Sonnet 4.6 (Balanced)',
'claude-opus-4-6' => 'Claude Opus 4.6 (Most capable, expensive)',
],
],
'google' => [
'label' => 'Google',
'models' => [
'gemini-2.0-flash' => 'Gemini 2.0 Flash (Fast, cheap)',
'gemini-2.5-pro' => 'Gemini 2.5 Pro (Most capable)',
],
],
'openai' => [
'label' => 'OpenAI',
'models' => [
'gpt-4o-mini' => 'GPT-4o Mini (Fast, cheap)',
'gpt-4o' => 'GPT-4o (Balanced)',
],
],
];
?>
admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('aimstr_generate'),
]);
wp_add_inline_script('aimstr-admin', '
jQuery(function($) {
$("#aimstr-provider").on("change", function() {
var provider = $(this).val();
var $model = $("#aimstr-model");
$model.find("option").each(function() {
var $opt = $(this);
if ( ! $opt.val() ) return;
$opt.toggle( $opt.data("provider") === provider );
});
$model.val("");
});
});
');
}
}
new AIMSTR_Admin();