Changeset 91
- Timestamp:
- 03/23/2009 11:00:14 PM (3 years ago)
- Location:
- trunk/WordPress/plugin/transposh
- Files:
-
- 2 edited
-
js/transposh.js (modified) (1 diff)
-
transposh.php (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/WordPress/plugin/transposh/js/transposh.js
r89 r91 166 166 }); 167 167 } 168 169 //to run at start 170 jQuery.noConflict(); 171 var transposh_post_url,transposh_target_lang; 172 jQuery("script[src*='transposh']").each(function (i) { 173 transposh_post_url = this.src.match('post_url=(.*?)&')[1]; 174 transposh_target_lang = this.src.match('lang=(.*?)&')[1]; 175 }); 176 google.load("language", "1"); 177 jQuery(document).ready( 178 function() { 179 do_auto_translate(); 180 } 181 ); 182 -
trunk/WordPress/plugin/transposh/transposh.php
r90 r91 66 66 $admin_msg; 67 67 68 69 68 /* 70 69 * Called when the buffer containing the original page is flused. Triggers the 71 70 * translation process. 72 *73 71 */ 74 72 function process_page(&$buffer) { 75 73 76 global $wp_query, $tr_page, $page, $pos, $lang, $plugin_url, $is_edit_mode, $wpdb, 77 $table_name; 78 79 $start_time = microtime(TRUE); 80 81 if (!isset($wp_query->query_vars[LANG_PARAM])) 82 { 83 //No language code - avoid further processing. 84 return $buffer; 85 86 } 87 88 $lang = $wp_query->query_vars[LANG_PARAM]; 89 $default_lang = get_default_lang(); 90 if($lang == $default_lang) 91 { 92 //Don't translate the default language 93 logger("Skipping translation for default language $default_lang", 3); 94 return $buffer; 95 } 96 97 98 $page = $buffer; 99 100 101 if (($wp_query->query_vars[EDIT_PARAM] == "1" || 102 $wp_query->query_vars[EDIT_PARAM] == "true")) 103 { 104 //Verify that the current language is editable and that the 105 //user has the required permissions 106 $editable_langs = get_option(EDITABLE_LANGS); 107 108 if(is_translator() && strstr($editable_langs, $lang)) 109 { 110 $is_edit_mode = TRUE; 111 } 112 113 } 114 115 logger("translating " . $_SERVER['REQUEST_URI'] . " to: $lang", 1); 116 117 //translate the entire page 118 process_html(); 119 120 $end_time = microtime(TRUE); 121 122 logger("Translation completed in " . ($end_time - $start_time) . " seconds", 1); 123 124 //return the translated page unless it is empty, othewise return the original 125 return (strlen($tr_page) > 0 ? $tr_page : $page); 74 global $wp_query, $tr_page, $page, $pos, $lang, $plugin_url, $is_edit_mode, $wpdb, $table_name; 75 76 $start_time = microtime(TRUE); 77 78 if (!isset($wp_query->query_vars[LANG_PARAM])) 79 { 80 //No language code - avoid further processing. 81 return $buffer; 82 83 } 84 85 $lang = $wp_query->query_vars[LANG_PARAM]; 86 $default_lang = get_default_lang(); 87 if($lang == $default_lang) 88 { 89 //Don't translate the default language 90 logger("Skipping translation for default language $default_lang", 3); 91 return $buffer; 92 } 93 94 $page = $buffer; 95 96 if (($wp_query->query_vars[EDIT_PARAM] == "1" || 97 $wp_query->query_vars[EDIT_PARAM] == "true")) 98 { 99 //Verify that the current language is editable and that the 100 //user has the required permissions 101 $editable_langs = get_option(EDITABLE_LANGS); 102 103 if(is_translator() && strstr($editable_langs, $lang)) 104 { 105 $is_edit_mode = TRUE; 106 } 107 } 108 109 logger("translating " . $_SERVER['REQUEST_URI'] . " to: $lang", 1); 110 111 //translate the entire page 112 process_html(); 113 114 $end_time = microtime(TRUE); 115 116 logger("Translation completed in " . ($end_time - $start_time) . " seconds", 1); 117 118 //return the translated page unless it is empty, othewise return the original 119 return (strlen($tr_page) > 0 ? $tr_page : $page); 126 120 } 127 121 … … 129 123 * Fix links on the page. href needs to be modified to include 130 124 * lang specifier and editing mode. 131 *132 125 */ 133 126 function process_anchor_tag($start, $end) 134 127 { 135 global $home_url, $home_url_quoted, $lang, $is_edit_mode, $wp_rewrite; 136 137 $href = get_attribute($start, $end, 'href'); 138 139 if($href == NULL) 140 { 141 return; 142 } 143 144 //Ignore urls not from this site 145 if(stripos($href, $home_url) === FALSE) 146 { 147 return; 148 } 149 150 $use_params = FALSE; 151 152 //Only use params if permalinks are not enabled. 153 //don't fix links pointing to real files as it will cause that the 154 //web server will not be able to locate them 155 if(!$wp_rewrite->using_permalinks() || 156 stripos($href, '/wp-admin') !== FALSE || 157 stripos($href, '/wp-content') !== FALSE || 158 stripos($href, '/wp-login') !== FALSE || 159 stripos($href, '/.php') !== FALSE) 160 { 161 $use_params = TRUE; 162 } 163 164 $href = rewrite_url_lang_param($href, $lang, $is_edit_mode, $use_params); 165 166 //rewrite url in translated page 167 update_translated_page($start, $end, $href); 168 logger(__METHOD__ . " $home_url href: $href"); 169 } 170 128 global $home_url, $home_url_quoted, $lang, $is_edit_mode, $wp_rewrite; 129 130 $href = get_attribute($start, $end, 'href'); 131 132 if($href == NULL) 133 { 134 return; 135 } 136 137 //Ignore urls not from this site 138 if(stripos($href, $home_url) === FALSE) 139 { 140 return; 141 } 142 143 $use_params = FALSE; 144 145 //Only use params if permalinks are not enabled. 146 //don't fix links pointing to real files as it will cause that the 147 //web server will not be able to locate them 148 if(!$wp_rewrite->using_permalinks() || 149 stripos($href, '/wp-admin') !== FALSE || 150 stripos($href, '/wp-content') !== FALSE || 151 stripos($href, '/wp-login') !== FALSE || 152 stripos($href, '/.php') !== FALSE) 153 { 154 $use_params = TRUE; 155 } 156 157 $href = rewrite_url_lang_param($href, $lang, $is_edit_mode, $use_params); 158 159 //rewrite url in translated page 160 update_translated_page($start, $end, $href); 161 logger(__METHOD__ . " $home_url href: $href"); 162 } 171 163 172 164 /* … … 179 171 function rewrite_url_lang_param($url, $lang, $is_edit, $use_params_only) 180 172 { 181 global $home_url, $home_url_quoted;182 183 if(!get_option(ENABLE_PERMALINKS_REWRITE))184 {185 //override the use only params - admin configured system to not touch permalinks186 $use_params_only = TRUE;187 }188 189 if($is_edit)190 {191 $params = EDIT_PARAM . '=1&';192 193 }194 195 if($use_params_only)196 {197 $params .= LANG_PARAM . "=$lang&";198 }199 else200 {201 $url = preg_replace("/$home_url_quoted\/(..\/)?\/?/",173 global $home_url, $home_url_quoted; 174 175 if(!get_option(ENABLE_PERMALINKS_REWRITE)) 176 { 177 //override the use only params - admin configured system to not touch permalinks 178 $use_params_only = TRUE; 179 } 180 181 if($is_edit) 182 { 183 $params = EDIT_PARAM . '=1&'; 184 185 } 186 187 if($use_params_only) 188 { 189 $params .= LANG_PARAM . "=$lang&"; 190 } 191 else 192 { 193 $url = preg_replace("/$home_url_quoted\/(..\/)?\/?/", 202 194 "$home_url/$lang/", $url); 203 }204 205 if($params)206 {207 //insert params to url208 $url = preg_replace("/(.+\/[^\?\#]*[\?]?)/", '$1?' . $params, $url);209 210 //Cleanup extra &211 $url = preg_replace("/&&+/", "&", $url);212 213 //Cleanup extra ?214 $url = preg_replace("/\?\?+/", "?", $url);215 }216 217 return $url;195 } 196 197 if($params) 198 { 199 //insert params to url 200 $url = preg_replace("/(.+\/[^\?\#]*[\?]?)/", '$1?' . $params, $url); 201 202 //Cleanup extra & 203 $url = preg_replace("/&&+/", "&", $url); 204 205 //Cleanup extra ? 206 $url = preg_replace("/\?\?+/", "?", $url); 207 } 208 209 return $url; 218 210 } 219 211 220 212 /* 221 213 * Fetch translation from db or cache. 222 * Returns An array that contains the translated string and it source. 223 * Will return NULL if no translation is available.214 * Returns An array that contains the translated string and it source. 215 * Will return NULL if no translation is available. 224 216 */ 225 217 function fetch_translation($original) 226 218 { 227 global $wpdb, $lang, $table_name; 228 $translated = NULL; 229 logger("Enter " . __METHOD__ . ": $original", 4); 230 231 //The original is saved in db in its escaped form 232 $original = $wpdb->escape(html_entity_decode($original, ENT_NOQUOTES, 'UTF-8')); 233 234 if(ENABLE_APC && function_exists('apc_fetch')) 235 { 236 $cached = apc_fetch($original .'___'. $lang, $rc); 237 if($rc === TRUE) 238 { 239 logger("Exit from cache " . __METHOD__ . ": $cached", 4); 240 return $cached; 241 } 242 } 243 244 $query = "SELECT * FROM $table_name WHERE original = '$original' and lang = '$lang' "; 245 $row = $wpdb->get_row($query); 246 247 if($row !== FALSE) 248 { 249 $translated_text = stripslashes($row->translated); 250 $translated = array($translated_text, $row->source); 251 252 logger("db result for $original >>> $translated_text ($lang) ({$row->source})" , 3); 253 } 254 255 256 if(ENABLE_APC && function_exists('apc_store')) 257 { 258 //If we don't have translation still we want to have it in cache 259 $cache_entry = $translated; 260 if($cache_entry == NULL) 261 { 262 $cache_entry = ""; 263 } 264 265 //update cache 266 $rc = apc_store($original .'___'. $lang, $cache_entry, 3600); 267 if($rc === TRUE) 268 { 269 logger("Stored in cache: $original => $translated", 3); 270 } 271 } 272 273 logger("Exit " . __METHOD__ . ": $translated", 4); 274 return $translated; 275 } 276 277 /* 278 * Insert references to the javascript files used in the transalted 279 * version of the page. 280 * 281 */ 282 function insert_javascript_includes() 283 { 284 global $plugin_url, $wp_query, $lang, $home_url, $enable_auto_translate; 285 286 $is_edit_param_enabled = ($wp_query->query_vars[EDIT_PARAM] == "1" || 287 $wp_query->query_vars[EDIT_PARAM] == "true"); 288 289 if (!$is_edit_param_enabled && ! $enable_auto_translate) 290 { 291 //TODO: check permission later - for now just make sure we don't load the 292 //js code when it is not needed 293 return; 294 } 295 296 297 $overlib_dir = "$plugin_url/js/overlibmws"; 298 299 if($is_edit_param_enabled) 300 { 301 $js = "\n<script type=\"text/javascript\" src=\"$overlib_dir/overlibmws.js\"></script>"; 302 $js .= "\n<script type=\"text/javascript\" src=\"$overlib_dir/overlibmws_filter.js\"></script>"; 303 $js .= "\n<script type=\"text/javascript\" src=\"$overlib_dir/overlibmws_modal.js\"></script>"; 304 $js .= "\n<script type=\"text/javascript\" src=\"$overlib_dir/overlibmws_overtwo.js\"></script>"; 305 $js .= "\n<script type=\"text/javascript\" src=\"$overlib_dir/overlibmws_scroll.js\"></script>"; 306 $js .= "\n<script type=\"text/javascript\" src=\"$overlib_dir/overlibmws_shadow.js\"></script>"; 307 } 308 309 if($is_edit_param_enabled || $enable_auto_translate) 310 { 311 $js .= "\n<script type=\"text/javascript\" src=\"$plugin_url/js/transposh.js\"></script>"; 312 $js .= "\n<script type=\"text/javascript\" src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js\"></script>"; 313 $js .= "\n<script type=\"text/javascript\">jQuery.noConflict();</script>"; 314 $js .= "\n<script type=\"text/javascript\" src=\"http://www.google.com/jsapi\"></script>"; 315 $js .= "\n<script type=\"text/javascript\">google.load(\"language\", \"1\");</script>"; 316 317 318 $post_url = $home_url . '/index.php'; 319 $js .= "\n<script type=\"text/javascript\">var transposh_post_url='$post_url';var transposh_target_lang='$lang';</script>"; 320 321 $js .= "\n<script type=\"text/javascript\">jQuery(document).ready(function() {do_auto_translate();});</script>"; 322 } 323 324 echo $js . "\n"; 325 } 326 219 global $wpdb, $lang, $table_name; 220 $translated = NULL; 221 logger("Enter " . __METHOD__ . ": $original", 4); 222 223 //The original is saved in db in its escaped form 224 $original = $wpdb->escape(html_entity_decode($original, ENT_NOQUOTES, 'UTF-8')); 225 226 if(ENABLE_APC && function_exists('apc_fetch')) 227 { 228 $cached = apc_fetch($original .'___'. $lang, $rc); 229 if($rc === TRUE) 230 { 231 logger("Exit from cache " . __METHOD__ . ": $cached", 4); 232 return $cached; 233 } 234 } 235 236 $query = "SELECT * FROM $table_name WHERE original = '$original' and lang = '$lang' "; 237 $row = $wpdb->get_row($query); 238 239 if($row !== FALSE) 240 { 241 $translated_text = stripslashes($row->translated); 242 $translated = array($translated_text, $row->source); 243 244 logger("db result for $original >>> $translated_text ($lang) ({$row->source})" , 3); 245 } 246 247 if(ENABLE_APC && function_exists('apc_store')) 248 { 249 //If we don't have translation still we want to have it in cache 250 $cache_entry = $translated; 251 if($cache_entry == NULL) 252 { 253 $cache_entry = ""; 254 } 255 256 //update cache 257 $rc = apc_store($original .'___'. $lang, $cache_entry, 3600); 258 if($rc === TRUE) 259 { 260 logger("Stored in cache: $original => $translated", 3); 261 } 262 } 263 264 logger("Exit " . __METHOD__ . ": $translated", 4); 265 return $translated; 266 } 327 267 328 268 /* … … 330 270 * item on the page. 331 271 * param segement_id The id (number) identifying this segment. Needs to be 332 placed within the img tag for use on client side operation (jquery)272 * placed within the img tag for use on client side operation (jquery) 333 273 */ 334 274 function get_img_tag($original, $translation, $source, $segment_id, $is_translated = FALSE) 335 275 { 336 global $plugin_url, $lang, $home_url;337 $url = $home_url . '/index.php';338 339 //For use in javascript, make the following changes:340 //1. Add slashes to escape the inner text341 //2. Convert the html special characters342 //The browser will take decode step 2 and pass it to the js engine which decode step 1 - a bit tricky343 $translation = htmlspecialchars(addslashes($translation));344 $original = htmlspecialchars(addslashes($original));345 346 if ($is_translated)347 {348 $add_img = "_fix";349 }350 351 if ($source == 1) {352 $add_img = "_auto";353 }354 355 $img = "<img src=\"$plugin_url/translate$add_img.png\" alt=\"translate\" id=\"" . IMG_PREFIX . "$segment_id\" 356 onclick=\"translate_dialog('$original','$translation','$segment_id'); return false;\"357 onmouseover=\"hint('$original'); return true;\"358 onmouseout=\"nd()\" />";359 276 global $plugin_url, $lang, $home_url; 277 $url = $home_url . '/index.php'; 278 279 //For use in javascript, make the following changes: 280 //1. Add slashes to escape the inner text 281 //2. Convert the html special characters 282 //The browser will take decode step 2 and pass it to the js engine which decode step 1 - a bit tricky 283 $translation = htmlspecialchars(addslashes($translation)); 284 $original = htmlspecialchars(addslashes($original)); 285 286 if ($is_translated) 287 { 288 $add_img = "_fix"; 289 } 290 291 if ($source == 1) { 292 $add_img = "_auto"; 293 } 294 295 $img = "<img src=\"$plugin_url/translate$add_img.png\" alt=\"translate\" id=\"" . IMG_PREFIX . "$segment_id\" ". 296 "onclick=\"translate_dialog('$original','$translation','$segment_id'); return false;\" ". 297 "onmouseover=\"hint('$original'); return true;\" ". 298 "onmouseout=\"nd()\" />"; 299 360 300 return $img; 361 301 } 362 302 363 364 /*365 * Add custom css, i.e. transposh.css366 *367 */368 function add_custom_css()369 {370 transposh_css();371 insert_javascript_includes();372 }373 374 // We need some CSS to position the paragraph375 function transposh_css()376 {377 global $plugin_url, $wp_query;378 379 if (!isset($wp_query->query_vars[LANG_PARAM]))380 {381 return;382 }383 384 //include the transposh.css385 echo "<link rel=\"stylesheet\" href=\"$plugin_url/transposh.css\" type=\"text/css\" />";386 387 logger("Added transposh_css");388 }389 390 303 /* 391 304 * Init global variables later used throughout this process … … 393 306 function init_global_vars() 394 307 { 395 global $home_url, $home_url_quoted, $plugin_url, $table_name, $wpdb, $enable_auto_translate;396 397 $home_url = get_option('home');398 $local_dir = preg_replace("/.*\//", "", dirname(__FILE__));399 400 $plugin_url= $home_url . "/wp-content/plugins/$local_dir";401 $home_url_quoted = preg_quote($home_url);402 $home_url_quoted = preg_replace("/\//", "\\/", $home_url_quoted);403 404 $table_name = $wpdb->prefix . TRANSLATIONS_TABLE;405 $enable_auto_translate = get_option(ENABLE_AUTO_TRANSLATE,1) && is_translator();308 global $home_url, $home_url_quoted, $plugin_url, $table_name, $wpdb, $enable_auto_translate; 309 310 $home_url = get_option('home'); 311 $local_dir = preg_replace("/.*\//", "", dirname(__FILE__)); 312 313 $plugin_url= $home_url . "/wp-content/plugins/$local_dir"; 314 $home_url_quoted = preg_quote($home_url); 315 $home_url_quoted = preg_replace("/\//", "\\/", $home_url_quoted); 316 317 $table_name = $wpdb->prefix . TRANSLATIONS_TABLE; 318 $enable_auto_translate = get_option(ENABLE_AUTO_TRANSLATE,1) && is_translator(); 406 319 } 407 320 408 321 /* 409 322 * A new translation has been posted, update the translation database. 410 *411 323 */ 412 324 function update_translation() 413 325 { 414 global $wpdb, $table_name;415 416 $ref=getenv('HTTP_REFERER');417 $original = base64_url_decode($_POST['token']);418 $translation = $_POST['translation'];419 $lang = $_POST['lang'];420 $source = $_POST['source'];421 422 if(!isset($original) || !isset($translation) || !isset($lang))423 {424 logger("Enter " . __FILE__ . " missing params: $original , $translation, $lang," . $ref, 0);425 return;426 }427 428 //Check that use is allowed to translate429 if(!is_translator())430 {431 logger("Unauthorized translation attempt " . $_SERVER['REMOTE_ADDR'] , 1);432 }433 434 //Decode & remove already escaped character to avoid double escaping435 $translation = $wpdb->escape(htmlspecialchars(stripslashes(urldecode($translation))));436 437 //The original content is encoded as base64 before it is sent (i.e. token), after we438 //decode it should just the same after it was parsed. 439 $original = $wpdb->escape(html_entity_decode($original, ENT_NOQUOTES, 'UTF-8'));440 441 $update = "REPLACE INTO $table_name (original, translated, lang, source)326 global $wpdb, $table_name; 327 328 $ref=getenv('HTTP_REFERER'); 329 $original = base64_url_decode($_POST['token']); 330 $translation = $_POST['translation']; 331 $lang = $_POST['lang']; 332 $source = $_POST['source']; 333 334 if(!isset($original) || !isset($translation) || !isset($lang)) 335 { 336 logger("Enter " . __FILE__ . " missing params: $original , $translation, $lang," . $ref, 0); 337 return; 338 } 339 340 //Check that use is allowed to translate 341 if(!is_translator()) 342 { 343 logger("Unauthorized translation attempt " . $_SERVER['REMOTE_ADDR'] , 1); 344 } 345 346 //Decode & remove already escaped character to avoid double escaping 347 $translation = $wpdb->escape(htmlspecialchars(stripslashes(urldecode($translation)))); 348 349 //The original content is encoded as base64 before it is sent (i.e. token), after we 350 //decode it should just the same after it was parsed. 351 $original = $wpdb->escape(html_entity_decode($original, ENT_NOQUOTES, 'UTF-8')); 352 353 $update = "REPLACE INTO $table_name (original, translated, lang, source) 442 354 VALUES ('" . $original . "','" . $translation . "','" . $lang . "','" . $source . "')"; 443 355 444 $result = $wpdb->query($update); 445 446 if($result !== FALSE) 447 { 448 update_transaction_log($original, $translation, $lang, $source); 449 450 //Delete entry from cache 451 if(ENABLE_APC && function_exists('apc_store')) 452 { 453 apc_delete($original .'___'. $lang); 454 } 455 456 logger("Inserted to db '$original' , '$translation', '$lang' " , 3); 457 } 458 else 459 { 460 logger("Error !!! failed to insert to db $original , $translation, $lang," , 0); 461 header("HTTP/1.0 404 Failed to update language database"); 462 } 463 464 exit; 465 } 466 356 $result = $wpdb->query($update); 357 358 if($result !== FALSE) 359 { 360 update_transaction_log($original, $translation, $lang, $source); 361 362 //Delete entry from cache 363 if(ENABLE_APC && function_exists('apc_store')) 364 { 365 apc_delete($original .'___'. $lang); 366 } 367 368 logger("Inserted to db '$original' , '$translation', '$lang' " , 3); 369 } 370 else 371 { 372 logger("Error !!! failed to insert to db $original , $translation, $lang," , 0); 373 header("HTTP/1.0 404 Failed to update language database"); 374 } 375 376 exit; 377 } 467 378 468 379 /* 469 380 * Update the transaction log 470 *471 381 */ 472 382 function update_transaction_log(&$original, &$translation, &$lang, $source) … … 477 387 // log either the user ID or his IP 478 388 if ('' == $user_ID) 479 {389 { 480 390 $loguser = $_SERVER['REMOTE_ADDR']; 481 391 } 482 else483 {392 else 393 { 484 394 $loguser = $user_ID; 485 395 } 486 396 487 $log = "INSERT INTO ".$wpdb->prefix.TRANSLATIONS_LOG." (original, translated, lang, translated_by, source) 488 VALUES ('" . $original . "','" . $translation . "','" . $lang . "','".$loguser."','".$source."')";489 490 $result = $wpdb->query($log);491 492 if($result === FALSE)493 {494 logger(mysql_error(),0);495 logger("Error !!! failed to update transaction log: $loguser, $original ,$translation, $lang, $source" , 0);496 }397 $log = "INSERT INTO ".$wpdb->prefix.TRANSLATIONS_LOG." (original, translated, lang, translated_by, source) ". 398 "VALUES ('" . $original . "','" . $translation . "','" . $lang . "','".$loguser."','".$source."')"; 399 400 $result = $wpdb->query($log); 401 402 if($result === FALSE) 403 { 404 logger(mysql_error(),0); 405 logger("Error !!! failed to update transaction log: $loguser, $original ,$translation, $lang, $source" , 0); 406 } 497 407 } 498 408 … … 504 414 function get_default_lang() 505 415 { 506 global $languages;507 508 $default = get_option(DEFAULT_LANG);509 if(!$languages[$default])510 {511 $default = "en";512 }513 514 return $default;416 global $languages; 417 418 $default = get_option(DEFAULT_LANG); 419 if(!$languages[$default]) 420 { 421 $default = "en"; 422 } 423 424 return $default; 515 425 } 516 426 … … 521 431 function on_init() 522 432 { 523 logger(__METHOD__ . $_SERVER['REQUEST_URI']);524 init_global_vars();525 526 if ($_POST['translation_posted'])527 {528 update_translation();529 }530 else531 {532 //set the callback for translating the page when it's done533 ob_start("process_page");534 }433 logger(__METHOD__ . $_SERVER['REQUEST_URI']); 434 init_global_vars(); 435 436 if ($_POST['translation_posted']) 437 { 438 update_translation(); 439 } 440 else 441 { 442 //set the callback for translating the page when it's done 443 ob_start("process_page"); 444 } 535 445 } 536 446 … … 540 450 function on_shutdown() 541 451 { 542 ob_flush();452 ob_flush(); 543 453 } 544 454 545 455 /* 546 456 * Update the url rewrite rules to include language identifier 547 *548 457 */ 549 458 function update_rewrite_rules($rules){ 550 logger("Enter update_rewrite_rules");551 552 if(!get_option(ENABLE_PERMALINKS_REWRITE))553 {554 logger("Not touching rewrite rules - permalinks modification disabled by admin");555 return $rules;556 }557 558 $newRules = array();559 $lang_prefix="([a-z]{2,2}(\-[a-z]{2,2})?)/";560 561 $lang_parameter= "&" . LANG_PARAM . '=$matches[1]';562 563 //catch the root url564 $newRules[$lang_prefix."?$"] = "index.php?lang=\$matches[1]";565 logger("\t" . $lang_prefix."?$" . " ---> " . "index.php?lang=\$matches[1]");566 567 foreach ($rules as $key=>$value) {568 $original_key = $key;569 $original_value = $value;570 571 $key = $lang_prefix . $key;572 573 //Shift existing matches[i] two step forward as we pushed new elements574 //in the beginning of the expression575 for($i = 6; $i > 0; $i--)576 {577 $value = str_replace('['. $i .']', '['. ($i + 2) .']', $value);578 }579 580 $value .= $lang_parameter;581 582 logger("\t" . $key . " ---> " . $value);583 584 585 $newRules[$key] = $value;586 $newRules[$original_key] = $original_value;587 588 logger("\t" . $original_key . " ---> " . $original_value);589 }590 591 logger("Exit update_rewrite_rules");592 return $newRules;459 logger("Enter update_rewrite_rules"); 460 461 if(!get_option(ENABLE_PERMALINKS_REWRITE)) 462 { 463 logger("Not touching rewrite rules - permalinks modification disabled by admin"); 464 return $rules; 465 } 466 467 $newRules = array(); 468 $lang_prefix="([a-z]{2,2}(\-[a-z]{2,2})?)/"; 469 470 $lang_parameter= "&" . LANG_PARAM . '=$matches[1]'; 471 472 //catch the root url 473 $newRules[$lang_prefix."?$"] = "index.php?lang=\$matches[1]"; 474 logger("\t" . $lang_prefix."?$" . " ---> " . "index.php?lang=\$matches[1]"); 475 476 foreach ($rules as $key=>$value) { 477 $original_key = $key; 478 $original_value = $value; 479 480 $key = $lang_prefix . $key; 481 482 //Shift existing matches[i] two step forward as we pushed new elements 483 //in the beginning of the expression 484 for($i = 6; $i > 0; $i--) 485 { 486 $value = str_replace('['. $i .']', '['. ($i + 2) .']', $value); 487 } 488 489 $value .= $lang_parameter; 490 491 logger("\t" . $key . " ---> " . $value); 492 493 494 $newRules[$key] = $value; 495 $newRules[$original_key] = $original_value; 496 497 logger("\t" . $original_key . " ---> " . $original_value); 498 } 499 500 logger("Exit update_rewrite_rules"); 501 return $newRules; 593 502 } 594 503 … … 598 507 function parameter_queryvars($qvars) 599 508 { 600 $qvars[] = LANG_PARAM;601 $qvars[] = EDIT_PARAM;602 603 return $qvars;509 $qvars[] = LANG_PARAM; 510 $qvars[] = EDIT_PARAM; 511 512 return $qvars; 604 513 } 605 514 606 515 /* 607 516 * Setup the translation database. 608 *609 517 */ 610 518 function setup_db() 611 519 { 612 logger("Enter " . __METHOD__ );520 logger("Enter " . __METHOD__ ); 613 521 global $wpdb; 614 require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); 615 616 $installed_ver = get_option(TRANSPOSH_DB_VERSION); 617 618 if( $installed_ver != DB_VERSION ) { 619 $table_name = $wpdb->prefix . TRANSLATIONS_TABLE; 620 621 logger("Attempting to create table $table_name", 0); 622 $sql = "CREATE TABLE $table_name (original VARCHAR(256) NOT NULL, 623 lang CHAR(5) NOT NULL, 624 translated VARCHAR(256), 625 source TINYINT NOT NULL, 626 PRIMARY KEY (original, lang)) "; 627 628 629 dbDelta($sql); 630 631 632 $table_name = $wpdb->prefix . TRANSLATIONS_LOG; 633 634 logger("Attempting to create table $table_name", 0); 635 $sql = "CREATE TABLE " . $table_name . " (original VARCHAR(256) NOT NULL, 636 lang CHAR(5) NOT NULL, 637 translated VARCHAR(256), 638 translated_by VARCHAR(15), 639 source TINYINT NOT NULL, 640 timestamp TIMESTAMP, 641 PRIMARY KEY (original, lang, timestamp)) "; 642 643 644 dbDelta($sql); 522 require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); 523 524 $installed_ver = get_option(TRANSPOSH_DB_VERSION); 525 526 if( $installed_ver != DB_VERSION ) { 527 $table_name = $wpdb->prefix . TRANSLATIONS_TABLE; 528 529 logger("Attempting to create table $table_name", 0); 530 $sql = "CREATE TABLE $table_name (original VARCHAR(256) NOT NULL,". 531 "lang CHAR(5) NOT NULL,". 532 "translated VARCHAR(256),". 533 "source TINYINT NOT NULL,". 534 "PRIMARY KEY (original, lang))"; 535 536 dbDelta($sql); 537 538 539 $table_name = $wpdb->prefix . TRANSLATIONS_LOG; 540 541 logger("Attempting to create table $table_name", 0); 542 $sql = "CREATE TABLE $table_name (original VARCHAR(256) NOT NULL,". 543 "lang CHAR(5) NOT NULL,". 544 "translated VARCHAR(256),". 545 "translated_by VARCHAR(15),". 546 "source TINYINT NOT NULL,". 547 "timestamp TIMESTAMP,". 548 "PRIMARY KEY (original, lang, timestamp))"; 549 550 dbDelta($sql); 645 551 update_option(TRANSPOSH_DB_VERSION, DB_VERSION); 646 }647 648 logger("Exit " . __METHOD__ );552 } 553 554 logger("Exit " . __METHOD__ ); 649 555 } 650 556 … … 655 561 function is_translator() 656 562 { 657 if(is_user_logged_in()) 658 { 659 if(current_user_can(TRANSLATOR)) 660 { 661 return TRUE; 662 } 663 } 664 665 if(get_option(ANONYMOUS_TRANSLATION)) 666 { 667 //if anonymous translation is allowed - let anyone enjoy it 668 return TRUE; 669 } 670 671 return FALSE; 672 } 673 563 if(is_user_logged_in()) 564 { 565 if(current_user_can(TRANSLATOR)) 566 { 567 return TRUE; 568 } 569 } 570 571 if(get_option(ANONYMOUS_TRANSLATION)) 572 { 573 //if anonymous translation is allowed - let anyone enjoy it 574 return TRUE; 575 } 576 577 return FALSE; 578 } 674 579 675 580 /* 676 581 * Plugin activated. 677 *678 582 */ 679 583 function plugin_activate() 680 584 { 681 global $wp_rewrite; 682 logger("plugin_activate enter: " . dirname(__FILE__)); 683 684 setup_db(); 685 686 add_filter('rewrite_rules_array', 'update_rewrite_rules'); 687 $wp_rewrite->flush_rules(); 688 689 logger("plugin_activate exit: " . dirname(__FILE__)); 690 } 691 585 global $wp_rewrite; 586 logger("plugin_activate enter: " . dirname(__FILE__)); 587 588 setup_db(); 589 590 add_filter('rewrite_rules_array', 'update_rewrite_rules'); 591 $wp_rewrite->flush_rules(); 592 593 logger("plugin_activate exit: " . dirname(__FILE__)); 594 } 692 595 693 596 /* 694 597 * Plugin deactivated. 695 *696 598 */ 697 599 function plugin_deactivate(){ 698 global $wp_rewrite;699 logger("plugin_deactivate enter: " . dirname(__FILE__));700 701 remove_filter('rewrite_rules_array', 'update_rewrite_rules');702 $wp_rewrite->flush_rules();703 704 logger("plugin_deactivate exit: " . dirname(__FILE__));600 global $wp_rewrite; 601 logger("plugin_deactivate enter: " . dirname(__FILE__)); 602 603 remove_filter('rewrite_rules_array', 'update_rewrite_rules'); 604 $wp_rewrite->flush_rules(); 605 606 logger("plugin_deactivate exit: " . dirname(__FILE__)); 705 607 } 706 608 707 609 /* 708 610 * Callback from admin_notices - display error message to the admin. 709 *710 611 */ 711 612 function plugin_install_error() 712 613 { 713 global $admin_msg; 714 logger("Enter " . __METHOD__, 0); 715 716 echo '<div class="updated"><p>'; 717 echo 'Error has occured in the installation process of the translation plugin: <br>'; 718 719 echo $admin_msg; 720 721 if (function_exists('deactivate_plugins') ) { 722 deactivate_plugins(get_plugin_name(), "translate.php"); 723 echo '<br> This plugin has been automatically deactivated.'; 724 } 725 726 echo '</p></div>'; 727 } 728 614 global $admin_msg; 615 logger("Enter " . __METHOD__, 0); 616 617 echo '<div class="updated"><p>'; 618 echo 'Error has occured in the installation process of the translation plugin: <br>'; 619 620 echo $admin_msg; 621 622 if (function_exists('deactivate_plugins') ) { 623 deactivate_plugins(get_plugin_name(), "translate.php"); 624 echo '<br> This plugin has been automatically deactivated.'; 625 } 626 627 echo '</p></div>'; 628 } 729 629 730 630 /* … … 732 632 * to check that the plugin loaded successfully else trigger notification 733 633 * to the admin and deactivate plugin. 734 *735 634 */ 736 635 function plugin_loaded() 737 636 { 738 global $admin_msg; 739 logger("Enter " . __METHOD__, 4); 740 741 $db_version = get_option(TRANSPOSH_DB_VERSION); 742 743 if ($db_version != DB_VERSION) 744 { 745 setup_db(); 746 //$admin_msg = "Translation database version ($db_version) is not comptabile with this plugin (". DB_VERSION . ") <br>"; 747 748 logger("Updating database in plugin loaded", 0); 749 //Some error occured - notify admin and deactivate plugin 750 //add_action('admin_notices', 'plugin_install_error'); 751 } 752 753 if ($db_version != DB_VERSION) 754 { 755 $admin_msg = "Failed to locate the translation table <em> " . TRANSLATIONS_TABLE . "</em> in local database. <br>"; 756 757 logger("Messsage to admin: $admin_msg", 0); 758 //Some error occured - notify admin and deactivate plugin 759 add_action('admin_notices', 'plugin_install_error'); 760 } 761 637 global $admin_msg; 638 logger("Enter " . __METHOD__, 4); 639 640 $db_version = get_option(TRANSPOSH_DB_VERSION); 641 642 if ($db_version != DB_VERSION) 643 { 644 setup_db(); 645 //$admin_msg = "Translation database version ($db_version) is not comptabile with this plugin (". DB_VERSION . ") <br>"; 646 647 logger("Updating database in plugin loaded", 0); 648 //Some error occured - notify admin and deactivate plugin 649 //add_action('admin_notices', 'plugin_install_error'); 650 } 651 652 if ($db_version != DB_VERSION) 653 { 654 $admin_msg = "Failed to locate the translation table <em> " . TRANSLATIONS_TABLE . "</em> in local database. <br>"; 655 656 logger("Messsage to admin: $admin_msg", 0); 657 //Some error occured - notify admin and deactivate plugin 658 add_action('admin_notices', 'plugin_install_error'); 659 } 762 660 } 763 661 … … 779 677 } 780 678 679 /* 680 * Add custom css, i.e. transposh.css 681 */ 682 function add_transposh_css() { 683 global $plugin_url, $wp_query; 684 if (!isset($wp_query->query_vars[LANG_PARAM])) 685 { 686 return; 687 } 688 //include the transposh.css 689 wp_enqueue_style("transposh","$plugin_url/transposh.css",array(),'1.0'); 690 logger("Added transposh_css"); 691 } 692 693 /* 694 * Insert references to the javascript files used in the transalted 695 * version of the page. 696 */ 697 function add_transposh_js() { 698 global $plugin_url, $wp_query, $lang, $home_url, $enable_auto_translate; 699 700 if (!isset($wp_query->query_vars[LANG_PARAM])) 701 { 702 return; 703 } 704 705 $is_edit_param_enabled = $wp_query->query_vars[EDIT_PARAM]; 706 707 if (!$is_edit_param_enabled && ! $enable_auto_translate) 708 { 709 //TODO: check permission later - for now just make sure we don't load the 710 //js code when it is not needed 711 return; 712 } 713 714 $overlib_dir = "$plugin_url/js/overlibmws"; 715 716 if($is_edit_param_enabled) 717 { 718 wp_enqueue_script("overlibmws","$overlib_dir/overlibmws.js",array(),'1.0'); 719 wp_enqueue_script("overlibmws1","$overlib_dir/overlibmws_filter.js",array("overlibmws"),'1.0'); 720 wp_enqueue_script("overlibmws2","$overlib_dir/overlibmws_modal.js",array("overlibmws1"),'1.0'); 721 wp_enqueue_script("overlibmws3","$overlib_dir/overlibmws_overtwo.js",array("overlibmws2"),'1.0'); 722 wp_enqueue_script("overlibmws4","$overlib_dir/overlibmws_scroll.js",array("overlibmws3"),'1.0'); 723 wp_enqueue_script("overlibmws5","$overlib_dir/overlibmws_shadow.js",array("overlibmws4"),'1.0'); 724 } 725 726 if($is_edit_param_enabled || $enable_auto_translate) 727 { 728 $post_url = $home_url . '/index.php'; 729 wp_enqueue_script("jquerymin","http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js",array(),'1.3.2'); 730 wp_enqueue_script("google","http://www.google.com/jsapi",array(),'1'); 731 wp_enqueue_script("transposh","$plugin_url/js/transposh.js?post_url=$post_url&lang={$lang}",array("jquerymin"),'1.0'); 732 } 733 } 734 781 735 //Register callbacks 782 add_action('wp_head', 'add_custom_css');783 736 add_filter('query_vars', 'parameter_queryvars' ); 737 add_action('wp_print_styles', 'add_transposh_css'); 738 add_action('wp_print_scripts', 'add_transposh_js'); 784 739 785 740 add_action('init', 'on_init');
Note: See TracChangeset
for help on using the changeset viewer.
