GTranslate Support Forum. Version: 3.0.9 Author: Translate AI Multilingual Solutions Author URI: https://gtranslate.io Text Domain: gtranslate */ /* Copyright 2010 - 2022 GTranslate Inc. ( website: https://gtranslate.com ) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ add_action('widgets_init', array('GTranslate', 'register')); register_activation_hook(__FILE__, array('GTranslate', 'activate')); register_deactivation_hook(__FILE__, array('GTranslate', 'deactivate')); add_filter('plugin_action_links_' . plugin_basename(__FILE__), array('GTranslate', 'settings_link')); add_action('admin_menu', array('GTranslate', 'admin_menu')); add_action('init', array('GTranslate', 'enqueue_scripts')); add_action('plugins_loaded', array('GTranslate', 'load_textdomain')); add_action('send_headers', array('GTranslate', 'set_dns_prefetch_header')); add_filter('script_loader_tag', array('GTranslate', 'add_script_attributes'), 10, 3); add_filter('walker_nav_menu_start_el', array('GTranslate', 'render_menu_items') , 10 , 4); add_shortcode('GTranslate', array('GTranslate', 'render_shortcode')); add_shortcode('gtranslate', array('GTranslate', 'render_shortcode')); add_shortcode('gt-link', array('GTranslate', 'render_single_item')); class GTranslate extends WP_Widget { public static $lang_array = array('en'=>'English','ar'=>'Arabic','bg'=>'Bulgarian','zh-CN'=>'Chinese (Simplified)','zh-TW'=>'Chinese (Traditional)','hr'=>'Croatian','cs'=>'Czech','da'=>'Danish','nl'=>'Dutch','fi'=>'Finnish','fr'=>'French','de'=>'German','el'=>'Greek','hi'=>'Hindi','it'=>'Italian','ja'=>'Japanese','ko'=>'Korean','no'=>'Norwegian','pl'=>'Polish','pt'=>'Portuguese','ro'=>'Romanian','ru'=>'Russian','es'=>'Spanish','sv'=>'Swedish','ca'=>'Catalan','tl'=>'Filipino','iw'=>'Hebrew','id'=>'Indonesian','lv'=>'Latvian','lt'=>'Lithuanian','sr'=>'Serbian','sk'=>'Slovak','sl'=>'Slovenian','uk'=>'Ukrainian','vi'=>'Vietnamese','sq'=>'Albanian','et'=>'Estonian','gl'=>'Galician','hu'=>'Hungarian','mt'=>'Maltese','th'=>'Thai','tr'=>'Turkish','fa'=>'Persian','af'=>'Afrikaans','ms'=>'Malay','sw'=>'Swahili','ga'=>'Irish','cy'=>'Welsh','be'=>'Belarusian','is'=>'Icelandic','mk'=>'Macedonian','yi'=>'Yiddish','hy'=>'Armenian','az'=>'Azerbaijani','eu'=>'Basque','ka'=>'Georgian','ht'=>'Haitian Creole','ur'=>'Urdu','bn' => 'Bengali','bs' => 'Bosnian','ceb' => 'Cebuano','eo' => 'Esperanto','gu' => 'Gujarati','ha' => 'Hausa','hmn' => 'Hmong','ig' => 'Igbo','jw' => 'Javanese','kn' => 'Kannada','km' => 'Khmer','lo' => 'Lao','la' => 'Latin','mi' => 'Maori','mr' => 'Marathi','mn' => 'Mongolian','ne' => 'Nepali','pa' => 'Punjabi','so' => 'Somali','ta' => 'Tamil','te' => 'Telugu','yo' => 'Yoruba','zu' => 'Zulu','my' => 'Myanmar (Burmese)','ny' => 'Chichewa','kk' => 'Kazakh','mg' => 'Malagasy','ml' => 'Malayalam','si' => 'Sinhala','st' => 'Sesotho','su' => 'Sundanese','tg' => 'Tajik','uz' => 'Uzbek','am' => 'Amharic','co' => 'Corsican','haw' => 'Hawaiian','ku' => 'Kurdish (Kurmanji)','ky' => 'Kyrgyz','lb' => 'Luxembourgish','ps' => 'Pashto','sm' => 'Samoan','gd' => 'Scottish Gaelic','sn' => 'Shona','sd' => 'Sindhi','fy' => 'Frisian','xh' => 'Xhosa'); public static $lang_array_native_json = '{"af":"Afrikaans","sq":"Shqip","am":"\u12a0\u121b\u122d\u129b","ar":"\u0627\u0644\u0639\u0631\u0628\u064a\u0629","hy":"\u0540\u0561\u0575\u0565\u0580\u0565\u0576","az":"Az\u0259rbaycan dili","eu":"Euskara","be":"\u0411\u0435\u043b\u0430\u0440\u0443\u0441\u043a\u0430\u044f \u043c\u043e\u0432\u0430","bn":"\u09ac\u09be\u0982\u09b2\u09be","bs":"Bosanski","bg":"\u0411\u044a\u043b\u0433\u0430\u0440\u0441\u043a\u0438","ca":"Catal\u00e0","ceb":"Cebuano","ny":"Chichewa","zh-CN":"\u7b80\u4f53\u4e2d\u6587","zh-TW":"\u7e41\u9ad4\u4e2d\u6587","co":"Corsu","hr":"Hrvatski","cs":"\u010ce\u0161tina\u200e","da":"Dansk","nl":"Nederlands","en":"English","eo":"Esperanto","et":"Eesti","tl":"Filipino","fi":"Suomi","fr":"Fran\u00e7ais","fy":"Frysk","gl":"Galego","ka":"\u10e5\u10d0\u10e0\u10d7\u10e3\u10da\u10d8","de":"Deutsch","el":"\u0395\u03bb\u03bb\u03b7\u03bd\u03b9\u03ba\u03ac","gu":"\u0a97\u0ac1\u0a9c\u0ab0\u0abe\u0aa4\u0ac0","ht":"Kreyol ayisyen","ha":"Harshen Hausa","haw":"\u014clelo Hawai\u02bbi","iw":"\u05e2\u05b4\u05d1\u05b0\u05e8\u05b4\u05d9\u05ea","hi":"\u0939\u093f\u0928\u094d\u0926\u0940","hmn":"Hmong","hu":"Magyar","is":"\u00cdslenska","ig":"Igbo","id":"Bahasa Indonesia","ga":"Gaeilge","it":"Italiano","ja":"\u65e5\u672c\u8a9e","jw":"Basa Jawa","kn":"\u0c95\u0ca8\u0ccd\u0ca8\u0ca1","kk":"\u049a\u0430\u0437\u0430\u049b \u0442\u0456\u043b\u0456","km":"\u1797\u17b6\u179f\u17b6\u1781\u17d2\u1798\u17c2\u179a","ko":"\ud55c\uad6d\uc5b4","ku":"\u0643\u0648\u0631\u062f\u06cc\u200e","ky":"\u041a\u044b\u0440\u0433\u044b\u0437\u0447\u0430","lo":"\u0e9e\u0eb2\u0eaa\u0eb2\u0ea5\u0eb2\u0ea7","la":"Latin","lv":"Latvie\u0161u valoda","lt":"Lietuvi\u0173 kalba","lb":"L\u00ebtzebuergesch","mk":"\u041c\u0430\u043a\u0435\u0434\u043e\u043d\u0441\u043a\u0438 \u0458\u0430\u0437\u0438\u043a","mg":"Malagasy","ms":"Bahasa Melayu","ml":"\u0d2e\u0d32\u0d2f\u0d3e\u0d33\u0d02","mt":"Maltese","mi":"Te Reo M\u0101ori","mr":"\u092e\u0930\u093e\u0920\u0940","mn":"\u041c\u043e\u043d\u0433\u043e\u043b","my":"\u1017\u1019\u102c\u1005\u102c","ne":"\u0928\u0947\u092a\u093e\u0932\u0940","no":"Norsk bokm\u00e5l","ps":"\u067e\u069a\u062a\u0648","fa":"\u0641\u0627\u0631\u0633\u06cc","pl":"Polski","pt":"Portugu\u00eas","pa":"\u0a2a\u0a70\u0a1c\u0a3e\u0a2c\u0a40","ro":"Rom\u00e2n\u0103","ru":"\u0420\u0443\u0441\u0441\u043a\u0438\u0439","sm":"Samoan","gd":"G\u00e0idhlig","sr":"\u0421\u0440\u043f\u0441\u043a\u0438 \u0458\u0435\u0437\u0438\u043a","st":"Sesotho","sn":"Shona","sd":"\u0633\u0646\u068c\u064a","si":"\u0dc3\u0dd2\u0d82\u0dc4\u0dbd","sk":"Sloven\u010dina","sl":"Sloven\u0161\u010dina","so":"Afsoomaali","es":"Espa\u00f1ol","su":"Basa Sunda","sw":"Kiswahili","sv":"Svenska","tg":"\u0422\u043e\u04b7\u0438\u043a\u04e3","ta":"\u0ba4\u0bae\u0bbf\u0bb4\u0bcd","te":"\u0c24\u0c46\u0c32\u0c41\u0c17\u0c41","th":"\u0e44\u0e17\u0e22","tr":"T\u00fcrk\u00e7e","uk":"\u0423\u043a\u0440\u0430\u0457\u043d\u0441\u044c\u043a\u0430","ur":"\u0627\u0631\u062f\u0648","uz":"O\u2018zbekcha","vi":"Ti\u1ebfng Vi\u1ec7t","cy":"Cymraeg","xh":"isiXhosa","yi":"\u05d9\u05d9\u05d3\u05d9\u05e9","yo":"Yor\u00f9b\u00e1","zu":"Zulu"}'; public static function activate() { $data = array( 'gtranslate_title' => esc_html__('Website Translator', 'gtranslate'), ); $data = get_option('GTranslate'); self::load_defaults($data); add_option('GTranslate', $data); } public static function deactivate() { // delete_option('GTranslate'); } public static function settings_link($links) { $settings_link = array(''.esc_html__('Settings', 'gtranslate').''); return array_merge($links, $settings_link); } public static function control() { $data = get_option('GTranslate'); ?>

'.esc_html__('GTranslate Settings', 'gtranslate').''); ?>

; rel=dns-prefetch', false); } public static function enqueue_scripts() { $data = get_option('GTranslate'); self::load_defaults($data); if(is_admin()) wp_enqueue_script('jquery'); // make sure main_lang is set correctly in config.php file if($data['pro_version'] or $data['enterprise_version']) { include dirname(__FILE__) . '/url_addon/config.php'; if($main_lang != $data['default_language']) { // update main_lang in config.php $config_file = dirname(__FILE__) . '/url_addon/config.php'; if(is_readable($config_file) and is_writable($config_file)) { $config = file_get_contents($config_file); if(strpos($config, 'main_lang') !== false) { $config = preg_replace('/\$main_lang = \'[a-z-]{2,5}\'/i', '$main_lang = \''.$data['default_language'].'\'', $config); if(is_string($config) and strlen($config) > 10) file_put_contents($config_file, $config); } } } } } public static function load_textdomain() { load_plugin_textdomain('gtranslate'); // set correct language direction global $text_direction; if(isset($_SERVER['HTTP_X_GT_LANG']) and in_array($_SERVER['HTTP_X_GT_LANG'], array('ar', 'iw', 'fa'))) $text_direction = 'rtl'; elseif(isset($_SERVER['HTTP_X_GT_LANG'])) $text_direction = 'ltr'; } public static function add_script_attributes($tag, $handle, $src) { if(!empty($src) and strpos($handle, 'gt_widget_script_') === 0) { $orig_url = strtok($_SERVER['REQUEST_URI'], '?'); $orig_domain = parse_url(site_url(), PHP_URL_HOST); $widget_id = str_replace('gt_widget_script_', '', $handle); $tag = strstr($tag, '', true) . ''; } return $tag; } public static function render_shortcode($atts) { if(!is_array($atts)) $atts = array(); $atts['position'] = 'inline'; $atts['wrapper_selector'] = '.gtranslate_wrapper'; return self::get_widget_code($atts); } public static function render_single_item($atts) { if(!is_array($atts) or !isset($atts['lang'])) return; $data = get_option('GTranslate'); self::load_defaults($data); $lang_code = $atts['lang']; if(!isset($atts['label'])) { $lang_array = $data['native_language_names'] ? json_decode(GTranslate::$lang_array_native_json, true) : GTranslate::$lang_array; $label = $lang_array[$lang_code]; } else { $label = $atts['label']; } $widget_look = isset($atts['widget_look']) ? $atts['widget_look'] : $data['widget_look']; if(!in_array($widget_look, array('flags', 'flags_code', 'flags_name', 'lang_codes', 'lang_names'))) $widget_look = 'flags_name'; $flag_size = $data['flag_size']; $flag_src = self::get_flag_src($lang_code); if(isset($atts['current_wrapper'])) $add_class = $lang_code == $data['default_language'] ? ' class="gt-current-wrapper notranslate"' : ' class="notranslate"'; else $add_class = $lang_code == $data['default_language'] ? ' class="gt-current-lang notranslate"' : ' class="notranslate"'; switch($widget_look) { case 'lang_names': $el_code = '' . esc_html($label) . ''; break; case 'lang_codes': $el_code = '' . esc_html(strtoupper($lang_code)) . ''; break; case 'flags': $el_code = '' . esc_attr($lang_code) . ''; break; case 'flags_name': $el_code = '' . esc_attr($lang_code) . ' ' . esc_html($label) . ''; break; case 'flags_code': $el_code = '' . esc_attr($lang_code) . ' ' . esc_html(strtoupper($lang_code)) . ''; break; } global $gt_base_loaded; if(!$gt_base_loaded) { $gt_base_loaded = true; $gt_settings = self::load_settings($data); $unique_id = wp_rand(10000000, 88888888); // remove excess settings based on widget_look to keep front-end code small $old_settings = $gt_settings; $gt_settings = array(); $keep_keys = array('default_language', 'languages', 'url_structure', 'native_language_names', 'detect_browser_language', 'flag_style', 'flag_size', 'alt_flags', 'custom_domains', 'custom_css'); foreach($keep_keys as $key) if(isset($old_settings[$key]) and $old_settings[$key] !== '') $gt_settings[$key] = $old_settings[$key]; if($data['enable_cdn']) { wp_enqueue_script('gt_widget_script_' . $unique_id, 'https://cdn.gtranslate.net/widgets/latest/base.js', array(), '', true); } else { $base_path = plugins_url('', __FILE__); if($data['enterprise_version']) $gt_settings['flags_location'] = $base_path . '/flags/'; else $gt_settings['flags_location'] = wp_make_link_relative($base_path) . '/flags/'; wp_enqueue_script('gt_widget_script_' . $unique_id, $base_path . '/js/base.js', array(), '', true); } wp_add_inline_script('gt_widget_script_' . $unique_id, "window.gtranslateSettings = /* document.write */ window.gtranslateSettings || {};window.gtranslateSettings['" . $unique_id . "'] = " . json_encode($gt_settings) . ";", 'before'); } return $el_code; } public static function render_menu_items($item_output, $item, $depth, $args) { if(!empty($item->post_title) and strpos($item->post_title, '[gtranslate') !== false) return do_shortcode($item->post_title); if(empty($item->description) or strpos($item->description, '[gt-link') === false) return $item_output; $output = do_shortcode($item->description); if(!empty($output)) return $output; return $item_output; } public static function get_flag_src($lang) { $data = get_option('GTranslate'); self::load_defaults($data); if($data['enable_cdn']) $base_src = 'https://cdn.gtranslate.net/flags/'; else $base_src = plugins_url('', __FILE__) . '/flags/'; if($data['flag_style'] == '2d') $base_src .= 'svg/'; else $base_src .= $data['flag_size'] . '/'; $flag_ext = $data['flag_style'] == '3d' ? '.png' : '.svg'; $alt_flags = array(); $raw_alt_flags = $data['alt_flags']; // example raw_alt_flags: ['us', 'br', 'ar'] foreach($raw_alt_flags as $country_code) { switch($country_code) { case 'us': $alt_flags['en'] = 'en-us'; break; case 'ca': $alt_flags['en'] = 'en-ca'; break; case 'br': $alt_flags['pt'] = 'pt-br'; break; case 'mx': $alt_flags['es'] = 'es-mx'; break; case 'ar': $alt_flags['es'] = 'es-ar'; break; case 'co': $alt_flags['es'] = 'es-co'; break; case 'qc': $alt_flags['fr'] = 'fr-qc'; break; default: break; } } $flag = isset($alt_flags[$lang]) ? $alt_flags[$lang] : $lang; return $base_src . $flag . $flag_ext; } public static function get_widget_code($atts) { $unique_id = wp_rand(10000000, 88888888); $data = get_option('GTranslate'); self::load_defaults($data); if(isset($atts['widget_look']) and in_array($atts['widget_look'], array('float', 'dropdown_with_flags', 'dropdown', 'flags_dropdown', 'popup', 'flags', 'globe', 'flags_code', 'flags_name', 'lang_names', 'lang_codes'))) { $data['widget_look'] = $atts['widget_look']; } if(isset($atts['wrapper_selector'])) { $data['wrapper_selector'] = $atts['wrapper_selector']; } $gt_settings = self::load_settings($data); if(isset($atts['position'])) { $position = $float_position = $atts['position']; } else { if($data['floating_language_selector'] == 'no') $position = $float_position = 'inline'; else $position = $float_position = $data['floating_language_selector']; } if($float_position == 'inline') { $switcher_horizontal_position = 'inline'; $switcher_vertical_position = ''; } else list($switcher_vertical_position, $switcher_horizontal_position) = explode('_', $float_position); $gt_settings['switcher_horizontal_position'] = $switcher_horizontal_position; $gt_settings['switcher_vertical_position'] = $switcher_vertical_position; if($position == 'inline') { $horizontal_position = 'inline'; $vertical_position = ''; } else list($vertical_position, $horizontal_position) = explode('_', $position); $gt_settings['horizontal_position'] = $horizontal_position; $gt_settings['vertical_position'] = $vertical_position; $widget_code = ''; if($gt_settings['wrapper_selector'] == '.gtranslate_wrapper' or empty(trim($gt_settings['wrapper_selector']))) { $gt_settings['wrapper_selector'] = '#gt-wrapper-' . $unique_id; $widget_code .= '
'; } if(strpos($data['widget_look'], '_') !== false) { $widget_short_name = explode('_', $data['widget_look']); foreach($widget_short_name as $i => $segment) $widget_short_name[$i] = substr($segment, 0, 1); $widget_short_name = implode('', $widget_short_name); } else { $widget_short_name = $data['widget_look']; } // remove excess settings based on widget_look to keep front-end code small $old_settings = $gt_settings; $gt_settings = array(); switch($data['widget_look']) { case 'float': $keep_keys = array('default_language', 'languages', 'url_structure', 'native_language_names', 'detect_browser_language', 'flag_style', 'wrapper_selector', 'alt_flags', 'custom_domains', 'float_switcher_open_direction', 'switcher_horizontal_position', 'switcher_vertical_position', 'custom_css'); break; case 'dropdown_with_flags': $keep_keys = array('default_language', 'languages', 'url_structure', 'native_language_names', 'detect_browser_language', 'flag_style', 'flag_size', 'wrapper_selector', 'alt_flags', 'custom_domains', 'switcher_open_direction', 'switcher_horizontal_position', 'switcher_vertical_position', 'switcher_text_color', 'switcher_arrow_color', 'switcher_border_color', 'switcher_background_color', 'switcher_background_shadow_color', 'switcher_background_hover_color', 'dropdown_text_color', 'dropdown_hover_color', 'dropdown_background_color', 'custom_css'); break; case 'dropdown': $keep_keys = array('default_language', 'languages', 'url_structure', 'native_language_names', 'detect_browser_language', 'wrapper_selector', 'custom_domains', 'select_language_label', 'custom_css', 'horizontal_position', 'vertical_position'); break; case 'flags_dropdown': $keep_keys = array('default_language', 'languages', 'dropdown_languages', 'url_structure', 'native_language_names', 'detect_browser_language', 'add_new_line', 'flag_style', 'flag_size', 'wrapper_selector', 'alt_flags', 'custom_domains', 'custom_css', 'horizontal_position', 'vertical_position'); break; case 'popup': case 'flags': case 'flags_name': case 'flags_code': $keep_keys = array('default_language', 'languages', 'url_structure', 'native_language_names', 'detect_browser_language', 'flag_style', 'flag_size', 'wrapper_selector', 'alt_flags', 'custom_domains', 'custom_css', 'horizontal_position', 'vertical_position'); break; case 'globe': $keep_keys = array('default_language', 'languages', 'url_structure', 'native_language_names', 'detect_browser_language', 'wrapper_selector', 'flag_size', 'globe_size', 'alt_flags', 'globe_color', 'custom_domains', 'custom_css', 'horizontal_position', 'vertical_position'); break; case 'lang_codes': case 'lang_names': $keep_keys = array('default_language', 'languages', 'url_structure', 'native_language_names', 'detect_browser_language', 'wrapper_selector', 'custom_domains', 'custom_css', 'horizontal_position', 'vertical_position'); break; default: $keep_keys = array_keys($old_settings); break; } foreach($keep_keys as $key) if(isset($old_settings[$key]) and $old_settings[$key] !== '') $gt_settings[$key] = $old_settings[$key]; // overwrite settings from shortcode attributes /* todo: sanitize input if(is_array($atts)) { foreach($keep_keys as $key) if(isset($atts[$key]) and $atts[$key] !== '') $gt_settings[$key] = $atts[$key]; } */ // add necessary js if($data['enable_cdn']) { wp_enqueue_script('gt_widget_script_' . $unique_id, 'https://cdn.gtranslate.net/widgets/latest/' . $widget_short_name . '.js', array(), '', true); } else { $base_path = plugins_url('', __FILE__); if($data['widget_look'] == 'globe') { if($data['enterprise_version']) $gt_settings['flags_location'] = $base_path . '/flags/svg/'; else $gt_settings['flags_location'] = wp_make_link_relative($base_path) . '/flags/svg/'; } else { if($data['enterprise_version']) $gt_settings['flags_location'] = $base_path . '/flags/'; else $gt_settings['flags_location'] = wp_make_link_relative($base_path) . '/flags/'; } wp_enqueue_script('gt_widget_script_' . $unique_id, $base_path . '/js/' . $widget_short_name . '.js', array(), '', true); } wp_add_inline_script('gt_widget_script_' . $unique_id, "window.gtranslateSettings = /* document.write */ window.gtranslateSettings || {};window.gtranslateSettings['" . $unique_id . "'] = " . json_encode($gt_settings) . ";", 'before'); return $widget_code; } public static function register() { register_widget('GTranslateWidget'); } public static function admin_menu() { add_options_page(__('GTranslate Options', 'gtranslate'), 'GTranslate', 'administrator', 'gtranslate_options', array('GTranslate', 'options')); } public static function options() { ?>

G|translate

'+l+''+data[l]+''); jQuery('.custom_domains_list').show(); }, error: function(e) { alert('Something strange happened, please try again later.'); jQuery('#custom_domains').prop('checked', false); RefreshDoWidgetCode(); jQuery('#custom_domains_status_sync').hide(); jQuery('.custom_domains_list').hide(); } }); } function RefreshDoWidgetCode() { var widget_look = jQuery('#widget_look').val(); var switcher_horizontal_position = jQuery('#floating_language_selector').val().split('_').pop(); var switcher_vertical_position = jQuery('#floating_language_selector').val().split('_').shift(); var horizontal_position = jQuery('#floating_language_selector').val().split('_').pop(); var vertical_position = jQuery('#floating_language_selector').val().split('_').shift(); var float_switcher_open_direction = jQuery('#float_switcher_open_direction').val(); var switcher_open_direction = jQuery('#switcher_open_direction').val(); var default_language = jQuery('#default_language').val(); var native_language_names = jQuery('#native_language_names').prop('checked'); var detect_browser_language = jQuery('#detect_browser_language').prop('checked'); var add_new_line = jQuery('#add_new_line').prop('checked'); var select_language_label = jQuery('#select_language_label').val(); var flag_size = parseInt(jQuery('#flag_size').val()); var flag_style = jQuery('#flag_style').val(); var globe_size = parseInt(jQuery('#globe_size').val()); var globe_color = jQuery('#globe_color').val(); var pro_version = jQuery('#pro_version').prop('checked'); var enterprise_version = jQuery('#enterprise_version').prop('checked'); var url_structure = 'none'; if(pro_version) url_structure = 'sub_directory'; else if(enterprise_version) url_structure = 'sub_domain'; var wrapper_selector = jQuery('#wrapper_selector').val(); var dropdown_languages = jQuery('input[name="incl_langs[]"]:checked').map(function(){return jQuery(this).val()}).get(); var languages = jQuery('input[name="fincl_langs[]"]:checked').map(function(){return jQuery(this).val()}).get(); if(widget_look == 'dropdown' || widget_look == 'globe' || widget_look == 'lang_names' || widget_look == 'lang_codes') languages = dropdown_languages; var custom_domains = jQuery('#custom_domains:checked').length > 0 ? true : false; var custom_domains_data = JSON.parse(jQuery('#custom_domains_data').val()||'{}'); var enable_cdn = jQuery('#enable_cdn:checked').length > 0 ? true : false; var show_in_menu = jQuery('#show_in_menu').val(); var floating_language_selector = jQuery('#floating_language_selector').val(); var email_translation = jQuery('#email_translation').prop('checked'); var switcher_text_color = jQuery('#switcher_text_color').val(); var switcher_arrow_color = jQuery('#switcher_arrow_color').val(); var switcher_border_color = jQuery('#switcher_border_color').val(); var switcher_background_color = jQuery('#switcher_background_color').val(); var switcher_background_shadow_color = jQuery('#switcher_background_shadow_color').val(); var switcher_background_hover_color = jQuery('#switcher_background_hover_color').val(); var dropdown_text_color = jQuery('#dropdown_text_color').val(); var dropdown_hover_color = jQuery('#dropdown_hover_color').val(); var dropdown_background_color = jQuery('#dropdown_background_color').val(); var alt_flags = {}; jQuery('input[name="alt_flags[]"]:checked').map(function(){return jQuery(this).val()}).get().forEach(function(country_code) { switch(country_code) { case 'us': alt_flags['en'] = 'usa'; break; case 'ca': alt_flags['en'] = 'canada'; break; case 'br': alt_flags['pt'] = 'brazil'; break; case 'mx': alt_flags['es'] = 'mexico'; break; case 'ar': alt_flags['es'] = 'argentina'; break; case 'co': alt_flags['es'] = 'colombia'; break; case 'qc': alt_flags['fr'] = 'quebec'; break; default: break; } }); var custom_css = jQuery('#custom_css').val(); var gt_settings = { default_language: default_language, url_structure: url_structure, switcher_horizontal_position: 'inline', horizontal_position: 'inline', float_switcher_open_direction: float_switcher_open_direction, switcher_open_direction: switcher_open_direction, native_language_names: native_language_names, add_new_line: add_new_line, select_language_label: select_language_label, flag_size: flag_size, flag_style: flag_style, globe_size: globe_size, globe_color: globe_color, languages: languages, dropdown_languages: dropdown_languages, custom_domains: custom_domains ? custom_domains_data : null, alt_flags: alt_flags, switcher_text_color: switcher_text_color, switcher_arrow_color: switcher_arrow_color, switcher_border_color: switcher_border_color, switcher_background_color: switcher_background_color, switcher_background_shadow_color: switcher_background_shadow_color, switcher_background_hover_color: switcher_background_hover_color, dropdown_text_color: dropdown_text_color, dropdown_hover_color: dropdown_hover_color, dropdown_background_color: dropdown_background_color, custom_css: custom_css, }; // disable loading on hover window.gt_translate_script = true; // make sure default language is on if(widget_look == 'flags_dropdown' || widget_look == 'float' || widget_look == 'dropdown_with_flags' || widget_look == 'flags' || widget_look == 'flags_name' || widget_look == 'flags_code' || widget_look == 'popup') jQuery('#fincl_langs'+default_language).prop('checked', true); if(widget_look == 'dropdown' || widget_look == 'globe' || widget_look == 'lang_names' || widget_look == 'lang_codes') jQuery('#incl_langs'+default_language).prop('checked', true); if(pro_version || enterprise_version) { if(enterprise_version) { jQuery('#custom_domains_option').show(); if(custom_domains) jQuery('.custom_domains_list').show(); else jQuery('.custom_domains_list').hide(); } else { jQuery('#custom_domains_option').hide(); jQuery('.custom_domains_list').hide(); } jQuery('#url_translation_option').show(); jQuery('#hreflang_tags_option').show(); jQuery('#email_translation_option').show(); if(email_translation) jQuery('#email_translation_debug_option').show(); else jQuery('#email_translation_debug_option').hide(); } else { jQuery('#custom_domains_option').hide(); jQuery('#url_translation_option').hide(); jQuery('#hreflang_tags_option').hide(); jQuery('#email_translation_option').hide(); jQuery('#email_translation_debug_option').hide(); } if(widget_look == 'dropdown' || widget_look == 'flags_dropdown' || widget_look == 'globe' || widget_look == 'lang_names' || widget_look == 'lang_codes') { jQuery('#dropdown_languages_option').show(); } else { jQuery('#dropdown_languages_option').hide(); } if(widget_look == 'globe') { jQuery('#alternative_flags_option').show(); } else { jQuery('#alternative_flags_option').hide(); } if(widget_look == 'flags' || widget_look == 'flags_dropdown' || widget_look == 'float' || widget_look == 'dropdown_with_flags' || widget_look == 'flags_name' || widget_look == 'flags_code' || widget_look == 'popup') { jQuery('#flag_languages_option').show(); jQuery('#alternative_flags_option').show(); } else { jQuery('#flag_languages_option').hide(); if(widget_look != 'globe') jQuery('#alternative_flags_option').hide(); } if(widget_look == 'flags_dropdown') { jQuery('#line_break_option').show(); } else { jQuery('#line_break_option').hide(); } if(widget_look == 'dropdown' || widget_look == 'lang_names' || widget_look == 'lang_codes' || widget_look == 'globe') { jQuery('#flag_style_option').hide(); if(widget_look == 'globe') jQuery('#flag_size_option').show(); else jQuery('#flag_size_option').hide(); } else { jQuery('#flag_style_option').show(); if(widget_look == 'float') jQuery('#flag_size_option').hide(); else jQuery('#flag_size_option').show(); } if(widget_look == 'dropdown_with_flags') { jQuery('.switcher_color_options').show(); jQuery('#switcher_open_direction_option').show(); } else { jQuery('.switcher_color_options').hide(); jQuery('#switcher_open_direction_option').hide(); } if(widget_look == 'globe') { jQuery('#globe_size_option').show(); jQuery('.globe_color_options').show(); } else { jQuery('#globe_size_option').hide(); jQuery('.globe_color_options').hide(); } if(widget_look == 'dropdown') { jQuery('#select_language_label_option').show(); } else { jQuery('#select_language_label_option').hide(); } if(widget_look == 'float') { jQuery('#float_switcher_open_direction_option').show(); } else { jQuery('#float_switcher_open_direction_option').hide(); } var init_widget_code = '
'; init_widget_code += ' ' . __('Changes Saved', 'gtranslate') . '

'; update_option('GTranslate', $data); if($data['pro_version']) { // check if rewrite rules are in place $htaccess_file = get_home_path() . '.htaccess'; // todo: use insert_with_markers functions instead if(is_writeable($htaccess_file)) { $htaccess = file_get_contents($htaccess_file); if(strpos($htaccess, 'gtranslate.php') === false) { // no config rules $rewrite_rules = file_get_contents(dirname(__FILE__) . '/url_addon/rewrite.txt'); $rewrite_rules = str_replace('GTRANSLATE_PLUGIN_PATH', str_replace(str_replace(array('https:', 'http:'), array(':', ':'), home_url()), '', str_replace(array('https:', 'http:'), array(':', ':'), plugins_url())) . '/gtranslate', $rewrite_rules); $htaccess = $rewrite_rules . "\r\n\r\n" . $htaccess; if(!empty($htaccess)) { // going to update .htaccess file_put_contents($htaccess_file, $htaccess); echo '

' . __('.htaccess file updated', 'gtranslate') . '

'; } } } else { $rewrite_rules = file_get_contents(dirname(__FILE__) . '/url_addon/rewrite.txt'); $rewrite_rules = str_replace('GTRANSLATE_PLUGIN_PATH', str_replace(home_url(), '', plugins_url()) . '/gtranslate', $rewrite_rules); echo '

' . __('Please add the following rules to the top of your .htaccess file', 'gtranslate') . '

'; echo '
' . $rewrite_rules . '
'; } // update main_lang in config.php $config_file = dirname(__FILE__) . '/url_addon/config.php'; if(is_readable($config_file) and is_writable($config_file)) { $config = file_get_contents($config_file); $config = preg_replace('/\$main_lang = \'[a-z-]{2,5}\'/i', '$main_lang = \''.$data['default_language'].'\'', $config); file_put_contents($config_file, $config); } else { echo '

' . __('Cannot update gtranslate/url_addon/config.php file. Make sure to update it manually and set correct $main_lang.', 'gtranslate') . '

'; } } else { // todo: remove rewrite rules // do nothing } } public static function load_settings($data) { $languages = $data['fincl_langs']; $dropdown_languages = $data['incl_langs']; if($data['widget_look'] == 'dropdown' or $data['widget_look'] == 'globe' or $data['widget_look'] == 'lang_names' or $data['widget_look'] == 'lang_codes') $languages = $dropdown_languages; $url_structure = 'none'; if($data['pro_version']) $url_structure = 'sub_directory'; elseif($data['enterprise_version']) $url_structure = 'sub_domain'; $gt_settings = array( 'default_language' => $data['default_language'], 'languages' => $languages, 'dropdown_languages' => $dropdown_languages, 'url_structure' => $url_structure, 'wrapper_selector' => $data['wrapper_selector'], 'globe_size' => $data['globe_size'], 'globe_color' => $data['globe_color'], 'flag_size' => $data['flag_size'], 'flag_style' => $data['flag_style'], 'custom_domains' => $data['custom_domains'] ? $data['custom_domains_data'] : null, 'float_switcher_open_direction' => $data['float_switcher_open_direction'], 'switcher_open_direction' => $data['switcher_open_direction'], 'native_language_names' => $data['native_language_names'], 'add_new_line' => $data['add_new_line'], 'select_language_label' => $data['select_language_label'], 'detect_browser_language' => $data['detect_browser_language'], 'custom_css' => $data['custom_css'], 'switcher_text_color' => $data['switcher_text_color'], 'switcher_arrow_color' => $data['switcher_arrow_color'], 'switcher_border_color' => $data['switcher_border_color'], 'switcher_background_color' => $data['switcher_background_color'], 'switcher_background_shadow_color' => $data['switcher_background_shadow_color'], 'switcher_background_hover_color' => $data['switcher_background_hover_color'], 'dropdown_text_color' => $data['dropdown_text_color'], 'dropdown_hover_color' => $data['dropdown_hover_color'], 'dropdown_background_color' => $data['dropdown_background_color'], ); $alt_flags = array(); $raw_alt_flags = $data['alt_flags']; // example raw_alt_flags: ['us', 'br', 'ar'] foreach($raw_alt_flags as $country_code) { switch($country_code) { case 'us': $alt_flags['en'] = 'usa'; break; case 'ca': $alt_flags['en'] = 'canada'; break; case 'br': $alt_flags['pt'] = 'brazil'; break; case 'mx': $alt_flags['es'] = 'mexico'; break; case 'ar': $alt_flags['es'] = 'argentina'; break; case 'co': $alt_flags['es'] = 'colombia'; break; case 'qc': $alt_flags['fr'] = 'quebec'; break; default: break; } } $gt_settings['alt_flags'] = $alt_flags; if(!empty($gt_settings['custom_domains'])) $gt_settings['custom_domains'] = json_decode(stripslashes($gt_settings['custom_domains'])); return $gt_settings; } public static function load_defaults(& $data) { if(!is_array($data)) $data = array(); $data['pro_version'] = isset($data['pro_version']) ? $data['pro_version'] : ''; $data['enterprise_version'] = isset($data['enterprise_version']) ? $data['enterprise_version'] : ''; $data['wrapper_selector'] = isset($data['wrapper_selector']) ? $data['wrapper_selector'] : '.gtranslate_wrapper'; $data['custom_domains'] = isset($data['custom_domains']) ? $data['custom_domains'] : ''; $data['custom_domains_data'] = isset($data['custom_domains_data']) ? $data['custom_domains_data'] : ''; $data['url_translation'] = isset($data['url_translation']) ? $data['url_translation'] : ''; $data['add_hreflang_tags'] = isset($data['add_hreflang_tags']) ? $data['add_hreflang_tags'] : ''; $data['email_translation'] = isset($data['email_translation']) ? $data['email_translation'] : ''; $data['email_translation_debug'] = isset($data['email_translation_debug']) ? $data['email_translation_debug'] : ''; $data['show_in_menu'] = isset($data['show_in_menu']) ? $data['show_in_menu'] : ((isset($data['show_in_primary_menu']) and $data['show_in_primary_menu'] == 1) ? 'primary' : ''); $data['floating_language_selector'] = isset($data['floating_language_selector']) ? $data['floating_language_selector'] : 'no'; $data['floating_language_selector'] = str_replace('_sticky', '', $data['floating_language_selector']); $data['native_language_names'] = isset($data['native_language_names']) ? $data['native_language_names'] : ''; $data['enable_cdn'] = isset($data['enable_cdn']) ? $data['enable_cdn'] : ''; $data['detect_browser_language'] = isset($data['detect_browser_language']) ? $data['detect_browser_language'] : ''; $data['add_new_line'] = isset($data['add_new_line']) ? $data['add_new_line'] : 1; $data['select_language_label'] = isset($data['select_language_label']) ? $data['select_language_label'] : 'Select Language'; $data['custom_css'] = isset($data['custom_css']) ? $data['custom_css'] : ''; if(!isset($data['default_language'])) { $locale_map = array('af'=>'af','am'=>'am','arq'=>'ar','ar'=>'ar','ary'=>'ar','az'=>'az','az_TR'=>'az','azb'=>'az','bel'=>'be','bg_BG'=>'bg','bn_BD'=>'bn','bs_BA'=>'bs','ca'=>'ca','bal'=>'ca','ceb'=>'ceb','co'=>'co','cs_CZ'=>'cs','cy'=>'cy','da_DK'=>'da','de_DE'=>'de','de_CH'=>'de','gsw'=>'de','el'=>'el','en_AU'=>'en','en_CA'=>'en','en_NZ'=>'en','en_ZA'=>'en','en_GB'=>'en','eo'=>'eo','es_AR'=>'es','es_CL'=>'es','es_CO'=>'es','es_GT'=>'es','es_MX'=>'es','es_PE'=>'es','es_PR'=>'es','es_ES'=>'es','es_VE'=>'es','et'=>'et','eu'=>'eu','fa_IR'=>'fa','fa_AF'=>'fa','fi'=>'fi','fr_BE'=>'fr','fr_CA'=>'fr','fr_FR'=>'fr','fy'=>'fy','ga'=>'ga','gd'=>'gd','gl_ES'=>'gl','gu'=>'gu','hau'=>'ha','haw_US'=>'haw','hi_IN'=>'hi','hr'=>'hr','hat'=>'ht','hu_HU'=>'hu','hy'=>'hy','id_ID'=>'id','is_IS'=>'is','it_IT'=>'it','he_IL'=>'iw','ja'=>'ja','jv_ID'=>'jw','ka_GE'=>'ka','kk'=>'kk','km'=>'km','kn'=>'kn','ko_KR'=>'ko','ckb'=>'ku','kir'=>'ky','lb_LU'=>'lb','lo'=>'lo','lt_LT'=>'lt','lv'=>'lv','mg_MG'=>'mg','mri'=>'mi','mk_MK'=>'mk','ml_IN'=>'ml','mn'=>'mn','mr'=>'mr','ms_MY'=>'ms','my_MM'=>'my','ne_NP'=>'ne','nl_NL'=>'nl','nl_BE'=>'nl','nb_NO'=>'no','nn_NO'=>'no','pa_IN'=>'pa','pl_PL'=>'pl','ps'=>'ps','pt_BR'=>'pt','pt_PT'=>'pt','ro_RO'=>'ro','ru_RU'=>'ru','snd'=>'sd','si_LK'=>'si','sk_SK'=>'sk','sl_SI'=>'sl','so_SO'=>'so','sq'=>'sq','sr_RS'=>'sr','su_ID'=>'su','sv_SE'=>'sv','sw'=>'sw','ta_IN'=>'ta','ta_LK'=>'ta','te'=>'te','tg'=>'tg','th'=>'th','tr_TR'=>'tr','uk'=>'uk','ur'=>'ur','uz_UZ'=>'uz','vi'=>'vi','xho'=>'xh','yor'=>'yo','zh_CN'=>'zh-CN','zh_HK'=>'zh-CN','zh_TW'=>'zh-TW'); $locale = get_locale(); $data['default_language'] = isset($locale_map[$locale]) ? $locale_map[$locale] : 'en'; } $data['widget_look'] = isset($data['widget_look']) ? $data['widget_look'] : 'float'; $data['flag_size'] = isset($data['flag_size']) ? $data['flag_size'] : 24; $data['flag_style'] = isset($data['flag_style']) ? $data['flag_style'] : '2d'; $data['globe_size'] = isset($data['globe_size']) ? intval($data['globe_size']) : 60; $data['globe_color'] = isset($data['globe_color']) ? sanitize_hex_color($data['globe_color']) : '#66aaff'; $data['incl_langs'] = isset($data['incl_langs']) ? $data['incl_langs'] : array('en', 'es', 'it', 'pt', 'de', 'fr', 'ru', 'nl', 'ar', 'zh-CN'); $data['fincl_langs'] = isset($data['fincl_langs']) ? $data['fincl_langs'] : array('en', 'es', 'it', 'pt', 'de', 'fr', 'ru', 'nl', 'ar', 'zh-CN'); $data['alt_flags'] = isset($data['alt_flags']) ? $data['alt_flags'] : array(); $data['switcher_text_color'] = isset($data['switcher_text_color']) ? $data['switcher_text_color'] : '#666'; $data['switcher_arrow_color'] = isset($data['switcher_arrow_color']) ? $data['switcher_arrow_color'] : '#666'; $data['switcher_border_color'] = isset($data['switcher_border_color']) ? $data['switcher_border_color'] : '#ccc'; $data['switcher_background_color'] = isset($data['switcher_background_color']) ? $data['switcher_background_color'] : '#fff'; $data['switcher_background_shadow_color'] = isset($data['switcher_background_shadow_color']) ? $data['switcher_background_shadow_color'] : '#efefef'; $data['switcher_background_hover_color'] = isset($data['switcher_background_hover_color']) ? $data['switcher_background_hover_color'] : '#fff'; $data['dropdown_text_color'] = isset($data['dropdown_text_color']) ? $data['dropdown_text_color'] : '#000'; $data['dropdown_hover_color'] = isset($data['dropdown_hover_color']) ? $data['dropdown_hover_color'] : '#fff'; // #ffc $data['dropdown_background_color'] = isset($data['dropdown_background_color']) ? $data['dropdown_background_color'] : '#eee'; $data['float_switcher_open_direction'] = isset($data['float_switcher_open_direction']) ? $data['float_switcher_open_direction'] : 'top'; $data['switcher_open_direction'] = isset($data['switcher_open_direction']) ? $data['switcher_open_direction'] : 'top'; $data['language_codes'] = (isset($data['language_codes']) and !empty($data['language_codes'])) ? $data['language_codes'] : 'af,sq,am,ar,hy,az,eu,be,bn,bs,bg,ca,ceb,ny,zh-CN,zh-TW,co,hr,cs,da,nl,en,eo,et,tl,fi,fr,fy,gl,ka,de,el,gu,ht,ha,haw,iw,hi,hmn,hu,is,ig,id,ga,it,ja,jw,kn,kk,km,ko,ku,ky,lo,la,lv,lt,lb,mk,mg,ms,ml,mt,mi,mr,mn,my,ne,no,ps,fa,pl,pt,pa,ro,ru,sm,gd,sr,st,sn,sd,si,sk,sl,so,es,su,sw,sv,tg,ta,te,th,tr,uk,ur,uz,vi,cy,xh,yi,yo,zu'; $data['language_codes2'] = (isset($data['language_codes2']) and !empty($data['language_codes2'])) ? $data['language_codes2'] : 'af,sq,am,ar,hy,az,eu,be,bn,bs,bg,ca,ceb,ny,zh-CN,zh-TW,co,hr,cs,da,nl,en,eo,et,tl,fi,fr,fy,gl,ka,de,el,gu,ht,ha,haw,iw,hi,hmn,hu,is,ig,id,ga,it,ja,jw,kn,kk,km,ko,ku,ky,lo,la,lv,lt,lb,mk,mg,ms,ml,mt,mi,mr,mn,my,ne,no,ps,fa,pl,pt,pa,ro,ru,sm,gd,sr,st,sn,sd,si,sk,sl,so,es,su,sw,sv,tg,ta,te,th,tr,uk,ur,uz,vi,cy,xh,yi,yo,zu'; // add missing languages once if(strlen($data['language_codes']) < strlen($data['language_codes2'])) $data['language_codes'] = $data['language_codes2']; } } class GTranslateWidget extends WP_Widget { function __construct() { parent::__construct('gtranslate', esc_html__('GTranslate', 'gtranslate'), array('description' => esc_html__('GTranslate language switcher', 'gtranslate'))); } public function widget($args, $instance) { echo $args['before_widget']; if(!empty($instance['title'])) { echo $args['before_title'] . apply_filters('widget_title', $instance['title']) . $args['after_title']; } echo GTranslate::get_widget_code(array('position' => 'inline', 'wrapper_selector' => '.gtranslate_wrapper')); echo $args['after_widget']; } public function form($instance) { $title = !empty($instance['title']) ? $instance['title'] : ''; ?>

prefix . '_admin_notice'); if (!isset($gt_settings['disable_admin_notices']) || (isset($gt_settings['disable_admin_notices']) && $gt_settings['disable_admin_notices'] == 0)) { if (current_user_can('manage_options')) { return true; } } return false; } // Primary notice function that can be called from an outside function sending necessary variables public function admin_notice($admin_notices) { // Check options if (!$this->gt_admin_notice()) { return false; } foreach ($admin_notices as $slug => $admin_notice) { // Call for spam protection if ($this->anti_notice_spam()) { return false; } // Check for proper page to display on if (isset( $admin_notices[$slug]['pages']) and is_array( $admin_notices[$slug]['pages'])) { if (!$this->admin_notice_pages($admin_notices[$slug]['pages'])) { return false; } } // Check for required fields if (!$this->required_fields($admin_notices[$slug])) { // Get the current date then set start date to either passed value or current date value and add interval $current_date = current_time("n/j/Y"); $start = (isset($admin_notices[$slug]['start']) ? $admin_notices[$slug]['start'] : $current_date); $start = date("n/j/Y", strtotime($start)); $end = ( isset( $admin_notices[ $slug ]['end'] ) ? $admin_notices[ $slug ]['end'] : $start ); $end = date( "n/j/Y", strtotime( $end ) ); $date_array = explode('/', $start); $interval = (isset($admin_notices[$slug]['int']) ? $admin_notices[$slug]['int'] : 0); $date_array[1] += $interval; $start = date("n/j/Y", mktime(0, 0, 0, $date_array[0], $date_array[1], $date_array[2])); // This is the main notices storage option $admin_notices_option = get_option($this->prefix . '_admin_notice', array()); // Check if the message is already stored and if so just grab the key otherwise store the message and its associated date information if (!array_key_exists( $slug, $admin_notices_option)) { $admin_notices_option[$slug]['start'] = $start; $admin_notices_option[$slug]['int'] = $interval; update_option($this->prefix . '_admin_notice', $admin_notices_option); } // Sanity check to ensure we have accurate information // New date information will not overwrite old date information $admin_display_check = (isset($admin_notices_option[$slug]['dismissed']) ? $admin_notices_option[$slug]['dismissed'] : 0); $admin_display_start = (isset($admin_notices_option[$slug]['start']) ? $admin_notices_option[$slug]['start'] : $start); $admin_display_interval = (isset($admin_notices_option[$slug]['int']) ? $admin_notices_option[$slug]['int'] : $interval); $admin_display_msg = (isset($admin_notices[$slug]['msg']) ? $admin_notices[$slug]['msg'] : ''); $admin_display_title = (isset($admin_notices[$slug]['title']) ? $admin_notices[$slug]['title'] : ''); $admin_display_link = (isset($admin_notices[$slug]['link']) ? $admin_notices[$slug]['link'] : ''); $admin_display_dismissible= (isset($admin_notices[$slug]['dismissible']) ? $admin_notices[$slug]['dismissible'] : true); $output_css = false; // Ensure the notice hasn't been hidden and that the current date is after the start date if ($admin_display_check == 0 and strtotime($admin_display_start) <= strtotime($current_date)) { // Get remaining query string $query_str = esc_url(add_query_arg($this->prefix . '_admin_notice_ignore', $slug)); // Admin notice display output echo '
'; echo ''; echo '

'; echo $admin_display_title; echo '

'; echo '

'; echo $admin_display_msg; echo '

'; echo ''; if($admin_display_dismissible) echo ''; echo '
'; $this->notice_spam += 1; $output_css = true; } if ($output_css) { wp_enqueue_style($this->prefix . '-admin-notices', plugins_url(plugin_basename(dirname(__FILE__))) . '/gtranslate-notices.css', array()); } } } } // Spam protection check public function anti_notice_spam() { if ($this->notice_spam >= $this->notice_spam_max) { return true; } return false; } // Ignore function that gets ran at admin init to ensure any messages that were dismissed get marked public function admin_notice_ignore() { // If user clicks to ignore the notice, update the option to not show it again if (isset($_GET[$this->prefix . '_admin_notice_ignore'])) { $admin_notices_option = get_option($this->prefix . '_admin_notice', array()); $key = $_GET[$this->prefix . '_admin_notice_ignore']; if(!preg_match('/^[a-z_0-9]+$/i', $key)) return; $admin_notices_option[$key]['dismissed'] = 1; update_option($this->prefix . '_admin_notice', $admin_notices_option); $query_str = remove_query_arg($this->prefix . '_admin_notice_ignore'); wp_redirect($query_str); exit; } } // Temp Ignore function that gets ran at admin init to ensure any messages that were temp dismissed get their start date changed public function admin_notice_temp_ignore() { // If user clicks to temp ignore the notice, update the option to change the start date - default interval of 14 days if (isset($_GET[$this->prefix . '_admin_notice_temp_ignore'])) { $admin_notices_option = get_option($this->prefix . '_admin_notice', array()); $current_date = current_time("n/j/Y"); $date_array = explode('/', $current_date); $interval = (isset($_GET['gt_int']) ? intval($_GET['gt_int']) : 14); $date_array[1] += $interval; $new_start = date("n/j/Y", mktime(0, 0, 0, $date_array[0], $date_array[1], $date_array[2])); $key = $_GET[$this->prefix . '_admin_notice_temp_ignore']; if(!preg_match('/^[a-z_0-9]+$/i', $key)) return; $admin_notices_option[$key]['start'] = $new_start; $admin_notices_option[$key]['dismissed'] = 0; update_option($this->prefix . '_admin_notice', $admin_notices_option); $query_str = remove_query_arg(array($this->prefix . '_admin_notice_temp_ignore', 'gt_int')); wp_redirect( $query_str ); exit; } } public function admin_notice_pages($pages) { foreach ($pages as $key => $page) { if (is_array($page)) { if (isset($_GET['page']) and $_GET['page'] == $page[0] and isset($_GET['tab']) and $_GET['tab'] == $page[1]) { return true; } } else { if ($page == 'all') { return true; } if (get_current_screen()->id === $page) { return true; } if (isset($_GET['page']) and $_GET['page'] == $page) { return true; } } } return false; } // Required fields check public function required_fields( $fields ) { if (!isset( $fields['msg']) or (isset($fields['msg']) and empty($fields['msg']))) { return true; } if (!isset( $fields['title']) or (isset($fields['title']) and empty($fields['title']))) { return true; } return false; } // Special parameters function that is to be used in any extension of this class public function special_parameters($admin_notices) { // Intentionally left blank } public function gt_admin_notices() { $deactivate_plugins= array('WP Translator' => 'wptranslator/WPTranslator.php', 'TranslatePress' => 'translatepress-multilingual/index.php', 'Google Language Translator' => 'google-language-translator/google-language-translator.php', 'Google Website Translator' => 'google-website-translator/google-website-translator.php', 'Weglot' => 'weglot/weglot.php', 'TransPosh' => 'transposh-translation-filter-for-wordpress/transposh.php', 'Advanced Google Translate' => 'advanced-google-translate/advanced-google-translate.php', 'My WP Translate' => 'my-wp-translate/my-wp-translate.php', 'WPML Multilingual CMS' => 'sitepress-multilingual-cms/sitepress.php'); foreach($deactivate_plugins as $name => $plugin_file) { if(is_plugin_active($plugin_file)) { $deactivate_link = wp_nonce_url('plugins.php?action=deactivate&plugin='.urlencode($plugin_file ).'&plugin_status=all&paged=1&s=', 'deactivate-plugin_' . $plugin_file); $notices['deactivate_plugin_'.strtolower(str_replace(' ', '', $name))] = array( 'title' => sprintf(esc_html__('Please deactivate %s plugin', 'gtranslate'), $name), 'msg' => sprintf(esc_html__('%s plugin causes conflicts with GTranslate.', 'gtranslate'), $name), 'link' => '
  • ' . sprintf(esc_html__('Deactivate %s plugin', 'gtranslate'), $name) . '
  • ', 'dismissible' => false, 'int' => 0 ); } } /* $one_week_support = esc_url(add_query_arg(array($this->prefix . '_admin_notice_ignore' => 'one_week_support'))); $notices['one_week_support'] = array( 'title' => esc_html__('Hey! How is it going?', 'gtranslate'), 'msg' => esc_html__('Thank you for using GTranslate! We hope that you have found everything you need, but if you have any questions you can use our Live Chat or Forum:', 'gtranslate'), 'link' => '
  • ' . esc_html__('Get help', 'gtranslate') . '
  • ' . '
  • '.esc_html__('Check videos', 'gtranslate') . '
  • ' . '
  • ' . esc_html__('Never show again', 'gtranslate') . '
  • ', 'int' => 1 ); */ $two_week_review_ignore = esc_url(add_query_arg(array($this->prefix . '_admin_notice_ignore' => 'two_week_review'))); $two_week_review_temp = esc_url(add_query_arg(array($this->prefix . '_admin_notice_temp_ignore' => 'two_week_review', 'gt_int' => 6))); $notices['two_week_review'] = array( 'title' => esc_html__('Please Leave a Review', 'gtranslate'), 'msg' => sprintf(esc_html__('We hope you have enjoyed using GTranslate! Would you mind taking a few minutes to write a review on WordPress.org? %1$sJust writing a simple %2$s\'thank you\'%3$s will make us happy!', 'gtranslate'), '
    ', '', ''), 'link' => '
  • ' . esc_html__('Sure! I would love to!', 'gtranslate') . '
  • ' . '
  • ' . esc_html__('I have already left a review', 'gtranslate') . '
  • ' . '
  • ' . esc_html__('Maybe later', 'gtranslate') . '
  • ' . '
  • ' . esc_html__('Never show again', 'gtranslate') . '
  • ', 'later_link' => $two_week_review_temp, 'int' => 5 ); $data = get_option('GTranslate'); GTranslate::load_defaults($data); // check if email debug is on and add a notice if($data['email_translation_debug']) { $settings_link = admin_url('options-general.php?page=gtranslate_options'); $view_debug_link = admin_url('plugin-editor.php?file=gtranslate%2Furl_addon%2Fdebug.txt&plugin=gtranslate%2Fgtranslate.php'); $notices['gt_debug_notice'] = array( 'title' => esc_html__('Email translation debug mode is ON.', 'gtranslate'), 'msg' => esc_html__('Please note that sensitive information can be written into gtranslate/url_addon/debug.txt file, which can be accessed publicly. It is your responsibility to deny public access to it and clean debug information after you are done.', 'gtranslate'), 'link' => '
  • ' . esc_html__('GTranslate Settings', 'gtranslate') . '
  • ' . '
  • ' . esc_html__('View debug.txt', 'gtranslate') . '
  • ', 'dismissible' => false, 'int' => 0 ); } // check if translation debug is on and add a notice include dirname(__FILE__) . '/url_addon/config.php'; if($debug) { $edit_file_link = admin_url('plugin-editor.php?file=gtranslate%2Furl_addon%2Fconfig.php&plugin=gtranslate%2Fgtranslate.php'); $view_debug_link = admin_url('plugin-editor.php?file=gtranslate%2Furl_addon%2Fdebug.txt&plugin=gtranslate%2Fgtranslate.php'); $notices['gt_debug_notice'] = array( 'title' => esc_html__('Translation debug mode is ON.', 'gtranslate'), 'msg' => esc_html__('Please note that sensitive information can be written into gtranslate/url_addon/debug.txt file, which can be accessed publicly. It is your responsibility to deny public access to it and clean debug information after you are done.', 'gtranslate'), 'link' => '
  • ' . esc_html__('Edit config.php', 'gtranslate') . '
  • ' . '
  • ' . esc_html__('View debug.txt', 'gtranslate') . '
  • ', 'dismissible' => false, 'int' => 0 ); } $upgrade_tips_ignore = esc_url(add_query_arg(array($this->prefix . '_admin_notice_ignore' => 'upgrade_tips'))); $upgrade_tips_temp = esc_url(add_query_arg(array($this->prefix . '_admin_notice_temp_ignore' => 'upgrade_tips', 'gt_int' => 7))); if($data['pro_version'] != '1' and $data['enterprise_version'] != '1') { $notices['upgrade_tips'][] = array( 'title' => esc_html__('Did you know?', 'gtranslate'), 'msg' => sprintf(esc_html__('You can have %1$sneural machine translations%2$s which are human level by upgrading your GTranslate.', 'gtranslate'), '', ''), 'link' => '
  • ' . esc_html__('Learn more', 'gtranslate') . '
  • ' . '
  • ' . esc_html__('Maybe later', 'gtranslate') . '
  • ' . '
  • ' . esc_html__('Never show again', 'gtranslate') . '
  • ', 'later_link' => $upgrade_tips_temp, 'int' => 2 ); $notices['upgrade_tips'][] = array( 'title' => esc_html__('Did you know?', 'gtranslate'), 'msg' => sprintf(esc_html__('You can %1$sincrease%2$s your international %1$straffic%2$s by upgrading your GTranslate.', 'gtranslate'), '', ''), 'link' => '
  • ' . esc_html__('Learn more', 'gtranslate') . '
  • ' . '
  • ' . esc_html__('Maybe later', 'gtranslate') . '
  • ' . '
  • ' . esc_html__('Never show again', 'gtranslate') . '
  • ', 'later_link' => $upgrade_tips_temp, 'int' => 2 ); $notices['upgrade_tips'][] = array( 'title' => esc_html__('Did you know?', 'gtranslate'), 'msg' => sprintf(esc_html__('You can have your %1$stranslated pages indexed%2$s in search engines by upgrading your GTranslate.', 'gtranslate'), '', ''), 'link' => '
  • ' . esc_html__('Learn more', 'gtranslate') . '
  • ' . '
  • ' . esc_html__('Maybe later', 'gtranslate') . '
  • ' . '
  • ' . esc_html__('Never show again', 'gtranslate') . '
  • ', 'later_link' => $upgrade_tips_temp, 'int' => 2 ); $notices['upgrade_tips'][] = array( 'title' => esc_html__('Did you know?', 'gtranslate'), 'msg' => sprintf(esc_html__('You can %1$sincrease%2$s your %1$sAdSense revenue%2$s by upgrading your GTranslate.', 'gtranslate'), '', ''), 'link' => '
  • ' . esc_html__('Learn more', 'gtranslate') . '
  • ' . '
  • ' . esc_html__('Maybe later', 'gtranslate') . '
  • ' . '
  • ' . esc_html__('Never show again', 'gtranslate') . '
  • ', 'later_link' => $upgrade_tips_temp, 'int' => 2 ); $notices['upgrade_tips'][] = array( 'title' => esc_html__('Did you know?', 'gtranslate'), 'msg' => sprintf(esc_html__('You can %1$sedit translations%2$s by upgrading your GTranslate.', 'gtranslate'), '', ''), 'link' => '
  • ' . esc_html__('Learn more', 'gtranslate') . '
  • ' . '
  • ' . esc_html__('Maybe later', 'gtranslate') . '
  • ' . '
  • ' . esc_html__('Never show again', 'gtranslate') . '
  • ', 'later_link' => $upgrade_tips_temp, 'int' => 2 ); shuffle($notices['upgrade_tips']); $notices['upgrade_tips'] = $notices['upgrade_tips'][0]; } $this->admin_notice($notices); } } if(is_admin()) { if(!defined('DOING_AJAX') or !DOING_AJAX) new GTranslate_Notices(); } $data = get_option('GTranslate'); GTranslate::load_defaults($data); if($data['pro_version']) { // gtranslate redirect rules with PHP (for environments with no .htaccess support (pantheon, flywheel, etc.), usually .htaccess rules override this) $url_params = explode('?', $_SERVER['REQUEST_URI']); $request_uri = $url_params[0]; if(isset($url_params[1])) $query_params = $url_params[1]; else $query_params = ''; if(preg_match('/^\/(af|sq|am|ar|hy|az|eu|be|bn|bs|bg|ca|ceb|ny|zh-CN|zh-TW|co|hr|cs|da|nl|en|eo|et|tl|fi|fr|fy|gl|ka|de|el|gu|ht|ha|haw|iw|hi|hmn|hu|is|ig|id|ga|it|ja|jw|kn|kk|km|ko|ku|ky|lo|la|lv|lt|lb|mk|mg|ms|ml|mt|mi|mr|mn|my|ne|no|ps|fa|pl|pt|pa|ro|ru|sm|gd|sr|st|sn|sd|si|sk|sl|so|es|su|sw|sv|tg|ta|te|th|tr|uk|ur|uz|vi|cy|xh|yi|yo|zu)\/(af|sq|am|ar|hy|az|eu|be|bn|bs|bg|ca|ceb|ny|zh-CN|zh-TW|co|hr|cs|da|nl|en|eo|et|tl|fi|fr|fy|gl|ka|de|el|gu|ht|ha|haw|iw|hi|hmn|hu|is|ig|id|ga|it|ja|jw|kn|kk|km|ko|ku|ky|lo|la|lv|lt|lb|mk|mg|ms|ml|mt|mi|mr|mn|my|ne|no|ps|fa|pl|pt|pa|ro|ru|sm|gd|sr|st|sn|sd|si|sk|sl|so|es|su|sw|sv|tg|ta|te|th|tr|uk|ur|uz|vi|cy|xh|yi|yo|zu)\/(.*)$/', $request_uri, $matches)) { header('Location: ' . '/' . $matches[1] . '/' . $matches[3] . (empty($query_params) ? '' : '?'.$query_params), true, 301); exit; } // #1 redirect double language codes /es/en/... if(preg_match('/^\/(af|sq|am|ar|hy|az|eu|be|bn|bs|bg|ca|ceb|ny|zh-CN|zh-TW|co|hr|cs|da|nl|en|eo|et|tl|fi|fr|fy|gl|ka|de|el|gu|ht|ha|haw|iw|hi|hmn|hu|is|ig|id|ga|it|ja|jw|kn|kk|km|ko|ku|ky|lo|la|lv|lt|lb|mk|mg|ms|ml|mt|mi|mr|mn|my|ne|no|ps|fa|pl|pt|pa|ro|ru|sm|gd|sr|st|sn|sd|si|sk|sl|so|es|su|sw|sv|tg|ta|te|th|tr|uk|ur|uz|vi|cy|xh|yi|yo|zu)$/', $request_uri)) { header('Location: ' . $request_uri . '/' . (empty($query_params) ? '' : '?'.$query_params), true, 301); exit; } // #2 add trailing slash if($data['widget_look'] == 'float' or $data['widget_look'] == 'flags' or $data['widget_look'] == 'float' or $data['widget_look'] == 'dropdown_with_flags' or $data['widget_look'] == 'flags_name' or $data['widget_look'] == 'flags_code' or $data['widget_look'] == 'popup') $allowed_languages = $data['fincl_langs']; elseif($data['widget_look'] == 'flags_dropdown') $allowed_languages = array_values(array_unique(array_merge($data['fincl_langs'], $data['incl_langs']))); else $allowed_languages = $data['incl_langs']; $allowed_languages = implode('|', $allowed_languages); // ex: en|ru|it|de if(preg_match('/^\/('.$allowed_languages.')\/(.*)/', $request_uri, $matches)) { $_GET['glang'] = $matches[1]; $_GET['gurl'] = rawurldecode($matches[2]); require_once dirname(__FILE__) . '/url_addon/gtranslate.php'; exit; } // #3 proxy translation } if(!empty($data['show_in_menu'])) { function gtranslate_menu_item($items, $args) { $data = get_option('GTranslate'); GTranslate::load_defaults($data); if($args->theme_location == $data['show_in_menu']) { if($data['widget_look'] == 'dropdown_with_flags' or $data['widget_look'] == 'float') { $unique_wrapper_id = wp_rand(10000, 88888); $items .= ''; } elseif($data['widget_look'] == 'flags' or $data['widget_look'] == 'flags_code' or $data['widget_look'] == 'flags_name' or $data['widget_look'] == 'lang_codes' or $data['widget_look'] == 'lang_names') { $lang_array = $data['native_language_names'] ? json_decode(GTranslate::$lang_array_native_json, true) : GTranslate::$lang_array; $items .= ''; } else { $unique_menu_class = 'gt-menu-' . wp_rand(10000, 88888); $items .= ''; } } return $items; } add_filter('wp_nav_menu_items', 'gtranslate_menu_item', 10, 2); } if($data['floating_language_selector'] != 'no' and !is_admin()) { function gtranslate_display_floating() { echo GTranslate::get_widget_code(false); } add_action('wp_footer', 'gtranslate_display_floating'); } if($data['wrapper_selector'] != '.gtranslate_wrapper' and !empty(trim($data['wrapper_selector']))) { function gtranslate_add_inline_selector() { echo GTranslate::get_widget_code(array('position' => 'inline')); } add_action('wp_footer', 'gtranslate_add_inline_selector'); } if($data['url_translation'] and ($data['pro_version'] or $data['enterprise_version'])) { function gtranslate_url_translation_meta() { echo ''; } add_action('wp_head', 'gtranslate_url_translation_meta', 1); } if($data['add_hreflang_tags'] and ($data['pro_version'] or $data['enterprise_version'])) { function gtranslate_add_hreflang_tags() { $url_fragments = parse_url(home_url()); if(!isset($url_fragments['scheme']) or !isset($url_fragments['host'])) return; $data = get_option('GTranslate'); GTranslate::load_defaults($data); $enabled_languages = array(); if($data['widget_look'] == 'flags' or $data['widget_look'] == 'float' or $data['widget_look'] == 'dropdown_with_flags' or $data['widget_look'] == 'flags_name' or $data['widget_look'] == 'flags_code' or $data['widget_look'] == 'popup') $enabled_languages = $data['fincl_langs']; elseif($data['widget_look'] == 'flags_dropdown') $enabled_languages = array_values(array_unique(array_merge($data['fincl_langs'], $data['incl_langs']))); else $enabled_languages = $data['incl_langs']; //$current_url = network_home_url(add_query_arg(null, null)); $current_url = $url_fragments['scheme'] . '://' . $url_fragments['host'] . '/' . ltrim(add_query_arg(null, null), '/'); // adding default language if($data['default_language'] === 'iw') echo ''."\n"; elseif($data['default_language'] === 'jw') echo ''."\n"; else echo ''."\n"; // adding enabled languages foreach($enabled_languages as $lang) { $href = ''; $domain = str_replace('www.', '', $_SERVER['HTTP_HOST']); if($data['enterprise_version']) { if($data['custom_domains'] and !empty($data['custom_domains_data'])) { $custom_domains_data = json_decode(stripslashes($data['custom_domains_data']), true); if(isset($custom_domains_data[$lang])) $href = str_ireplace('://' . $_SERVER['HTTP_HOST'], '://' . $custom_domains_data[$lang], $current_url); else $href = str_ireplace('://' . $_SERVER['HTTP_HOST'], '://' . $lang . '.' . $domain, $current_url); } else $href = str_ireplace('://' . $_SERVER['HTTP_HOST'], '://' . $lang . '.' . $domain, $current_url); } elseif($data['pro_version']) $href = str_ireplace('://' . $_SERVER['HTTP_HOST'], '://' . $_SERVER['HTTP_HOST'] . '/' . $lang, $current_url); if(!empty($href) and $lang != $data['default_language']) { if($lang === 'iw') echo ''."\n"; elseif($lang === 'jw') echo ''."\n"; else echo ''."\n"; } } } add_action('wp_head', 'gtranslate_add_hreflang_tags', 1); } // translate WP REST API posts and categories data in JSON response if($data['pro_version'] or $data['enterprise_version']) { function gtranslate_rest_post($response, $post, $request) { if(isset($response->data['content']) and is_array($response->data['content'])) $response->data['content']['gt_translate_keys'] = array(array('key' => 'rendered', 'format' => 'html')); if(isset($response->data['excerpt']) and is_array($response->data['excerpt'])) $response->data['excerpt']['gt_translate_keys'] = array(array('key' => 'rendered', 'format' => 'html')); if(isset($response->data['title']) and is_array($response->data['title'])) $response->data['title']['gt_translate_keys'] = array(array('key' => 'rendered', 'format' => 'text')); if(isset($response->data['link'])) $response->data['gt_translate_keys'] = array(array('key' => 'link', 'format' => 'url')); // more fields can be added here return $response; } function gtranslate_rest_category($response, $category, $request) { if(isset($response->data['description'])) $response->data['gt_translate_keys'][] = array('key' => 'description', 'format' => 'html'); if(isset($response->data['name'])) $response->data['gt_translate_keys'][] = array('key' => 'name', 'format' => 'text'); if(isset($response->data['link'])) $response->data['gt_translate_keys'][] = array('key' => 'link', 'format' => 'url'); // more fields can be added here return $response; } add_filter('rest_prepare_post', 'gtranslate_rest_post', 10, 3); add_filter('rest_prepare_category', 'gtranslate_rest_category', 10, 3); } // auto redirect to browser language if(($data['pro_version'] or $data['enterprise_version']) and $data['detect_browser_language'] and parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) == parse_url(site_url(), PHP_URL_PATH) . '/' and isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) and isset($_SERVER['HTTP_USER_AGENT']) and !isset($_SERVER['HTTP_X_GT_LANG']) and preg_match('/bot|spider|slurp|facebook/i', $_SERVER['HTTP_USER_AGENT']) == 0) { if($data['widget_look'] == 'flags' or $data['widget_look'] == 'float' or $data['widget_look'] == 'dropdown_with_flags' or $data['widget_look'] == 'flags_name' or $data['widget_look'] == 'flags_code' or $data['widget_look'] == 'popup') $allowed_languages = $data['fincl_langs']; elseif($data['widget_look'] == 'flags_dropdown') $allowed_languages = array_values(array_unique(array_merge($data['fincl_langs'], $data['incl_langs']))); else $allowed_languages = $data['incl_langs']; $accept_language = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2)); // for debug purposes only if(isset($_GET['gt_auto_switch_to'])) $accept_language = $_GET['gt_auto_switch_to']; if($accept_language == 'zh') $accept_language = 'zh-CN'; elseif($accept_language == 'he') $accept_language = 'iw'; if($accept_language != $data['default_language'] and in_array($accept_language, $allowed_languages) and !isset($_COOKIE['gt_auto_switch'])) { // set cookie for 30 days and redirect setcookie('gt_auto_switch', 1, time() + 2592000); if($data['pro_version']) header('Location: ' . home_url() . '/' . $accept_language . '/'); if($data['enterprise_version'] and isset($_SERVER['HTTP_HOST'])) { if($data['custom_domains'] and !empty($data['custom_domains_data'])) { $custom_domains_data = json_decode(stripslashes($data['custom_domains_data']), true); if(isset($custom_domains_data[$accept_language])) $href = str_ireplace('://' . $_SERVER['HTTP_HOST'], '://' . $custom_domains_data[$accept_language], site_url()); else $href = str_ireplace('://' . $_SERVER['HTTP_HOST'], '://' . $accept_language . '.' . preg_replace('/^www\./', '', $_SERVER['HTTP_HOST']), site_url()); } else $href = str_ireplace('://' . $_SERVER['HTTP_HOST'], '://' . $accept_language . '.' . preg_replace('/^www\./', '', $_SERVER['HTTP_HOST']), site_url()); header('Location: ' . $href); } header('Vary: Accept-Language'); exit; } } if($data['pro_version'] or $data['enterprise_version']) { // filter for woocommerce script params function gt_filter_woocommerce_scripts_data($data, $handle) { switch($handle) { case 'wc-address-i18n': { $data['gt_translate_keys'] = array( array('key' => 'locale', 'format' => 'json'), array('key' => 'i18n_required_text', 'format' => 'text'), array('key' => 'i18n_optional_text', 'format' => 'html'), ); $locale = json_decode($data['locale']); if(isset($locale->default->address_1)) $locale->default->address_1->gt_translate_keys = array('label', 'placeholder'); if(isset($locale->default->address_2)) $locale->default->address_2->gt_translate_keys = array('label', 'placeholder'); if(isset($locale->default->city)) $locale->default->city->gt_translate_keys = array('label', 'placeholder'); if(isset($locale->default->postcode)) $locale->default->postcode->gt_translate_keys = array('label', 'placeholder'); if(isset($locale->default->state)) $locale->default->state->gt_translate_keys = array('label', 'placeholder'); if(isset($locale->default->shipping->address_1)) $locale->default->shipping->address_1->gt_translate_keys = array('label', 'placeholder'); if(isset($locale->default->shipping->address_2)) $locale->default->shipping->address_2->gt_translate_keys = array('label', 'placeholder'); if(isset($locale->default->shipping->city)) $locale->default->shipping->city->gt_translate_keys = array('label', 'placeholder'); if(isset($locale->default->shipping->postcode)) $locale->default->shipping->postcode->gt_translate_keys = array('label', 'placeholder'); if(isset($locale->default->shipping->state)) $locale->default->shipping->state->gt_translate_keys = array('label', 'placeholder'); if(isset($locale->default->billing->address_1)) $locale->default->billing->address_1->gt_translate_keys = array('label', 'placeholder'); if(isset($locale->default->billing->address_2)) $locale->default->billing->address_2->gt_translate_keys = array('label', 'placeholder'); if(isset($locale->default->billing->city)) $locale->default->billing->city->gt_translate_keys = array('label', 'placeholder'); if(isset($locale->default->billing->postcode)) $locale->default->billing->postcode->gt_translate_keys = array('label', 'placeholder'); if(isset($locale->default->billing->state)) $locale->default->billing->state->gt_translate_keys = array('label', 'placeholder'); $data['locale'] = json_encode($locale); } break; case 'wc-single-product': { $data['gt_translate_keys'] = array('i18n_required_rating_text'); } break; case 'wc-checkout': { $data['gt_translate_keys'] = array('i18n_checkout_error'); } break; case 'wc-country-select': { $data['gt_translate_keys'] = array('i18n_ajax_error', 'i18n_input_too_long_1', 'i18n_input_too_long_n', 'i18n_input_too_short_1', 'i18n_input_too_short_n', 'i18n_load_more', 'i18n_no_matches', 'i18n_searching', 'i18n_select_state_text', 'i18n_selection_too_long_1', 'i18n_selection_too_long_n'); } break; case 'wc-add-to-cart': { $data['gt_translate_keys'] = array('i18n_view_cart', array('key' => 'cart_url', 'format' => 'url')); } break; case 'wc-add-to-cart-variation': { $data['gt_translate_keys'] = array('i18n_no_matching_variations_text', 'i18n_make_a_selection_text', 'i18n_unavailable_text'); } break; case 'wc-password-strength-meter': { $data['gt_translate_keys'] = array('i18n_password_error', 'i18n_password_hint', ''); } break; default: break; } return $data; } function gt_woocommerce_geolocate_ip($false) { if(isset($_SERVER['HTTP_X_GT_VIEWER_IP'])) $_SERVER['HTTP_X_REAL_IP'] = $_SERVER['HTTP_X_GT_VIEWER_IP']; elseif(isset($_SERVER['HTTP_X_GT_CLIENTIP'])) $_SERVER['HTTP_X_REAL_IP'] = $_SERVER['HTTP_X_GT_CLIENTIP']; if(isset($_SERVER['HTTP_X_REAL_IP'])) $_SERVER['HTTP_X_REAL_IP'] = trim(current(explode(',', sanitize_text_field(wp_unslash($_SERVER['HTTP_X_REAL_IP']))))); if(isset($_SERVER['HTTP_X_GT_CLIENTIP'], $_SERVER['HTTP_CF_IPCOUNTRY'])) unset($_SERVER['HTTP_CF_IPCOUNTRY']); return $false; } add_filter('woocommerce_get_script_data', 'gt_filter_woocommerce_scripts_data', 10, 2 ); add_filter('woocommerce_geolocate_ip', 'gt_woocommerce_geolocate_ip', 10, 4); // translate emails if($data['email_translation']) { function gt_translate_emails($args) { if(!is_array($args) or !isset($args['subject']) or !isset($args['message'])) return $args; $subject = $args['subject']; $message = $args['message']; if(function_exists('curl_init') and isset($_SERVER['HTTP_X_GT_LANG'])) { //file_put_contents(dirname(__FILE__) . '/url_addon/debug.txt', date('Y-m-d H:i:s') . " - $subject$message\n", FILE_APPEND); // translate woocommerce if(strpos($message, 'woocommerce') !== false) { $data = get_option('GTranslate'); GTranslate::load_defaults($data); include dirname(__FILE__) . '/url_addon/config.php'; $server_id = intval(substr(md5(preg_replace('/^www\./', '', $_SERVER['HTTP_HOST'])), 0, 5), 16) % count($servers); $server = $servers[$server_id]; $host = $_SERVER['HTTP_X_GT_LANG'] . '.' . preg_replace('/^www\./', '', $_SERVER['HTTP_HOST']); if($data['custom_domains'] and !empty($data['custom_domains_data'])) { $custom_domains_data = json_decode(stripslashes($data['custom_domains_data']), true); if(isset($custom_domains_data[$_SERVER['HTTP_X_GT_LANG']])) $host = $custom_domains_data[$_SERVER['HTTP_X_GT_LANG']]; } $protocol = ((isset($_SERVER['HTTPS']) and ($_SERVER['HTTPS'] == 'on' or $_SERVER['HTTPS'] == 1)) or (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) and $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https' : 'http'; $headers = array(); $headers[] = 'Host: ' . $host; // add real visitor IP header if(isset($_SERVER['HTTP_CLIENT_IP']) and !empty($_SERVER['HTTP_CLIENT_IP'])) $viewer_ip_address = $_SERVER['HTTP_CLIENT_IP']; if(isset($_SERVER['HTTP_CF_CONNECTING_IP']) and !empty($_SERVER['HTTP_CF_CONNECTING_IP'])) $viewer_ip_address = $_SERVER['HTTP_CF_CONNECTING_IP']; if(isset($_SERVER['HTTP_X_SUCURI_CLIENTIP']) and !empty($_SERVER['HTTP_X_SUCURI_CLIENTIP'])) $viewer_ip_address = $_SERVER['HTTP_X_SUCURI_CLIENTIP']; if(!isset($viewer_ip_address)) $viewer_ip_address = $_SERVER['REMOTE_ADDR']; $headers[] = 'X-GT-Viewer-IP: ' . $viewer_ip_address; $headers[] = 'User-Agent: GTranslate-Email-Translate'; // add X-Forwarded-For if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) and !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) $headers[] = 'X-GT-Forwarded-For: ' . $_SERVER['HTTP_X_FORWARDED_FOR']; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $protocol.'://'.$server.'.tdn.gtranslate.net'.wp_make_link_relative(plugins_url('gtranslate/url_addon/gtranslate-email.php').'?glang='.$_SERVER['HTTP_X_GT_LANG'])); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); if(defined('CURL_IPRESOLVE_V4')) curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/url_addon/cacert.pem'); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, array('body' => base64_encode(do_shortcode("$subject$message")), 'access_key' => md5(substr(NONCE_SALT, 0, 10) . substr(NONCE_KEY, 0, 5)))); if($data['email_translation_debug']) { $fh = fopen(dirname(__FILE__) . '/url_addon/debug.txt', 'a'); curl_setopt($ch, CURLOPT_VERBOSE, true); curl_setopt($ch, CURLOPT_STDERR, $fh); } $response = curl_exec($ch); $response_info = curl_getinfo($ch); curl_close($ch); if($data['email_translation_debug']) { file_put_contents(dirname(__FILE__) . '/url_addon/debug.txt', 'Response: ' . $response . "\n", FILE_APPEND); file_put_contents(dirname(__FILE__) . '/url_addon/debug.txt', 'Response_info: ' . print_r($response_info, true) . "\n", FILE_APPEND); } if(isset($response_info['http_code']) and $response_info['http_code'] == 200) { $response = json_decode($response, true); if(empty($response)) return $args; $response = base64_decode($response['email-body']); if($response === false) return $args; if($data['pro_version']) $response = str_ireplace($host, $_SERVER['HTTP_HOST'] . '/' . $_SERVER['HTTP_X_GT_LANG'], $response); preg_match_all('/(.*?)<\/subject>(.*?)<\/message>/s', $response, $matches); //file_put_contents(dirname(__FILE__) . '/url_addon/debug.txt', 'Matches: ' . print_r($matches, true) . "\n", FILE_APPEND); if(isset($matches[1][0], $matches[2][0])) { $subject = $matches[1][0]; $message = $matches[2][0]; if($data['email_translation_debug']) { file_put_contents(dirname(__FILE__) . '/url_addon/debug.txt', 'Translated Subject: ' . $subject . "\n", FILE_APPEND); file_put_contents(dirname(__FILE__) . '/url_addon/debug.txt', 'Translated Message: ' . $message . "\n", FILE_APPEND); } $args['subject'] = $subject; $args['message'] = $message; } } } } return $args; } // woocommerce pdf invoice translation function gt_translate_invoice_pdf($html) { if(function_exists('curl_init') and isset($_SERVER['HTTP_X_GT_LANG'])) { $data = get_option('GTranslate'); GTranslate::load_defaults($data); // add notranslate for addresses $html = str_replace('-address"', '-address notranslate"', $html); include dirname(__FILE__) . '/url_addon/config.php'; $server_id = intval(substr(md5(preg_replace('/^www\./', '', $_SERVER['HTTP_HOST'])), 0, 5), 16) % count($servers); $server = $servers[$server_id]; $host = $_SERVER['HTTP_X_GT_LANG'] . '.' . preg_replace('/^www\./', '', $_SERVER['HTTP_HOST']); if($data['custom_domains'] and !empty($data['custom_domains_data'])) { $custom_domains_data = json_decode(stripslashes($data['custom_domains_data']), true); if(isset($custom_domains_data[$_SERVER['HTTP_X_GT_LANG']])) $host = $custom_domains_data[$_SERVER['HTTP_X_GT_LANG']]; } $protocol = ((isset($_SERVER['HTTPS']) and ($_SERVER['HTTPS'] == 'on' or $_SERVER['HTTPS'] == 1)) or (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) and $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https' : 'http'; $headers = array(); $headers[] = 'Host: ' . $host; // add real visitor IP header if(isset($_SERVER['HTTP_CLIENT_IP']) and !empty($_SERVER['HTTP_CLIENT_IP'])) $viewer_ip_address = $_SERVER['HTTP_CLIENT_IP']; if(isset($_SERVER['HTTP_CF_CONNECTING_IP']) and !empty($_SERVER['HTTP_CF_CONNECTING_IP'])) $viewer_ip_address = $_SERVER['HTTP_CF_CONNECTING_IP']; if(isset($_SERVER['HTTP_X_SUCURI_CLIENTIP']) and !empty($_SERVER['HTTP_X_SUCURI_CLIENTIP'])) $viewer_ip_address = $_SERVER['HTTP_X_SUCURI_CLIENTIP']; if(!isset($viewer_ip_address)) $viewer_ip_address = $_SERVER['REMOTE_ADDR']; $headers[] = 'X-GT-Viewer-IP: ' . $viewer_ip_address; $headers[] = 'User-Agent: GTranslate-Email-Translate'; // add X-Forwarded-For if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) and !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) $headers[] = 'X-GT-Forwarded-For: ' . $_SERVER['HTTP_X_FORWARDED_FOR']; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $protocol.'://'.$server.'.tdn.gtranslate.net'.wp_make_link_relative(plugins_url('gtranslate/url_addon/gtranslate-email.php').'?format=pdf_html&glang='.$_SERVER['HTTP_X_GT_LANG'])); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); if(defined('CURL_IPRESOLVE_V4')) curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . '/url_addon/cacert.pem'); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, array('body' => base64_encode(do_shortcode("PDF Invoice$html")), 'access_key' => md5(substr(NONCE_SALT, 0, 10) . substr(NONCE_KEY, 0, 5)))); if($data['email_translation_debug']) { $fh = fopen(dirname(__FILE__) . '/url_addon/debug.txt', 'a'); curl_setopt($ch, CURLOPT_VERBOSE, true); curl_setopt($ch, CURLOPT_STDERR, $fh); } $response = curl_exec($ch); $response_info = curl_getinfo($ch); curl_close($ch); if($data['email_translation_debug']) { file_put_contents(dirname(__FILE__) . '/url_addon/debug.txt', 'Response PDF: ' . $response . "\n", FILE_APPEND); file_put_contents(dirname(__FILE__) . '/url_addon/debug.txt', 'Response_info PDF: ' . print_r($response_info, true) . "\n", FILE_APPEND); } if(isset($response_info['http_code']) and $response_info['http_code'] == 200) { $response = json_decode($response, true); if(empty($response)) return $html; $response = base64_decode($response['email-body']); if($response === false) return $html; if($data['pro_version']) $response = str_ireplace($host, $_SERVER['HTTP_HOST'] . '/' . $_SERVER['HTTP_X_GT_LANG'], $response); preg_match_all('/(.*?)<\/subject>(.*?)<\/message>/s', $response, $matches); //file_put_contents(dirname(__FILE__) . '/url_addon/debug.txt', 'Matches: ' . print_r($matches, true) . "\n", FILE_APPEND); if(isset($matches[1][0], $matches[2][0])) { $html = $matches[2][0]; // fix image $html = str_replace(' src="https://' . $_SERVER['HTTP_HOST'], ' src="', $html); if($data['email_translation_debug']) { file_put_contents(dirname(__FILE__) . '/url_addon/debug.txt', 'Translated PDF HTML: ' . $html . "\n", FILE_APPEND); } } } } return $html; } function gt_translate_wp_smtp_email($args) { //file_put_contents(dirname(__FILE__) . '/url_addon/debug.txt', date('Y-m-d H:i:s') . " - gt_translate_wp_smtp_email args: " . print_r($args, true) . "\n", FILE_APPEND); if(!is_object($args) or !isset($args->Subject) or !isset($args->Body)) return; $result = gt_translate_emails(array('subject' => $args->Subject, 'message' => $args->Body)); $args->Subject = $result['subject']; $args->Body = $result['message']; } add_filter('wpo_wcpdf_get_html', 'gt_translate_invoice_pdf', 10000, 1); add_filter('wp_mail', 'gt_translate_emails', 10000, 1); add_filter('wp_mail_smtp_mailcatcher_smtp_pre_send_before', 'gt_translate_wp_smtp_email', 10000, 1); } } if($data['enterprise_version']) { // solve wp_get_referer issue function gt_allowed_redirect_hosts($hosts) { $data = get_option('GTranslate'); GTranslate::load_defaults($data); if($data['custom_domains'] and !empty($data['custom_domains_data'])) { $custom_domains_data = json_decode(stripslashes($data['custom_domains_data']), true); $gt_hosts = array_values($custom_domains_data); } else { $gt_hosts = array(); } if(isset($_SERVER['HTTP_X_GT_LANG'])) $gt_hosts[] = $_SERVER['HTTP_X_GT_LANG'] . '.' . preg_replace('/^www\./', '', $_SERVER['HTTP_HOST']); return array_merge($hosts, $gt_hosts); } add_filter('allowed_redirect_hosts', 'gt_allowed_redirect_hosts'); } // exclude Autoptimize minification function ao_cache_exclude_js_gtranslate($exclude_js) { if(is_string($exclude_js)) $exclude_js .= ', gtranslate'; return $exclude_js; } add_filter('autoptimize_filter_js_exclude', 'ao_cache_exclude_js_gtranslate', 10, 1); // exclude javascript minification by cache plugins function cache_exclude_js_gtranslate($excluded_js) { if(is_array($excluded_js) or empty($excluded_js)) { $excluded_js[] = '/gtranslate/js/.+\.js'; $excluded_js[] = 'cdn.gtranslate.net'; $excluded_js[] = 'gtranslate'; } return $excluded_js; } // LiteSpeed Cache add_filter('litespeed_optimize_js_excludes', 'cache_exclude_js_gtranslate'); add_filter('litespeed_optm_js_defer_exc', 'cache_exclude_js_gtranslate'); // WP Rocket add_filter('rocket_exclude_js', 'cache_exclude_js_gtranslate'); add_filter('rocket_minify_excluded_external_js', 'cache_exclude_js_gtranslate'); // WP Rocket inline script exclusions function rocket_exclude_inline_gt_scripts($excluded_patterns) { if(is_array($excluded_patterns)) { $excluded_patterns[] = 'gtranslate'; return $excluded_patterns; } return array('gtranslate'); } add_filter('rocket_defer_inline_exclusions', 'rocket_exclude_inline_gt_scripts', 1000, 1); // W3 Total Cache function w3tc_cache_exclude_js_gtranslate($do_tag_minification, $script_tag, $file) { if(strpos($file, 'gtranslate') !== false) return false; return $do_tag_minification; } add_filter('w3tc_minify_js_do_tag_minification', 'w3tc_cache_exclude_js_gtranslate', 10, 3); // WP Optimize function wpo_cache_exclude_js_gtranslate($excluded_js) { if(is_array($excluded_js) or empty($excluded_js)) $excluded_js[] = 'gtranslate'; return $excluded_js; } add_filter('wp-optimize-minify-default-exclusions', 'wpo_cache_exclude_js_gtranslate', 10, 1); // Siteground SG Optimize function sg_cache_exclude_js_gtranslate($excluded_js) { if(!is_array($excluded_js)) return $excluded_js; global $wp_scripts; $registered_handles = array_keys($wp_scripts->registered); foreach($registered_handles as $handle) { if(strpos($handle, 'gt_widget_script') !== false) $excluded_js[] = $handle; } return $excluded_js; } add_filter('sgo_js_minify_exclude', 'sg_cache_exclude_js_gtranslate', 10, 1); add_filter('sgo_javascript_combine_exclude', 'sg_cache_exclude_js_gtranslate', 10, 1); add_filter('sgo_javascript_combine_excluded_external_paths', 'cache_exclude_js_gtranslate', 10, 1);