| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) |
| 4 |
exit; |
| 5 |
|
| 6 |
class berqPolylang extends berqIntegrations |
| 7 |
{ |
| 8 |
function __construct() |
| 9 |
{ |
| 10 |
add_filter('berqwp_page_translation_urls', [$this, 'get_page_translations'], 10, 2); |
| 11 |
add_filter('berqwp_site_url', [$this, 'set_home_url'], 10); |
| 12 |
} |
| 13 |
|
| 14 |
function set_home_url($home_url) { |
| 15 |
if (function_exists('pll_home_url')) { |
| 16 |
return pll_home_url(); |
| 17 |
} |
| 18 |
return $home_url; |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
function get_page_translations($translated_urls, $post_url) |
| 23 |
{ |
| 24 |
// 1. Skip if Polylang isn’t active. |
| 25 |
if (!function_exists('pll_get_post')) { |
| 26 |
return $translated_urls; |
| 27 |
} |
| 28 |
|
| 29 |
// 2. Resolve the post ID from the passed-in URL. |
| 30 |
$post_id = url_to_postid($post_url); |
| 31 |
if (!$post_id) { |
| 32 |
return $translated_urls; // not a singular URL → nothing to add |
| 33 |
} |
| 34 |
|
| 35 |
// 3. Loop through every enabled language and get the translation ID. |
| 36 |
foreach (pll_languages_list() as $lang) { |
| 37 |
$tr_id = pll_get_post($post_id, $lang); |
| 38 |
|
| 39 |
// If a translation exists, push its permalink (numeric index). |
| 40 |
if ($tr_id) { |
| 41 |
$translated_urls[] = get_permalink($tr_id); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
return $translated_urls; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
new berqPolylang(); |
| 52 |
|