/products/photocrati_nextgen/modules/i18n/module.i18n.php

  1. <?php 
  2. /*** 
  3. { 
  4. Module: photocrati-i18n,  
  5. Depends: {photocrati-fs, photocrati-router} 
  6. } 
  7. ***/ 
  8. class M_I18N extends C_Base_Module 
  9. function define($id = 'pope-module',  
  10. $name = 'Pope Module',  
  11. $description = '',  
  12. $version = '',  
  13. $uri = '',  
  14. $author = '',  
  15. $author_uri = '',  
  16. $context = FALSE) 
  17. parent::define( 
  18. 'photocrati-i18n',  
  19. 'Internationalization',  
  20. "Adds I18N resources and methods",  
  21. '0.3',  
  22. 'https://www.imagely.com/languages/',  
  23. 'Imagely',  
  24. 'https://www.imagely.com' 
  25. ); 
  26.  
  27. function _register_adapters() 
  28. // Provides translating the name & description of images, albums, and galleries 
  29. $this->get_registry()->add_adapter('I_Image_Mapper', 'A_I18N_Image_Translation'); 
  30. $this->get_registry()->add_adapter('I_Album_Mapper', 'A_I18N_Album_Translation'); 
  31. $this->get_registry()->add_adapter('I_Gallery_Mapper', 'A_I18N_Gallery_Translation'); 
  32. $this->get_registry()->add_adapter('I_Displayed_Gallery', 'A_I18N_Displayed_Gallery_Translation'); 
  33.  
  34. // qTranslate requires we disable "Hide Untranslated Content" during routed app requests like 
  35. // photocrati-ajax, when uploading new images, or retrieving dynamically altered (watermarked) images 
  36. $this->get_registry()->add_adapter('I_Routing_App', 'A_I18N_Routing_App'); 
  37.  
  38. function _register_hooks() 
  39. add_action('init', array(&$this, 'register_translation_hooks'), 2); 
  40.  
  41. function register_translation_hooks() 
  42. $fs = C_Fs::get_instance(); 
  43. $dir = str_replace( 
  44. $fs->get_document_root('plugins'),  
  45. '',  
  46. $fs->get_abspath('lang', 'photocrati-i18n') 
  47. ); 
  48.  
  49. // Load text domain 
  50. load_plugin_textdomain('nggallery', false, $dir); 
  51.  
  52. // Hooks to register image, gallery, and album name & description with WPML 
  53. add_action('ngg_image_updated', array($this, 'register_image_strings')); 
  54. add_action('ngg_album_updated', array($this, 'register_album_strings')); 
  55. add_action('ngg_created_new_gallery', array($this, 'register_gallery_strings')); 
  56.  
  57. // do not let WPML translate posts we use as a document store 
  58. add_filter('get_translatable_documents', array($this, 'wpml_translatable_documents')); 
  59.  
  60. if (class_exists('SitePress')) 
  61. // Copy AttachToPost entries when duplicating posts to another language 
  62. add_action('icl_make_duplicate', array($this, 'wpml_adjust_gallery_id'), 10, 4); 
  63. add_action('save_post', array($this, 'wpml_set_gallery_language_on_save_post'), 101, 1); 
  64.  
  65. // see function comments 
  66. add_filter('ngg_displayed_gallery_cache_params', array($this, 'set_qtranslate_cache_parameters')); 
  67. add_filter('ngg_displayed_gallery_cache_params', array($this, 'set_wpml_cache_parameters')); 
  68.  
  69. /** 
  70. * When QTranslate is active we must add its language & url-mode settings as display parameters 
  71. * so as to generate a unique cache for each language. 
  72. * 
  73. * @param array $arr 
  74. * @return array 
  75. */ 
  76. function set_qtranslate_cache_parameters($arr) 
  77. if (empty($GLOBALS['q_config']) || !defined('QTRANS_INIT')) 
  78. return $arr; 
  79.  
  80. global $q_config; 
  81. $arr['qtranslate_language'] = $q_config['language']; 
  82. $arr['qtranslate_url_mode'] = $q_config['url_mode']; 
  83.  
  84. return $arr; 
  85.  
  86. /** 
  87. * See notes on set_qtranslate_cache_paramters() 
  88. * 
  89. * @param array $arr 
  90. * @return array 
  91. */ 
  92. function set_wpml_cache_parameters($arr) 
  93. if (empty($GLOBALS['sitepress']) || !defined('WPML_ST_VERSION')) 
  94. return $arr; 
  95.  
  96. global $sitepress; 
  97. $settings = $sitepress->get_settings(); 
  98. $arr['wpml_language'] = ICL_LANGUAGE_CODE; 
  99. $arr['wpml_url_mode'] = $settings['language_negotiation_type']; 
  100.  
  101. return $arr; 
  102.  
  103. /** 
  104. * Registers gallery strings with WPML 
  105. * 
  106. * @param object $gallery 
  107. */ 
  108. function register_gallery_strings($gallery_id) 
  109. if (function_exists('icl_register_string')) 
  110. $gallery = C_Gallery_Mapper::get_instance()->find($gallery_id); 
  111. if ($gallery) 
  112. if (isset($gallery->title) && !empty($gallery->title)) 
  113. icl_register_string('plugin_ngg', 'gallery_' . $gallery->{$gallery->id_field} . '_name', $gallery->title, TRUE); 
  114. if (isset($gallery->galdesc) && !empty($gallery->galdesc)) 
  115. icl_register_string('plugin_ngg', 'gallery_' . $gallery->{$gallery->id_field} . '_description', $gallery->galdesc, TRUE); 
  116.  
  117. /** 
  118. * Registers image strings with WPML 
  119. * 
  120. * @param object $image 
  121. */ 
  122. function register_image_strings($image) 
  123. if (function_exists('icl_register_string')) 
  124. if (isset($image->description) && !empty($image->description)) 
  125. icl_register_string('plugin_ngg', 'pic_' . $image->{$image->id_field} . '_description', $image->description, TRUE); 
  126. if (isset($image->alttext) && !empty($image->alttext)) 
  127. icl_register_string('plugin_ngg', 'pic_' . $image->{$image->id_field} . '_alttext', $image->alttext, TRUE); 
  128.  
  129. /** 
  130. * Registers album strings with WPML 
  131. * 
  132. * @param object $album 
  133. */ 
  134. function register_album_strings($album) 
  135. if (function_exists('icl_register_string')) 
  136. if (isset($album->name) && !empty($album->name)) 
  137. icl_register_string('plugin_ngg', 'album_' . $album->{$album->id_field} . '_name', $album->name, TRUE); 
  138. if (isset($album->albumdesc) && !empty($album->albumdesc)) 
  139. icl_register_string('plugin_ngg', 'album_' . $album->{$album->id_field} . '_description', $album->albumdesc, TRUE); 
  140.  
  141. /** 
  142. * NextGEN stores some data in custom posts that MUST NOT be automatically translated by WPML 
  143. * 
  144. * @param array $icl_post_types 
  145. * @return array $icl_post_types without any NextGEN custom posts 
  146. */ 
  147. function wpml_translatable_documents($icl_post_types = array()) 
  148. $nextgen_post_types = array( 
  149. 'ngg_album',  
  150. 'ngg_gallery',  
  151. 'ngg_pictures',  
  152. 'display_type',  
  153. 'gal_display_source',  
  154. 'lightbox_library',  
  155. 'photocrati-comments' 
  156. ); 
  157. foreach ($icl_post_types as $ndx => $post_type) { 
  158. if (in_array($post_type->name, $nextgen_post_types)) 
  159. unset($icl_post_types[$ndx]); 
  160. return $icl_post_types; 
  161.  
  162. function wpml_adjust_gallery_id($master_post_id, $lang, $post_array, $id) 
  163. if (!isset($post_array['post_type']) || $post_array['post_type'] == "displayed_gallery") 
  164. return; 
  165.  
  166. $re = "|preview/id--(\d+)|mi"; 
  167. if (preg_match_all($re, $post_array['post_content'], $gallery_ids)) 
  168. foreach($gallery_ids[1] as $index => $gallery_id) { 
  169. $translated_gallery_id = apply_filters('wpml_object_id', (int)$gallery_id, "displayed_gallery", true, $lang); 
  170. $search[$index] = "preview/id--" . $gallery_id; 
  171. $replace[$index] = "preview/id--" . $translated_gallery_id; 
  172.  
  173. $post_array['post_content'] = str_replace($search, $replace, $post_array['post_content']); 
  174.  
  175. $to_save = array( 
  176. 'ID' => $id,  
  177. 'post_content' => $post_array['post_content'] 
  178. ); 
  179.  
  180. add_filter('ngg_cleanup_displayed_galleries', '__return_false', 10, 1); 
  181. wp_update_post($to_save); 
  182. add_filter('ngg_cleanup_displayed_galleries', '__return_true', 11, 1); 
  183.  
  184. function wpml_set_gallery_language_on_save_post($post_id) 
  185. if (wp_is_post_revision($post_id)) 
  186. return; 
  187.  
  188. if (isset($_POST['icl_ajx_action']) && $_POST['icl_ajx_action'] == 'make_duplicates') 
  189. return; 
  190.  
  191. $post = get_post($post_id); 
  192.  
  193. if ($post->post_type == 'displayed_gallery') 
  194. return; 
  195.  
  196. if (preg_match_all("#<img.*http(s)?://(.*)?" . NGG_ATTACH_TO_POST_SLUG . "(=|/)preview(/|&|&)id(=|--)(\\d+).*?>#mi", $post->post_content, $matches, PREG_SET_ORDER)) 
  197. $mapper = C_Displayed_Gallery_Mapper::get_instance(); 
  198. foreach ($matches as $match) { 
  199. // Find the displayed gallery 
  200. $displayed_gallery_id = $match[6]; 
  201. add_filter('wpml_suppress_filters', '__return_true', 10, 1); 
  202. $displayed_gallery = $mapper->find($displayed_gallery_id, TRUE); 
  203. add_filter('wpml_suppress_filters', '__return_false', 11, 1); 
  204. if ($displayed_gallery) 
  205. $displayed_gallery_type = apply_filters('wpml_element_type', 'displayed_gallery'); 
  206.  
  207. // set language of this gallery 
  208. $displayed_gallery_lang = apply_filters('wpml_post_language_details', null, $displayed_gallery->ID); 
  209. $post_language = apply_filters('wpml_post_language_details', null, $post_id); 
  210.  
  211. if (!$displayed_gallery_lang || $displayed_gallery_lang['language_code'] != $post_language['language_code']) 
  212. if ($post_language) 
  213. $args = array( 
  214. 'element_id' => $displayed_gallery->ID,  
  215. 'element_type' => $displayed_gallery_type,  
  216. 'language_code' => $post_language['language_code'] 
  217. ); 
  218. do_action('wpml_set_element_language_details', $args); 
  219.  
  220. // duplicate gallery to other languages 
  221. $is_translated = apply_filters('wpml_element_has_translations', '', $displayed_gallery->ID, $displayed_gallery_type); 
  222. if (!$is_translated) 
  223. do_action('wpml_admin_make_post_duplicates', $displayed_gallery->ID); 
  224.  
  225. static function translate($in, $name = null) 
  226. if (function_exists('langswitch_filter_langs_with_message')) 
  227. $in = langswitch_filter_langs_with_message($in); 
  228.  
  229. if (function_exists('polyglot_filter')) 
  230. $in = polyglot_filter($in); 
  231.  
  232. if (function_exists('qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage')) 
  233. $in = qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($in); 
  234.  
  235. if (is_string($name) && !empty($name) && function_exists('icl_translate')) 
  236. $in = icl_translate('plugin_ngg', $name, $in, true); 
  237.  
  238. $in = apply_filters('localization', $in); 
  239.  
  240. return $in; 
  241.  
  242. static function mb_pathinfo($path, $options=null) 
  243. $ret = array( 
  244. 'dirname' => '',  
  245. 'basename' => '',  
  246. 'extension' => '',  
  247. 'filename' => '' 
  248. ); 
  249. $pathinfo = array(); 
  250. if (preg_match('%^(.*?)[\\\\/]*(([^/\\\\]*?)(\.([^\.\\\\/]+?)|))[\\\\/\.]*$%im', $path, $pathinfo)) 
  251. if (array_key_exists(1, $pathinfo)) 
  252. $ret['dirname'] = $pathinfo[1]; 
  253. if (array_key_exists(2, $pathinfo)) 
  254. $ret['basename'] = $pathinfo[2]; 
  255. if (array_key_exists(5, $pathinfo)) 
  256. $ret['extension'] = $pathinfo[5]; 
  257. if (array_key_exists(3, $pathinfo)) 
  258. $ret['filename'] = $pathinfo[3]; 
  259. switch ($options) { 
  260. case PATHINFO_DIRNAME: 
  261. case 'dirname': 
  262. return $ret['dirname']; 
  263. case PATHINFO_BASENAME: 
  264. case 'basename': 
  265. return $ret['basename']; 
  266. case PATHINFO_EXTENSION: 
  267. case 'extension': 
  268. return $ret['extension']; 
  269. case PATHINFO_FILENAME: 
  270. case 'filename': 
  271. return $ret['filename']; 
  272. default: 
  273. return $ret; 
  274.  
  275. static function mb_basename($path) 
  276. $separator = " qq "; 
  277. $path = preg_replace("/[^ ]/u", $separator . "\$0" . $separator, $path); 
  278. $base = basename($path); 
  279. return str_replace($separator, "", $base); 
  280.  
  281. function get_type_list() 
  282. return array( 
  283. 'A_I18N_Displayed_Gallery_Translation' => 'adapter.i18n_displayed_gallery_translation.php',  
  284. 'A_I18N_Image_Translation' => 'adapter.i18n_image_translation.php',  
  285. 'A_I18N_Album_Translation' => 'adapter.i18n_album_translation.php',  
  286. 'A_I18N_Gallery_Translation' => 'adapter.i18n_gallery_translation.php',  
  287. 'A_I18N_Routing_App' => 'adapter.i18n_routing_app.php' 
  288. ); 
  289.  
  290. new M_I18N(); 
.