Advertisement
MoonWatch

qTranslate qtranslate_javascript.php fix for WP 3.5

Dec 12th, 2012
8,619
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.85 KB | None | 0 0
  1. <?php // encoding: utf-8
  2.  
  3. /*  Copyright 2008  Qian Qin  (email : mail@qianqin.de)
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  18. */
  19.  
  20. // qTranslate Javascript functions
  21. function qtrans_initJS() {
  22.     global $q_config;
  23.     $q_config['js']['qtrans_xsplit'] = "
  24.         var usr_sett_ed = getUserSetting( 'editor' );
  25.         if ( ! parseInt( getUserSetting( 'ed_size' ) ) || parseInt( getUserSetting( 'ed_size' ) ) < 300 ) {
  26.             setUserSetting( 'ed_size', 300 );
  27.         }
  28.         setUserSetting( 'editor', 'html' );
  29.         String.prototype.xsplit = function(_regEx){
  30.             // Most browsers can do this properly, so let them — they'll do it faster
  31.             if ('a~b'.split(/(~)/).length === 3) { return this.split(_regEx); }
  32.  
  33.             if (!_regEx.global)
  34.             { _regEx = new RegExp(_regEx.source, 'g' + (_regEx.ignoreCase ? 'i' : '')); }
  35.  
  36.             // IE (and any other browser that can't capture the delimiter)
  37.             // will, unfortunately, have to be slowed down
  38.             var start = 0, arr=[];
  39.             var result;
  40.             while((result = _regEx.exec(this)) != null){
  41.                 arr.push(this.slice(start, result.index));
  42.                 if(result.length > 1) arr.push(result[1]);
  43.                 start = _regEx.lastIndex;
  44.             }
  45.             if(start < this.length) arr.push(this.slice(start));
  46.             if(start == this.length) arr.push(''); //delim at the end
  47.             return arr;
  48.         };
  49.         ";
  50.  
  51.     $q_config['js']['qtrans_is_array'] = "
  52.         qtrans_isArray = function(obj) {
  53.            if (obj.constructor.toString().indexOf('Array') == -1)
  54.               return false;
  55.            else
  56.               return true;
  57.         }
  58.         ";
  59.  
  60.     $q_config['js']['qtrans_split'] = "
  61.         qtrans_split = function(text) {
  62.             var split_regex = /(<!--.*?-->)/gi;
  63.             var lang_begin_regex = /<!--:([a-z]{2})-->/gi;
  64.             var lang_end_regex = /<!--:-->/gi;
  65.             var morenextpage_regex = /(<!--more-->|<!--nextpage-->)+$/gi;
  66.             var matches = null;
  67.             var result = new Object;
  68.             var matched = false;
  69.         ";
  70.     foreach($q_config['enabled_languages'] as $language)
  71.         $q_config['js']['qtrans_split'].= "
  72.             result['".$language."'] = '';
  73.             ";
  74.     $q_config['js']['qtrans_split'].= "
  75.            
  76.             var blocks = text.xsplit(split_regex);
  77.             if(qtrans_isArray(blocks)) {
  78.                 for (var i = 0;i<blocks.length;i++) {
  79.                     if((matches = lang_begin_regex.exec(blocks[i])) != null) {
  80.                         matched = matches[1];
  81.                     } else if(lang_end_regex.test(blocks[i])) {
  82.                         matched = false;
  83.                     } else {
  84.                         if(matched) {
  85.                             result[matched] += blocks[i];
  86.                         } else {
  87.         ";
  88.     foreach($q_config['enabled_languages'] as $language)
  89.         $q_config['js']['qtrans_split'].= "
  90.                             result['".$language."'] += blocks[i];
  91.             ";
  92.     $q_config['js']['qtrans_split'].= "
  93.                         }
  94.                     }
  95.                 }
  96.             }
  97.             for (var i = 0;i<result.length;i++) {
  98.                 result[i] = result[i].replace(morenextpage_regex,'');
  99.             }
  100.             return result;
  101.         }
  102.         ";
  103.  
  104.     $q_config['js']['qtrans_use'] = "
  105.         qtrans_use = function(lang, text) {
  106.             var result = qtrans_split(text);
  107.             return result[lang];
  108.         }
  109.         ";
  110.        
  111.     $q_config['js']['qtrans_integrate'] = "
  112.         qtrans_integrate = function(lang, lang_text, text) {
  113.             var texts = qtrans_split(text);
  114.             var moreregex = /<!--more-->/i;
  115.             var text = '';
  116.             var max = 0;
  117.             var morenextpage_regex = /(<!--more-->|<!--nextpage-->)+$/gi;
  118.            
  119.             texts[lang] = lang_text;
  120.         ";
  121.     foreach($q_config['enabled_languages'] as $language)
  122.         $q_config['js']['qtrans_integrate'].= "
  123.             texts['".$language."'] = texts['".$language."'].split(moreregex);
  124.             if(!qtrans_isArray(texts['".$language."'])) {
  125.                 texts['".$language."'] = [texts['".$language."']];
  126.             }
  127.             if(max < texts['".$language."'].length) max = texts['".$language."'].length;
  128.             ";
  129.     $q_config['js']['qtrans_integrate'].= "
  130.             for(var i=0; i<max; i++) {
  131.                 if(i >= 1) {
  132.                     text += '<!--more-->';
  133.                 }
  134.         ";
  135.     foreach($q_config['enabled_languages'] as $language)
  136.         $q_config['js']['qtrans_integrate'].= "
  137.                 if(texts['".$language."'][i] && texts['".$language."'][i]!=''){
  138.                     text += '<!--:".$language."-->';
  139.                     text += texts['".$language."'][i];
  140.                     text += '<!--:-->';
  141.                 }
  142.             ";
  143.     $q_config['js']['qtrans_integrate'].= "
  144.             }
  145.             text = text.replace(morenextpage_regex,'');
  146.             return text;
  147.         }
  148.         ";
  149.        
  150.     $q_config['js']['qtrans_save'] = "
  151.         qtrans_save = function(text) {
  152.             var ta = document.getElementById('content');
  153.             ta.value = qtrans_integrate(qtrans_get_active_language(),text,ta.value);
  154.             return ta.value;
  155.         }
  156.         ";
  157.        
  158.     $q_config['js']['qtrans_integrate_category'] = "
  159.         qtrans_integrate_category = function() {
  160.             var t = document.getElementById('cat_name');
  161.         ";
  162.     foreach($q_config['enabled_languages'] as $language)
  163.         $q_config['js']['qtrans_integrate_category'].= "
  164.             if(document.getElementById('qtrans_category_".$language."').value!='')
  165.                 t.value = qtrans_integrate('".$language."',document.getElementById('qtrans_category_".$language."').value,t.value);
  166.             ";
  167.     $q_config['js']['qtrans_integrate_category'].= "
  168.         }
  169.         ";
  170.        
  171.     $q_config['js']['qtrans_integrate_tag'] = "
  172.         qtrans_integrate_tag = function() {
  173.             var t = document.getElementById('name');
  174.         ";
  175.     foreach($q_config['enabled_languages'] as $language)
  176.         $q_config['js']['qtrans_integrate_tag'].= "
  177.             if(document.getElementById('qtrans_tag_".$language."').value!='')
  178.                 t.value = qtrans_integrate('".$language."',document.getElementById('qtrans_tag_".$language."').value,t.value);
  179.             ";
  180.     $q_config['js']['qtrans_integrate_tag'].= "
  181.         }
  182.         ";
  183.        
  184.     $q_config['js']['qtrans_integrate_link_category'] = "
  185.         qtrans_integrate_link_category = function() {
  186.             var t = document.getElementById('name');
  187.         ";
  188.     foreach($q_config['enabled_languages'] as $language)
  189.         $q_config['js']['qtrans_integrate_link_category'].= "
  190.             if(document.getElementById('qtrans_link_category_".$language."').value!='')
  191.                 t.value = qtrans_integrate('".$language."',document.getElementById('qtrans_link_category_".$language."').value,t.value);
  192.             ";
  193.     $q_config['js']['qtrans_integrate_link_category'].= "
  194.         }
  195.         ";
  196.        
  197.     $q_config['js']['qtrans_integrate_title'] = "
  198.         qtrans_integrate_title = function() {
  199.             var t = document.getElementById('title');
  200.         ";
  201.     foreach($q_config['enabled_languages'] as $language)
  202.         $q_config['js']['qtrans_integrate_title'].= "
  203.             t.value = qtrans_integrate('".$language."',document.getElementById('qtrans_title_".$language."').value,t.value);
  204.             ";
  205.     $q_config['js']['qtrans_integrate_title'].= "
  206.         }
  207.         ";
  208.        
  209.     $q_config['js']['qtrans_assign'] = "
  210.         qtrans_assign = function(id, text) {
  211.             var inst = tinyMCE.get(id);
  212.             var ta = document.getElementById(id);
  213.             if(inst && ! inst.isHidden()) {
  214.                 text = switchEditors.wpautop(text);
  215.                 inst.execCommand('mceSetContent', null, text);
  216.             } else {
  217.                 ta.value = text;
  218.             }
  219.         }
  220.         ";
  221.        
  222.     $q_config['js']['qtrans_disable_old_editor'] = "
  223.         var waitForTinyMCE = window.setInterval(function() {
  224.                 if(typeof(tinyMCE) !== 'undefined' && typeof(tinyMCE.get2) == 'function' && tinyMCE.get2('content')!=undefined) {
  225.                     content=jQuery('#content').val();
  226.                     tinyMCE.get2('content').remove();
  227.                     jQuery('#content').val(content);
  228.                     window.clearInterval(waitForTinyMCE);
  229.                 }
  230.             }, 250);
  231.         ";
  232.        
  233.     $q_config['js']['qtrans_tinyMCEOverload'] = "
  234.         tinyMCE.get2 = tinyMCE.get;
  235.         tinyMCE.get = function(id) {
  236.             if(id=='content'&&this.get2('qtrans_textarea_'+id)!=undefined)
  237.                 return this.get2('qtrans_textarea_'+id);
  238.             return this.get2(id);
  239.         }
  240.        
  241.         ";
  242.        
  243.     $q_config['js']['qtrans_wpActiveEditorOverload'] = "
  244.         jQuery('.wp-editor-wrap').unbind('mousedown');
  245.         jQuery('.wp-editor-wrap').mousedown(function(e){
  246.             wpActiveEditor = 'qtrans_textarea_'+this.id.slice(3, -5);
  247.         });
  248.         ";
  249.    
  250.     $q_config['js']['qtrans_wpOnload'] = "
  251.         if(typeof(wpOnload)!='undefined') wpOnload2 = wpOnload;
  252.         wpOnload = function() {
  253.  
  254.             for(var i in tinyMCEPreInit.qtInit) {
  255.                 var tmp = tinyMCEPreInit.qtInit[i];
  256.                 tmp.id = 'qtrans_textarea_'+tmp.id;
  257.                 try { quicktags( tmp ); } catch(e){}
  258.                 jQuery('#ed_toolbar').hide();
  259.             }
  260.             // remove hook so tinymce doesn't load for content
  261.             var hook = tinyMCEPreInit.mceInit['content']
  262.             hook.elements='qtrans_textarea_content';
  263.             delete tinyMCEPreInit.mceInit['content'];
  264.             tinyMCEPreInit.mceInit['qtrans_textarea_content'] = hook;
  265.            
  266.             // fix html for tinymce
  267.             if('html' != getUserSetting( 'editor' )) {
  268.                 var ta = document.getElementById('content');
  269.                 var texts = qtrans_split(ta.value);
  270.                 var content = '';
  271.     ";
  272.     foreach($q_config['enabled_languages'] as $language)
  273.         $q_config['js']['qtrans_wpOnload'].= "
  274.                 content = qtrans_integrate('".$language."', switchEditors.wpautop(texts['".$language."']), content);
  275.             ";
  276.     $q_config['js']['qtrans_wpOnload'].= "
  277.                 ta.value = content;
  278.             }
  279.             if(typeof(wpOnload2)=='function') wpOnload2();
  280.         }
  281.        
  282.         jQuery(document).ready(function() {
  283.             qtrans_editorInit();
  284.             setUserSetting( 'editor', usr_sett_ed );
  285.         });
  286.         ";
  287.        
  288.     $q_config['js']['qtrans_editorInit'] = "
  289.         qtrans_editorInit = function() {
  290.             qtrans_editorInit1();
  291.             qtrans_editorInit2();
  292.             jQuery('#qtrans_imsg').hide();
  293.             qtrans_editorInit3();
  294.            
  295.             var h = getUserSetting( 'ed_size' );
  296.            
  297.             jQuery('#content').hide();
  298.             if ( getUserSetting( 'editor' ) == 'html' ) {
  299.                 if ( h )
  300.                     jQuery('#qtrans_textarea_content').css('height', h - 20 + 'px');
  301.                 jQuery('#qtrans_textarea_content').show();
  302.             } else {
  303.                 // Activate TinyMCE if it's the user's default editor
  304.                 jQuery('#qtrans_textarea_content').show();
  305.                 qtrans_hook_on_tinyMCE('qtrans_textarea_content');
  306.             }
  307.         }
  308.         ";
  309.    
  310.     $q_config['js']['qtrans_hook_on_tinyMCE'] = "
  311.         qtrans_hook_on_tinyMCE = function(id) {
  312.             tinyMCEPreInit.mceInit[id].setup = function(ed) {
  313.                 ed.onSaveContent.add(function(ed, o) {
  314.                     if (!ed.isHidden())  {
  315.                         qtrans_save(switchEditors.pre_wpautop(o.content));
  316.                     }
  317.                 });
  318.             };
  319.             ed = new tinymce.Editor(id, tinyMCEPreInit.mceInit[id]);
  320.             ed.render();
  321.         }
  322.         ";
  323.    
  324.     $q_config['js']['qtrans_get_active_language'] = "
  325.    
  326.         qtrans_get_active_language = function() {
  327.     ";
  328.     foreach($q_config['enabled_languages'] as $language)
  329.         $q_config['js']['qtrans_get_active_language'].= "
  330.                 if(document.getElementById('qtrans_select_".$language."').className=='wp-switch-editor switch-tmce switch-html')
  331.                     return '".$language."';
  332.             ";
  333.     $q_config['js']['qtrans_get_active_language'].= "
  334.         }
  335.         ";
  336.        
  337.     $q_config['js']['qtrans_switch_postbox'] = "
  338.         function qtrans_switch_postbox(parent, target, lang) {
  339.     ";
  340.     foreach($q_config['enabled_languages'] as $language)
  341.         $q_config['js']['qtrans_switch_postbox'].= "
  342.                 jQuery('#'+target).val(qtrans_integrate('".$language."', jQuery('#qtrans_textarea_'+target+'_'+'".$language."').val(), jQuery('#'+target).val()));
  343.                 jQuery('#'+parent+' .qtranslate_lang_div').removeClass('switch-html');
  344.                 jQuery('#'+parent+' .qtranslate_lang_div').removeClass('switch-tmce');
  345.                 if(lang!=false) jQuery('#qtrans_textarea_'+target+'_'+'".$language."').hide();
  346.             ";
  347.     $q_config['js']['qtrans_switch_postbox'].= "
  348.             if(lang!=false) {
  349.                 jQuery('#qtrans_switcher_'+parent+'_'+lang).addClass('switch-tmce');
  350.                 jQuery('#qtrans_switcher_'+parent+'_'+lang).addClass('switch-html');
  351.                 jQuery('#qtrans_textarea_'+target+'_'+lang).show().focus();
  352.             }
  353.         }
  354.     ";
  355.        
  356.     $q_config['js']['qtrans_switch'] = "
  357.         switchEditors.go = function(id, lang) {
  358.             id = id || 'content';
  359.             lang = lang || 'toggle';
  360.            
  361.             if ( 'toggle' == lang ) {
  362.                 if ( ed && !ed.isHidden() )
  363.                     lang = 'html';
  364.                 else
  365.                     lang = 'tmce';
  366.             } else if( 'tinymce' == lang )
  367.                 lang = 'tmce';
  368.        
  369.             var inst = tinyMCE.get('qtrans_textarea_' + id);
  370.             var vta = document.getElementById('qtrans_textarea_' + id);
  371.             var ta = document.getElementById(id);
  372.             var dom = tinymce.DOM;
  373.             var wrap_id = 'wp-'+id+'-wrap';
  374.            
  375.             // update merged content
  376.             if(inst && ! inst.isHidden()) {
  377.                 tinyMCE.triggerSave();
  378.             } else {
  379.                 qtrans_save(vta.value);
  380.             }
  381.            
  382.            
  383.             // check if language is already active
  384.             if(lang!='tmce' && lang!='html' && document.getElementById('qtrans_select_'+lang).className=='wp-switch-editor switch-tmce switch-html') {
  385.                 return;
  386.             }
  387.            
  388.             if(lang!='tmce' && lang!='html') {
  389.                 document.getElementById('qtrans_select_'+qtrans_get_active_language()).className='wp-switch-editor';
  390.                 document.getElementById('qtrans_select_'+lang).className='wp-switch-editor switch-tmce switch-html';
  391.             }
  392.            
  393.             if(lang=='html') {
  394.                 if ( inst && inst.isHidden() )
  395.                     return false;
  396.                 if ( inst ) {
  397.                     vta.style.height = inst.getContentAreaContainer().offsetHeight + 20 + 'px';
  398.                     inst.hide();
  399.                 }
  400.                
  401.                 dom.removeClass(wrap_id, 'tmce-active');
  402.                 dom.addClass(wrap_id, 'html-active');
  403.                 setUserSetting( 'editor', 'html' );
  404.             } else if(lang=='tmce') {
  405.                 if(inst && ! inst.isHidden())
  406.                     return false;
  407.                 if ( typeof(QTags) != 'undefined' )
  408.                     QTags.closeAllTags(id);
  409.                 if ( tinyMCEPreInit.mceInit['qtrans_textarea_'+id] && tinyMCEPreInit.mceInit['qtrans_textarea_'+id].wpautop )
  410.                     vta.value = this.wpautop(qtrans_use(qtrans_get_active_language(),ta.value));
  411.                 if (inst) {
  412.                     inst.show();
  413.                 } else {
  414.                     qtrans_hook_on_tinyMCE('qtrans_textarea_'+id);
  415.                 }
  416.                
  417.                 dom.removeClass(wrap_id, 'html-active');
  418.                 dom.addClass(wrap_id, 'tmce-active');
  419.                 setUserSetting('editor', 'tinymce');
  420.             } else {
  421.                 // switch content
  422.                 qtrans_assign('qtrans_textarea_'+id,qtrans_use(lang,ta.value));
  423.             }
  424.         }
  425.         ";
  426. }
  427.  
  428. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement