correct logic rfo

This commit is contained in:
argoexpert press
2025-06-18 18:12:37 +03:00
parent 740d2267a3
commit cd0c2b9f97
10 changed files with 269 additions and 35 deletions

View File

@@ -219,7 +219,7 @@ function custom_wpseo_breadcrumb_output($output)
function custom_pagination($query = null)
function custom_pagination($query = null, $class = "pagination")
{
global $wp_query;
$query = $query ?: $wp_query;
@@ -241,7 +241,7 @@ function custom_pagination($query = null)
$txt['next'] = 'Далее';
}
echo '<div class="pagination"><div class="pagination__list">';
echo '<div class="'.$class.'"><div class="pagination__list">';
// Кнопка "Назад"
if ($current_page > 1) {
@@ -601,6 +601,41 @@ function get_category_name($post = null)
return $category_name;
}
/**
* Получает полный URL категории (с учётом иерархии)
*
* @param int|WP_Post|null $post ID поста, объект или null (текущий пост)
* @return string Полный URL категории (включая родительские) или URL архива по умолчанию
*/
function get_category_url($post = null) {
// Получаем все категории поста
$categories = get_the_category($post);
if (empty($categories) || is_wp_error($categories)) {
return '';
}
// Фильтруем категории, исключая слаг 'all-events'
$filtered_categories = array_filter($categories, function ($category) {
return $category instanceof WP_Term && $category->slug !== 'all-events';
});
if (!empty($filtered_categories)) {
// Берём первую подходящую категорию
$first_category = reset($filtered_categories);
// Получаем ссылку по всем правилам WordPress
$category_url = get_category_link($first_category->term_id);
// Возвращаем безопасный URL
return esc_url($category_url);
}
return '';
}
function get_category_name_en($post = null)
{
$categories = get_the_category($post);
@@ -1273,6 +1308,37 @@ function replace_first_figure_in_content($content) {
return $content;
}
function delete_first_figure_in_content($content) {
global $post;
if (!isset($post->ID)) {
return $content;
}
$startPos = strpos($content, '<figure');
if ($startPos === false) {
return $content; // Если <figure> не найдено, возвращаем исходный контент
}
$endPos = strpos($content, '</figure>', $startPos);
if ($endPos === false) {
return $content; // Если </figure> не найдено, возвращаем исходный контент
}
// Рассчитываем длину части, которую нужно заменить
$endPos += strlen('</figure>'); // Двигаем указатель на конец тега </figure>
$figureHtml = substr($content, $startPos, $endPos - $startPos);
// Заменяем найденный кусок на новый HTML
$newContent = substr_replace($content, '', $startPos, $endPos - $startPos);
return $newContent;
}
function add_cookie_consent() {
if (!isset($_COOKIE['cookie_consent_accepted'])) {
@@ -1303,6 +1369,55 @@ add_filter('rest_category_query', function($args, $request) {
}, 10, 2);
/**
* Рассчитывает время чтения поста (в минутах) со склонением слова "минута"
*
* @param int|null $post_id ID поста. Если null, используется текущий пост в цикле.
* @param bool $include_text Нужно ли добавлять "мин." или полный текст (по умолчанию true)
* @return string Пример: "5 минут" или "1 минута"
*/
function calculate_reading_time($post_id = null, $include_text = true) {
// Если ID поста не передан, пытаемся определить текущий пост
if ($post_id === null) {
global $post;
if (!isset($post) || !is_a($post, 'WP_Post')) {
return $include_text ? '0 минут' : '0';
}
$post_id = $post->ID;
}
$content = get_post_field('post_content', $post_id);
if (empty($content)) {
return $include_text ? '0 минут' : '0';
}
$word_count = str_word_count(strip_tags($content));
//$reading_speed = apply_filters('reading_time_speed', 200); // Фильтр для изменения скорости
$reading_speed = 200;
$reading_time = ceil($word_count / $reading_speed);
$reading_time = max(1, $reading_time); // Минимум 1 минута
// Склонение слова "минута"
$minutes_text = 'минут';
$last_digit = $reading_time % 10;
$last_two_digits = $reading_time % 100;
if ($last_digit == 1 && $last_two_digits != 11) {
$minutes_text = 'минута';
} elseif (($last_digit >= 2 && $last_digit <= 4) && !($last_two_digits >= 12 && $last_two_digits <= 14)) {
$minutes_text = 'минуты';
}
return $include_text
? sprintf('%d %s', $reading_time, $minutes_text)
: $reading_time;
}