Changeset 120


Ignore:
Timestamp:
03/29/2009 05:21:39 PM (3 years ago)
Author:
amir
Message:

Some more refactoring. Any wordpress specific code should reside within files prefixed with transposh_ .
Fixed bug in auto translate - broken on previous checkin.
Added a custom header to the translation posted so we can have some better monitoring.

Location:
trunk/WordPress/plugin/transposh
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/WordPress/plugin/transposh/parser.php

    r107 r120  
    2727require_once("constants.php"); 
    2828require_once("globals.php"); 
     29require_once("utils.php"); 
    2930 
    3031//The html page which starts contains the content being translated 
     
    880881} 
    881882 
    882 /** 
    883  * Encode a string as base 64 while avoiding characters which should be avoided  
    884  * in uri, e.g. + is interpeted as a space. 
    885  */ 
    886 function base64_url_encode($input) { 
    887     return strtr(base64_encode($input), '+/=', '-_,'); 
    888 } 
    889  
    890 /** 
    891  * Decode a string previously decoded with base64_url_encode 
    892  */ 
    893 function base64_url_decode($input) { 
    894     return base64_decode(strtr($input, '-_,', '+/=')); 
     883 
     884/* 
     885 * Fix links on the page. href needs to be modified to include 
     886 * lang specifier and editing mode. 
     887 */ 
     888function process_anchor_tag($start, $end) 
     889{ 
     890        global $home_url, $home_url_quoted, $lang, $is_edit_mode, $enable_permalinks_rewrite; 
     891 
     892        $href = get_attribute($start, $end, 'href'); 
     893 
     894        if($href == NULL) 
     895        { 
     896                return; 
     897        } 
     898 
     899        //Ignore urls not from this site 
     900        if(stripos($href, $home_url) === FALSE) 
     901        { 
     902                return; 
     903        } 
     904 
     905        $use_params = !$enable_permalinks_rewrite; 
     906 
     907        //Allow specific override for url rewriting .  
     908        if($enable_permalinks_rewrite &&  function_exists('is_url_excluded_from_permalink_rewrite') && 
     909           is_url_excluded_from_permalink_rewrite($href)) 
     910        { 
     911                $use_params = TRUE; 
     912        } 
     913         
     914        $href = rewrite_url_lang_param($href, $lang, $is_edit_mode, $use_params); 
     915 
     916        //rewrite url in translated page 
     917        update_translated_page($start, $end, $href); 
     918        logger(__METHOD__ . " $home_url href: $href"); 
     919} 
     920/* 
     921 * Return the img tag that will added to enable editing a translatable 
     922 * item on the page. 
     923 * param segement_id The id (number) identifying this segment. Needs to be 
     924 * placed within the img tag for use on client side operation (jquery) 
     925 */ 
     926function get_img_tag($original, $translation, $source, $segment_id, $is_translated = FALSE) 
     927{ 
     928        global $plugin_url, $lang, $home_url; 
     929        $url = $home_url . '/index.php'; 
     930 
     931        //For use in javascript, make the following changes: 
     932        //1. Add slashes to escape the inner text 
     933        //2. Convert the html special characters 
     934        //The browser will take decode step 2 and pass it to the js engine which decode step 1 - a bit tricky 
     935        $translation = htmlspecialchars(addslashes($translation)); 
     936        $original    = htmlspecialchars(addslashes($original)); 
     937 
     938        if ($is_translated) 
     939        { 
     940                $add_img = "_fix"; 
     941        } 
     942 
     943        if ($source == 1) { 
     944                $add_img = "_auto"; 
     945        } 
     946 
     947        $img = "<img src=\"$plugin_url/translate$add_img.png\" alt=\"translate\" class=\"".IMG_PREFIX."\" id=\"" . IMG_PREFIX . "$segment_id\" ". 
     948           "onclick=\"translate_dialog('$original','$translation','$segment_id'); return false;\" ". 
     949           "onmouseover=\"hint('$original'); return true;\" ". 
     950           "onmouseout=\"nd()\" />"; 
     951 
     952        return $img; 
    895953} 
    896954 
  • trunk/WordPress/plugin/transposh/transposh.php

    r119 r120  
    3030require_once("constants.php"); 
    3131require_once("globals.php"); 
     32require_once("utils.php"); 
    3233require_once("transposh_db.php"); 
    3334require_once("parser.php"); 
     
    6667        $page = $buffer; 
    6768 
    68         if (($wp_query->query_vars[EDIT_PARAM] == "1" || 
    69         $wp_query->query_vars[EDIT_PARAM] == "true")) 
    70         { 
    71                 //Verify that the current language is editable and that the 
    72                 //user has the required permissions 
    73                 $editable_langs = get_option(EDITABLE_LANGS); 
    74  
    75                 if(is_translator() && strstr($editable_langs, $lang)) 
    76                 { 
    77                         $is_edit_mode = TRUE; 
    78                 } 
     69        if (($wp_query->query_vars[EDIT_PARAM] == "1" || $wp_query->query_vars[EDIT_PARAM] == "true") && 
     70             is_translation_allowed()) 
     71        { 
     72                $is_edit_mode = TRUE; 
    7973        } 
    8074 
     
    9387 
    9488/* 
    95  * Fix links on the page. href needs to be modified to include 
    96  * lang specifier and editing mode. 
    97  */ 
    98 function process_anchor_tag($start, $end) 
    99 { 
    100         global $home_url, $home_url_quoted, $lang, $is_edit_mode, $enable_permalinks_rewrite; 
    101  
    102         $href = get_attribute($start, $end, 'href'); 
    103  
    104         if($href == NULL) 
    105         { 
     89 * Init global variables later used throughout this process.  
     90 * Note that at the time that this function is called the wp_query is not initialized,  
     91 * which means that query parameters are not accessiable.  
     92 */ 
     93function init_global_vars() 
     94{ 
     95        global $home_url, $home_url_quoted, $plugin_url, $enable_permalinks_rewrite, $wp_rewrite; 
     96 
     97        $home_url = get_option('home'); 
     98        $local_dir = preg_replace("/.*\//", "", dirname(__FILE__)); 
     99 
     100        $plugin_url= $home_url . "/wp-content/plugins/$local_dir"; 
     101        $home_url_quoted = preg_quote($home_url); 
     102        $home_url_quoted = preg_replace("/\//", "\\/", $home_url_quoted); 
     103 
     104        if($wp_rewrite->using_permalinks() && get_option(ENABLE_PERMALINKS_REWRITE)) 
     105        { 
     106                $enable_permalinks_rewrite = TRUE; 
     107        } 
     108} 
     109 
     110/* 
     111 * Gets the default language setting, i.e. the source language which 
     112 * should not be translated. 
     113 * Return the default language setting 
     114 */ 
     115function get_default_lang() 
     116{ 
     117        global $languages; 
     118 
     119        $default = get_option(DEFAULT_LANG); 
     120        if(!$languages[$default]) 
     121        { 
     122                $default = "en"; 
     123        } 
     124 
     125        return $default; 
     126} 
     127 
     128/* 
     129 * Setup a buffer that will contain the contents of the html page. 
     130 * Once processing is completed the buffer will go into the translation process. 
     131 */ 
     132function on_init() 
     133{ 
     134        logger(__METHOD__ . $_SERVER['REQUEST_URI']); 
     135        init_global_vars(); 
     136 
     137        if ($_POST['translation_posted']) 
     138        { 
     139                update_translation(); 
     140        } 
     141        else 
     142        { 
     143                //set the callback for translating the page when it's done 
     144                ob_start("process_page"); 
     145        } 
     146} 
     147 
     148/* 
     149 * Page generation completed - flush buffer. 
     150 */ 
     151function on_shutdown() 
     152{ 
     153        ob_flush(); 
     154} 
     155 
     156/* 
     157 * Update the url rewrite rules to include language identifier 
     158 */ 
     159function update_rewrite_rules($rules){ 
     160        logger("Enter update_rewrite_rules"); 
     161 
     162        if(!get_option(ENABLE_PERMALINKS_REWRITE)) 
     163        { 
     164                logger("Not touching rewrite rules - permalinks modification disabled by admin"); 
     165                return $rules; 
     166        } 
     167 
     168        $newRules = array(); 
     169        $lang_prefix="([a-z]{2,2}(\-[a-z]{2,2})?)/"; 
     170 
     171        $lang_parameter= "&" . LANG_PARAM . '=$matches[1]'; 
     172 
     173        //catch the root url 
     174        $newRules[$lang_prefix."?$"] = "index.php?lang=\$matches[1]"; 
     175        logger("\t" . $lang_prefix."?$" . "  --->  " . "index.php?lang=\$matches[1]"); 
     176 
     177        foreach ($rules as $key=>$value) { 
     178                $original_key = $key; 
     179                $original_value = $value; 
     180 
     181                $key = $lang_prefix . $key; 
     182 
     183                //Shift existing matches[i] two step forward as we pushed new elements 
     184                //in the beginning of the expression 
     185                for($i = 6; $i > 0; $i--) 
     186                { 
     187                        $value = str_replace('['. $i .']', '['. ($i + 2) .']', $value); 
     188                } 
     189 
     190                $value .= $lang_parameter; 
     191 
     192                logger("\t" . $key . "  --->  " . $value); 
     193 
     194 
     195                $newRules[$key] = $value; 
     196                $newRules[$original_key] = $original_value; 
     197 
     198                logger("\t" . $original_key . "  --->  " . $original_value); 
     199        } 
     200 
     201        logger("Exit update_rewrite_rules"); 
     202        return $newRules; 
     203} 
     204 
     205/* 
     206 * Let WordPress which parameters are of interest to us. 
     207 */ 
     208function parameter_queryvars($qvars) 
     209{ 
     210        $qvars[] = LANG_PARAM; 
     211        $qvars[] = EDIT_PARAM; 
     212 
     213        return $qvars; 
     214} 
     215 
     216/* 
     217 * Determine if the current user is allowed to translate. 
     218 * Return TRUE if allowed to translate otherwise FALSE 
     219 */ 
     220function is_translator() 
     221{ 
     222        if(is_user_logged_in()) 
     223        { 
     224                if(current_user_can(TRANSLATOR)) 
     225                { 
     226                        return TRUE; 
     227                } 
     228        } 
     229 
     230        if(get_option(ANONYMOUS_TRANSLATION)) 
     231        { 
     232                //if anonymous translation is allowed - let anyone enjoy it 
     233                return TRUE; 
     234        } 
     235 
     236        return FALSE; 
     237} 
     238 
     239/* 
     240 * Plugin activated. 
     241 */ 
     242function plugin_activate() 
     243{ 
     244        global $wp_rewrite; 
     245        logger("plugin_activate enter: " . dirname(__FILE__)); 
     246 
     247        setup_db(); 
     248 
     249        add_filter('rewrite_rules_array', 'update_rewrite_rules'); 
     250        $wp_rewrite->flush_rules(); 
     251 
     252        logger("plugin_activate exit: " . dirname(__FILE__)); 
     253} 
     254 
     255/* 
     256 * Plugin deactivated. 
     257 */ 
     258function plugin_deactivate(){ 
     259        global $wp_rewrite; 
     260        logger("plugin_deactivate enter: " . dirname(__FILE__)); 
     261 
     262        remove_filter('rewrite_rules_array', 'update_rewrite_rules'); 
     263        $wp_rewrite->flush_rules(); 
     264 
     265        logger("plugin_deactivate exit: " . dirname(__FILE__)); 
     266} 
     267 
     268/* 
     269 * Callback from admin_notices - display error message to the admin. 
     270 */ 
     271function plugin_install_error() 
     272{ 
     273        global $admin_msg; 
     274        logger("Enter " . __METHOD__, 0); 
     275 
     276        echo '<div class="updated"><p>'; 
     277        echo 'Error has occured in the installation process of the translation plugin: <br>'; 
     278 
     279        echo $admin_msg; 
     280 
     281        if (function_exists('deactivate_plugins') ) { 
     282                deactivate_plugins(get_plugin_name(), "translate.php"); 
     283                echo '<br> This plugin has been automatically deactivated.'; 
     284        } 
     285 
     286        echo '</p></div>'; 
     287} 
     288 
     289/* 
     290 * Callback when all plugins have been loaded. Serves as the location 
     291 * to check that the plugin loaded successfully else trigger notification 
     292 * to the admin and deactivate plugin. 
     293 */ 
     294function plugin_loaded() 
     295{ 
     296        global $admin_msg; 
     297        logger("Enter " . __METHOD__, 4); 
     298 
     299        $db_version = get_option(TRANSPOSH_DB_VERSION); 
     300 
     301        if ($db_version != DB_VERSION) 
     302        { 
     303                setup_db(); 
     304                //$admin_msg = "Translation database version ($db_version) is not comptabile with this plugin (". DB_VERSION . ")  <br>"; 
     305 
     306                logger("Updating database in plugin loaded", 0); 
     307                //Some error occured - notify admin and deactivate plugin 
     308                //add_action('admin_notices', 'plugin_install_error'); 
     309        } 
     310 
     311        if ($db_version != DB_VERSION) 
     312        { 
     313                $admin_msg = "Failed to locate the translation table  <em> " . TRANSLATIONS_TABLE . "</em> in local database. <br>"; 
     314 
     315                logger("Messsage to admin: $admin_msg", 0); 
     316                //Some error occured - notify admin and deactivate plugin 
     317                add_action('admin_notices', 'plugin_install_error'); 
     318        } 
     319} 
     320 
     321/* 
     322 * Gets the plugin name to be used in activation/decativation hooks. 
     323 * Keep only the file name and its containing directory. Don't use the full 
     324 * path as it will break when using symbollic links. 
     325 */ 
     326function get_plugin_name() 
     327{ 
     328        $file = __FILE__; 
     329        $file = str_replace('\\','/',$file); // sanitize for Win32 installs 
     330        $file = preg_replace('|/+|','/', $file); // remove any duplicate slash 
     331 
     332        //keep only the file name and its parent directory 
     333        $file = preg_replace('/.*\/([^\/]+\/[^\/]+)$/', '$1', $file); 
     334        logger("Plugin path $file", 3); 
     335        return $file; 
     336} 
     337 
     338/* 
     339 * Add custom css, i.e. transposh.css 
     340 */ 
     341function add_transposh_css() { 
     342        global $plugin_url; 
     343         
     344        if(!is_translation_allowed()) 
     345        { 
     346                //translation not allowed - no need for the transposh.css        
    106347                return; 
    107348        } 
    108  
    109         //Ignore urls not from this site 
    110         if(stripos($href, $home_url) === FALSE) 
    111         { 
     349        //include the transposh.css 
     350        wp_enqueue_style("transposh","$plugin_url/transposh.css",array(),'1.0.1'); 
     351        logger("Added transposh_css"); 
     352} 
     353 
     354/* 
     355 * Insert references to the javascript files used in the transalted 
     356 * version of the page. 
     357 */ 
     358function add_transposh_js() { 
     359        global $plugin_url, $wp_query, $lang, $home_url,  $enable_auto_translate; 
     360 
     361        if(!is_translation_allowed()) 
     362        { 
     363                //translation not allowed - no need for any js.  
    112364                return; 
    113365        } 
    114  
    115         $use_params = !$enable_permalinks_rewrite; 
    116  
    117         //Only use params if permalinks are not enabled. 
     366         
     367        $is_edit_param_enabled = $wp_query->query_vars[EDIT_PARAM]; 
     368        $enable_auto_translate = get_option(ENABLE_AUTO_TRANSLATE,1) && is_translation_allowed(); 
     369         
     370        if (!$is_edit_param_enabled && !$enable_auto_translate) 
     371        { 
     372                //Not in any translation mode - no need for any js. 
     373                return; 
     374        } 
     375 
     376        $overlib_dir = "$plugin_url/js/overlibmws"; 
     377 
     378        if($is_edit_param_enabled) 
     379        { 
     380                wp_enqueue_script("overlibmws","$overlib_dir/overlibmws.js",array(),'1.0'); 
     381                wp_enqueue_script("overlibmws1","$overlib_dir/overlibmws_filter.js",array("overlibmws"),'1.0'); 
     382                wp_enqueue_script("overlibmws2","$overlib_dir/overlibmws_modal.js",array("overlibmws1"),'1.0'); 
     383                wp_enqueue_script("overlibmws3","$overlib_dir/overlibmws_overtwo.js",array("overlibmws2"),'1.0'); 
     384                wp_enqueue_script("overlibmws4","$overlib_dir/overlibmws_scroll.js",array("overlibmws3"),'1.0'); 
     385                wp_enqueue_script("overlibmws5","$overlib_dir/overlibmws_shadow.js",array("overlibmws4"),'1.0'); 
     386        } 
     387 
     388        if($is_edit_param_enabled || $enable_auto_translate) 
     389        { 
     390                $post_url = $home_url . '/index.php'; 
     391                wp_enqueue_script("jquerymin","http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js",array(),'1.3.2'); 
     392                wp_enqueue_script("google","http://www.google.com/jsapi",array(),'1'); 
     393                wp_enqueue_script("transposh","$plugin_url/js/transposh.js?post_url=$post_url&lang={$lang}",array("jquerymin"),'1.0'); 
     394        } 
     395} 
     396 
     397 
     398/** 
     399 * Determine if the currently selected language (taken from the query parameters) is in the admin's list  
     400 * of editable languages and the current user is allowed to translate.  
     401 *   
     402 * @return TRUE if translation allowed otherwise FALSE 
     403 */ 
     404function is_translation_allowed() 
     405{ 
     406        global $wp_query; 
     407 
     408        if(!is_translator()) 
     409        { 
     410                return FALSE; 
     411        } 
     412         
     413        if (!isset($wp_query->query_vars[LANG_PARAM])) 
     414        { 
     415                return FALSE; 
     416        } 
     417         
     418        $lang = $wp_query->query_vars[LANG_PARAM]; 
     419        return is_editable_lang($lang); 
     420} 
     421 
     422/** 
     423 * Determine if the given language in on the list of editable languages 
     424 * @return TRUE if editable othewise FALSE 
     425 */ 
     426function is_editable_lang($lang) 
     427{ 
     428        $editable_langs = get_option(EDITABLE_LANGS); 
     429 
     430        if(strpos($editable_langs, $lang) === FALSE) 
     431        { 
     432                //not an editable language 
     433                return FALSE; 
     434        } 
     435         
     436        return TRUE; 
     437} 
     438 
     439/** 
     440 * Callback from parser allowing to overide the global setting of url rewriting using permalinks. 
     441 * Some urls should be modified only by adding parameters and should be identified by this  
     442 * function. 
     443 * @param $href 
     444 * @return TRUE if parameters should be used instead of rewriting as a permalink 
     445 */ 
     446function is_url_excluded_from_permalink_rewrite($href) 
     447{ 
     448        $use_params = FALSE; 
     449         
    118450        //don't fix links pointing to real files as it will cause that the 
    119451        //web server will not be able to locate them 
     
    125457                $use_params = TRUE; 
    126458        } 
    127  
    128         $href = rewrite_url_lang_param($href, $lang, $is_edit_mode, $use_params); 
    129  
    130         //rewrite url in translated page 
    131         update_translated_page($start, $end, $href); 
    132         logger(__METHOD__ . " $home_url href: $href"); 
    133 } 
    134  
    135 /* 
    136  * Update the given url to include language params. 
    137  * param url - the original url to rewrite 
    138  * param lang - language code 
    139  * param is_edit - is running in edit mode. 
    140  * param use_params_only - use only parameters as modifiers, i.e. not permalinks 
    141  */ 
    142 function rewrite_url_lang_param($url, $lang, $is_edit, $use_params_only=FALSE) 
    143 { 
    144         global $home_url, $home_url_quoted, $enable_permalinks_rewrite; 
    145  
    146         $url = html_entity_decode($url, ENT_NOQUOTES); 
    147  
    148         if(!$enable_permalinks_rewrite) 
    149         { 
    150                 //override the use only params - admin configured system to not touch permalinks 
    151                 $use_params_only = TRUE; 
    152         } 
    153  
    154         if($is_edit) 
    155         { 
    156                 $params = EDIT_PARAM . '=1&'; 
    157  
    158         } 
    159  
    160         if($use_params_only) 
    161         { 
    162                 $params .= LANG_PARAM . "=$lang&"; 
    163         } 
    164         else 
    165         { 
    166                 $url = preg_replace("/$home_url_quoted\/(..(-..)?\/)?\/?/", 
    167                                  "$home_url/$lang/",  $url); 
    168         } 
    169  
    170         if($params) 
    171         { 
    172                 //insert params to url 
    173                 $url = preg_replace("/(.+\/[^\?\#]*[\?]?)/", '$1?' . $params, $url); 
    174  
    175                 //Cleanup extra & 
    176                 $url = preg_replace("/&&+/", "&", $url); 
    177  
    178                 //Cleanup extra ? 
    179                 $url = preg_replace("/\?\?+/", "?", $url); 
    180         } 
    181  
    182         // more cleanups 
    183         $url = preg_replace("/&$/", "", $url); 
    184         $url = preg_replace("/\?$/", "", $url); 
    185  
    186         $url = htmlentities($url, ENT_NOQUOTES); 
    187  
    188         return $url; 
    189 } 
    190 /* 
    191  * Return the img tag that will added to enable editing a translatable 
    192  * item on the page. 
    193  * param segement_id The id (number) identifying this segment. Needs to be 
    194  * placed within the img tag for use on client side operation (jquery) 
    195  */ 
    196 function get_img_tag($original, $translation, $source, $segment_id, $is_translated = FALSE) 
    197 { 
    198         global $plugin_url, $lang, $home_url; 
    199         $url = $home_url . '/index.php'; 
    200  
    201         //For use in javascript, make the following changes: 
    202         //1. Add slashes to escape the inner text 
    203         //2. Convert the html special characters 
    204         //The browser will take decode step 2 and pass it to the js engine which decode step 1 - a bit tricky 
    205         $translation = htmlspecialchars(addslashes($translation)); 
    206         $original    = htmlspecialchars(addslashes($original)); 
    207  
    208         if ($is_translated) 
    209         { 
    210                 $add_img = "_fix"; 
    211         } 
    212  
    213         if ($source == 1) { 
    214                 $add_img = "_auto"; 
    215         } 
    216  
    217         $img = "<img src=\"$plugin_url/translate$add_img.png\" alt=\"translate\" class=\"".IMG_PREFIX."\" id=\"" . IMG_PREFIX . "$segment_id\" ". 
    218            "onclick=\"translate_dialog('$original','$translation','$segment_id'); return false;\" ". 
    219            "onmouseover=\"hint('$original'); return true;\" ". 
    220            "onmouseout=\"nd()\" />"; 
    221  
    222         return $img; 
    223 } 
    224  
    225 /* 
    226  * Init global variables later used throughout this process 
    227  */ 
    228 function init_global_vars() 
    229 { 
    230         global $home_url, $home_url_quoted, $plugin_url, $enable_auto_translate, 
    231               $enable_permalinks_rewrite, $wp_rewrite; 
    232  
    233         $home_url = get_option('home'); 
    234         $local_dir = preg_replace("/.*\//", "", dirname(__FILE__)); 
    235  
    236         $plugin_url= $home_url . "/wp-content/plugins/$local_dir"; 
    237         $home_url_quoted = preg_quote($home_url); 
    238         $home_url_quoted = preg_replace("/\//", "\\/", $home_url_quoted); 
    239  
    240         $enable_auto_translate = get_option(ENABLE_AUTO_TRANSLATE,1) && is_translation_allowed(); 
    241  
    242         if($wp_rewrite->using_permalinks() && get_option(ENABLE_PERMALINKS_REWRITE)) 
    243         { 
    244                 $enable_permalinks_rewrite = TRUE; 
    245         } 
    246 } 
    247  
    248 /* 
    249  * Gets the default language setting, i.e. the source language which 
    250  * should not be translated. 
    251  * Return the default language setting 
    252  */ 
    253 function get_default_lang() 
    254 { 
    255         global $languages; 
    256  
    257         $default = get_option(DEFAULT_LANG); 
    258         if(!$languages[$default]) 
    259         { 
    260                 $default = "en"; 
    261         } 
    262  
    263         return $default; 
    264 } 
    265  
    266 /* 
    267  * Setup a buffer that will contain the contents of the html page. 
    268  * Once processing is completed the buffer will go into the translation process. 
    269  */ 
    270 function on_init() 
    271 { 
    272         logger(__METHOD__ . $_SERVER['REQUEST_URI']); 
    273         init_global_vars(); 
    274  
    275         if ($_POST['translation_posted']) 
    276         { 
    277                 update_translation(); 
    278         } 
    279         else 
    280         { 
    281                 //set the callback for translating the page when it's done 
    282                 ob_start("process_page"); 
    283         } 
    284 } 
    285  
    286 /* 
    287  * Page generation completed - flush buffer. 
    288  */ 
    289 function on_shutdown() 
    290 { 
    291         ob_flush(); 
    292 } 
    293  
    294 /* 
    295  * Update the url rewrite rules to include language identifier 
    296  */ 
    297 function update_rewrite_rules($rules){ 
    298         logger("Enter update_rewrite_rules"); 
    299  
    300         if(!get_option(ENABLE_PERMALINKS_REWRITE)) 
    301         { 
    302                 logger("Not touching rewrite rules - permalinks modification disabled by admin"); 
    303                 return $rules; 
    304         } 
    305  
    306         $newRules = array(); 
    307         $lang_prefix="([a-z]{2,2}(\-[a-z]{2,2})?)/"; 
    308  
    309         $lang_parameter= "&" . LANG_PARAM . '=$matches[1]'; 
    310  
    311         //catch the root url 
    312         $newRules[$lang_prefix."?$"] = "index.php?lang=\$matches[1]"; 
    313         logger("\t" . $lang_prefix."?$" . "  --->  " . "index.php?lang=\$matches[1]"); 
    314  
    315         foreach ($rules as $key=>$value) { 
    316                 $original_key = $key; 
    317                 $original_value = $value; 
    318  
    319                 $key = $lang_prefix . $key; 
    320  
    321                 //Shift existing matches[i] two step forward as we pushed new elements 
    322                 //in the beginning of the expression 
    323                 for($i = 6; $i > 0; $i--) 
    324                 { 
    325                         $value = str_replace('['. $i .']', '['. ($i + 2) .']', $value); 
    326                 } 
    327  
    328                 $value .= $lang_parameter; 
    329  
    330                 logger("\t" . $key . "  --->  " . $value); 
    331  
    332  
    333                 $newRules[$key] = $value; 
    334                 $newRules[$original_key] = $original_value; 
    335  
    336                 logger("\t" . $original_key . "  --->  " . $original_value); 
    337         } 
    338  
    339         logger("Exit update_rewrite_rules"); 
    340         return $newRules; 
    341 } 
    342  
    343 /* 
    344  * Let WordPress which parameters are of interest to us. 
    345  */ 
    346 function parameter_queryvars($qvars) 
    347 { 
    348         $qvars[] = LANG_PARAM; 
    349         $qvars[] = EDIT_PARAM; 
    350  
    351         return $qvars; 
    352 } 
    353  
    354 /* 
    355  * Determine if the current user is allowed to translate. 
    356  * Return TRUE if allowed to translate otherwise FALSE 
    357  */ 
    358 function is_translator() 
    359 { 
    360         if(is_user_logged_in()) 
    361         { 
    362                 if(current_user_can(TRANSLATOR)) 
    363                 { 
    364                         return TRUE; 
    365                 } 
    366         } 
    367  
    368         if(get_option(ANONYMOUS_TRANSLATION)) 
    369         { 
    370                 //if anonymous translation is allowed - let anyone enjoy it 
    371                 return TRUE; 
    372         } 
    373  
    374         return FALSE; 
    375 } 
    376  
    377 /* 
    378  * Plugin activated. 
    379  */ 
    380 function plugin_activate() 
    381 { 
    382         global $wp_rewrite; 
    383         logger("plugin_activate enter: " . dirname(__FILE__)); 
    384  
    385         setup_db(); 
    386  
    387         add_filter('rewrite_rules_array', 'update_rewrite_rules'); 
    388         $wp_rewrite->flush_rules(); 
    389  
    390         logger("plugin_activate exit: " . dirname(__FILE__)); 
    391 } 
    392  
    393 /* 
    394  * Plugin deactivated. 
    395  */ 
    396 function plugin_deactivate(){ 
    397         global $wp_rewrite; 
    398         logger("plugin_deactivate enter: " . dirname(__FILE__)); 
    399  
    400         remove_filter('rewrite_rules_array', 'update_rewrite_rules'); 
    401         $wp_rewrite->flush_rules(); 
    402  
    403         logger("plugin_deactivate exit: " . dirname(__FILE__)); 
    404 } 
    405  
    406 /* 
    407  * Callback from admin_notices - display error message to the admin. 
    408  */ 
    409 function plugin_install_error() 
    410 { 
    411         global $admin_msg; 
    412         logger("Enter " . __METHOD__, 0); 
    413  
    414         echo '<div class="updated"><p>'; 
    415         echo 'Error has occured in the installation process of the translation plugin: <br>'; 
    416  
    417         echo $admin_msg; 
    418  
    419         if (function_exists('deactivate_plugins') ) { 
    420                 deactivate_plugins(get_plugin_name(), "translate.php"); 
    421                 echo '<br> This plugin has been automatically deactivated.'; 
    422         } 
    423  
    424         echo '</p></div>'; 
    425 } 
    426  
    427 /* 
    428  * Callback when all plugins have been loaded. Serves as the location 
    429  * to check that the plugin loaded successfully else trigger notification 
    430  * to the admin and deactivate plugin. 
    431  */ 
    432 function plugin_loaded() 
    433 { 
    434         global $admin_msg; 
    435         logger("Enter " . __METHOD__, 4); 
    436  
    437         $db_version = get_option(TRANSPOSH_DB_VERSION); 
    438  
    439         if ($db_version != DB_VERSION) 
    440         { 
    441                 setup_db(); 
    442                 //$admin_msg = "Translation database version ($db_version) is not comptabile with this plugin (". DB_VERSION . ")  <br>"; 
    443  
    444                 logger("Updating database in plugin loaded", 0); 
    445                 //Some error occured - notify admin and deactivate plugin 
    446                 //add_action('admin_notices', 'plugin_install_error'); 
    447         } 
    448  
    449         if ($db_version != DB_VERSION) 
    450         { 
    451                 $admin_msg = "Failed to locate the translation table  <em> " . TRANSLATIONS_TABLE . "</em> in local database. <br>"; 
    452  
    453                 logger("Messsage to admin: $admin_msg", 0); 
    454                 //Some error occured - notify admin and deactivate plugin 
    455                 add_action('admin_notices', 'plugin_install_error'); 
    456         } 
    457 } 
    458  
    459 /* 
    460  * Gets the plugin name to be used in activation/decativation hooks. 
    461  * Keep only the file name and its containing directory. Don't use the full 
    462  * path as it will break when using symbollic links. 
    463  */ 
    464 function get_plugin_name() 
    465 { 
    466         $file = __FILE__; 
    467         $file = str_replace('\\','/',$file); // sanitize for Win32 installs 
    468         $file = preg_replace('|/+|','/', $file); // remove any duplicate slash 
    469  
    470         //keep only the file name and its parent directory 
    471         $file = preg_replace('/.*\/([^\/]+\/[^\/]+)$/', '$1', $file); 
    472         logger("Plugin path $file", 3); 
    473         return $file; 
    474 } 
    475  
    476 /* 
    477  * Add custom css, i.e. transposh.css 
    478  */ 
    479 function add_transposh_css() { 
    480         global $plugin_url; 
    481          
    482         if(!is_translation_allowed()) 
    483         { 
    484                 //translation not allowed - no need for the transposh.css        
    485                 return; 
    486         } 
    487         //include the transposh.css 
    488         wp_enqueue_style("transposh","$plugin_url/transposh.css",array(),'1.0.1'); 
    489         logger("Added transposh_css"); 
    490 } 
    491  
    492 /* 
    493  * Insert references to the javascript files used in the transalted 
    494  * version of the page. 
    495  */ 
    496 function add_transposh_js() { 
    497         global $plugin_url, $wp_query, $lang, $home_url,  $enable_auto_translate; 
    498  
    499         if(!is_translation_allowed()) 
    500         { 
    501                 //translation not allowed - no need for any js.  
    502                 return; 
    503         } 
    504          
    505         $is_edit_param_enabled = $wp_query->query_vars[EDIT_PARAM]; 
    506         if (!$is_edit_param_enabled && ! $enable_auto_translate) 
    507         { 
    508                 //Not in any translation mode - no need for any js. 
    509                 return; 
    510         } 
    511  
    512         $overlib_dir = "$plugin_url/js/overlibmws"; 
    513  
    514         if($is_edit_param_enabled) 
    515         { 
    516                 wp_enqueue_script("overlibmws","$overlib_dir/overlibmws.js",array(),'1.0'); 
    517                 wp_enqueue_script("overlibmws1","$overlib_dir/overlibmws_filter.js",array("overlibmws"),'1.0'); 
    518                 wp_enqueue_script("overlibmws2","$overlib_dir/overlibmws_modal.js",array("overlibmws1"),'1.0'); 
    519                 wp_enqueue_script("overlibmws3","$overlib_dir/overlibmws_overtwo.js",array("overlibmws2"),'1.0'); 
    520                 wp_enqueue_script("overlibmws4","$overlib_dir/overlibmws_scroll.js",array("overlibmws3"),'1.0'); 
    521                 wp_enqueue_script("overlibmws5","$overlib_dir/overlibmws_shadow.js",array("overlibmws4"),'1.0'); 
    522         } 
    523  
    524         if($is_edit_param_enabled || $enable_auto_translate) 
    525         { 
    526                 $post_url = $home_url . '/index.php'; 
    527                 wp_enqueue_script("jquerymin","http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js",array(),'1.3.2'); 
    528                 wp_enqueue_script("google","http://www.google.com/jsapi",array(),'1'); 
    529                 wp_enqueue_script("transposh","$plugin_url/js/transposh.js?post_url=$post_url&lang={$lang}",array("jquerymin"),'1.0'); 
    530         } 
    531 } 
    532  
    533  
    534 /** 
    535  * Determine if the currently selected language (taken from the query parameters) is in the admin's list  
    536  * of editable languages and the current user is allowed to translate.  
    537  *   
    538  * @return TRUE if translation allowed otherwise FALSE 
    539  */ 
    540 function is_translation_allowed() 
    541 { 
    542         global $wp_query; 
    543  
    544         if(!is_translator()) 
    545         { 
    546                 return FALSE; 
    547         } 
    548          
    549         if (!isset($wp_query->query_vars[LANG_PARAM])) 
    550         { 
    551                 return FALSE; 
    552         } 
    553          
    554         $lang = $wp_query->query_vars[LANG_PARAM]; 
    555         return is_editable_lang($lang); 
    556 } 
    557  
    558 /** 
    559  * Determine if the given language in on the list of editable languages 
    560  * @return TRUE if editable othewise FALSE 
    561  */ 
    562 function is_editable_lang($lang) 
    563 { 
    564         $editable_langs = get_option(EDITABLE_LANGS); 
    565  
    566         if(strpos($editable_langs, $lang) === FALSE) 
    567         { 
    568                 //not an editable language 
    569                 return FALSE; 
    570         } 
    571          
    572         return TRUE; 
     459         
     460        return $use_params; 
    573461} 
    574462 
  • trunk/WordPress/plugin/transposh/transposh_db.php

    r119 r120  
    2626require_once("logging.php"); 
    2727require_once("constants.php"); 
     28require_once("utils.php"); 
    2829 
    2930// 
     
    117118                return; 
    118119        } 
    119  
     120         
     121        //add  our own custom header - so we will know that we got here  
     122        header("Transposh: version_". DB_VERSION); 
     123         
    120124        //Check that user is allowed to translate this language 
    121125        if(!is_translator() || !is_editable_lang($lang)) 
Note: See TracChangeset for help on using the changeset viewer.