PageRenderTime 129ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-content/plugins/sitepress-multilingual-cms/inc/wpml-api.php

https://bitbucket.org/dkrzos/phc
PHP | 519 lines | 252 code | 82 blank | 185 comment | 50 complexity | da8da80be9c4568931cd8ded993153e8 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /* This file includes a set of funcitons that can be used by WP plugins developers to make their plugins interract with WPML */
  3. /* constants */
  4. define('WPML_API_SUCCESS' , 0);
  5. define('WPML_API_ERROR' , 99);
  6. define('WPML_API_INVALID_LANGUAGE_CODE' , 1);
  7. define('WPML_API_INVALID_TRID' , 2);
  8. define('WPML_API_LANGUAGE_CODE_EXISTS' , 3);
  9. define('WPML_API_CONTENT_NOT_FOUND' , 4);
  10. define('WPML_API_TRANSLATION_NOT_FOUND' , 5);
  11. define('WPML_API_INVALID_CONTENT_TYPE' , 6);
  12. define('WPML_API_CONTENT_EXISTS' , 7);
  13. define('WPML_API_FUNCTION_ALREADY_DECLARED', 8);
  14. define('WPML_API_CONTENT_TRANSLATION_DISABLED', 9);
  15. define('WPML_API_GET_CONTENT_ERROR' , 0);
  16. define('WPML_API_MAGIC_NUMBER', 6);
  17. define('WPML_API_ASIAN_LANGUAGES', 'zh-hans|zh-hant|ja|ko');
  18. define('WPML_API_COST_PER_WORD', 0.09);
  19. function _wpml_api_allowed_content_type($content_type){
  20. $reserved_types = array(
  21. 'post' => 1,
  22. 'page' => 1,
  23. 'tax_post_tag' => 1,
  24. 'tax_category' => 1,
  25. 'comment' => 1
  26. );
  27. return !isset($reserved_types[$content_type]) && preg_match('#([a-z0-9_\-])#i', $content_type);
  28. }
  29. /**
  30. * Add translatable content to the WPML translations table
  31. *
  32. * @since 1.3
  33. * @package WPML
  34. * @subpackage WPML API
  35. *
  36. * @param string $content_type Content type.
  37. * @param int $content_id Content ID.
  38. * @param string $language_code Content language code. (defaults to current language)
  39. * @param int $trid Content trid - if a translation in a different language already exists.
  40. *
  41. * @return int error code
  42. * */
  43. function wpml_add_translatable_content($content_type, $content_id, $language_code = false, $trid = false){
  44. global $sitepress, $wpdb;
  45. if(!_wpml_api_allowed_content_type($content_type)){
  46. return WPML_API_INVALID_CONTENT_TYPE;
  47. }
  48. if($language_code && !$sitepress->get_language_details($language_code)){
  49. return WPML_API_INVALID_LANGUAGE_CODE;
  50. }
  51. if($trid){
  52. $trid_type = $wpdb->get_var("SELECT element_type FROM {$wpdb->prefix}icl_translations WHERE trid='{$trid}'");
  53. if(!$trid_type || $trid_type != $content_type){
  54. return WPML_API_INVALID_TRID;
  55. }
  56. }
  57. if($wpdb->get_var("SELECT translation_id FROM {$wpdb->prefix}icl_translations WHERE element_type='".$wpdb->escape($content_type)."' AND element_id='{$content_id}'")){
  58. return WPML_API_CONTENT_EXISTS;
  59. }
  60. $t = $sitepress->set_element_language_details($content_id, $content_type, $trid, $language_code);
  61. if(!$t){
  62. return WPML_API_ERROR;
  63. }else{
  64. return WPML_API_SUCCESS;
  65. }
  66. }
  67. /**
  68. * Update translatable content in the WPML translations table
  69. *
  70. * @since 1.3
  71. * @package WPML
  72. * @subpackage WPML API
  73. *
  74. * @param string $content_type Content type.
  75. * @param int $content_id Content ID.
  76. * @param string $language_code Content language code.
  77. *
  78. * @return int error code
  79. * */
  80. function wpml_update_translatable_content($content_type, $content_id, $language_code){
  81. global $sitepress, $wpdb;
  82. if(!_wpml_api_allowed_content_type($content_type)){
  83. return WPML_API_INVALID_CONTENT_TYPE;
  84. }
  85. if(!$sitepress->get_language_details($language_code)){
  86. return WPML_API_INVALID_LANGUAGE_CODE;
  87. }
  88. $trid = $sitepress->get_element_trid($content_id, $content_type);
  89. if(!$trid){
  90. return WPML_API_CONTENT_NOT_FOUND;
  91. }
  92. $translations = $sitepress->get_element_translations($trid);
  93. if(isset($translations[$language_code]) && !$translations[$language_code]->element_id != $content_id){
  94. return WPML_API_LANGUAGE_CODE_EXISTS;
  95. }
  96. $t = $sitepress->set_element_language_details($content_id, $content_type, $trid, $language_code);
  97. if(!$t){
  98. return WPML_API_ERROR;
  99. }else{
  100. return WPML_API_SUCCESS;
  101. }
  102. }
  103. /**
  104. * Update translatable content in the WPML translations table
  105. *
  106. * @since 1.3
  107. * @package WPML
  108. * @subpackage WPML API
  109. *
  110. * @param string $content_type Content type.
  111. * @param int $content_id Content ID.
  112. * @param string $language_code Content language code. (when ommitted - delete all translations associated with the respective content)
  113. *
  114. * @return int error code
  115. * */
  116. function wpml_delete_translatable_content($content_type, $content_id, $language_code = false){
  117. global $sitepress, $wpdb;
  118. if(!_wpml_api_allowed_content_type($content_type)){
  119. return WPML_API_INVALID_CONTENT_TYPE;
  120. }
  121. if($language_code && !$sitepress->get_language_details($language_code)){
  122. return WPML_API_INVALID_LANGUAGE_CODE;
  123. }
  124. $trid = $sitepress->get_element_trid($content_id, $content_type);
  125. if(!$trid){
  126. return WPML_API_CONTENT_NOT_FOUND;
  127. }
  128. if($language_code){
  129. $translations = $sitepress->get_element_translations($trid);
  130. if(!isset($translations[$language_code])){
  131. return WPML_API_TRANSLATION_NOT_FOUND;
  132. }
  133. }
  134. $sitepress->delete_element_translation($trid, $content_type, $language_code);
  135. return WPML_API_SUCCESS;
  136. }
  137. /**
  138. * Get trid value for a specific piece of content
  139. *
  140. * @since 1.3
  141. * @package WPML
  142. * @subpackage WPML API
  143. *
  144. * @param string $content_type Content type.
  145. * @param int $content_id Content ID.
  146. *
  147. * @return int trid or 0 for error
  148. * */
  149. function wpml_get_content_trid($content_type, $content_id){
  150. global $sitepress;
  151. if(!_wpml_api_allowed_content_type($content_type)){
  152. return WPML_API_GET_CONTENT_ERROR; //WPML_API_INVALID_CONTENT_TYPE;
  153. }
  154. $trid = $sitepress->get_element_trid($content_id, $content_type);
  155. if(!$trid){
  156. return WPML_API_GET_CONTENT_ERROR;
  157. }else{
  158. return $trid;
  159. }
  160. }
  161. /**
  162. * Detects the current language and returns the language relevant content id. optionally it can return the original id if a translation is not found
  163. *
  164. * @since 1.3
  165. * @package WPML
  166. * @subpackage WPML API
  167. *
  168. * @param string $content_type Content type.
  169. * @param int $content_id Content ID.
  170. * @param bool $return_original return the original id when translation not found.
  171. *
  172. * @return int trid or 0 for error
  173. * */
  174. function wpml_get_content($content_type, $content_id, $return_original = true){
  175. global $sitepress, $wpdb;
  176. $trid = $sitepress->get_element_trid($content_id, $content_type);
  177. if(!$trid){
  178. return WPML_API_GET_CONTENT_ERROR;
  179. }else{
  180. if($content_id <= 0){
  181. return $content_id;
  182. }
  183. if($content_type=='category' || $content_type=='post_tag' || $content_type=='tag'){
  184. $content_id = $wpdb->get_var($wpdb->prepare("SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE term_id= %d AND taxonomy='{$content_type}'",$content_id));
  185. }
  186. if($content_type=='post_tag'){
  187. $icl_element_type = 'tax_post_tag';
  188. }elseif($content_type=='category'){
  189. $icl_element_type = 'tax_category';
  190. }elseif($content_type=='page'){
  191. $icl_element_type = 'post';
  192. }else{
  193. $icl_element_type = $content_type;
  194. }
  195. $trid = $sitepress->get_element_trid($content_id, $icl_element_type);
  196. $translations = $sitepress->get_element_translations($trid, $icl_element_type);
  197. if(isset($translations[ICL_LANGUAGE_CODE]->element_id)){
  198. $ret_element_id = $translations[ICL_LANGUAGE_CODE]->element_id;
  199. if($content_type=='category' || $content_type=='post_tag'){
  200. $ret_element_id = $wpdb->get_var($wpdb->prepare("SELECT t.term_id FROM {$wpdb->term_taxonomy} tx JOIN {$wpdb->terms} t ON t.term_id = tx.term_id WHERE tx.term_taxonomy_id = %d AND tx.taxonomy='{$content_type}'", $ret_element_id));
  201. }
  202. }else{
  203. $ret_element_id = $return_original ? $content_id : null;
  204. }
  205. return $ret_element_id;
  206. }
  207. }
  208. /**
  209. * Get translations for a certain piece of content
  210. *
  211. * @since 1.3
  212. * @package WPML
  213. * @subpackage WPML API
  214. *
  215. * @param string $content_type Content type.
  216. * @param int $content_id Content ID.
  217. * @param bool $return_original return the original id when translation not found.
  218. *
  219. * @return int trid or error code
  220. * */
  221. function wpml_get_content_translations($content_type, $content_id, $skip_missing = true){
  222. global $sitepress;
  223. $trid = $sitepress->get_element_trid($content_id, $content_type);
  224. if(!$trid){
  225. return WPML_API_TRANSLATION_NOT_FOUND;
  226. }
  227. $translations = $sitepress->get_element_translations($trid, $content_type, $skip_missing);
  228. $tr = array();
  229. foreach($translations as $k=>$v){
  230. $tr[$k] = $v->element_id;
  231. }
  232. return $tr;
  233. }
  234. /**
  235. * Returns a certain translation for a piece of content
  236. *
  237. * @since 1.3
  238. * @package WPML
  239. * @subpackage WPML API
  240. *
  241. * @param string $content_type Content type.
  242. * @param int $content_id Content ID.
  243. * @param bool $language_code
  244. *
  245. * @return error code or array('lang'=>element_id)
  246. * */
  247. function wpml_get_content_translation($content_type, $content_id, $language_code){
  248. global $sitepress;
  249. $trid = $sitepress->get_element_trid($content_id, $content_type);
  250. if(!$trid){
  251. return WPML_API_CONTENT_NOT_FOUND;
  252. }
  253. $translations = $sitepress->get_element_translations($trid, $content_type, true);
  254. if(!isset($translations[$language_code])){
  255. return WPML_API_TRANSLATION_NOT_FOUND;
  256. }else{
  257. return array($language_code => $translations[$language_code]->element_id);
  258. }
  259. }
  260. /**
  261. * Returns the list of active languages
  262. *
  263. * @since 1.3
  264. * @package WPML
  265. * @subpackage WPML API
  266. *
  267. *
  268. * @return array
  269. * */
  270. function wpml_get_active_languages(){
  271. global $sitepress;
  272. $langs = $sitepress->get_active_languages();
  273. return $langs;
  274. }
  275. /**
  276. * Returns the default language
  277. *
  278. * @since 1.3
  279. * @package WPML
  280. * @subpackage WPML API
  281. *
  282. *
  283. * @return string
  284. * */
  285. function wpml_get_default_language(){
  286. global $sitepress;
  287. return $sitepress->get_default_language();
  288. }
  289. /**
  290. * Get current language
  291. *
  292. * @since 1.3
  293. * @package WPML
  294. * @subpackage WPML API
  295. *
  296. * @return string
  297. * */
  298. function wpml_get_current_language(){
  299. global $sitepress;
  300. return $sitepress->get_current_language();
  301. }
  302. /**
  303. * Get contents of a specific type
  304. *
  305. * @since 1.3
  306. * @package WPML
  307. * @subpackage WPML API
  308. *
  309. * @param string $content_type Content type.
  310. *
  311. * @return int or array
  312. * */
  313. function wpml_get_contents($content_type, $language_code = false){
  314. global $sitepress, $wpdb;
  315. if($language_code && !$sitepress->get_language_details($language_code)){
  316. return WPML_API_INVALID_LANGUAGE_CODE;
  317. }
  318. if(!$language_code){
  319. $language_code = $sitepress->get_current_language();
  320. }
  321. $contents = $wpdb->get_col("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE element_type='".$wpdb->escape($content_type)."' AND language_code='{$language_code}'");
  322. return $contents;
  323. }
  324. /**
  325. * Returns google translation for given string
  326. *
  327. * @since 1.3
  328. * @package WPML
  329. * @subpackage WPML API
  330. *
  331. * @param string $string String
  332. * @param string $from_language Language to translate from
  333. * @param string $to_language Language to translate into
  334. *
  335. * @return int (error code) or string
  336. * */
  337. function wpml_machine_translation($string, $from_language, $to_language){
  338. global $sitepress;
  339. if(!$sitepress->get_language_details($from_language) || !$sitepress->get_language_details($to_language)){
  340. return WPML_API_INVALID_LANGUAGE_CODE;
  341. }
  342. return IclCommentsTranslation::machine_translate($from_language, $to_language, $string);
  343. }
  344. /**
  345. * Sends piece of content (string) to professional translation @ ICanLocalize
  346. *
  347. * @since 1.3
  348. * @package WPML
  349. * @subpackage WPML API
  350. *
  351. * @param string $string String
  352. * @param string $from_language Language to translate from
  353. * @param int $content_id Content ID
  354. * @param string $content_type Content Type
  355. * @param string $to_language Language to translate into
  356. *
  357. * @return int request id
  358. * */
  359. function wpml_send_content_to_translation($string, $content_id, $content_type, $from_language, $to_language){
  360. global $sitepress, $sitepress_settings, $wpdb;
  361. if(!$sitepress->get_icl_translation_enabled()){
  362. return 0; //WPML_API_CONTENT_TRANSLATION_DISABLED
  363. }
  364. if(!_wpml_api_allowed_content_type($content_type)){
  365. return 0; //WPML_API_INVALID_CONTENT_TYPE
  366. }
  367. if(!$sitepress->get_language_details($from_language) || !$sitepress->get_language_details($to_language)){
  368. return 0; // WPML_API_INVALID_LANGUAGE_CODE
  369. }
  370. $from_lang = $sitepress->get_language_details($from_language);
  371. $to_lang = $sitepress->get_language_details($to_language);
  372. $from_lang_server = apply_filters('icl_server_languages_map', $from_lang['english_name']);
  373. $to_lang_server = apply_filters('icl_server_languages_map', $to_lang['english_name']);
  374. $iclq = new ICanLocalizeQuery($sitepress_settings['site_id'], $sitepress_settings['access_key']);
  375. $rid = $iclq->cms_create_message($string, $from_lang_server, $to_lang_server);
  376. if($rid > 0){
  377. // does this comment already exist in the messages status queue?
  378. $msid = $wpdb->get_var("SELECT id FROM {$wpdb->prefix}icl_message_status WHERE object_type='{$content_type}' AND object_id={$content_id}");
  379. if($msid){
  380. $wpdb->update($wpdb->prefix.'icl_message_status',
  381. array('rid'=>$rid, 'md5' => md5($string), 'status' => MESSAGE_TRANSLATION_IN_PROGRESS),
  382. array('id' => $msid)
  383. );
  384. }else{
  385. $wpdb->insert($wpdb->prefix.'icl_message_status', array(
  386. 'rid' => $rid,
  387. 'object_id' => $content_id,
  388. 'from_language' => $from_language,
  389. 'to_language' => $to_language,
  390. 'md5' => md5($string),
  391. 'object_type' => $content_type,
  392. 'status' => MESSAGE_TRANSLATION_IN_PROGRESS
  393. ));
  394. }
  395. }
  396. return $rid;
  397. }
  398. /**
  399. * Registers a callback for when a translation is received from the server.
  400. * The callback parameters are int $request_id, string $content, string $language
  401. * @since 1.3
  402. * @package WPML
  403. * @subpackage WPML API
  404. *
  405. * @param string $content_type
  406. * @param string $callback
  407. *
  408. * @return error code (0 on success)
  409. * */
  410. function wpml_add_callback_for_received_translation($content_type, $callback){
  411. global $wpml_add_message_translation_callbacks;
  412. $wpml_add_message_translation_callbacks[$content_type][] = $callback;
  413. return 0;
  414. }
  415. /**
  416. * Returns the number of the words that will be sent to translation and a cost estimate
  417. * @since 1.3
  418. * @package WPML
  419. * @subpackage WPML API
  420. *
  421. * @param string $string
  422. * @param string $language - should be specified when the language is one of zh-hans|zh-hant|ja|ko
  423. *
  424. * @return array (count, cost)
  425. * */
  426. function wpml_get_word_count($string, $language = false){
  427. $asian_languages = explode('|', WPML_API_ASIAN_LANGUAGES);
  428. if($language && in_array($language, $asian_languages)){
  429. $count = ceil(mb_strlen($string)/WPML_API_MAGIC_NUMBER);
  430. }else{
  431. $count = count(explode(' ', $string));
  432. }
  433. $cost = $count * WPML_API_COST_PER_WORD;
  434. $ret = array('count'=>$count, 'cost'=>$cost);
  435. return $ret;
  436. }
  437. ?>