Transposh WordPress Translation - Version 0.1.1

Version Description

Download this release

Release Info

Developer oferwald
Plugin Icon 128x128 Transposh WordPress Translation
Version 0.1.1
Comparing to
See all releases

Code changes from version 0.1.0 to 0.1.1

Files changed (6) hide show
  1. js/transposh.js +32 -18
  2. parser.php +26 -3
  3. readme.txt +7 -2
  4. transposh.css +6 -0
  5. transposh.php +424 -468
  6. transposh_widget.php +1 -2
js/transposh.js CHANGED
@@ -49,43 +49,42 @@ function hint(original)
49
  // fetch translation from google translate...
50
  function getgt()
51
  {
52
- google.language.translate($("#tr_original_unescaped").text(), "", transposh_target_lang, function(result) {
53
  if (!result.error) {
54
- $("#tr_translation").val(result.translation);
55
  }
56
  });
57
  }
58
 
59
  //Ajax translation
60
  function ajax_translate(original,translation,source,segment_id) {
61
- var token = $("#tr_" + segment_id).attr('token');
62
  var query = 'token=' + token +
63
  '&translation=' + translation +
64
  '&lang=' + transposh_target_lang +
65
  '&source=' + source +
66
  '&translation_posted=1';
67
 
68
- //$("span:contains("+translation+")").css("text-decoration", "underline");
69
- $.ajax({
70
  type: "POST",
71
  url: transposh_post_url,
72
  data: query,
73
  success: function(req) {
74
- var pre_translated = $("#tr_" + segment_id).html();
75
  var new_text = translation;
76
  //reset to the original content - the unescaped version if translation is empty
77
  if(jQuery.trim(translation).length == 0) {
78
  new_text = original;
79
  }
80
  // rewrite text for all matching items at once
81
- $(".tr_t,.tr_u").filter(function() {return $(this).html() == pre_translated;}).html(new_text)
82
  .each(function (i) { // handle the image changes
83
- var img_segment_id = $(this).attr('id').substr($(this).attr('id').lastIndexOf('_')+1);
84
  //current img
85
- var img = $("#tr_img_" + img_segment_id).attr('src');
86
 
87
  //rewrite onclick function - in case of re-edit
88
- $("#tr_img_" + img_segment_id).click(function () {
89
  translate_dialog(original, translation, img_segment_id);
90
  });
91
 
@@ -105,7 +104,7 @@ function ajax_translate(original,translation,source,segment_id) {
105
  }
106
  }
107
  //rewrite image
108
- $("#tr_img_" + img_segment_id).attr('src', img);
109
 
110
  });
111
 
@@ -140,8 +139,8 @@ var dialog = ''+
140
  display_dialog(caption, dialog);
141
 
142
  // attach handler to form's submit event
143
- $('#tr_form').submit(function() {
144
- var translation = $('#tr_translation').val();
145
 
146
  ajax_translate(original,translation,0,segment_id);
147
 
@@ -154,14 +153,29 @@ var dialog = ''+
154
  //function for auto translation
155
 
156
  function do_auto_translate() {
157
- $(".tr_u").each(function (i) {
158
- var translated_id = $(this).attr('id');
159
- google.language.translate($(this).text(), "", transposh_target_lang, function(result) {
160
  if (!result.error) {
161
  var segment_id = translated_id.substr(translated_id.lastIndexOf('_')+1);
162
- ajax_translate($("#"+translated_id).text(),result.translation,1,segment_id);
163
- $("#"+translated_id).addClass("tr_t").removeClass("tr_u");
164
  }
165
  });
166
  });
167
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  // fetch translation from google translate...
50
  function getgt()
51
  {
52
+ google.language.translate(jQuery("#tr_original_unescaped").text(), "", transposh_target_lang, function(result) {
53
  if (!result.error) {
54
+ jQuery("#tr_translation").val(result.translation);
55
  }
56
  });
57
  }
58
 
59
  //Ajax translation
60
  function ajax_translate(original,translation,source,segment_id) {
61
+ var token = jQuery("#tr_" + segment_id).attr('token');
62
  var query = 'token=' + token +
63
  '&translation=' + translation +
64
  '&lang=' + transposh_target_lang +
65
  '&source=' + source +
66
  '&translation_posted=1';
67
 
68
+ jQuery.ajax({
 
69
  type: "POST",
70
  url: transposh_post_url,
71
  data: query,
72
  success: function(req) {
73
+ var pre_translated = jQuery("#tr_" + segment_id).html();
74
  var new_text = translation;
75
  //reset to the original content - the unescaped version if translation is empty
76
  if(jQuery.trim(translation).length == 0) {
77
  new_text = original;
78
  }
79
  // rewrite text for all matching items at once
80
+ jQuery(".tr_t,.tr_u").filter(function() {return jQuery(this).html() == pre_translated;}).html(new_text)
81
  .each(function (i) { // handle the image changes
82
+ var img_segment_id = jQuery(this).attr('id').substr(jQuery(this).attr('id').lastIndexOf('_')+1);
83
  //current img
84
+ var img = jQuery("#tr_img_" + img_segment_id).attr('src');
85
 
86
  //rewrite onclick function - in case of re-edit
87
+ jQuery("#tr_img_" + img_segment_id).click(function () {
88
  translate_dialog(original, translation, img_segment_id);
89
  });
90
 
104
  }
105
  }
106
  //rewrite image
107
+ jQuery("#tr_img_" + img_segment_id).attr('src', img);
108
 
109
  });
110
 
139
  display_dialog(caption, dialog);
140
 
141
  // attach handler to form's submit event
142
+ jQuery('#tr_form').submit(function() {
143
+ var translation = jQuery('#tr_translation').val();
144
 
145
  ajax_translate(original,translation,0,segment_id);
146
 
153
  //function for auto translation
154
 
155
  function do_auto_translate() {
156
+ jQuery(".tr_u").each(function (i) {
157
+ var translated_id = jQuery(this).attr('id');
158
+ google.language.translate(jQuery(this).text(), "", transposh_target_lang, function(result) {
159
  if (!result.error) {
160
  var segment_id = translated_id.substr(translated_id.lastIndexOf('_')+1);
161
+ ajax_translate(jQuery("#"+translated_id).text(),result.translation,1,segment_id);
162
+ jQuery("#"+translated_id).addClass("tr_t").removeClass("tr_u");
163
  }
164
  });
165
  });
166
  }
167
+
168
+ //to run at start
169
+ jQuery.noConflict();
170
+ var transposh_post_url,transposh_target_lang;
171
+ jQuery("script[src*='transposh.js']").each(function (i) {
172
+ transposh_post_url = unescape(this.src.match('post_url=(.*?)&')[1]);
173
+ transposh_target_lang = this.src.match('lang=(.*?)&')[1];
174
+ });
175
+ google.load("language", "1");
176
+ jQuery(document).ready(
177
+ function() {
178
+ do_auto_translate();
179
+ }
180
+ );
181
+
parser.php CHANGED
@@ -568,11 +568,18 @@ function is_html_entity($position, &$is_breaker)
568
  $entity = substr($page, $position, $end_pos - $position + 1);
569
 
570
  //Don't break on ` so for our use we don't consider it an entity
571
- //e.g. Jack`s apple
572
- if(!($entity == "’" || $entity == "'" || $entity == "'"))
 
 
 
 
 
 
573
  {
574
  $is_breaker = TRUE;
575
  }
 
576
 
577
  //It is an html entity.
578
  return $end_pos + 1;
@@ -786,7 +793,7 @@ function insert_translation(&$original_text, &$translated_text, $source, $start,
786
 
787
  //Use base64 encoding to make that when the page is translated (i.e. update_translation) we
788
  //get back exactlly the same string without having the client decode/encode it in anyway.
789
- $token = "token=\"" . base64_encode($original_text) . "\"";
790
 
791
  if($translated_text == NULL)
792
  {
@@ -875,4 +882,20 @@ function update_translated_page($start, $end, $translated_text)
875
  }
876
 
877
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
878
  ?>
568
  $entity = substr($page, $position, $end_pos - $position + 1);
569
 
570
  //Don't break on ` so for our use we don't consider it an entity
571
+ //e.g. Jack`s apple.
572
+ //Exception: don't break when we there is a white space after the apostrophe. e.g. `uncategorized`
573
+ if(($entity == "’" || $entity == "'" || $entity == "'")
574
+ && $page[$end_pos + 1] != " ")
575
+ {
576
+ $is_breaker = FALSE;
577
+ }
578
+ else
579
  {
580
  $is_breaker = TRUE;
581
  }
582
+
583
 
584
  //It is an html entity.
585
  return $end_pos + 1;
793
 
794
  //Use base64 encoding to make that when the page is translated (i.e. update_translation) we
795
  //get back exactlly the same string without having the client decode/encode it in anyway.
796
+ $token = "token=\"" . base64_url_encode($original_text) . "\"";
797
 
798
  if($translated_text == NULL)
799
  {
882
  }
883
 
884
  }
885
+
886
+ /**
887
+ * Encode a string as base 64 while avoiding characters which should be avoided
888
+ * in uri, e.g. + is interpeted as a space.
889
+ */
890
+ function base64_url_encode($input) {
891
+ return strtr(base64_encode($input), '+/=', '-_,');
892
+ }
893
+
894
+ /**
895
+ * Decode a string previously decoded with base64_url_encode
896
+ */
897
+ function base64_url_decode($input) {
898
+ return base64_decode(strtr($input, '-_,', '+/='));
899
+ }
900
+
901
  ?>
readme.txt CHANGED
@@ -2,9 +2,9 @@
2
  Contributors: oferwald, amirperlman
3
  Donate link: http://transposh.org/
4
  Tags: translation, widget, filter, bilingual, multilingual, transposh, language, RTL, Hebrew, Spanish, French, Russian, English, Arabic, crowdsourcing, context, wiki
5
- Requires at least: 2.5
6
  Tested up to: 2.7.1
7
- Stable tag: 0.1.0
8
 
9
  Transposh filter allows in context quick translation of websites, it allows you to crowd-source the translation to your users
10
 
@@ -80,6 +80,11 @@ harnessed to get your message out to more people. Future versions will give more
80
 
81
  == Release notes ==
82
 
 
 
 
 
 
83
  * 2009/03/22 - 0.1.0
84
  * Enabled automatic translation for site readers
85
  * Added many languages to the default list
2
  Contributors: oferwald, amirperlman
3
  Donate link: http://transposh.org/
4
  Tags: translation, widget, filter, bilingual, multilingual, transposh, language, RTL, Hebrew, Spanish, French, Russian, English, Arabic, crowdsourcing, context, wiki
5
+ Requires at least: 2.7
6
  Tested up to: 2.7.1
7
+ Stable tag: 0.1.1
8
 
9
  Transposh filter allows in context quick translation of websites, it allows you to crowd-source the translation to your users
10
 
80
 
81
  == Release notes ==
82
 
83
+ * 2009/03/24 - 0.1.1
84
+ * Fixed compatability issues with other scripts (thanks [Eike](http://spotterblog.de/))
85
+ * Fixed minor issues with encoding some strings
86
+ * Verify UTF charset and collation upon database creation
87
+ * Some CSS improvments
88
  * 2009/03/22 - 0.1.0
89
  * Enabled automatic translation for site readers
90
  * Added many languages to the default list
transposh.css CHANGED
@@ -27,6 +27,12 @@
27
  #overDiv table tr:hover {
28
  background-color:#225588;}
29
 
 
 
 
 
 
 
30
  /*
31
  * Define our styling
32
  */
27
  #overDiv table tr:hover {
28
  background-color:#225588;}
29
 
30
+ .post .postbody .tr_img_, .attachment .tr_img_, .navigation .alignleft .tr_img_, .navigation .alignright .tr_img_,
31
+ .tr_img_ {
32
+ border: 0px;
33
+ margin: 0px;
34
+ }
35
+
36
  /*
37
  * Define our styling
38
  */
transposh.php CHANGED
@@ -4,7 +4,7 @@
4
  Plugin URI: http://transposh.org/#
5
  Description: Translation filter for WordPress, After enabling please set languages at the <a href="options-general.php?page=Transposh">the options page</a> Want to help? visit our development site at <a href="http://trac.transposh.org/">trac.transposh.org</a>.
6
  Author: Team Transposh
7
- Version: 0.1.0
8
  Author URI: http://transposh.org/
9
  License: GPL (http://www.gnu.org/licenses/gpl.txt)
10
  */
@@ -65,109 +65,101 @@ $plugin_url;
65
  //Error message displayed for the admin in case of failure
66
  $admin_msg;
67
 
68
-
69
  /*
70
  * Called when the buffer containing the original page is flused. Triggers the
71
  * translation process.
72
- *
73
  */
74
  function process_page(&$buffer) {
75
 
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
-
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
-
116
 
117
- //translate the entire page
118
- process_html();
119
 
120
- $end_time = microtime(TRUE);
121
 
122
-
123
 
124
- //return the translated page unless it is empty, othewise return the original
125
- return (strlen($tr_page) > 0 ? $tr_page : $page);
126
  }
127
 
128
  /*
129
  * Fix links on the page. href needs to be modified to include
130
  * lang specifier and editing mode.
131
- *
132
  */
133
  function process_anchor_tag($start, $end)
134
  {
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
-
169
- }
170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
 
172
  /*
173
  * Update the given url to include language params.
@@ -178,212 +170,134 @@ function process_anchor_tag($start, $end)
178
  */
179
  function rewrite_url_lang_param($url, $lang, $is_edit, $use_params_only)
180
  {
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 permalinks
186
- $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
- else
200
- {
201
- $url = preg_replace("/$home_url_quoted\/(..\/)?\/?/",
202
  "$home_url/$lang/", $url);
203
- }
204
 
205
- if($params)
206
- {
207
- //insert params to url
208
- $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;
218
  }
219
 
220
  /*
221
  * 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.
224
  */
225
  function fetch_translation($original)
226
  {
227
- global $wpdb, $lang, $table_name;
228
- $translated = NULL;
229
-
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
-
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
-
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
-
270
- }
271
- }
272
-
273
-
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\" src=\"http://www.google.com/jsapi\"></script>";
314
- $js .= "\n<script type=\"text/javascript\">google.load(\"language\", \"1\");</script>";
315
-
316
-
317
- $post_url = $home_url . '/index.php';
318
- $js .= "\n<script type=\"text/javascript\">var transposh_post_url='$post_url';var transposh_target_lang='$lang';</script>";
319
-
320
- $js .= "\n<script type=\"text/javascript\">$(document).ready(function() {do_auto_translate();});</script>";
321
- }
322
-
323
- echo $js . "\n";
324
- }
325
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
326
 
327
  /*
328
  * Return the img tag that will added to enable editing a translatable
329
  * item on the page.
330
  * param segement_id The id (number) identifying this segment. Needs to be
331
- placed within the img tag for use on client side operation (jquery)
332
  */
333
  function get_img_tag($original, $translation, $source, $segment_id, $is_translated = FALSE)
334
  {
335
- global $plugin_url, $lang, $home_url;
336
- $url = $home_url . '/index.php';
337
-
338
- //For use in javascript, make the following changes:
339
- //1. Add slashes to escape the inner text
340
- //2. Convert the html special characters
341
- //The browser will take decode step 2 and pass it to the js engine which decode step 1 - a bit tricky
342
- $translation = htmlspecialchars(addslashes($translation));
343
- $original = htmlspecialchars(addslashes($original));
344
-
345
- if ($is_translated)
346
- {
347
- $add_img = "_fix";
348
- }
349
-
350
- if ($source == 1) {
351
- $add_img = "_auto";
352
- }
353
-
354
- $img = "<img src=\"$plugin_url/translate$add_img.png\" alt=\"translate\" id=\"" . IMG_PREFIX . "$segment_id\"
355
- onclick=\"translate_dialog('$original','$translation','$segment_id'); return false;\"
356
- onmouseover=\"hint('$original'); return true;\"
357
- onmouseout=\"nd()\" />";
358
-
359
- return $img;
360
- }
361
-
362
-
363
- /*
364
- * Add custom css, i.e. transposh.css
365
- *
366
- */
367
- function add_custom_css()
368
- {
369
- transposh_css();
370
- insert_javascript_includes();
371
- }
372
-
373
- // We need some CSS to position the paragraph
374
- function transposh_css()
375
- {
376
- global $plugin_url, $wp_query;
377
 
378
- if (!isset($wp_query->query_vars[LANG_PARAM]))
379
- {
380
- return;
381
- }
382
 
383
- //include the transposh.css
384
- echo "<link rel=\"stylesheet\" href=\"$plugin_url/transposh.css\" type=\"text/css\" />";
 
 
385
 
386
-
387
  }
388
 
389
  /*
@@ -391,82 +305,79 @@ function transposh_css()
391
  */
392
  function init_global_vars()
393
  {
394
- global $home_url, $home_url_quoted, $plugin_url, $table_name, $wpdb, $enable_auto_translate;
395
 
396
- $home_url = get_option('home');
397
- $local_dir = preg_replace("/.*\//", "", dirname(__FILE__));
398
 
399
- $plugin_url= $home_url . "/wp-content/plugins/$local_dir";
400
- $home_url_quoted = preg_quote($home_url);
401
- $home_url_quoted = preg_replace("/\//", "\\/", $home_url_quoted);
402
 
403
- $table_name = $wpdb->prefix . TRANSLATIONS_TABLE;
404
- $enable_auto_translate = get_option(ENABLE_AUTO_TRANSLATE,1) && is_translator();
405
  }
406
 
407
  /*
408
  * A new translation has been posted, update the translation database.
409
- *
410
  */
411
  function update_translation()
412
  {
413
- global $wpdb, $table_name;
414
-
415
- $ref=getenv('HTTP_REFERER');
416
- $original = base64_decode($_POST['token'], TRUE);
417
- $translation = $_POST['translation'];
418
- $lang = $_POST['lang'];
419
- $source = $_POST['source'];
420
-
421
- if(!isset($original) || !isset($translation) || !isset($lang))
422
- {
423
-
424
- return;
425
- }
426
-
427
- //Check that use is allowed to translate
428
- if(!is_translator())
429
- {
430
-
431
- }
432
-
433
- //Decode & remove already escaped character to avoid double escaping
434
- $translation = $wpdb->escape(htmlspecialchars(stripslashes(urldecode($translation))));
435
-
436
- //The original content is encoded as base64 before it is sent (i.e. token), after we
437
- //decode it should just the same after it was parsed.
438
- $original = $wpdb->escape(html_entity_decode($original, ENT_NOQUOTES, 'UTF-8'));
439
-
440
- $update = "REPLACE INTO $table_name (original, translated, lang, source)
441
  VALUES ('" . $original . "','" . $translation . "','" . $lang . "','" . $source . "')";
442
 
443
- $result = $wpdb->query($update);
444
 
445
- if($result !== FALSE)
446
- {
447
- update_transaction_log($original, $translation, $lang, $source);
448
 
449
- //Delete entry from cache
450
- if(ENABLE_APC && function_exists('apc_store'))
451
- {
452
- apc_delete($original .'___'. $lang);
453
- }
454
 
455
-
456
- }
457
- else
458
- {
459
-
460
- header("HTTP/1.0 404 Failed to update language database");
461
- }
462
 
463
- exit;
464
  }
465
 
466
-
467
  /*
468
  * Update the transaction log
469
- *
470
  */
471
  function update_transaction_log(&$original, &$translation, &$lang, $source)
472
  {
@@ -475,24 +386,24 @@ function update_transaction_log(&$original, &$translation, &$lang, $source)
475
 
476
  // log either the user ID or his IP
477
  if ('' == $user_ID)
478
- {
479
  $loguser = $_SERVER['REMOTE_ADDR'];
480
  }
481
- else
482
- {
483
  $loguser = $user_ID;
484
  }
485
 
486
- $log = "INSERT INTO ".$wpdb->prefix.TRANSLATIONS_LOG." (original, translated, lang, translated_by, source)
487
- VALUES ('" . $original . "','" . $translation . "','" . $lang . "','".$loguser."','".$source."')";
488
 
489
- $result = $wpdb->query($log);
490
 
491
- if($result === FALSE)
492
- {
493
-
494
-
495
- }
496
  }
497
 
498
  /*
@@ -502,15 +413,15 @@ function update_transaction_log(&$original, &$translation, &$lang, $source)
502
  */
503
  function get_default_lang()
504
  {
505
- global $languages;
506
 
507
- $default = get_option(DEFAULT_LANG);
508
- if(!$languages[$default])
509
- {
510
- $default = "en";
511
- }
512
 
513
- return $default;
514
  }
515
 
516
  /*
@@ -519,18 +430,18 @@ function get_default_lang()
519
  */
520
  function on_init()
521
  {
522
-
523
- init_global_vars();
524
 
525
- if ($_POST['translation_posted'])
526
- {
527
- update_translation();
528
- }
529
- else
530
- {
531
- //set the callback for translating the page when it's done
532
- ob_start("process_page");
533
- }
 
 
534
  }
535
 
536
  /*
@@ -538,57 +449,56 @@ function on_init()
538
  */
539
  function on_shutdown()
540
  {
541
- ob_flush();
542
  }
543
 
544
  /*
545
  * Update the url rewrite rules to include language identifier
546
- *
547
  */
548
  function update_rewrite_rules($rules){
549
-
550
 
551
- if(!get_option(ENABLE_PERMALINKS_REWRITE))
552
- {
553
-
554
- return $rules;
555
- }
556
 
557
- $newRules = array();
558
- $lang_prefix="([a-z]{2,2}(\-[a-z]{2,2})?)/";
559
 
560
- $lang_parameter= "&" . LANG_PARAM . '=$matches[1]';
561
 
562
- //catch the root url
563
- $newRules[$lang_prefix."?$"] = "index.php?lang=\$matches[1]";
564
-
565
 
566
- foreach ($rules as $key=>$value) {
567
- $original_key = $key;
568
- $original_value = $value;
569
 
570
- $key = $lang_prefix . $key;
571
 
572
- //Shift existing matches[i] two step forward as we pushed new elements
573
- //in the beginning of the expression
574
- for($i = 6; $i > 0; $i--)
575
- {
576
- $value = str_replace('['. $i .']', '['. ($i + 2) .']', $value);
577
- }
578
 
579
- $value .= $lang_parameter;
580
 
581
-
582
 
583
 
584
- $newRules[$key] = $value;
585
- $newRules[$original_key] = $original_value;
586
 
587
-
588
- }
589
 
590
-
591
- return $newRules;
592
  }
593
 
594
  /*
@@ -596,55 +506,52 @@ function update_rewrite_rules($rules){
596
  */
597
  function parameter_queryvars($qvars)
598
  {
599
- $qvars[] = LANG_PARAM;
600
- $qvars[] = EDIT_PARAM;
601
 
602
- return $qvars;
603
  }
604
 
605
  /*
606
  * Setup the translation database.
607
- *
608
  */
609
  function setup_db()
610
  {
611
-
612
  global $wpdb;
613
- require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
614
-
615
- $installed_ver = get_option(TRANSPOSH_DB_VERSION);
616
-
617
- if( $installed_ver != DB_VERSION ) {
618
- $table_name = $wpdb->prefix . TRANSLATIONS_TABLE;
619
 
620
-
621
- $sql = "CREATE TABLE $table_name (original VARCHAR(256) NOT NULL,
622
- lang CHAR(5) NOT NULL,
623
- translated VARCHAR(256),
624
- source TINYINT NOT NULL,
625
- PRIMARY KEY (original, lang)) ";
626
 
 
 
627
 
628
- dbDelta($sql);
 
 
 
 
 
629
 
 
630
 
631
- $table_name = $wpdb->prefix . TRANSLATIONS_LOG;
632
 
633
-
634
- $sql = "CREATE TABLE " . $table_name . " (original VARCHAR(256) NOT NULL,
635
- lang CHAR(5) NOT NULL,
636
- translated VARCHAR(256),
637
- translated_by VARCHAR(15),
638
- source TINYINT NOT NULL,
639
- timestamp TIMESTAMP,
640
- PRIMARY KEY (original, lang, timestamp)) ";
641
 
 
 
 
 
 
 
 
 
642
 
643
- dbDelta($sql);
644
  update_option(TRANSPOSH_DB_VERSION, DB_VERSION);
645
- }
646
 
647
-
648
  }
649
 
650
  /*
@@ -653,111 +560,103 @@ function setup_db()
653
  */
654
  function is_translator()
655
  {
656
- if(is_user_logged_in())
657
- {
658
- if(current_user_can(TRANSLATOR))
659
- {
660
- return TRUE;
661
- }
662
- }
663
-
664
- if(get_option(ANONYMOUS_TRANSLATION))
665
- {
666
- //if anonymous translation is allowed - let anyone enjoy it
667
- return TRUE;
668
- }
669
-
670
- return FALSE;
671
- }
672
 
 
 
 
 
 
 
 
 
673
 
674
  /*
675
  * Plugin activated.
676
- *
677
  */
678
  function plugin_activate()
679
  {
680
- global $wp_rewrite;
681
-
682
 
683
- setup_db();
684
 
685
- add_filter('rewrite_rules_array', 'update_rewrite_rules');
686
- $wp_rewrite->flush_rules();
687
 
688
-
689
  }
690
 
691
-
692
  /*
693
  * Plugin deactivated.
694
- *
695
  */
696
  function plugin_deactivate(){
697
- global $wp_rewrite;
698
-
699
 
700
- remove_filter('rewrite_rules_array', 'update_rewrite_rules');
701
- $wp_rewrite->flush_rules();
702
 
703
-
704
  }
705
 
706
  /*
707
  * Callback from admin_notices - display error message to the admin.
708
- *
709
  */
710
  function plugin_install_error()
711
  {
712
- global $admin_msg;
713
-
714
 
715
- echo '<div class="updated"><p>';
716
- echo 'Error has occured in the installation process of the translation plugin: <br>';
717
 
718
- echo $admin_msg;
719
 
720
- if (function_exists('deactivate_plugins') ) {
721
- deactivate_plugins(get_plugin_name(), "translate.php");
722
- echo '<br> This plugin has been automatically deactivated.';
723
- }
724
 
725
- echo '</p></div>';
726
  }
727
 
728
-
729
  /*
730
  * Callback when all plugins have been loaded. Serves as the location
731
  * to check that the plugin loaded successfully else trigger notification
732
  * to the admin and deactivate plugin.
733
- *
734
  */
735
  function plugin_loaded()
736
  {
737
- global $admin_msg;
738
-
739
-
740
- $db_version = get_option(TRANSPOSH_DB_VERSION);
741
 
742
- if ($db_version != DB_VERSION)
743
- {
744
- setup_db();
745
- //$admin_msg = "Translation database version ($db_version) is not comptabile with this plugin (". DB_VERSION . ") <br>";
746
 
747
-
748
- //Some error occured - notify admin and deactivate plugin
749
- //add_action('admin_notices', 'plugin_install_error');
750
- }
751
 
752
- if ($db_version != DB_VERSION)
753
- {
754
- $admin_msg = "Failed to locate the translation table <em> " . TRANSLATIONS_TABLE . "</em> in local database. <br>";
 
755
 
756
-
757
- //Some error occured - notify admin and deactivate plugin
758
- add_action('admin_notices', 'plugin_install_error');
759
- }
760
 
 
 
 
 
761
  }
762
 
763
  /*
@@ -777,9 +676,66 @@ function get_plugin_name()
777
  return $file;
778
  }
779
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
780
  //Register callbacks
781
- add_action('wp_head', 'add_custom_css');
782
  add_filter('query_vars', 'parameter_queryvars' );
 
 
783
 
784
  add_action('init', 'on_init');
785
  add_action('shutdown', 'on_shutdown');
4
  Plugin URI: http://transposh.org/#
5
  Description: Translation filter for WordPress, After enabling please set languages at the <a href="options-general.php?page=Transposh">the options page</a> Want to help? visit our development site at <a href="http://trac.transposh.org/">trac.transposh.org</a>.
6
  Author: Team Transposh
7
+ Version: 0.1.1
8
  Author URI: http://transposh.org/
9
  License: GPL (http://www.gnu.org/licenses/gpl.txt)
10
  */
65
  //Error message displayed for the admin in case of failure
66
  $admin_msg;
67
 
 
68
  /*
69
  * Called when the buffer containing the original page is flused. Triggers the
70
  * translation process.
 
71
  */
72
  function process_page(&$buffer) {
73
 
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
+
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
+
110
 
111
+ //translate the entire page
112
+ process_html();
113
 
114
+ $end_time = microtime(TRUE);
115
 
116
+
117
 
118
+ //return the translated page unless it is empty, othewise return the original
119
+ return (strlen($tr_page) > 0 ? $tr_page : $page);
120
  }
121
 
122
  /*
123
  * Fix links on the page. href needs to be modified to include
124
  * lang specifier and editing mode.
 
125
  */
126
  function process_anchor_tag($start, $end)
127
  {
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
+
162
+ }
163
 
164
  /*
165
  * Update the given url to include language params.
170
  */
171
  function rewrite_url_lang_param($url, $lang, $is_edit, $use_params_only)
172
  {
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\/(..(-..)?\/)?\/?/",
194
  "$home_url/$lang/", $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;
210
  }
211
 
212
  /*
213
  * Fetch translation from db or cache.
214
+ * Returns An array that contains the translated string and it source.
215
+ * Will return NULL if no translation is available.
216
  */
217
  function fetch_translation($original)
218
  {
219
+ global $wpdb, $lang, $table_name;
220
+ $translated = NULL;
221
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
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
+
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
+
261
+ }
262
+ }
263
+
264
+
265
+ return $translated;
266
+ }
267
 
268
  /*
269
  * Return the img tag that will added to enable editing a translatable
270
  * item on the page.
271
  * param segement_id The id (number) identifying this segment. Needs to be
272
+ * placed within the img tag for use on client side operation (jquery)
273
  */
274
  function get_img_tag($original, $translation, $source, $segment_id, $is_translated = FALSE)
275
  {
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\" class=\"".IMG_PREFIX."\" 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
 
300
+ return $img;
301
  }
302
 
303
  /*
305
  */
306
  function init_global_vars()
307
  {
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();
319
  }
320
 
321
  /*
322
  * A new translation has been posted, update the translation database.
 
323
  */
324
  function update_translation()
325
  {
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
+
337
+ return;
338
+ }
339
+
340
+ //Check that use is allowed to translate
341
+ if(!is_translator())
342
+ {
343
+
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)
354
  VALUES ('" . $original . "','" . $translation . "','" . $lang . "','" . $source . "')";
355
 
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
+
369
+ }
370
+ else
371
+ {
372
+
373
+ header("HTTP/1.0 404 Failed to update language database");
374
+ }
375
 
376
+ exit;
377
  }
378
 
 
379
  /*
380
  * Update the transaction log
 
381
  */
382
  function update_transaction_log(&$original, &$translation, &$lang, $source)
383
  {
386
 
387
  // log either the user ID or his IP
388
  if ('' == $user_ID)
389
+ {
390
  $loguser = $_SERVER['REMOTE_ADDR'];
391
  }
392
+ else
393
+ {
394
  $loguser = $user_ID;
395
  }
396
 
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
+
405
+
406
+ }
407
  }
408
 
409
  /*
413
  */
414
  function get_default_lang()
415
  {
416
+ global $languages;
417
 
418
+ $default = get_option(DEFAULT_LANG);
419
+ if(!$languages[$default])
420
+ {
421
+ $default = "en";
422
+ }
423
 
424
+ return $default;
425
  }
426
 
427
  /*
430
  */
431
  function on_init()
432
  {
 
 
433
 
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
+ }
445
  }
446
 
447
  /*
449
  */
450
  function on_shutdown()
451
  {
452
+ ob_flush();
453
  }
454
 
455
  /*
456
  * Update the url rewrite rules to include language identifier
 
457
  */
458
  function update_rewrite_rules($rules){
459
+
460
 
461
+ if(!get_option(ENABLE_PERMALINKS_REWRITE))
462
+ {
463
+
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
+
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
+
492
 
493
 
494
+ $newRules[$key] = $value;
495
+ $newRules[$original_key] = $original_value;
496
 
497
+
498
+ }
499
 
500
+
501
+ return $newRules;
502
  }
503
 
504
  /*
506
  */
507
  function parameter_queryvars($qvars)
508
  {
509
+ $qvars[] = LANG_PARAM;
510
+ $qvars[] = EDIT_PARAM;
511
 
512
+ return $qvars;
513
  }
514
 
515
  /*
516
  * Setup the translation database.
 
517
  */
518
  function setup_db()
519
  {
520
+
521
  global $wpdb;
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
+
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)) DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci";
535
 
536
+ dbDelta($sql);
537
 
 
538
 
539
+ $table_name = $wpdb->prefix . TRANSLATIONS_LOG;
 
 
 
 
 
 
 
540
 
541
+
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)) DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci";
549
 
550
+ dbDelta($sql);
551
  update_option(TRANSPOSH_DB_VERSION, DB_VERSION);
552
+ }
553
 
554
+
555
  }
556
 
557
  /*
560
  */
561
  function is_translator()
562
  {
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
+ }
579
 
580
  /*
581
  * Plugin activated.
 
582
  */
583
  function plugin_activate()
584
  {
585
+ global $wp_rewrite;
586
+
587
 
588
+ setup_db();
589
 
590
+ add_filter('rewrite_rules_array', 'update_rewrite_rules');
591
+ $wp_rewrite->flush_rules();
592
 
593
+
594
  }
595
 
 
596
  /*
597
  * Plugin deactivated.
 
598
  */
599
  function plugin_deactivate(){
600
+ global $wp_rewrite;
601
+
602
 
603
+ remove_filter('rewrite_rules_array', 'update_rewrite_rules');
604
+ $wp_rewrite->flush_rules();
605
 
606
+
607
  }
608
 
609
  /*
610
  * Callback from admin_notices - display error message to the admin.
 
611
  */
612
  function plugin_install_error()
613
  {
614
+ global $admin_msg;
615
+
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
  }
629
 
 
630
  /*
631
  * Callback when all plugins have been loaded. Serves as the location
632
  * to check that the plugin loaded successfully else trigger notification
633
  * to the admin and deactivate plugin.
 
634
  */
635
  function plugin_loaded()
636
  {
637
+ global $admin_msg;
638
+
 
 
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
+
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
+
657
+ //Some error occured - notify admin and deactivate plugin
658
+ add_action('admin_notices', 'plugin_install_error');
659
+ }
660
  }
661
 
662
  /*
676
  return $file;
677
  }
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.1');
690
+
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
+
735
  //Register callbacks
 
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');
739
 
740
  add_action('init', 'on_init');
741
  add_action('shutdown', 'on_shutdown');
transposh_widget.php CHANGED
@@ -82,7 +82,7 @@ function transposh_widget_init()
82
  function transposh_widget($args)
83
  {
84
 
85
- global $languages, $wp_query, $wp_rewrite;
86
  extract($args);
87
 
88
  $page_url = ($_SERVER['HTTPS'] == 'on' ?
@@ -108,7 +108,6 @@ function transposh_widget($args)
108
  //keep the flags in the same direction regardless of the overall page direction
109
  echo "<div style=\"text-align: left;\" class=\"" . NO_TRANSLATE_CLASS . "\" >";
110
 
111
- global $plugin_url;
112
  $using_permalinks = $wp_rewrite->using_permalinks();
113
 
114
  foreach($languages as $code => $lang2)
82
  function transposh_widget($args)
83
  {
84
 
85
+ global $languages, $wp_query, $wp_rewrite, $plugin_url;
86
  extract($args);
87
 
88
  $page_url = ($_SERVER['HTTPS'] == 'on' ?
108
  //keep the flags in the same direction regardless of the overall page direction
109
  echo "<div style=\"text-align: left;\" class=\"" . NO_TRANSLATE_CLASS . "\" >";
110
 
 
111
  $using_permalinks = $wp_rewrite->using_permalinks();
112
 
113
  foreach($languages as $code => $lang2)