diff --git a/filters/fix_agroexpert_links.php b/filters/fix_agroexpert_links.php index 1ab5149..7fbedfd 100644 --- a/filters/fix_agroexpert_links.php +++ b/filters/fix_agroexpert_links.php @@ -4,21 +4,48 @@ function fix_agroexpert_links($content) { $needle = 'https://agroexpert.press'; $position = 0; + $excluded_extensions = ['.jpg', '.jpeg', '.png', '.gif', '.svg', '.webp', '.pdf', '.doc', '.docx', '.xls', '.xlsx', '.zip', '.rar', '.mp3', '.mp4']; + while (($pos = strpos($content, $needle, $position)) !== false) { - // Найдём конец ссылки (до пробела, кавычки или закрывающего тега) $end = $pos + strlen($needle); while (isset($content[$end]) && !in_array($content[$end], ['"', "'", ' ', "\n", "\r", '<', '>'])) { $end++; } - // Вырежем саму ссылку $link = substr($content, $pos, $end - $pos); + $lower_link = strtolower($link); - // Если не заканчивается на / и нет query/anchor - if (!str_ends_with($link, '/') && !str_contains($link, '?') && !str_contains($link, '#')) { - $link_fixed = $link . '/'; + // Проверка на параметры или якорь + $has_query_or_anchor = str_contains($link, '?') || str_contains($link, '#'); + + // Получаем URL без параметров и якоря + $link_base = strtok($link, '?#'); + + // Исключаем медиафайлы по расширению + foreach ($excluded_extensions as $ext) { + if (str_ends_with($link_base, $ext)) { + $position = $end; + continue 2; + } + } + + // Если есть ? и перед ним нет / — вставляем / перед ? + if (str_contains($link, '?')) { + $q_pos = strpos($link, '?'); + if ($q_pos > 0 && $link[$q_pos - 1] !== '/') { + $link_fixed = substr($link, 0, $q_pos) . '/' . substr($link, $q_pos); + $content = substr_replace($content, $link_fixed, $pos, strlen($link)); + $position = $pos + strlen($link_fixed); + continue; + } + } + + // Если нет / на конце базового пути и нет параметров/якоря — добавляем / + if (!str_ends_with($link_base, '/')) { + $suffix = substr($link, strlen($link_base)); // ?params или #anchor + $link_fixed = $link_base . '/' . $suffix; $content = substr_replace($content, $link_fixed, $pos, strlen($link)); - $position = $pos + strlen($link_fixed); // Продвигаемся дальше + $position = $pos + strlen($link_fixed); } else { $position = $end; } @@ -26,5 +53,3 @@ function fix_agroexpert_links($content) { return $content; } - -add_filter('the_content', 'fix_agroexpert_links'); diff --git a/functions.php b/functions.php index 1ba7018..5c336f2 100644 --- a/functions.php +++ b/functions.php @@ -30,10 +30,6 @@ function redirect_non_admin_users() { } add_action('admin_init', 'redirect_non_admin_users'); - - require_once get_template_directory() . '/filters/fix_agroexpert_links.php'; - - define('ENPART', 740); define('EN_PARTS', '746,741,742,743,744,745'); diff --git a/partials/pinned-post-mob.php b/partials/pinned-post-mob.php index 6723ab1..53ed9b4 100644 --- a/partials/pinned-post-mob.php +++ b/partials/pinned-post-mob.php @@ -6,7 +6,7 @@ if ($pinned_post): $mainpost_id = $pinned_post->ID; ?> -
Главное
+
Тема дня
diff --git a/partials/pinned-post.php b/partials/pinned-post.php index 4f211fa..4ca1acf 100644 --- a/partials/pinned-post.php +++ b/partials/pinned-post.php @@ -10,7 +10,7 @@ if ($pinned_post) :
ID );?>
-

Главное

+

Тема дня

ID); ?>