Changeset 3135322
- Timestamp:
- 08/14/2024 06:23:04 AM (20 months ago)
- Location:
- ai-translate-for-polylang
- Files:
-
- 8 added
- 2 edited
-
tags/1.0.6 (added)
-
tags/1.0.6/ai-translate-polylang.php (added)
-
tags/1.0.6/inc (added)
-
tags/1.0.6/inc/open-ai (added)
-
tags/1.0.6/inc/open-ai/OpenAi.php (added)
-
tags/1.0.6/inc/open-ai/Url.php (added)
-
tags/1.0.6/inc/settingslib.php (added)
-
tags/1.0.6/readme.txt (added)
-
trunk/ai-translate-polylang.php (modified) (6 diffs)
-
trunk/readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
ai-translate-for-polylang/trunk/ai-translate-polylang.php
r3135283 r3135322 4 4 Plugin URI: https://wordpress.org/plugins/ai-translate-polylang/ 5 5 Description: Add auto AI translation caperbility to Polylang 6 Version: 1.0. 56 Version: 1.0.6 7 7 Author: James Low 8 8 Author URI: http://jameslow.com … … 54 54 add_filter('default_content', array(static::class, 'default_content'), 10, 2); 55 55 //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); 57 57 } 58 58 public static function init() { … … 99 99 return $keys; 100 100 } 101 public static function pll_translate_post_meta($value, $key, $lang , $from, $to) {101 public static function pll_translate_post_meta($value, $key, $lang) { 102 102 if (in_array($key, self::meta_clear())) { 103 103 $value = ''; 104 104 } else if (in_array($key, self::meta_translate())) { 105 $value = self::translate_field($value );105 $value = self::translate_field($value, $key, true); 106 106 } 107 107 return $value; … … 133 133 if (get_option('ai_translate_new_post', '0') == '1' && $_GET['new_lang'] && isset($_GET['from_post'])) { 134 134 if (!$original || $original != '') { 135 $ code= sanitize_key($_GET['new_lang']);135 $to = sanitize_key($_GET['new_lang']); 136 136 $post_id = sanitize_key($_GET['from_post']); 137 137 if ($field) { … … 144 144 } 145 145 } 146 $translation = self::translate($original, $ code, pll_get_post_language($post_id));146 $translation = self::translate($original, $to, pll_get_post_language($post_id)); 147 147 } else { 148 148 $translation = $original; … … 154 154 } 155 155 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 } 162 166 } 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; 173 242 } 174 243 } -
ai-translate-for-polylang/trunk/readme.txt
r3135283 r3135322 4 4 Tags: language, translate, multilingual, translation, polylang 5 5 Requires at least: 3.0 6 Tested up to: 6. 5.47 Stable tag: 1.0. 56 Tested up to: 6.6.1 7 Stable tag: 1.0.6 8 8 License: MIT 9 9 License URI: https://opensource.org/licenses/MIT … … 34 34 == Changelog == 35 35 36 = 1.0.6 = 37 * Add helper function for programmatic translation in PHP 38 36 39 = 1.0.5 = 37 40 * Edit prompt to make sure only the new content is returned
Note: See TracChangeset
for help on using the changeset viewer.