Changeset 2789383
- Timestamp:
- 09/23/2022 03:25:21 PM (4 years ago)
- Location:
- automatic-translation/trunk
- Files:
-
- 3 added
- 10 edited
-
assets/images/flags/id.svg (added)
-
assets/images/flags/tr.svg (added)
-
assets/images/flags/uk.svg (added)
-
assets/languages.json (modified) (1 diff)
-
readme.txt (modified) (2 diffs)
-
src/admin/scripts.php (modified) (1 diff)
-
src/configuration.php (modified) (2 diffs)
-
translator.php (modified) (2 diffs)
-
vendor/translator/script-php/composer.json (modified) (2 diffs)
-
vendor/translator/script-php/src/Cache.php (modified) (7 diffs)
-
vendor/translator/script-php/src/Databases/Mysql.php (modified) (1 diff)
-
vendor/translator/script-php/src/Processor.php (modified) (2 diffs)
-
vendor/translator/script-php/src/Request.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
automatic-translation/trunk/assets/languages.json
r2760104 r2789383 155 155 "code": "zh", 156 156 "image" : "zh.svg" 157 }, 158 "tr": { 159 "name": "Turkish", 160 "original_name": "Türkçe", 161 "code": "tr" 162 }, 163 "id": { 164 "name": "Indonesian", 165 "original_name": "Indonesian", 166 "code": "id" 167 }, 168 "uk": { 169 "name": "Ukrainian", 170 "original_name": "Українська", 171 "code": "uk" 157 172 } 158 159 173 } -
automatic-translation/trunk/readme.txt
r2780020 r2789383 6 6 Tested up to: 6.0 7 7 Requires PHP: 7.0 8 Stable tag: 1.0. 18 Stable tag: 1.0.3 9 9 License: GPLv2 or later 10 10 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 83 83 == Changelog == 84 84 85 = 1.0.3 = 86 * Addign Turkish, Ukrainian and Indonesian languages 87 88 = 1.0.2 = 89 * Translation cache refactoring 90 85 91 = 1.0.1 = 86 92 * Bugfix : Fixed an issue -
automatic-translation/trunk/src/admin/scripts.php
r2760104 r2789383 224 224 $options = translatorGetOptions(); 225 225 if(isset($options["translator_api_token"])){ 226 227 228 226 if(isset($post->post_type) && ($post->post_type == "page" || $post->post_type == "post")){ 229 230 $link = get_permalink($post->ID); 231 232 if(isset($options["languages_enabled"])){ 233 foreach ($options["languages_enabled"] as $key => $languages_enabled) { 234 235 236 $uri_path = str_replace(home_url(), "", $link); 237 238 global $wpdb; 239 240 $translator_urls_table= $wpdb->prefix."translator_urls"; 241 242 $urlls = $wpdb->get_results("select translation,source from `$translator_urls_table` where source = '$uri_path' AND language = '$languages_enabled'"); 243 244 if(empty($urlls)){ 245 246 $uri_path = str_replace(home_url(), "", $link); 247 248 $uri_path = rtrim($uri_path,"/"); 249 250 $urlls = $wpdb->get_results("select translation,source from `$translator_urls_table` where source = '$uri_path' AND language = '$languages_enabled'"); 251 252 } 253 254 255 256 257 258 259 260 261 262 foreach ($urlls as $key => $urll) { 263 264 if(isset($options["translator_api_token"])){ 265 266 $translated_url1 = $urll->translation; 267 $translated_hash_path = $languages_enabled."_".md5(trim($translated_url1)); 268 269 270 271 272 273 $token_hash = md5('data'.$options["translator_api_token"]); 274 $file_hash = TRANSLATOR_PLUGIN_PATH."vendor/translator/script-php/".$token_hash."/cache/".$translated_hash_path.".php"; 275 if(file_exists($file_hash)){ 276 277 unlink($file_hash); 278 $success++; 279 /*$files = glob($file_hash . '*.php'); 280 foreach($files as $file) { 281 unlink($file); 282 }*/ 283 } 284 285 286 $translated_url2 = "/".$languages_enabled.$urll->source; 287 $translated_hash_path2 = $languages_enabled."_".md5(trim($translated_url2)); 288 289 290 $file_hash2 = TRANSLATOR_PLUGIN_PATH."vendor/translator/script-php/".$token_hash."/cache/".$translated_hash_path2.".php"; 291 if(file_exists($file_hash2)){ 292 293 unlink($file_hash2); 294 $success++; 295 /*$files = glob($file_hash . '*.php'); 296 foreach($files as $file) { 297 unlink($file); 298 }*/ 299 } 300 301 302 303 } 304 305 } 306 307 } 308 } 309 310 if($success != 0){ 227 $link = get_permalink($post->ID); 228 $uri_path = str_replace(home_url(), "", $link); 229 $opstat = TranslatorConfiguration::sourceDeleteCacheComp($uri_path); 230 if (!isset($opstat['removed']) || !$opstat['removed']) { 231 $opstat = TranslatorConfiguration::sourceDeleteCacheComp(rtrim($uri_path, "/")); 232 } 233 if ((isset($opstat['removed']) && $opstat['removed'])) { 311 234 deleteCacheApiHit("Cache clear from page save"); 312 235 } 313 236 }else{ 314 315 $token_hash = md5('data'.$options["translator_api_token"]); 316 $file_hash = TRANSLATOR_PLUGIN_PATH."vendor/translator/script-php/".$token_hash."/cache/"; 317 if(file_exists($file_hash)){ 318 $files = glob($file_hash . '*.php'); 319 foreach($files as $file) { 320 $success++; 321 unlink($file); 322 } 323 } 324 if($success != 0){ 325 deleteCacheApiHit("Cache clear from save data"); 326 } 237 \Translator\Vendor\Translator\Script\Core\Cache::getInstance()->clear_cache(); 238 deleteCacheApiHit("Cache clear from save data"); 327 239 } 328 240 } -
automatic-translation/trunk/src/configuration.php
r2760104 r2789383 225 225 226 226 public function deleteCache($url_id){ 227 228 $message = ""; 229 if($url_id != ""){ 230 227 $message = ''; 228 if(!empty($url_id)){ 231 229 global $wpdb; 232 230 $translator_urls_table= $wpdb->prefix."translator_urls"; … … 235 233 $url_id 236 234 ); 237 238 235 $source = $wpdb->get_row($query1); 239 236 240 241 if($source){ 242 243 $this->sourceDeleteCacheComp($source->source); 244 245 $query2 = $wpdb->prepare( 246 "select translation,language from `$translator_urls_table` where `source` = %s", 247 $source->source 248 ); 249 250 $urlss = $wpdb->get_results($query2); 251 252 $this->deleteCacheComp($urlss); 253 254 $url_hit = home_url().$source->source; 255 256 $message = "Delete Cache from url (". $url_hit.")"; 257 237 if(isset($source->source)){ 238 self::sourceDeleteCacheComp($source->source); 239 $message = "Delete Cache from url (". home_url().$source->source.")"; 258 240 $this->deleteCacheFromCompHit($message); 259 260 241 $message = "Cache clear successfully!"; 261 262 263 264 } 265 } 266 242 } 243 } 267 244 return $message; 268 245 } 269 246 270 public function sourceDeleteCacheComp($urll){ 271 272 273 247 public static function sourceDeleteCacheComp($urll){ 274 248 $options = translatorGetOptions(); 275 249 276 277 278 279 280 281 if(isset($options["languages_enabled"])){ 282 foreach ($options["languages_enabled"] as $key => $languages_enabled) { 283 284 285 286 287 288 $url2 = "/".$languages_enabled.$urll; 289 290 291 $translated_hash_path = $languages_enabled."_".md5(trim($url2)); 292 293 294 if(isset($options["translator_api_token"])){ 295 $token_hash = md5('data'.$options["translator_api_token"]); 296 $file_hash = TRANSLATOR_PLUGIN_PATH."vendor/translator/script-php/".$token_hash."/cache/".$translated_hash_path.".php"; 297 if(file_exists($file_hash)){ 298 299 unlink($file_hash); 300 301 } 302 } 303 304 305 306 } 307 } 308 309 250 $opstat = ['removed'=>0, 'failed'=>0]; 251 if(isset($options["translator_api_token"])){ 252 $token_hash = md5('data'.$options["translator_api_token"]); 253 254 $folder = TRANSLATOR_PLUGIN_PATH."vendor/translator/script-php/".$token_hash."/cache/". md5(trim($urll)); 255 $files = glob($folder . '/*.php'); 256 foreach($files as $file) { 257 if(@unlink($file)) { 258 $opstat['removed']++; 259 } else { 260 $opstat['failed']++; 261 } 262 } 263 if(@rmdir($folder)) { 264 $opstat['removed']++; 265 } else { 266 $opstat['failed']++; 267 } 268 } 269 return $opstat; 310 270 } 311 271 -
automatic-translation/trunk/translator.php
r2780021 r2789383 4 4 * Plugin URI: https://automatic-translation.online/ 5 5 * Description: Automatic translation plugin used to translate frontend website content with Deepl machine translator. 6 * Version: 1.0. 16 * Version: 1.0.3 7 7 * Text Domain: automatic-translation.online 8 8 * Author: masterhomepage … … 38 38 } 39 39 40 define('TRANSLATOR_VERSION', '1.0. 1');40 define('TRANSLATOR_VERSION', '1.0.3'); 41 41 define('TRANSLATOR_PLUGIN_URL', plugin_dir_url(__FILE__)); 42 42 define('TRANSLATOR_PLUGIN_PATH', plugin_dir_path(__FILE__)); -
automatic-translation/trunk/vendor/translator/script-php/composer.json
r2760104 r2789383 6 6 "authors": [ 7 7 { 8 "name": " Translator",9 "email": " dev@translator.com"8 "name": "Automatic Translation", 9 "email": "info@automatic-translation.online" 10 10 } 11 11 ], … … 18 18 "preferred-install": { 19 19 "*": "dist" 20 }, 21 "platform-check": false 20 } 22 21 }, 23 22 "require": { -
automatic-translation/trunk/vendor/translator/script-php/src/Cache.php
r2760104 r2789383 56 56 } 57 57 58 private function getHash($request_url, $language=null) { 59 if (!$language) { 60 $language = Request::getInstance()->getLanguage(); 61 } 62 $url = '/'.ltrim($request_url, '/' . $language); 63 $folder = $this->getCacheFolder($url); 64 $params = []; 65 66 sort($params); 67 68 return $folder . DIRECTORY_SEPARATOR . $language . '_' . md5(implode('', $params)); 69 } 70 71 private function getCacheFolder($request_url) { 72 return md5(trim($request_url)); 73 } 74 58 75 public function serve() { 59 76 … … 69 86 $request_url = Request::getInstance()->getRequestedUrl(); 70 87 $request_url = str_replace(home_url(), "", $request_url); 71 $this->_hash = md5(trim($request_url)); 88 // $this->_hash = md5(trim($request_url)); 89 $this->_hash = $this->getHash($request_url); 72 90 73 91 // $this->_hash = md5(trim(Request::getInstance()->getRequestedUrl())); … … 117 135 118 136 $tr_url = "/".$language.$urls_d1->source; 119 $this->_hash = md5(trim($tr_url)); 120 137 // $this->_hash = md5(trim($tr_url)); 138 $this->_hash = $this->getHash(trim($tr_url)); 121 139 if (!$this->load()) { 122 140 … … 200 218 }*/ 201 219 202 $cache_file = Configuration::getInstance()->get('data_dir') . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . $this->_language . '_' . $this->_hash . '.php'; 203 220 $cache_file = $this->getPath() . $this->_hash . '.php'; 204 221 if (!file_exists($cache_file)) { 205 222 return false; … … 230 247 } 231 248 232 $cache_ path = $this->getPath();233 $cache_ file = $cache_path . $this->_language . '_' . $this->_hash . '.php';249 $cache_file = $this->getPath() . $this->_hash . '.php'; 250 $cache_path = pathinfo($cache_file, PATHINFO_DIRNAME); 234 251 235 252 if (!file_exists($cache_path)) { … … 265 282 $cache_path = $this->getPath(); 266 283 267 $files = glob($cache_path . '*.php'); 268 269 usort($files, function($x, $y) { 270 return filemtime($x) < filemtime($y); 271 }); 272 273 $max_size = Configuration::getInstance()->get('cache_max_size') * 1024 * 1024; 284 $folders = scandir($cache_path); 285 if (!count($folders)) return; 286 $folders = array_diff($folders, ['.', '..']); 287 if (!count($folders)) return; 288 274 289 $total_size = 0; 275 290 $total_cleared = 0; 276 foreach($files as $file) { 277 if ($total_size > $max_size) { 278 unlink($file); 279 continue; 291 foreach($folders as $folder) { 292 $files = glob($cache_path . $folder . '/*.php'); 293 foreach($files as $file) { 294 $size = filesize($file); 295 $total_cleared += $size; 296 $total_size += $size; 297 @unlink($file); 280 298 } 281 $size = filesize($file); 282 $total_cleared += $size; 283 $total_size += $size; 299 @rmdir($cache_path . $folder); 284 300 } 285 301 … … 293 309 public function clear_cache(){ 294 310 $cache_path = $this->getPath(); 295 $files = glob($cache_path . '*.php'); 296 297 usort($files, function($x, $y) { 298 return filemtime($x) < filemtime($y); 299 }); 300 301 foreach($files as $file) { 302 // unlink($file); 311 312 $folders = scandir($cache_path); 313 if (!count($folders)) return; 314 $folders = array_diff($folders, ['.', '..']); 315 if (!count($folders)) return; 316 317 foreach($folders as $folder) { 318 $files = glob($cache_path . $folder . '/*.php'); 319 foreach($files as $file) { 320 $x = $file; 321 @unlink($file); 322 } 323 @rmdir($cache_path . $folder); 303 324 } 304 325 } -
automatic-translation/trunk/vendor/translator/script-php/src/Databases/Mysql.php
r2760104 r2789383 139 139 $elements = array(); 140 140 foreach ($urlll as $source => $translation) { 141 if (trim($source)==trim($translation)) continue; // source identical to target is not allowed 141 142 $source = str_replace($requested_path, '', $source); 142 143 $translation = str_replace($requested_path, '', $translation); 143 144 $elements[] = '("'.mysqli_real_escape_string($this->_database, Request::getInstance()->getLanguage()).'", "'.mysqli_real_escape_string($this->_database, $source).'", "'.mysqli_real_escape_string($this->_database, $translation).'", "'.md5($source).'", "'.md5($translation).'")'; 144 145 } 145 $query .= implode(',', $elements); 146 $query .= ' ON DUPLICATE KEY UPDATE source=VALUES(source), translation=VALUES(translation), hash_source=VALUES(hash_source), hash_translation=VALUES(hash_translation)'; 147 $this->_database->query($query); 146 if(count($elements)) { 147 $query .= implode(',', $elements); 148 $query .= ' ON DUPLICATE KEY UPDATE source=VALUES(source), translation=VALUES(translation), hash_source=VALUES(hash_source), hash_translation=VALUES(hash_translation)'; 149 $this->_database->query($query); 150 } 148 151 } 149 152 } -
automatic-translation/trunk/vendor/translator/script-php/src/Processor.php
r2760104 r2789383 51 51 } 52 52 53 // Normalize language in request url to avoid duplicate content and redirect loop 54 $norm_url = Request::getInstance()->getLanguageNormalizedURL(); 55 if (strlen($norm_url['url'])!=strlen(rtrim($norm_url['normurl'],'/'))) { 56 Response::getInstance()->setRedirect($norm_url['normurl']); 57 Response::getInstance()->end(); 58 } 59 53 60 Debug::log('$_SERVER: ' . print_r(array_merge($_SERVER, ['PHP_AUTH_PW' => '', 'HTTP_AUTHORIZATION' => '', 'HTTP_COOKIE' => '']), true), 5); 54 61 … … 79 86 $contents = Response::getInstance()->getContent(); 80 87 $contents = str_replace(' ','<nbsp></nbsp>',$contents); 88 89 //HOTFIX: invalid html closing tag in javascript 90 $matches = []; 91 preg_match_all('/<script([\S\s]*?)<\/script>/i', $contents, $matches, PREG_SET_ORDER, 0); 92 foreach($matches as $match) { 93 if(count($match)==2) { 94 $script = trim(array_shift($match)); 95 $inner_script = trim(array_shift($match)); 96 $replacement = str_replace('</', '<\/',$inner_script); 97 $contents = str_replace($script, str_replace($inner_script, $replacement, $script), $contents); 98 } 99 } 100 81 101 Response::getInstance()->setContent($contents); 82 102 -
automatic-translation/trunk/vendor/translator/script-php/src/Request.php
r2760104 r2789383 132 132 133 133 /** 134 * Retrieve language normalized url 135 */ 136 public function getLanguageNormalizedURL() { 137 $url = $this->getPathname(true); 138 // Detect nested language request and redirect to source 139 $options = translatorGetOptions(); 140 if(isset($options["languages_enabled"])){ 141 $paths = explode('/', $url); 142 $paths_count = count($paths); 143 foreach($paths as $key=>$value) { 144 if (empty($value) || in_array(strtolower($value), $options["languages_enabled"])) { 145 unset($paths[$key]); 146 } else { 147 break; 148 } 149 } 150 if (count($paths)!=$paths_count) { 151 $url = rtrim((count($paths)?implode('/', $paths):''), '/'); 152 } 153 } 154 return ['url'=> '/' . $this->getLanguage() . $this->getPathname(false),'normurl'=>'/' . $this->getLanguage() . '/' . $url]; 155 } 156 157 /** 134 158 * Retrieve current multilingual non translated url 135 159 *
Note: See TracChangeset
for help on using the changeset viewer.