rename theme day
This commit is contained in:
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user