Plugin Directory

Changeset 3135322


Ignore:
Timestamp:
08/14/2024 06:23:04 AM (20 months ago)
Author:
jamesdlow
Message:

1.0.6

  • Add helper function for programmatic translation in PHP
Location:
ai-translate-for-polylang
Files:
8 added
2 edited

Legend:

Unmodified
Added
Removed
  • ai-translate-for-polylang/trunk/ai-translate-polylang.php

    r3135283 r3135322  
    44Plugin URI: https://wordpress.org/plugins/ai-translate-polylang/
    55Description: Add auto AI translation caperbility to Polylang
    6 Version: 1.0.5
     6Version: 1.0.6
    77Author: James Low
    88Author URI: http://jameslow.com
     
    5454        add_filter('default_content', array(static::class, 'default_content'), 10, 2);
    5555        //add_filter('pll_copy_post_metas', array(static::class, 'pll_copy_post_metas'), 11, 5); //This gets called, but doesn't clear out keys
    56         add_filter('pll_translate_post_meta', array(static::class, 'pll_translate_post_meta'), 10, 5);
     56        add_filter('pll_translate_post_meta', array(static::class, 'pll_translate_post_meta'), 10, 3);
    5757    }
    5858    public static function init() {
     
    9999        return $keys;
    100100    }
    101     public static function pll_translate_post_meta($value, $key, $lang, $from, $to) {
     101    public static function pll_translate_post_meta($value, $key, $lang) {
    102102        if (in_array($key, self::meta_clear())) {
    103103            $value = '';
    104104        } else if (in_array($key, self::meta_translate())) {
    105             $value = self::translate_field($value);
     105            $value = self::translate_field($value, $key, true);
    106106        }
    107107        return $value;
     
    133133        if (get_option('ai_translate_new_post', '0') == '1' && $_GET['new_lang'] && isset($_GET['from_post'])) {
    134134            if (!$original || $original != '') {
    135                 $code = sanitize_key($_GET['new_lang']);
     135                $to = sanitize_key($_GET['new_lang']);
    136136                $post_id = sanitize_key($_GET['from_post']);
    137137                if ($field) {
     
    144144                }
    145145            }
    146             $translation = self::translate($original, $code, pll_get_post_language($post_id));
     146            $translation = self::translate($original, $to, pll_get_post_language($post_id));
    147147        } else {
    148148            $translation = $original;
     
    154154    }
    155155    public static function translate($text, $to, $from = 'en') {
    156         $prompt = self::prompt($to, $from);
    157         if (get_option('ai_translate_llm', 'OpenAI') == 'Claude') {
    158             $result = self::claude_message($text, $prompt);
    159             $body = json_decode($result['body'], true);
    160             if ($result['response']['code'] == 200) {
    161                 return $body['content'][0]['text'];
     156        if ($text && trim($text) != '') {
     157            $prompt = self::prompt($to, $from);
     158            if (get_option('ai_translate_llm', 'OpenAI') == 'Claude') {
     159                $result = self::claude_message($text, $prompt);
     160                $body = json_decode($result['body'], true);
     161                if ($result['response']['code'] == 200) {
     162                    return $body['content'][0]['text'];
     163                } else {
     164                    return 'ERROR: '.$body['error']['message'];
     165                }
    162166            } else {
    163                 return 'ERROR: '.$body['error']['message'];
    164             }
    165         } else {
    166             $result = self::openai_api($text, $prompt);
    167             if (!isset($result['error'])) {
    168                 $translation = $result['choices'][0]['message']['content'];
    169             } else {
    170                 $translation = 'ERROR: '.$result['error']['message'];
    171             }
    172             return $translation;
     167                $result = self::openai_api($text, $prompt);
     168                if (!isset($result['error'])) {
     169                    $translation = $result['choices'][0]['message']['content'];
     170                } else {
     171                    $translation = 'ERROR: '.$result['error']['message'];
     172                }
     173                return $translation;
     174            }
     175        } else {
     176            return '';
     177        }
     178    }
     179    public static function translate_post($post_id, $to, $status = 'publish') {
     180        $from = pll_get_post_language($post_id);
     181        $translation = pll_get_post($post_id, $to);
     182        if (!$translation || get_post_status($translation) !== false) {
     183            $post = get_post($post_id);
     184
     185            // Create a new post with the same content
     186            $new_post_id = wp_insert_post([
     187                'post_title'   => self::translate($post->post_title, $to, $from),
     188                'post_content' => self::translate($post->post_content, $to, $from),
     189                'post_excerpt' => self::translate($post->post_excerpt, $to, $from),
     190                'post_status'  => $status,
     191                'post_type'    => $post->post_type
     192            ]);
     193
     194            // Set the language for the new post
     195            pll_set_post_language($new_post_id, $to);
     196
     197            // Duplicate post meta
     198            $meta = get_post_meta($post_id);
     199            foreach ($meta as $key => $values) {
     200                foreach ($values as $value) {
     201                    $value = maybe_unserialize($value);
     202                    if (in_array($key, self::meta_clear())) {
     203                        $value = '';
     204                    } else if (in_array($key, self::meta_translate())) {
     205                        $value = self::translate($value, $to, $from);
     206                    }
     207                    add_post_meta($new_post_id, $key, $value);
     208                }
     209            }
     210
     211            //TODO: Should we use PLL_Sync_Tax->copy($from,$to, $lang)
     212            // Get the translated term IDs for categories
     213            $categories = get_the_category($post_id);
     214            $translated_category_ids = [];
     215            foreach ($categories as $category) {
     216                $translated_category_id = pll_get_term($category->term_id, $to);
     217                if ($translated_category_id) {
     218                    $translated_category_ids[] = $translated_category_id;
     219                }
     220            }
     221            wp_set_post_categories($new_post_id, $translated_category_ids);
     222
     223            // Get the translated term IDs for tags
     224            $tags = wp_get_post_tags($post_id);
     225            $translated_tag_ids = [];
     226            foreach ($tags as $tag) {
     227                $translated_tag_id = pll_get_term($tag->term_id, $to);
     228                if ($translated_tag_id) {
     229                    $translated_tag_ids[] = $translated_tag_id;
     230                }
     231            }
     232            wp_set_post_tags($new_post_id, $translated_tag_ids);
     233
     234            // Link the new translation with the original post
     235            pll_save_post_translations([
     236                $to => $new_post_id,
     237                pll_get_post_language($post_id) => $post_id,
     238            ]);
     239            return $new_post_id;
     240        } else {
     241            return null;
    173242        }
    174243    }
  • ai-translate-for-polylang/trunk/readme.txt

    r3135283 r3135322  
    44Tags: language, translate, multilingual, translation, polylang
    55Requires at least: 3.0
    6 Tested up to: 6.5.4
    7 Stable tag: 1.0.5
     6Tested up to: 6.6.1
     7Stable tag: 1.0.6
    88License: MIT
    99License URI: https://opensource.org/licenses/MIT
     
    3434== Changelog ==
    3535
     36= 1.0.6 =
     37* Add helper function for programmatic translation in PHP
     38
    3639= 1.0.5 =
    3740* Edit prompt to make sure only the new content is returned
Note: See TracChangeset for help on using the changeset viewer.