Changeset 57 for trunk/WordPress/plugin/transposh/transposh.php
- Timestamp:
- 03/03/2009 10:34:39 AM (3 years ago)
- File:
-
- 1 edited
-
trunk/WordPress/plugin/transposh/transposh.php (modified) (44 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/WordPress/plugin/transposh/transposh.php
r55 r57 62 62 //The url pointing to the base of the plugin 63 63 $plugin_url; 64 64 65 65 //Error message displayed for the admin in case of failure 66 66 $admin_msg; … … 70 70 * Called when the buffer containing the original page is flused. Triggers the 71 71 * translation process. 72 * 73 */ 72 * 73 */ 74 74 function process_page(&$buffer) { 75 75 76 76 global $wp_query, $tr_page, $page, $pos, $lang, $plugin_url, $is_edit_mode, $wpdb, 77 77 $table_name; 78 78 79 79 $start_time = microtime(TRUE); 80 80 81 81 if (!isset($wp_query->query_vars[LANG_PARAM])) 82 82 { 83 83 //No language code - avoid further processing. 84 84 return $buffer; 85 85 86 86 } 87 87 … … 94 94 return $buffer; 95 95 } 96 97 96 97 98 98 $page = $buffer; 99 99 … … 105 105 //user has the required permissions 106 106 $editable_langs = get_option(EDITABLE_LANGS); 107 107 108 108 if(is_translator() && strstr($editable_langs, $lang)) 109 109 { 110 110 $is_edit_mode = TRUE; 111 111 } 112 113 } 114 112 113 } 114 115 115 logger("translating " . $_SERVER['REQUEST_URI'] . " to: $lang", 1); 116 116 117 117 //translate the entire page 118 118 process_html(); 119 119 120 120 $end_time = microtime(TRUE); 121 121 122 122 logger("Translation completed in " . ($end_time - $start_time) . " seconds", 1); 123 123 124 124 //return the translated page unless it is empty, othewise return the original 125 125 return (strlen($tr_page) > 0 ? $tr_page : $page); … … 128 128 /* 129 129 * Fix links on the page. href needs to be modified to include 130 * lang specifier and editing mode. 130 * lang specifier and editing mode. 131 131 * 132 132 */ … … 134 134 { 135 135 global $home_url, $home_url_quoted, $lang, $is_edit_mode, $wp_rewrite; 136 136 137 137 $href = get_attribute($start, $end, 'href'); 138 138 … … 149 149 150 150 $use_params = FALSE; 151 152 //Only use params if permalinks are not enabled. 151 152 //Only use params if permalinks are not enabled. 153 153 //don't fix links pointing to real files as it will cause that the 154 154 //web server will not be able to locate them 155 if(!$wp_rewrite->using_permalinks() || 155 if(!$wp_rewrite->using_permalinks() || 156 156 stripos($href, '/wp-admin') !== FALSE || 157 157 stripos($href, '/wp-content') !== FALSE || … … 171 171 172 172 /* 173 * Update the given url to include language params. 173 * Update the given url to include language params. 174 174 * param url - the original url to rewrite 175 175 * param lang - language code … … 186 186 $use_params_only = TRUE; 187 187 } 188 188 189 189 if($is_edit) 190 190 { 191 191 $params = EDIT_PARAM . '=1&'; 192 192 193 193 } 194 194 … … 210 210 //Cleanup extra & 211 211 $url = preg_replace("/&&+/", "&", $url); 212 212 213 213 //Cleanup extra ? 214 214 $url = preg_replace("/\?\?+/", "?", $url); … … 219 219 220 220 /* 221 * Fetch translation from db or cache. 221 * Fetch translation from db or cache. 222 222 * Returns the translated string or NULL if not available. 223 223 */ … … 226 226 global $wpdb, $lang, $table_name; 227 227 $translated = NULL; 228 229 logger("Enter " . __METHOD__ . " $original", 4);228 229 logger("Enter " . __METHOD__ . ": $original", 4); 230 230 if(ENABLE_APC && function_exists('apc_fetch')) 231 231 { … … 233 233 if($rc === TRUE) 234 234 { 235 return $cached; 236 } 237 } 238 239 $query = "SELECT * FROM $table_name WHERE original = '$original' and lang = '$lang' "; 235 logger("Exit from cache " . __METHOD__ . ": $cached", 4); 236 return $cached; 237 } 238 } 239 240 $query = "SELECT * FROM $table_name WHERE original = '$original' and lang = '$lang' "; 240 241 $row = $wpdb->get_row($query); 241 242 242 243 if($row !== FALSE) 243 244 { 244 245 $translated = $row->translated; 245 246 $translated = stripslashes($translated); 246 247 247 248 logger("db result for $original >>> $translated ($lang)" , 3); 248 249 } 249 250 250 251 251 252 if(ENABLE_APC && function_exists('apc_store')) 252 253 { … … 257 258 $cache_entry = ""; 258 259 } 259 260 260 261 //update cache 261 262 $rc = apc_store($original . $lang, $cache_entry, 3600); … … 265 266 } 266 267 } 267 268 logger("Exit " . __METHOD__ . " $translated", 4);268 269 logger("Exit " . __METHOD__ . ": $translated", 4); 269 270 return $translated; 270 271 } … … 278 279 { 279 280 global $plugin_url; 280 281 281 282 $overlib_dir = "$plugin_url/js/overlibmws"; 282 283 283 284 $js = "\n<script type=\"text/javascript\" src=\"$overlib_dir/overlibmws.js\"></script>"; 284 285 $js .= "\n<script type=\"text/javascript\" src=\"$overlib_dir/overlibmws_filter.js\"></script>"; … … 297 298 /* 298 299 * Return the img tag that will added to enable editing a translatable 299 * item on the page. 300 * item on the page. 300 301 * param segement_id The id (number) identifying this segment. Needs to be 301 302 placed within the img tag for use on client side operation (jquery) … … 305 306 global $plugin_url, $lang, $home_url; 306 307 $url = $home_url . '/index.php'; 307 308 //For use in javascript, make the following changes: 308 309 //For use in javascript, make the following changes: 309 310 //1. Add slashes to escape the inner text 310 311 //2. Convert the html special characters … … 318 319 } 319 320 320 $img = "<img src=\"$plugin_url/translate$add_img.png\" alt=\"translate\" id=\"" . IMG_PREFIX . "$segment_id\" 321 onclick=\"translate_dialog('$original','$translation','$lang','$url', '$segment_id'); return false;\" 322 onmouseover=\"hint('$original'); return true;\" 321 $img = "<img src=\"$plugin_url/translate$add_img.png\" alt=\"translate\" id=\"" . IMG_PREFIX . "$segment_id\" 322 onclick=\"translate_dialog('$original','$translation','$lang','$url', '$segment_id'); return false;\" 323 onmouseover=\"hint('$original'); return true;\" 323 324 onmouseout=\"nd()\" />"; 324 325 325 326 return $img; 326 327 } … … 346 347 return; 347 348 } 348 349 //include the transposh.css 349 350 //include the transposh.css 350 351 echo "<link rel=\"stylesheet\" href=\"$plugin_url/transposh.css\" type=\"text/css\" />"; 351 352 … … 359 360 { 360 361 global $home_url, $home_url_quoted, $plugin_url, $table_name, $wpdb; 361 362 362 363 $home_url = get_option('home'); 363 364 $local_dir = preg_replace("/.*\//", "", dirname(__FILE__)); 364 365 365 366 $plugin_url= $home_url . "/wp-content/plugins/$local_dir"; 366 367 $home_url_quoted = preg_quote($home_url); … … 372 373 373 374 /* 374 * A new translation has been posted, update the translation database. 375 * A new translation has been posted, update the translation database. 375 376 * 376 377 */ … … 378 379 { 379 380 global $wpdb, $table_name; 380 381 381 382 $ref=getenv('HTTP_REFERER'); 382 383 $original = $_POST['original']; 383 384 $translation = $_POST['translation']; 384 385 $lang = $_POST['lang']; 385 386 386 387 if(!isset($original) || !isset($translation) || !isset($lang)) 387 388 { … … 397 398 } 398 399 399 //Decode & remove already escaped character to avoid double escaping 400 //Decode & remove already escaped character to avoid double escaping 400 401 $original = $wpdb->escape(stripslashes(urldecode($original))); 401 402 $translation = $wpdb->escape(htmlspecialchars(stripslashes(urldecode($translation)))); 402 403 403 404 $update = "REPLACE INTO $table_name (original, translated, lang) 404 405 VALUES ('" . $original . "','" . $translation . "','" . $lang . "')"; … … 455 456 logger("Error !!! failed to update transaction log: $loguser, $original ,$translation, $lang" , 0); 456 457 } 457 458 458 459 } 459 460 … … 467 468 { 468 469 global $languages; 469 470 470 471 $default = get_option(DEFAULT_LANG); 471 472 if(!$languages[$default]) … … 500 501 501 502 /* 502 * Page generation completed - flush buffer. 503 * Page generation completed - flush buffer. 503 504 */ 504 505 function on_shutdown() … … 520 521 return $rule; 521 522 } 522 523 523 524 $newRules = array(); 524 525 $lang_prefix="([a-z]{2,2}(\-[a-z]{2,2})?)/"; … … 526 527 $lang_parameter= "&" . LANG_PARAM . '=$matches[1]'; 527 528 528 //catch the root url 529 //catch the root url 529 530 $newRules[$lang_prefix."?$"] = "index.php?lang=\$matches[1]"; 530 531 logger("\t" . $lang_prefix."?$" . " ---> " . "index.php?lang=\$matches[1]"); … … 533 534 $original_key = $key; 534 535 $original_value = $value; 535 536 536 537 $key = $lang_prefix . $key; 537 538 … … 544 545 545 546 $value .= $lang_parameter; 546 547 547 548 logger("\t" . $key . " ---> " . $value); 548 549 549 550 550 551 $newRules[$key] = $value; … … 571 572 572 573 /* 573 * Setup the translation database. 574 * 575 */ 576 function setup_db() 574 * Setup the translation database. 575 * 576 */ 577 function setup_db() 577 578 { 578 579 … … 584 585 if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) 585 586 { 586 logger("Attempting to create table $table_name", 0); 587 logger("Attempting to create table $table_name", 0); 587 588 $sql = "CREATE TABLE " . $table_name . " (original VARCHAR(256) NOT NULL, 588 589 lang CHAR(5) NOT NULL, 589 590 translated VARCHAR(256), 590 591 PRIMARY KEY (original, lang)) "; 591 592 592 593 593 594 dbDelta($sql); 594 595 … … 598 599 599 600 $result = $wpdb->query($insert); 600 601 601 602 if($result === FALSE) 602 603 { 603 logger("Error failed to create $table_name !!!", 0); 604 logger("Error failed to create $table_name !!!", 0); 604 605 } 605 606 else 606 607 { 607 logger("Table $table_name was created successfuly", 0); 608 logger("Table $table_name was created successfuly", 0); 608 609 add_option(TRANSPOSH_DB_VERSION, DB_VERSION); 609 610 } … … 614 615 if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) 615 616 { 616 logger("Attempting to create table $table_name", 0); 617 logger("Attempting to create table $table_name", 0); 617 618 $sql = "CREATE TABLE " . $table_name . " (original VARCHAR(256) NOT NULL, 618 619 lang CHAR(5) NOT NULL, … … 621 622 timestamp TIMESTAMP, 622 623 PRIMARY KEY (original, lang, timestamp)) "; 623 624 624 625 625 626 dbDelta($sql); 626 627 } 627 628 628 629 logger("Exit " . __METHOD__ ); 629 630 } … … 645 646 if(get_option(ANONYMOUS_TRANSLATION)) 646 647 { 647 //if anonymous translation is allowed - let anyone enjoy it 648 //if anonymous translation is allowed - let anyone enjoy it 648 649 return TRUE; 649 650 } 650 651 651 652 return FALSE; 652 653 } 653 654 655 /* 656 * Plugin activated. 654 655 656 /* 657 * Plugin activated. 657 658 * 658 659 */ … … 666 667 add_filter('rewrite_rules_array', 'update_rewrite_rules'); 667 668 $wp_rewrite->flush_rules(); 668 669 669 670 logger("plugin_activate exit: " . dirname(__FILE__)); 670 671 } … … 681 682 remove_filter('rewrite_rules_array', 'update_rewrite_rules'); 682 683 $wp_rewrite->flush_rules(); 683 684 684 685 logger("plugin_deactivate exit: " . dirname(__FILE__)); 685 686 } 686 687 687 688 /* 688 * Callback from admin_notices - display error message to the admin. 689 * Callback from admin_notices - display error message to the admin. 689 690 * 690 691 */ … … 693 694 global $admin_msg; 694 695 logger("Enter " . __METHOD__, 0); 695 696 696 697 echo '<div class="updated"><p>'; 697 698 echo 'Error has occured in the installation process of the translation plugin: <br>'; 698 699 699 700 echo $admin_msg; 700 701 701 702 if (function_exists('deactivate_plugins') ) { 702 703 deactivate_plugins("transposh/translate.php", "translate.php"); … … 711 712 * Callback when all plugins have been loaded. Serves as the location 712 713 * to check that the plugin loaded successfully else trigger notification 713 * to the admin and deactivate plugin. 714 * to the admin and deactivate plugin. 714 715 * 715 716 */ … … 722 723 { 723 724 $admin_msg = "Failed to locate the translation table <em> " . TRANSLATIONS_TABLE . "</em> in local database. <br>"; 724 725 725 726 logger("Messsage to admin: $admin_msg", 0); 726 727 //Some error occured - notify admin and deactivate plugin … … 733 734 { 734 735 $admin_msg = "Translation database version ($db_version) is not comptabile with this plugin (". DB_VERSION . ") <br>"; 735 736 736 737 logger("Messsage to admin: $admin_msg", 0); 737 738 //Some error occured - notify admin and deactivate plugin … … 740 741 } 741 742 742 //Register callbacks 743 //Register callbacks 743 744 add_action('wp_head', 'add_custom_css'); 744 745 add_filter('query_vars', 'parameter_queryvars' );
Note: See TracChangeset
for help on using the changeset viewer.
