PageRenderTime 79ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wpml-string-translation/inc/auto-download-locales.php

https://bitbucket.org/kenaku/karate
PHP | 340 lines | 247 code | 89 blank | 4 comment | 34 complexity | 17e4c83e63cb30560ef0242b2b8c18f3 MD5 | raw file
  1. <?php
  2. class WPML_ST_MO_Downloader{
  3. const LOCALES_XML_FILE = 'http://icanlocalze-static.icanlocalize.com/wp-locales.xml.gz';
  4. const CONTEXT = 'WordPress';
  5. private $settings;
  6. private $xml;
  7. private $translation_files = array();
  8. function __construct(){
  9. global $wp_version;
  10. // requires Sitepress
  11. if(!defined('ICL_SITEPRESS_VERSION') || ICL_PLUGIN_INACTIVE) return;
  12. $wpversion = preg_replace('#-(.+)$#', '', $wp_version);
  13. $this->settings = get_option('icl_adl_settings');
  14. if(empty($this->settings['wp_version']) || version_compare($wp_version, $this->settings['wp_version'], '>')){
  15. try{
  16. $this->updates_check(array('trigger' => 'wp-update'));
  17. }catch(Exception $e){
  18. // do nothing - this is automated request for updates
  19. }
  20. }
  21. add_action('wp_ajax_icl_adm_updates_check', array($this, 'show_updates'));
  22. add_action('wp_ajax_icl_adm_save_preferences', array($this, 'save_preferences'));
  23. }
  24. function updates_check($args = array()){
  25. global $wp_version, $sitepress;
  26. $wpversion = preg_replace('#-(.+)$#', '', $wp_version);
  27. $defaults = array(
  28. 'trigger' => 'manual'
  29. );
  30. extract($defaults);
  31. extract($args, EXTR_OVERWRITE);
  32. $active_languages = $sitepress->get_active_languages();
  33. $default_language = $sitepress->get_default_language();
  34. $this->load_xml();
  35. $this->get_translation_files();
  36. $updates = array();
  37. foreach($active_languages as $language){
  38. if($language != $default_language){
  39. if(isset($this->translation_files[$language['code']]['core'])){
  40. $int = preg_match('@tags/([^/]+)/@', $this->translation_files[$language['code']]['core'], $matches);
  41. if($int){
  42. $version = $matches[1];
  43. if(empty($this->settings['translations'][$language['code']]['installed'])
  44. || version_compare($this->settings['translations'][$language['code']]['installed'], $version, '<')){
  45. $updates['languages'][$language['code']] = $version;
  46. }
  47. $this->settings['translations'][$language['code']]['available'] = $version;
  48. }else{
  49. $int = preg_match('@/trunk/@', $this->translation_files[$language['code']]['core']);
  50. if($int){
  51. $this->settings['translations'][$language['code']]['available'] = 'trunk';
  52. }
  53. }
  54. }
  55. }
  56. }
  57. $this->settings['wp_version'] = $wpversion;
  58. $this->settings['last_time_xml_check'] = time();
  59. $this->settings['last_time_xml_check_trigger'] = $trigger;
  60. $this->save_settings();
  61. return $updates;
  62. }
  63. function show_updates(){
  64. global $sitepress;
  65. $html = '';
  66. try{
  67. $updates = $this->updates_check();
  68. if(!empty($updates)){
  69. $html .= '<table>';
  70. foreach($updates['languages'] as $language => $version){
  71. $l = $sitepress->get_language_details($language);
  72. $html .= '<tr>';
  73. $html .= '<td>' . sprintf(__("Updated %s translation is available for WordPress %s.", 'wpml-string-translation'),
  74. '<strong>' . $l['display_name'] . '</strong>' , '<strong>' . $version . '</strong>') . '</td>';
  75. $html .= '<td align="right">';
  76. $html .= '<a href="' . admin_url('admin.php?page=' . WPML_ST_FOLDER . '/menu/string-translation.php&amp;download_mo=' . $language . '&amp;version=' . $version) . '" class="button-secondary">' . __('Review changes and update', 'wpml-string-translation') . '</a>';
  77. $html .= '</td>';
  78. $html .= '<tr>';
  79. $html .= '</tr>';
  80. }
  81. $html .= '</table>';
  82. }else{
  83. $html .= __('No newer versions found.', 'wpml-string-translation');
  84. }
  85. }catch(Exception $error){
  86. $html .= '<span style="color:#f00" >' . $error->getMessage() . '</span>';
  87. }
  88. echo json_encode(array('html' => $html));
  89. exit;
  90. }
  91. function save_preferences(){
  92. global $sitepress;
  93. $iclsettings['st']['auto_download_mo'] = @intval($_POST['auto_download_mo']);
  94. $iclsettings['hide_upgrade_notice'] = implode('.', array_slice(explode('.', ICL_SITEPRESS_VERSION), 0, 3));
  95. $sitepress->save_settings($iclsettings);
  96. echo json_encode(array('enabled' => $iclsettings['st']['auto_download_mo']));
  97. exit;
  98. }
  99. function save_settings(){
  100. update_option('icl_adl_settings', $this->settings);
  101. }
  102. function get_option($name){
  103. return isset($this->settings[$name]) ? $this->settings[$name] : null;
  104. }
  105. function load_xml(){
  106. if(!class_exists('WP_Http')) include_once ABSPATH . WPINC . '/class-http.php';
  107. $client = new WP_Http();
  108. $response = $client->request(self::LOCALES_XML_FILE, array('timeout'=>15, 'decompress'=>false));
  109. if(is_wp_error($response)){
  110. throw new Exception(__('Failed downloading the language information file. Please go back and try a little later.', 'wpml-string-translation'));
  111. }else{
  112. if($response['response']['code'] == 200){
  113. $this->xml = new SimpleXMLElement(icl_gzdecode($response['body']));
  114. //$this->xml = new SimpleXMLElement($response['body']);
  115. }
  116. }
  117. }
  118. function get_mo_file_urls($locale){
  119. global $wp_version;
  120. $wpversion = preg_replace('#-(.+)$#', '', $wp_version) ;
  121. if(false !== strpos($locale, '_')){
  122. $exp = explode('_', $locale);
  123. $lpath = $exp[0] . '/' . $exp[1];
  124. }else{
  125. $lpath = $locale;
  126. }
  127. $mo_files = array();
  128. $language_path = $this->xml->xpath($lpath . '/versions/version[@number="' . $wpversion . '"]');
  129. if(empty($language_path)){
  130. $language_path = $this->xml->xpath($lpath . '/versions/version[@number="trunk"]');
  131. }
  132. if(!empty($language_path)){
  133. $mo_files = (array)$language_path[0];
  134. unset($mo_files['@attributes']);
  135. }elseif(empty($language_path)){
  136. $other_versions = $this->xml->xpath($lpath . '/versions/version');
  137. if(is_array($other_versions) && !empty($other_versions)){
  138. $most_recent = 0;
  139. foreach($other_versions as $v){
  140. $tmpv = (string)$v->attributes()->number;
  141. if(version_compare($tmpv , $most_recent, '>')){
  142. $most_recent = $tmpv;
  143. }
  144. }
  145. if($most_recent > 0){
  146. $most_recent_version = $this->xml->xpath($lpath . '/versions/version[@number="' . $most_recent . '"]');
  147. $mo_files['core'] = (string)$most_recent_version[0]->core[0];
  148. }
  149. }
  150. }
  151. return $mo_files;
  152. }
  153. function get_translation_files(){
  154. global $sitepress;
  155. $active_languages = $sitepress->get_active_languages();
  156. $default_language = $sitepress->get_default_language();
  157. foreach($active_languages as $language){
  158. $locale = $sitepress->get_locale($language['code']);
  159. $urls = $this->get_mo_file_urls($locale);
  160. if(!empty($urls)){
  161. $this->translation_files[$language['code']] = $urls;
  162. }
  163. }
  164. return $this->translation_files;
  165. }
  166. function get_translations($language, $args = array()){
  167. global $wpdb;
  168. $translations = array();
  169. // defaults
  170. $defaults = array(
  171. 'type' => 'core'
  172. );
  173. extract($defaults);
  174. extract($args, EXTR_OVERWRITE);
  175. if(isset($this->translation_files[$language])){
  176. if(!class_exists('WP_Http')) include_once ABSPATH . WPINC . '/class-http.php';
  177. $client = new WP_Http();
  178. $response = $client->request($this->translation_files[$language][$type], array('timeout'=>15));
  179. if(is_wp_error($response)){
  180. $err = __('Error getting the translation file. Please go back and try again.', 'wpml-string-translation');
  181. if(isset($response->errors['http_request_failed'][0])){
  182. $err .= '<br />' . $response->errors['http_request_failed'][0];
  183. }
  184. echo '<div class="error"><p>' . $err . '</p></div>';
  185. return false;
  186. }
  187. $mo = new MO();
  188. $pomo_reader = new POMO_StringReader($response['body']);
  189. $mo->import_from_reader( $pomo_reader );
  190. foreach($mo->entries as $key=>$v){
  191. $tpairs = array();
  192. $v->singular = str_replace("\n",'\n', $v->singular);
  193. $tpairs[] = array(
  194. 'string' => $v->singular,
  195. 'translation' => $v->translations[0],
  196. 'name' => !empty($v->context) ? $v->context . ': ' . $v->singular : md5($v->singular)
  197. );
  198. if($v->is_plural){
  199. $v->plural = str_replace("\n",'\n', $v->plural);
  200. $tpairs[] = array(
  201. 'string' => $v->plural,
  202. 'translation' => !empty($v->translations[1]) ? $v->translations[1] : $v->translations[0],
  203. 'name' => !empty($v->context) ? $v->context . ': ' . $v->plural : md5($v->singular)
  204. );
  205. }
  206. foreach($tpairs as $pair){
  207. $existing_translation = $wpdb->get_var($wpdb->prepare("
  208. SELECT st.value
  209. FROM {$wpdb->prefix}icl_string_translations st
  210. JOIN {$wpdb->prefix}icl_strings s ON st.string_id = s.id
  211. WHERE s.context = %s AND s.name = %s AND st.language = %s
  212. ", self::CONTEXT, $pair['name'], $language));
  213. if(empty($existing_translation)){
  214. $translations['new'][] = array(
  215. 'string' => $pair['string'],
  216. 'translation' => '',
  217. 'new' => $pair['translation'],
  218. 'name' => $pair['name']
  219. );
  220. }else{
  221. if(strcmp($existing_translation, $pair['translation']) !== 0){
  222. $translations['updated'][] = array(
  223. 'string' => $pair['string'],
  224. 'translation' => $existing_translation,
  225. 'new' => $pair['translation'],
  226. 'name' => $pair['name']
  227. );
  228. }
  229. }
  230. }
  231. }
  232. }
  233. return $translations;
  234. }
  235. function save_translations($data, $language, $version = false){
  236. if(false === $version){
  237. global $wp_version;
  238. $version = preg_replace('#-(.+)$#', '', $wp_version) ;
  239. }
  240. foreach($data as $key => $string){
  241. $string_id = icl_register_string(self::CONTEXT, $string['name'], $string['string']);
  242. if($string_id){
  243. icl_add_string_translation($string_id, $language, $string['translation'], ICL_STRING_TRANSLATION_COMPLETE);
  244. }
  245. }
  246. $this->settings['translations'][$language]['time'] = time();
  247. $this->settings['translations'][$language]['installed'] = $version;
  248. $this->save_settings();
  249. }
  250. }
  251. ?>