Plugin Directory

Changeset 3313568


Ignore:
Timestamp:
06/18/2025 03:21:45 AM (10 months ago)
Author:
jamesdlow
Message:

1.1.2

  • Change AI model to text string, so you can choose your own model
  • Fix internal translate function for scripting
  • Add option to overwrite on internal translate function
Location:
ai-translate-for-polylang
Files:
8 added
2 edited

Legend:

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

    r3292957 r3313568  
    44Plugin URI: https://wordpress.org/plugins/ai-translate-polylang/
    55Description: Add auto AI translation caperbility to Polylang
    6 Version: 1.1.1
     6Version: 1.1.2
    77Author: James Low
    88Author URI: http://jameslow.com
     
    7070            array('id'=>'ai_translate_openai_key', 'type'=>'string', 'title'=>'OpenAI API Key', 'description'=>''),
    7171            array('id'=>'ai_translate_openai_org', 'type'=>'string', 'title'=>'OpenAI Organization', 'description'=>'(Optional)'),
    72             array('id'=>'ai_translate_openai_model', 'type'=>'select', 'title'=>'OpenAI Model', 'default'=>self::$OPENAI_MODEL, 'values'=>array(
     72            array('id'=>'ai_translate_openai_model', 'type'=>'string', 'title'=>'OpenAI Model', 'default'=>self::$OPENAI_MODEL, 'values'=>array(
    7373                'gpt-4o',
    7474                'gpt-4o-mini',
     
    7979            array('id'=>'ai_translate_claude', 'type'=>'title', 'title'=>'Claude', 'description'=>''),
    8080            array('id'=>'ai_translate_claude_key', 'type'=>'string', 'title'=>'Claude API Key', 'description'=>''),
    81             array('id'=>'ai_translate_claude_model', 'type'=>'select', 'title'=>'OpenAI Model', 'default'=>self::$CLAUDE_MODEL, 'values'=>array(
     81            array('id'=>'ai_translate_claude_model', 'type'=>'string', 'title'=>'OpenAI Model', 'default'=>self::$CLAUDE_MODEL, 'values'=>array(
    8282                'claude-3-5-sonnet-20240620',
    8383                'claude-3-opus-20240229',
     
    8787            array('id'=>'ai_translate_gemini', 'type'=>'title', 'title'=>'Gemini', 'description'=>''),
    8888            array('id'=>'ai_translate_gemini_key', 'type'=>'string', 'title'=>'Gemini API Key', 'description'=>''),
    89             array('id'=>'ai_translate_gemini_model', 'type'=>'select', 'title'=>'Gemini Model', 'default'=>self::$GEMINI_MODEL, 'values'=>array(
     89            array('id'=>'ai_translate_gemini_model', 'type'=>'string', 'title'=>'Gemini Model', 'default'=>self::$GEMINI_MODEL, 'values'=>array(
    9090                'gemini-2.0-flash',
    9191                'gemini-1.5-flash',
     
    202202        return $data;
    203203    }
    204     public static function translate_post($post_id, $to, $status = 'publish') {
     204    public static function translate_post($post_id, $to, $status = 'publish', $overwrite = false) {
    205205        $from = pll_get_post_language($post_id);
    206206        $translation = pll_get_post($post_id, $to);
    207         if (!$translation || get_post_status($translation) !== false) {
     207        if (!$translation || get_post_status($translation) === false || $overwrite) {
    208208            $post = get_post($post_id);
    209209           
    210210            self::$author = $post->post_author;
    211211            add_filter('wp_insert_post_data', array(static::class, 'wp_insert_post_data'), '99', 2);
    212             // Create a new post with the same content
    213             $new_post_id = wp_insert_post([
    214                 'post_status'       => 'draft',
    215                 'post_type'         => $post->post_type,
    216                 'post_author'       => $post->post_author, //Wordpress overrides author to 0, hence hook
    217                 'post_date'         => $post->post_date,
    218                 'post_date_gmt'     => $post->post_date_gmt,
    219                 'post_modified'     => $post->post_modified,
    220                 'post_modified_gmt' => $post->post_modified_gmt
    221             ]);
    222 
    223             // Set the language for the new post first in case something goes wrong
    224             pll_set_post_language($new_post_id, $to);
     212            if (!$translation) {
     213                // Create a new post with the same content
     214                $translation = wp_insert_post([
     215                    'post_status'       => 'draft',
     216                    'post_title'        => $post->post_title." ($to)",
     217                    'post_content'      => ' ',
     218                    'post_type'         => $post->post_type,
     219                    'post_author'       => $post->post_author, //Wordpress overrides author to 0, hence hook
     220                    'post_date'         => $post->post_date,
     221                    'post_date_gmt'     => $post->post_date_gmt,
     222                    'post_modified'     => $post->post_modified,
     223                    'post_modified_gmt' => $post->post_modified_gmt
     224                ]);
     225                // Set the language for the new post first in case something goes wrong
     226                pll_set_post_language($translation, $to);
     227            }
    225228
    226229            wp_update_post(array(
    227                 'ID'                => $new_post_id,
     230                'ID'                => $translation,
    228231                'post_title'        => self::translate($post->post_title, $to, $from),
    229232                'post_content'      => self::translate($post->post_content, $to, $from),
     
    242245                        $value = self::translate($value, $to, $from);
    243246                    }
    244                     add_post_meta($new_post_id, $key, $value);
     247                    add_post_meta($translation, $key, $value);
    245248                }
    246249            }
     
    256259                }
    257260            }
    258             wp_set_post_categories($new_post_id, $translated_category_ids);
     261            wp_set_post_categories($translation, $translated_category_ids);
    259262
    260263            // Get the translated term IDs for tags
     
    267270                }
    268271            }
    269             wp_set_post_tags($new_post_id, $translated_tag_ids);
     272            wp_set_post_tags($translation, $translated_tag_ids);
    270273
    271274            // Link the new translation with the original post
    272275            pll_save_post_translations([
    273                 $to => $new_post_id,
     276                $to => $translation,
    274277                pll_get_post_language($post_id) => $post_id,
    275278            ]);
    276             return $new_post_id;
    277         } else {
    278             return null;
    279         }
     279        }
     280        return $translation;
    280281    }
    281282
  • ai-translate-for-polylang/trunk/readme.txt

    r3292957 r3313568  
    44Tags: language, translate, multilingual, translation, polylang
    55Requires at least: 3.0
    6 Tested up to: 6.6.1
    7 Stable tag: 1.1.1
     6Tested up to: 6.8.1
     7Stable tag: trunk
    88License: MIT
    99License URI: https://opensource.org/licenses/MIT
     
    3333
    3434== Changelog ==
     35
     36= 1.1.2 =
     37* Change AI model to text string, so you can choose your own model
     38* Fix internal translate function for scripting
     39* Add option to overwrite on internal translate function
    3540
    3641= 1.1.1 =
Note: See TracChangeset for help on using the changeset viewer.