Advertisement
Wh0CaREs

Qtranslate Full

Nov 24th, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.51 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 Widget */
  21.  
  22. class qTranslateWidget extends WP_Widget {
  23.     function qTranslateWidget() {
  24.         $widget_ops = array('classname' => 'widget_qtranslate', 'description' => __('Allows your visitors to choose a Language.','qtranslate') );
  25.         $this->WP_Widget('qtranslate', __('qTranslate Language Chooser','qtranslate'), $widget_ops);
  26.     }
  27.    
  28.     function widget($args, $instance) {
  29.         extract($args);
  30.        
  31.         echo $before_widget;
  32.         $title = empty($instance['title']) ? __('Language', 'qtranslate') : apply_filters('widget_title', $instance['title']);
  33.         $hide_title = empty($instance['hide-title']) ? false : 'on';
  34.         $type = $instance['type'];
  35.         if($type!='text'&&$type!='image'&&$type!='both'&&$type!='dropdown') $type='text';
  36.  
  37.         if($hide_title!='on') { echo $before_title . $title . $after_title; };
  38.         qtrans_generateLanguageSelectCode($type, $this->id);
  39.         echo $after_widget;
  40.     }
  41.    
  42.     function update($new_instance, $old_instance) {
  43.         $instance = $old_instance;
  44.         $instance['title'] = $new_instance['title'];
  45.         if(isset($new_instance['hide-title'])) $instance['hide-title'] = $new_instance['hide-title'];
  46.         $instance['type'] = $new_instance['type'];
  47.  
  48.         return $instance;
  49.     }
  50.    
  51.     function form($instance) {
  52.         $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'hide-title' => false, 'type' => 'text' ) );
  53.         $title = $instance['title'];
  54.         $hide_title = $instance['hide-title'];
  55.         $type = $instance['type'];
  56. ?>
  57.         <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'qtranslate'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></label></p>
  58.         <p><label for="<?php echo $this->get_field_id('hide-title'); ?>"><?php _e('Hide Title:', 'qtranslate'); ?> <input type="checkbox" id="<?php echo $this->get_field_id('hide-title'); ?>" name="<?php echo $this->get_field_name('hide-title'); ?>" <?php echo ($hide_title=='on')?'checked="checked"':''; ?>/></label></p>
  59.         <p><?php _e('Display:', 'qtranslate'); ?></p>
  60.         <p><label for="<?php echo $this->get_field_id('type'); ?>1"><input type="radio" name="<?php echo $this->get_field_name('type'); ?>" id="<?php echo $this->get_field_id('type'); ?>1" value="text"<?php echo ($type=='text')?' checked="checked"':'' ?>/> <?php _e('Text only', 'qtranslate'); ?></label></p>
  61.         <p><label for="<?php echo $this->get_field_id('type'); ?>2"><input type="radio" name="<?php echo $this->get_field_name('type'); ?>" id="<?php echo $this->get_field_id('type'); ?>2" value="image"<?php echo ($type=='image')?' checked="checked"':'' ?>/> <?php _e('Image only', 'qtranslate'); ?></label></p>
  62.         <p><label for="<?php echo $this->get_field_id('type'); ?>3"><input type="radio" name="<?php echo $this->get_field_name('type'); ?>" id="<?php echo $this->get_field_id('type'); ?>3" value="both"<?php echo ($type=='both')?' checked="checked"':'' ?>/> <?php _e('Text and Image', 'qtranslate'); ?></label></p>
  63.         <p><label for="<?php echo $this->get_field_id('type'); ?>4"><input type="radio" name="<?php echo $this->get_field_name('type'); ?>" id="<?php echo $this->get_field_id('type'); ?>4" value="dropdown"<?php echo ($type=='dropdown')?' checked="checked"':'' ?>/> <?php _e('Dropdown Box', 'qtranslate'); ?></label></p>
  64. <?php
  65.     }
  66. }
  67.  
  68. // Language Select Code for non-Widget users
  69. function qtrans_generateLanguageSelectCode($style='', $id='') {
  70.     global $q_config;
  71.     if($style=='') $style='text';
  72.     if(is_bool($style)&&$style) $style='image';
  73.     if(is_404()) $url = get_option('home'); else $url = '';
  74.     if($id=='') $id = 'qtranslate';
  75.     $id .= '-chooser';
  76.     switch($style) {
  77.         case 'image':
  78.         case 'text':
  79.         case 'dropdown':
  80.             echo '<ul class="qtrans_language_chooser" id="'.$id.'">';
  81.             foreach(qtrans_getSortedLanguages() as $language) {
  82.                 $classes = array('lang-'.$language);
  83.                 if($language == $q_config['language'])
  84.                     $classes[] = 'active';
  85.                 echo '<li class="'. implode(' ', $classes) .'"><a href="'.qtrans_convertURL($url, $language).'"';
  86.                 // set hreflang
  87.                 echo ' hreflang="'.$language.'" title="'.$q_config['language_name'][$language].'"';
  88.                 if($style=='image')
  89.                     echo ' class="qtrans_flag qtrans_flag_'.$language.'"';
  90.                 echo '><span';
  91.                 if($style=='image')
  92.                     echo ' style="display:none"';
  93.                 echo '>'.$q_config['language_name'][$language].'</span></a></li>';
  94.             }
  95.             echo "</ul><div class=\"qtrans_widget_end\"></div>";
  96.             if($style=='dropdown') {
  97.                 echo "<script type=\"text/javascript\">\n// <![CDATA[\r\n";
  98.                 echo "var lc = document.getElementById('".$id."');\n";
  99.                 echo "var s = document.createElement('select');\n";
  100.                 echo "s.id = 'qtrans_select_".$id."';\n";
  101.                 echo "lc.parentNode.insertBefore(s,lc);";
  102.                 // create dropdown fields for each language
  103.                 foreach(qtrans_getSortedLanguages() as $language) {
  104.                     echo qtrans_insertDropDownElement($language, qtrans_convertURL($url, $language), $id);
  105.                 }
  106.                 // hide html language chooser text
  107.                 echo "s.onchange = function() { document.location.href = this.value;}\n";
  108.                 echo "lc.style.display='none';\n";
  109.                 echo "// ]]>\n</script>\n";
  110.             }
  111.             break;
  112.         case 'both':
  113.             echo '<ul class="qtrans_language_chooser" id="'.$id.'">';
  114.             foreach(qtrans_getSortedLanguages() as $language) {
  115.                 echo '<li';
  116.                 if($language == $q_config['language'])
  117.                     echo ' class="active"';
  118.                 echo '><a href="'.qtrans_convertURL($url, $language).'"';
  119.                 echo ' class="qtrans_flag_'.$language.' qtrans_flag_and_text" title="'.$q_config['language_name'][$language].'"';
  120.                 echo '><span>'.$q_config['language_name'][$language].'</span></a></li>';
  121.             }
  122.             echo "</ul><div class=\"qtrans_widget_end\"></div>";
  123.             break;
  124.     }
  125. }
  126.  
  127. function qtrans_widget_init() {
  128.     register_widget('qTranslateWidget');
  129. }
  130.  
  131. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement