if (!function_exists('reiss_ml_get_request_path')) {
function reiss_ml_get_request_path() {
$request_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '/';
$path = wp_parse_url($request_uri, PHP_URL_PATH);
if (!is_string($path) || $path === '') {
$path = '/';
}
return $path;
}
}
if (!function_exists('reiss_ml_get_supported_languages')) {
function reiss_ml_get_supported_languages() {
return array(
'en', 'af', 'sq', 'am', 'ar', 'hy', 'az', 'eu', 'be', 'bn', 'bs', 'bg', 'ca', 'ceb', 'ny',
'co', 'hr', 'cs', 'da', 'nl', 'eo', 'et', 'tl', 'fi', 'fr', 'fy', 'gl', 'ka', 'de', 'el',
'gu', 'ht', 'ha', 'haw', 'iw', 'he', 'hi', 'hmn', 'hu', 'is', 'ig', 'id', 'ga', 'it', 'ja',
'jw', 'kn', 'kk', 'km', 'ko', 'ku', 'ky', 'lo', 'la', 'lv', 'lt', 'lb', 'mk', 'mg', 'ms',
'ml', 'mt', 'mi', 'mr', 'mn', 'my', 'ne', 'no', 'ps', 'fa', 'pl', 'pt', 'pa', 'ro', 'ru',
'sm', 'gd', 'sr', 'st', 'sn', 'sd', 'si', 'sk', 'sl', 'so', 'es', 'su', 'sw', 'sv', 'tg',
'ta', 'te', 'th', 'tr', 'uk', 'ur', 'uz', 'vi', 'cy', 'xh', 'yi', 'yo', 'zu'
);
}
}
if (!function_exists('reiss_ml_normalize_lang')) {
function reiss_ml_normalize_lang($lang) {
$lang = strtolower(trim((string) $lang));
if ($lang === 'he') {
return 'iw';
}
return $lang;
}
}
if (!function_exists('reiss_ml_get_current_language')) {
function reiss_ml_get_current_language() {
static $current_lang = null;
if ($current_lang !== null) {
return $current_lang;
}
$supported = reiss_ml_get_supported_languages();
if (!empty($_GET['lang'])) {
$lang = reiss_ml_normalize_lang($_GET['lang']);
if (in_array($lang, $supported, true)) {
$current_lang = $lang;
return $current_lang;
}
}
$path = trim(reiss_ml_get_request_path(), '/');
if ($path !== '') {
$segments = explode('/', $path);
$first = reiss_ml_normalize_lang($segments[0]);
if (in_array($first, $supported, true)) {
$current_lang = $first;
return $current_lang;
}
}
if (!empty($_SERVER['HTTP_X_GT_LANG'])) {
$lang = reiss_ml_normalize_lang($_SERVER['HTTP_X_GT_LANG']);
if (in_array($lang, $supported, true)) {
$current_lang = $lang;
return $current_lang;
}
}
if (!empty($_COOKIE['googtrans'])) {
$cookie = trim((string) $_COOKIE['googtrans']);
if (preg_match('~^/?[a-zA-Z_-]+/([a-zA-Z_-]+)$~', $cookie, $m)) {
$lang = reiss_ml_normalize_lang($m[1]);
if (in_array($lang, $supported, true)) {
$current_lang = $lang;
return $current_lang;
}
}
}
$current_lang = 'en';
return $current_lang;
}
}
if (!function_exists('reiss_ml_strip_language_from_path')) {
function reiss_ml_strip_language_from_path($path = '') {
if ($path === '') {
$path = reiss_ml_get_request_path();
}
$path = '/' . ltrim((string) $path, '/');
$supported = reiss_ml_get_supported_languages();
$trimmed = trim($path, '/');
if ($trimmed === '') {
return '/';
}
$segments = explode('/', $trimmed);
$first = reiss_ml_normalize_lang($segments[0]);
if (in_array($first, $supported, true)) {
array_shift($segments);
}
$new_path = '/' . implode('/', $segments);
$new_path = preg_replace('#/+#', '/', $new_path);
if ($new_path === '') {
$new_path = '/';
}
return $new_path;
}
}
if (!function_exists('reiss_ml_get_current_language_path')) {
function reiss_ml_get_current_language_path() {
$lang = reiss_ml_get_current_language();
$base_path = reiss_ml_strip_language_from_path();
if ($lang === 'en') {
return $base_path;
}
if ($base_path === '/') {
return '/' . $lang . '/';
}
return '/' . $lang . $base_path;
}
}
if (!function_exists('reiss_ml_trailingslashit_url')) {
function reiss_ml_trailingslashit_url($url) {
$parts = wp_parse_url($url);
if (!$parts || empty($parts['scheme']) || empty($parts['host'])) {
return $url;
}
$path = isset($parts['path']) ? $parts['path'] : '/';
$path = user_trailingslashit($path);
$rebuilt = $parts['scheme'] . '://' . $parts['host'];
if (!empty($parts['port'])) {
$rebuilt .= ':' . $parts['port'];
}
$rebuilt .= $path;
if (!empty($parts['query'])) {
$rebuilt .= '?' . $parts['query'];
}
if (!empty($parts['fragment'])) {
$rebuilt .= '#' . $parts['fragment'];
}
return $rebuilt;
}
}
if (!function_exists('reiss_ml_get_current_language_url')) {
function reiss_ml_get_current_language_url($path = null) {
if ($path === null) {
$path = reiss_ml_get_current_language_path();
}
$home = home_url('/');
$url = home_url($path);
return reiss_ml_trailingslashit_url($url);
}
}
if (!function_exists('reiss_ml_get_language_home_url')) {
function reiss_ml_get_language_home_url($lang) {
$lang = reiss_ml_normalize_lang($lang);
if ($lang === 'en') {
return reiss_ml_trailingslashit_url(home_url('/'));
}
return reiss_ml_trailingslashit_url(home_url('/' . $lang . '/'));
}
}
if (!function_exists('reiss_ml_get_language_url_for_current_page')) {
function reiss_ml_get_language_url_for_current_page($lang) {
$lang = reiss_ml_normalize_lang($lang);
$base_path = reiss_ml_strip_language_from_path();
if ($lang === 'en') {
return reiss_ml_trailingslashit_url(home_url($base_path));
}
if ($base_path === '/') {
return reiss_ml_trailingslashit_url(home_url('/' . $lang . '/'));
}
return reiss_ml_trailingslashit_url(home_url('/' . $lang . $base_path));
}
}
if (!function_exists('reiss_ml_get_locale_map')) {
function reiss_ml_get_locale_map() {
return array(
'en' => 'en_US',
'af' => 'af_ZA',
'sq' => 'sq_AL',
'am' => 'am_ET',
'ar' => 'ar_SA',
'hy' => 'hy_AM',
'az' => 'az_AZ',
'eu' => 'eu_ES',
'be' => 'be_BY',
'bn' => 'bn_BD',
'bs' => 'bs_BA',
'bg' => 'bg_BG',
'ca' => 'ca_ES',
'ceb' => 'ceb_PH',
'ny' => 'ny_MW',
'co' => 'co_FR',
'hr' => 'hr_HR',
'cs' => 'cs_CZ',
'da' => 'da_DK',
'nl' => 'nl_NL',
'eo' => 'eo_001',
'et' => 'et_EE',
'tl' => 'tl_PH',
'fi' => 'fi_FI',
'fr' => 'fr_FR',
'fy' => 'fy_NL',
'gl' => 'gl_ES',
'ka' => 'ka_GE',
'de' => 'de_DE',
'el' => 'el_GR',
'gu' => 'gu_IN',
'ht' => 'ht_HT',
'ha' => 'ha_NG',
'haw' => 'haw_US',
'iw' => 'he_IL',
'he' => 'he_IL',
'hi' => 'hi_IN',
'hmn' => 'hmn_001',
'hu' => 'hu_HU',
'is' => 'is_IS',
'ig' => 'ig_NG',
'id' => 'id_ID',
'ga' => 'ga_IE',
'it' => 'it_IT',
'ja' => 'ja_JP',
'jw' => 'jv_ID',
'kn' => 'kn_IN',
'kk' => 'kk_KZ',
'km' => 'km_KH',
'ko' => 'ko_KR',
'ku' => 'ku_TR',
'ky' => 'ky_KG',
'lo' => 'lo_LA',
'la' => 'la_VA',
'lv' => 'lv_LV',
'lt' => 'lt_LT',
'lb' => 'lb_LU',
'mk' => 'mk_MK',
'mg' => 'mg_MG',
'ms' => 'ms_MY',
'ml' => 'ml_IN',
'mt' => 'mt_MT',
'mi' => 'mi_NZ',
'mr' => 'mr_IN',
'mn' => 'mn_MN',
'my' => 'my_MM',
'ne' => 'ne_NP',
'no' => 'no_NO',
'ps' => 'ps_AF',
'fa' => 'fa_IR',
'pl' => 'pl_PL',
'pt' => 'pt_PT',
'pa' => 'pa_IN',
'ro' => 'ro_RO',
'ru' => 'ru_RU',
'sm' => 'sm_WS',
'gd' => 'gd_GB',
'sr' => 'sr_RS',
'st' => 'st_LS',
'sn' => 'sn_ZW',
'sd' => 'sd_PK',
'si' => 'si_LK',
'sk' => 'sk_SK',
'sl' => 'sl_SI',
'so' => 'so_SO',
'es' => 'es_ES',
'su' => 'su_ID',
'sw' => 'sw_TZ',
'sv' => 'sv_SE',
'tg' => 'tg_TJ',
'ta' => 'ta_IN',
'te' => 'te_IN',
'th' => 'th_TH',
'tr' => 'tr_TR',
'uk' => 'uk_UA',
'ur' => 'ur_PK',
'uz' => 'uz_UZ',
'vi' => 'vi_VN',
'cy' => 'cy_GB',
'xh' => 'xh_ZA',
'yi' => 'yi_001',
'yo' => 'yo_NG',
'zu' => 'zu_ZA',
);
}
}
if (!function_exists('reiss_ml_get_current_locale')) {
function reiss_ml_get_current_locale() {
$lang = reiss_ml_get_current_language();
$map = reiss_ml_get_locale_map();
if (isset($map[$lang])) {
return $map[$lang];
}
return 'en_US';
}
}
if (!function_exists('reiss_ml_replace_home_url_prefix')) {
function reiss_ml_replace_home_url_prefix($value) {
if (!is_string($value) || $value === '') {
return $value;
}
$lang = reiss_ml_get_current_language();
if ($lang === 'en') {
return $value;
}
$home_en = reiss_ml_trailingslashit_url(home_url('/'));
$home_ml = reiss_ml_get_language_home_url($lang);
$variants = array(
untrailingslashit($home_en),
$home_en
);
foreach ($variants as $variant) {
if ($variant !== '' && strpos($value, $variant) === 0) {
$suffix = substr($value, strlen($variant));
$suffix = ltrim($suffix, '/');
$rebuilt = untrailingslashit($home_ml);
if ($suffix !== '') {
$rebuilt .= '/' . $suffix;
}
return $rebuilt;
}
}
return $value;
}
}
if (!function_exists('reiss_ml_replace_schema_urls_recursive')) {
function reiss_ml_replace_schema_urls_recursive($data) {
if (is_array($data)) {
foreach ($data as $key => $value) {
if (is_array($value)) {
$data[$key] = reiss_ml_replace_schema_urls_recursive($value);
continue;
}
if (is_string($value)) {
if (in_array($key, array('@id', 'url', 'item', 'contentUrl', 'embedUrl'), true)) {
$data[$key] = reiss_ml_replace_home_url_prefix($value);
} elseif ($key === 'target') {
$data[$key] = reiss_ml_replace_home_url_prefix($value);
}
}
}
}
return $data;
}
}
if (!function_exists('reiss_ml_filter_wpseo_locale')) {
function reiss_ml_filter_wpseo_locale($locale) {
return reiss_ml_get_current_locale();
}
}
add_filter('wpseo_locale', 'reiss_ml_filter_wpseo_locale', 999);
if (!function_exists('reiss_ml_filter_og_locale')) {
function reiss_ml_filter_og_locale($locale) {
return reiss_ml_get_current_locale();
}
}
add_filter('wpseo_opengraph_locale', 'reiss_ml_filter_og_locale', 999);
if (!function_exists('reiss_ml_filter_canonical')) {
function reiss_ml_filter_canonical($canonical) {
return reiss_ml_get_current_language_url();
}
}
add_filter('wpseo_canonical', 'reiss_ml_filter_canonical', 999);
if (!function_exists('reiss_ml_filter_opengraph_url')) {
function reiss_ml_filter_opengraph_url($url) {
return reiss_ml_get_current_language_url();
}
}
add_filter('wpseo_opengraph_url', 'reiss_ml_filter_opengraph_url', 999);
if (!function_exists('reiss_ml_filter_schema_graph')) {
function reiss_ml_filter_schema_graph($graph, $context) {
if (empty($graph) || !is_array($graph)) {
return $graph;
}
$lang = reiss_ml_get_current_language();
$locale = reiss_ml_get_current_locale();
foreach ($graph as $index => $piece) {
if (!is_array($piece)) {
continue;
}
$graph[$index] = reiss_ml_replace_schema_urls_recursive($piece);
if (isset($graph[$index]['@type'])) {
$types = $graph[$index]['@type'];
if (!is_array($types)) {
$types = array($types);
}
if (in_array('WebPage', $types, true)) {
$graph[$index]['@id'] = reiss_ml_get_current_language_url() . '#webpage';
$graph[$index]['url'] = reiss_ml_get_current_language_url();
if (isset($graph[$index]['breadcrumb']['@id'])) {
$graph[$index]['breadcrumb']['@id'] = reiss_ml_get_current_language_url() . '#breadcrumb';
}
if (isset($graph[$index]['primaryImageOfPage']['@id'])) {
$graph[$index]['primaryImageOfPage']['@id'] = reiss_ml_get_current_language_url() . '#primaryimage';
}
if (isset($graph[$index]['image']['@id'])) {
$graph[$index]['image']['@id'] = reiss_ml_get_current_language_url() . '#primaryimage';
}
if (isset($graph[$index]['potentialAction']) && is_array($graph[$index]['potentialAction'])) {
foreach ($graph[$index]['potentialAction'] as $a_index => $action) {
if (isset($action['target'])) {
if (is_array($action['target'])) {
$graph[$index]['potentialAction'][$a_index]['target'] = array(reiss_ml_get_current_language_url());
} else {
$graph[$index]['potentialAction'][$a_index]['target'] = reiss_ml_get_current_language_url();
}
}
}
}
$graph[$index]['inLanguage'] = ($lang === 'en') ? 'en-US' : $lang;
}
if (in_array('BreadcrumbList', $types, true)) {
$graph[$index]['@id'] = reiss_ml_get_current_language_url() . '#breadcrumb';
}
if (in_array('WebSite', $types, true)) {
$graph[$index]['url'] = reiss_ml_get_language_home_url($lang);
$graph[$index]['inLanguage'] = ($lang === 'en') ? 'en-US' : $lang;
if (isset($graph[$index]['potentialAction']) && is_array($graph[$index]['potentialAction'])) {
foreach ($graph[$index]['potentialAction'] as $pa_index => $action) {
if (isset($action['target']['urlTemplate'])) {
$graph[$index]['potentialAction'][$pa_index]['target']['urlTemplate'] = reiss_ml_get_language_home_url($lang) . '?s={search_term_string}';
}
}
}
}
if (in_array('ImageObject', $types, true)) {
if (isset($graph[$index]['@id']) && strpos($graph[$index]['@id'], '#primaryimage') !== false) {
$graph[$index]['@id'] = reiss_ml_get_current_language_url() . '#primaryimage';
}
if (isset($graph[$index]['@id']) && strpos($graph[$index]['@id'], '#local-main-organization-logo') !== false) {
$graph[$index]['@id'] = reiss_ml_get_current_language_url() . '#local-main-organization-logo';
}
$graph[$index]['inLanguage'] = $locale;
}
if (in_array('Organization', $types, true) || in_array('Place', $types, true)) {
if (isset($graph[$index]['logo']['@id'])) {
$graph[$index]['logo']['@id'] = reiss_ml_get_current_language_url() . '#local-main-organization-logo';
}
if (isset($graph[$index]['image']['@id'])) {
$graph[$index]['image']['@id'] = reiss_ml_get_current_language_url() . '#local-main-organization-logo';
}
}
}
}
return $graph;
}
}
add_filter('wpseo_schema_graph', 'reiss_ml_filter_schema_graph', 999, 2);
if (!function_exists('reiss_ml_output_hreflang_links')) {
function reiss_ml_output_hreflang_links() {
if (is_admin()) {
return;
}
$languages = reiss_ml_get_supported_languages();
$path = reiss_ml_strip_language_from_path();
foreach ($languages as $lang) {
$href = reiss_ml_get_language_url_for_current_page($lang);
echo '' . "\n";
}
echo '' . "\n";
}
}
add_action('wp_head', 'reiss_ml_output_hreflang_links', 1);
if (!function_exists('reiss_ml_buffer_fix_og_locale')) {
function reiss_ml_buffer_fix_og_locale($html) {
if (!is_string($html) || $html === '') {
return $html;
}
$locale = reiss_ml_get_current_locale();
$html = preg_replace(
'~~i',
'',
$html,
1
);
if (!preg_match('~~i',
' ' . "\n",
$html,
1
);
}
return $html;
}
}
if (!function_exists('reiss_ml_start_buffer')) {
function reiss_ml_start_buffer() {
if (is_admin()) {
return;
}
ob_start('reiss_ml_buffer_fix_og_locale');
}
}
add_action('template_redirect', 'reiss_ml_start_buffer', 0);