'', 'english_name' => '', 'translations' => 'array', 'flag' => '', 'default_locale' => ''); var $add_validation_failed = false; function __construct() { // Set upload dir $wp_upload_dir = wp_upload_dir(); $this->upload_dir = $wp_upload_dir['basedir'] . '/flags'; if (!is_dir($this->upload_dir)) { if (!mkdir($this->upload_dir)) { $this->error(__('Upload directory cannot be created. Check your permissions.','sitepress')); } } if (!$this->is_writable = is_writable($this->upload_dir)) { $this->error(__('Upload dir is not writable','sitepress')); } $this->migrate(); $this->get_active_languages(); // Trigger save. if (isset($_POST['icl_edit_languages_action']) && $_POST['icl_edit_languages_action'] == 'update') { if(wp_verify_nonce($_POST['_wpnonce'], 'icl_edit_languages')){ $this->update(); } } add_action('admin_footer', array(&$this,'scripts')); ?>


For each language, you need to enter the following information: ', 'sitepress'); ?>
error) { echo '

'.$this->error.'

'; } ?>
edit_table(); ?>
active_languages as $lang) { ?> active_languages as $lang) { ?> active_languages as $lang) { $this->table_row($lang); } if ($this->add_validation_failed) { $_POST['icl_edit_languages']['add']['id'] = 'add'; $new_lang = $_POST['icl_edit_languages']['add']; } else { $new_lang = array('id'=>'add'); } $this->table_row($new_lang,true,true); ?>
add_validation_failed) echo 'style="display:none;" ';?>class="icl_edit_languages_show"> ()
add_validation_failed) echo 'style="display:none;" ';?>class="icl_edit_languages_show"> ()

« 

add_validation_failed) { ?> style="display:none;" /> add_validation_failed) { ?> style="display:none;" /> 


> readonly="readonly" /> readonly="readonly" /> add_validation_failed) echo 'style="display:none;" ';?>class="icl_edit_languages_show"> active_languages as $translation){ if ($lang['id'] == 'add') { $value = isset($_POST['icl_edit_languages']['add']['translations'][$translation['code']]) ? $_POST['icl_edit_languages']['add']['translations'][$translation['code']] : ''; } else { $value = isset($lang['translation'][$translation['id']]) ? $lang['translation'][$translation['id']] : ''; } ?> is_writable) { ?> is_writable) { ?>

active_languages = $sitepress->get_active_languages(true); foreach ($this->active_languages as $lang) { foreach ($this->active_languages as $lang_translation) { $this->active_languages[$lang['code']]['translation'][$lang_translation['id']] = $sitepress->get_display_language_name($lang['code'], $lang_translation['code']); } $flag = $sitepress->get_flag($lang['code']); $this->active_languages[$lang['code']]['flag'] = $flag->flag; $this->active_languages[$lang['code']]['from_template'] = $flag->from_template; $this->active_languages[$lang['code']]['default_locale'] = $wpdb->get_var("SELECT default_locale FROM {$wpdb->prefix}icl_languages WHERE code='".$lang['code']."'"); $this->active_languages[$lang['code']]['encode_url'] = $lang['encode_url']; } } function insert_main_table($code, $english_name, $default_locale, $major = 0, $active = 0, $encode_url = 0) { global $wpdb; return $wpdb->insert($wpdb->prefix . 'icl_languages', array( 'code' => $code, 'english_name' => $english_name, 'default_locale'=> $default_locale, 'major' => $major, 'active' => $active, 'encode_url' => $encode_url )); } function update_main_table($id, $code, $default_locale, $encode_url){ global $wpdb; $wpdb->update($wpdb->prefix . 'icl_languages', array('code' => $code, 'default_locale' => $default_locale, 'encode_url'=>$encode_url), array('ID' => $id)); } function insert_translation($name, $language_code, $display_language_code) { global $wpdb; return $wpdb->query("INSERT INTO {$wpdb->prefix}icl_languages_translations (name, language_code, display_language_code) VALUES('".$name."', '".$language_code."', '".$display_language_code."')"); } function update_translation($name, $language_code, $display_language_code) { global $wpdb; $wpdb->query("UPDATE {$wpdb->prefix}icl_languages_translations SET name='".$name."' WHERE language_code = '".$language_code."' AND display_language_code = '".$display_language_code."'"); } function insert_flag($lang_code, $flag, $from_template) { global $wpdb; return $wpdb->query("INSERT INTO {$wpdb->prefix}icl_flags (lang_code, flag, from_template) VALUES('".$lang_code."', '".$flag."', ".$from_template.")"); } function update_flag($lang_code, $flag, $from_template) { global $wpdb; $wpdb->query("UPDATE {$wpdb->prefix}icl_flags SET flag='".$flag."',from_template=".$from_template." WHERE lang_code = '".$lang_code."'"); } function update() { // Basic check. if (!isset($_POST['icl_edit_languages']) || !is_array($_POST['icl_edit_languages'])){ $this->error(__('Please, enter valid data.','sitepress')); return; } global $sitepress,$wpdb; // First check if add and validate it. if (isset($_POST['icl_edit_languages']['add']) && $_POST['icl_edit_languages_ignore_add'] == 'false') { if ($this->validate_one('add', $_POST['icl_edit_languages']['add'])) { $this->insert_one($this->sanitize($_POST['icl_edit_languages']['add'])); } // Reset flag upload field. $_POST['icl_edit_languages']['add']['flag_upload'] = 'false'; } foreach ($_POST['icl_edit_languages'] as $id => $data){ // Ignore insert. if ($id == 'add') { continue; } // Validate and sanitize data. if (!$this->validate_one($id, $data)) continue; $data = $this->sanitize($data); // Update main table. $this->update_main_table($id, $data['code'], $data['default_locale'], $data['encode_url']); $wpdb->update($wpdb->prefix . 'icl_locale_map', array('locale' => $data['default_locale']), array('code' => $data['code'])); // Update translations table. foreach ($data['translations'] as $translation_code => $translation_value) { // If new (add language) translations are submitted. if ($translation_code == 'add') { if ($this->add_validation_failed || $_POST['icl_edit_languages_ignore_add'] == 'true') { continue; } if (empty($translation_value)) { $translation_value = $data['english_name']; } $translation_code = $_POST['icl_edit_languages']['add']['code']; } // Check if update. if ($wpdb->get_var("SELECT id FROM {$wpdb->prefix}icl_languages_translations WHERE language_code='".$data['code']."' AND display_language_code='".$translation_code."'")) { $this->update_translation($translation_value, $data['code'], $translation_code); } else { if (!$this->insert_translation($translation_value, $data['code'], $translation_code)) { $this->error(sprintf(__('Error adding translation %s for %s.', 'sitepress'), $data['code'], $translation_code)); } } } // Handle flag. if ($data['flag_upload'] == 'true' && !empty($_FILES['icl_edit_languages']['name'][$id]['flag_file'])) { if ($filename = $this->upload_flag($id, $data)) { $data['flag'] = $filename; $from_template = 1; } else { $data['flag'] = $data['code'] . '.png'; $this->error(__('Error uploading flag file.', 'sitepress')); $from_template = 0; } } else { if (empty($data['flag'])) { $data['flag'] = $data['code'] . '.png'; $from_template = 0; } else { $from_template = isset($data['from_template']) ? 1 : 0; } } // Update flag table. $this->update_flag($data['code'], $data['flag'], $from_template); // Reset flag upload field. $_POST['icl_edit_languages'][$id]['flag_upload'] = 'false'; } // Refresh cache. $sitepress->icl_language_name_cache->clear(); $sitepress->icl_flag_cache->clear(); delete_option('_icl_cache'); // Unset ADD fields. if (!$this->add_validation_failed) { unset($_POST['icl_edit_languages']['add']); } // Reser active languages. $this->get_active_languages(); } function insert_one($data) { global $sitepress; // Insert main table. if (!$this->insert_main_table($data['code'], $data['english_name'], $data['default_locale'], 0, 1, $data['encode_url'])) { $this->error(__('Adding language failed.', 'sitepress')); return false; } // Insert translations. $all_languages = $sitepress->get_languages(); foreach ($all_languages as $key => $lang) { // If submitted. if (array_key_exists($lang['code'], $data['translations'])) { if (empty($data['translations'][$lang['code']])) { $data['translations'][$lang['code']] = $data['english_name']; } if (!$this->insert_translation($data['translations'][$lang['code']], $data['code'], $lang['code'])) { $this->error(sprintf(__('Error adding translation %s for %s.', 'sitepress'), $data['code'], $lang['code'])); } continue; } // Insert dummy translation. if (!$this->insert_translation($data['english_name'], $data['code'], $lang['code'])) { $this->error(sprintf(__('Error adding translation %s for %s.', 'sitepress'), $data['code'], $lang['code'])); } } // Insert native name. if (!isset($data['translations']['add']) || empty($data['translations']['add'])) { $data['translations']['add'] = $data['english_name']; } if (!$this->insert_translation($data['translations']['add'], $data['code'], $data['code'])) { $this->error(__('Error adding native name.', 'sitepress')); } // Handle flag. if ($data['flag_upload'] == 'true' && !empty($_FILES['icl_edit_languages']['name']['add']['flag_file'])) { if ($filename = $this->upload_flag('add', $data)) { $data['flag'] = $filename; $from_template = 1; } else { $data['flag'] = $data['code'] . '.png'; $from_template = 0; } } else { if (empty($data['flag'])) { $data['flag'] = $data['code'] . '.png'; } $from_template = 0; } // Insert flag table. if (!$this->insert_flag($data['code'], $data['flag'], $from_template)) { $this->error(__('Error adding flag.', 'sitepress')); } } function validate_one($id, $data) { global $wpdb; // If insert, check if languge code (unique) exists. if ($exists = $wpdb->get_var("SELECT code FROM {$wpdb->prefix}icl_languages WHERE code='".$data['code']."'") && $id == 'add') { $this->error = __('Language code exists','sitepress'); $this->add_validation_failed = true; return false; // Illegal change of code } else if ($exists && $wpdb->get_var("SELECT code FROM {$wpdb->prefix}icl_languages WHERE code='".$data['code']."' AND id=".$data['id']) != $data['code']) { $this->error = __('Language code exists','sitepress'); if ($id == 'add') $this->add_validation_failed = true; return false; } foreach ($this->required_fields as $name => $type) { if ($name == 'flag') { if ($data['flag_upload'] == 'true') { $check = $_FILES['icl_edit_languages']['name'][$id]['flag_file']; if (empty($check)) continue; if (!$this->check_extension($check)) { if ($id == 'add') { $this->add_validation_failed = true; } return false; } } continue; } if (!isset($_POST['icl_edit_languages'][$id][$name]) || empty($_POST['icl_edit_languages'][$id][$name])) { if ($_POST['icl_edit_languages_ignore_add'] == 'true') { return false; } $this->error(__('Please, enter required data.','sitepress')); if ($id == 'add') { $this->add_validation_failed = true; } return false; } if ($type == 'array' && !is_array($_POST['icl_edit_languages'][$id][$name])) { if ($id == 'add') { $this->add_validation_failed = true; } $this->error(__('Please, enter valid data.','sitepress')); return false; } } return true; } function sanitize($data) { global $wpdb; foreach ($data as $key => $value) { if (is_array($value)) { foreach ($value as $k => $v) { $data[$key][$k] = $wpdb->escape($v); } } $data[$key] = $wpdb->escape($value); } return $data; } function check_extension($file) { $extension = substr($file, strrpos($file, '.') + 1); if (!in_array(strtolower($extension),array('png','gif','jpg'))) { $this->error(__('File extension not allowed.','sitepress')); return false; } return true; } function error($str = false) { if (!$this->error) { $this->error = ''; } $this->error .= $str . '
'; } function upload_flag($id, $data) { $filename = basename($_FILES['icl_edit_languages']['name'][$id]['flag_file']); $target_path = $this->upload_dir . '/' . $filename; $fileinfo = getimagesize($_FILES['icl_edit_languages']['tmp_name'][$id]['flag_file']); $validated = is_array($fileinfo) && in_array($fileinfo['mime'], array('image/gif', 'image/jpeg', 'image/png')) && $fileinfo['0'] > 0; if ($validated && move_uploaded_file($_FILES['icl_edit_languages']['tmp_name'][$id]['flag_file'], $target_path) ) { return $filename; } else { $this->error(__('There was an error uploading the file, please try again!','sitepress')); return false; } } function migrate() { global $sitepress, $sitepress_settings; if (!isset($sitepress_settings['edit_languages_flag_migration'])) { foreach( glob(get_stylesheet_directory().'/flags/*') as $filename ){ rename($filename, $this->upload_dir . '/' . basename($filename)); } $sitepress->save_settings(array('edit_languages_flag_migration' => 1)); } } } global $icl_edit_languages; $icl_edit_languages = new SitePress_EditLanguages;