lang_map[$locale] = $code;
}
$this->lang_map_rev = array_flip($this->lang_map);
$this->settings = get_option('icl_adl_settings');
if(empty($this->settings['wp_version']) || version_compare($wp_version, $this->settings['wp_version'], '>')){
$this->updates_check(array('trigger' => 'wp-update'));
}
}
function updates_check($args = array()){
global $wp_version, $WordPress_language;
$wpversion = preg_replace('#-(.+)$#', '', $wp_version);
$defaults = array(
'trigger' => 'manual'
);
extract($defaults);
extract($args, EXTR_OVERWRITE);
$this->load_xml();
$this->get_translation_files();
$active_languages = $WordPress_language->get_languages();
$updates = array();
foreach($active_languages as $code => $language){
$this->set_translation($code, $updates); //&$updates
$locales = $this->get_locales($code);
foreach ($locales as $locale => $data) {
$this->set_translation($locale, $updates); //&$updates
}
}
$this->settings['wp_version'] = $wpversion;
$this->settings['last_time_xml_check'] = time();
$this->settings['last_time_xml_check_trigger'] = $trigger;
$this->save_settings();
return $updates;
}
function load_xml($reload = false){
if(!class_exists('WP_Http')) include_once ABSPATH . WPINC . '/class-http.php';
$client = new WP_Http();
$response = $client->request(self::LOCALES_XML_FILE, array('timeout'=>15, 'decompress'=>false));
if(is_wp_error($response)){
throw new Exception(__('Failed downloading the language information file. Please go back and try a little later.', 'wordpress-language'));
}else{
if($response['response']['code'] == 200){
$this->xml = new SimpleXMLElement(wp_trans_gzdecode($response['body']));
}
}
}
function get_locales($lang_code) {
$locales = array();
$locales_result = $this->xml->xpath($lang_code);
if($locales_result){
foreach($locales_result[0] as $locale => $data){
$locales[$this->lang_map_rev[$locale]] = $this->get_mo_file_urls($lang_code, $locale);
}
}
return $locales;
}
function get_translation_files(){
global $WordPress_language;
$active_languages = $WordPress_language->get_languages();
foreach($active_languages as $code => $language){
$locales = $this->get_locales($code);
foreach ($locales as $locale => $projects) {
foreach($projects as $project_name => $project_data){
$this->translation_files[$locale][$project_name] = $project_data;
}
}
}
return $this->translation_files;
}
function set_translation($code_or_locale, &$updates) {
if(isset($this->translation_files[$code_or_locale]['core']['signature'])){
$signature = $this->translation_files[$code_or_locale]['core']['signature'];
$updates['languages'][$code_or_locale] = $signature;
$this->settings['translations'][$code_or_locale]['available'] = $signature;
}
}
function get_mo_file_urls($lang_code, $locale){
global $wp_version;
$wpversion = preg_replace('#-(.+)$#', '', $wp_version);
$wpversion = join('.', array_slice(explode('.', $wpversion), 0, 2)) . '.x';
$mo_files = array();
if(empty($this->xml)){
$this->load_xml();
}
$projects = $this->xml->xpath($lang_code . '/' . $locale);
if(!empty($projects)){
$project_names = array();
foreach($projects[0] as $project_name => $data){
// subprojects
if(empty($data->versions)){
$subprojects = $this->xml->xpath($lang_code . '/' . $locale . '/' . $project_name);
if(!empty($subprojects)){
foreach($subprojects[0] as $sub_project_name => $sdata){
$project_names[] = $project_name . '/' . $sub_project_name ;
}
}
}else{
$project_names[] = $project_name;
}
}
if(!empty($project_names)){
foreach($project_names as $project_name){
// try to get the corresponding version
$locv_path = $this->xml->xpath("{$lang_code}/{$locale}/{$project_name}/versions/version[@number=\"" . $wpversion . "\"]");
// try to get the dev recent version
if(empty($locv_path)){
$locv_path = $this->xml->xpath("{$lang_code}/{$locale}/{$project_name}/versions/version[@number=\"dev\"]");
}
if(!empty($locv_path)){
$mo_files[$project_name]['url'] = (string)$locv_path[0]->url;
$mo_files[$project_name]['signature'] = (string)$locv_path[0]['signature'];
$mo_files[$project_name]['translated'] = (string)$locv_path[0]['translated'];
$mo_files[$project_name]['untranslated']= (string)$locv_path[0]['untranslated'];
}
}
}
}
return $mo_files;
}
function save_settings(){
update_option('icl_adl_settings', $this->settings);
}
function get_option($name){
return isset($this->settings[$name]) ? $this->settings[$name] : null;
}
function is_locale_installed($locale) {
return isset($this->settings['translations'][$locale]['installed']) &&
($this->settings['translations'][$locale]['installed'] == $this->settings['translations'][$locale]['available']);
}
function get_translations($language, $args = array()){
global $wpdb;
$translations = array();
// defaults
$defaults = array(
'types' => array('core')
);
extract($defaults);
extract($args, EXTR_OVERWRITE);
if(!class_exists('WP_Http')) include_once ABSPATH . WPINC . '/class-http.php';
$client = new WP_Http();
foreach($types as $type){
if(isset($this->translation_files[$language][$type]['url'])){
$response = $client->request($this->translation_files[$language][$type]['url'], array('timeout'=>15));
if(is_wp_error($response)){
$err = __('Error getting the translation file. Please go back and try again.', 'wordpress-language');
if(isset($response->errors['http_request_failed'][0])){
$err .= '
' . $response->errors['http_request_failed'][0];
}
echo '
' . $err . '