; ?>)
'+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 '
' . $admin_display_link . '
';
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);