Advertisement
mbis

permalink-manager-language-plugins.php

May 19th, 2022
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.17 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Third parties integration
  5. */
  6. class Permalink_Manager_Language_Plugins extends Permalink_Manager_Class {
  7.  
  8. public function __construct() {
  9. add_action('init', array($this, 'init_hooks'), 99);
  10. }
  11.  
  12. function init_hooks() {
  13. global $sitepress_settings, $permalink_manager_options, $polylang, $translate_press_settings;
  14.  
  15. // 1. WPML, Polylang & TranslatePress
  16. if($sitepress_settings || !empty($polylang->links_model->options) || class_exists('TRP_Translate_Press')) {
  17. // Detect Post/Term function
  18. add_filter('permalink_manager_detected_post_id', array($this, 'fix_language_mismatch'), 9, 3);
  19. add_filter('permalink_manager_detected_term_id', array($this, 'fix_language_mismatch'), 9, 3);
  20.  
  21. // Fix posts page
  22. // else {
  23. add_filter('permalink_manager_filter_query', array($this, 'fix_posts_page'), 5, 5);
  24. // }
  25.  
  26. // URI Editor
  27. add_filter('permalink_manager_uri_editor_extra_info', array($this, 'language_column_uri_editor'), 9, 3);
  28.  
  29. // Adjust front page ID
  30. add_filter('permalink_manager_is_front_page', array($this, 'wpml_is_front_page'), 9, 3);
  31.  
  32. // Get translation mode
  33. $mode = 0;
  34.  
  35. // A. WPML
  36. if(isset($sitepress_settings['language_negotiation_type'])) {
  37. $url_settings = $sitepress_settings['language_negotiation_type'];
  38.  
  39. if(in_array($sitepress_settings['language_negotiation_type'], array(1, 2))) {
  40. $mode = 'prepend';
  41. } else if($sitepress_settings['language_negotiation_type'] == 3) {
  42. $mode = 'append';
  43. }
  44. }
  45. // B. Polylang
  46. else if(isset($polylang->links_model->options['force_lang'])) {
  47. $url_settings = $polylang->links_model->options['force_lang'];
  48.  
  49. if(in_array($url_settings, array(1, 2, 3))) {
  50. $mode = 'prepend';
  51. }
  52. }
  53. // C. TranslatePress
  54. else if(class_exists('TRP_Translate_Press')) {
  55. $translate_press_settings = get_option('trp_settings');
  56.  
  57. $mode = 'prepend';
  58. }
  59.  
  60. if($mode === 'prepend') {
  61. add_filter('permalink_manager_detect_uri', array($this, 'detect_uri_language'), 9, 3);
  62. add_filter('permalink_manager_filter_permalink_base', array($this, 'prepend_lang_prefix'), 9, 2);
  63. add_filter('template_redirect', array($this, 'wpml_redirect'), 0, 998 );
  64. } else if($mode === 'append') {
  65. add_filter('permalink_manager_filter_final_post_permalink', array($this, 'append_lang_prefix'), 5, 2);
  66. add_filter('permalink_manager_filter_final_term_permalink', array($this, 'append_lang_prefix'), 5, 2);
  67. add_filter('permalink_manager_detect_uri', array($this, 'wpml_ignore_lang_query_parameter'), 9);
  68. }
  69.  
  70. // Translate permastructures
  71. add_filter('permalink_manager_filter_permastructure', array($this, 'translate_permastructure'), 9, 2);
  72.  
  73. // Translate custom permalinks
  74. if($this->is_wpml_compatible()) {
  75. add_filter('permalink_manager_filter_final_post_permalink', array($this, 'translate_post_permalinks'), 9, 2);
  76. }
  77.  
  78. // Translate post type slug
  79. if(class_exists('WPML_Slug_Translation')) {
  80. add_filter('permalink_manager_filter_post_type_slug', array($this, 'wpml_translate_post_type_slug'), 9, 3);
  81. }
  82.  
  83. // Translate WooCommerce endpoints
  84. if(class_exists('WCML_Endpoints')) {
  85. add_filter('request', array($this, 'wpml_translate_wc_endpoints'), 99999);
  86. }
  87.  
  88. // Edit custom URI using WPML Classic Translation Editor
  89. if(class_exists('WPML_Translation_Editor_UI')) {
  90. add_filter('wpml_tm_adjust_translation_fields', array($this, 'wpml_translation_edit_uri'), 999, 2);
  91. add_action('icl_pro_translation_saved', array($this, 'wpml_translation_save_uri'), 999, 3);
  92. add_filter('wpml_translation_editor_save_job_data', array($this, 'wpml_translation_save_uri'), 999, 2);
  93. }
  94.  
  95. // Generate custom permalink after WPML's Advanced Translation editor is used
  96. if(!empty($sitepress_settings['translation-management']) && !empty($sitepress_settings['translation-management']['doc_translation_method']) && $sitepress_settings['translation-management']['doc_translation_method'] == 'ATE') {
  97. add_action('icl_pro_translation_completed', array($this, 'regenerate_uri_after_wpml_translation_completed'), 99, 3);
  98. }
  99.  
  100. add_action('icl_make_duplicate', array($this, 'wpml_duplicate_uri'), 999, 4);
  101.  
  102. // Allow canonical redirect for default language if "Hide URL language information for default language" is turned on in Polylang settings
  103. if(!empty($polylang) && !empty($polylang->links_model) && !empty($polylang->links_model->options['hide_default'])) {
  104. add_filter('permalink_manager_filter_query', array($this, 'pl_allow_canonical_redirect'), 3, 5);
  105. }
  106. }
  107. }
  108.  
  109. /**
  110. * Let users decide if they want Permalink Manager to force language code in the custom permalinks
  111. */
  112. public static function is_wpml_compatible() {
  113. global $permalink_manager_options;
  114.  
  115. // Use the current language if translation is not available but fallback mode is turned on
  116. return (!empty($permalink_manager_options['general']['wpml_support'])) ? $permalink_manager_options['general']['wpml_support'] : false;
  117. }
  118.  
  119. /**
  120. * WPML/Polylang/TranslatePress filters
  121. */
  122. public static function get_language_code($element) {
  123. global $TRP_LANGUAGE, $translate_press_settings, $icl_adjust_id_url_filter_off, $sitepress, $wpml_post_translations, $wpml_term_translations;
  124.  
  125. // Disable WPML adjust ID filter
  126. $icl_adjust_id_url_filter_off = true;
  127.  
  128. // Fallback
  129. if(is_string($element) && strpos($element, 'tax-') !== false) {
  130. $element_id = intval(preg_replace("/[^0-9]/", "", $element));
  131. $element = get_term($element_id);
  132. } else if(is_numeric($element)) {
  133. $element = get_post($element);
  134. }
  135.  
  136. $lang_code = '';
  137.  
  138. // A. TranslatePress
  139. if(!empty($TRP_LANGUAGE)) {
  140. $lang_code = self::get_translatepress_language_code($TRP_LANGUAGE);
  141. }
  142. // B. WPML & Polylang
  143. else {
  144. $is_wpml_compatible = (!empty($sitepress) && method_exists($sitepress, 'is_display_as_translated_post_type')) ? self::is_wpml_compatible() : false;
  145.  
  146. if(isset($element->post_type)) {
  147. $element_id = $element->ID;
  148. $element_type = $element->post_type;
  149.  
  150. $fallback_lang_on = ($is_wpml_compatible) ? $sitepress->is_display_as_translated_post_type($element_type) : false;
  151. } else if(isset($element->taxonomy)) {
  152. $element_id = $element->term_taxonomy_id;
  153. $element_type = $element->taxonomy;
  154.  
  155. if($is_wpml_compatible) {
  156. if(class_exists('WPML_Translation_Element_Factory2')) {
  157. $wpml_tef = new WPML_Translation_Element_Factory($sitepress);
  158. $wpml_cel = $wpml_tef->create($element_id, 'term');
  159. $wpml_gls = WPML_Get_LS_Languages_Status::get_instance();
  160.  
  161. if(!($wpml_cel->is_display_as_translated() && $wpml_cel->is_in_default_language()) || $wpml_gls->is_getting_ls_languages()) {
  162. $lang_code = $wpml_cel->get_language_code();
  163. }
  164. } else {
  165. $fallback_lang_on = $sitepress->is_display_as_translated_taxonomy($element_type);
  166. }
  167. } else {
  168. $fallback_lang_on = false;
  169. }
  170.  
  171. $fallback_lang_on = ($is_wpml_compatible) ? $sitepress->is_display_as_translated_taxonomy($element_type) : false;
  172. } else {
  173. return false;
  174. }
  175.  
  176. if(!empty($fallback_lang_on) && !is_admin() && !wp_doing_ajax() && !defined('REST_REQUEST')) {
  177. $current_language = $sitepress->get_current_language();
  178.  
  179. if(!empty($element->post_type)) {
  180. $force_current_lang = $wpml_post_translations->element_id_in($element_id, $current_language) ? false : $current_language;
  181. } else if(!empty($element->taxonomy)) {
  182. $force_current_lang = $wpml_term_translations->element_id_in($element_id, $current_language) ? false : $current_language;
  183. }
  184. }
  185.  
  186. $lang_code = (!empty($force_current_lang)) ? $force_current_lang : apply_filters('wpml_element_language_code', $lang_code, array('element_id' => $element_id, 'element_type' => $element_type));
  187. }
  188.  
  189. // Enable WPML adjust ID filter
  190. $icl_adjust_id_url_filter_off = false;
  191.  
  192. // Use default language if nothing detected
  193. return ($lang_code) ? $lang_code : self::get_default_language();
  194. }
  195.  
  196. public static function get_translatepress_language_code($lang) {
  197. global $translate_press_settings;
  198.  
  199. if(!empty($translate_press_settings['url-slugs'])) {
  200. $lang_code = (!empty($translate_press_settings['url-slugs'][$lang])) ? $translate_press_settings['url-slugs'][$lang] : '';
  201. }
  202.  
  203. return (!empty($lang_code)) ? $lang_code : false;
  204. }
  205.  
  206. public static function get_default_language() {
  207. global $sitepress, $translate_press_settings;
  208.  
  209. if(function_exists('pll_default_language')) {
  210. $def_lang = pll_default_language('slug');
  211. } else if(is_object($sitepress)) {
  212. $def_lang = $sitepress->get_default_language();
  213. } else if(!empty($translate_press_settings['default-language'])) {
  214. $def_lang = self::get_translatepress_language_code($translate_press_settings['default-language']);
  215. } else {
  216. $def_lang = '';
  217. }
  218.  
  219. return $def_lang;
  220. }
  221.  
  222. public static function get_all_languages($exclude_default_language = false) {
  223. global $sitepress, $sitepress_settings, $polylang, $translate_press_settings;
  224.  
  225. $languages_array = $active_languages = array();
  226. $default_language = self::get_default_language();
  227.  
  228. if(!empty($sitepress_settings['active_languages'])) {
  229. $languages_array = $sitepress_settings['active_languages'];
  230. } elseif(function_exists('pll_languages_list')) {
  231. $languages_array = pll_languages_list(array('fields' => null));
  232. } if(!empty($translate_press_settings['url-slugs'])) {
  233. // $languages_array = $translate_press_settings['url-slugs'];
  234. }
  235.  
  236. // Get native language names as value
  237. if($languages_array) {
  238. foreach($languages_array as $val) {
  239. if(!empty($sitepress)) {
  240. $lang = $val;
  241. $lang_details = $sitepress->get_language_details($lang);
  242. $language_name = $lang_details['native_name'];
  243. } else if(!empty($val->name)) {
  244. $lang = $val->slug;
  245. $language_name = $val->name;
  246. }
  247.  
  248. $active_languages[$lang] = (!empty($language_name)) ? sprintf('%s <span>(%s)</span>', $language_name, $lang) : '-';
  249. }
  250.  
  251. // Exclude default language if needed
  252. if($exclude_default_language && $default_language && !empty($active_languages[$default_language])) {
  253. unset($active_languages[$default_language]);
  254. }
  255. }
  256.  
  257. return (array) $active_languages;
  258. }
  259.  
  260. function fix_language_mismatch($item_id, $uri_parts, $is_term = false) {
  261. global $wp, $language_code, $permalink_manager_options;
  262.  
  263. $mode = (!empty($permalink_manager_options['general']['fix_language_mismatch'])) ? $permalink_manager_options['general']['fix_language_mismatch'] : 0;
  264.  
  265. if($is_term) {
  266. $element = get_term($item_id);
  267. if(!empty($element) && !is_wp_error($element)) {
  268. $element_id = $element->term_taxonomy_id;
  269. $element_type = $element->taxonomy;
  270. } else {
  271. return false;
  272. }
  273. } else {
  274. $element = get_post($item_id);
  275.  
  276. if(!empty($element->post_type)) {
  277. $element_id = $item_id;
  278. $element_type = $element->post_type;
  279. }
  280. }
  281.  
  282. // Stop if no term or post is detected
  283. if(empty($element)) { return false; }
  284.  
  285. // Get the language code of the found post/term
  286. $element_language_code = self::get_language_code($element);
  287.  
  288. // Get the detected language code
  289. if(defined('ICL_LANGUAGE_CODE')) {
  290. $detected_language_code = ICL_LANGUAGE_CODE;
  291. } else if(!empty($uri_parts['lang'])) {
  292. $detected_language_code = $uri_parts['lang'];
  293. } else {
  294. return $item_id;
  295. }
  296.  
  297. if($detected_language_code !== $element_language_code) {
  298. // A. Display the content in requested language
  299. if($mode == 1) {
  300. $item_id = apply_filters('wpml_object_id', $element_id, $element_type);
  301. }
  302. // C. Display "404 error"
  303. else {
  304. $item_id = 0;
  305. }
  306. }
  307.  
  308. return $item_id;
  309. }
  310.  
  311. /**
  312. * 5C. Fix for WPML (language switcher on blog page)
  313. */
  314. function fix_posts_page($query, $old_query, $uri_parts, $pm_query, $content_type) {
  315. if(empty($pm_query['id']) || !is_numeric($pm_query['id'])) {
  316. return $query;
  317. }
  318.  
  319. $blog_page_id = apply_filters('wpml_object_id', get_option('page_for_posts'), 'page');
  320. $element_id = apply_filters('wpml_object_id', $pm_query['id'], 'page');
  321.  
  322. if(!empty($blog_page_id) && !empty($blog_page_id) && ($blog_page_id == $element_id) && !isset($query['page'])) {
  323. $query['page'] = '';
  324. }
  325.  
  326. return $query;
  327. }
  328.  
  329. function detect_uri_language($uri_parts, $request_url, $endpoints) {
  330. global $sitepress, $sitepress_settings, $polylang, $translate_press_settings;
  331.  
  332. if(!empty($sitepress_settings['active_languages'])) {
  333. $languages_list = (array) $sitepress_settings['active_languages'];
  334. } elseif(function_exists('pll_languages_list')) {
  335. $languages_array = pll_languages_list();
  336. $languages_list = (is_array($languages_array)) ? (array) $languages_array : "";
  337. } elseif($translate_press_settings['url-slugs']) {
  338. $languages_list = $translate_press_settings['url-slugs'];
  339. }
  340.  
  341. if(is_array($languages_list)) {
  342. $languages_list = implode("|", $languages_list);
  343. } else {
  344. return $uri_parts;
  345. }
  346.  
  347. $default_language = self::get_default_language();
  348.  
  349. // Fix for multidomain language configuration
  350. if((isset($sitepress_settings['language_negotiation_type']) && $sitepress_settings['language_negotiation_type'] == 2) || (!empty($polylang->options['force_lang']) && $polylang->options['force_lang'] == 3)) {
  351. if(!empty($polylang->options['domains'])) {
  352. $domains = (array) $polylang->options['domains'];
  353. } else if(!empty($sitepress_settings['language_domains'])) {
  354. $domains = (array) $sitepress_settings['language_domains'];
  355. }
  356.  
  357. foreach($domains as &$domain) {
  358. $domain = preg_replace('/((http(s)?:\/\/(www\.)?)|(www\.))?(.+?)\/?$/', 'http://$6', $domain);
  359. }
  360.  
  361. $request_url = trim(str_replace($domains, "", $request_url), "/");
  362. }
  363.  
  364. if(!empty($languages_list)) {
  365. //preg_match("/^(?:({$languages_list})\/)?(.+?)(?|\/({$endpoints})[\/$]([^\/]*)|\/()([\d+]))?\/?$/i", $request_url, $regex_parts);
  366. preg_match("/^(?:({$languages_list})\/)?(.+?)(?|\/({$endpoints})(?|\/(.*)|$)|\/()([\d]+)\/?)?$/i", $request_url, $regex_parts);
  367.  
  368. $uri_parts['lang'] = (!empty($regex_parts[1])) ? $regex_parts[1] : $default_language;
  369. $uri_parts['uri'] = (!empty($regex_parts[2])) ? $regex_parts[2] : "";
  370. $uri_parts['endpoint'] = (!empty($regex_parts[3])) ? $regex_parts[3] : "";
  371. $uri_parts['endpoint_value'] = (!empty($regex_parts[4])) ? $regex_parts[4] : "";
  372. }
  373.  
  374. return $uri_parts;
  375. }
  376.  
  377. static function prepend_lang_prefix($base, $element, $language_code = '') {
  378. global $sitepress_settings, $polylang, $permalink_manager_uris, $translate_press_settings;
  379.  
  380. if(!empty($element) && empty($language_code)) {
  381. $language_code = self::get_language_code($element);
  382.  
  383. // Last instance - use language paramater from &_GET array
  384. $language_code = (is_admin() && empty($language_code) && !empty($_GET['lang'])) ? sanitize_key($_GET['lang']) : $language_code;
  385. }
  386.  
  387. // Adjust URL base
  388. if(!empty($language_code)) {
  389. $default_language_code = self::get_default_language();
  390. $home_url = get_home_url();
  391.  
  392. // Hide language code if "Use directory for default language" option is enabled
  393. $hide_prefix_for_default_lang = ((isset($sitepress_settings['urls']['directory_for_default_language']) && $sitepress_settings['urls']['directory_for_default_language'] != 1) || !empty($polylang->links_model->options['hide_default']) || (!empty($translate_press_settings) && $translate_press_settings['add-subdirectory-to-default-language'] !== 'yes')) ? true : false;
  394.  
  395. // A. Different domain per language
  396. if((isset($sitepress_settings['language_negotiation_type']) && $sitepress_settings['language_negotiation_type'] == 2) || (!empty($polylang->options['force_lang']) && $polylang->options['force_lang'] == 3)) {
  397. if(!empty($polylang->options['domains'])) {
  398. $domains = $polylang->options['domains'];
  399. } else if(!empty($sitepress_settings['language_domains'])) {
  400. $domains = $sitepress_settings['language_domains'];
  401. }
  402.  
  403. if(!empty($element)) {
  404. $is_term = (!empty($element->term_id)) ? true : false;
  405. $element_id = ($is_term) ? "tax-{$element->term_id}" : $element->ID;
  406.  
  407. // Filter only custom permalinks
  408. if(empty($permalink_manager_uris[$element_id]) || empty($domains)) { return $base; }
  409. }
  410.  
  411. // Replace the domain name
  412. if(!empty($domains[$language_code])) {
  413. $base = trim($domains[$language_code], "/");
  414.  
  415. // Append URL scheme
  416. if(!preg_match("~^(?:f|ht)tps?://~i", $base)) {
  417. $scehme = parse_url($home_url, PHP_URL_SCHEME);
  418. $base = "{$scehme}://{$base}";
  419. }
  420. }
  421. }
  422. // B. Prepend language code
  423. else if(!empty($polylang->options['force_lang']) && $polylang->options['force_lang'] == 2) {
  424. if($hide_prefix_for_default_lang && ($default_language_code == $language_code)) {
  425. return $base;
  426. } else {
  427. $base = preg_replace('/(https?:\/\/)/', "$1{$language_code}.", $home_url);
  428. }
  429. }
  430. // C. Append prefix
  431. else {
  432. if($hide_prefix_for_default_lang && ($default_language_code == $language_code)) {
  433. return $base;
  434. } else {
  435. $base .= "/{$language_code}";
  436. }
  437. }
  438. }
  439.  
  440. return $base;
  441. }
  442.  
  443. function append_lang_prefix($permalink, $element) {
  444. global $sitepress_settings, $polylang, $permalink_manager_uris;
  445.  
  446. $language_code = self::get_language_code($element);
  447. $default_language_code = self::get_default_language();
  448.  
  449. // Last instance - use language paramater from &_GET array
  450. if(is_admin()) {
  451. $language_code = (empty($language_code) && !empty($_GET['lang'])) ? $_GET['lang'] : $language_code;
  452. }
  453.  
  454. // B. Append ?lang query parameter
  455. if(isset($sitepress_settings['language_negotiation_type']) && $sitepress_settings['language_negotiation_type'] == 3) {
  456. if($default_language_code == $language_code) {
  457. return $permalink;
  458. } else if(strpos($permalink, "lang=") === false) {
  459. $permalink .= "?lang={$language_code}";
  460. }
  461. }
  462.  
  463. return $permalink;
  464. }
  465.  
  466. function language_column_uri_editor($output, $column, $element) {
  467. $language_code = self::get_language_code($element);
  468. $output .= (!empty($language_code)) ? sprintf(" | <span><strong>%s:</strong> %s</span>", __("Language"), $language_code) : "";
  469.  
  470. return $output;
  471. }
  472.  
  473. function wpml_is_front_page($bool, $page_id, $front_page_id) {
  474. $default_language_code = self::get_default_language();
  475. $page_id = apply_filters('wpml_object_id', $page_id, 'page', true, $default_language_code);
  476.  
  477. return (!empty($page_id) && $page_id == $front_page_id) ? true : $bool;
  478. }
  479.  
  480. function wpml_ignore_lang_query_parameter($uri_parts) {
  481. global $permalink_manager_uris;
  482.  
  483. foreach($permalink_manager_uris as &$uri) {
  484. $uri = trim(strtok($uri, '?'), "/");
  485. }
  486.  
  487. return $uri_parts;
  488. }
  489.  
  490. function wpml_redirect() {
  491. global $language_code, $wp_query;
  492.  
  493. if(!empty($language_code) && defined('ICL_LANGUAGE_CODE') && ICL_LANGUAGE_CODE != $language_code && !empty($wp_query->query['do_not_redirect'])) {
  494. unset($wp_query->query['do_not_redirect']);
  495. }
  496. }
  497.  
  498. /*
  499. * Copy WPML URL hooks and use them for custom permalinks filtered with Permalink Manager
  500. */
  501. function translate_post_permalinks($permalink, $post) {
  502. global $wpml_url_filters, $wp_filter;
  503.  
  504. if(!empty($wpml_url_filters)) {
  505. $wpml_url_hook_name = _wp_filter_build_unique_id('post_link', array($wpml_url_filters, 'permalink_filter'), 1);
  506.  
  507. if(has_filter('post_link', $wpml_url_hook_name)) {
  508. $permalink = $wpml_url_filters->permalink_filter($permalink, $post);
  509. }
  510. }
  511.  
  512. return $permalink;
  513. }
  514.  
  515. function translate_permastructure($permastructure, $element) {
  516. global $permalink_manager_permastructs, $pagenow;;
  517.  
  518. // Get element language code
  519. if(!empty($_REQUEST['data']) && is_string($_REQUEST['data']) && strpos($_REQUEST['data'], "target_lang")) {
  520. $language_code = preg_replace('/(.*target_lang=)([^=&]+)(.*)/', '$2', $_REQUEST['data']);
  521. } else if(in_array($pagenow, array('post.php', 'post-new.php')) && !empty($_GET['lang'])) {
  522. $language_code = $_GET['lang'];
  523. } else if(!empty($_REQUEST['icl_post_language'])) {
  524. $language_code = $_REQUEST['icl_post_language'];
  525. } else if(!empty($_POST['action']) && $_POST['action'] == 'pm_save_permalink' && defined('ICL_LANGUAGE_CODE')) {
  526. $language_code = ICL_LANGUAGE_CODE;
  527. } else {
  528. $language_code = self::get_language_code($element);
  529. }
  530.  
  531. if(!empty($element->ID)) {
  532. $translated_permastructure = (!empty($permalink_manager_permastructs["post_types"]["{$element->post_type}_{$language_code}"])) ? $permalink_manager_permastructs["post_types"]["{$element->post_type}_{$language_code}"] : '';
  533. } else if(!empty($element->term_id)) {
  534. $translated_permastructure = (!empty($permalink_manager_permastructs["taxonomies"]["{$element->taxonomy}_{$language_code}"])) ? $permalink_manager_permastructs["taxonomies"]["{$element->taxonomy}_{$language_code}"] : '';
  535. }
  536.  
  537. return (!empty($translated_permastructure)) ? $translated_permastructure : $permastructure;
  538. }
  539.  
  540. function wpml_translate_post_type_slug($post_type_slug, $element, $post_type) {
  541. $post = (is_integer($element)) ? get_post($element) : $element;
  542. $language_code = self::get_language_code($post);
  543.  
  544. $post_type_slug = apply_filters('wpml_get_translated_slug', $post_type_slug, $post_type, $language_code);
  545.  
  546. // Translate %post_type% tag in custom permastructures
  547. return $post_type_slug;
  548. }
  549.  
  550. function wpml_translate_wc_endpoints($request) {
  551. global $woocommerce, $wpdb;
  552.  
  553. if(!empty($woocommerce->query->query_vars)) {
  554. // Get WooCommerce original endpoints
  555. $endpoints = $woocommerce->query->query_vars;
  556.  
  557. // Get all endppoint translations
  558. $endpoint_translations = $wpdb->get_results("SELECT t.value AS translated_endpoint, t.language, s.value AS endpoint FROM {$wpdb->prefix}icl_string_translations AS t LEFT JOIN {$wpdb->prefix}icl_strings AS s ON t.string_id = s.id WHERE context = 'WP Endpoints'");
  559.  
  560. // Replace translate endpoint with its original name
  561. foreach($endpoint_translations as $endpoint) {
  562. if(isset($request[$endpoint->translated_endpoint]) && ($endpoint->endpoint !== $endpoint->translated_endpoint)) {
  563. $request[$endpoint->endpoint] = $request[$endpoint->translated_endpoint];
  564. unset($request[$endpoint->translated_endpoint]);
  565. }
  566. }
  567. }
  568.  
  569. return $request;
  570. }
  571.  
  572. /**
  573. * Generate custom permalink after WPML's Advanced Translation editor is used
  574. */
  575. function regenerate_uri_after_wpml_translation_completed($post_id, $postdata, $job) {
  576. global $permalink_manager_uris;
  577.  
  578. // Get the default custom permalink based on a permastructure set with Permalink Manager
  579. if(empty($permalink_manager_uris[$post_id])) {
  580. $permalink_manager_uris[$post_id] = Permalink_Manager_URI_Functions_Post::get_default_post_uri($post_id);
  581.  
  582. // Save the update
  583. update_option('permalink-manager-uris', $permalink_manager_uris);
  584. }
  585. }
  586.  
  587. /**
  588. * Edit custom URI using WPML Classic Translation Editor
  589. */
  590. function wpml_translation_edit_uri($fields, $job) {
  591. global $permalink_manager_uris;
  592.  
  593. $element_type = (!empty($job->original_post_type) && strpos($job->original_post_type, 'post_') !== false) ? preg_replace('/^(post_)/', '', $job->original_post_type) : '';
  594.  
  595. if(!empty($element_type)) {
  596. $original_id = $job->original_doc_id;
  597. $translation_id = apply_filters('wpml_object_id', $original_id, $element_type, false, $job->language_code);
  598.  
  599. $original_custom_uri = Permalink_Manager_URI_Functions_Post::get_post_uri($original_id, true);
  600.  
  601. if(!empty($translation_id)) {
  602. $translation_custom_uri = Permalink_Manager_URI_Functions_Post::get_post_uri($translation_id, true);
  603. $uri_translation_complete = (!empty($permalink_manager_uris[$translation_id])) ? '1' : '0';
  604. } else {
  605. $translation_custom_uri = $original_custom_uri;
  606. $uri_translation_complete = '0';
  607. }
  608.  
  609. $fields[] = array(
  610. 'field_type' => 'pm-custom_uri',
  611. //'tid' => 9999,
  612. 'field_data' => $original_custom_uri,
  613. 'field_data_translated' => $translation_custom_uri,
  614. 'field_finished' => $uri_translation_complete,
  615. 'field_style' => '0',
  616. 'title' => 'Custom URI',
  617. );
  618. }
  619.  
  620. return $fields;
  621. }
  622.  
  623. function wpml_translation_save_uri($in = '', $data = '', $job = '') {
  624. global $permalink_manager_uris;
  625.  
  626. // A. Save the URI also when the translation is uncompleted
  627. if(!empty($in['fields'])) {
  628. $data = $in['fields'];
  629.  
  630. $original_id = $in['job_post_id'];
  631. $element_type = (strpos($in['job_post_type'], 'post_') !== false) ? preg_replace('/^(post_)/', '', $in['job_post_type']) : '';
  632.  
  633. $translation_id = apply_filters('wpml_object_id', $original_id, $element_type, false, $in['target_lang']);
  634. }
  635. // B. Save the URI also when the translation is completed
  636. else if(is_numeric($in)) {
  637. $translation_id = $in;
  638. }
  639.  
  640. if(isset($data['pm-custom_uri']) && isset($data['pm-custom_uri']['data']) && !empty($translation_id)) {
  641. $permalink_manager_uris[$translation_id] = (!empty($data['pm-custom_uri']['data'])) ? Permalink_Manager_Helper_Functions::sanitize_title($data['pm-custom_uri']['data'], true) : Permalink_Manager_URI_Functions_Post::get_default_post_uri($translation_id);
  642.  
  643. update_option('permalink-manager-uris', $permalink_manager_uris);
  644. }
  645.  
  646. // Return the data when the translation is uncompleted
  647. if(!empty($in['fields'])) {
  648. return $in;
  649. }
  650. }
  651.  
  652. function wpml_duplicate_uri($master_post_id, $lang, $post_array, $id) {
  653. global $permalink_manager_uris;
  654.  
  655. // Trigger the function only if duplicate is created in the metabox
  656. if(empty($_POST['action']) || $_POST['action'] !== 'make_duplicates') { return; }
  657.  
  658. $permalink_manager_uris[$id] = Permalink_Manager_URI_Functions_Post::get_default_post_uri($id);
  659.  
  660. update_option('permalink-manager-uris', $permalink_manager_uris);
  661. }
  662.  
  663. function pl_allow_canonical_redirect($query, $old_query, $uri_parts, $pm_query, $content_type) {
  664. global $polylang;
  665.  
  666. // Run only if "Hide URL language information for default language" is turned on in Polylang settings
  667. if(!empty($pm_query['id']) && !empty($pm_query['lang'])) {
  668. $url_lang = $polylang->links_model->get_language_from_url();
  669. $def_lang = pll_default_language('slug');
  670.  
  671. // Check if the slug of default language is present in the requested URL
  672. if($url_lang == $def_lang) {
  673. // Allow canonical redirect
  674. unset($query['do_not_redirect']);
  675. }
  676. }
  677.  
  678. return $query;
  679. }
  680.  
  681. }
  682.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement